fix null pointer
[sdc/sdc-tosca.git] / src / test / java / org / onap / sdc / toscaparser / api / JToscaImportTest.java
1 package org.onap.sdc.toscaparser.api;
2
3 import org.junit.Test;
4 import org.onap.sdc.toscaparser.api.common.JToscaException;
5 import org.onap.sdc.toscaparser.api.parameters.Annotation;
6 import org.onap.sdc.toscaparser.api.parameters.Input;
7 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
8
9 import java.io.File;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Optional;
13 import java.util.stream.Collectors;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertNull;
18 import static org.junit.Assert.assertTrue;
19
20 public class JToscaImportTest {
21
22         @Test
23         public void testNoMissingTypeValidationError() throws JToscaException {
24                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/sdc-onboarding_csar.csar")
25                                 .getFile();
26                 File file = new File(fileStr);
27                 new ToscaTemplate(file.getAbsolutePath(), null, true, null);
28                 List<String> missingTypeErrors = ThreadLocalsHolder.getCollector().getValidationIssueReport().stream()
29                                 .filter(s -> s.contains("JE136")).collect(Collectors.toList());
30                 assertEquals(0, missingTypeErrors.size());
31         }
32
33         @Test
34         public void testNoStackOverFlowError() {
35                 Exception jte = null;
36                 try {
37                         String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/sdc-onboarding_csar.csar")
38                                         .getFile();
39                         File file = new File(fileStr);
40                         new ToscaTemplate(file.getAbsolutePath(), null, true, null);
41                 } catch (Exception e) {
42                         jte = e;
43                 }
44                 assertEquals(null, jte);
45         }
46
47         @Test
48         public void testNoInvalidImports() throws JToscaException {
49                 List<String> fileNames = new ArrayList<>();
50                 fileNames.add("csars/tmpCSAR_Huawei_vSPGW_fixed.csar");
51                 fileNames.add("csars/sdc-onboarding_csar.csar");
52                 fileNames.add("csars/resource-Spgw-csar-ZTE.csar");
53
54                 for (String fileName : fileNames) {
55                         String fileStr = JToscaImportTest.class.getClassLoader().getResource(fileName).getFile();
56                         File file = new File(fileStr);
57                         new ToscaTemplate(file.getAbsolutePath(), null, true, null);
58                         List<String> invalidImportErrors = ThreadLocalsHolder.getCollector().getValidationIssueReport().stream()
59                                         .filter(s -> s.contains("JE195")).collect(Collectors.toList());
60                         assertEquals(0, invalidImportErrors.size());
61                 }
62         }
63         
64         @Test
65         public void testParseAnnotations() throws JToscaException {
66
67                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
68                 File file = new File(fileStr);
69                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
70                 
71                 List<Input> inputs = toscaTemplate.getInputs();
72                 assertNotNull(inputs);
73                 assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
74                 
75                 inputs.forEach(Input::parseAnnotations);
76                 assertTrue(!inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
77         }
78
79         @Test
80         public void testGetInputsWithAndWithoutAnnotations() throws JToscaException {
81
82                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
83                 File file = new File(fileStr);
84                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
85                 List<Input> inputs = toscaTemplate.getInputs();
86                 assertNotNull(inputs);
87                 assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
88
89                 inputs = toscaTemplate.getInputs(true);
90                 assertNotNull(inputs);
91                 validateInputsAnnotations(inputs);
92
93                 inputs = toscaTemplate.getInputs(false);
94                 assertNotNull(inputs);
95                 assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty());
96         }
97
98     @Test
99     public void testGetPropertyNameTest() throws JToscaException {
100
101         String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile();
102         File file = new File(fileStr);
103         ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
104         NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(0);
105
106         ArrayList<String> valueList = (ArrayList<String>)nodeTemplate.getPropertyValueFromTemplatesByName("vmxvpfe_sriov41_0_port_vlanfilter");
107         assertEquals(4, valueList.size());
108
109         assertEquals("vPE", (String) nodeTemplate.getPropertyValueFromTemplatesByName("nf_role"));
110
111         assertNull(nodeTemplate.getPropertyValueFromTemplatesByName("test"));
112     }
113
114         @Test
115         public void testNullValueHasNoNullPointerException() throws JToscaException {
116
117                 String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-JennyVtsbcKarunaSvc-csar.csar").getFile();
118                 File file = new File(fileStr);
119                 ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null);
120                 List<Input> inputs = toscaTemplate.getInputs();
121                 assertNotNull(inputs);
122         }
123
124     private void validateInputsAnnotations(List<Input> inputs) {
125                 List<Input> inputsWithAnnotations = inputs.stream().filter(i -> i.getAnnotations() != null)
126                                 .collect(Collectors.toList());
127                 assertTrue(!inputs.isEmpty());
128                 inputsWithAnnotations.stream().forEach(i -> validateAnnotations(i));
129         }
130
131         private void validateAnnotations(Input input) {
132                 assertNotNull(input.getAnnotations());
133                 assertEquals(input.getAnnotations().size(), 1);
134                 Annotation annotation = input.getAnnotations().get("source");
135                 assertEquals(annotation.getName(), "source");
136                 assertEquals(annotation.getType().toLowerCase(), "org.openecomp.annotations.source");
137                 assertNotNull(annotation.getProperties());
138                 Optional<Property> source_type = annotation.getProperties().stream()
139                                 .filter(p -> p.getName().equals("source_type")).findFirst();
140                 assertTrue(source_type.isPresent());
141                 assertEquals(source_type.get().getValue(), "HEAT");
142         }
143
144 }