1 import com.google.gson.JsonObject;
2 import json.response.ItemsResponse.Item;
3 import json.response.ItemsResponse.Model;
4 import json.templateInfo.Composition;
5 import json.templateInfo.Relation;
6 import json.templateInfo.TemplateInfo;
7 import org.junit.Assert;
8 import org.junit.Before;
10 import tools.TemplateContainer;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
18 public class TemplateContainerTest extends BaseTest {
20 private TemplateContainer templateContainer;
22 private List<TemplateInfo> templateInfos;
23 private Map<String, List<Item>> elementsByFolderNames;
28 templateInfos = new ArrayList<>();
29 TemplateInfo templateInfo = new TemplateInfo();
30 templateInfo.setName(TEMPLATE_INFO_NAME);
31 Composition composition = new Composition();
32 composition.setType(ELEMENT_NAME3);
33 composition.setAlias(ALIAS_NAME3);
34 templateInfo.setComposition(Collections.singletonList(composition));
35 templateInfos.add(templateInfo);
36 elementsByFolderNames = new HashMap<>();
37 Item item = new Item();
38 item.setName(ELEMENT_NAME3);
40 Model model = new Model();
42 List<Model> models = Collections.singletonList(model);
43 item.setModels(models);
44 List<Item> items = new ArrayList<>(Collections.singletonList(item));
45 elementsByFolderNames.put(TEMPLATE_INFO_NAME, items);
50 public void getCdumps_emptyTemplateInfo_returnEmptyMap() {
51 templateContainer = new TemplateContainer(report, dcaeRestClient, new ArrayList<>(), new HashMap<>());
53 Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
55 Assert.assertTrue(templateInfoJsonObjectMap.size() == 0);
59 public void getCdumps_returnNotFoundEmptyList() {
60 elementsByFolderNames = new HashMap<>();
61 Item item = new Item();
62 item.setName(ELEMENT_NAME2);
63 List<Item> items = new ArrayList<>(Collections.singletonList(item));
64 elementsByFolderNames.put(TEMPLATE_INFO_NAME, items);
65 templateContainer = new TemplateContainer(report, dcaeRestClient, templateInfos, elementsByFolderNames);
67 Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
69 verify(report).addErrorMessage(anyString());
70 Assert.assertTrue(templateInfoJsonObjectMap.size() == 0);
74 public void getCdumps_returnOneCdump() {
75 templateContainer = new TemplateContainer(report, dcaeRestClient, templateInfos, elementsByFolderNames);
77 Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
78 JsonObject jsonObject = templateInfoJsonObjectMap.get(templateInfos.get(0));
79 String result = jsonObject.toString();
82 verify(report, times(0)).addErrorMessage(anyString());
83 Assert.assertTrue(templateInfoJsonObjectMap.size() == 1);
87 public void getChumps_returnOneChumpWithRelations() {
88 templateInfos = new ArrayList<>();
89 TemplateInfo templateInfo = new TemplateInfo();
90 templateInfo.setName(TEMPLATE_INFO_NAME);
91 List<Composition> compositionList = new ArrayList<>();
92 Composition composition = new Composition();
93 composition.setType(ELEMENT_NAME3);
94 composition.setAlias(ALIAS_NAME3);
95 compositionList.add(composition);
96 composition = new Composition();
97 composition.setType(ELEMENT_NAME2);
98 composition.setAlias(ALIAS_NAME2);
99 compositionList.add(composition);
100 templateInfo.setComposition(compositionList);
101 Relation relation = new Relation();
102 relation.setFromComponent(ALIAS_NAME3 + ".SomeNameFromRequirement");
103 relation.setToComponent(ALIAS_NAME2 + ".SomeNameToCapability");
104 relation.setFromRequirement("SomeNameFromRequirement");
105 relation.setToCapability("SomeNameToCapability");
106 templateInfo.setRelations(Collections.singletonList(relation));
107 templateInfos.add(templateInfo);
108 elementsByFolderNames = new HashMap<>();
109 List<Item> itemList = new ArrayList<>();
110 Item item = new Item();
111 item.setName(ELEMENT_NAME3);
113 Model model = new Model();
115 List<Model> models = Collections.singletonList(model);
116 item.setModels(models);
119 item.setName(ELEMENT_NAME2);
123 models = Collections.singletonList(model);
124 item.setModels(models);
126 elementsByFolderNames.put(TEMPLATE_INFO_NAME, itemList);
127 templateContainer = new TemplateContainer(report, dcaeRestClient, templateInfos, elementsByFolderNames);
129 Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
130 JsonObject jsonObject = templateInfoJsonObjectMap.get(templateInfos.get(0));
131 String result = jsonObject.toString();
133 verifyCdumpRelations(result);
134 verify(report, times(0)).addErrorMessage(anyString());
135 Assert.assertTrue(templateInfoJsonObjectMap.size() == 1);
138 private void verifyCdumpRelations(String result) {
139 Assert.assertTrue(result.contains("p2\":\"SomeNameToCapability\""));
140 Assert.assertTrue(result.contains("\"SomeNameFromRequirement\",null,\"SomeNameToCapability\""));
141 Assert.assertTrue(result.contains("\"relationship\":["));
142 Assert.assertTrue(result.contains("\"n1\":\"n."));
143 Assert.assertTrue(result.contains("\"relations\":[{"));
144 Assert.assertTrue(result.contains("\"name1\":\"SomeNameFromRequirement\""));
145 Assert.assertTrue(result.contains("\"n2\":\"n."));
146 Assert.assertTrue(result.contains("\"p1\":\"SomeNameFromRequirement\""));
149 private void verifyCdump(String result) {
150 String expectedResultStart = "{\"version\":0,\"flowType\":\"templateInfoName\",\"nodes\":[{\"name\":\"SomeNameFromRequirement\",\"description\":\"\",\"id\":\"e45ec9d7-01df-4cb1-896f-aff2a6ca5a8b\",\"nid\":\"n.";
151 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.";
152 String expectedResultEnd = "\",\"label\":\"SomeNameFromRequirement\",\"x\":438,\"y\":435,\"px\":437,\"py\":434,\"ports\":[],\"radius\":30}}],\"inputs\":[],\"outputs\":[],\"relations\":[]}";
153 Assert.assertTrue(result.startsWith(expectedResultStart));
154 Assert.assertTrue(result.contains(expectedResultMid));
155 Assert.assertTrue(result.endsWith(expectedResultEnd));