1 package org.onap.vid.asdc.parser;
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.fasterxml.jackson.databind.SerializationFeature;
6 import com.google.common.collect.ImmutableList;
7 import net.javacrumbs.jsonunit.JsonAssert;
8 import org.apache.commons.io.IOUtils;
9 import org.apache.log4j.Logger;
10 import org.json.JSONObject;
11 import org.json.JSONTokener;
12 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
13 import org.onap.sdc.toscaparser.api.Group;
14 import org.onap.sdc.toscaparser.api.NodeTemplate;
15 import org.onap.vid.asdc.AsdcCatalogException;
16 import org.onap.vid.asdc.AsdcClient;
17 import org.onap.vid.asdc.local.LocalAsdcClient;
18 import org.onap.vid.model.*;
19 import org.onap.vid.controllers.ToscaParserMockHelper;
20 import org.testng.Assert;
21 import org.testng.annotations.BeforeClass;
22 import org.testng.annotations.Test;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.charset.StandardCharsets;
27 import java.nio.file.Path;
30 import static org.hamcrest.Matchers.*;
31 import static org.junit.Assert.assertThat;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
34 import static org.onap.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
37 public class ToscaParserImpl2Test {
39 private final String myUUID = "myUUID";
40 private static final Logger log = Logger.getLogger(ToscaParserImpl2Test.class);
42 private ToscaParserImpl2 toscaParserImpl2 = new ToscaParserImpl2();
44 private AsdcClient asdcClient;
45 private ObjectMapper om = new ObjectMapper();
48 void init() throws IOException {
50 final InputStream asdcServicesFile = this.getClass().getClassLoader().getResourceAsStream("sdcservices.json");
52 final JSONTokener jsonTokener = new JSONTokener(IOUtils.toString(asdcServicesFile));
53 final JSONObject sdcServicesCatalog = new JSONObject(jsonTokener);
55 asdcClient = new LocalAsdcClient.Builder().catalog(sdcServicesCatalog).build();
60 public void assertEqualsBetweenServices() throws Exception {
61 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
62 Service expectedService = mockHelper.getNewServiceModel().getService();
63 Service actualService = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getService();
64 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedService), om.writeValueAsString(actualService));
69 public void assertEqualBetweenObjects() throws Exception {
70 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
71 final Path csarPath = getCsarPath(mockHelper.getUuid());
72 System.out.println("Comparing for csar " + csarPath);
73 ServiceModel actualServiceModel = toscaParserImpl2.makeServiceModel(csarPath, getServiceByUuid(mockHelper.getUuid()));
74 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(mockHelper.getNewServiceModel()), om.writeValueAsString(actualServiceModel));
79 public void assertEqualsBetweenNetworkNodes() throws Exception {
80 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
81 Map<String, Network> expectedNetworksMap = mockHelper.getNewServiceModel().getNetworks();
82 Map<String, Network> actualNetworksMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getNetworks();
83 for (Map.Entry<String, Network> entry : expectedNetworksMap.entrySet()) {
84 Network expectedNetwork = entry.getValue();
85 Network actualNetwork = actualNetworksMap.get(entry.getKey());
86 Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName());
87 verifyBaseNodeProperties(expectedNetwork, actualNetwork);
88 compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties());
93 //Because we are not supporting the old flow, the JSON are different by definition.
95 public void assertEqualsBetweenVnfsOfTosca() throws Exception {
96 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
97 Map<String, VNF> expectedVnfsMap = mockHelper.getNewServiceModel().getVnfs();
98 Map<String, VNF> actualVnfsMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVnfs();
99 for (Map.Entry<String, VNF> entry : expectedVnfsMap.entrySet()) {
100 VNF expectedVnf = entry.getValue();
101 VNF actualVnf = actualVnfsMap.get(entry.getKey());
102 verifyBaseNodeProperties(expectedVnf, actualVnf);
103 Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName());
104 compareProperties(expectedVnf.getProperties(), actualVnf.getProperties());
105 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf));
111 public void assertEqualsBetweenCollectionResourcesOfTosca() throws Exception {
112 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
113 Map<String, CR> expectedVnfsMap = mockHelper.getNewServiceModel().getCollectionResource();
114 Map<String, CR> actualCRsMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getCollectionResource();
115 if(!actualCRsMap.isEmpty()) {
116 for (Map.Entry<String, CR> entry : expectedVnfsMap.entrySet()) {
117 CR expectedCR = entry.getValue();
118 CR actualCR = actualCRsMap.get(entry.getKey());
119 verifyCollectionResource(expectedCR, actualCR);
120 Assert.assertEquals(expectedCR.getName(), actualCR.getName());
121 compareProperties(expectedCR.getProperties(), actualCR.getProperties());
122 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedCR), om.writeValueAsString(actualCR));
128 private void verifyCollectionResource(CR expectedCR, CR actualCR) {
129 verifyBaseNodeProperties(expectedCR, actualCR);
130 Assert.assertEquals(expectedCR.getCategory(), actualCR.getCategory());
131 Assert.assertEquals(expectedCR.getSubcategory(), actualCR.getSubcategory());
132 Assert.assertEquals(expectedCR.getResourceVendor(), actualCR.getResourceVendor());
133 Assert.assertEquals(expectedCR.getResourceVendorRelease(), actualCR.getResourceVendorRelease());
134 Assert.assertEquals(expectedCR.getResourceVendorModelNumber(), actualCR.getResourceVendorModelNumber());
135 Assert.assertEquals(expectedCR.getCustomizationUUID(), actualCR.getCustomizationUUID());
136 verifyNetworkCollections(expectedCR.getNetworksCollection(), actualCR.getNetworksCollection());
139 private void verifyNetworkCollections(Map<String, NetworkCollection> expectedNetworksCollection, Map<String, NetworkCollection> actualNetworksCollection) {
140 for (Map.Entry<String, NetworkCollection> property : expectedNetworksCollection.entrySet()) {
141 NetworkCollection expectedValue = property.getValue();
142 String key = property.getKey();
143 NetworkCollection actualValue = actualNetworksCollection.get(key);
144 verifyNetworkCollection(expectedValue, actualValue);
148 private void verifyNetworkCollection(NetworkCollection expectedValue, NetworkCollection actualValue) {
149 Assert.assertEquals(expectedValue.getInvariantUuid(), actualValue.getInvariantUuid());
150 Assert.assertEquals(expectedValue.getName(), actualValue.getName());
151 Assert.assertEquals(expectedValue.getUuid(), actualValue.getUuid());
152 Assert.assertEquals(expectedValue.getVersion(), actualValue.getVersion());
153 Assert.assertEquals(expectedValue.getNetworkCollectionProperties().getNetworkCollectionDescription(), actualValue.getNetworkCollectionProperties().getNetworkCollectionDescription());
154 Assert.assertEquals(expectedValue.getNetworkCollectionProperties().getNetworkCollectionFunction(), actualValue.getNetworkCollectionProperties().getNetworkCollectionFunction());
159 public void assertEqualsBetweenVolumeGroups() throws Exception {
160 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
161 Map<String, VolumeGroup> actualVolumeGroups = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVolumeGroups();
162 Map<String, VolumeGroup> expectedVolumeGroups = mockHelper.getNewServiceModel().getVolumeGroups();
163 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVolumeGroups), om.writeValueAsString(actualVolumeGroups));
168 public void assertEqualsBetweenVfModules() throws Exception {
169 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
170 Map<String, VfModule> actualVfModules = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVfModules();
171 Map<String, VfModule> expectedVfModules = mockHelper.getNewServiceModel().getVfModules();
172 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVfModules), om.writeValueAsString(actualVfModules));
177 public void assertEqualsBetweenPolicyConfigurationNodes() throws Exception {
178 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
179 Map<String, PortMirroringConfig> actualConfigurations = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getConfigurations();
180 Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getNewServiceModel().getConfigurations();
181 JsonAssert.assertJsonEquals(actualConfigurations, expectedConfigurations);
185 public void assertEqualsBetweenPolicyConfigurationByPolicyFalse() throws Exception {
186 ToscaParserMockHelper mockHelper = new ToscaParserMockHelper(Constants.configurationByPolicyFalseUuid, Constants.configurationByPolicyFalseFilePath);
187 InputStream jsonFile = this.getClass().getClassLoader().getResourceAsStream(mockHelper.getFilePath());
188 String expectedJsonAsString = IOUtils.toString(jsonFile, StandardCharsets.UTF_8.name());
189 NewServiceModel newServiceModel1 = om.readValue(expectedJsonAsString, NewServiceModel.class);
190 mockHelper.setNewServiceModel(newServiceModel1);
191 Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getNewServiceModel().getConfigurations();
192 Map<String, PortMirroringConfig> actualConfigurations = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getConfigurations();
194 setPprobeServiceProxy(expectedConfigurations);
196 JsonAssert.assertJsonEquals(expectedConfigurations, actualConfigurations);
199 private void setPprobeServiceProxy(Map<String, PortMirroringConfig> expectedConfigurations){
200 //Port Mirroring Configuration By Policy 0 doesn't contains pProbe.
201 // But due to sdc design if pProbe not exists parser expects to get it from other source.
202 // In a follow implementation provided the expected pProbe.
203 PortMirroringConfig pmconfig = expectedConfigurations.get("Port Mirroring Configuration By Policy 0");
204 pmconfig.setCollectorNodes(new ArrayList<>(Arrays.asList("pprobeservice_proxy 4")));
208 public void assertEqualsBetweenServiceProxyNodes() throws Exception {
209 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
210 Map<String, ServiceProxy> actualServiceProxies = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getServiceProxies();
211 Map<String, ServiceProxy> expectedServiceProxies = mockHelper.getNewServiceModel().getServiceProxies();
212 JsonAssert.assertJsonEquals(actualServiceProxies, expectedServiceProxies);
216 private void verifyBaseNodeProperties(Node expectedNode, Node actualNode) {
217 Assert.assertEquals(expectedNode.getName(), actualNode.getName());
218 Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid());
219 Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription());
220 Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid());
221 Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid());
222 Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion());
225 private void compareProperties(Map<String, String> expectedProperties, Map<String, String> actualProperties) {
226 for (Map.Entry<String, String> property : expectedProperties.entrySet()) {
227 String expectedValue = property.getValue();
228 String key = property.getKey();
229 String actualValue = actualProperties.get(key);
230 Assert.assertEquals(expectedValue, actualValue);
234 private ToscaParserMockHelper[] getExpectedServiceModel() throws IOException {
235 ToscaParserMockHelper[] mockHelpers = {
236 new ToscaParserMockHelper(Constants.vlUuid, Constants.vlFilePath),
237 new ToscaParserMockHelper(Constants.vfUuid, Constants.vfFilePath),
238 new ToscaParserMockHelper(Constants.crUuid, Constants.crFilePath),
239 new ToscaParserMockHelper(Constants.vfWithAnnotationUuid, Constants.vfWithAnnotationFilePath),
240 new ToscaParserMockHelper(Constants.vfWithVfcGroup, Constants.vfWithVfcGroupFilePath),
241 new ToscaParserMockHelper(Constants.configurationUuid, Constants.configurationFilePath)
243 for (ToscaParserMockHelper mockHelper : mockHelpers) {
244 InputStream jsonFile = this.getClass().getClassLoader().getResourceAsStream(mockHelper.getFilePath());
245 System.out.println(jsonFile);
246 String expectedJsonAsString = IOUtils.toString(jsonFile, StandardCharsets.UTF_8.name());
247 NewServiceModel newServiceModel1 = om.readValue(expectedJsonAsString, NewServiceModel.class);
248 mockHelper.setNewServiceModel(newServiceModel1);
254 private Path getCsarPath(String uuid) throws AsdcCatalogException {
255 return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
258 private org.onap.vid.asdc.beans.Service getServiceByUuid(String uuid) throws AsdcCatalogException {
259 return asdcClient.getService(UUID.fromString(uuid));
262 public class Constants {
263 public static final String configurationUuid = "ee6d61be-4841-4f98-8f23-5de9da846ca7";
264 public static final String configurationFilePath = "policy-configuration-csar.JSON";
265 static final String vfUuid = "48a52540-8772-4368-9cdb-1f124ea5c931";
266 static final String vfWithAnnotationUuid = "f4d84bb4-a416-4b4e-997e-0059973630b9";
267 static final String vlUuid = "cb49608f-5a24-4789-b0f7-2595473cb997";
268 static final String crUuid = "76f27dfe-33e5-472f-8e0b-acf524adc4f0";
269 static final String vfWithVfcGroup = "6bce7302-70bd-4057-b48e-8d5b99e686ca";
270 // public static final String PNFUuid = "68101369-6f08-4e99-9a28-fa6327d344f3";
271 static final String vfFilePath = "vf-csar.JSON";
272 static final String vlFilePath = "vl-csar.JSON";
273 static final String crFilePath = "cr-csar.JSON";
274 static final String vfWithAnnotationFilePath = "vf-with-annotation-csar.json";
275 static final String vfWithVfcGroupFilePath = "vf-with-vfcInstanceGroups.json";
276 public static final String configurationByPolicyFalseUuid = "ee6d61be-4841-4f98-8f23-5de9da845544";
277 public static final String configurationByPolicyFalseFilePath = "policy-configuration-by-policy-false.JSON";
285 public void testGetNFModuleFromVf() {
286 ISdcCsarHelper csarHelper = getMockedSdcCsarHelper();
288 Map<String, VfModule> vfModulesFromVF = toscaParserImpl2.getVfModulesFromVF(csarHelper, myUUID);
290 assertThat(vfModulesFromVF, allOf(
292 hasKey("withoutVol"),
298 public void testGetVolumeGroupsFromVF() {
299 ISdcCsarHelper csarHelper = getMockedSdcCsarHelper();
301 Map<String, VolumeGroup> volumeGroupsFromVF = toscaParserImpl2.getVolumeGroupsFromVF(csarHelper, myUUID);
303 assertThat(volumeGroupsFromVF, allOf(
309 private ISdcCsarHelper getMockedSdcCsarHelper() {
310 ISdcCsarHelper csarHelper = mock(ISdcCsarHelper.class);
312 Group withVol = createMinimalGroup("withVol", true);
313 Group withoutVol = createMinimalGroup("withoutVol", false);
315 when(csarHelper.getVfModulesByVf(myUUID))
316 .thenReturn(ImmutableList.of(withVol, withoutVol));
321 private static Group createMinimalGroup(String name, boolean isVolumeGroup) {
322 LinkedHashMap<String, Object>
331 templates = new LinkedHashMap<>();
332 templates.put("type", "org.onap.groups.VfModule");
334 properties = addNewNamedMap(templates, "properties");
335 properties.put("volume_group", isVolumeGroup);
337 metadata = addNewNamedMap(templates, "metadata");
339 ArrayList<NodeTemplate> memberNodes = new ArrayList<>();
341 customDef = new LinkedHashMap<>();
342 vfModule = addNewNamedMap(customDef, "org.onap.groups.VfModule");
343 vfModuleProperties = addNewNamedMap(vfModule, "properties");
345 volumeGroup = addNewNamedMap(vfModuleProperties, "volume_group");
346 volumeGroup.put("type", "boolean");
347 volumeGroup.put("default", false);
348 volumeGroup.put("required", true);
351 Group group = new Group(
359 log.info(String.format("Built a group: %s",
360 (new com.fasterxml.jackson.databind.ObjectMapper())
361 .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
362 .writeValueAsString(group)
364 } catch (JsonProcessingException e) {
365 throw new RuntimeException(e);
371 private static LinkedHashMap<String, Object> addNewNamedMap(LinkedHashMap<String, Object> root, String key) {
372 LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
373 root.put(key, properties);