summaryrefslogtreecommitdiffstats
path: root/dcaedt_tools/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'dcaedt_tools/src/test/java')
-rw-r--r--dcaedt_tools/src/test/java/BaseTest.java151
-rw-r--r--dcaedt_tools/src/test/java/DeployTemplateTest.java54
-rw-r--r--dcaedt_tools/src/test/java/EntitiesRetrieverTest.java29
-rw-r--r--dcaedt_tools/src/test/java/TemplateContainerTest.java157
4 files changed, 391 insertions, 0 deletions
diff --git a/dcaedt_tools/src/test/java/BaseTest.java b/dcaedt_tools/src/test/java/BaseTest.java
new file mode 100644
index 0000000..49534a5
--- /dev/null
+++ b/dcaedt_tools/src/test/java/BaseTest.java
@@ -0,0 +1,151 @@
+import json.response.ElementsResponse.Element;
+import json.response.ItemsResponse.Item;
+import json.response.ItemsResponse.Model;
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
+import utilities.IDcaeRestClient;
+import utilities.IReport;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+abstract class BaseTest {
+ static final String USER_ID = "userId";
+ static final String TEMPLATE_INFO_NAME = "templateInfoName";
+ static final String VFCMT_NAME1 = "my vfcmt1";
+ static final String UUID1 = "my uuid1";
+ static final String VFCMT_NAME2 = "my vfcmt2";
+ static final String UUID2 = "my uuid2";
+ static final String VFCMT_NAME3 = "my vfcmt3";
+ static final String UUID3 = "my uuid3";
+ static final String ELEMENT_NAME1 = "my element1";
+ static final String ELEMENT_NAME2 = "my element2";
+ static final String ELEMENT_NAME3 = "my element3";
+ static final String ALIAS_NAME1 = "my alias1";
+ static final String ALIAS_NAME2 = "my alias2";
+ static final String ALIAS_NAME3 = "my alias3";
+ static final String ITEM_NAME1 = "my item1";
+ static final String ITEM_NAME2 = "my item2";
+ static final String ITEM_NAME3 = "my item3";
+
+ @Mock
+ IReport report;
+ @Mock
+ IDcaeRestClient dcaeRestClient;
+
+ @Before
+ public void setup() {
+ when(dcaeRestClient.getUserId()).thenReturn(USER_ID);
+ mockGetAllVfcmt();
+ mockGetElements();
+ mockGetItems();
+ mockGetItemModel();
+ mockGetItemType();
+ mockCheckoutVfcmtAndCreateResource();
+ when(dcaeRestClient.saveComposition(anyString(), anyString())).thenReturn("Composition Created");
+ }
+
+ private void mockCheckoutVfcmtAndCreateResource() {
+ ResourceDetailed resourceDetailed = new ResourceDetailed();
+ resourceDetailed.setName(VFCMT_NAME1);
+ resourceDetailed.setUuid(UUID1);
+ resourceDetailed.setLifecycleState("NOT_CERTIFIED_CHECKOUT");
+ resourceDetailed.setLastUpdaterUserId(USER_ID);
+ when(dcaeRestClient.checkoutVfcmt(anyString())).thenReturn(resourceDetailed);
+ when(dcaeRestClient.createResource(any())).thenReturn(resourceDetailed);
+ }
+
+ private void mockGetItemType() {
+ when(dcaeRestClient.getItemType(anyString(), anyString())).thenReturn("{\"data\":{\"type\":{\"itemId\":\"e45ec9d7-01df-4cb1-896f-aff2a6ca5a8b/tosca.dcae.nodes.cdapApp.Map\", \"typeinfo\":\"typeInfo\"}}}");
+ }
+
+ private void mockGetItemModel() {
+ when(dcaeRestClient.getItemModel(anyString())).thenReturn("{\"data\":{\"model\":{\"nodes\":[{\"capability\":{\"type\":\"someType\"}, \"type\":\"type\", \"name\":\"SomeNameFromRequirement\", \"requirements\":[{\"name\":\"SomeNameFromRequirement\"}], \"properties\":[{}], \"capabilities\":[{\"name\":\"SomeNameToCapability\"}],\"type\":\"type\"}]}}}",
+ "{\"data\":{\"model\":{\"nodes\":[{\"capability\":{\"type\":\"someType\"}, \"type\":\"type\", \"name\":\"SomeNameToCapability\", \"requirements\":[{\"name\":\"SomeNameFromRequirement\"}], \"properties\":[{}], \"capabilities\":[{\"name\":\"SomeNameToCapability\"}],\"type\":\"type\"}]}}}");
+ }
+
+ private void mockGetItems() {
+ when(dcaeRestClient.getItem(ELEMENT_NAME1)).thenReturn(null);
+ List<Item> items = new ArrayList<>();
+ Item item = new Item();
+ item.setName(ITEM_NAME1);
+ Model model = new Model();
+ model.setItemId("");
+ List<Model> models = Collections.singletonList(model);
+ item.setModels(models);
+ items.add(item);
+ item = new Item();
+ item.setName(ITEM_NAME2);
+ item.setModels(models);
+ items.add(item);
+ when(dcaeRestClient.getItem(ELEMENT_NAME2)).thenReturn(items);
+ items = new ArrayList<>();
+ item = new Item();
+ item.setName(ITEM_NAME3);
+ item.setModels(models);
+ items.add(item);
+ when(dcaeRestClient.getItem(ELEMENT_NAME3)).thenReturn(items);
+ }
+
+ private void mockGetElements() {
+ List<Element> elements = new ArrayList<>();
+ Element element = new Element();
+ element.setName(ELEMENT_NAME1);
+ elements.add(element);
+ element = new Element();
+ element.setName(ELEMENT_NAME2);
+ elements.add(element);
+ element = new Element();
+ element.setName(ELEMENT_NAME3);
+ elements.add(element);
+ when(dcaeRestClient.getElements()).thenReturn(elements);
+ }
+
+ private void mockGetAllVfcmt() {
+ List<ResourceDetailed> resourceDetaileds = new ArrayList<>();
+ ResourceDetailed resourceDetailed = new ResourceDetailed();
+ resourceDetailed.setName(VFCMT_NAME1);
+ resourceDetailed.setUuid(UUID1);
+ resourceDetailed.setLifecycleState("NOT_CERTIFIED_CHECKOUT");
+ resourceDetailed.setLastUpdaterUserId(USER_ID);
+ resourceDetaileds.add(resourceDetailed);
+ resourceDetailed = new ResourceDetailed();
+ resourceDetailed.setName(VFCMT_NAME2);
+ resourceDetailed.setUuid(UUID2);
+ resourceDetaileds.add(resourceDetailed);
+ resourceDetailed = new ResourceDetailed();
+ resourceDetailed.setName(VFCMT_NAME3);
+ resourceDetailed.setUuid(UUID3);
+ resourceDetaileds.add(resourceDetailed);
+
+ List<ResourceDetailed> resourceDetaileds2 = new ArrayList<>();
+ resourceDetailed = new ResourceDetailed();
+ resourceDetailed.setName(VFCMT_NAME1);
+ resourceDetailed.setUuid(UUID1);
+ resourceDetailed.setLifecycleState("NOT_CERTIFIED_CHECKOUT");
+ resourceDetailed.setLastUpdaterUserId(USER_ID);
+ resourceDetaileds2.add(resourceDetailed);
+ resourceDetailed = new ResourceDetailed();
+ resourceDetailed.setName(VFCMT_NAME2);
+ resourceDetailed.setUuid(UUID2);
+ resourceDetaileds2.add(resourceDetailed);
+ resourceDetailed = new ResourceDetailed();
+ resourceDetailed.setName(VFCMT_NAME3);
+ resourceDetailed.setUuid(UUID3);
+ resourceDetaileds2.add(resourceDetailed);
+ resourceDetailed = new ResourceDetailed();
+ resourceDetailed.setName(TEMPLATE_INFO_NAME);
+ resourceDetailed.setUuid(UUID3);
+ resourceDetaileds2.add(resourceDetailed);
+ when(dcaeRestClient.getAllVfcmts()).thenReturn(resourceDetaileds, resourceDetaileds2);
+ }
+}
diff --git a/dcaedt_tools/src/test/java/DeployTemplateTest.java b/dcaedt_tools/src/test/java/DeployTemplateTest.java
new file mode 100644
index 0000000..3fe0a7d
--- /dev/null
+++ b/dcaedt_tools/src/test/java/DeployTemplateTest.java
@@ -0,0 +1,54 @@
+import com.google.gson.JsonObject;
+import json.templateInfo.TemplateInfo;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import tools.DeployTemplate;
+
+import java.util.*;
+
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class DeployTemplateTest extends BaseTest {
+
+ @InjectMocks
+ DeployTemplate deployTemplate;
+
+ private Map<TemplateInfo, JsonObject> templateInfoToJsonObjectMap;
+
+ @Before
+ @Override
+ public void setup() {
+ super.setup();
+ templateInfoToJsonObjectMap = new HashMap<>();
+ TemplateInfo templateInfo = new TemplateInfo();
+ templateInfo.setName(VFCMT_NAME1);
+ templateInfo.setCategory("category");
+ templateInfo.setSubCategory("subCategory");
+ templateInfo.setDescription("description");
+ templateInfo.setUpdateIfExist(true);
+ templateInfoToJsonObjectMap.put(templateInfo, new JsonObject());
+ templateInfo = new TemplateInfo();
+ templateInfo.setName(TEMPLATE_INFO_NAME);
+ templateInfo.setCategory("category");
+ templateInfo.setSubCategory("subCategory");
+ templateInfo.setDescription("description");
+ templateInfoToJsonObjectMap.put(templateInfo, new JsonObject());
+ }
+
+ @Test
+ public void deployHappyFlow() {
+ deployTemplate.deploy(templateInfoToJsonObjectMap);
+ verify(report, times(0)).addErrorMessage(anyString());
+ }
+
+ @Test
+ public void deploy_failedSaving_failedVerify() {
+ when(dcaeRestClient.saveComposition(anyString(), anyString())).thenReturn("failed");
+ deployTemplate.deploy(templateInfoToJsonObjectMap);
+ verify(report, times(4)).addErrorMessage(anyString());
+ }
+}
diff --git a/dcaedt_tools/src/test/java/EntitiesRetrieverTest.java b/dcaedt_tools/src/test/java/EntitiesRetrieverTest.java
new file mode 100644
index 0000000..ea7eb2b
--- /dev/null
+++ b/dcaedt_tools/src/test/java/EntitiesRetrieverTest.java
@@ -0,0 +1,29 @@
+
+import json.response.ItemsResponse.Item;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import tools.EntitiesRetriever;
+
+import java.util.List;
+import java.util.Map;
+
+
+public class EntitiesRetrieverTest extends BaseTest {
+
+ @InjectMocks
+ EntitiesRetriever entitiesRetriever;
+
+ @Before
+ @Override
+ public void setup() {
+ super.setup();
+ }
+
+ @Test
+ public void getElementsByFolder_returns2Items() {
+ Map<String, List<Item>> result = entitiesRetriever.getElementsByFolder();
+ Assert.assertTrue(result.size() == 2);
+ }
+}
diff --git a/dcaedt_tools/src/test/java/TemplateContainerTest.java b/dcaedt_tools/src/test/java/TemplateContainerTest.java
new file mode 100644
index 0000000..7c3d287
--- /dev/null
+++ b/dcaedt_tools/src/test/java/TemplateContainerTest.java
@@ -0,0 +1,157 @@
+import com.google.gson.JsonObject;
+import json.response.ItemsResponse.Item;
+import json.response.ItemsResponse.Model;
+import json.templateInfo.Composition;
+import json.templateInfo.Relation;
+import json.templateInfo.TemplateInfo;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import tools.TemplateContainer;
+
+import java.util.*;
+
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class TemplateContainerTest extends BaseTest {
+
+ private TemplateContainer templateContainer;
+
+ private List<TemplateInfo> templateInfos;
+ private Map<String, List<Item>> elementsByFolderNames;
+ @Before
+ @Override
+ public void setup() {
+ super.setup();
+ templateInfos = new ArrayList<>();
+ TemplateInfo templateInfo = new TemplateInfo();
+ templateInfo.setName(TEMPLATE_INFO_NAME);
+ Composition composition = new Composition();
+ composition.setType(ELEMENT_NAME3);
+ composition.setAlias(ALIAS_NAME3);
+ templateInfo.setComposition(Collections.singletonList(composition));
+ templateInfos.add(templateInfo);
+ elementsByFolderNames = new HashMap<>();
+ Item item = new Item();
+ item.setName(ELEMENT_NAME3);
+ item.setItemId("");
+ Model model = new Model();
+ model.setItemId("");
+ List<Model> models = Collections.singletonList(model);
+ item.setModels(models);
+ List<Item> items = new ArrayList<>(Collections.singletonList(item));
+ elementsByFolderNames.put(TEMPLATE_INFO_NAME, items);
+
+ }
+
+ @Test
+ public void getCdumps_emptyTemplateInfo_returnEmptyMap() {
+ templateContainer = new TemplateContainer(report, dcaeRestClient, new ArrayList<>(), new HashMap<>());
+
+ Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
+
+ Assert.assertTrue(templateInfoJsonObjectMap.size() == 0);
+ }
+
+ @Test
+ public void getCdumps_returnNotFoundEmptyList() {
+ elementsByFolderNames = new HashMap<>();
+ Item item = new Item();
+ item.setName(ELEMENT_NAME2);
+ List<Item> items = new ArrayList<>(Collections.singletonList(item));
+ elementsByFolderNames.put(TEMPLATE_INFO_NAME, items);
+ templateContainer = new TemplateContainer(report, dcaeRestClient, templateInfos, elementsByFolderNames);
+
+ Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
+
+ verify(report).addErrorMessage(anyString());
+ Assert.assertTrue(templateInfoJsonObjectMap.size() == 0);
+ }
+
+ @Test
+ public void getCdumps_returnOneCdump() {
+ templateContainer = new TemplateContainer(report, dcaeRestClient, templateInfos, elementsByFolderNames);
+
+ Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
+ JsonObject jsonObject = templateInfoJsonObjectMap.get(templateInfos.get(0));
+ String result = jsonObject.toString();
+
+ verifyCdump(result);
+ verify(report, times(0)).addErrorMessage(anyString());
+ Assert.assertTrue(templateInfoJsonObjectMap.size() == 1);
+ }
+
+ @Test
+ public void getChumps_returnOneChumpWithRelations() {
+ templateInfos = new ArrayList<>();
+ TemplateInfo templateInfo = new TemplateInfo();
+ templateInfo.setName(TEMPLATE_INFO_NAME);
+ List<Composition> compositionList = new ArrayList<>();
+ Composition composition = new Composition();
+ composition.setType(ELEMENT_NAME3);
+ composition.setAlias(ALIAS_NAME3);
+ compositionList.add(composition);
+ composition = new Composition();
+ composition.setType(ELEMENT_NAME2);
+ composition.setAlias(ALIAS_NAME2);
+ compositionList.add(composition);
+ templateInfo.setComposition(compositionList);
+ Relation relation = new Relation();
+ relation.setFromComponent(ALIAS_NAME3 + ".SomeNameFromRequirement");
+ relation.setToComponent(ALIAS_NAME2 + ".SomeNameToCapability");
+ relation.setFromRequirement("SomeNameFromRequirement");
+ relation.setToCapability("SomeNameToCapability");
+ templateInfo.setRelations(Collections.singletonList(relation));
+ templateInfos.add(templateInfo);
+ elementsByFolderNames = new HashMap<>();
+ List<Item> itemList = new ArrayList<>();
+ Item item = new Item();
+ item.setName(ELEMENT_NAME3);
+ item.setItemId("");
+ Model model = new Model();
+ model.setItemId("");
+ List<Model> models = Collections.singletonList(model);
+ item.setModels(models);
+ itemList.add(item);
+ item = new Item();
+ item.setName(ELEMENT_NAME2);
+ item.setItemId("");
+ model = new Model();
+ model.setItemId("");
+ models = Collections.singletonList(model);
+ item.setModels(models);
+ itemList.add(item);
+ elementsByFolderNames.put(TEMPLATE_INFO_NAME, itemList);
+ templateContainer = new TemplateContainer(report, dcaeRestClient, templateInfos, elementsByFolderNames);
+
+ Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
+ JsonObject jsonObject = templateInfoJsonObjectMap.get(templateInfos.get(0));
+ String result = jsonObject.toString();
+
+ verifyCdumpRelations(result);
+ verify(report, times(0)).addErrorMessage(anyString());
+ Assert.assertTrue(templateInfoJsonObjectMap.size() == 1);
+ }
+
+ private void verifyCdumpRelations(String result) {
+ Assert.assertTrue(result.contains("p2\":\"SomeNameToCapability\""));
+ Assert.assertTrue(result.contains("\"SomeNameFromRequirement\",null,\"SomeNameToCapability\""));
+ Assert.assertTrue(result.contains("\"relationship\":["));
+ Assert.assertTrue(result.contains("\"n1\":\"n."));
+ Assert.assertTrue(result.contains("\"relations\":[{"));
+ Assert.assertTrue(result.contains("\"name1\":\"SomeNameFromRequirement\""));
+ Assert.assertTrue(result.contains("\"n2\":\"n."));
+ Assert.assertTrue(result.contains("\"p1\":\"SomeNameFromRequirement\""));
+ }
+
+ private void verifyCdump(String result) {
+ String expectedResultStart = "{\"version\":0,\"flowType\":\"templateInfoName\",\"nodes\":[{\"name\":\"SomeNameFromRequirement\",\"description\":\"\",\"id\":\"e45ec9d7-01df-4cb1-896f-aff2a6ca5a8b\",\"nid\":\"n.";
+ String expectedResultMid = "\",\"capabilities\":[{\"name\":\"SomeNameToCapability\"}],\"requirements\":[{\"name\":\"SomeNameFromRequirement\"}],\"properties\":[{}],\"typeinfo\":{\"itemId\":\"e45ec9d7-01df-4cb1-896f-aff2a6ca5a8b/tosca.dcae.nodes.cdapApp.Map\",\"typeinfo\":\"typeInfo\"},\"type\":{\"name\":\"type\"},\"ndata\":{\"name\":\"n.";
+ String expectedResultEnd = "\",\"label\":\"SomeNameFromRequirement\",\"x\":438,\"y\":435,\"px\":437,\"py\":434,\"ports\":[],\"radius\":30}}],\"inputs\":[],\"outputs\":[],\"relations\":[]}";
+ Assert.assertTrue(result.startsWith(expectedResultStart));
+ Assert.assertTrue(result.contains(expectedResultMid));
+ Assert.assertTrue(result.endsWith(expectedResultEnd));
+ }
+}