1 package org.openecomp.sdc.be.tosca;
4 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
5 import org.openecomp.sdc.be.model.operations.impl.ComponentInstanceOperation;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.springframework.beans.factory.annotation.Autowired;
10 import com.google.gson.Gson;
11 import com.google.gson.GsonBuilder;
13 public class ToscaExportUtilsTest {
14 private static final Logger log = LoggerFactory.getLogger(ToscaExportUtilsTest.class);
15 @javax.annotation.Resource
16 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
18 private ToscaExportHandler exportUtils;
20 private ComponentInstanceOperation componentInstanceOperation;
22 Gson gson = new GsonBuilder().setPrettyPrinting().create();
25 public void testExportService() {
26 /* Resource resource1 = ResourceTestUtils.prepareResource(0);
27 resource1.setResourceType(ResourceTypeEnum.VF);
28 Either<Resource, ResponseFormat> createResource1 = resourceBusinessLogic.createResource(resource1, getAdminUser(), null, null);
29 assertTrue(createResource1.isLeft());
30 Resource certifiedVFC1 = changeResourceStateToCertify(createResource1.left().value());
32 Resource resource2 = ResourceTestUtils.prepareResource(1);
33 resource2.setResourceType(ResourceTypeEnum.VF);
34 Either<Resource, ResponseFormat> createResource2 = resourceBusinessLogic.createResource(resource2, getAdminUser(), null, null);
35 assertTrue(createResource2.isLeft());
36 Resource certifiedVFC2 = changeResourceStateToCertify(createResource2.left().value());
38 Service service = ResourceTestUtils.prepareService(0);
39 Either<Service, ResponseFormat> createService = serviceBusinessLogic.createService(service, getAdminUser());
40 assertTrue(createService.isLeft());
42 // add VFC instance to VF
43 ComponentInstance vfcResourceInstance1 = new ComponentInstance();
44 vfcResourceInstance1.setDescription("VFC instance 1");
45 vfcResourceInstance1.setName(certifiedVFC1.getName());
46 vfcResourceInstance1.setComponentUid(certifiedVFC1.getUniqueId());
48 Either<ComponentInstance, ResponseFormat> createResourceVfcInstance1 = serviceInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, createService.left().value().getUniqueId(), adminUser.getAttuid(),
49 vfcResourceInstance1);
50 assertTrue(createResourceVfcInstance1.isLeft());
52 ComponentInstance vfcResourceInstance2 = new ComponentInstance();
53 vfcResourceInstance2.setDescription("VFC instance 2");
54 vfcResourceInstance2.setName(certifiedVFC2.getName());
55 vfcResourceInstance2.setComponentUid(certifiedVFC2.getUniqueId());
56 Either<ComponentInstance, ResponseFormat> createResourceVfcInstance2 = serviceInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, createService.left().value().getUniqueId(), adminUser.getAttuid(),
57 vfcResourceInstance2);
58 assertTrue(createResourceVfcInstance2.isLeft());
60 Either<Service, ResponseFormat> serviceFetch = serviceBusinessLogic.getService(createService.left().value().getUniqueId(), adminUser);
61 assertTrue(serviceFetch.isLeft());
63 List<ComponentInstance> componentInstances = serviceFetch.left().value().getComponentInstances();
64 String ciname1 = null;
65 String ciname2 = null;
67 for (ComponentInstance ci : componentInstances) {
68 if (ci.getComponentUid().equals(certifiedVFC1.getUniqueId())) {
69 ciname1 = ci.getName();
71 if (ci.getComponentUid().equals(certifiedVFC2.getUniqueId())) {
72 ciname2 = ci.getName();
76 Either<ToscaRepresentation, ToscaError> result = exportUtils.exportComponent(serviceFetch.left().value());
77 assertTrue(result.isLeft());
79 String mainYaml = result.left().value().getMainYaml();
80 assertNotNull(mainYaml);
82 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
83 assertTrue(yamlToObjectConverter.isValidYaml(mainYaml.getBytes()));
86 Yaml yaml = new Yaml();
88 InputStream inputStream = new ByteArrayInputStream(mainYaml.getBytes());
89 Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
90 Map<String, Object> imports = (Map<String, Object>) load.get("imports");
91 assertNotNull(imports);
92 assertEquals("Validate imports size in yml", 2, imports.size());
94 Map<String, Object> metadata = (Map<String, Object>) load.get("metadata");
95 assertNotNull(metadata);
96 validateMetadata(metadata, serviceFetch.left().value(), false);
98 Map<String, Object> vf1 = (Map<String, Object>) imports.get(certifiedVFC1.getName());
99 String fileName = (String) vf1.get(ToscaExportHandler.IMPORTS_FILE_KEY);
100 ArtifactDefinition artifactDefinition = certifiedVFC1.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
101 assertEquals("Validate 1 file name", artifactDefinition.getArtifactName(), fileName);
103 Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template");
104 Map<String, Object> node_templates = (Map<String, Object>) topology_template.get("node_templates");
105 Map<String, Object> inst1 = (Map<String, Object>) node_templates.get(ciname1);
106 Map<String, Object> inst2 = (Map<String, Object>) node_templates.get(ciname2);
108 Map<String, Object> inst1MD = (Map<String, Object>) inst1.get("metadata");
109 Map<String, Object> inst2MD = (Map<String, Object>) inst2.get("metadata");
111 validateMetadata(inst1MD, certifiedVFC1, true);
113 Map<String, Object> vf2 = (Map<String, Object>) imports.get(certifiedVFC2.getName());
114 fileName = (String) vf2.get(ToscaExportHandler.IMPORTS_FILE_KEY);
115 artifactDefinition = certifiedVFC2.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
116 assertEquals("Validate 2 file name", artifactDefinition.getArtifactName(), fileName);
118 validateMetadata(inst2MD, certifiedVFC2, true);*/