Merge "Fix to pass calling application identifier to SO"
[vid.git] / vid-app-common / src / test / java / org / opencomp / vid / controller / VidControllerTest.java
1 package org.opencomp.vid.controller;
2
3 import net.javacrumbs.jsonunit.JsonAssert;
4 import org.apache.commons.io.IOUtils;
5 import org.codehaus.jackson.map.ObjectMapper;
6 import org.junit.Assert;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 import org.openecomp.vid.asdc.AsdcCatalogException;
10 import org.openecomp.vid.asdc.AsdcClient;
11 import org.openecomp.vid.asdc.parser.ToscaParserImpl2;
12 import org.openecomp.vid.controller.WebConfig;
13 import org.openecomp.vid.model.*;
14 import org.openecomp.vid.properties.AsdcClientConfiguration;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.test.context.ContextConfiguration;
17 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.nio.file.Path;
21 import java.util.Map;
22 import java.util.UUID;
23
24 import static org.opencomp.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
25
26 @RunWith(SpringJUnit4ClassRunner.class)
27 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class})
28 public class VidControllerTest {
29
30     @Autowired
31     private AsdcClient asdcClient;
32
33     private String uuid = "f430728a-4530-42be-a577-1206b9484cef";
34     //TODO: add as a test case.
35     private String vfFilePath = "vf-csar.JSON";
36     private String vlFilePath = "vl-csar.JSON";
37
38     private ToscaParserImpl2 p2 = new ToscaParserImpl2();
39     private ObjectMapper om = new ObjectMapper();
40     final InputStream jsonFile = VidControllerTest.class.getClassLoader().getResourceAsStream(vfFilePath);
41
42
43     @Test
44     public void assertEqualsBetweenServices() throws Exception {
45         Service expectedService = getExpectedServiceModel().getService();
46         Service actualService = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getService();
47         JsonAssert.assertJsonEquals(expectedService, actualService);
48     }
49
50     @Test
51     public void assertEqualBetweenObjects() throws Exception {
52         ServiceModel actualServiceModel = p2.makeServiceModel(getCsarPath(), getServiceByUuid());
53         JsonAssert.assertJsonEquals(getExpectedServiceModel(), actualServiceModel);
54     }
55
56     @Test
57     public void assertEqualsBetweenNetworkNodes() throws Exception {
58         Map<String, Network> expectedNetworksMap = getExpectedServiceModel().getNetworks();
59         Map<String, Network> actualNetworksMap = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getNetworks();
60         for (Map.Entry<String, Network> entry : expectedNetworksMap.entrySet()) {
61             Network expectedNetwork = entry.getValue();
62             Network actualNetwork = actualNetworksMap.get(entry.getKey());
63             Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName());
64             verifyBaseNodeProperties(expectedNetwork, actualNetwork);
65             compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties());
66         }
67     }
68
69     //Because we are not supporting the old flow, the JSON are different by definition.
70     @Test
71     public void assertEqualsBetweenVnfsOfTosca() throws Exception {
72         Map<String, VNF> expectedVnfsMap = getExpectedServiceModel().getVnfs();
73         Map<String, VNF> actualVnfsMap = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVnfs();
74         for (Map.Entry<String, VNF> entry : expectedVnfsMap.entrySet()) {
75             VNF expectedVnf = entry.getValue();
76             VNF actualVnf = actualVnfsMap.get(entry.getKey());
77             verifyBaseNodeProperties(expectedVnf, actualVnf);
78             Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName());
79             compareProperties(expectedVnf.getProperties(), actualVnf.getProperties());
80             assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf));
81         }
82     }
83
84     @Test
85     public void assertEqualsBetweenVolumeGroups() throws Exception {
86         Map<String, VolumeGroup> actualVolumeGroups = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVolumeGroups();
87         Map<String, VolumeGroup> expectedVolumeGroups = getExpectedServiceModel().getVolumeGroups();
88         JsonAssert.assertJsonEquals(actualVolumeGroups, expectedVolumeGroups);
89     }
90
91     @Test
92     public void assertEqualsBetweenVfModules() throws Exception {
93         Map<String, VfModule> actualVfModules = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVfModules();
94         Map<String, VfModule> expectedVfModules = getExpectedServiceModel().getVfModules();
95         JsonAssert.assertJsonEquals(actualVfModules, expectedVfModules);
96     }
97
98     private void verifyBaseNodeProperties(Node expectedNode, Node actualNode) {
99         Assert.assertEquals(expectedNode.getName(), actualNode.getName());
100         Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid());
101         Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription());
102         Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid());
103         Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid());
104         Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion());
105     }
106
107     private void compareProperties(Map<String, String> expectedProperties, Map<String, String> actualProperties) {
108         for (Map.Entry<String, String> property : expectedProperties.entrySet()) {
109             String expectedValue = property.getValue();
110             String key = property.getKey();
111             String actualValue = actualProperties.get(key);
112             Assert.assertEquals(expectedValue, actualValue);
113         }
114     }
115
116     private NewServiceModel getExpectedServiceModel() throws IOException {
117         String expectedJsonAsString  = IOUtils.toString(jsonFile).toString();
118         return om.readValue(expectedJsonAsString,NewServiceModel.class);
119     }
120
121     private Path getCsarPath() throws AsdcCatalogException {
122         return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
123     }
124
125     private org.openecomp.vid.asdc.beans.Service getServiceByUuid() throws AsdcCatalogException {
126         return asdcClient.getService(UUID.fromString(uuid));
127     }
128 }