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