aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java
blob: 6fc87712428c6a56d00c83dbf3f11b085ab1d8f7 (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
125
126
127
128
129
130
131
132
133
134
package org.onap.sdc.toscaparser.api;

import org.junit.Test;
import org.onap.sdc.toscaparser.api.common.JToscaException;
import org.onap.sdc.toscaparser.api.parameters.Annotation;
import org.onap.sdc.toscaparser.api.parameters.Input;
import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class JToscaImportTest {

	@Test
	public void testNoMissingTypeValidationError() throws JToscaException {
		String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/sdc-onboarding_csar.csar")
				.getFile();
		File file = new File(fileStr);
		new ToscaTemplate(file.getAbsolutePath(), null, true, null);
		List<String> missingTypeErrors = ThreadLocalsHolder.getCollector().getValidationIssueReport().stream()
				.filter(s -> s.contains("JE136")).collect(Collectors.toList());
		assertEquals(0, missingTypeErrors.size());
	}

	@Test
	public void testNoStackOverFlowError() {
		Exception jte = null;
		try {
			String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/sdc-onboarding_csar.csar")
					.getFile();
			File file = new File(fileStr);
			new ToscaTemplate(file.getAbsolutePath(), null, true, null);
		} catch (Exception e) {
			jte = e;
		}
		assertEquals(null, jte);
	}

	@Test
	public void testNoInvalidImports() throws JToscaException {
		List<String> fileNames = new ArrayList<>();
		fileNames.add("csars/tmpCSAR_Huawei_vSPGW_fixed.csar");
		fileNames.add("csars/sdc-onboarding_csar.csar");
		fileNames.add("csars/resource-Spgw-csar-ZTE.csar");

		for (String fileName : fileNames) {
			String fileStr = JToscaImportTest.class.getClassLoader().getResource(fileName).getFile();
			File file = new File(fileStr);
			new ToscaTemplate(file.getAbsolutePath(), null, true, null);
			List<String> invalidImportErrors = ThreadLocalsHolder.getCollector().getValidationIssueReport().stream()
					.filter(s -> s.contains("JE195")).collect(Collectors.toList());
			assertEquals(0, invalidImportErrors.size());
		}
	}
	
	@Test
	public void testParseAnnotations() throws JToscaException {

		String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
		File file = new File(fileStr);
		ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
		
		List<Input> inputs = toscaTemplate.getInputs();
		assertNotNull(inputs);
		assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
		
		inputs.forEach(Input::parseAnnotations);
		assertTrue(!inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
	}

	@Test
	public void testGetInputsWithAndWithoutAnnotations() throws JToscaException {

		String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
		File file = new File(fileStr);
		ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
		List<Input> inputs = toscaTemplate.getInputs();
		assertNotNull(inputs);
		assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());

		inputs = toscaTemplate.getInputs(true);
		assertNotNull(inputs);
		validateInputsAnnotations(inputs);

		inputs = toscaTemplate.getInputs(false);
		assertNotNull(inputs);
		assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
	}

    @Test
    public void testGetPropertyNameTest() throws JToscaException {

        String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
        File file = new File(fileStr);
        ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
        NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(0);

        ArrayList<String> valueList = (ArrayList<String>)nodeTemplate.getPropertyValueFromTemplatesByName("vmxvpfe_sriov41_0_port_vlanfilter");
        assertEquals(4, valueList.size());

        assertEquals("vPE", (String) nodeTemplate.getPropertyValueFromTemplatesByName("nf_role"));

        assertNull(nodeTemplate.getPropertyValueFromTemplatesByName("test"));
    }

    private void validateInputsAnnotations(List<Input> inputs) {
		List<Input> inputsWithAnnotations = inputs.stream().filter(i -> i.getAnnotations() != null)
				.collect(Collectors.toList());
		assertTrue(!inputs.isEmpty());
		inputsWithAnnotations.stream().forEach(i -> validateAnnotations(i));
	}

	private void validateAnnotations(Input input) {
		assertNotNull(input.getAnnotations());
		assertEquals(input.getAnnotations().size(), 1);
		Annotation annotation = input.getAnnotations().get("source");
		assertEquals(annotation.getName(), "source");
		assertEquals(annotation.getType().toLowerCase(), "org.openecomp.annotations.source");
		assertNotNull(annotation.getProperties());
		Optional<Property> source_type = annotation.getProperties().stream()
				.filter(p -> p.getName().equals("source_type")).findFirst();
		assertTrue(source_type.isPresent());
		assertEquals(source_type.get().getValue(), "HEAT");
	}

}