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