diff options
author | Sébastien Determe <sebastien.determe@intl.att.com> | 2020-01-16 13:38:31 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-01-16 13:38:31 +0000 |
commit | 5dfa4b58906921dea2aab38d20d761f460f49964 (patch) | |
tree | 693e297621d9b114f2d648ef13b8fdfa599f6e90 /src/test/java/org | |
parent | 8c2ce6065ddd8405ef3faa99969453e080b63f55 (diff) | |
parent | 825612628f130b394f9ee1aa1ad2cca41b67bc7f (diff) |
Merge "Add template and tosca model entities and repositories"
Diffstat (limited to 'src/test/java/org')
8 files changed, 437 insertions, 93 deletions
diff --git a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java index df952aa16..2ebea7b17 100644 --- a/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java +++ b/src/test/java/org/onap/clamp/loop/CsarInstallerItCase.java @@ -63,7 +63,7 @@ import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.Rollback; +import org.springframework.test.annotation.Commit; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @@ -183,7 +183,7 @@ public class CsarInstallerItCase { @Test @Transactional - @Rollback(value = false) + @Commit public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException, JSONException, InterruptedException { String generatedName = RandomStringUtils.randomAlphanumeric(5); @@ -209,7 +209,7 @@ public class CsarInstallerItCase { assertThat(loop.getOperationalPolicies()).hasSize(1); assertThat(loop.getModelService().getServiceUuid()).isEqualTo("63cac700-ab9a-4115-a74f-7eac85e3fce0"); JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), - JsonUtils.GSON_JPA_MODEL.toJson(loop.getModelService()), true); + JsonUtils.GSON_JPA_MODEL.toJson(loop.getModelService()), true); JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/service-details.json"), JsonUtils.GSON_JPA_MODEL.toJson(loop.getModelService().getServiceDetails()), true); JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/resource-details.json"), diff --git a/src/test/java/org/onap/clamp/loop/LoopControllerTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopControllerTestItCase.java index 67ae985c7..a41b5c251 100644 --- a/src/test/java/org/onap/clamp/loop/LoopControllerTestItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopControllerTestItCase.java @@ -32,18 +32,15 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.util.Set; + import javax.transaction.Transactional; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; - import org.onap.clamp.clds.Application; import org.onap.clamp.clds.util.JsonUtils; - import org.onap.clamp.policy.microservice.MicroServicePolicy; -import org.onap.clamp.policy.microservice.MicroservicePolicyService; +import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -63,7 +60,7 @@ public class LoopControllerTestItCase { LoopsRepository loopsRepository; @Autowired - MicroservicePolicyService microServicePolicyService; + MicroServicePolicyService microServicePolicyService; @Autowired LoopController loopController; @@ -78,18 +75,10 @@ public class LoopControllerTestItCase { return new Loop(loopName, loopBlueprint, loopSvg); } - @Before - public void setUp() { - saveTestLoopToDb(); - } - - @After - public void tearDown() { - loopsRepository.deleteAll(); - } - @Test + @Transactional public void testUpdateOperationalPolicies() { + saveTestLoopToDb(); String policy = "[{\"name\":\"OPERATIONAL_CLholmes31_v1_0_vFW_PG_T10_k8s-holmes-rules\"," + "\"configurationsJson\":{\"guard_policies\":{}," + "\"operational_policy\":{\"controlLoop\":{\"trigger_policy\":\"unique-policy-id-1-modifyConfig\"," @@ -113,6 +102,7 @@ public class LoopControllerTestItCase { @Test @Transactional public void testUpdateGlobalProperties() { + saveTestLoopToDb(); String policy = "{\"dcaeDeployParameters\":{\"aaiEnrichmentHost\":\"aai.onap.svc.cluster.local\"," + "\"aaiEnrichmentPort\":\"8443\",\"enableAAIEnrichment\":\"false\",\"dmaap_host\":\"message-router" + ".onap\",\"dmaap_port\":\"3904\",\"enableRedisCaching\":\"false\",\"redisHosts\":\"dcae-redis.onap" @@ -134,9 +124,10 @@ public class LoopControllerTestItCase { @Test @Transactional public void testUpdateMicroservicePolicy() { + saveTestLoopToDb(); MicroServicePolicy policy = new MicroServicePolicy("policyName", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopController.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, policy); assertThat(microServicePolicyService.isExisting("policyName")).isTrue(); } @@ -144,7 +135,8 @@ public class LoopControllerTestItCase { @Test @Transactional public void testGetSvgRepresentation() { - String svgRepresentation = loopController.getSvgRepresentation(EXAMPLE_LOOP_NAME); + saveTestLoopToDb(); + String svgRepresentation = loopController.getSvgRepresentation(EXAMPLE_LOOP_NAME); assertThat(svgRepresentation).isEqualTo("representation"); } }
\ No newline at end of file diff --git a/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java index 57b2cef61..c172a9a07 100644 --- a/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopLogServiceTestItCase.java @@ -27,11 +27,11 @@ import static org.assertj.core.api.Assertions.assertThat; import com.google.gson.JsonObject; import java.util.Set; + import javax.transaction.Transactional; import org.junit.Test; import org.junit.runner.RunWith; - import org.onap.clamp.clds.Application; import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.loop.log.LogType; @@ -77,10 +77,10 @@ public class LoopLogServiceTestItCase { assertThat(loopLogs).hasSize(1); LoopLog loopLog = loopLogs.iterator().next(); assertThat(loopLog.getMessage()).isEqualTo(SAMPLE_LOG_MESSAGE); - loopsRepository.deleteAll(); } @Test + @Transactional public void testLoopLog() { LoopLog log = new LoopLog(); Long id = Long.valueOf(100); diff --git a/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java b/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java index 78e0d2e3d..44feaebd8 100644 --- a/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java @@ -40,8 +40,17 @@ import org.onap.clamp.clds.Application; import org.onap.clamp.loop.log.LogType; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.log.LoopLogRepository; +import org.onap.clamp.loop.service.Service; +import org.onap.clamp.loop.service.ServicesRepository; +import org.onap.clamp.loop.template.LoopTemplate; +import org.onap.clamp.loop.template.LoopTemplatesRepository; +import org.onap.clamp.loop.template.MicroServiceModel; +import org.onap.clamp.loop.template.MicroServiceModelsRepository; +import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelId; +import org.onap.clamp.loop.template.PolicyModelsRepository; import org.onap.clamp.policy.microservice.MicroServicePolicy; -import org.onap.clamp.policy.microservice.MicroservicePolicyService; +import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; import org.onap.clamp.policy.operational.OperationalPolicyService; import org.springframework.beans.factory.annotation.Autowired; @@ -59,7 +68,7 @@ public class LoopRepositoriesItCase { private LoopsRepository loopRepository; @Autowired - private MicroservicePolicyService microServicePolicyService; + private MicroServicePolicyService microServicePolicyService; @Autowired private OperationalPolicyService operationalPolicyService; @@ -67,12 +76,47 @@ public class LoopRepositoriesItCase { @Autowired private LoopLogRepository loopLogRepository; + @Autowired + private LoopTemplatesRepository loopTemplateRepository; + + @Autowired + private MicroServiceModelsRepository microServiceModelsRepository; + + @Autowired + private PolicyModelsRepository policyModelsRepository; + + @Autowired + private ServicesRepository servicesRepository; + + private Service getService(String serviceDetails, String resourceDetails) { + return new Service(serviceDetails, resourceDetails); + } + private OperationalPolicy getOperationalPolicy(String configJson, String name) { return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class)); } + private MicroServiceModel getMicroServiceModel(String yaml, String name, String policyType, String createdBy, + PolicyModel policyModel) { + MicroServiceModel model = new MicroServiceModel(name, policyType, yaml, policyModel); + return model; + } + + private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym, + String policyVariant, String createdBy) { + return new PolicyModel(policyType, policyModelTosca, version, policyAcronym, policyVariant); + } + + private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation, String createdBy, + Integer maxInstancesAllowed) { + LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null); + template.addMicroServiceModel(getMicroServiceModel("yaml", "microService1", "org.onap.policy.drools", createdBy, + getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1", createdBy))); + return template; + } + private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl, String dcaeBlueprintId) { + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { Loop loop = new Loop(); loop.setName(name); loop.setSvgRepresentation(svgRepresentation); @@ -82,13 +126,14 @@ public class LoopRepositoriesItCase { loop.setDcaeDeploymentId(dcaeId); loop.setDcaeDeploymentStatusUrl(dcaeUrl); loop.setDcaeBlueprintId(dcaeBlueprintId); + loop.setLoopTemplate(getLoopTemplate("templateName", "yaml", "svg", "toto", 1)); return loop; } private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation, - String policyTosca, String jsonProperties, boolean shared) { + String policyTosca, String jsonProperties, boolean shared) { MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared, - gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); + gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); return microService; } @@ -100,52 +145,108 @@ public class LoopRepositoriesItCase { @Test @Transactional public void crudTest() { + // Setup Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org", "UUID-blueprint"); + "123456789", "https://dcaetest.org", "UUID-blueprint"); OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); loopTest.addOperationalPolicy(opPolicy); MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "", - "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", - "{\"param1\":\"value1\"}", true); + "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0", + "{\"param1\":\"value1\"}", true); loopTest.addMicroServicePolicy(microServicePolicy); LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest); loopTest.addLog(loopLog); + Service service = getService( + "{\"name\": \"vLoadBalancerMS\", \"UUID\": \"63cac700-ab9a-4115-a74f-7eac85e3fce0\"}", "{\"CP\": {}}"); + loopTest.setModelService(service); - // Attemp to save into the database the entire loop + // Attempt to save into the database the entire loop Loop loopInDb = loopRepository.save(loopTest); assertThat(loopInDb).isNotNull(); + assertThat(loopRepository.findById(loopInDb.getName()).get()).isNotNull(); + assertThat(loopInDb.getCreatedDate()).isNotNull(); + assertThat(loopInDb.getUpdatedDate()).isNotNull(); + assertThat(loopInDb.getUpdatedDate()).isEqualTo(loopInDb.getCreatedDate()); assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest"); - // Now set the ID in the previous model so that we can compare the objects + // Autogen id so now set the ID in the previous model so that we can compare the + // objects loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId()); - assertThat(loopInDb).isEqualToIgnoringGivenFields(loopTest, "components"); + assertThat(loopInDb).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate", + "createdBy", "updatedBy"); assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true); assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(true); assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true); assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true); + assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true); + assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true); + assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true); + assertThat(microServiceModelsRepository.existsById( + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getName())) + .isEqualTo(true); + assertThat(policyModelsRepository.existsById(new PolicyModelId( + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel() + .getPolicyModelType(), + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel() + .getVersion()))).isEqualTo(true); // Now attempt to read from database Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get(); - assertThat(loopInDbRetrieved).isEqualToIgnoringGivenFields(loopTest, "components"); + assertThat(loopInDbRetrieved).isEqualToIgnoringGivenFields(loopTest, "components", "createdDate", "updatedDate", + "createdBy", "updatedBy"); + assertThat(loopInDbRetrieved).isEqualToComparingOnlyGivenFields(loopInDb, "createdDate", "updatedDate", + "createdBy", "updatedBy"); assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog); assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]) - .isEqualToComparingFieldByField(opPolicy); + .isEqualToComparingFieldByField(opPolicy); assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0]) - .isEqualToComparingFieldByField(microServicePolicy); + .isEqualToIgnoringGivenFields(microServicePolicy, "createdDate", "updatedDate", "createdBy", + "updatedBy"); // Attempt an update ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now()); - loopRepository.save(loopInDbRetrieved); - Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get(); + loopInDbRetrieved.setBlueprint("yaml2"); + Loop loopInDbRetrievedUpdated = loopRepository.saveAndFlush(loopInDbRetrieved); + // Loop loopInDbRetrievedUpdated = + // loopRepository.findById(loopTest.getName()).get(); + assertThat(loopInDbRetrievedUpdated.getBlueprint()).isEqualTo("yaml2"); assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0]) - .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); + .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); + // UpdatedDate should have been changed + assertThat(loopInDbRetrievedUpdated.getUpdatedDate()).isNotEqualTo(loopInDbRetrievedUpdated.getCreatedDate()); + // createdDate should have NOT been changed + assertThat(loopInDbRetrievedUpdated.getCreatedDate()).isEqualTo(loopInDb.getCreatedDate()); + // other audit are the same + assertThat(loopInDbRetrievedUpdated.getCreatedBy()).isEqualTo(""); + assertThat(loopInDbRetrievedUpdated.getUpdatedBy()).isEqualTo(""); // Attempt to delete the object and check it has well been cascaded + loopRepository.delete(loopInDbRetrieved); assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false); assertThat(operationalPolicyService.isExisting(opPolicy.getName())).isEqualTo(false); - assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(false); + assertThat(microServicePolicyService.isExisting(microServicePolicy.getName())).isEqualTo(true); assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false); + assertThat(loopTemplateRepository.existsById(loopInDb.getLoopTemplate().getName())).isEqualTo(true); + assertThat(servicesRepository.existsById(loopInDb.getModelService().getServiceUuid())).isEqualTo(true); + assertThat(microServiceModelsRepository.existsById( + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getName())) + .isEqualTo(true); + + assertThat(policyModelsRepository.existsById(new PolicyModelId( + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel() + .getPolicyModelType(), + loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel() + .getVersion()))).isEqualTo(true); + + // Cleanup + // microServiceModelsRepository + // .delete(loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel()); + // + // policyModelsRepository.delete( + // loopInDb.getLoopTemplate().getMicroServiceModelUsed().first().getMicroServiceModel().getPolicyModel()); + // loopTemplateRepository.delete(loopInDb.getLoopTemplate()); + // servicesRepository.delete(service); } } diff --git a/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java index 28a92e371..d19c8a808 100644 --- a/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java +++ b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java @@ -33,7 +33,6 @@ import java.util.stream.Collectors; import javax.transaction.Transactional; import org.assertj.core.util.Lists; -import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.clamp.clds.Application; @@ -42,7 +41,7 @@ import org.onap.clamp.loop.log.LogType; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.log.LoopLogService; import org.onap.clamp.policy.microservice.MicroServicePolicy; -import org.onap.clamp.policy.microservice.MicroservicePolicyService; +import org.onap.clamp.policy.microservice.MicroServicePolicyService; import org.onap.clamp.policy.operational.OperationalPolicy; import org.onap.clamp.policy.operational.OperationalPolicyService; import org.springframework.beans.factory.annotation.Autowired; @@ -63,7 +62,7 @@ public class LoopServiceTestItCase { LoopsRepository loopsRepository; @Autowired - MicroservicePolicyService microServicePolicyService; + MicroServicePolicyService microServicePolicyService; @Autowired OperationalPolicyService operationalPolicyService; @@ -71,11 +70,6 @@ public class LoopServiceTestItCase { @Autowired LoopLogService loopLogService; - @After - public void tearDown() { - loopsRepository.deleteAll(); - } - @Test @Transactional public void shouldCreateEmptyLoop() { @@ -96,7 +90,7 @@ public class LoopServiceTestItCase { assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint); assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg); assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString()) - .isEqualTo("testValue"); + .isEqualTo("testValue"); } @Test @@ -105,11 +99,11 @@ public class LoopServiceTestItCase { // given saveTestLoopToDb(); OperationalPolicy operationalPolicy = new OperationalPolicy("policyName", null, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); // when Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(operationalPolicy)); + Lists.newArrayList(operationalPolicy)); // then assertThat(actualLoop).isNotNull(); @@ -128,20 +122,20 @@ public class LoopServiceTestItCase { // given saveTestLoopToDb(); MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); // when Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(microServicePolicy)); + Lists.newArrayList(microServicePolicy)); // then assertThat(actualLoop).isNotNull(); assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies(); assertThat(savedPolicies).hasSize(1); - assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") - .containsExactly(microServicePolicy); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate", + "createdBy", "updatedBy").containsExactly(microServicePolicy); assertThat(savedPolicies).extracting("usedByLoops").hasSize(1); } @@ -153,16 +147,16 @@ public class LoopServiceTestItCase { saveTestLoopToDb(); MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", "", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy)); MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("secondPolicyName", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", true, JsonUtils.GSON.fromJson("{}", JsonObject.class), - null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", true, + JsonUtils.GSON.fromJson("{}", JsonObject.class), null); // when firstMicroServicePolicy.setProperties(JsonUtils.GSON.fromJson("{\"name1\":\"value1\"}", JsonObject.class)); Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy)); + Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy)); // then assertThat(actualLoop).isNotNull(); @@ -171,8 +165,8 @@ public class LoopServiceTestItCase { assertThat(savedPolicies).hasSize(2); assertThat(savedPolicies).contains(firstMicroServicePolicy); assertThat(savedPolicies).contains(secondMicroServicePolicy); - assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") - .containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate", + "createdBy", "updatedBy").containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy); } @@ -189,24 +183,24 @@ public class LoopServiceTestItCase { saveTestLoopToDb(); MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy("firstPolicyName", "", - "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "\"tosca_definitions_version: tosca_simple_yaml_1_0_0\"", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy)); MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy("policyName", "", "secondPolicyTosca", - true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null); + true, JsonUtils.GSON.fromJson("{}", JsonObject.class), null); // when Loop actualLoop = loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(secondMicroServicePolicy)); + Lists.newArrayList(secondMicroServicePolicy)); // then assertThat(actualLoop).isNotNull(); assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); Set<MicroServicePolicy> savedPolicies = actualLoop.getMicroServicePolicies(); assertThat(savedPolicies).hasSize(1); - assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") - .containsExactly(secondMicroServicePolicy); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops", "createdDate", "updatedDate", + "createdBy", "updatedBy").containsExactly(secondMicroServicePolicy); } @@ -219,16 +213,16 @@ public class LoopServiceTestItCase { JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class); OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy)); OperationalPolicy secondOperationalPolicy = new OperationalPolicy("secondPolicyName", null, - newJsonConfiguration); + newJsonConfiguration); // when firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration); Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy)); + Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy)); // then assertThat(actualLoop).isNotNull(); @@ -236,9 +230,9 @@ public class LoopServiceTestItCase { Set<OperationalPolicy> savedPolicies = actualLoop.getOperationalPolicies(); assertThat(savedPolicies).hasSize(2); assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop") - .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy); + .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy); Set<String> policiesLoops = Lists.newArrayList(savedPolicies).stream().map(OperationalPolicy::getLoop) - .map(Loop::getName).collect(Collectors.toSet()); + .map(Loop::getName).collect(Collectors.toSet()); assertThat(policiesLoops).containsExactly(EXAMPLE_LOOP_NAME); } @@ -249,15 +243,15 @@ public class LoopServiceTestItCase { saveTestLoopToDb(); OperationalPolicy firstOperationalPolicy = new OperationalPolicy("firstPolicyName", null, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy)); OperationalPolicy secondOperationalPolicy = new OperationalPolicy("policyName", null, - JsonUtils.GSON.fromJson("{}", JsonObject.class)); + JsonUtils.GSON.fromJson("{}", JsonObject.class)); // when Loop actualLoop = loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, - Lists.newArrayList(secondOperationalPolicy)); + Lists.newArrayList(secondOperationalPolicy)); // then assertThat(actualLoop).isNotNull(); @@ -300,21 +294,21 @@ public class LoopServiceTestItCase { loop = loopService.saveOrUpdateLoop(loop); // Add op policy OperationalPolicy operationalPolicy = new OperationalPolicy("opPolicy", null, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); loopService.updateAndSaveOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy)); // Add Micro service policy MicroServicePolicy microServicePolicy = new MicroServicePolicy("microPolicy", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopService.updateAndSaveMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy)); // Verify it's there assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNotNull(); loopService.deleteLoop(EXAMPLE_LOOP_NAME); - // Verify it's well deleted and has been cascaded + // Verify it's well deleted and has been cascaded, except for Microservice assertThat(loopsRepository.findById(EXAMPLE_LOOP_NAME).orElse(null)).isNull(); - assertThat(microServicePolicyService.isExisting("microPolicy")).isFalse(); + assertThat(microServicePolicyService.isExisting("microPolicy")).isTrue(); assertThat(operationalPolicyService.isExisting("opPolicy")).isFalse(); assertThat(loopLogService.isExisting(((LoopLog) loop.getLoopLogs().toArray()[0]).getId())).isFalse(); } @@ -334,8 +328,8 @@ public class LoopServiceTestItCase { public void testUpdateDcaeDeploymentFields() { saveTestLoopToDb(); Loop loop = loopService.getLoop(EXAMPLE_LOOP_NAME); - loopService.updateDcaeDeploymentFields(loop,"CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85", - "https4://deployment-handler.onap:8443"); + loopService.updateDcaeDeploymentFields(loop, "CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85", + "https4://deployment-handler.onap:8443"); loop = loopService.getLoop(EXAMPLE_LOOP_NAME); assertThat(loop.getDcaeDeploymentId()).isEqualTo("CLAMP_c5ce429a-f570-48c5-a7ea-53bed8f86f85"); assertThat(loop.getDcaeDeploymentStatusUrl()).isEqualTo("https4://deployment-handler.onap:8443"); @@ -347,8 +341,8 @@ public class LoopServiceTestItCase { saveTestLoopToDb(); assertThat(microServicePolicyService.isExisting("policyName")).isFalse(); MicroServicePolicy microServicePolicy = new MicroServicePolicy("policyName", "", - "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, - JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + "tosca_definitions_version: tosca_simple_yaml_1_0_0", false, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); loopService.updateMicroservicePolicy(EXAMPLE_LOOP_NAME, microServicePolicy); assertThat(microServicePolicyService.isExisting("policyName")).isTrue(); } diff --git a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java index 68fe487ef..914c64ea5 100644 --- a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java +++ b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java @@ -44,6 +44,9 @@ import org.onap.clamp.loop.components.external.PolicyComponent; import org.onap.clamp.loop.log.LogType; import org.onap.clamp.loop.log.LoopLog; import org.onap.clamp.loop.service.Service; +import org.onap.clamp.loop.template.LoopTemplate; +import org.onap.clamp.loop.template.MicroServiceModel; +import org.onap.clamp.loop.template.PolicyModel; import org.onap.clamp.policy.microservice.MicroServicePolicy; import org.onap.clamp.policy.operational.OperationalPolicy; import org.skyscreamer.jsonassert.JSONAssert; @@ -72,10 +75,30 @@ public class LoopToJsonTest { MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared, gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); - return microService; } + private MicroServiceModel getMicroServiceModel(String yaml, String name, PolicyModel policyModel) { + MicroServiceModel model = new MicroServiceModel(); + model.setBlueprint(yaml); + model.setName(name); + model.setPolicyModel(policyModel); + return model; + } + + private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym, + String policyVariant) { + return new PolicyModel(policyType, policyModelTosca, version, policyAcronym, policyVariant); + } + + private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation, + Integer maxInstancesAllowed) { + LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null); + template.addMicroServiceModel(getMicroServiceModel("yaml", "microService1", + getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1"))); + return template; + } + private LoopLog getLoopLog(LogType type, String message, Loop loop) { LoopLog log = new LoopLog(message, type, "CLAMP", loop); log.setId(Long.valueOf(new Random().nextInt())); @@ -95,6 +118,8 @@ public class LoopToJsonTest { loopTest.addMicroServicePolicy(microServicePolicy); LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest); loopTest.addLog(loopLog); + LoopTemplate loopTemplate = getLoopTemplate("templateName", "yaml", "svg", 1); + loopTest.setLoopTemplate(loopTemplate); String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest); assertThat(jsonSerialized).isNotNull().isNotEmpty(); @@ -116,6 +141,9 @@ public class LoopToJsonTest { assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog); assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, "loop"); + + // Verify the loop template + assertThat(loopTestDeserialized.getLoopTemplate()).isEqualTo(loopTemplate); } @Test @@ -128,17 +156,14 @@ public class LoopToJsonTest { Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(), jsonModel.get("resourceDetails").getAsJsonObject(), "1.0"); loopTest2.setModelService(service); - String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2); assertThat(jsonSerialized).isNotNull().isNotEmpty(); System.out.println(jsonSerialized); - JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/loop.json"), - jsonSerialized, true); Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class); assertNotNull(loopTestDeserialized); - assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", - "svgRepresentation", "blueprint", "components"); + assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", "svgRepresentation", + "blueprint", "components"); } @Test diff --git a/src/test/java/org/onap/clamp/loop/PolicyModelServiceItCase.java b/src/test/java/org/onap/clamp/loop/PolicyModelServiceItCase.java new file mode 100644 index 000000000..b284dd795 --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/PolicyModelServiceItCase.java @@ -0,0 +1,161 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.loop; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; + +import javax.transaction.Transactional; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.clamp.clds.Application; +import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelId; +import org.onap.clamp.loop.template.PolicyModelsRepository; +import org.onap.clamp.loop.template.PolicyModelsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +public class PolicyModelServiceItCase { + + @Autowired + PolicyModelsService policyModelsService; + + @Autowired + PolicyModelsRepository policyModelsRepository; + + private static final String POLICY_MODEL_TYPE_1 = "org.onap.test"; + private static final String POLICY_MODEL_TYPE_1_VERSION_1 = "1.0.0"; + + private static final String POLICY_MODEL_TYPE_2 = "org.onap.test2"; + private static final String POLICY_MODEL_TYPE_2_VERSION_1 = "1.0.0"; + private static final String POLICY_MODEL_TYPE_2_VERSION_2 = "2.0.0"; + + private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym, + String policyVariant, String createdBy) { + PolicyModel policyModel = new PolicyModel(); + policyModel.setCreatedBy(createdBy); + policyModel.setPolicyAcronym(policyAcronym); + policyModel.setPolicyModelTosca(policyModelTosca); + policyModel.setPolicyModelType(policyType); + policyModel.setPolicyVariant(policyVariant); + policyModel.setUpdatedBy(createdBy); + policyModel.setVersion(version); + return policyModel; + } + + @Test + @Transactional + public void shouldCreatePolicyModel() { + // given + PolicyModel policyModel = getPolicyModel(POLICY_MODEL_TYPE_1, "yaml", POLICY_MODEL_TYPE_1_VERSION_1, "TEST", + "VARIANT", "user"); + + // when + PolicyModel actualPolicyModel = policyModelsService.saveOrUpdatePolicyModel(policyModel); + + // then + assertThat(actualPolicyModel).isNotNull(); + assertThat(actualPolicyModel).isEqualTo(policyModelsRepository + .findById(new PolicyModelId(actualPolicyModel.getPolicyModelType(), actualPolicyModel.getVersion())) + .get()); + assertThat(actualPolicyModel.getPolicyModelType()).isEqualTo(policyModel.getPolicyModelType()); + assertThat(actualPolicyModel.getCreatedBy()).isEqualTo(""); + assertThat(actualPolicyModel.getCreatedDate()).isNotNull(); + assertThat(actualPolicyModel.getPolicyAcronym()).isEqualTo(policyModel.getPolicyAcronym()); + assertThat(actualPolicyModel.getPolicyModelTosca()).isEqualTo(policyModel.getPolicyModelTosca()); + assertThat(actualPolicyModel.getPolicyVariant()).isEqualTo(policyModel.getPolicyVariant()); + assertThat(actualPolicyModel.getUpdatedBy()).isEqualTo(""); + assertThat(actualPolicyModel.getUpdatedDate()).isNotNull(); + assertThat(actualPolicyModel.getVersion()).isEqualTo(policyModel.getVersion()); + + assertThat(policyModelsService.getPolicyModel(POLICY_MODEL_TYPE_1, POLICY_MODEL_TYPE_1_VERSION_1)) + .isEqualToIgnoringGivenFields(policyModel, "createdDate", "updatedDate", "createdBy", "updatedBy"); + } + + @Test + @Transactional + public void shouldReturnAllPolicyModelTypes() { + // given + PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel1); + PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel2); + List<String> policyModelTypesList = policyModelsService.getAllPolicyModelTypes(); + + assertThat(policyModelTypesList).containsOnly(policyModel1.getPolicyModelType(), + policyModel2.getPolicyModelType()); + } + + @Test + @Transactional + public void shouldReturnAllPolicyModels() { + PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel1); + PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel2); + + assertThat(policyModelsService.getAllPolicyModels()).containsOnly(policyModel1, policyModel2); + } + + @Test + @Transactional + public void shouldReturnAllModelsByType() { + PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel1); + PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel2); + + assertThat(policyModelsService.getAllPolicyModelsByType(POLICY_MODEL_TYPE_2)).containsOnly(policyModel1, + policyModel2); + } + + @Test + @Transactional + public void shouldReturnSortedSet() { + PolicyModel policyModel1 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_1, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel1); + PolicyModel policyModel2 = getPolicyModel(POLICY_MODEL_TYPE_2, "yaml", POLICY_MODEL_TYPE_2_VERSION_2, "TEST", + "VARIANT", "user"); + policyModelsService.saveOrUpdatePolicyModel(policyModel2); + + SortedSet<PolicyModel> sortedSet = new TreeSet<>(); + policyModelsService.getAllPolicyModels().forEach(sortedSet::add); + assertThat(sortedSet).containsExactly(policyModel2, policyModel1); + } +} diff --git a/src/test/java/org/onap/clamp/util/SemanticVersioningTest.java b/src/test/java/org/onap/clamp/util/SemanticVersioningTest.java new file mode 100644 index 000000000..1fb5922fd --- /dev/null +++ b/src/test/java/org/onap/clamp/util/SemanticVersioningTest.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class SemanticVersioningTest { + + @Test + public void compareTest() { + assertThat(SemanticVersioning.compare("1.0.0", "2.0.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.5.0", "2.0.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.5.0", "2.1.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.5.3", "2.0.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5.3", "2.6.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5", "2.5.1")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5.0", "2.5.1")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5.0.0", "2.5.1")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("2.5.1.0", "2.5.1")).isEqualTo(1); + + assertThat(SemanticVersioning.compare("2.0.0", "1.0.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.0.0", "1.5.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.1.0", "1.5.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.0.0", "1.5.3")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.6.0", "2.5.3")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.5.1", "2.5")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.5.1", "2.5.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("2.5.1", "2.5.0.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("1", "1.2.3.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.2", "1")).isEqualTo(1); + } + + @Test + public void compareEqualsTest() { + assertThat(SemanticVersioning.compare("1.0.0", "1.0.0")).isEqualTo(0); + assertThat(SemanticVersioning.compare("1.0.0.0", "1.0.0")).isEqualTo(1); + assertThat(SemanticVersioning.compare("1.2.3", "1.2.3")).isEqualTo(0); + assertThat(SemanticVersioning.compare("1.2.3", "1.2.3.0")).isEqualTo(-1); + + } + + @Test + public void compareNullTest() { + assertThat(SemanticVersioning.compare(null, null)).isEqualTo(0); + assertThat(SemanticVersioning.compare(null, "1.0")).isEqualTo(-1); + assertThat(SemanticVersioning.compare("1.0", null)).isEqualTo(1); + } +} |