Upgrade sonar plugin
[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.portalsdk.core.util.SystemProperties;
10 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
11 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
12 import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory;
13 import org.openecomp.sdc.tosca.parser.impl.SdcTypes;
14 import org.openecomp.vid.aai.AaiClient;
15 import org.openecomp.vid.aai.AaiResponse;
16 import org.openecomp.vid.asdc.AsdcCatalogException;
17 import org.openecomp.vid.asdc.AsdcClient;
18 import org.openecomp.vid.asdc.parser.ToscaParserImpl2;
19 import org.openecomp.vid.controller.WebConfig;
20 import org.openecomp.vid.model.*;
21 import org.openecomp.vid.properties.AsdcClientConfiguration;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.test.context.ContextConfiguration;
24 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
25 import org.springframework.test.context.web.WebAppConfiguration;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.file.Path;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.UUID;
34
35 import javax.servlet.ServletContext;
36
37 import static org.opencomp.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
38
39 @RunWith(SpringJUnit4ClassRunner.class)
40 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class,SystemProperties.class })
41 @WebAppConfiguration
42
43 public class VidControllerTest {
44
45         @Autowired
46         private AsdcClient asdcClient;
47         
48     @Autowired
49     ServletContext context;
50     public class Constants{
51         public static final String vfUuid = "48a52540-8772-4368-9cdb-1f124ea5c931";
52         public static final String vlUuid = "68101369-6f08-4e99-9a28-fa6327d344f3";
53         public static final String PNFUuid = "73e1322a-8a9a-49dc-9558-b0c5c5770e4a";
54         public static final String vfFilePath = "vf-csar.JSON";
55         public static final String vlFilePath = "vl-csar.JSON";
56         public static final String PNFFilePath = "/vid_internal/vid-app-common/src/main/resources/pnf.csar";
57     }
58
59     private ToscaParserImpl2 p2 = new ToscaParserImpl2();
60     private ObjectMapper om = new ObjectMapper();
61
62     
63     @Test
64     public void test() {
65         AaiClient client = new AaiClient(context);
66         AaiResponse<?> response = client.getVNFData();
67          try {
68                         System.out.println(new ObjectMapper().writeValueAsString(response));
69                 } catch (IOException e) {
70                         // TODO Auto-generated catch block
71                         e.printStackTrace();
72                 }
73     }
74     
75     @Test
76     public void checkPNFFieldsExist() throws SdcToscaParserException, AsdcCatalogException {
77         String serviceRoleString = "serviceRole";
78         String serviceTypeString = "serviceType";
79
80         SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
81         ISdcCsarHelper sdcCsarHelper = factory.getSdcCsarHelper(getCsarPath(Constants.PNFUuid).toString());
82         List<org.openecomp.sdc.toscaparser.api.NodeTemplate> pnfs = sdcCsarHelper.getServiceNodeTemplateBySdcType(SdcTypes.PNF);
83         Assert.assertEquals(sdcCsarHelper.getServiceMetadata().getValue(serviceTypeString).toLowerCase(),"transport");
84         Assert.assertEquals(sdcCsarHelper.getServiceMetadata().getValue(serviceRoleString).toLowerCase(),"pnf");
85         Assert.assertTrue(pnfs.size()>0);
86
87     }
88
89
90     @Test
91     public void assertEqualsBetweenServices() throws Exception {
92         for (ToscaParserMockHelper mockHelper: getExpectedServiceModel()) {
93             Service expectedService = mockHelper.getNewServiceModel().getService();
94             Service actualService = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getService();
95             JsonAssert.assertJsonEquals(expectedService, actualService);
96         }
97     }
98
99     //@Test
100         // bug in SDC tosca filterNodeTemplatePropertiesByValue
101         public void assertEqualBetweenObjects() throws Exception {
102                 for (ToscaParserMockHelper mockHelper: getExpectedServiceModel()) {
103                         ServiceModel actualServiceModel = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid()));
104                         JsonAssert.assertJsonEquals(mockHelper.getNewServiceModel(), actualServiceModel);
105                         /*java.lang.AssertionError: JSON documents are different:
106                         Different keys found in node "networks.ExtVL 0.commands". Expected [exVL_naming#naming_policy, network_role, network_scope], got [network_role, network_scope]. Missing: "networks.ExtVL 0.commands.exVL_naming#naming_policy"
107                         Different keys found in node "networks.ExtVL 0.inputs". Expected [exVL_naming#naming_policy, network_role, network_scope], got [network_role, network_scope]. Missing: "networks.ExtVL 0.inputs.exVL_naming#naming_policy"*/
108                 }
109         }
110
111         @Test
112         public void assertEqualsBetweenNetworkNodes() throws Exception {
113                 for (ToscaParserMockHelper mockHelper: getExpectedServiceModel()) {
114                         Map<String, Network> expectedNetworksMap = mockHelper.getNewServiceModel().getNetworks();
115                         Map<String, Network> actualNetworksMap = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getNetworks();
116                         for (Map.Entry<String, Network> entry : expectedNetworksMap.entrySet()) {
117                                 Network expectedNetwork = entry.getValue();
118                                 Network actualNetwork = actualNetworksMap.get(entry.getKey());
119                                 Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName());
120                                 verifyBaseNodeProperties(expectedNetwork, actualNetwork);
121                                 compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties());
122                         }
123                 }
124         }
125
126         //Because we are not supporting the old flow, the JSON are different by definition.
127         @Test
128         public void assertEqualsBetweenVnfsOfTosca() throws Exception {
129                 for (ToscaParserMockHelper mockHelper: getExpectedServiceModel()) {
130                         Map<String, VNF> expectedVnfsMap = mockHelper.getNewServiceModel().getVnfs();
131                         Map<String, VNF> actualVnfsMap = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVnfs();
132                         for (Map.Entry<String, VNF> entry : expectedVnfsMap.entrySet()) {
133                                 VNF expectedVnf = entry.getValue();
134                                 VNF actualVnf = actualVnfsMap.get(entry.getKey());
135                                 verifyBaseNodeProperties(expectedVnf, actualVnf);
136                                 Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName());
137                                 compareProperties(expectedVnf.getProperties(), actualVnf.getProperties());
138                                 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf));
139                         }
140                 }
141         }
142
143         @Test
144         public void assertEqualsBetweenVolumeGroups() throws Exception {
145                 for (ToscaParserMockHelper mockHelper: getExpectedServiceModel()) {
146                         Path cSarPath = getCsarPath(mockHelper.getUuid());
147                         Map<String, VolumeGroup> actualVolumeGroups = new HashMap<>();
148                         if(cSarPath != null) {
149                                 actualVolumeGroups = p2.makeServiceModel(cSarPath, getServiceByUuid(mockHelper.getUuid())).getVolumeGroups();
150                         }
151                         Map<String, VolumeGroup> expectedVolumeGroups = mockHelper.getNewServiceModel().getVolumeGroups();
152                         JsonAssert.assertJsonEquals(actualVolumeGroups, expectedVolumeGroups);
153                 }
154         }
155
156         @Test
157         public void assertEqualsBetweenVfModules() throws Exception {
158                 for (ToscaParserMockHelper mockHelper: getExpectedServiceModel()) {
159                         Map<String, VfModule> actualVfModules = p2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVfModules();
160                         Map<String, VfModule> expectedVfModules = mockHelper.getNewServiceModel().getVfModules();
161                         JsonAssert.assertJsonEquals(actualVfModules, expectedVfModules);
162                 }
163         }
164
165         private void verifyBaseNodeProperties(Node expectedNode, Node actualNode) {
166                 Assert.assertEquals(expectedNode.getName(), actualNode.getName());
167                 Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid());
168                 Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription());
169                 Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid());
170                 Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid());
171                 Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion());
172         }
173
174         private void compareProperties(Map<String, String> expectedProperties, Map<String, String> actualProperties) {
175                 for (Map.Entry<String, String> property : expectedProperties.entrySet()) {
176                         String expectedValue = property.getValue();
177                         String key = property.getKey();
178                         String actualValue = actualProperties.get(key);
179                         Assert.assertEquals(expectedValue, actualValue);
180                 }
181         }
182
183         private ToscaParserMockHelper[] getExpectedServiceModel() throws IOException {
184                 ToscaParserMockHelper[] mockHelpers = {new ToscaParserMockHelper(Constants.vlUuid, Constants.vlFilePath), new ToscaParserMockHelper(Constants.vfUuid, Constants.vfFilePath)};
185                 for(ToscaParserMockHelper mockHelper: mockHelpers) {
186                         InputStream jsonFile = VidControllerTest.class.getClassLoader().getResourceAsStream(mockHelper.getFilePath());
187                         String expectedJsonAsString = IOUtils.toString(jsonFile).toString();
188                         NewServiceModel newServiceModel1 = om.readValue(expectedJsonAsString, NewServiceModel.class);
189                         mockHelper.setNewServiceModel(newServiceModel1);
190                 }
191                 return mockHelpers;
192         }
193
194         private Path getCsarPath(String uuid) throws AsdcCatalogException {
195                 return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
196         }
197
198         private org.openecomp.vid.asdc.beans.Service getServiceByUuid(String uuid) throws AsdcCatalogException {
199                 return asdcClient.getService(UUID.fromString(uuid));
200         }
201 }