Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / test / java / org / onap / ccsdk / features / params / service / ConfigModelServiceTest.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.features.params.service;\r
19 \r
20 import static org.junit.Assert.fail;\r
21 import java.io.File;\r
22 import java.nio.charset.Charset;\r
23 import java.util.ArrayList;\r
24 import java.util.HashMap;\r
25 import java.util.List;\r
26 import java.util.Map;\r
27 import org.apache.commons.io.FileUtils;\r
28 import org.junit.Assert;\r
29 import org.junit.Test;\r
30 import org.junit.runner.RunWith;\r
31 import org.mockito.Matchers;\r
32 import org.mockito.Mock;\r
33 import org.mockito.Mockito;\r
34 import org.mockito.runners.MockitoJUnitRunner;\r
35 import org.onap.ccsdk.features.model.ConfigModelConstant;\r
36 import org.onap.ccsdk.features.model.data.ServiceTemplate;\r
37 import org.onap.ccsdk.features.model.domain.ConfigModel;\r
38 import org.onap.ccsdk.features.model.domain.ConfigModelContent;\r
39 import org.onap.ccsdk.features.model.service.ConfigModelService;\r
40 import org.onap.ccsdk.features.model.service.ConfigModelServiceImpl;\r
41 import org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorService;\r
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
44 import com.att.eelf.configuration.EELFLogger;\r
45 import com.att.eelf.configuration.EELFManager;\r
46 \r
47 @RunWith(MockitoJUnitRunner.class)\r
48 public class ConfigModelServiceTest {\r
49 \r
50     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigModelServiceTest.class);\r
51 \r
52     @Mock\r
53     private ConfigRestAdaptorService configRestAdaptorService;\r
54 \r
55     @Test\r
56     public void testConfigAssignmentInputOutputParams() throws Exception {\r
57 \r
58         String fileContent = FileUtils.readFileToString(\r
59                 new File("src/test/resources/service_templates/resource_assignment.json"), Charset.defaultCharset());\r
60 \r
61         Map<String, String> context = new HashMap<>();\r
62         ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);\r
63         context = configModelServiceImpl.convertServiceTemplate2Properties(fileContent, context);\r
64 \r
65         Assert.assertNotNull("Failed to Prepare Context : ", context);\r
66 \r
67         context.put("request-id", "12345");\r
68         context.put("vnf-id", "vnf12345");\r
69 \r
70         Map<String, String> inparams = new HashMap<String, String>();\r
71         inparams.put(ConfigModelConstant.PROPERTY_SELECTOR, "resource-assignment");\r
72 \r
73         SvcLogicContext inputContext = new SvcLogicContext();\r
74         context.forEach((name, value) -> {\r
75             inputContext.setAttribute(name, value);\r
76         });\r
77 \r
78         // TransformationUtils.printProperty(inputContext.toProperties());\r
79 \r
80         configModelServiceImpl.assignInParamsFromModel(inputContext, inparams);\r
81         Assert.assertNotNull("In Param is Null : ", inparams);\r
82         Assert.assertNotNull("Failed to get entity-id in Inparms : ", inparams.get("resource-id"));\r
83         Assert.assertEquals("Failed to get entity-id vlaue in Inparms ", String.valueOf("vnf12345"),\r
84                 inparams.get("resource-id"));\r
85         Assert.assertNotNull("Failed to get request-id in Inparms : ", inparams.get("request-id"));\r
86         Assert.assertEquals("Failed to get request-id vlaue in Inparms ", String.valueOf("12345"),\r
87                 inparams.get("request-id"));\r
88 \r
89         configModelServiceImpl.assignOutParamsFromModel(inputContext, inparams);\r
90         logger.info("*************** Output Params *************");\r
91         // TransformationUtils.printProperty(inputContext.toProperties());\r
92 \r
93     }\r
94 \r
95     @Test\r
96     public void testConvertServiceTemplate2PropertiesComplex() throws Exception {\r
97         String fileContent = FileUtils.readFileToString(\r
98                 new File("src/test/resources/service_templates/resource_assignment.json"), Charset.defaultCharset());\r
99 \r
100         Map<String, String> context = new HashMap<>();\r
101         context.put("host-password", "1234");\r
102         context.put("host-ip-address", "[123.23.34.45, 123.23.34.45]");\r
103 \r
104         ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);\r
105         configModelServiceImpl.convertServiceTemplate2Properties(fileContent, context);\r
106 \r
107         // TransformationUtils.printMap(context);\r
108 \r
109         Map<String, String> inparams = new HashMap<String, String>();\r
110         inparams.put(ConfigModelConstant.PROPERTY_SELECTOR, "resource-assignment");\r
111         logger.info("Before Input Result: " + inparams);\r
112 \r
113         SvcLogicContext inputContext = new SvcLogicContext();\r
114         context.forEach((name, value) -> {\r
115             inputContext.setAttribute(name, value);\r
116         });\r
117 \r
118         configModelServiceImpl.assignInParamsFromModel(inputContext, inparams);\r
119         logger.info("----------Input Result: " + inparams);\r
120 \r
121         inputContext.setAttribute("assignment-params", "default-assigned");\r
122         configModelServiceImpl.assignOutParamsFromModel(inputContext, inparams);\r
123 \r
124         // TransformationUtils.printProperty(inputContext.toProperties());\r
125 \r
126     }\r
127 \r
128     @Test\r
129     public void testGetNodeTemplateContent() throws Exception {\r
130         String templateContent = "{\"id\":\"id\"}";\r
131         SvcLogicContext context = new SvcLogicContext();\r
132         context.setAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + templateContent + ".content",\r
133                 templateContent);\r
134 \r
135         ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);\r
136         String content = configModelServiceImpl.getNodeTemplateContent(context, templateContent);\r
137 \r
138         Assert.assertEquals(content, templateContent);\r
139     }\r
140 \r
141     @Test\r
142     public void testGetNodeTemplateMapping() throws Exception {\r
143         String templateContent = "{\"capabilities\":{\"mapping\":{\"properties\":{\"mapping\":[\"test\"]}}}}";\r
144         SvcLogicContext context = new SvcLogicContext();\r
145         context.setAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + templateContent, templateContent);\r
146 \r
147         ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);\r
148         configModelServiceImpl.getNodeTemplateMapping(context, templateContent);\r
149         // Assert.assertEquals(content, templateContent);\r
150     }\r
151 \r
152     @Test\r
153     public void testValidateServiceTemplate() throws Exception {\r
154         ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);\r
155         ServiceTemplate serviceTemplate = new ServiceTemplate();\r
156 \r
157         try {\r
158             configModelServiceImpl.validateServiceTemplate(null);\r
159             fail("Should have thrown exception");\r
160         } catch (SvcLogicException e) {\r
161         }\r
162 \r
163         try {\r
164             configModelServiceImpl.validateServiceTemplate(serviceTemplate);\r
165             fail("Should have thrown exception");\r
166         } catch (SvcLogicException e) {\r
167         }\r
168 \r
169         Map<String, String> metadata = new HashMap<String, String>();\r
170         metadata.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_AUTHOR, "author");\r
171         metadata.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_NAME, "name");\r
172         metadata.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_VERSION, "version");\r
173         serviceTemplate.setMetadata(metadata);\r
174 \r
175         Assert.assertTrue(configModelServiceImpl.validateServiceTemplate(serviceTemplate));\r
176     }\r
177 \r
178     @Test\r
179     public void testPrepareContext() throws Exception {\r
180         Mockito.when(configRestAdaptorService.getResource(Matchers.anyString(), Matchers.anyString(), Matchers.any()))\r
181                 .thenReturn(createConfigModel());\r
182 \r
183         String input = "{\"action-name\": \"resource-assignment-action\"}";\r
184         ConfigModelService configModelService = new ConfigModelServiceImpl(configRestAdaptorService);\r
185 \r
186         Map<String, String> ctx =\r
187                 configModelService.prepareContext(null, input, "serviceTemplateName", "serviceTemplateVersion");\r
188         Assert.assertEquals("resource-assignment-action", ctx.get(ConfigModelConstant.PROPERTY_ACTION_NAME));\r
189 \r
190         ctx = configModelService.prepareContext(null, input, "{}");\r
191         Assert.assertEquals("resource-assignment-action", ctx.get(ConfigModelConstant.PROPERTY_ACTION_NAME));\r
192     }\r
193 \r
194     @Test\r
195     public void testConvertServiceTemplate2Properties() throws Exception {\r
196         Map<String, String> metadata = new HashMap<String, String>();\r
197         metadata.put("key", "value");\r
198         ServiceTemplate serviceTemplate = new ServiceTemplate();\r
199         serviceTemplate.setMetadata(metadata);\r
200         Map<String, String> context = new HashMap<String, String>();\r
201 \r
202         ConfigModelService configModelService = new ConfigModelServiceImpl(configRestAdaptorService);\r
203         Map<String, String> ctx = configModelService.convertServiceTemplate2Properties(serviceTemplate, context);\r
204 \r
205         Assert.assertEquals("value", ctx.get("key"));\r
206     }\r
207 \r
208     private ConfigModel createConfigModel() {\r
209         ConfigModel configModel = new ConfigModel();\r
210         List<ConfigModelContent> configModelContents = new ArrayList<ConfigModelContent>();\r
211         ConfigModelContent configModelContent = new ConfigModelContent();\r
212         configModelContent.setContentType(ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON);\r
213         configModelContent.setContent("{\"description\": \"description\"}");\r
214         configModelContents.add(configModelContent);\r
215         configModel.setConfigModelContents(configModelContents);\r
216         configModel.setPublished("Y");\r
217         return configModel;\r
218     }\r
219 }\r