b9f2b838c28b998de3665b750b8bfe77b74d3c94
[sdc.git] / common / onap-tosca-datatype / src / test / java / org / onap / sdc / tosca / datatypes / model / NodeTemplateTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Modifications copyright (c) 2019 Nokia
17  */
18
19 package org.onap.sdc.tosca.datatypes.model;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
29
30 import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsExcluding;
31 import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanHashCodeExcluding;
32 import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding;
33 import static org.junit.Assert.assertThat;
34
35
36 public class NodeTemplateTest {
37
38     private static final String NODE_WITH_INTERFACE = "nodeWithInterface";
39     public static final String INTERFACE_KEY = "newInterface";
40     public static final String INPUT_KEY = "newInput";
41     public static final String INPUT_VAL = "myVal";
42     public static final String OPER_KEY = "oper1";
43     public static final String MY_WF_JSON = "myWf.json";
44     public static final String STANDARD_INTERFACE_KEY = "Standard";
45     public static final String CREATE_OPER = "create";
46     public static final String NORMALIZE_INTERFACE_DEFINITION = "/mock/nodeTemplate/normalizeInterfaceDefinition.yaml";
47     public static final String INTERFACE_DEFINITION_FOR_UPD_RESULT =
48             "/mock/nodeTemplate/interfaceDefinitionForUpdResult.yaml";
49     public static final String INTERFACE_DEFINITION_FOR_UPD = "/mock/nodeTemplate/interfaceDefinitionForUpd.yaml";
50
51     @Test
52     public void getNormalizeInterfacesTest() throws IOException {
53         ServiceTemplate serviceTemplateFromYaml =
54                 getServiceTemplate(NORMALIZE_INTERFACE_DEFINITION);
55         NodeTemplate nodeTemplate =
56                 serviceTemplateFromYaml.getTopology_template().getNode_templates().get(NODE_WITH_INTERFACE);
57         Map<String, InterfaceDefinitionTemplate> normalizeInterfaces = nodeTemplate.getNormalizeInterfaces();
58         chkData(normalizeInterfaces);
59
60     }
61
62     @Test
63     public void addInterfacesTest() throws IOException {
64         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
65         ServiceTemplate expectedServiceTemplateFromYaml = getServiceTemplate(INTERFACE_DEFINITION_FOR_UPD_RESULT);
66         ServiceTemplate serviceTemplateForUpdate = getServiceTemplate(INTERFACE_DEFINITION_FOR_UPD);
67         NodeTemplate nodeTemplate =
68                 serviceTemplateForUpdate.getTopology_template().getNode_templates().get(NODE_WITH_INTERFACE);
69         nodeTemplate.addInterface(INTERFACE_KEY, createInterfaceDefinitionTemplate());
70
71         String expectedServiceTemplate = toscaExtensionYamlUtil.objectToYaml(expectedServiceTemplateFromYaml);
72         String actualServiceTemplate = toscaExtensionYamlUtil.objectToYaml(serviceTemplateForUpdate);
73         Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate);
74     }
75
76     @Test
77     public void shouldHaveValidGettersAndSetters() {
78         assertThat(NodeTemplate.class, hasValidGettersAndSettersExcluding("normalizeInterfaces"));
79     }
80
81     @Test
82     public void shouldHaveValidEquals() {
83         assertThat(NodeTemplate.class, hasValidBeanEqualsExcluding("normalizeInterfaces"));
84     }
85
86     @Test
87     public void shouldHaveValidHashCode() {
88         assertThat(NodeTemplate.class, hasValidBeanHashCodeExcluding("normalizeInterfaces"));
89     }
90
91     private InterfaceDefinitionTemplate createInterfaceDefinitionTemplate() {
92         InterfaceDefinitionTemplate interfaceDefinitionTemplate = new InterfaceDefinitionTemplate();
93         interfaceDefinitionTemplate.setInputs(new HashMap<>());
94         interfaceDefinitionTemplate.getInputs().put(INPUT_KEY, INPUT_VAL);
95         interfaceDefinitionTemplate.addOperation(OPER_KEY, createOperationDefinitionTemplate());
96         return interfaceDefinitionTemplate;
97     }
98
99     private OperationDefinitionTemplate createOperationDefinitionTemplate() {
100         OperationDefinitionTemplate operationDefinitionTemplate = new OperationDefinitionTemplate();
101         operationDefinitionTemplate.setImplementation(createImpl());
102         return operationDefinitionTemplate;
103
104     }
105
106     private Implementation createImpl() {
107         Implementation implementation = new Implementation();
108         implementation.setPrimary(MY_WF_JSON);
109         return implementation;
110     }
111
112     protected InterfaceDefinitionTemplate chkData(Map<String, InterfaceDefinitionTemplate> normalizeInterfaces) {
113         Assert.assertNotNull(normalizeInterfaces);
114         InterfaceDefinitionTemplate interfaceDefinitionTemplate = normalizeInterfaces.get(STANDARD_INTERFACE_KEY);
115         Assert.assertNotNull(interfaceDefinitionTemplate);
116         Assert.assertNotNull(interfaceDefinitionTemplate.getInputs());
117         Assert.assertEquals(1, interfaceDefinitionTemplate.getInputs().size());
118         Assert.assertNotNull(interfaceDefinitionTemplate.getOperations());
119         Assert.assertEquals(1, interfaceDefinitionTemplate.getOperations().size());
120         OperationDefinitionTemplate createOperation = interfaceDefinitionTemplate.getOperations().get(CREATE_OPER);
121         Assert.assertNotNull(createOperation);
122         Assert.assertNotNull(createOperation.getInputs());
123         return interfaceDefinitionTemplate;
124     }
125
126     protected ServiceTemplate getServiceTemplate(String inputPath) throws IOException {
127         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
128         try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(inputPath)) {
129             return toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
130         }
131     }
132
133 }