aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportUtilsTest.java
blob: 4c22cf3599b91bfcdf1918a5c5f9821a1b1cc96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.openecomp.sdc.be.tosca;

import org.junit.Test;
import org.openecomp.sdc.be.components.impl.ServiceComponentInstanceBusinessLogic;
import org.openecomp.sdc.be.components.impl.VFComponentInstanceBusinessLogic;
import org.openecomp.sdc.be.model.operations.impl.ComponentInstanceOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ToscaExportUtilsTest {
	private static Logger log = LoggerFactory.getLogger(ToscaExportUtilsTest.class.getName());
	@javax.annotation.Resource
	private VFComponentInstanceBusinessLogic componentInstanceBusinessLogic;
	@javax.annotation.Resource
	private ServiceComponentInstanceBusinessLogic serviceInstanceBusinessLogic;
	@Autowired
	private ToscaExportHandler exportUtils;
	@Autowired
	private ComponentInstanceOperation componentInstanceOperation;

	Gson gson = new GsonBuilder().setPrettyPrinting().create();
	
	@Test
	public void testExportService() {
	/*	Resource resource1 = ResourceTestUtils.prepareResource(0);
		resource1.setResourceType(ResourceTypeEnum.VF);
		Either<Resource, ResponseFormat> createResource1 = resourceBusinessLogic.createResource(resource1, getAdminUser(), null, null);
		assertTrue(createResource1.isLeft());
		Resource certifiedVFC1 = changeResourceStateToCertify(createResource1.left().value());

		Resource resource2 = ResourceTestUtils.prepareResource(1);
		resource2.setResourceType(ResourceTypeEnum.VF);
		Either<Resource, ResponseFormat> createResource2 = resourceBusinessLogic.createResource(resource2, getAdminUser(), null, null);
		assertTrue(createResource2.isLeft());
		Resource certifiedVFC2 = changeResourceStateToCertify(createResource2.left().value());

		Service service = ResourceTestUtils.prepareService(0);
		Either<Service, ResponseFormat> createService = serviceBusinessLogic.createService(service, getAdminUser());
		assertTrue(createService.isLeft());

		// add VFC instance to VF
		ComponentInstance vfcResourceInstance1 = new ComponentInstance();
		vfcResourceInstance1.setDescription("VFC instance 1");
		vfcResourceInstance1.setName(certifiedVFC1.getName());
		vfcResourceInstance1.setComponentUid(certifiedVFC1.getUniqueId());

		Either<ComponentInstance, ResponseFormat> createResourceVfcInstance1 = serviceInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, createService.left().value().getUniqueId(), adminUser.getAttuid(),
				vfcResourceInstance1);
		assertTrue(createResourceVfcInstance1.isLeft());

		ComponentInstance vfcResourceInstance2 = new ComponentInstance();
		vfcResourceInstance2.setDescription("VFC instance 2");
		vfcResourceInstance2.setName(certifiedVFC2.getName());
		vfcResourceInstance2.setComponentUid(certifiedVFC2.getUniqueId());
		Either<ComponentInstance, ResponseFormat> createResourceVfcInstance2 = serviceInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, createService.left().value().getUniqueId(), adminUser.getAttuid(),
				vfcResourceInstance2);
		assertTrue(createResourceVfcInstance2.isLeft());

		Either<Service, ResponseFormat> serviceFetch = serviceBusinessLogic.getService(createService.left().value().getUniqueId(), adminUser);
		assertTrue(serviceFetch.isLeft());

		List<ComponentInstance> componentInstances = serviceFetch.left().value().getComponentInstances();
		String ciname1 = null;
		String ciname2 = null;

		for (ComponentInstance ci : componentInstances) {
			if (ci.getComponentUid().equals(certifiedVFC1.getUniqueId())) {
				ciname1 = ci.getName();
			}
			if (ci.getComponentUid().equals(certifiedVFC2.getUniqueId())) {
				ciname2 = ci.getName();
			}
		}

		Either<ToscaRepresentation, ToscaError> result = exportUtils.exportComponent(serviceFetch.left().value());
		assertTrue(result.isLeft());

		String mainYaml = result.left().value().getMainYaml();
		assertNotNull(mainYaml);

		YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
		assertTrue(yamlToObjectConverter.isValidYaml(mainYaml.getBytes()));
		log.debug(mainYaml);

		Yaml yaml = new Yaml();

		InputStream inputStream = new ByteArrayInputStream(mainYaml.getBytes());
		Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream);
		Map<String, Object> imports = (Map<String, Object>) load.get("imports");
		assertNotNull(imports);
		assertEquals("Validate imports size in yml", 2, imports.size());

		Map<String, Object> metadata = (Map<String, Object>) load.get("metadata");
		assertNotNull(metadata);
		validateMetadata(metadata, serviceFetch.left().value(), false);

		Map<String, Object> vf1 = (Map<String, Object>) imports.get(certifiedVFC1.getName());
		String fileName = (String) vf1.get(ToscaExportHandler.IMPORTS_FILE_KEY);
		ArtifactDefinition artifactDefinition = certifiedVFC1.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
		assertEquals("Validate 1 file name", artifactDefinition.getArtifactName(), fileName);

		Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template");
		Map<String, Object> node_templates = (Map<String, Object>) topology_template.get("node_templates");
		Map<String, Object> inst1 = (Map<String, Object>) node_templates.get(ciname1);
		Map<String, Object> inst2 = (Map<String, Object>) node_templates.get(ciname2);

		Map<String, Object> inst1MD = (Map<String, Object>) inst1.get("metadata");
		Map<String, Object> inst2MD = (Map<String, Object>) inst2.get("metadata");

		validateMetadata(inst1MD, certifiedVFC1, true);

		Map<String, Object> vf2 = (Map<String, Object>) imports.get(certifiedVFC2.getName());
		fileName = (String) vf2.get(ToscaExportHandler.IMPORTS_FILE_KEY);
		artifactDefinition = certifiedVFC2.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE);
		assertEquals("Validate 2 file name", artifactDefinition.getArtifactName(), fileName);

		validateMetadata(inst2MD, certifiedVFC2, true);*/
	}

}