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.vid.asdc.AsdcCatalogException;
10 import org.openecomp.vid.asdc.AsdcClient;
11 import org.openecomp.vid.asdc.parser.ToscaParserImpl2;
12 import org.openecomp.vid.controller.WebConfig;
13 import org.openecomp.vid.model.*;
14 import org.openecomp.vid.properties.AsdcClientConfiguration;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.test.context.ContextConfiguration;
17 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.nio.file.Path;
22 import java.util.UUID;
24 import static org.opencomp.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
26 @RunWith(SpringJUnit4ClassRunner.class)
27 @ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class})
28 public class VidControllerTest {
31 private AsdcClient asdcClient;
33 private String uuid = "f430728a-4530-42be-a577-1206b9484cef";
34 //TODO: add as a test case.
35 private String vfFilePath = "vf-csar.JSON";
36 private String vlFilePath = "vl-csar.JSON";
38 private ToscaParserImpl2 p2 = new ToscaParserImpl2();
39 private ObjectMapper om = new ObjectMapper();
40 final InputStream jsonFile = VidControllerTest.class.getClassLoader().getResourceAsStream(vfFilePath);
44 public void assertEqualsBetweenServices() throws Exception {
45 Service expectedService = getExpectedServiceModel().getService();
46 Service actualService = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getService();
47 JsonAssert.assertJsonEquals(expectedService, actualService);
51 public void assertEqualBetweenObjects() throws Exception {
52 ServiceModel actualServiceModel = p2.makeServiceModel(getCsarPath(), getServiceByUuid());
53 JsonAssert.assertJsonEquals(getExpectedServiceModel(), actualServiceModel);
57 public void assertEqualsBetweenNetworkNodes() throws Exception {
58 Map<String, Network> expectedNetworksMap = getExpectedServiceModel().getNetworks();
59 Map<String, Network> actualNetworksMap = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getNetworks();
60 for (Map.Entry<String, Network> entry : expectedNetworksMap.entrySet()) {
61 Network expectedNetwork = entry.getValue();
62 Network actualNetwork = actualNetworksMap.get(entry.getKey());
63 Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName());
64 verifyBaseNodeProperties(expectedNetwork, actualNetwork);
65 compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties());
69 //Because we are not supporting the old flow, the JSON are different by definition.
71 public void assertEqualsBetweenVnfsOfTosca() throws Exception {
72 Map<String, VNF> expectedVnfsMap = getExpectedServiceModel().getVnfs();
73 Map<String, VNF> actualVnfsMap = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVnfs();
74 for (Map.Entry<String, VNF> entry : expectedVnfsMap.entrySet()) {
75 VNF expectedVnf = entry.getValue();
76 VNF actualVnf = actualVnfsMap.get(entry.getKey());
77 verifyBaseNodeProperties(expectedVnf, actualVnf);
78 Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName());
79 compareProperties(expectedVnf.getProperties(), actualVnf.getProperties());
80 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf));
85 public void assertEqualsBetweenVolumeGroups() throws Exception {
86 Map<String, VolumeGroup> actualVolumeGroups = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVolumeGroups();
87 Map<String, VolumeGroup> expectedVolumeGroups = getExpectedServiceModel().getVolumeGroups();
88 JsonAssert.assertJsonEquals(actualVolumeGroups, expectedVolumeGroups);
92 public void assertEqualsBetweenVfModules() throws Exception {
93 Map<String, VfModule> actualVfModules = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVfModules();
94 Map<String, VfModule> expectedVfModules = getExpectedServiceModel().getVfModules();
95 JsonAssert.assertJsonEquals(actualVfModules, expectedVfModules);
98 private void verifyBaseNodeProperties(Node expectedNode, Node actualNode) {
99 Assert.assertEquals(expectedNode.getName(), actualNode.getName());
100 Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid());
101 Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription());
102 Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid());
103 Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid());
104 Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion());
107 private void compareProperties(Map<String, String> expectedProperties, Map<String, String> actualProperties) {
108 for (Map.Entry<String, String> property : expectedProperties.entrySet()) {
109 String expectedValue = property.getValue();
110 String key = property.getKey();
111 String actualValue = actualProperties.get(key);
112 Assert.assertEquals(expectedValue, actualValue);
116 private NewServiceModel getExpectedServiceModel() throws IOException {
117 String expectedJsonAsString = IOUtils.toString(jsonFile).toString();
118 return om.readValue(expectedJsonAsString,NewServiceModel.class);
121 private Path getCsarPath() throws AsdcCatalogException {
122 return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
125 private org.openecomp.vid.asdc.beans.Service getServiceByUuid() throws AsdcCatalogException {
126 return asdcClient.getService(UUID.fromString(uuid));