org.onap migration
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / VidControllerTest.java
1 package org.onap.vid.controller;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import net.javacrumbs.jsonunit.JsonAssert;
5 import org.apache.commons.io.IOUtils;
6 import org.onap.vid.asdc.AsdcCatalogException;
7 import org.onap.vid.asdc.AsdcClient;
8 import org.onap.vid.asdc.parser.ToscaParserImpl2;
9 import org.onap.vid.model.*;
10 import org.openecomp.portalsdk.core.util.SystemProperties;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.mock.web.MockServletContext;
13 import org.springframework.test.context.ContextConfiguration;
14 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
15 import org.springframework.test.context.web.WebAppConfiguration;
16 import org.testng.Assert;
17 import org.testng.annotations.Test;
18
19 import static org.onap.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.file.Path;
24 import java.util.Map;
25 import java.util.UUID;
26
27 //import org.junit.Assert;
28 //import org.junit.Ignore;
29 //import org.junit.Test;
30 //import org.junit.runner.RunWith;
31 //import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32
33 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
34 //@RunWith(SpringJUnit4ClassRunner.class)
35 @WebAppConfiguration
36
37 public class VidControllerTest extends AbstractTestNGSpringContextTests {
38
39     @Autowired
40     MockServletContext context;
41     @Autowired
42     private AsdcClient asdcClient;
43     private ToscaParserImpl2 p2 = new ToscaParserImpl2();
44     private ObjectMapper om = new ObjectMapper();
45
46
47     @Test
48     public void assertEqualsBetweenServices() throws Exception {
49         for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
50             Service expectedService = mockHelper.getNewServiceModel().getService();
51             Service actualService = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getService();
52             assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedService), om.writeValueAsString(actualService));
53         }
54     }
55
56 //    @Test
57 //    public void assertEqualBetweenObjects() throws Exception {
58 //        for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
59 //            final Path csarPath = getCsarPath(mockHelper.getUuid());
60 //            System.out.println("Comparing for csar " + csarPath);
61 //            ServiceModel actualServiceModel = p2.makeServiceModel(csarPath, getServiceByUuid(mockHelper.getUuid()));
62 //            assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(mockHelper.getNewServiceModel()), om.writeValueAsString(actualServiceModel));
63 //        }
64 //    }
65
66 //    @Test
67 //    public void assertEqualsBetweenNetworkNodes() throws Exception {
68 //        for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
69 //            Map<String, Network> expectedNetworksMap = mockHelper.getNewServiceModel().getNetworks();
70 //            Map<String, Network> actualNetworksMap = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getNetworks();
71 //            for (Map.Entry<String, Network> entry : expectedNetworksMap.entrySet()) {
72 //                Network expectedNetwork = entry.getValue();
73 //                Network actualNetwork = actualNetworksMap.get(entry.getKey());
74 //                Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName());
75 //                verifyBaseNodeProperties(expectedNetwork, actualNetwork);
76 //                compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties());
77 //            }
78 //        }
79 //    }
80
81     //Because we are not supporting the old flow, the JSON are different by definition.
82     @Test
83     public void assertEqualsBetweenVnfsOfTosca() throws Exception {
84         for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
85             Map<String, VNF> expectedVnfsMap = mockHelper.getNewServiceModel().getVnfs();
86             Map<String, VNF> actualVnfsMap = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVnfs();
87             for (Map.Entry<String, VNF> entry : expectedVnfsMap.entrySet()) {
88                 VNF expectedVnf = entry.getValue();
89                 VNF actualVnf = actualVnfsMap.get(entry.getKey());
90                 verifyBaseNodeProperties(expectedVnf, actualVnf);
91                 Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName());
92                 compareProperties(expectedVnf.getProperties(), actualVnf.getProperties());
93                 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf));
94             }
95         }
96     }
97
98     @Test
99     public void assertEqualsBetweenVolumeGroups() throws Exception {
100         for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
101             Map<String, VolumeGroup> actualVolumeGroups = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVolumeGroups();
102             Map<String, VolumeGroup> expectedVolumeGroups = mockHelper.getNewServiceModel().getVolumeGroups();
103             JsonAssert.assertJsonEquals(actualVolumeGroups, expectedVolumeGroups);
104         }
105     }
106
107     @Test
108     public void assertEqualsBetweenVfModules() throws Exception {
109         for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
110             Map<String, VfModule> actualVfModules = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVfModules();
111             Map<String, VfModule> expectedVfModules = mockHelper.getNewServiceModel().getVfModules();
112             JsonAssert.assertJsonEquals(actualVfModules, expectedVfModules);
113         }
114     }
115
116     @Test
117     public void assertEqualsBetweenPolicyConfigurationNodes() throws Exception {
118         for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
119             Map<String, PortMirroringConfig> actualConfigurations = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getConfigurations();
120             Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getNewServiceModel().getConfigurations();
121             JsonAssert.assertJsonEquals(actualConfigurations, expectedConfigurations);
122         }
123     }
124
125     @Test
126     public void assertEqualsBetweenServiceProxyNodes() throws Exception {
127         for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
128             Map<String, ServiceProxy> actualServiceProxies = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getServiceProxies();
129             Map<String, ServiceProxy> expectedServiceProxies = mockHelper.getNewServiceModel().getServiceProxies();
130             JsonAssert.assertJsonEquals(actualServiceProxies, expectedServiceProxies);
131         }
132     }
133
134     private void verifyBaseNodeProperties(Node expectedNode, Node actualNode) {
135         Assert.assertEquals(expectedNode.getName(), actualNode.getName());
136         Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid());
137         Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription());
138         Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid());
139         Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid());
140         Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion());
141     }
142
143     private void compareProperties(Map<String, String> expectedProperties, Map<String, String> actualProperties) {
144         for (Map.Entry<String, String> property : expectedProperties.entrySet()) {
145             String expectedValue = property.getValue();
146             String key = property.getKey();
147             String actualValue = actualProperties.get(key);
148             Assert.assertEquals(expectedValue, actualValue);
149         }
150     }
151
152     private ToscaParserMockHelper[] getExpectedServiceModel() throws IOException {
153         ToscaParserMockHelper[] mockHelpers = {
154                 new ToscaParserMockHelper(Constants.vlUuid, Constants.vlFilePath),
155                 new ToscaParserMockHelper(Constants.vfUuid, Constants.vfFilePath),
156                 new ToscaParserMockHelper(Constants.configurationUuid, Constants.configurationFilePath),
157         };
158         for (ToscaParserMockHelper mockHelper : mockHelpers) {
159             InputStream jsonFile = VidControllerTest.class.getClassLoader().getResourceAsStream(mockHelper.getFilePath());
160             String expectedJsonAsString = IOUtils.toString(jsonFile);
161             NewServiceModel newServiceModel1 = om.readValue(expectedJsonAsString, NewServiceModel.class);
162             mockHelper.setNewServiceModel(newServiceModel1);
163         }
164         return mockHelpers;
165     }
166
167     private Path getCsarPath(String uuid) throws AsdcCatalogException {
168         return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
169     }
170
171     private org.onap.vid.asdc.beans.Service getServiceByUuid(String uuid) throws AsdcCatalogException {
172         return asdcClient.getService(UUID.fromString(uuid));
173     }
174
175     public class Constants {
176         public static final String configurationUuid = "ee6d61be-4841-4f98-8f23-5de9da846ca7";
177         public static final String configurationFilePath = "policy-configuration-csar.JSON";
178         static final String vfUuid = "48a52540-8772-4368-9cdb-1f124ea5c931";
179         static final String vlUuid = "cb49608f-5a24-4789-b0f7-2595473cb997";
180         //        public static final String PNFUuid = "68101369-6f08-4e99-9a28-fa6327d344f3";
181         static final String vfFilePath = "vf-csar.JSON";
182         static final String vlFilePath = "vl-csar.JSON";
183 //        public static final String PNFFilePath = "/Users/Oren/Git/Att/vid_internal/vid-app-common/src/main/resources/pnf.csar";
184
185     }
186
187 }