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