aeb0100719acc2d7adeb34c1b25c570d22cb74ec
[vid.git] / vid-app-common / src / test / java / org / onap / vid / asdc / parser / ToscaParserImpl2Test.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.asdc.parser;
22
23 import static com.google.common.collect.Lists.newArrayList;
24 import static org.hamcrest.Matchers.aMapWithSize;
25 import static org.hamcrest.Matchers.allOf;
26 import static org.hamcrest.Matchers.hasKey;
27 import static org.hamcrest.Matchers.is;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertThat;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33 import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.ECOMP_GENERATED_NAMING_PROPERTY;
34 import static org.onap.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.fasterxml.jackson.databind.SerializationFeature;
39 import com.google.common.collect.ImmutableList;
40 import com.google.common.collect.ImmutableMap;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.nio.file.Path;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.LinkedHashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.UUID;
50 import java.util.stream.Collectors;
51 import java.util.stream.Stream;
52 import net.javacrumbs.jsonunit.JsonAssert;
53 import org.apache.commons.io.IOUtils;
54 import org.apache.log4j.LogManager;
55 import org.apache.log4j.Logger;
56 import org.json.JSONObject;
57 import org.json.JSONTokener;
58 import org.mockito.InjectMocks;
59 import org.mockito.Mock;
60 import org.mockito.MockitoAnnotations;
61 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
62 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
63 import org.onap.sdc.toscaparser.api.Group;
64 import org.onap.sdc.toscaparser.api.NodeTemplate;
65 import org.onap.sdc.toscaparser.api.Property;
66 import org.onap.sdc.toscaparser.api.elements.Metadata;
67 import org.onap.vid.asdc.AsdcCatalogException;
68 import org.onap.vid.asdc.AsdcClient;
69 import org.onap.vid.asdc.local.LocalAsdcClient;
70 import org.onap.vid.controller.ToscaParserMockHelper;
71 import org.onap.vid.model.CR;
72 import org.onap.vid.model.Network;
73 import org.onap.vid.model.NetworkCollection;
74 import org.onap.vid.model.Node;
75 import org.onap.vid.model.PortMirroringConfig;
76 import org.onap.vid.model.ResourceGroup;
77 import org.onap.vid.model.Service;
78 import org.onap.vid.model.ServiceModel;
79 import org.onap.vid.model.ServiceProxy;
80 import org.onap.vid.model.VNF;
81 import org.onap.vid.model.VfModule;
82 import org.onap.vid.model.VidNotions;
83 import org.onap.vid.model.VolumeGroup;
84 import org.onap.vid.properties.Features;
85 import org.testng.Assert;
86 import org.testng.annotations.BeforeClass;
87 import org.testng.annotations.BeforeMethod;
88 import org.testng.annotations.DataProvider;
89 import org.testng.annotations.Test;
90 import org.togglz.core.manager.FeatureManager;
91
92 public class ToscaParserImpl2Test {
93
94     private final String myUUID = "myUUID";
95     private static final Logger log = LogManager.getLogger(ToscaParserImpl2Test.class);
96
97     @InjectMocks
98     private ToscaParserImpl2 toscaParserImpl2;
99
100     private AsdcClient asdcClient;
101     private ObjectMapper om = new ObjectMapper();
102
103     @Mock
104     private VidNotionsBuilder vidNotionsBuilder;
105
106     @BeforeClass
107     void init() throws IOException {
108
109         final InputStream asdcServicesFile = this.getClass().getClassLoader().getResourceAsStream("sdcservices.json");
110
111         final JSONTokener jsonTokener = new JSONTokener(IOUtils.toString(asdcServicesFile));
112         final JSONObject sdcServicesCatalog = new JSONObject(jsonTokener);
113
114         asdcClient = new LocalAsdcClient.Builder().catalog(sdcServicesCatalog).build();
115
116     }
117
118     @BeforeMethod
119     public void initMocks() {
120         MockitoAnnotations.initMocks(this);
121     }
122
123     @Test(dataProvider = "expectedServiceModel")
124     public void assertEqualsBetweenServices(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
125         Service expectedService = mockHelper.getServiceModel().getService();
126         Service actualService = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getService();
127         assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedService), om.writeValueAsString(actualService));
128     }
129
130     @Test(dataProvider = "expectedServiceModel")
131     public void assertEqualBetweenObjects(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
132         final Path csarPath = getCsarPath(mockHelper.getUuid());
133         log.info("Comparing for csar " + csarPath);
134         ServiceModel actualServiceModel = toscaParserImpl2.makeServiceModel(csarPath, getServiceByUuid(mockHelper.getUuid()));
135         assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(mockHelper.getServiceModel()), om.writeValueAsString(actualServiceModel));
136     }
137
138     @Test(dataProvider = "expectedServiceModel")
139     public void assertEqualsBetweenNetworkNodes(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
140         Map<String, Network> expectedNetworksMap = mockHelper.getServiceModel().getNetworks();
141         Map<String, Network> actualNetworksMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getNetworks();
142         for (Map.Entry<String, Network> entry : expectedNetworksMap.entrySet()) {
143             Network expectedNetwork = entry.getValue();
144             Network actualNetwork = actualNetworksMap.get(entry.getKey());
145             Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName());
146             verifyBaseNodeMetadata(expectedNetwork, actualNetwork);
147             compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties());
148         }
149     }
150
151     //Because we are not supporting the old flow, the JSON are different by definition.
152     @Test(dataProvider = "expectedServiceModel")
153     public void assertEqualsBetweenVnfsOfTosca(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
154         Map<String, VNF> expectedVnfsMap = mockHelper.getServiceModel().getVnfs();
155         Map<String, VNF> actualVnfsMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVnfs();
156         for (Map.Entry<String, VNF> entry : expectedVnfsMap.entrySet()) {
157             VNF expectedVnf = entry.getValue();
158             VNF actualVnf = actualVnfsMap.get(entry.getKey());
159             verifyBaseNodeMetadata(expectedVnf, actualVnf);
160             Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName());
161             compareProperties(expectedVnf.getProperties(), actualVnf.getProperties());
162             assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf));
163         }
164     }
165
166
167
168     @Test(dataProvider = "expectedServiceModel")
169     public void assertEqualsBetweenCollectionResourcesOfTosca(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
170         Map<String, CR> expectedVnfsMap = mockHelper.getServiceModel().getCollectionResources();
171             Map<String, CR> actualCRsMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getCollectionResources();
172             if(!actualCRsMap.isEmpty()) {
173                 for (Map.Entry<String, CR> entry : expectedVnfsMap.entrySet()) {
174                     CR expectedCR = entry.getValue();
175                     CR actualCR = actualCRsMap.get(entry.getKey());
176                     verifyCollectionResource(expectedCR, actualCR);
177                     Assert.assertEquals(expectedCR.getName(), actualCR.getName());
178                     compareProperties(expectedCR.getProperties(), actualCR.getProperties());
179                     assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedCR), om.writeValueAsString(actualCR));
180                 }
181             }
182     }
183
184     @Test
185     public void verifyFabricConfiguration() throws Exception {
186         ToscaParserMockHelper toscaParserMockHelper = Arrays.stream(getExpectedServiceModel()).filter(x -> x.getUuid().equals(Constants.fabricConfigurationUuid)).findFirst().get();
187         ServiceModel actualServiceModel = toscaParserImpl2.makeServiceModel(getCsarPath(Constants.fabricConfigurationUuid), getServiceByUuid(Constants.fabricConfigurationUuid));
188         final Map<String, Node> fabricConfigurations = actualServiceModel.getFabricConfigurations();
189         String fabricConfigName = "Fabric Configuration 0";
190         Map<String, Node> expectedFC = toscaParserMockHelper.getServiceModel().getFabricConfigurations();
191         verifyBaseNodeMetadata(expectedFC.get(fabricConfigName), fabricConfigurations.get(fabricConfigName));
192     }
193
194
195     private void verifyCollectionResource(CR expectedCR, CR actualCR) {
196         verifyBaseNodeMetadata(expectedCR, actualCR);
197         Assert.assertEquals(expectedCR.getCategory(), actualCR.getCategory());
198         Assert.assertEquals(expectedCR.getSubcategory(), actualCR.getSubcategory());
199         Assert.assertEquals(expectedCR.getResourceVendor(), actualCR.getResourceVendor());
200         Assert.assertEquals(expectedCR.getResourceVendorRelease(), actualCR.getResourceVendorRelease());
201         Assert.assertEquals(expectedCR.getResourceVendorModelNumber(), actualCR.getResourceVendorModelNumber());
202         Assert.assertEquals(expectedCR.getCustomizationUUID(), actualCR.getCustomizationUUID());
203         verifyNetworkCollections(expectedCR.getNetworksCollection(), actualCR.getNetworksCollection());
204     }
205
206     private void verifyNetworkCollections(Map<String, NetworkCollection> expectedNetworksCollection, Map<String, NetworkCollection> actualNetworksCollection) {
207         for (Map.Entry<String, NetworkCollection> property : expectedNetworksCollection.entrySet()) {
208             NetworkCollection expectedValue = property.getValue();
209             String key = property.getKey();
210             NetworkCollection actualValue = actualNetworksCollection.get(key);
211             verifyNetworkCollection(expectedValue, actualValue);
212         }
213     }
214
215     private void verifyNetworkCollection(NetworkCollection expectedValue, NetworkCollection actualValue) {
216         Assert.assertEquals(expectedValue.getInvariantUuid(), actualValue.getInvariantUuid());
217         Assert.assertEquals(expectedValue.getName(), actualValue.getName());
218         Assert.assertEquals(expectedValue.getUuid(), actualValue.getUuid());
219         Assert.assertEquals(expectedValue.getVersion(), actualValue.getVersion());
220         Assert.assertEquals(expectedValue.getNetworkCollectionProperties().getNetworkCollectionDescription(), actualValue.getNetworkCollectionProperties().getNetworkCollectionDescription());
221         Assert.assertEquals(expectedValue.getNetworkCollectionProperties().getNetworkCollectionFunction(), actualValue.getNetworkCollectionProperties().getNetworkCollectionFunction());
222     }
223
224
225     @Test(dataProvider = "expectedServiceModel")
226     public void assertEqualsBetweenVolumeGroups(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
227             Map<String, VolumeGroup> actualVolumeGroups = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVolumeGroups();
228             Map<String, VolumeGroup> expectedVolumeGroups = mockHelper.getServiceModel().getVolumeGroups();
229             assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVolumeGroups), om.writeValueAsString(actualVolumeGroups));
230     }
231
232     @Test(dataProvider = "expectedServiceModel")
233     public void assertEqualsBetweenVfModules(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
234             Map<String, VfModule> actualVfModules = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVfModules();
235             Map<String, VfModule> expectedVfModules = mockHelper.getServiceModel().getVfModules();
236             assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVfModules), om.writeValueAsString(actualVfModules));
237     }
238
239     @Test(dataProvider = "expectedServiceModel")
240     public void assertEqualsBetweenPolicyConfigurationNodes(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
241             Map<String, PortMirroringConfig> actualConfigurations = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getConfigurations();
242             Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getServiceModel().getConfigurations();
243             JsonAssert.assertJsonEquals(actualConfigurations, expectedConfigurations);
244     }
245
246     @Test
247     public void assertEqualsBetweenPolicyConfigurationByPolicyFalse() throws Exception {
248         ToscaParserMockHelper mockHelper = new ToscaParserMockHelper(Constants.configurationByPolicyFalseUuid, Constants.configurationByPolicyFalseFilePath);
249         Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getServiceModel().getConfigurations();
250         Map<String, PortMirroringConfig> actualConfigurations = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getConfigurations();
251
252         setPprobeServiceProxy(expectedConfigurations);
253
254         JsonAssert.assertJsonEquals(expectedConfigurations, actualConfigurations);
255     }
256
257     @Test
258     public void once5GInNewInstantiationFlagIsActive_vidNotionsIsAppended() throws Exception {
259         FeatureManager featureManager = mock(FeatureManager.class);
260         when(featureManager.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
261
262         ToscaParserImpl2 toscaParserImpl2_local = new ToscaParserImpl2(new VidNotionsBuilder(featureManager));
263
264         final ToscaParserMockHelper mockHelper = new ToscaParserMockHelper(Constants.vlUuid, Constants.vlFilePath);
265         final ServiceModel serviceModel = toscaParserImpl2_local.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid()));
266
267         assertThat(serviceModel.getService().getVidNotions().getInstantiationUI(), is(VidNotions.InstantiationUI.LEGACY));
268         assertThat(serviceModel.getService().getVidNotions().getModelCategory(), is(VidNotions.ModelCategory.OTHER));
269         assertJsonStringEqualsIgnoreNulls("{ service: { vidNotions: { instantiationUI: \"legacy\", modelCategory: \"other\" } } }", om.writeValueAsString(serviceModel));
270     }
271
272     @Test
273     public void modelWithAnnotatedInputWithTwoProperties_vfModuleGetsTheInput() throws Exception {
274         final ToscaParserMockHelper mockHelper = new ToscaParserMockHelper("90fe6842-aa76-4b68-8329-5c86ff564407", "empty.json");
275         final ServiceModel serviceModel = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid()));
276
277         assertJsonStringEqualsIgnoreNulls("{ vfModules: { 201712488_pasqualevpe10..201712488PasqualeVpe1..PASQUALE_vRE_BV..module-1: { inputs: { availability_zone_0: { } } } } }", om.writeValueAsString(serviceModel));
278     }
279
280     @DataProvider
281     public static Object[] oldCsarUuid() {
282         return new Object[][]{{ "2a53419b-3f85-4ad5-a9c9-d79905500a27", "MNS VNN1B EXN VF 1" }
283             , {"e32a5014-357f-4be4-b3f9-fecb0010811e", "MNS VNN1B DMZ VF 1"}};
284     }
285
286     @Test(dataProvider = "oldCsarUuid")
287     public void csarWithVnfWithVfModuleInModel(String oldCsarUuid, String vnfName) throws Exception {
288         ToscaParser tosca = new ToscaParserImpl();
289         final UUID uuid = UUID.fromString(oldCsarUuid);
290         final ServiceModel serviceModel = tosca.makeServiceModel(
291             oldCsarUuid, asdcClient.getServiceToscaModel(uuid), asdcClient.getService(uuid)
292         );
293         assertThat(serviceModel.getVnfs(), aMapWithSize(1));
294         assertThat(serviceModel.getVfModules(), aMapWithSize(2));
295         assertThat(serviceModel.getVolumeGroups(), aMapWithSize(0));
296         assertThat(serviceModel.getVnfs().get(vnfName).getVfModules(), aMapWithSize(2));
297     }
298
299     @Test
300     public void modelWithNfNamingWithToValues_ecompGeneratedNamingIsExtracted() throws Exception {
301         final ToscaParserMockHelper mockHelper = new ToscaParserMockHelper("90fe6842-aa76-4b68-8329-5c86ff564407", "empty.json");
302         final ServiceModel serviceModel = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid()));
303
304         assertJsonStringEqualsIgnoreNulls("" +
305                 "{ vnfs: " +
306                 "  { \"201712-488_PASQUALE-vPE-1 0\": " +
307                 "    { properties: { " +
308                 "      ecomp_generated_naming: \"true\", " +
309                 "      nf_naming: \"{naming_policy=SDNC_Policy.Config_MS_1806SRIOV_VPE_ADIoDJson, ecomp_generated_naming=true}\" " +
310                 "} } } }", om.writeValueAsString(serviceModel));
311     }
312
313     private void setPprobeServiceProxy(Map<String, PortMirroringConfig> expectedConfigurations){
314         //Port Mirroring Configuration By Policy 0 doesn't contains pProbe.
315         // But due to sdc design if pProbe not exists parser expects to get it from other source.
316         // In a follow implementation provided the expected pProbe.
317         PortMirroringConfig pmconfig = expectedConfigurations.get("Port Mirroring Configuration By Policy 0");
318         pmconfig.setCollectorNodes(new ArrayList<>(Arrays.asList("pprobeservice_proxy 4")));
319
320     }
321     @Test(dataProvider = "expectedServiceModel")
322     public void assertEqualsBetweenServiceProxyNodes(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
323             Map<String, ServiceProxy> actualServiceProxies = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getServiceProxies();
324             Map<String, ServiceProxy> expectedServiceProxies = mockHelper.getServiceModel().getServiceProxies();
325             JsonAssert.assertJsonEquals(actualServiceProxies, expectedServiceProxies);
326     }
327
328     @Test(dataProvider = "expectedServiceModel")
329     public void assertEqualsBetweenVnfGroups(String uuid, ToscaParserMockHelper mockHelper) throws Exception {
330         Map<String, ResourceGroup> actualVnfGroups = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVnfGroups();
331         Map<String, ResourceGroup> expectedVnfGroups = mockHelper.getServiceModel().getVnfGroups();
332         JsonAssert.assertJsonEquals(actualVnfGroups, expectedVnfGroups);
333     }
334
335     @Test
336     public void assertEqualsBetweenVrfs() throws Exception {
337         ToscaParserMockHelper  mockHelper = new ToscaParserMockHelper(Constants.vrfUuid, Constants.vrfFilePath);
338         ServiceModel serviceModel = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid()));
339         Map<String, Node> actualVrfs = serviceModel.getVrfs();
340         Map<String, Node> expectedVrfs = mockHelper.getServiceModel().getVrfs();
341         JsonAssert.assertJsonEquals(expectedVrfs, actualVrfs);
342         //assert that vrf isn't returned also as configuration (because it's type is configuration)
343         Map<String, PortMirroringConfig> actualConfigurations = serviceModel.getConfigurations();
344         Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getServiceModel().getConfigurations();
345         JsonAssert.assertJsonEquals(expectedConfigurations, actualConfigurations );
346     }
347
348     private void verifyBaseNodeMetadata(Node expectedNode, Node actualNode) {
349         Assert.assertEquals(expectedNode.getName(), actualNode.getName());
350         Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid());
351         Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription());
352         Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid());
353         Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid());
354         Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion());
355     }
356
357     private void compareProperties(Map<String, String> expectedProperties, Map<String, String> actualProperties) {
358         JsonAssert.assertJsonEquals(expectedProperties, actualProperties);
359     }
360
361     @DataProvider
362     public Object[][] expectedServiceModel() throws IOException {
363         return Stream.of(getExpectedServiceModel())
364                         .map(l -> ImmutableList.of(l.getUuid(), l).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
365     }
366
367
368     private ToscaParserMockHelper[] getExpectedServiceModel() throws IOException {
369         ToscaParserMockHelper[] mockHelpers = {
370                 new ToscaParserMockHelper(Constants.vlUuid, Constants.vlFilePath),
371                 new ToscaParserMockHelper(Constants.vfUuid, Constants.vfFilePath),
372                 new ToscaParserMockHelper(Constants.crUuid, Constants.crFilePath),
373                 new ToscaParserMockHelper(Constants.vfWithAnnotationUuid, Constants.vfWithAnnotationFilePath),
374                 new ToscaParserMockHelper(Constants.vfWithVfcGroup, Constants.vfWithVfcGroupFilePath),
375                 new ToscaParserMockHelper(Constants.configurationUuid, Constants.configurationFilePath),
376                 new ToscaParserMockHelper(Constants.fabricConfigurationUuid, Constants.fabricConfigurationFilePath),
377                 new ToscaParserMockHelper(Constants.vlanTaggingUuid, Constants.vlanTaggingFilePath),
378                 new ToscaParserMockHelper(Constants.vnfGroupingUuid, Constants.vnfGroupingFilePath),
379             new ToscaParserMockHelper("3f6bd9e9-0942-49d3-84e8-6cdccd6de339", "./vLoadBalancerMS-with-policy.TOSCA.json"),
380         };
381
382         return mockHelpers;
383     }
384
385
386     private Path getCsarPath(String uuid) throws AsdcCatalogException {
387         return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
388     }
389
390     private org.onap.vid.asdc.beans.Service getServiceByUuid(String uuid) throws AsdcCatalogException {
391         return asdcClient.getService(UUID.fromString(uuid));
392     }
393
394     public class Constants {
395         public static final String configurationUuid = "ee6d61be-4841-4f98-8f23-5de9da846ca7";
396         public static final String configurationFilePath = "policy-configuration-csar.JSON";
397         static final String vfUuid = "48a52540-8772-4368-9cdb-1f124ea5c931";    //service-vf-csar.zip
398         static final String vfWithAnnotationUuid = "f4d84bb4-a416-4b4e-997e-0059973630b9";
399         static final String vlUuid = "cb49608f-5a24-4789-b0f7-2595473cb997";
400         static final String crUuid = "76f27dfe-33e5-472f-8e0b-acf524adc4f0";
401         static final String vfWithVfcGroup = "6bce7302-70bd-4057-b48e-8d5b99e686ca"; //service-VdorotheaSrv-csar.zip
402         //        public static final String PNFUuid = "68101369-6f08-4e99-9a28-fa6327d344f3";
403         static final String vfFilePath = "vf-csar.JSON";
404         static final String vlFilePath = "vl-csar.JSON";
405         static final String crFilePath = "cr-csar.JSON";
406         static final String vfWithAnnotationFilePath = "vf-with-annotation-csar.json";
407         static final String vfWithVfcGroupFilePath = "vf-with-vfcInstanceGroups.json";
408         public static final String configurationByPolicyFalseUuid = "ee6d61be-4841-4f98-8f23-5de9da845544";
409         public static final String configurationByPolicyFalseFilePath = "policy-configuration-by-policy-false.JSON";
410         public static final String fabricConfigurationUuid = "12344bb4-a416-4b4e-997e-0059973630b9";
411         public static final String fabricConfigurationFilePath = "fabric-configuration.json";
412         public static final String vlanTaggingUuid = "1837481c-fa7d-4362-8ce1-d05fafc87bd1";
413         public static final String vlanTaggingFilePath = "vlan-tagging.json";
414         public static final String vnfGroupingUuid = "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc";
415         public static final String vnfGroupingFilePath = "vnf-grouping-csar.json";
416         public static final String vrfUuid = "f028b2e2-7080-4b13-91b2-94944d4c42d8";
417         public static final String vrfFilePath = "vrf-csar.json";
418
419         public static final String QUANTITY = "quantity";
420
421     }
422
423
424
425     @Test
426     public void testGetNFModuleFromVf() {
427         ISdcCsarHelper csarHelper = getMockedSdcCsarHelper(myUUID);
428
429         Map<String, VfModule> vfModulesFromVF = toscaParserImpl2.getVfModulesFromVF(csarHelper, myUUID);
430
431         assertThat(vfModulesFromVF, allOf(
432                 aMapWithSize(2),
433                 hasKey("withoutVol"),
434                 hasKey("withVol")
435         ));
436     }
437
438     @Test
439     public void testGetVolumeGroupsFromVF() {
440         ISdcCsarHelper csarHelper = getMockedSdcCsarHelper(myUUID);
441
442         Map<String, VolumeGroup> volumeGroupsFromVF = toscaParserImpl2.getVolumeGroupsFromVF(csarHelper, myUUID);
443
444         assertThat(volumeGroupsFromVF, allOf(
445                 aMapWithSize(1),
446                 hasKey("withVol")
447         ));
448     }
449
450     @DataProvider
451     public Object[][] expectedPoliciesTargets() {
452         return new Object[][] {
453             {Constants.vnfGroupingUuid, newArrayList("groupingservicefortest..ResourceInstanceGroup..0", "groupingservicefortest..ResourceInstanceGroup..1")},
454             {Constants.vfUuid, newArrayList()},
455             {Constants.vlanTaggingUuid, newArrayList()}
456         };
457     }
458
459     @Test(dataProvider = "expectedPoliciesTargets")
460     public void testExtractNamingPoliciesTargets(String uuid, ArrayList<String> expectedTargets) throws AsdcCatalogException, SdcToscaParserException {
461         ISdcCsarHelper sdcCsarHelper = toscaParserImpl2.getSdcCsarHelper(getCsarPath(uuid));
462         List<String> policiesTargets = toscaParserImpl2.extractNamingPoliciesTargets(sdcCsarHelper);
463
464         assertEquals(expectedTargets, policiesTargets);
465     }
466
467     @Test
468     public void testScalingPolicyOfVnfGroup() throws AsdcCatalogException, SdcToscaParserException {
469         String vnfGroupingUuid = "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc";
470         Map<String, ResourceGroup> actualVnfGroups = toscaParserImpl2.makeServiceModel(getCsarPath(vnfGroupingUuid), getServiceByUuid(vnfGroupingUuid)).getVnfGroups();
471         assertFalse(actualVnfGroups.get("groupingservicefortest..ResourceInstanceGroup..0").getProperties().containsKey(Constants.QUANTITY));
472         assertEquals(3, actualVnfGroups.get("groupingservicefortest..ResourceInstanceGroup..1").getProperties().get(Constants.QUANTITY));
473     }
474
475     @DataProvider
476     public Object[][] expectedEcompGeneratedNaming() {
477         return new Object[][] {
478                 {"nf_naming property false", "nf_naming", "false", "false"},
479                 {"nf_naming property true", "nf_naming", "true", "true"},
480                 {"nf_naming property doesn't exist", "nf_naming", null, "false"},
481                 {"exVL_naming property false", "exVL_naming", "false", "false"},
482                 {"exVL_naming property true", "exVL_naming", "true", "true"},
483                 {"exVL_naming property doesn't exist", "exVL_naming", null, "false"},
484         };
485     }
486
487     @Test(dataProvider = "expectedEcompGeneratedNaming")
488     public void testEcompGeneratedNamingForNode(String description, String parentProperty, String ecompNamingProperty, String expectedResult) {
489         Property property = mock(Property.class);
490         when(property.getName()).thenReturn("any_key");
491         when(property.getValue()).thenReturn("any_value");
492         ArrayList<Property> properties = newArrayList(property);
493
494         if (ecompNamingProperty != null) {
495             Property nfNamingProperty = mock(Property.class);
496             when(nfNamingProperty.getName()).thenReturn(parentProperty);
497             when(nfNamingProperty.getValue()).thenReturn(ImmutableMap.of(ECOMP_GENERATED_NAMING_PROPERTY, ecompNamingProperty));
498             properties.add(nfNamingProperty);
499         }
500
501         NodeTemplate node = mock(NodeTemplate.class);
502         when(node.getName()).thenReturn("node_name");
503         when(node.getPropertiesObjects()).thenReturn(properties);
504
505         String result = ToscaNamingPolicy.getEcompNamingValueForNode(node, parentProperty);
506         assertEquals(expectedResult, result);
507     }
508
509     public static ISdcCsarHelper getMockedSdcCsarHelper(String myUUID) {
510         ISdcCsarHelper csarHelper = mock(ISdcCsarHelper.class);
511
512         Group withVol = createMinimalGroup("withVol", true);
513         Group withoutVol = createMinimalGroup("withoutVol", false);
514
515         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
516                 "instantiationType", "A-La-Carte"
517         )));
518
519         when(csarHelper.getVfModulesByVf(myUUID))
520                 .thenReturn(ImmutableList.of(withVol, withoutVol));
521
522         return csarHelper;
523     }
524
525     private static Group createMinimalGroup(String name, boolean isVolumeGroup) {
526         LinkedHashMap<String, Object>
527                 templates,
528                 properties,
529                 metadata,
530                 customDef,
531                 vfModule,
532                 vfModuleProperties,
533                 volumeGroup;
534
535         templates = new LinkedHashMap<>();
536         templates.put("type", "org.onap.groups.VfModule");
537
538         properties = addNewNamedMap(templates, "properties");
539         properties.put("volume_group", isVolumeGroup);
540
541         metadata = addNewNamedMap(templates, "metadata");
542
543         ArrayList<NodeTemplate> memberNodes = new ArrayList<>();
544
545         customDef = new LinkedHashMap<>();
546         vfModule = addNewNamedMap(customDef, "org.onap.groups.VfModule");
547         vfModuleProperties = addNewNamedMap(vfModule, "properties");
548
549         volumeGroup = addNewNamedMap(vfModuleProperties, "volume_group");
550         volumeGroup.put("type", "boolean");
551         volumeGroup.put("default", false);
552         volumeGroup.put("required", true);
553
554
555         Group group = new Group(
556                 name,
557                 templates,
558                 memberNodes,
559                 customDef
560         );
561
562         try {
563             log.info(String.format("Built a group: %s",
564                     (new ObjectMapper())
565                             .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
566                             .writeValueAsString(group)
567             ));
568         } catch (JsonProcessingException e) {
569             throw new RuntimeException(e);
570         }
571
572         return group;
573     }
574
575     private static LinkedHashMap<String, Object> addNewNamedMap(LinkedHashMap<String, Object> root, String key) {
576         LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
577         root.put(key, properties);
578         return properties;
579     }
580
581 }