1 package org.opencomp.vid.controller;
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;
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;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.nio.file.Path;
30 import java.util.List;
32 import java.util.UUID;
34 import javax.servlet.ServletContext;
36 import static org.opencomp.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
38 @RunWith(SpringJUnit4ClassRunner.class)
39 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class,SystemProperties.class })
42 public class VidControllerTest {
45 private AsdcClient asdcClient;
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";
58 private ToscaParserImpl2 p2 = new ToscaParserImpl2();
59 private ObjectMapper om = new ObjectMapper();
64 AaiClient client = new AaiClient(context);
65 AaiResponse<?> response = client.getVNFData();
67 System.out.println(new ObjectMapper().writeValueAsString(response));
68 } catch (IOException e) {
69 // TODO Auto-generated catch block
75 public void checkPNFFieldsExist() throws SdcToscaParserException {
76 String serviceRoleString = "serviceRole";
77 String serviceTypeString = "serviceType";
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);
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);
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);
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());
121 //Because we are not supporting the old flow, the JSON are different by definition.
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));
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);
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);
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());
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);
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);
185 private Path getCsarPath(String uuid) throws AsdcCatalogException {
186 return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
189 private org.openecomp.vid.asdc.beans.Service getServiceByUuid(String uuid) throws AsdcCatalogException {
190 return asdcClient.getService(UUID.fromString(uuid));