99f8ef38a3a75228d9b9c76a9ef07fb37e0e3498
[vid.git] / vid-app-common / src / test / java / org / onap / vid / asdc / parser / ToscaParserImpl2Test.java
1 package org.onap.vid.asdc.parser;
2
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.SerializationFeature;
5 import com.google.common.collect.ImmutableList;
6 import org.apache.log4j.Logger;
7 import org.junit.Assert;
8 import org.onap.vid.controller.WebConfig;
9 import org.onap.vid.model.VfModule;
10 import org.onap.vid.model.VolumeGroup;
11 import org.onap.vid.properties.AsdcClientConfiguration;
12 import org.openecomp.portalsdk.core.util.SystemProperties;
13 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
14 import org.openecomp.sdc.toscaparser.api.Group;
15 import org.openecomp.sdc.toscaparser.api.NodeTemplate;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.test.context.ContextConfiguration;
18 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
19 import org.springframework.test.context.web.WebAppConfiguration;
20 import org.springframework.web.context.WebApplicationContext;
21 import org.testng.annotations.BeforeMethod;
22 import org.testng.annotations.Test;
23
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27
28 import static org.hamcrest.Matchers.*;
29 import static org.junit.Assert.assertThat;
30 import static org.mockito.Mockito.*;
31
32 @Test
33 @ContextConfiguration(classes = { WebConfig.class, AsdcClientConfiguration.class, SystemProperties.class })
34 @WebAppConfiguration
35 public class ToscaParserImpl2Test extends AbstractTestNGSpringContextTests {
36
37     private final String myUUID = "myUUID";
38     private static final Logger log = Logger.getLogger(ToscaParserImpl2Test.class);
39
40     @Autowired
41     private ToscaParserImpl2 toscaParserImpl2;
42
43     @Autowired
44     private WebApplicationContext wac;
45
46     @BeforeMethod
47     private void verifyWiring() {
48         Assert.assertNotNull(wac);
49         Assert.assertNotNull(toscaParserImpl2);
50     }
51
52     @Test
53     public void testGetNFModuleFromVf() throws Exception {
54         ISdcCsarHelper csarHelper = getMockedSdcCsarHelper();
55
56         Map<String, VfModule> vfModulesFromVF = toscaParserImpl2.getVfModulesFromVF(csarHelper, myUUID);
57
58         assertThat(vfModulesFromVF, allOf(
59                 aMapWithSize(2),
60                 hasKey("withoutVol"),
61                 hasKey("withVol")
62         ));
63
64         verify(csarHelper, only()).getVfModulesByVf(anyString());
65     }
66
67     @Test
68     public void testGetVolumeGroupsFromVF() throws Exception {
69         ISdcCsarHelper csarHelper = getMockedSdcCsarHelper();
70
71         Map<String, VolumeGroup> volumeGroupsFromVF = toscaParserImpl2.getVolumeGroupsFromVF(csarHelper, myUUID);
72
73         assertThat(volumeGroupsFromVF, allOf(
74                 aMapWithSize(1),
75                 hasKey("withVol")
76         ));
77
78         verify(csarHelper, only()).getVfModulesByVf(anyString());
79     }
80
81     private ISdcCsarHelper getMockedSdcCsarHelper() {
82         ISdcCsarHelper csarHelper = mock(ISdcCsarHelper.class);
83
84 //        ThreadLocalsHolder.setCollector(new ExceptionCollector("c:\\temp\\foo"));
85
86         Group withVol = createMinimalGroup("withVol", true);
87         Group withoutVol = createMinimalGroup("withoutVol", false);
88
89         when(csarHelper.getVfModulesByVf(myUUID))
90                 .thenReturn(ImmutableList.of(withVol, withoutVol));
91
92         return csarHelper;
93     }
94
95     private static Group createMinimalGroup(String name, boolean isVolumeGroup) {
96         LinkedHashMap<String, Object>
97                 templates,
98                 properties,
99                 metadata,
100                 customDef,
101                 vfModule,
102                 vfModuleProperties,
103                 volumeGroup;
104
105         templates = new LinkedHashMap<>();
106         templates.put("type", "org.onap.groups.VfModule");
107
108         properties = addNewNamedMap(templates, "properties");
109         properties.put("volume_group", isVolumeGroup);
110
111         metadata = addNewNamedMap(templates, "metadata");
112
113         ArrayList<NodeTemplate> memberNodes = new ArrayList<>();
114
115         customDef = new LinkedHashMap<>();
116         vfModule = addNewNamedMap(customDef, "org.onap.groups.VfModule");
117         vfModuleProperties = addNewNamedMap(vfModule, "properties");
118 //        vfModule.put("derived_from", "tosca.groups.Root");
119 //        vfModule.put("description", "Grouped all heat resources which are in the same VF Module");
120
121         volumeGroup = addNewNamedMap(vfModuleProperties, "volume_group");
122 //        volumeGroup.put("description", "volume_group");
123         volumeGroup.put("type", "boolean");
124         volumeGroup.put("default", false);
125         volumeGroup.put("required", true);
126
127
128         Group group = new Group(
129                 name,
130                 templates,
131                 memberNodes,
132                 customDef
133         );
134
135         try {
136             log.info(String.format("Built a group: %s",
137                     (new com.fasterxml.jackson.databind.ObjectMapper())
138                             .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
139                             .writeValueAsString(group)
140             ));
141         } catch (JsonProcessingException e) {
142             throw new RuntimeException(e);
143         }
144
145         return group;
146     }
147
148     private static LinkedHashMap<String, Object> addNewNamedMap(LinkedHashMap<String, Object> root, String key) {
149         LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
150         root.put(key, properties);
151         return properties;
152     }
153
154 }