Added oparent to sdc main
[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
17 package org.onap.sdc.tosca.datatypes.model;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
27
28
29 public class NodeTemplateTest {
30
31     private static final String NODE_WITH_INTERFACE = "nodeWithInterface";
32     public static final String INTERFACE_KEY = "newInterface";
33     public static final String INPUT_KEY = "newInput";
34     public static final String INPUT_VAL = "myVal";
35     public static final String OPER_KEY = "oper1";
36     public static final String MY_WF_JSON = "myWf.json";
37     public static final String STANDARD_INTERFACE_KEY = "Standard";
38     public static final String CREATE_OPER = "create";
39     public static final String NORMALIZE_INTERFACE_DEFINITION = "/mock/nodeTemplate/normalizeInterfaceDefinition.yaml";
40     public static final String INTERFACE_DEFINITION_FOR_UPD_RESULT =
41             "/mock/nodeTemplate/interfaceDefinitionForUpdResult.yaml";
42     public static final String INTERFACE_DEFINITION_FOR_UPD = "/mock/nodeTemplate/interfaceDefinitionForUpd.yaml";
43
44     @Test
45     public void getNormalizeInterfacesTest() throws IOException {
46         ServiceTemplate serviceTemplateFromYaml =
47                 getServiceTemplate(NORMALIZE_INTERFACE_DEFINITION);
48         NodeTemplate nodeTemplate =
49                 serviceTemplateFromYaml.getTopology_template().getNode_templates().get(NODE_WITH_INTERFACE);
50         Map<String, InterfaceDefinitionTemplate> normalizeInterfaces = nodeTemplate.getNormalizeInterfaces();
51         chkData(normalizeInterfaces);
52
53     }
54
55     @Test
56     public void addInterfacesTest() throws IOException {
57         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
58         ServiceTemplate expectedServiceTemplateFromYaml = getServiceTemplate(INTERFACE_DEFINITION_FOR_UPD_RESULT);
59         ServiceTemplate serviceTemplateForUpdate = getServiceTemplate(INTERFACE_DEFINITION_FOR_UPD);
60         NodeTemplate nodeTemplate =
61                 serviceTemplateForUpdate.getTopology_template().getNode_templates().get(NODE_WITH_INTERFACE);
62         nodeTemplate.addInterface(INTERFACE_KEY, createInterfaceDefinitionTemplate());
63
64         String expectedServiceTemplate = toscaExtensionYamlUtil.objectToYaml(expectedServiceTemplateFromYaml);
65         String actualServiceTemplate = toscaExtensionYamlUtil.objectToYaml(serviceTemplateForUpdate);
66         Assert.assertEquals(expectedServiceTemplate, actualServiceTemplate);
67     }
68
69
70     private InterfaceDefinitionTemplate createInterfaceDefinitionTemplate() {
71         InterfaceDefinitionTemplate interfaceDefinitionTemplate = new InterfaceDefinitionTemplate();
72         interfaceDefinitionTemplate.setInputs(new HashMap<>());
73         interfaceDefinitionTemplate.getInputs().put(INPUT_KEY, INPUT_VAL);
74         interfaceDefinitionTemplate.addOperation(OPER_KEY, createOperationDefinitionTemplate());
75         return interfaceDefinitionTemplate;
76     }
77
78     private OperationDefinitionTemplate createOperationDefinitionTemplate() {
79         OperationDefinitionTemplate operationDefinitionTemplate = new OperationDefinitionTemplate();
80         operationDefinitionTemplate.setImplementation(createImpl());
81         return operationDefinitionTemplate;
82
83     }
84
85     private Implementation createImpl() {
86         Implementation implementation = new Implementation();
87         implementation.setPrimary(MY_WF_JSON);
88         return implementation;
89     }
90
91     protected InterfaceDefinitionTemplate chkData(Map<String, InterfaceDefinitionTemplate> normalizeInterfaces) {
92         Assert.assertNotNull(normalizeInterfaces);
93         InterfaceDefinitionTemplate interfaceDefinitionTemplate = normalizeInterfaces.get(STANDARD_INTERFACE_KEY);
94         Assert.assertNotNull(interfaceDefinitionTemplate);
95         Assert.assertNotNull(interfaceDefinitionTemplate.getInputs());
96         Assert.assertEquals(1, interfaceDefinitionTemplate.getInputs().size());
97         Assert.assertNotNull(interfaceDefinitionTemplate.getOperations());
98         Assert.assertEquals(1, interfaceDefinitionTemplate.getOperations().size());
99         OperationDefinitionTemplate createOperation = interfaceDefinitionTemplate.getOperations().get(CREATE_OPER);
100         Assert.assertNotNull(createOperation);
101         Assert.assertNotNull(createOperation.getInputs());
102         return interfaceDefinitionTemplate;
103     }
104
105     protected ServiceTemplate getServiceTemplate(String inputPath) throws IOException {
106         ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
107         try (InputStream yamlFile = toscaExtensionYamlUtil.loadYamlFileIs(inputPath)) {
108             return toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
109         }
110     }
111
112 }