From 5f14c7ada8aca6bdfecdb4ac3bd2d1387315250c Mon Sep 17 00:00:00 2001 From: Krysiak Adam Date: Fri, 1 Mar 2019 15:32:19 +0100 Subject: created Api for closed loop managing Change-Id: Ib6bd92d1ae3119a857c5a63df10e8f9bb130f75f Issue-ID: CLAMP-301 Signed-off-by: Krysiak Adam --- .../java/org/onap/clamp/clds/it/HttpsItCase.java | 2 +- .../org/onap/clamp/dao/model/LoopToJsonTest.java | 104 -------- .../clamp/it/dao/model/LoopRepositoriesItCase.java | 160 ------------ .../onap/clamp/loop/LoopRepositoriesItCase.java | 151 ++++++++++++ .../org/onap/clamp/loop/LoopServiceTestItCase.java | 270 +++++++++++++++++++++ .../java/org/onap/clamp/loop/LoopToJsonTest.java | 107 ++++++++ 6 files changed, 529 insertions(+), 265 deletions(-) delete mode 100644 src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java delete mode 100644 src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java create mode 100644 src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java create mode 100644 src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java create mode 100644 src/test/java/org/onap/clamp/loop/LoopToJsonTest.java (limited to 'src/test/java/org/onap') diff --git a/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java b/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java index 81b6b835..57dbcf7a 100644 --- a/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/HttpsItCase.java @@ -130,7 +130,7 @@ public class HttpsItCase { }); template.setRequestFactory(factory); ResponseEntity httpsEntity = template - .getForEntity("https://localhost:" + this.httpsPort + "/restservices/clds/v1/api-doc", String.class); + .getForEntity("https://localhost:" + this.httpsPort + "/restservices/clds/api-doc", String.class); assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(httpsEntity.getBody()).contains("swagger"); FileUtils.writeStringToFile( diff --git a/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java b/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java deleted file mode 100644 index 2d9bfd2d..00000000 --- a/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/*- - * ============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.dao.model; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertNotNull; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; - -import java.util.Random; - -import org.junit.Test; -import org.onap.clamp.clds.util.JsonUtils; - -public class LoopToJsonTest { - - private OperationalPolicy getOperationalPolicy(String configJson, String name) { - OperationalPolicy opPolicy = new OperationalPolicy(); - opPolicy.setName(name); - opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, JsonObject.class)); - return opPolicy; - } - - private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl, String dcaeBlueprintId) { - Loop loop = new Loop(); - loop.setName(name); - loop.setSvgRepresentation(svgRepresentation); - loop.setBlueprint(blueprint); - loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); - loop.setLastComputedState(LoopState.DESIGN); - loop.setDcaeDeploymentId(dcaeId); - loop.setDcaeDeploymentStatusUrl(dcaeUrl); - loop.setDcaeBlueprintId(dcaeBlueprintId); - return loop; - } - - private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, - String jsonProperties, boolean shared) { - MicroServicePolicy µService = new MicroServicePolicy(); - µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, JsonObject.class)); - µService.setPolicyTosca(policyTosca); - µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); - µService.setShared(shared); - - µService.setName(name); - return µService; - } - - private LoopLog getLoopLog(LogType type, String message) { - LoopLog log = new LoopLog(); - log.setLogType(type); - log.setMessage(message); - log.setId(Long.valueOf(new Random().nextInt())); - return log; - } - - @Test - public void LoopGsonTest() { - Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org", "UUID-blueprint"); - OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); - loopTest.addOperationalPolicy(opPolicy); - MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", - "YamlContent", "{\"param1\":\"value1\"}", true); - loopTest.addMicroServicePolicy(microServicePolicy); - LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); - loopTest.addLog(loopLog); - - String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest); - assertThat(jsonSerialized).isNotNull().isNotEmpty(); - System.out.println(jsonSerialized); - Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class); - assertNotNull(loopTestDeserialized); - assertThat(loopTestDeserialized).isEqualToComparingFieldByField(loopTest); - assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy); - assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy); - assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog); - assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, - "loop"); - } -} diff --git a/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java b/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java deleted file mode 100644 index 8a523fc0..00000000 --- a/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java +++ /dev/null @@ -1,160 +0,0 @@ -/*- - * ============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.it.dao.model; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonObject; - -import java.time.Instant; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.clamp.clds.Application; -import org.onap.clamp.dao.LoopLogRepository; -import org.onap.clamp.dao.LoopsRepository; -import org.onap.clamp.dao.MicroServicePolicyRepository; -import org.onap.clamp.dao.OperationalPolicyRepository; -import org.onap.clamp.dao.model.LogType; -import org.onap.clamp.dao.model.Loop; -import org.onap.clamp.dao.model.LoopLog; -import org.onap.clamp.dao.model.LoopState; -import org.onap.clamp.dao.model.MicroServicePolicy; -import org.onap.clamp.dao.model.OperationalPolicy; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.transaction.annotation.Transactional; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = Application.class) -public class LoopRepositoriesItCase { - - private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); - - @Autowired - private LoopsRepository loopRepository; - - @Autowired - private MicroServicePolicyRepository microServicePolicyRepository; - - @Autowired - private OperationalPolicyRepository operationalPolicyRepository; - - @Autowired - private LoopLogRepository loopLogRepository; - - private OperationalPolicy getOperationalPolicy(String configJson, String name) { - OperationalPolicy opPolicy = new OperationalPolicy(); - opPolicy.setName(name); - opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, JsonObject.class)); - return opPolicy; - } - - private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, - String dcaeId, String dcaeUrl, String dcaeBlueprintId) { - Loop loop = new Loop(); - loop.setName(name); - loop.setSvgRepresentation(svgRepresentation); - loop.setBlueprint(blueprint); - loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); - loop.setLastComputedState(LoopState.DESIGN); - loop.setDcaeDeploymentId(dcaeId); - loop.setDcaeDeploymentStatusUrl(dcaeUrl); - loop.setDcaeBlueprintId(dcaeBlueprintId); - return loop; - } - - private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, - String jsonProperties, boolean shared) { - MicroServicePolicy µService = new MicroServicePolicy(); - µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, JsonObject.class)); - µService.setPolicyTosca(policyTosca); - µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); - µService.setShared(shared); - - µService.setName(name); - return µService; - } - - private LoopLog getLoopLog(LogType type, String message) { - LoopLog log = new LoopLog(); - log.setLogType(type); - log.setMessage(message); - return log; - } - - @Test - @Transactional - public void CrudTest() { - Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", - "123456789", "https://dcaetest.org", "UUID-blueprint"); - OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); - loopTest.addOperationalPolicy(opPolicy); - MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", - "YamlContent", "{\"param1\":\"value1\"}", true); - loopTest.addMicroServicePolicy(microServicePolicy); - LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); - loopTest.addLog(loopLog); - - // Attemp to save into the database the entire loop - Loop loopInDb = loopRepository.save(loopTest); - assertThat(loopInDb).isNotNull(); - assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest"); - // 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).isEqualToComparingFieldByField(loopTest); - assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true); - assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(true); - assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(true); - assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true); - - // Now attempt to read from database - Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get(); - assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest); - assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog); - assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]) - .isEqualToComparingFieldByField(opPolicy); - assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0]) - .isEqualToComparingFieldByField(microServicePolicy); - - // Attempt an update - ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now()); - loopRepository.save(loopInDbRetrieved); - Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get(); - assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0]) - .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); - - // Attempt to delete the object and check it has well been cascaded - loopRepository.delete(loopInDbRetrieved); - assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false); - assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(false); - assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(false); - assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false); - - } -} diff --git a/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java b/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java new file mode 100644 index 00000000..c524eec3 --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/LoopRepositoriesItCase.java @@ -0,0 +1,151 @@ +/*- + * ============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 com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; + +import java.time.Instant; + +import java.util.HashSet; +import org.junit.Test; +import org.junit.runner.RunWith; +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.policy.microservice.MicroServicePolicy; +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; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +public class LoopRepositoriesItCase { + + private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); + + @Autowired + private LoopsRepository loopRepository; + + @Autowired + private MicroservicePolicyService microServicePolicyService; + + @Autowired + private OperationalPolicyService operationalPolicyService; + + @Autowired + private LoopLogRepository loopLogRepository; + + private OperationalPolicy getOperationalPolicy(String configJson, String name) { + return new OperationalPolicy(name, null, new Gson().fromJson(configJson, JsonObject.class)); + } + + private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { + Loop loop = new Loop(); + loop.setName(name); + loop.setSvgRepresentation(svgRepresentation); + loop.setBlueprint(blueprint); + loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); + loop.setLastComputedState(LoopState.DESIGN); + loop.setDcaeDeploymentId(dcaeId); + loop.setDcaeDeploymentStatusUrl(dcaeUrl); + loop.setDcaeBlueprintId(dcaeBlueprintId); + return loop; + } + + private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, + String jsonProperties, boolean shared) { + MicroServicePolicy µService = new MicroServicePolicy(name, policyTosca, shared, + gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); + µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); + return µService; + } + + private LoopLog getLoopLog(LogType type, String message) { + LoopLog log = new LoopLog(); + log.setLogType(type); + log.setMessage(message); + return log; + } + + @Test + @Transactional + public void CrudTest() { + Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", + "123456789", "https://dcaetest.org", "UUID-blueprint"); + OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); + loopTest.addOperationalPolicy(opPolicy); + MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", + "YamlContent", "{\"param1\":\"value1\"}", true); + loopTest.addMicroServicePolicy(microServicePolicy); + LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); + loopTest.addLog(loopLog); + + // Attemp to save into the database the entire loop + Loop loopInDb = loopRepository.save(loopTest); + assertThat(loopInDb).isNotNull(); + assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest"); + // 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).isEqualToComparingFieldByField(loopTest); + 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); + + // Now attempt to read from database + Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get(); + assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest); + assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog); + assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]) + .isEqualToComparingFieldByField(opPolicy); + assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0]) + .isEqualToComparingFieldByField(microServicePolicy); + + // Attempt an update + ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now()); + loopRepository.save(loopInDbRetrieved); + Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get(); + assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0]) + .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); + + // 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(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false); + + } +} diff --git a/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java new file mode 100644 index 00000000..a9c30873 --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/LoopServiceTestItCase.java @@ -0,0 +1,270 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia 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 com.google.gson.JsonObject; +import java.util.Set; +import java.util.stream.Collectors; +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; +import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +public class LoopServiceTestItCase { + + private static final String EXAMPLE_LOOP_NAME = "ClosedLoopTest"; + private static final String EXAMPLE_JSON = "{\"testName\":\"testValue\"}"; + + @Autowired + LoopService loopService; + + @Autowired + LoopsRepository loopsRepository; + + @After + public void tearDown() { + loopsRepository.deleteAll(); + } + + @Test + public void shouldCreateEmptyLoop() { + //given + String loopBlueprint = "blueprint"; + String loopSvg = "representation"; + Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, loopBlueprint, loopSvg); + testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + testLoop.setLastComputedState(LoopState.DESIGN); + + //when + Loop actualLoop = loopService.addNewLoop(testLoop); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop).isEqualTo(loopsRepository.findById(actualLoop.getName()).get()); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + assertThat(actualLoop.getBlueprint()).isEqualTo(loopBlueprint); + assertThat(actualLoop.getSvgRepresentation()).isEqualTo(loopSvg); + assertThat(actualLoop.getGlobalPropertiesJson().getAsJsonPrimitive("testName").getAsString()) + .isEqualTo("testValue"); + } + + @Test + public void shouldAddOperationalPolicyToLoop() { + //given + saveTestLoopToDb(); + JsonObject confJson = JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class); + String policyName = "policyName"; + OperationalPolicy operationalPolicy = new OperationalPolicy(policyName, null, confJson); + + //when + Loop actualLoop = loopService + .updateOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(operationalPolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getOperationalPolicies(); + assertThat(savedPolicies).hasSize(1); + assertThat(savedPolicies) + .usingElementComparatorIgnoringFields("loop") + .contains(operationalPolicy); + OperationalPolicy savedPolicy = savedPolicies.iterator().next(); + assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME); + + } + + @Test + public void shouldAddMicroservicePolicyToLoop() { + //given + saveTestLoopToDb(); + JsonObject confJson = JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class); + String policyName = "policyName"; + String policyTosca = "policyTosca"; + MicroServicePolicy microServicePolicy = new MicroServicePolicy(policyName, policyTosca, false, confJson, null); + + //when + Loop actualLoop = loopService + .updateMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(microServicePolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getMicroServicePolicies(); + assertThat(savedPolicies).hasSize(1); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") + .containsExactly(microServicePolicy); + assertThat(savedPolicies).extracting("usedByLoops") + .hasSize(1); + + } + + @Test + @Transactional + public void shouldCreateNewMicroservicePolicyAndUpdateJsonRepresentationOfOldOne() { + //given + saveTestLoopToDb(); + String firstPolicyName = "firstPolicyName"; + JsonObject newJsonRepresentation = JsonUtils.GSON.fromJson("{}", JsonObject.class); + String secondPolicyName = "secondPolicyName"; + String secondPolicyTosca = "secondPolicyTosca"; + MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy(firstPolicyName, "policyTosca", + false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + loopService.updateMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy)); + + MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy(secondPolicyName, secondPolicyTosca, true, + newJsonRepresentation, null); + + //when + firstMicroServicePolicy.setJsonRepresentation(newJsonRepresentation); + Loop actualLoop = loopService.updateMicroservicePolicies(EXAMPLE_LOOP_NAME, + Lists.newArrayList(firstMicroServicePolicy, secondMicroServicePolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getMicroServicePolicies(); + assertThat(savedPolicies).hasSize(2); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") + .containsExactlyInAnyOrder(firstMicroServicePolicy, secondMicroServicePolicy); + + } + + private void saveTestLoopToDb() { + Loop testLoop = createTestLoop(EXAMPLE_LOOP_NAME, "blueprint", "representation"); + testLoop.setGlobalPropertiesJson(JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + loopService.addNewLoop(testLoop); + } + + @Test + public void shouldRemoveOldMicroservicePolicyIfNotInUpdatedList() { + //given + saveTestLoopToDb(); + + JsonObject jsonRepresentation = JsonUtils.GSON.fromJson("{}", JsonObject.class); + String firstPolicyName = "firstPolicyName"; + String secondPolicyName = "policyName"; + String secondPolicyTosca = "secondPolicyTosca"; + MicroServicePolicy firstMicroServicePolicy = new MicroServicePolicy(firstPolicyName, "policyTosca", + false, JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class), null); + loopService.updateMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstMicroServicePolicy)); + + MicroServicePolicy secondMicroServicePolicy = new MicroServicePolicy(secondPolicyName, secondPolicyTosca, true, + jsonRepresentation, null); + + //when + Loop actualLoop = loopService + .updateMicroservicePolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(secondMicroServicePolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getMicroServicePolicies(); + assertThat(savedPolicies).hasSize(1); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("usedByLoops") + .containsExactly(secondMicroServicePolicy); + + } + + @Test + @Transactional + public void shouldCreateNewOperationalPolicyAndUpdateJsonRepresentationOfOldOne() { + //given + saveTestLoopToDb(); + + String firstPolicyName = "firstPolicyName"; + JsonObject newJsonConfiguration = JsonUtils.GSON.fromJson("{}", JsonObject.class); + String secondPolicyName = "secondPolicyName"; + OperationalPolicy firstOperationalPolicy = new OperationalPolicy(firstPolicyName, null, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + loopService.updateOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy)); + + OperationalPolicy secondOperationalPolicy = new OperationalPolicy(secondPolicyName, null, newJsonConfiguration); + + //when + firstOperationalPolicy.setConfigurationsJson(newJsonConfiguration); + Loop actualLoop = loopService.updateOperationalPolicies(EXAMPLE_LOOP_NAME, + Lists.newArrayList(firstOperationalPolicy, secondOperationalPolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getOperationalPolicies(); + assertThat(savedPolicies).hasSize(2); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop") + .containsExactlyInAnyOrder(firstOperationalPolicy, secondOperationalPolicy); + Set policiesLoops = Lists.newArrayList(savedPolicies).stream() + .map(OperationalPolicy::getLoop) + .map(Loop::getName) + .collect(Collectors.toSet()); + assertThat(policiesLoops) + .containsExactly(EXAMPLE_LOOP_NAME); + } + + @Test + public void shouldRemoveOldOperationalPolicyIfNotInUpdatedList() { + //given + saveTestLoopToDb(); + + JsonObject jsonRepresentation = JsonUtils.GSON.fromJson("{}", JsonObject.class); + String firstPolicyName = "firstPolicyName"; + String secondPolicyName = "policyName"; + OperationalPolicy firstOperationalPolicy = new OperationalPolicy(firstPolicyName, null, + JsonUtils.GSON.fromJson(EXAMPLE_JSON, JsonObject.class)); + loopService.updateOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(firstOperationalPolicy)); + + OperationalPolicy secondOperationalPolicy = new OperationalPolicy(secondPolicyName, null, jsonRepresentation); + + //when + Loop actualLoop = loopService + .updateOperationalPolicies(EXAMPLE_LOOP_NAME, Lists.newArrayList(secondOperationalPolicy)); + + //then + assertThat(actualLoop).isNotNull(); + assertThat(actualLoop.getName()).isEqualTo(EXAMPLE_LOOP_NAME); + Set savedPolicies = actualLoop.getOperationalPolicies(); + assertThat(savedPolicies).hasSize(1); + assertThat(savedPolicies).usingElementComparatorIgnoringFields("loop") + .containsExactly(secondOperationalPolicy); + OperationalPolicy savedPolicy = savedPolicies.iterator().next(); + assertThat(savedPolicy.getLoop().getName()).isEqualTo(EXAMPLE_LOOP_NAME); + + } + + private Loop createTestLoop(String loopName, String loopBlueprint, String loopSvg) { + return new Loop(loopName, loopBlueprint, loopSvg); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java new file mode 100644 index 00000000..92a0e14c --- /dev/null +++ b/src/test/java/org/onap/clamp/loop/LoopToJsonTest.java @@ -0,0 +1,107 @@ +/*- + * ============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 static org.junit.Assert.assertNotNull; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.util.HashSet; +import java.util.Random; + +import org.junit.Test; +import org.onap.clamp.loop.log.LogType; +import org.onap.clamp.loop.log.LoopLog; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.policy.operational.OperationalPolicy; +import org.onap.clamp.clds.util.JsonUtils; + +public class LoopToJsonTest { + + private Gson gson = new Gson(); + + private OperationalPolicy getOperationalPolicy(String configJson, String name) { + return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class)); + } + + private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, + String dcaeId, String dcaeUrl, String dcaeBlueprintId) { + Loop loop = new Loop(name, blueprint, svgRepresentation); + loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class)); + loop.setLastComputedState(LoopState.DESIGN); + loop.setDcaeDeploymentId(dcaeId); + loop.setDcaeDeploymentStatusUrl(dcaeUrl); + loop.setDcaeBlueprintId(dcaeBlueprintId); + return loop; + } + + private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, + String jsonProperties, boolean shared) { + MicroServicePolicy µService = new MicroServicePolicy(name, policyTosca, shared, + gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>()); + µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class)); + + return µService; + } + + private LoopLog getLoopLog(LogType type, String message) { + LoopLog log = new LoopLog(); + log.setLogType(type); + log.setMessage(message); + log.setId(Long.valueOf(new Random().nextInt())); + return log; + } + + @Test + public void LoopGsonTest() { + Loop loopTest = getLoop("ControlLoopTest", "", "yamlcontent", "{\"testname\":\"testvalue\"}", + "123456789", "https://dcaetest.org", "UUID-blueprint"); + OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); + loopTest.addOperationalPolicy(opPolicy); + MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", + "YamlContent", "{\"param1\":\"value1\"}", true); + loopTest.addMicroServicePolicy(microServicePolicy); + LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); + loopTest.addLog(loopLog); + + String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest); + assertThat(jsonSerialized).isNotNull().isNotEmpty(); + System.out.println(jsonSerialized); + Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class); + assertNotNull(loopTestDeserialized); + assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint"); + + //svg and blueprint not exposed so wont be deserialized + assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null); + assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null); + + assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy); + assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy); + assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog); + assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, + "loop"); + } +} -- cgit 1.2.3-korg