2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.config.params.service;
\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.config.model.ConfigModelConstant;
\r
36 import org.onap.ccsdk.config.model.data.ServiceTemplate;
\r
37 import org.onap.ccsdk.config.model.domain.ConfigModel;
\r
38 import org.onap.ccsdk.config.model.domain.ConfigModelContent;
\r
39 import org.onap.ccsdk.config.model.service.ConfigModelService;
\r
40 import org.onap.ccsdk.config.model.service.ConfigModelServiceImpl;
\r
41 import org.onap.ccsdk.config.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
47 @RunWith(MockitoJUnitRunner.class)
\r
48 public class ConfigModelServiceTest {
\r
50 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigModelServiceTest.class);
\r
53 private ConfigRestAdaptorService configRestAdaptorService;
\r
56 public void testConfigAssignmentInputOutputParams() throws Exception {
\r
58 String fileContent = FileUtils.readFileToString(
\r
59 new File("src/test/resources/service_templates/resource_assignment.json"), Charset.defaultCharset());
\r
61 Map<String, String> context = new HashMap<>();
\r
62 ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);
\r
63 context = configModelServiceImpl.convertServiceTemplate2Properties(fileContent, context);
\r
65 Assert.assertNotNull("Failed to Prepare Context : ", context);
\r
67 context.put("request-id", "12345");
\r
68 context.put("vnf-id", "vnf12345");
\r
70 Map<String, String> inparams = new HashMap<String, String>();
\r
71 inparams.put(ConfigModelConstant.PROPERTY_SELECTOR, "resource-assignment");
\r
73 SvcLogicContext inputContext = new SvcLogicContext();
\r
74 context.forEach((name, value) -> {
\r
75 inputContext.setAttribute(name, value);
\r
78 // TransformationUtils.printProperty(inputContext.toProperties());
\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
89 configModelServiceImpl.assignOutParamsFromModel(inputContext, inparams);
\r
90 logger.info("*************** Output Params *************");
\r
91 // TransformationUtils.printProperty(inputContext.toProperties());
\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
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
104 ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);
\r
105 configModelServiceImpl.convertServiceTemplate2Properties(fileContent, context);
\r
107 // TransformationUtils.printMap(context);
\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
113 SvcLogicContext inputContext = new SvcLogicContext();
\r
114 context.forEach((name, value) -> {
\r
115 inputContext.setAttribute(name, value);
\r
118 configModelServiceImpl.assignInParamsFromModel(inputContext, inparams);
\r
119 logger.info("----------Input Result: " + inparams);
\r
121 inputContext.setAttribute("assignment-params", "default-assigned");
\r
122 configModelServiceImpl.assignOutParamsFromModel(inputContext, inparams);
\r
124 // TransformationUtils.printProperty(inputContext.toProperties());
\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
135 ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);
\r
136 String content = configModelServiceImpl.getNodeTemplateContent(context, templateContent);
\r
138 Assert.assertEquals(content, templateContent);
\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
147 ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);
\r
148 configModelServiceImpl.getNodeTemplateMapping(context, templateContent);
\r
149 // Assert.assertEquals(content, templateContent);
\r
153 public void testValidateServiceTemplate() throws Exception {
\r
154 ConfigModelServiceImpl configModelServiceImpl = new ConfigModelServiceImpl(configRestAdaptorService);
\r
155 ServiceTemplate serviceTemplate = new ServiceTemplate();
\r
158 configModelServiceImpl.validateServiceTemplate(null);
\r
159 fail("Should have thrown exception");
\r
160 } catch (SvcLogicException e) {
\r
164 configModelServiceImpl.validateServiceTemplate(serviceTemplate);
\r
165 fail("Should have thrown exception");
\r
166 } catch (SvcLogicException e) {
\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
175 Assert.assertTrue(configModelServiceImpl.validateServiceTemplate(serviceTemplate));
\r
179 public void testPrepareContext() throws Exception {
\r
180 Mockito.when(configRestAdaptorService.getResource(Matchers.anyString(), Matchers.anyString(), Matchers.any()))
\r
181 .thenReturn(createConfigModel());
\r
183 String input = "{\"action-name\": \"resource-assignment-action\"}";
\r
184 ConfigModelService configModelService = new ConfigModelServiceImpl(configRestAdaptorService);
\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
190 ctx = configModelService.prepareContext(null, input, "{}");
\r
191 Assert.assertEquals("resource-assignment-action", ctx.get(ConfigModelConstant.PROPERTY_ACTION_NAME));
\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
202 ConfigModelService configModelService = new ConfigModelServiceImpl(configRestAdaptorService);
\r
203 Map<String, String> ctx = configModelService.convertServiceTemplate2Properties(serviceTemplate, context);
\r
205 Assert.assertEquals("value", ctx.get("key"));
\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