Skip to content

Commit

Permalink
add service started and service stopped time
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto committed Dec 15, 2023
1 parent cb63301 commit c852438
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package org.eclipse.xpanse.modules.database.service;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
Expand All @@ -19,6 +20,7 @@
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand All @@ -36,6 +38,7 @@
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

/**
* DeployServiceEntity for persistence.
Expand Down Expand Up @@ -140,4 +143,15 @@ public class DeployServiceEntity extends CreateModifiedTime {

@Column(name = "RESULT_MESSAGE", length = Integer.MAX_VALUE)
private String resultMessage;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss XXX")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss XXX")
@Column(name = "LAST_STARTED_AT")
private OffsetDateTime lastStartedAt;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss XXX")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss XXX")
@Column(name = "LAST_STOPPED_AT")
private OffsetDateTime lastStoppedAt;

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package org.eclipse.xpanse.modules.database.service;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -39,6 +40,8 @@ class DeployServiceEntityTest {
private final Map<String, String> PROPERTIES = new HashMap<>();
private final Map<String, String> PRIVATE_PROPERTIES = new HashMap<>();
private final String RESULT_MESSAGE = "RESULT_MESSAGE";
private static final OffsetDateTime LAST_STARTED_AT = OffsetDateTime.now();
private static final OffsetDateTime LAST_STOPPED_AT = OffsetDateTime.now();


private DeployServiceEntity test;
Expand All @@ -62,6 +65,8 @@ void setUp() {
test.setProperties(PROPERTIES);
test.setPrivateProperties(PRIVATE_PROPERTIES);
test.setResultMessage(RESULT_MESSAGE);
test.setLastStartedAt(LAST_STARTED_AT);
test.setLastStoppedAt(LAST_STOPPED_AT);
}

@Test
Expand All @@ -82,7 +87,9 @@ void testToString() {
//+ "deployResourceList=" + DEPLOY_RESOURCE_LIST+ ", "
+ "properties=" + PROPERTIES + ", "
+ "privateProperties=" + PRIVATE_PROPERTIES + ", "
+ "resultMessage=" + RESULT_MESSAGE + ")";
+ "resultMessage=" + RESULT_MESSAGE + ", "
+ "lastStartedAt=" + LAST_STARTED_AT + ", "
+ "lastStoppedAt=" + LAST_STOPPED_AT + ")";
Assertions.assertEquals(expectedToString, test.toString());
}

Expand Down Expand Up @@ -205,6 +212,20 @@ void testEqualsAndHashCode() {
Assertions.assertNotEquals(test1.hashCode(), test2.hashCode());

test1.setResultMessage(RESULT_MESSAGE);
Assertions.assertNotEquals(test, test1);
Assertions.assertNotEquals(test, test2);
Assertions.assertNotEquals(test1, test2);
Assertions.assertNotEquals(test.hashCode(), test1.hashCode());
Assertions.assertNotEquals(test1.hashCode(), test2.hashCode());

test1.setLastStartedAt(LAST_STARTED_AT);
Assertions.assertNotEquals(test, test1);
Assertions.assertNotEquals(test, test2);
Assertions.assertNotEquals(test1, test2);
Assertions.assertNotEquals(test.hashCode(), test1.hashCode());
Assertions.assertNotEquals(test1.hashCode(), test2.hashCode());

test1.setLastStoppedAt(LAST_STOPPED_AT);
Assertions.assertEquals(test, test1);
Assertions.assertNotEquals(test, test2);
Assertions.assertNotEquals(test1, test2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ public DeployedService startService(UUID id) {
deployServiceEntity.setServiceState(ServiceState.STARTING);
deployServiceStorage.storeAndFlush(deployServiceEntity);
if (start(deployServiceEntity)) {
deployServiceEntity.setLastStartedAt(OffsetDateTime.now());
deployServiceEntity.setServiceState(ServiceState.RUNNING);
} else {
deployServiceEntity.setServiceState(ServiceState.STARTING_FAILED);
Expand Down Expand Up @@ -972,6 +973,7 @@ public DeployedService stopService(UUID id) {
deployServiceEntity.setServiceState(ServiceState.STOPPING);
deployServiceStorage.storeAndFlush(deployServiceEntity);
if (stop(deployServiceEntity)) {
deployServiceEntity.setLastStoppedAt(OffsetDateTime.now());
deployServiceEntity.setServiceState(ServiceState.STOPPED);
} else {
deployServiceEntity.setServiceState(ServiceState.STOPPING_FAILED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,14 @@ public class DeployedService {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss XXX")
@JsonSerialize(using = OffsetDateTimeSerializer.class)
private OffsetDateTime lastModifiedTime;

@Schema(description = "Time of start service.")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss XXX")
@JsonSerialize(using = OffsetDateTimeSerializer.class)
private OffsetDateTime lastStartedAt;

@Schema(description = "Time of stop service.")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss XXX")
@JsonSerialize(using = OffsetDateTimeSerializer.class)
private OffsetDateTime lastStoppedAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class DeployedServiceTest {
private static final OffsetDateTime createTime = OffsetDateTime.now();
private static final OffsetDateTime lastModifiedTime = OffsetDateTime.now();
private static DeployedService deployedService;
private static final OffsetDateTime LAST_STARTED_AT = OffsetDateTime.now();
private static final OffsetDateTime LAST_STOPPED_AT = OffsetDateTime.now();

@BeforeEach
void setUp() {
Expand All @@ -54,6 +56,8 @@ void setUp() {
deployedService.setCreateTime(createTime);
deployedService.setLastModifiedTime(lastModifiedTime);
deployedService.setServiceHostingType(ServiceHostingType.SERVICE_VENDOR);
deployedService.setLastStartedAt(LAST_STARTED_AT);
deployedService.setLastStoppedAt(LAST_STOPPED_AT);
}

@Test
Expand All @@ -68,6 +72,8 @@ void testGetterAndSetter() {
assertEquals(serviceDeploymentState, deployedService.getServiceDeploymentState());
assertEquals(createTime, deployedService.getCreateTime());
assertEquals(lastModifiedTime, deployedService.getLastModifiedTime());
assertEquals(LAST_STARTED_AT, deployedService.getLastStartedAt());
assertEquals(LAST_STOPPED_AT, deployedService.getLastStoppedAt());
}

@Test
Expand Down Expand Up @@ -149,6 +155,20 @@ public void testEqualsAndHashCode() {

deployedService1.setLastModifiedTime(lastModifiedTime);
deployedService1.setServiceHostingType(ServiceHostingType.SERVICE_VENDOR);
assertNotEquals(deployedService, deployedService1);
assertNotEquals(deployedService1, deployedService2);
assertNotEquals(deployedService.hashCode(), deployedService1.hashCode());
assertNotEquals(deployedService1.hashCode(), deployedService2.hashCode());

deployedService1.setLastStartedAt(LAST_STARTED_AT);
deployedService1.setServiceHostingType(ServiceHostingType.SERVICE_VENDOR);
assertNotEquals(deployedService, deployedService1);
assertNotEquals(deployedService1, deployedService2);
assertNotEquals(deployedService.hashCode(), deployedService1.hashCode());
assertNotEquals(deployedService1.hashCode(), deployedService2.hashCode());

deployedService1.setLastStoppedAt(LAST_STOPPED_AT);
deployedService1.setServiceHostingType(ServiceHostingType.SERVICE_VENDOR);
assertEquals(deployedService, deployedService1);
assertNotEquals(deployedService1, deployedService2);
assertEquals(deployedService.hashCode(), deployedService1.hashCode());
Expand All @@ -170,6 +190,8 @@ void testToString() {
", serviceHostingType=" + ServiceHostingType.SERVICE_VENDOR +
", createTime=" + createTime +
", lastModifiedTime=" + lastModifiedTime +
", lastStartedAt=" + LAST_STARTED_AT +
", lastStoppedAt=" + LAST_STOPPED_AT +
")";
assertEquals(expectedString, deployedService.toString());
}
Expand Down

0 comments on commit c852438

Please sign in to comment.