1 package org.onap.vid.controller;
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;
19 import static org.onap.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.file.Path;
25 import java.util.UUID;
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;
33 @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class})
34 //@RunWith(SpringJUnit4ClassRunner.class)
37 public class VidControllerTest extends AbstractTestNGSpringContextTests {
40 MockServletContext context;
42 private AsdcClient asdcClient;
43 private ToscaParserImpl2 p2 = new ToscaParserImpl2();
44 private ObjectMapper om = new ObjectMapper();
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));
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));
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());
81 //Because we are not supporting the old flow, the JSON are different by definition.
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));
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);
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);
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);
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);
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());
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);
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),
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);
167 private Path getCsarPath(String uuid) throws AsdcCatalogException {
168 return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
171 private org.onap.vid.asdc.beans.Service getServiceByUuid(String uuid) throws AsdcCatalogException {
172 return asdcClient.getService(UUID.fromString(uuid));
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";