aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java
diff options
context:
space:
mode:
authorSébastien Determe <sebastien.determe@intl.att.com>2019-03-05 14:25:50 +0000
committerGerrit Code Review <gerrit@onap.org>2019-03-05 14:25:50 +0000
commit179bae8d30eed732b671f1956ffab90de0700ac2 (patch)
treeef2d3975d242d63cda1b2d618acc59c66a408aa8 /src/test/java
parent87eaeb7dd8ac3dcbcc17a6a9632213648e35bb04 (diff)
parent75c0f1dd0b0b9700a3f235611131fdc500e10eb0 (diff)
Merge "Improve tests"
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java2
-rw-r--r--src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java103
-rw-r--r--src/test/java/org/onap/clamp/it/dao/model/LoopItCase.java98
-rw-r--r--src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java159
4 files changed, 263 insertions, 99 deletions
diff --git a/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java
index d4172c244..50858f794 100644
--- a/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java
+++ b/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java
@@ -57,7 +57,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
public class CldsDaoItCase {
- protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDao.class);
+ protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDaoItCase.class);
@Autowired
public CldsDao cldsDao;
private String bpmnText;
diff --git a/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java b/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java
new file mode 100644
index 000000000..496d8c0ec
--- /dev/null
+++ b/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java
@@ -0,0 +1,103 @@
+/*-
+ * ============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 java.util.Map;
+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, Map.class));
+ return opPolicy;
+ }
+
+ private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
+ String dcaeId, String dcaeUrl) {
+ Loop loop = new Loop();
+ loop.setName(name);
+ loop.setSvgRepresentation(svgRepresentation);
+ loop.setBlueprint(blueprint);
+ loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, Map.class));
+ loop.setLastComputedState(LoopState.DESIGN);
+ loop.setDcaeDeploymentId(dcaeId);
+ loop.setDcaeDeploymentStatusUrl(dcaeUrl);
+ 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, Map.class));
+ µService.setPolicyTosca(policyTosca);
+ µService.setProperties(new Gson().fromJson(jsonProperties, Map.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", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
+ "123456789", "https://dcaetest.org");
+ 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/LoopItCase.java b/src/test/java/org/onap/clamp/it/dao/model/LoopItCase.java
deleted file mode 100644
index 06f4a7f88..000000000
--- a/src/test/java/org/onap/clamp/it/dao/model/LoopItCase.java
+++ /dev/null
@@ -1,98 +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.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
-import java.util.Map;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.onap.clamp.clds.Application;
-import org.onap.clamp.dao.LoopsRepository;
-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;
-
-@RunWith(SpringJUnit4ClassRunner.class)
-@SpringBootTest(classes = Application.class)
-public class LoopItCase {
-
- @Autowired
- private LoopsRepository loopRepository;
-
- @Test
- public void DecodeEncodeTest() {
- Loop loopTest = new Loop();
- loopTest.setName("ClosedLoopTest");
- loopTest.setSvgRepresentation("representation");
- loopTest.setBlueprint("blueprint");
- loopTest.setGlobalPropertiesJson(new Gson().fromJson("{\"testName\":\"testValue\"}", Map.class));
- loopTest.setLastComputedState(LoopState.DESIGN);
- loopTest.setBlueprint("yaml");
-
- OperationalPolicy opPolicy = new OperationalPolicy();
- opPolicy.setName("OpPolicyTest");
- opPolicy.setConfigurationsJson(new Gson().fromJson("{\"testname\":\"testvalue\"}", Map.class));
- opPolicy.setLoop(loopTest);
- loopTest.addOperationalPolicy(opPolicy);
-
- MicroServicePolicy µService = new MicroServicePolicy();
- µService.setJsonRepresentation(new Gson().fromJson("{\"testrepresentation\":\"value\"}", Map.class));
- µService.setPolicyTosca("tosca");
- µService.setProperties(new Gson().fromJson("{\"testparam\":\"testvalue\"}", Map.class));
- µService.setShared(true);
-
- µService.setName("ConfigPolicyTest");
- loopTest.addMicroServicePolicy(µService);
-
- LoopLog log = new LoopLog();
- log.setLogType(LogType.INFO);
- log.setMessage("test message");
-
- loopTest.addLog(log);
-
- Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
- String json = gson.toJson(loopTest);
- assertNotNull(json);
- Loop loopTestDecoded = gson.fromJson(json, Loop.class);
- assertNotNull(loopTestDecoded);
- Loop loop = gson.fromJson(json, Loop.class);
- assertNotNull(loop);
-
- Loop loopInDb = loopRepository.save(loopTest);
- assertNotNull(loopInDb);
- assertTrue(loopRepository.findById(loopInDb.getName()).get().getName().equals("ClosedLoopTest"));
- }
-}
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
new file mode 100644
index 000000000..67ab247f5
--- /dev/null
+++ b/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java
@@ -0,0 +1,159 @@
+/*-
+ * ============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 java.time.Instant;
+import java.util.Map;
+
+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, Map.class));
+ return opPolicy;
+ }
+
+ private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
+ String dcaeId, String dcaeUrl) {
+ Loop loop = new Loop();
+ loop.setName(name);
+ loop.setSvgRepresentation(svgRepresentation);
+ loop.setBlueprint(blueprint);
+ loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, Map.class));
+ loop.setLastComputedState(LoopState.DESIGN);
+ loop.setDcaeDeploymentId(dcaeId);
+ loop.setDcaeDeploymentStatusUrl(dcaeUrl);
+ 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, Map.class));
+ µService.setPolicyTosca(policyTosca);
+ µService.setProperties(new Gson().fromJson(jsonProperties, Map.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", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
+ "123456789", "https://dcaetest.org");
+ 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);
+
+ }
+}