DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_tools / src / test / java / TemplateContainerTest.java
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;
9 import org.junit.Test;
10 import tools.TemplateContainer;
11
12 import java.util.*;
13
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17
18 public class TemplateContainerTest extends BaseTest {
19
20     private TemplateContainer templateContainer;
21
22     private List<TemplateInfo> templateInfos;
23     private Map<String, List<Item>> elementsByFolderNames;
24     @Before
25     @Override
26     public void setup() {
27         super.setup();
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);
39         item.setItemId("");
40         Model model =  new Model();
41         model.setItemId("");
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);
46
47     }
48
49     @Test
50     public void getCdumps_emptyTemplateInfo_returnEmptyMap() {
51         templateContainer = new TemplateContainer(report, dcaeRestClient, new ArrayList<>(), new HashMap<>());
52
53         Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
54
55         Assert.assertTrue(templateInfoJsonObjectMap.size() == 0);
56     }
57
58     @Test
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);
66
67         Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
68
69         verify(report).addErrorMessage(anyString());
70         Assert.assertTrue(templateInfoJsonObjectMap.size() == 0);
71     }
72
73     @Test
74     public void getCdumps_returnOneCdump() {
75         templateContainer = new TemplateContainer(report, dcaeRestClient, templateInfos, elementsByFolderNames);
76
77         Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
78         JsonObject jsonObject = templateInfoJsonObjectMap.get(templateInfos.get(0));
79         String result = jsonObject.toString();
80
81         verifyCdump(result);
82         verify(report, times(0)).addErrorMessage(anyString());
83         Assert.assertTrue(templateInfoJsonObjectMap.size() == 1);
84     }
85
86     @Test
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);
112         item.setItemId("");
113         Model model =  new Model();
114         model.setItemId("");
115         List<Model> models = Collections.singletonList(model);
116         item.setModels(models);
117         itemList.add(item);
118         item = new Item();
119         item.setName(ELEMENT_NAME2);
120         item.setItemId("");
121         model =  new Model();
122         model.setItemId("");
123         models = Collections.singletonList(model);
124         item.setModels(models);
125         itemList.add(item);
126         elementsByFolderNames.put(TEMPLATE_INFO_NAME, itemList);
127         templateContainer = new TemplateContainer(report, dcaeRestClient, templateInfos, elementsByFolderNames);
128
129         Map<TemplateInfo, JsonObject> templateInfoJsonObjectMap = templateContainer.getCdumps();
130         JsonObject jsonObject = templateInfoJsonObjectMap.get(templateInfos.get(0));
131         String result = jsonObject.toString();
132
133         verifyCdumpRelations(result);
134         verify(report, times(0)).addErrorMessage(anyString());
135         Assert.assertTrue(templateInfoJsonObjectMap.size() == 1);
136     }
137
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\""));
147     }
148
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));
156     }
157 }