Rename packages from openecomp to onap.
[sdc.git] / common / onap-sdc-artifact-generator-lib / onap-sdc-artifact-generator-core / src / test / java / org / onap / sdc / generator / ArtifactGenerationServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.sdc.generator;
22
23 import org.apache.commons.io.FileUtils;
24 import org.onap.sdc.generator.aai.tosca.GroupDefinition;
25 import org.onap.sdc.generator.data.AdditionalParams;
26 import org.onap.sdc.generator.data.GenerationData;
27 import org.onap.sdc.generator.data.GeneratorConstants;
28 import org.onap.sdc.generator.impl.ArtifactGenerationServiceImpl;
29 import org.onap.sdc.generator.aai.model.Resource;
30 import org.onap.sdc.generator.aai.model.Service;
31 import org.onap.sdc.generator.aai.model.Widget;
32 import org.onap.sdc.generator.aai.tosca.NodeTemplate;
33 import org.onap.sdc.generator.aai.tosca.ToscaTemplate;
34 import org.onap.sdc.generator.aai.xml.Model;
35 import org.onap.sdc.generator.aai.xml.ModelElement;
36 import org.onap.sdc.generator.aai.xml.ModelElements;
37 import org.onap.sdc.generator.aai.xml.ModelVer;
38 import org.onap.sdc.generator.aai.xml.Relationship;
39 import org.onap.sdc.generator.aai.xml.RelationshipData;
40 import org.onap.sdc.generator.data.Artifact;
41 import org.onap.sdc.generator.data.ArtifactType;
42 import org.onap.sdc.generator.data.GeneratorUtil;
43 import org.onap.sdc.generator.data.GroupType;
44 import org.testng.Assert;
45 import org.testng.annotations.BeforeSuite;
46 import org.testng.annotations.Test;
47
48 import java.io.ByteArrayInputStream;
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.FileOutputStream;
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.io.OutputStream;
55 import java.net.URL;
56 import java.util.*;
57 import javax.xml.bind.JAXBContext;
58 import javax.xml.bind.JAXBException;
59 import javax.xml.bind.Unmarshaller;
60
61 @SuppressWarnings("Duplicates")
62 public class ArtifactGenerationServiceTest {
63
64   private static final String aaiArtifactType = ArtifactType.AAI.name();
65   private static final String aaiArtifactGroupType = GroupType.DEPLOYMENT.name();
66   private static final String generatorConfig = "{\"artifactTypes\": [\"OTHER\",\"AAI\"]}";
67   private final Properties properties = new Properties();
68   private final Map<String, String> additionalParams = new HashMap<>();
69   private final Map<String, String> resourcesVersion = new HashMap<>();
70
71   @BeforeSuite
72   public void loadProperties() throws Exception{
73     loadConfigFromClasspath(properties);
74     additionalParams.put(AdditionalParams.ServiceVersion.getName(), "1.0");
75   }
76
77   @Test
78   public void testArtifactGeneration() {
79     // Sunny day scenario service with VF anf vfmodule
80     try {
81
82       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
83       List<ToscaTemplate> toscas = new LinkedList<>();
84       String aaiResourceBasePaths = "aai/";
85       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
86       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
87       if (data.getErrorData().isEmpty()) {
88         for (Artifact inputArtifact : inputArtifacts) {
89           toscas.add(getToscaModel(inputArtifact));
90         }
91         List<Artifact> resultData = data.getResultData();
92
93         /*for( int i = 0 ; i < resultData.size() ; i++) {
94           Artifact artifact = resultData.get(i);
95           String fileName = artifact.getName();
96           while(fileName.contains(":")){
97             fileName = fileName.replace(":","");
98           }
99           File targetFile =new File("src/test/resources/"+fileName);
100           OutputStream outStream = new FileOutputStream(targetFile);
101           outStream.write(Base64.getDecoder().decode(artifact.getPayload()));
102         }*/
103
104         Assert.assertEquals(resultData.size(),5);  //  1-service,1-VF-resource,1-vfmodule and 2
105         // others
106         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
107         testServiceTosca(toscas, outputArtifactMap);
108         testResourceTosca(toscas.iterator(), outputArtifactMap);
109       } else {
110         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
111       }
112
113     } catch (Exception e) {
114       Assert.fail(e.getMessage());
115     }
116   }
117
118   @Test
119   public void testWidgetinServiceTosca() {
120     // Sunny day scenario service with VF and extra widget like CP anf vf has vfmodule without
121     // member
122     try {
123       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
124       List<ToscaTemplate> toscas = new LinkedList<>();
125       String aaiResourceBasePaths = "testWidgetinServiceTosca/";
126       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
127       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
128       if (data.getErrorData().isEmpty()) {
129         for (Artifact inputArtifact : inputArtifacts) {
130           toscas.add(getToscaModel(inputArtifact));
131         }
132         List<Artifact> resultData = data.getResultData();
133         Assert.assertEquals(resultData.size(),5);  //  1-service,1-VF-resource,1-vfmodule and 2
134         // others
135         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
136         testServiceTosca(toscas, outputArtifactMap);
137         testResourceTosca(toscas.iterator(), outputArtifactMap);
138       } else {
139         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
140       }
141
142     } catch (Exception e) {
143       Assert.fail(e.getMessage());
144     }
145   }
146
147   @Test
148   public void testSameVLdifferentVersion() {
149     // Sunny day scenario service with VF and extra widget like CP anf vf has vfmodule without
150     // member
151     try {
152       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
153       List<ToscaTemplate> toscas = new LinkedList<>();
154       String aaiResourceBasePaths = "testSameVLdifferentVersion/";
155       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
156       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
157       if (data.getErrorData().isEmpty()) {
158         for (Artifact inputArtifact : inputArtifacts) {
159           toscas.add(getToscaModel(inputArtifact));
160         }
161         List<Artifact> resultData = data.getResultData();
162         Assert.assertEquals(resultData.size(),8);
163         // others
164         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
165         testServiceTosca(toscas, outputArtifactMap);
166         testResourceTosca(toscas.iterator(), outputArtifactMap);
167       } else {
168         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
169       }
170
171     } catch (Exception e) {
172       Assert.fail(e.getMessage());
173     }
174   }
175
176   @Test
177   public void testArtifactGenerationAllottedResourceAndL3Network() {
178     // Sunny day scenario service with allotted resource and L3-network
179     try {
180       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
181       List<ToscaTemplate> toscas = new LinkedList<>();
182       String aaiResourceBasePaths = "aai2/";
183       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
184       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
185       if (data.getErrorData().isEmpty()) {
186         for (Artifact inputArtifact : inputArtifacts) {
187           toscas.add(getToscaModel(inputArtifact));
188         }
189         List<Artifact> resultData = data.getResultData();
190
191         Assert.assertEquals(resultData.size(),5);
192         // and 2
193         // others
194         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
195         testServiceTosca(toscas, outputArtifactMap);
196         testResourceTosca(toscas.iterator(), outputArtifactMap);
197       } else {
198         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
199       }
200
201     } catch (Exception e) {
202       Assert.fail(e.getMessage());
203     }
204   }
205
206   @Test(dependsOnMethods = {"testArtifactGeneration"})
207   public void testWhenMissingTosca() {
208     try {
209       //Missing Service tosca test case
210       String aaiResourceBasePaths = "testArtifactGeneration2/";
211       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
212       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
213       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
214       Assert.assertEquals(data.getErrorData().get("AAI").get(0),"Service tosca missing from list of input artifacts");
215     } catch (Exception e) {
216       Assert.fail(e.getMessage());
217     }
218   }
219
220   @Test(dependsOnMethods = {"testWhenMissingTosca"})
221   public void testWhenInvaildConfig() {
222     try {
223       //Invalid config test case
224       String generatorConfig1 = "{\"artifactTypes\": [\"ABC\"]}";
225       String aaiResourceBasePaths = "testArtifactGeneration2/";
226       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
227       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
228       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig1, additionalParams);
229       Assert.assertEquals(data.getErrorData().get("ARTIFACT_GENERATOR_INVOCATION_ERROR").get(0),"Invalid Client Configuration");
230     } catch (Exception e) {
231       Assert.fail(e.getMessage());
232     }
233   }
234
235   @Test(dependsOnMethods = {"testArtifactGeneration"})
236   public void testWhenOnlyServToscaNoResTosca() {
237     try {
238       //Testing only service tosca no resource Tosca
239       String aaiResourceBasePaths = "testArtifactGeneration4/";
240       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
241       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
242       List<ToscaTemplate> toscas = new LinkedList<>();
243       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
244       List<Artifact> resultData = data.getResultData();
245       if (data.getErrorData().isEmpty()) {
246         for (Artifact inputArtifact : inputArtifacts) {
247           toscas.add(getToscaModel(inputArtifact));
248         }
249       }
250       Assert.assertEquals(resultData.size(),3);  //  1-service and 2-Others
251       Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
252       testServiceTosca(toscas, outputArtifactMap);
253     } catch (Exception e) {
254       Assert.fail(e.getMessage());
255     }
256   }
257
258   @Test(dependsOnMethods = {"testArtifactGeneration"})
259   public void testWhenInvaildYaml() {
260     try {
261       //Invalid Yaml file test case
262       String aaiResourceBasePaths = "testArtifactGeneration5/";
263       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
264       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
265       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
266       Assert.assertEquals(data.getErrorData().get("AAI").get(0),"Invalid format for Tosca YML  : " + inputArtifacts.get(0).getName());
267     } catch (Exception e) {
268       Assert.fail(e.getMessage());
269     }
270   }
271
272   @Test(dependsOnMethods = {"testArtifactGeneration"})
273   public void testWhenExtraResToscaNotPartOfServ() {
274     try {
275       // Valid scenario with extra resource tosca which is not part of Service
276       String aaiResourceBasePaths = "testArtifactGeneration6/";
277       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
278       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
279       List<ToscaTemplate> toscas = new LinkedList<>();
280       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
281       List<Artifact> resultData = data.getResultData();
282       if (data.getErrorData().isEmpty()) {
283         for (Artifact inputArtifact : inputArtifacts) {
284           toscas.add(getToscaModel(inputArtifact));
285         }
286       }
287       Assert.assertEquals(resultData.size(),3);  //  1-service and 2 Others
288       Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
289       testServiceTosca(toscas, outputArtifactMap);
290     } catch (Exception e) {
291       Assert.fail(e.getMessage());
292     }
293   }
294
295
296   @Test
297   public void testWhenInvUuIdAttrMissing() {
298     try {
299       // mandatory attribute <invariantUUID> missing
300       String aaiResourceBasePaths = "testArtifactGeneration8/";
301       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
302       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
303       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
304       Assert.assertEquals(
305           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <invariantUUID> missing in Artifact: <" +
306               inputArtifacts.get(0).getName() + ">");
307
308     } catch (Exception e) {
309       Assert.fail(e.getMessage());
310     }
311   }
312
313   @Test
314   public void testErrorWhenInvalidInvId() {
315     try {
316       //Invariant Id in service tosca of length not 36
317       String aaiResourceBasePaths = "testErrorWhenInvalidInvId/";
318       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
319       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
320       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
321       Assert.assertEquals(
322           data.getErrorData().get("AAI").get(0),String.format(
323               GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID,
324               "invariantUUID",inputArtifacts.get(0).getName()));
325
326     } catch (Exception e) {
327       Assert.fail(e.getMessage());
328     }
329   }
330
331   @Test
332   public void testWhenUuIdAttrMissing() {
333     try {
334       //mandatory attribute <UUID> missing
335       String aaiResourceBasePaths = "testArtifactGeneration9/";
336       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
337       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
338       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
339       Assert.assertEquals(
340           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <UUID> missing in Artifact: <" +
341               inputArtifacts.get(0).getName() + ">");
342
343     } catch (Exception e) {
344       Assert.fail(e.getMessage());
345     }
346   }
347
348   @Test
349   public void testErrorWhenInvalidUuId() {
350     try {
351       //UUID Id in service tosca of length not 36
352       String aaiResourceBasePaths = "testErrorWhenInvalidUuId/";
353       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
354       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
355       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
356       Assert.assertEquals(
357           data.getErrorData().get("AAI").get(0),String.format(
358               GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID,
359               "UUID",inputArtifacts.get(0).getName()));
360
361     } catch (Exception e) {
362       Assert.fail(e.getMessage());
363     }
364   }
365
366   @Test
367   public void testWhenNameAttrMissing() {
368     try {
369       //mandatory attribute <name> missing
370       String aaiResourceBasePaths = "testArtifactGeneration10/";
371       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
372       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
373       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
374       Assert.assertEquals(
375           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <name> missing in Artifact: <" +
376               inputArtifacts.get(0).getName() + ">");
377
378     } catch (Exception e) {
379       Assert.fail(e.getMessage());
380     }
381   }
382
383   @Test //(dependsOnMethods = {"testArtifactGeneration"})
384   public void testWhenVfModInvUuIdAttrMissing() {
385     try {
386       //mandatory attribute <vfModuleModelInvariantUUID> missing
387       List<Artifact> inputArtifacts = new ArrayList<>();
388       readPayloadFromResource(inputArtifacts, "service_vmme_template_ModInvUUID.yml");
389       readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModInvUUID.yml");
390       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
391       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
392       Assert.assertEquals(
393           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <vfModuleModelInvariantUUID> missing in Artifact: <" +
394               inputArtifacts.get(1).getName() + ">");
395
396     } catch (Exception e) {
397       Assert.fail(e.getMessage());
398     }
399   }
400
401   public static void readPayload(List<Artifact> inputArtifacts, InputStream fis, String fileName) throws
402       IOException {
403     byte[] payload = new byte[fis.available()];
404     fis.read(payload);
405     String checksum = GeneratorUtil.checkSum(payload);
406     byte[] encodedPayload = GeneratorUtil.encode(payload);
407     Artifact artifact = new Artifact(aaiArtifactType, aaiArtifactGroupType, checksum, encodedPayload);
408     artifact.setName(fileName);
409     artifact.setLabel(fileName);
410     artifact.setDescription(fileName);
411     artifact.setVersion("1.0");
412     System.out.println(artifact.getName());
413     inputArtifacts.add(artifact);
414   }
415
416   @Test
417   public void testWhenInvalidVfModInvUuIdAttr() {
418     try {
419       //invalid id since not of length 36 for  <vfModuleModelInvariantUUID>
420       List<Artifact> inputArtifacts = new ArrayList<>();
421       readPayloadFromResource(inputArtifacts, "service_vmme_template_InvalidVfModInvUuIdAttr.yml");
422
423       readPayloadFromResource(inputArtifacts, "vf_vmme_template_InvalidVfModInvUuIdAttr.yml");
424       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
425       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
426       Assert.assertEquals(
427           data.getErrorData().get("AAI").get(0),String.format(
428               GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID,
429               "vfModuleModelInvariantUUID", inputArtifacts.get(1).getName() ));
430
431     } catch (Exception e) {
432       Assert.fail(e.getMessage());
433     }
434   }
435
436   @Test //(dependsOnMethods = {"testArtifactGeneration"})
437   public void testWhenVfModNameAttrMissing() {
438     try {
439       //mandatory attribute <vfModuleModelName> missing
440       List<Artifact> inputArtifacts = new ArrayList<>();
441       readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelName.yml");
442
443       readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelName.yml");
444       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
445       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
446       Assert.assertEquals(
447           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <vfModuleModelName> missing in Artifact: <" +
448               inputArtifacts.get(1).getName() + ">");
449
450     } catch (Exception e) {
451       Assert.fail(e.getMessage());
452     }
453   }
454
455   /*public static void readPayload(List<Artifact> inputArtifacts,InputStream fis, String fileName)
456       throws
457       IOException {
458     byte[] payload = new byte[fis.available()];
459     fis.read(payload);
460     String checksum = GeneratorUtil.checkSum(payload);
461     byte[] encodedPayload = GeneratorUtil.encode(payload);
462     Artifact artifact = new Artifact(aaiArtifactType, aaiArtifactGroupType, checksum, encodedPayload);
463     artifact.setName(fileName);
464     artifact.setLabel(fileName);
465     artifact.setDescription(fileName);
466     artifact.setVersion("1.0");
467     System.out.println(artifact.getName());
468     inputArtifacts.add(artifact);
469   }*/
470
471   @Test //(dependsOnMethods = {"testArtifactGeneration"})
472   public void testWhenVfModUuIdAttrMissing() {
473     try {
474       //mandatory attribute <vfModuleModelUUID> missing in Artifact
475       List<Artifact> inputArtifacts = new ArrayList<>();
476       readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelUUID.yml");
477
478       readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelUUID.yml");
479       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
480       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
481       Assert.assertEquals(
482           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory " +
483               "attribute <vfModuleModelUUID> missing in Artifact: <" +
484               inputArtifacts.get(1).getName() + ">");
485
486     } catch (Exception e) {
487       Assert.fail(e.getMessage());
488     }
489   }
490
491   @Test
492   public void testWhenInvalidVfModUuIdAttr() {
493     try {
494       //invalid id since not of length 36 for  <vfModuleModelUUID>
495       List<Artifact> inputArtifacts = new ArrayList<>();
496       readPayloadFromResource(inputArtifacts, "service_vmme_template_InvalidVfModUuIdAttr.yml");
497
498       readPayloadFromResource(inputArtifacts, "vf_vmme_template_InvalidVfModUuIdAttr.yml");
499       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
500       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
501       Assert.assertEquals(
502           data.getErrorData().get("AAI").get(0),String.format(
503               GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_ID,
504               "vfModuleModelUUID", inputArtifacts.get(1).getName() ));
505
506     } catch (Exception e) {
507       Assert.fail(e.getMessage());
508     }
509   }
510
511   @Test //(dependsOnMethods = {"testArtifactGeneration"})
512   public void testWhenVfModVersionAttrMissing() {
513     try {
514       //mandatory attribute <vfModuleModelVersion> missing
515       List<Artifact> inputArtifacts = new ArrayList<>();
516       readPayloadFromResource(inputArtifacts, "service_vmme_template_ModelVersion.yml");
517
518       readPayloadFromResource(inputArtifacts, "vf_vmme_template_ModelVersion.yml");
519       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
520       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
521       Assert.assertEquals(
522           data.getErrorData().get("AAI").get(0),"Invalid Service/Resource definition mandatory attribute <vfModuleModelVersion> missing in Artifact: <" +
523               inputArtifacts.get(1).getName() + ">");
524
525     } catch (Exception e) {
526       Assert.fail(e.getMessage());
527     }
528   }
529
530   @Test
531   public void testErrorWhenNoSystemPropConfigured() throws Exception  {
532     String configLoc = System.getProperty("artifactgenerator.config");
533     try {
534       System.clearProperty("artifactgenerator.config");
535       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
536       String aaiResourceBasePaths = "aai/";
537       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
538       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
539       Assert.assertEquals(false,data.getErrorData().isEmpty());
540       Assert.assertEquals(data.getErrorData().
541           get("AAI").get(0), GeneratorConstants.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND);
542     } catch (Exception e) {
543       Assert.fail(e.getMessage());
544     }
545     finally{
546       System.setProperty("artifactgenerator.config",configLoc);
547     }
548   }
549
550   @Test
551   public void testErrorWhenNoWidgetInConfig() throws Exception  {
552     String configLoc = System.getProperty("artifactgenerator.config");
553     final File configFile = new File(configLoc);
554     String configDir = configLoc.substring(0, configLoc.lastIndexOf(File.separator));
555     final File tempFile = new File(configDir + File.separator + "temp.properties");
556     try {
557       //copy orignal Artifact-Generator.properties to temp.properties for backup
558       FileUtils.copyFile(configFile, tempFile);
559
560       String serviceWidgetName = ArtifactType.AAI.name()+".model-version-id."+Widget.getWidget
561           (Widget.Type.SERVICE)
562           .getName();
563       String assertMsg = ArtifactType.AAI.name() + ".model-version-id." +Widget.getWidget
564           (Widget.Type.SERVICE).getName();
565
566       //Remove property from Artifact-Generator.properties
567       properties.remove(serviceWidgetName);
568       try (OutputStream fos = new FileOutputStream(new File(configLoc))) {
569         properties.store(fos,null);
570       }
571
572       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
573       String aaiResourceBasePaths = "aai/";
574       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
575       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
576       Assert.assertEquals(false,data.getErrorData().isEmpty());
577       String errMsg = String.format(GeneratorConstants.GENERATOR_AAI_CONFIGLPROP_NOT_FOUND,assertMsg);
578       Assert.assertEquals(data.getErrorData().get("AAI").get(0),errMsg);
579     } catch (Exception e) {
580       Assert.fail(e.getMessage());
581     }
582     finally{
583       if(tempFile.exists()){
584         //Revert the changes
585         FileUtils.copyFile(tempFile, configFile);
586         loadConfigFromClasspath(properties);
587         tempFile.delete();
588       }
589     }
590   }
591
592   @Test
593   public void testErrorWhenNoFileAtConfigLocation() throws Exception  {
594     String configLoc = System.getProperty("artifactgenerator.config");
595     try {
596       System.setProperty("artifactgenerator.config",configLoc + File.separator + "testErrorWhenNoFileAtConfigLocation");
597       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
598       String aaiResourceBasePaths = "aai/";
599       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
600       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
601       Assert.assertEquals(data.getErrorData().isEmpty(),false);
602       Assert.assertEquals(data.getErrorData().get("AAI").get(0),String.format(
603           GeneratorConstants.GENERATOR_AAI_CONFIGFILE_NOT_FOUND,System.getProperty
604           ("artifactgenerator.config")));
605     } catch (Exception e) {
606       Assert.fail(e.getMessage());
607     }
608     finally{
609       System.setProperty("artifactgenerator.config",configLoc);
610     }
611   }
612
613   @Test
614   public void testErrorWhenNoServiceVersion() {
615     //  scenario service with VF anf vfmodule but no service version in additional parameter
616     try {
617       additionalParams.clear();
618       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
619       String aaiResourceBasePaths = "aai/";
620       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
621       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
622       Assert.assertEquals(data.getErrorData().isEmpty(),false);
623       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
624           GeneratorConstants.GENERATOR_AAI_ERROR_MISSING_SERVICE_VERSION);
625     } catch (Exception e) {
626       Assert.fail(e.getMessage());
627     }
628     finally{
629       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"1.0");
630     }
631   }
632
633   @Test
634   public void testArtifactGenerationWithServiceVersion() {
635     // Sunny day scenario service with VF anf vfmodule and service version as adiitional parameter
636     try {
637       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"9.0");
638       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
639       List<ToscaTemplate> toscas = new LinkedList<>();
640       String aaiResourceBasePaths = "aai/";
641       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
642       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
643       if (data.getErrorData().isEmpty()) {
644         for (Artifact inputArtifact : inputArtifacts) {
645           toscas.add(getToscaModel(inputArtifact));
646         }
647         List<Artifact> resultData = data.getResultData();
648
649         /*for( int i = 0 ; i < resultData.size() ; i++) {
650           Artifact artifact = resultData.get(i);
651           String fileName = artifact.getName();
652           while(fileName.contains(":")){
653             fileName = fileName.replace(":","");
654           }
655           File targetFile =new File("src/test/resources/"+fileName);
656           OutputStream outStream = new FileOutputStream(targetFile);
657           outStream.write(Base64.getDecoder().decode(artifact.getPayload()));
658         }*/
659
660         Assert.assertEquals(resultData.size(),5);  //  1-service,1-VF-resource,1-vfmodule and 2
661         // others
662         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
663         testServiceTosca(toscas, outputArtifactMap);
664         testResourceTosca(toscas.iterator(), outputArtifactMap);
665       } else {
666         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
667       }
668
669     } catch (Exception e) {
670       Assert.fail(e.getMessage());
671     }
672     finally{
673       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"1.0");
674     }
675   }
676
677   @Test
678   public void testErrorWhenInvalidServiceVersion() {
679     //  scenario service with VF anf vfmodule but invalid service version in additional parameter
680     try {
681       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
682       String aaiResourceBasePaths = "aai/";
683       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
684
685       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"1");
686       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
687       Assert.assertEquals(data.getErrorData().isEmpty(),false);
688       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
689           GeneratorConstants.GENERATOR_AAI_INVALID_SERVICE_VERSION);
690
691       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"0.1");
692       data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
693       Assert.assertEquals(data.getErrorData().isEmpty(),false);
694       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
695           GeneratorConstants.GENERATOR_AAI_INVALID_SERVICE_VERSION);
696
697       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"0.0");
698       data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
699       Assert.assertEquals(data.getErrorData().isEmpty(),false);
700       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
701           GeneratorConstants.GENERATOR_AAI_INVALID_SERVICE_VERSION);
702
703     } catch (Exception e) {
704       Assert.fail(e.getMessage());
705     }
706     finally{
707       additionalParams.put(AdditionalParams.ServiceVersion.getName(),"1.0");
708     }
709   }
710
711   @Test
712   public void testMissingResourceTosca() {
713     try {
714       //Service with resource but seperate resource tosca not coming as input.
715       String aaiResourceBasePaths = "missingResourceTosca/";
716       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
717       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
718       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
719       Assert.assertEquals(data.getErrorData().get("AAI").get(0),"Cannot generate artifacts. Resource Tosca missing for resource with UUID: <b020ed1e-4bc7-4fc0-ba7e-cc7af6da7ffc>");
720     } catch (Exception e) {
721       Assert.fail(e.getMessage());
722     }
723   }
724   @Test
725   public void testMissingVLTosca() {
726     try {
727       //Service with VL but seperate VL tosca not coming as input artifact.
728       String aaiResourceBasePaths = "missingVLTosca/";
729       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
730       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
731       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
732       Assert.assertEquals(data.getErrorData().get("AAI").get(0),"Cannot generate artifacts. Resource Tosca missing for resource with UUID: <3f8fa4d2-2b86-4b36-bbc8-ffb8f9f57468>");
733     } catch (Exception e) {
734       Assert.fail(e.getMessage());
735     }
736   }
737
738   @Test
739   public void testErrorWhenNoResourceVersion() {
740     //  scenario service with VF but missing resource version in service tosca
741     try {
742       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
743       String aaiResourceBasePaths = "testErrorWhenNoResourceVersion/";
744       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
745       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
746       Assert.assertEquals(data.getErrorData().isEmpty(),false);
747       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
748           "Invalid Service definition mandatory attribute version missing for resource with UUID: <b020ed1e-4bc7-4fc0-ba7e-cc7af6da7ffc>");
749     } catch (Exception e) {
750       Assert.fail(e.getMessage());
751     }
752   }
753
754   @Test
755   public void testErrorWhenInvalidResourceVersion1() {
756     //  scenario service with VF but invalid resource version 0.0 in service tosca
757     try {
758       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
759       String aaiResourceBasePaths = "testErrorWhenInvalidResourceVersion1/";
760       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
761       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
762       Assert.assertEquals(data.getErrorData().isEmpty(),false);
763       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
764           "Cannot generate artifacts. Invalid Resource version in Service tosca for resource with UUID: <b020ed1e-4bc7-4fc0-ba7e-cc7af6da7ffc>");
765     } catch (Exception e) {
766       Assert.fail(e.getMessage());
767     }
768   }
769
770   @Test
771   public void testErrorWhenInvalidResourceVersion2() {
772     //  scenario service with VF but invalid resource version 1 in service tosca
773     try {
774       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
775       String aaiResourceBasePaths = "testErrorWhenInvalidResourceVersion2/";
776       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
777       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
778       Assert.assertEquals(data.getErrorData().isEmpty(),false);
779       Assert.assertEquals(data.getErrorData().get("AAI").get(0),
780           "Cannot generate artifacts. Invalid Resource version in Service tosca for resource with UUID: <b020ed1e-4bc7-4fc0-ba7e-cc7af6da7ffc>");
781     } catch (Exception e) {
782       Assert.fail(e.getMessage());
783     }
784   }
785
786   @Test
787   public void testArtifactGenerationAllottedResourceWithIpMuxAndTunnelXConn() {
788     try {
789       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
790       List<ToscaTemplate> toscas = new LinkedList<>();
791       String aaiResourceBasePaths = "testArtifactGeneration15/";
792       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
793       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
794       if (data.getErrorData().isEmpty()) {
795         for (Artifact inputArtifact : inputArtifacts) {
796           toscas.add(getToscaModel(inputArtifact));
797         }
798         List<Artifact> resultData = data.getResultData();
799
800         Assert.assertEquals(resultData.size(),5);
801         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
802         testServiceTosca(toscas, outputArtifactMap);
803         testResourceTosca(toscas.iterator(), outputArtifactMap);
804       } else {
805         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
806       }
807
808     } catch (Exception e) {
809       Assert.fail(e.getMessage());
810     }
811   }
812
813 //  @Test
814   public void testErrorWhenAllottedResourceWithOutProvidingServiceId() {
815     try {
816       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
817       String aaiResourceBasePaths = "testErrorWhenAllottedResourceWithOutDependingServiceId/";
818       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
819       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
820       Assert.assertEquals(data.getErrorData().isEmpty(),false);
821       Assert.assertEquals(data.getErrorData().get("AAI").get(0),String.format
822               (GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, "707b2850-e830-4b00-9902-879f44ac05a4"));
823     } catch (Exception e) {
824       Assert.fail(e.getMessage());
825     }
826   }
827
828   @Test
829   public void testArtifactGenerationWithoutAllottedResource() {
830     try {
831       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
832       String aaiResourceBasePaths = "testArtifactGeneration16/";
833       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
834       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
835       Assert.assertEquals(data.getErrorData().isEmpty(),false);
836       Assert.assertEquals(data.getErrorData().get("AAI").get(0),String.format
837           (GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_MISSING, "a54a5235-b69d-4f8a-838b-d011e6783fa5"));
838     } catch (Exception e) {
839       Assert.fail(e.getMessage());
840     }
841   }
842
843   @Test
844   public void testArtifactGenerationAllottedResourceIpmuxSameInvariantDiffVersion() {
845     try {
846       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
847       List<ToscaTemplate> toscas = new LinkedList<>();
848       String aaiResourceBasePaths = "testArtifactGeneration17/";
849       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
850       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
851       if (data.getErrorData().isEmpty()) {
852         for (Artifact inputArtifact : inputArtifacts) {
853           toscas.add(getToscaModel(inputArtifact));
854         }
855         List<Artifact> resultData = data.getResultData();
856
857         Assert.assertEquals(resultData.size(),5);
858         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
859         testServiceTosca(toscas, outputArtifactMap);
860         testResourceTosca(toscas.iterator(), outputArtifactMap);
861       } else {
862         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
863       }
864
865     } catch (Exception e) {
866       Assert.fail(e.getMessage());
867     }
868   }
869
870   @Test
871   public void testArtifactGenerationAllottedResourceIpmuxSameInvariantSameVersion() {
872     try {
873       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
874       List<ToscaTemplate> toscas = new LinkedList<>();
875       String aaiResourceBasePaths = "testArtifactGeneration18/";
876       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
877       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
878       if (data.getErrorData().isEmpty()) {
879         for (Artifact inputArtifact : inputArtifacts) {
880           toscas.add(getToscaModel(inputArtifact));
881         }
882         List<Artifact> resultData = data.getResultData();
883
884
885         Assert.assertEquals(resultData.size(),4);
886         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
887         testServiceTosca(toscas, outputArtifactMap);
888         testResourceTosca(toscas.iterator(), outputArtifactMap);
889       } else {
890         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
891       }
892
893     } catch (Exception e) {
894       Assert.fail(e.getMessage());
895     }
896   }
897
898   @Test
899   public void testArtifactGenerationAllottedResourceIpmuxWithGroups() {
900     try {
901       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
902       List<ToscaTemplate> toscas = new LinkedList<>();
903       String aaiResourceBasePaths = "testAllotedResourceWithDependingSerWithGroups/";
904       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
905       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
906       if (data.getErrorData().isEmpty()) {
907         for (Artifact inputArtifact : inputArtifacts) {
908           toscas.add(getToscaModel(inputArtifact));
909         }
910         List<Artifact> resultData = data.getResultData();
911
912         Assert.assertEquals(resultData.size(),5);
913         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
914         testServiceTosca(toscas, outputArtifactMap);
915         testResourceTosca(toscas.iterator(), outputArtifactMap);
916       } else {
917         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
918       }
919
920     } catch (Exception e) {
921       Assert.fail(e.getMessage());
922     }
923   }
924
925   @Test
926   public void testArtifactGenerationAllottedResourceWithVF() {
927     try {
928       ArtifactGenerationServiceImpl obj = new ArtifactGenerationServiceImpl();
929       List<ToscaTemplate> toscas = new LinkedList<>();
930       String aaiResourceBasePaths = "testArtifactGenerationAllottedResourceWithVF/";
931       List<Artifact> inputArtifacts = init(aaiResourceBasePaths);
932       GenerationData data = obj.generateArtifact(inputArtifacts, generatorConfig, additionalParams);
933       if (data.getErrorData().isEmpty()) {
934         for (Artifact inputArtifact : inputArtifacts) {
935           toscas.add(getToscaModel(inputArtifact));
936         }
937         List<Artifact> resultData = data.getResultData();
938
939         Assert.assertEquals(resultData.size(),7);
940         Map<String, Model> outputArtifactMap = populateAAIGeneratedModelStore(resultData);
941         testServiceTosca(toscas, outputArtifactMap);
942         testResourceTosca(toscas.iterator(), outputArtifactMap);
943       } else {
944         Assert.fail("error encountered : " + data.getErrorData().get("AAI"));
945       }
946
947     } catch (Exception e) {
948       Assert.fail(e.getMessage());
949     }
950   }
951
952
953   //@Test
954   public void testServiceTosca(List<ToscaTemplate> toscas, Map<String, Model> outputArtifactMap) {
955     try {
956       ToscaTemplate serviceTosca = getServiceTosca(toscas);
957       if (serviceTosca == null) {
958         Assert.fail("Service Tosca not found");
959       }
960       serviceTosca.getMetadata().put("version", "1.0");
961       Service service = new Service();
962       service.populateModelIdentificationInformation(serviceTosca.getMetadata());
963       String serviceNameVersionId = service.getModelNameVersionId();
964       Model serviceAAIModel = getAAIModelByNameVersionId(serviceNameVersionId, outputArtifactMap);
965       validateServiceModelMetadata(service, serviceAAIModel);
966       //Validate Service instance base widget
967       ModelVer modelVersion =  serviceAAIModel.getModelVers().getModelVer().get(0);
968
969       List<ModelElement> matchedServiceBaseWidgetElements =
970           getModelElementbyRelationshipValue( modelVersion.getModelElements(),
971               Widget.getWidget(Widget.Type.SERVICE).getId());
972       validateMatchedModelElementsInService(matchedServiceBaseWidgetElements,
973           Widget.getWidget(Widget.Type.SERVICE).getName());
974
975       validateWidgetIds(matchedServiceBaseWidgetElements, Widget.getWidget(Widget.Type.SERVICE).getName(),
976           Widget.getWidget(Widget.Type.SERVICE).getWidgetId());
977
978       ModelElements baseServiceWidgetModelElements =
979           matchedServiceBaseWidgetElements.get(0).getModelElements();
980
981
982       Map<String, String> nodeTemplateIdTypeStore = getNodeTemplateTypeStore(serviceTosca);
983       if (nodeTemplateIdTypeStore != null) {
984         for (String key : nodeTemplateIdTypeStore.keySet()) {
985           if (nodeTemplateIdTypeStore.get(key).contains("org.openecomp.resource.vf")) {
986             List<ModelElement> matchedResourceElements =
987                 getModelElementbyRelationshipValue(baseServiceWidgetModelElements, key);
988             if (nodeTemplateIdTypeStore.get(key).contains("org.openecomp.resource.vf.allottedResource")){
989               validateMatchedModelElementsInService(matchedResourceElements,
990                   Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getName());
991             }else {
992               validateMatchedModelElementsInService(matchedResourceElements,
993                   Widget.getWidget(Widget.Type.VF).getName());
994             }
995
996             //Validate uuid and invariantuuid are populated in model-ver.model-version-id and model.model-invariant-id
997             Assert.assertEquals(matchedResourceElements.get(0).getRelationshipList()
998                 .getRelationship().get(0)
999                 .getRelationshipData().get(0).getRelationshipValue(),key);
1000
1001             Assert.assertEquals(matchedResourceElements.get(0).getRelationshipList().getRelationship().get(0)
1002                 .getRelationshipData().get(1).getRelationshipValue(), nodeTemplateIdTypeStore
1003                 .get(key+"-INV_UID"));
1004           } else if(nodeTemplateIdTypeStore.get(key).contains("org.openecomp.resource.vl")){
1005             //validate l3-network in service tosca
1006             List<ModelElement> matchedResourceElements =
1007                 getModelElementbyRelationshipValue(baseServiceWidgetModelElements, key);
1008             validateMatchedModelElementsInService(matchedResourceElements,
1009                 Widget.getWidget(Widget.Type.L3_NET).getName());
1010             //Validate uuid and invariantuuid are populated in model-ver.model-version-id and model.model-invariant-id
1011             Assert.assertEquals(matchedResourceElements.get(0).getRelationshipList()
1012                 .getRelationship().get(0)
1013                 .getRelationshipData().get(0).getRelationshipValue(),key);
1014
1015             Assert.assertEquals(matchedResourceElements.get(0).getRelationshipList().getRelationship().get(0)
1016                 .getRelationshipData().get(1).getRelationshipValue(), nodeTemplateIdTypeStore
1017                 .get(key+"-INV_UID"));
1018           }
1019         }
1020
1021
1022       System.out.println();
1023
1024       }
1025     } catch (IllegalArgumentException e) {
1026       Assert.fail(e.getMessage());    //Can come while populating metadata
1027     }
1028   }
1029
1030   private void validateWidgetIds(List<ModelElement> matchedServiceBaseWidgetElements,
1031                                  String widgetName, String widgetInvUuId) {
1032     Assert.assertEquals(matchedServiceBaseWidgetElements.get(0).getRelationshipList().getRelationship().get(0)
1033         .getRelationshipData().get(0).getRelationshipValue(), properties.getProperty(ArtifactType.AAI.name()
1034         + ".model-version-id."+ widgetName));
1035
1036     Assert.assertEquals(matchedServiceBaseWidgetElements.get(0).getRelationshipList().getRelationship().get(0)
1037         .getRelationshipData().get(1).getRelationshipValue(), widgetInvUuId);
1038   }
1039
1040
1041   public void testL3NetworkResourceTosca(Map<String, Model> outputArtifactMap , ToscaTemplate
1042       resourceTosca) {
1043     try {
1044       if (resourceTosca != null) {
1045         Resource resource = new Resource();
1046         resource.populateModelIdentificationInformation(resourceTosca.getMetadata());
1047         String resourceNameVersionId = resource.getModelNameVersionId();
1048         Model resourceAAIModel =
1049             getAAIModelByNameVersionId(resourceNameVersionId, outputArtifactMap);
1050         if (resourceAAIModel != null) {
1051           validateResourceModelMetadata(resource, resourceAAIModel);
1052           //Validate Resource instance base widget
1053
1054           ModelVer modelVersion = resourceAAIModel.getModelVers().getModelVer().get(0);
1055
1056           List<ModelElement> matchedVFBaseWidgetElements =
1057               getModelElementbyRelationshipValue(modelVersion.getModelElements(),
1058                   Widget.getWidget(Widget.Type.L3_NET).getId());
1059           validateMatchedModelElementsInService(matchedVFBaseWidgetElements,
1060               Widget.getWidget(Widget.Type.L3_NET).getName());
1061
1062           validateWidgetIds(matchedVFBaseWidgetElements, Widget.getWidget(Widget.Type.L3_NET).getName(),
1063               Widget.getWidget(Widget.Type.L3_NET).getWidgetId());
1064
1065         }else {
1066           System.out.println("Resource mapping not found for " + resourceNameVersionId);
1067         }
1068       }
1069
1070     }catch (IllegalArgumentException e) {
1071       Assert.fail(e.getMessage());    //Can come while populating metadata
1072     }
1073
1074   }
1075
1076   public void testAllottedResourceTosca(Map<String, Model> outputArtifactMap , ToscaTemplate
1077       resourceTosca) {
1078     try {
1079       if (resourceTosca != null) {
1080         Resource resource = new Resource();
1081         resource.populateModelIdentificationInformation(resourceTosca.getMetadata());
1082         String resourceNameVersionId = resource.getModelNameVersionId();
1083         Model resourceAAIModel =
1084             getAAIModelByNameVersionId(resourceNameVersionId, outputArtifactMap);
1085         if (resourceAAIModel != null) {
1086           validateResourceModelMetadata(resource, resourceAAIModel);
1087           //Validate Resource instance base widget
1088
1089           ModelVer modelVersion = resourceAAIModel.getModelVers().getModelVer().get(0);
1090
1091           List<ModelElement> matchedVFBaseWidgetElements =
1092               getModelElementbyRelationshipValue(modelVersion.getModelElements(),
1093                   Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getId());
1094           validateMatchedModelElementsInService(matchedVFBaseWidgetElements,
1095               Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getName());
1096
1097           validateWidgetIds(matchedVFBaseWidgetElements, Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getName(),
1098               Widget.getWidget(Widget.Type.ALLOTTED_RESOURCE).getWidgetId());
1099
1100           Map<String, Object> dependingServiceDetails = getProvidingServiceDetails(resourceTosca);
1101
1102           ModelElements containedModelElements = modelVersion.getModelElements().getModelElement().
1103                   get(0).getModelElements();
1104           Assert.assertEquals( containedModelElements.getModelElement().get(0).getRelationshipList()
1105                 .getRelationship().get(0).getRelationshipData().get(0).getRelationshipValue(),
1106                 dependingServiceDetails.get("providing_service_uuid"));
1107
1108           Assert.assertEquals(containedModelElements.getModelElement().get(0).getRelationshipList()
1109                 .getRelationship().get(0).getRelationshipData().get(1).getRelationshipValue(),
1110                 dependingServiceDetails.get("providing_service_invariant_uuid"));
1111
1112           if("Allotted Resource".equals(resourceTosca.getMetadata().get("category")) &&
1113                   "Tunnel XConnect".equals(resourceTosca.getMetadata().get("subcategory"))) {
1114
1115             List<ModelElement> matchedTunnelXConnectWidgetElements =
1116                     getModelElementbyRelationshipValue(containedModelElements,
1117                             Widget.getWidget(Widget.Type.TUNNEL_XCONNECT).getId());
1118             validateMatchedModelElementsInService(matchedTunnelXConnectWidgetElements,
1119                     Widget.getWidget(Widget.Type.TUNNEL_XCONNECT).getName());
1120
1121             validateWidgetIds(matchedTunnelXConnectWidgetElements, Widget.getWidget(Widget.Type.TUNNEL_XCONNECT).getName(),
1122                     Widget.getWidget(Widget.Type.TUNNEL_XCONNECT).getWidgetId());
1123           }
1124
1125         }else {
1126           System.out.println("Resource mapping not found for " + resourceNameVersionId);
1127         }
1128       }
1129
1130     }catch (IllegalArgumentException e) {
1131       Assert.fail(e.getMessage());    //Can come while populating metadata
1132     }
1133
1134   }
1135
1136   public Map<String, Object> getProvidingServiceDetails(ToscaTemplate resourceTemplate) {
1137 Set<String> keys = resourceTemplate.getTopology_template().getNode_templates().keySet();
1138
1139 Map<String, Object> nodeProperties =null;
1140 for(String key : keys) {
1141 NodeTemplate node = resourceTemplate.getTopology_template().getNode_templates().get(key);
1142 if(node.getType().equals("org.openecomp.resource.vfc.AllottedResource")) {
1143 nodeProperties = node.getProperties();
1144   }
1145 }
1146
1147   return nodeProperties;
1148   }
1149
1150   public void testVfTosca(Map<String, Model> outputArtifactMap , ToscaTemplate resourceTosca) {
1151     try {
1152       //ToscaTemplate resourceTosca = getResourceTosca(toscas);
1153       //resourceTosca.getTopology_template().getGroups().
1154       if (resourceTosca != null) {
1155         Resource resource = new Resource();
1156         resource.populateModelIdentificationInformation(resourceTosca.getMetadata());
1157         String resourceNameVersionId = resource.getModelNameVersionId();
1158         Model resourceAAIModel =
1159             getAAIModelByNameVersionId(resourceNameVersionId, outputArtifactMap);
1160         if (resourceAAIModel != null) {
1161           validateResourceModelMetadata(resource, resourceAAIModel);
1162           //Validate Resource instance base widget
1163
1164           ModelVer modelVersion = resourceAAIModel.getModelVers().getModelVer().get(0);
1165
1166           List<ModelElement> matchedVFBaseWidgetElements =
1167               getModelElementbyRelationshipValue(modelVersion.getModelElements(),
1168                   Widget.getWidget(Widget.Type.VF).getId());
1169           validateMatchedModelElementsInService(matchedVFBaseWidgetElements,
1170               Widget.getWidget(Widget.Type.VF).getName());
1171
1172           validateWidgetIds(matchedVFBaseWidgetElements, Widget.getWidget(Widget.Type.VF).getName(),
1173                 Widget.getWidget(Widget.Type.VF).getWidgetId());
1174
1175           ModelElements baseResourceWidgetModelElements =
1176               matchedVFBaseWidgetElements.get(0).getModelElements();
1177           if (resourceTosca.getTopology_template() != null) {
1178             Map<String, String> groupIdTypeStore = getGroupsTypeStore(resourceTosca);
1179
1180             if (baseResourceWidgetModelElements.getModelElement().size() !=
1181                 groupIdTypeStore.size()) {
1182               Assert.fail("Missing VFModule in VF model.xml");
1183             }
1184
1185             for (String key : groupIdTypeStore.keySet()) {
1186
1187               List<ModelElement> matchedResourceElements =
1188                   getModelElementbyRelationshipValue(baseResourceWidgetModelElements, key);
1189               validateMatchedModelElementsInService(matchedResourceElements,
1190                   Widget.getWidget(Widget.Type.VFMODULE).getName());
1191               Model resourceAAIVFModel = getAAIModelByNameVersionId(key, outputArtifactMap);
1192               Map<String, String> vfModuleModelMetadata =
1193                   getVFModuleMetadataTosca(resourceTosca, key);
1194               Map<String, Object> vfModuleMembers = getVFModuleMembersTosca(resourceTosca, key);
1195
1196               validateVFModelMetadata(vfModuleModelMetadata, resourceAAIVFModel);
1197
1198
1199               ModelVer modelVfVersion = resourceAAIVFModel.getModelVers().getModelVer().get(0);
1200
1201               List<ModelElement> matchedVFModuleBaseWidgetElements =
1202                   getModelElementbyRelationshipValue(modelVfVersion.getModelElements(),
1203                       Widget.getWidget(Widget.Type.VFMODULE).getId());
1204               validateMatchedModelElementsInService(matchedVFModuleBaseWidgetElements,
1205                   Widget.getWidget(Widget.Type.VFMODULE).getName());
1206               validateWidgetIds(matchedVFModuleBaseWidgetElements, Widget.getWidget(Widget.Type.VFMODULE)
1207                   .getName(), Widget.getWidget(Widget.Type.VFMODULE).getWidgetId());
1208
1209               ModelElements baseResourceVFModuleWidgetModelElements =
1210                   matchedVFModuleBaseWidgetElements.get(0).getModelElements();
1211              if (vfModuleMembers.containsKey("l3-network")) {
1212                 //Validate l3
1213                 List<ModelElement> matchedL3NetworkElements =
1214                     getModelElementbyRelationshipValue(baseResourceVFModuleWidgetModelElements,
1215                         Widget.getWidget(Widget.Type.L3_NET).getId());
1216                 validateMatchedModelElementsInService(matchedL3NetworkElements,
1217                     Widget.getWidget(Widget.Type.L3_NET).getName());
1218                 validateWidgetIds(matchedL3NetworkElements, Widget.getWidget(Widget.Type.L3_NET)
1219                     .getName(), Widget.getWidget(Widget.Type.L3_NET).getWidgetId());
1220               }
1221               if (vfModuleMembers.containsKey("vserver")) {
1222                 //Validate vserver
1223                 List<ModelElement> matchedVserverElements =
1224                     getModelElementbyRelationshipValue(baseResourceVFModuleWidgetModelElements,
1225                         Widget.getWidget(Widget.Type.VSERVER).getId());
1226                 validateMatchedModelElementsInService(matchedVserverElements,
1227                     Widget.getWidget(Widget.Type.VSERVER).getName());
1228                 ModelElements vserverWidgetModelElements =
1229                     matchedVserverElements.get(0).getModelElements();
1230
1231                 validateWidgetIds(matchedVserverElements, Widget.getWidget(Widget.Type.VSERVER)
1232                     .getName(), Widget.getWidget(Widget.Type.VSERVER).getWidgetId());
1233
1234
1235                 //Validate vserver->vfc
1236                 List<ModelElement> matchedVfcElements =
1237                     getModelElementbyRelationshipValue(vserverWidgetModelElements,
1238                         Widget.getWidget(Widget.Type.VFC).getId());
1239                 validateMatchedModelElementsInService(matchedVfcElements,
1240                     Widget.getWidget(Widget.Type.VFC).getName());
1241                 validateWidgetIds(matchedVfcElements, Widget.getWidget(Widget.Type.VFC).getName(),
1242                     Widget.getWidget(Widget.Type.VFC).getWidgetId());
1243
1244                 //Validate vserver->Image
1245                 List<ModelElement> matchedImageElements =
1246                     getModelElementbyRelationshipValue(vserverWidgetModelElements,
1247                         Widget.getWidget(Widget.Type.IMAGE).getId());
1248                 validateMatchedModelElementsInService(matchedImageElements,
1249                     Widget.getWidget(Widget.Type.IMAGE).getName());
1250                 validateWidgetIds(matchedImageElements, Widget.getWidget(Widget.Type.IMAGE)
1251                     .getName(), Widget.getWidget(Widget.Type.IMAGE).getWidgetId());
1252
1253
1254                 //Validate vserver->Flavor
1255                 List<ModelElement> matchedFlavorElements =
1256                     getModelElementbyRelationshipValue(vserverWidgetModelElements,
1257                         Widget.getWidget(Widget.Type.FLAVOR).getId());
1258                 validateMatchedModelElementsInService(matchedFlavorElements,
1259                     Widget.getWidget(Widget.Type.FLAVOR).getName());
1260                 validateWidgetIds(matchedFlavorElements, Widget.getWidget(Widget.Type.FLAVOR).getName(),
1261                     Widget.getWidget(Widget.Type.FLAVOR).getWidgetId());
1262
1263                 //Validate vserver->Tenant
1264                 List<ModelElement> matchedTenantElements =
1265                     getModelElementbyRelationshipValue(vserverWidgetModelElements,
1266                         Widget.getWidget(Widget.Type.TENANT).getId());
1267                 validateMatchedModelElementsInService(matchedTenantElements,
1268                     Widget.getWidget(Widget.Type.TENANT).getName());
1269                 validateWidgetIds(matchedTenantElements, Widget.getWidget(Widget.Type.TENANT).getName(),
1270                     Widget.getWidget(Widget.Type.TENANT).getWidgetId());
1271
1272                 //Validate vserver->l-interface
1273                 if (vfModuleMembers.containsKey("l-interface")) {
1274                   List<ModelElement> matchedLinterfaceElements =
1275                       getModelElementbyRelationshipValue(vserverWidgetModelElements,
1276                           Widget.getWidget(Widget.Type.LINT).getId());
1277                   validateMatchedModelElementsInService(matchedLinterfaceElements,
1278                       Widget.getWidget(Widget.Type.LINT).getName());
1279                   validateWidgetIds(matchedLinterfaceElements, Widget.getWidget(Widget.Type.LINT).getName(),
1280                       Widget.getWidget(Widget.Type.LINT).getWidgetId());
1281                 }
1282                 //Validate vserver->volume
1283                 if (vfModuleMembers.containsKey("volume")) {
1284                   List<ModelElement> matchedVolumeElements =
1285                       getModelElementbyRelationshipValue(vserverWidgetModelElements,
1286                           Widget.getWidget(Widget.Type.VOLUME).getId());
1287                   validateMatchedModelElementsInService(matchedVolumeElements,
1288                       Widget.getWidget(Widget.Type.VOLUME).getName());
1289                   validateWidgetIds(matchedVolumeElements, Widget.getWidget(Widget.Type.VOLUME).getName(),
1290                       Widget.getWidget(Widget.Type.VOLUME).getWidgetId());
1291                 }
1292               }
1293             }
1294           }
1295         } else {
1296           System.out.println("Resource mapping not found for " + resourceNameVersionId);
1297         }
1298       }
1299
1300     } catch (IllegalArgumentException e) {
1301       Assert.fail(e.getMessage());    //Can come while populating metadata
1302     }
1303
1304   }
1305
1306   private void validateMatchedModelElementsInService(List<ModelElement> matchedModelElements,
1307                                                      String modelType) {
1308     if (matchedModelElements.isEmpty()) {
1309       Assert.fail(modelType + " not present ");
1310     }
1311     if (matchedModelElements.size() > 1) {
1312       Assert.fail("More than one " + modelType + " present ");
1313     }
1314   }
1315
1316   private Map<String, String> getNodeTemplateTypeStore(ToscaTemplate toscaTemplate) {
1317     if (toscaTemplate.getTopology_template() != null) {
1318       Map<String, NodeTemplate> nodeTemplateMap =
1319           toscaTemplate.getTopology_template().getNode_templates();
1320       Map<String, String> nodeTemplateIdTypeStore = new LinkedHashMap<>();
1321       if (nodeTemplateMap != null) {
1322         for (Map.Entry<String, NodeTemplate> e : nodeTemplateMap.entrySet()) {
1323           String uuid = e.getValue().getMetadata().get("resourceUUID");
1324           if (GeneratorUtil.isEmpty(uuid)) {
1325             uuid = e.getValue().getMetadata().get("UUID");
1326             if (GeneratorUtil.isEmpty(uuid)) {
1327               Assert.fail("UUID Not found");
1328             }
1329           }
1330           if(e.getValue().getType().contains("org.openecomp.resource.vf.")&& (e.getValue()
1331               .getMetadata().get("category").equals("Allotted Resource")))
1332           {
1333             e.getValue().setType("org.openecomp.resource.vf.allottedResource");
1334           }
1335           nodeTemplateIdTypeStore.put(uuid, e.getValue().getType());
1336           resourcesVersion.put(uuid,e.getValue().getMetadata().get
1337               ("version"));
1338           //Populate invraintUuId for V9
1339           String invUuId = e.getValue().getMetadata().get("invariantUUID");
1340           nodeTemplateIdTypeStore.put(uuid+"-INV_UID" , invUuId);
1341         }
1342       }
1343       return nodeTemplateIdTypeStore;
1344     } else {
1345       return null;
1346     }
1347   }
1348
1349   private Map<String, String> getGroupsTypeStore(ToscaTemplate toscaTemplate) {
1350     if (toscaTemplate.getTopology_template() != null) {
1351       Map<String, GroupDefinition> groupDefinitionMap =
1352           toscaTemplate.getTopology_template().getGroups();
1353       Map<String, String> groupDefinitionIdTypeStore = new LinkedHashMap<>();
1354       if (groupDefinitionMap != null) {
1355         for (Map.Entry<String, GroupDefinition> e : groupDefinitionMap.entrySet()) {
1356           if (e.getValue().getType().contains("org.openecomp.groups.VfModule")) {
1357             String uuid = e.getValue().getMetadata().get("vfModuleModelUUID");
1358             if (GeneratorUtil.isEmpty(uuid)) {
1359               uuid = e.getValue().getMetadata().get("UUID");
1360               if (GeneratorUtil.isEmpty(uuid)) {
1361                 Assert.fail("UUID Not found");
1362               }
1363             }
1364             groupDefinitionIdTypeStore.put(uuid, e.getValue().getType());
1365           }
1366         }
1367       }
1368       return groupDefinitionIdTypeStore;
1369     } else {
1370       return null;
1371     }
1372
1373   }
1374
1375   private void validateServiceModelMetadata(Service serviceToscaModel, Model generatedAAIModel) {
1376     ModelVer modelVersion =  generatedAAIModel.getModelVers().getModelVer().get(0);
1377     Assert.assertEquals(serviceToscaModel.getModelNameVersionId(),
1378         modelVersion.getModelVersionId());
1379     Assert.assertEquals(serviceToscaModel.getModelId(), generatedAAIModel.getModelInvariantId());
1380     Assert.assertEquals(serviceToscaModel.getModelName(), modelVersion.getModelName());
1381     Assert.assertEquals(additionalParams.get(AdditionalParams.ServiceVersion.getName()), modelVersion
1382         .getModelVersion());
1383     Assert.assertEquals(serviceToscaModel.getModelDescription(),
1384         modelVersion.getModelDescription());
1385
1386   }
1387
1388   private void validateResourceModelMetadata(Resource resouerceToscaModel,
1389                                              Model generatedAAIModel) {
1390     ModelVer modelVersion =  generatedAAIModel.getModelVers().getModelVer().get(0);
1391     Assert.assertEquals(resouerceToscaModel.getModelNameVersionId(),
1392         modelVersion.getModelVersionId());
1393     Assert.assertEquals(resouerceToscaModel.getModelId(), generatedAAIModel.getModelInvariantId());
1394     Assert.assertEquals(resouerceToscaModel.getModelName(), modelVersion.getModelName());
1395     Assert
1396         .assertEquals(resouerceToscaModel.getModelVersion(), modelVersion.getModelVersion());
1397     Assert.assertEquals(resouerceToscaModel.getModelDescription(),
1398         modelVersion.getModelDescription());
1399
1400   }
1401
1402   private void validateVFModelMetadata(Map<String, String> vfModuleModelMetadata,
1403                                        Model generatedAAIModel) {
1404     ModelVer modelVersion =  generatedAAIModel.getModelVers().getModelVer().get(0);
1405     Assert.assertEquals(vfModuleModelMetadata.get("vfModuleModelUUID"),
1406         modelVersion.getModelVersionId());
1407     Assert.assertEquals(vfModuleModelMetadata.get("vfModuleModelInvariantUUID"),
1408         generatedAAIModel.getModelInvariantId());
1409     Assert.assertEquals(vfModuleModelMetadata.get("vfModuleModelName"),
1410         modelVersion.getModelName());
1411     Assert.assertEquals(vfModuleModelMetadata.get("vfModuleModelVersion"),
1412         modelVersion.getModelVersion());
1413     Assert.assertEquals(vfModuleModelMetadata.get("vf_module_description"),
1414         modelVersion.getModelDescription());
1415   }
1416
1417   private Model getAAIModelByNameVersionId(String nameVersionId,
1418                                            Map<String, Model> outputArtifactMap) {
1419     return outputArtifactMap.get(nameVersionId);
1420   }
1421
1422   private List<ModelElement> getModelElementbyRelationshipValue(ModelElements modelElements,
1423                                                                 String relationshipValue) {
1424     List<ModelElement> matchedModelElements = new ArrayList<>();
1425     if (modelElements != null) {
1426       List<ModelElement> modelElementList = modelElements.getModelElement();
1427       for (ModelElement element : modelElementList) {
1428         List<Relationship> relationshipList = element.getRelationshipList().getRelationship();
1429         for (Relationship r : relationshipList) {
1430           List<RelationshipData> relationshipDataList = r.getRelationshipData();
1431           for (RelationshipData relationshipData : relationshipDataList) {
1432             if (relationshipData.getRelationshipValue().equals(relationshipValue)) {
1433               matchedModelElements.add(element);
1434             }
1435           }
1436         }
1437       }
1438     }
1439     return matchedModelElements;
1440   }
1441
1442   private Map<String, Model> populateAAIGeneratedModelStore(List<Artifact> resultData) throws IOException {
1443     Map<String, Model> outputArtifactMap = new HashMap<>();
1444     for (Artifact outputArtifact : resultData) {
1445       if (outputArtifact.getType().equals(ArtifactType.MODEL_INVENTORY_PROFILE.name())) {
1446         byte[] decodedPayload = GeneratorUtil.decoder(outputArtifact.getPayload());
1447         Model aaiModel = getUnmarshalledArtifactModel(new String(decodedPayload));
1448         List<ModelVer> modelVersions =  aaiModel.getModelVers().getModelVer();
1449         outputArtifactMap.put(modelVersions.get(0).getModelVersionId(), aaiModel);
1450       }
1451     }
1452     return outputArtifactMap;
1453   }
1454
1455   private Model getUnmarshalledArtifactModel(String aaiModel) throws IOException {
1456
1457     try {
1458
1459       JAXBContext jaxbContext = JAXBContext.newInstance(Model.class);
1460       Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
1461
1462       try (InputStream aaiModelStream = new ByteArrayInputStream(aaiModel.getBytes())) {
1463         return (Model) unmarshaller.unmarshal(aaiModelStream);
1464       }
1465
1466     } catch (JAXBException e) {
1467       e.printStackTrace();
1468     }
1469
1470     return null;
1471   }
1472
1473   /**
1474    * Get the tosca java model from the tosca input artifact
1475    *
1476    * @param input Input tosca file and its metadata information as {@link Artifact} object
1477    * @return Translated {@link ToscaTemplate tosca} object
1478    */
1479   private ToscaTemplate getToscaModel(Artifact input) throws SecurityException {
1480     byte[] decodedInput = GeneratorUtil.decoder(input.getPayload());
1481     String checksum = GeneratorUtil.checkSum(decodedInput);
1482     if (checksum.equals(input.getChecksum())) {
1483       try {
1484         return GeneratorUtil.translateTosca(new String(decodedInput), ToscaTemplate.class);
1485       } catch (Exception e) {
1486         e.printStackTrace();
1487         throw new IllegalArgumentException(
1488             String.format(GeneratorConstants.GENERATOR_AAI_ERROR_INVALID_TOSCA, input.getName()));
1489       }
1490     } else {
1491       throw new SecurityException(
1492           String.format(GeneratorConstants.GENERATOR_AAI_ERROR_CHECKSUM_MISMATCH, input.getName()));
1493     }
1494   }
1495
1496   /**
1497    * Identify the service tosca artifact from the list of translated tosca inputs
1498    *
1499    * @param input List of translated {@link ToscaTemplate tosca} object models
1500    * @return Identified service {@link ToscaTemplate tosca}
1501    */
1502   private ToscaTemplate getServiceTosca(List<ToscaTemplate> input) {
1503     Iterator<ToscaTemplate> iter = input.iterator();
1504     while (iter.hasNext()) {
1505       ToscaTemplate tosca = iter.next();
1506       if (tosca.isService()) {
1507         iter.remove();
1508         return tosca;
1509       }
1510     }
1511     return null;
1512   }
1513
1514   private Map<String, String> getVFModuleMetadataTosca(ToscaTemplate toscaTemplate,
1515                                                        String vfModuleModelUUID) {
1516     Map<String, GroupDefinition> groupDefinitionMap =
1517         toscaTemplate.getTopology_template().getGroups();
1518     Map<String, String> vfModuleModelMetadata = new LinkedHashMap<>();
1519     for (Map.Entry<String, GroupDefinition> e : groupDefinitionMap.entrySet()) {
1520       if (e.getValue().getType().contains("org.openecomp.groups.VfModule")) {
1521         String uuid = e.getValue().getMetadata().get("vfModuleModelUUID");
1522         if (Objects.equals(uuid, vfModuleModelUUID)) {
1523           vfModuleModelMetadata = e.getValue().getMetadata();
1524           vfModuleModelMetadata.put("vf_module_description",
1525               (String) e.getValue().getProperties().get("vf_module_description"));
1526         }
1527       }
1528     }
1529     return vfModuleModelMetadata;
1530   }
1531
1532   private Map<String, Object> getVFModuleMembersTosca(ToscaTemplate toscaTemplate,
1533                                                       String vfModuleModelUUID) {
1534     Map<String, GroupDefinition> groupDefinitionMap =
1535         toscaTemplate.getTopology_template().getGroups();
1536     Map<String, NodeTemplate> nodeTemplateMaps =
1537         toscaTemplate.getTopology_template().getNode_templates();
1538     Map<String, Object> vfModuleMembers = new LinkedHashMap<>();
1539     List<String> vfModuleModelMetadata;
1540     for (Map.Entry<String, GroupDefinition> e : groupDefinitionMap.entrySet()) {
1541       if (e.getValue().getType().contains("org.openecomp.groups.VfModule")) {
1542         String uuid = e.getValue().getMetadata().get("vfModuleModelUUID");
1543         if (Objects.equals(uuid, vfModuleModelUUID)) {
1544           vfModuleModelMetadata = e.getValue().getMembers();
1545           if (vfModuleModelMetadata !=null) {
1546             for (Object key : vfModuleModelMetadata) {
1547               NodeTemplate nodeTemplate = nodeTemplateMaps.get(key);
1548               String nodetype = null;
1549               if (nodeTemplate != null) {
1550                 nodetype = nodeTemplate.getType();
1551               }
1552               if (nodetype != null) {
1553                 String widgetType = membersType(nodetype);
1554                 if (widgetType != null) {
1555                   vfModuleMembers.put(widgetType, key);
1556                 }
1557               }
1558             }
1559           }
1560         }
1561       }
1562     }
1563
1564     return vfModuleMembers;
1565   }
1566
1567
1568   private String membersType(String toscaType) {
1569     String modelToBeReturned = null;
1570     while (toscaType != null && toscaType.lastIndexOf(".") != -1 && modelToBeReturned == null) {
1571
1572       switch (toscaType) {
1573         case "org.openecomp.resource.vf.allottedResource":
1574           modelToBeReturned = "allotted-resource";
1575           break;
1576         case "org.openecomp.resource.vfc":
1577           modelToBeReturned = "vserver";
1578           break;
1579         case "org.openecomp.resource.cp":
1580         case "org.openecomp.cp":
1581           modelToBeReturned = "l-interface";
1582           break;
1583         case "org.openecomp.resource.vl":
1584           modelToBeReturned = "l3-network";
1585           break;
1586         case "org.openecomp.resource.vf":
1587           modelToBeReturned = "generic-vnf";
1588           break;
1589         case "org.openecomp.groups.VfModule":
1590           modelToBeReturned = "vf-module";
1591           break;
1592         case "org.openecomp.resource.vfc.nodes.heat.cinder":
1593           modelToBeReturned = "volume";
1594           break;
1595         default:
1596           modelToBeReturned = null;
1597           break;
1598       }
1599
1600       toscaType = toscaType.substring(0, toscaType.lastIndexOf("."));
1601     }
1602     return modelToBeReturned;
1603   }
1604
1605   private List<Artifact> init(String aaiResourceBasePaths) {
1606     List<Artifact> inputArtifacts1 = new ArrayList<>();
1607     try {
1608
1609       String[] resourceFileList = {};
1610       URL resourceDirUrl = this.getClass().getClassLoader().getResource(aaiResourceBasePaths);
1611       if (resourceDirUrl != null && resourceDirUrl.getProtocol().equals("file")) {
1612         resourceFileList = new File(resourceDirUrl.toURI()).list();
1613       } else {
1614         Assert.fail("Invalid resource directory");
1615       }
1616
1617       for (String aResourceFileList : resourceFileList) {
1618         File resourceFile = new File(
1619                 this.getClass().getClassLoader().getResource(aaiResourceBasePaths + aResourceFileList)
1620                         .getPath());
1621
1622         //convert service tosca file into array of bytes
1623         byte[] payload = new byte[(int) resourceFile.length()];
1624         try (FileInputStream fileInputStream = new FileInputStream(resourceFile)) {
1625           fileInputStream.read(payload);
1626         }
1627
1628         String checksum = GeneratorUtil.checkSum(payload);
1629         byte[] encodedPayload = GeneratorUtil.encode(payload);
1630         Artifact artifact =
1631                 new Artifact(aaiArtifactType, aaiArtifactGroupType, checksum, encodedPayload);
1632         artifact.setName(aResourceFileList);
1633         artifact.setLabel(aResourceFileList);
1634         artifact.setDescription(aResourceFileList);
1635         artifact.setVersion("1.0");
1636         inputArtifacts1.add(artifact);
1637
1638       }
1639     } catch (Exception e) {
1640       //e.printStackTrace();
1641       Assert.fail(e.getMessage());
1642     }
1643     return inputArtifacts1;
1644   }
1645
1646   private void loadConfigFromClasspath(Properties properties) throws IOException {
1647     String configLocation = System.getProperty("artifactgenerator.config");
1648     if (configLocation != null) {
1649       File file = new File(configLocation);
1650       if (file.exists()) {
1651         properties.load(new FileInputStream(file));
1652       }
1653     }
1654   }
1655
1656   public  void testResourceTosca(Iterator<ToscaTemplate> itr, Map<String, Model>
1657       outputArtifactMap) {
1658     while(itr.hasNext()){
1659       ToscaTemplate toscaTemplate = itr.next();
1660       String resourceVersion=resourcesVersion.get(toscaTemplate.getMetadata().get("UUID"));
1661       toscaTemplate.getMetadata().put("version", resourceVersion);
1662       if("VF".equals(toscaTemplate.getMetadata().get("type")) && !("Allotted Resource".equals
1663           (toscaTemplate.getMetadata().get("category"))) ){
1664         testVfTosca(outputArtifactMap, toscaTemplate);
1665       } else if("VF".equals(toscaTemplate.getMetadata().get("type")) && ("Allotted Resource".equals
1666           (toscaTemplate.getMetadata().get("category"))) ){
1667         testAllottedResourceTosca(outputArtifactMap, toscaTemplate);
1668       } else if("VL".equals(toscaTemplate.getMetadata().get("type"))){
1669         testL3NetworkResourceTosca(outputArtifactMap, toscaTemplate);
1670       }
1671     }
1672   }
1673
1674   private void readPayloadFromResource(List<Artifact> inputArtifacts, String file) throws IOException {
1675     try (InputStream stream = ArtifactGenerationServiceTest.class.getResourceAsStream("/" + file)) {
1676       readPayload(inputArtifacts, stream, file);
1677     }
1678   }
1679 }