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.generator.service;
\r
20 import static org.mockito.Matchers.any;
\r
21 import java.nio.charset.Charset;
\r
22 import java.util.ArrayList;
\r
23 import java.util.Arrays;
\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.IOUtils;
\r
28 import org.junit.Assert;
\r
29 import org.junit.Before;
\r
30 import org.junit.Test;
\r
31 import org.junit.runner.RunWith;
\r
32 import org.mockito.Mock;
\r
33 import org.mockito.Mockito;
\r
34 import org.mockito.MockitoAnnotations;
\r
35 import org.mockito.invocation.InvocationOnMock;
\r
36 import org.mockito.runners.MockitoJUnitRunner;
\r
37 import org.mockito.stubbing.Answer;
\r
38 import org.onap.ccsdk.config.data.adaptor.domain.ConfigResource;
\r
39 import org.onap.ccsdk.config.data.adaptor.domain.TransactionLog;
\r
40 import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;
\r
41 import org.onap.ccsdk.config.generator.ConfigGeneratorConstant;
\r
42 import org.onap.ccsdk.config.model.ConfigModelConstant;
\r
43 import org.onap.ccsdk.config.model.service.ConfigModelService;
\r
44 import org.onap.ccsdk.config.model.service.ConfigModelServiceImpl;
\r
45 import org.onap.ccsdk.config.model.utils.TransformationUtils;
\r
46 import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService;
\r
47 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
\r
48 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
\r
49 import com.att.eelf.configuration.EELFLogger;
\r
50 import com.att.eelf.configuration.EELFManager;
\r
52 @RunWith(MockitoJUnitRunner.class)
\r
53 public class ConfigGeneratorNodeTest {
\r
55 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigGeneratorNodeTest.class);
\r
58 private ConfigResourceService configResourceService;
\r
61 private ConfigRestAdaptorService configRestAdaptorService;
\r
63 private ConfigModelService configModelService;
\r
66 public void setUp() throws Exception {
\r
68 configModelService = new ConfigModelServiceImpl(configRestAdaptorService);
\r
70 MockitoAnnotations.initMocks(this);
\r
73 Mockito.doAnswer(new Answer<Void>() {
\r
75 public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
\r
76 Object[] args = invocationOnMock.getArguments();
\r
78 logger.trace("Transaction info " + Arrays.asList(args));
\r
82 }).when(configResourceService).save(any(TransactionLog.class));
\r
84 Mockito.doAnswer(new Answer<List<ConfigResource>>() {
\r
86 public List<ConfigResource> answer(InvocationOnMock invocationOnMock) throws Throwable {
\r
87 List<ConfigResource> configResources = new ArrayList<>();
\r
88 Object[] args = invocationOnMock.getArguments();
\r
90 logger.trace("Transaction info " + Arrays.asList(args));
\r
91 String resourceData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader()
\r
92 .getResourceAsStream("service_templates/configdata.json"), Charset.defaultCharset());
\r
93 ConfigResource configResource = (ConfigResource) args[0];
\r
94 configResource.setRecipeName("Sample-recipe");
\r
95 configResource.setResourceData(resourceData);
\r
96 configResources.add(configResource);
\r
98 return configResources;
\r
100 }).when(configResourceService).getConfigResource(any(ConfigResource.class));
\r
102 } catch (SvcLogicException e) {
\r
103 e.printStackTrace();
\r
108 public void testInputTemplateContentNData() throws Exception {
\r
110 ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService);
\r
112 Map<String, String> inParams = new HashMap<String, String>();
\r
113 inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "test");
\r
115 String jsonData = IOUtils.toString(
\r
116 ConfigGeneratorNodeTest.class.getClassLoader().getResourceAsStream("service_templates/configdata.json"),
\r
117 Charset.defaultCharset());
\r
118 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, jsonData);
\r
120 String templateData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader()
\r
121 .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset());
\r
122 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT, templateData);
\r
124 SvcLogicContext ctx = new SvcLogicContext();
\r
125 Map<String, Object> componentContext = new HashMap<>();
\r
126 configGeneratorNode.process(inParams, ctx, componentContext);
\r
127 Assert.assertEquals("Failed to generate Configuration Status as Failure",
\r
128 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
\r
129 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
\r
130 Assert.assertNotNull("Failed to generate Configuration",
\r
131 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG));
\r
133 logger.trace("Generated Configuration:\n "
\r
134 + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG));
\r
135 logger.trace("Generated Configuration:\n "
\r
136 + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_MASK_INFO));
\r
141 public void testInputTemplateWithNullData() throws Exception {
\r
143 ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService);
\r
145 Map<String, String> inParams = new HashMap<String, String>();
\r
146 inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "test");
\r
148 String jsonData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader()
\r
149 .getResourceAsStream("service_templates/configdata_with_null.json"), Charset.defaultCharset());
\r
150 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA, jsonData);
\r
152 String templateData = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader()
\r
153 .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset());
\r
154 inParams.put(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_CONTENT, templateData);
\r
156 SvcLogicContext ctx = new SvcLogicContext();
\r
157 Map<String, Object> componentContext = new HashMap<>();
\r
158 configGeneratorNode.process(inParams, ctx, componentContext);
\r
159 Assert.assertEquals("Failed to generate Configuration Status as Failure",
\r
160 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
\r
161 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
\r
162 Assert.assertNotNull("Failed to generate Configuration",
\r
163 ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG));
\r
165 logger.trace("Generated Configuration:\n "
\r
166 + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG));
\r
167 logger.trace("Generated Configuration:\n "
\r
168 + ctx.getAttribute("test." + ConfigGeneratorConstant.OUTPUT_PARAM_MASK_INFO));
\r
173 public void testDBTemplateContentNData() throws Exception {
\r
175 String fileContent = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader()
\r
176 .getResourceAsStream("service_templates/generate_configuration.json"), Charset.defaultCharset());
\r
178 String baseConfigTemplateContent = IOUtils.toString(ConfigGeneratorNodeTest.class.getClassLoader()
\r
179 .getResourceAsStream("service_templates/velocity/base-config-template.vtl"), Charset.defaultCharset());
\r
181 Map<String, String> context = new HashMap<>();
\r
182 context = configModelService.convertServiceTemplate2Properties(fileContent, context);
\r
184 context.put("node_templates.base-config-template.content", baseConfigTemplateContent);
\r
186 Assert.assertNotNull("Failed to Prepare Context : ", context);
\r
188 context.put("request-id", "12345");
\r
189 context.put("vnf-id", "vnf12345");
\r
190 context.put("action-name", "config-generator-action");
\r
192 Map<String, String> inparams = new HashMap<String, String>();
\r
193 inparams.put(ConfigModelConstant.PROPERTY_SELECTOR, "generate-configuration");
\r
195 SvcLogicContext inputContext = new SvcLogicContext();
\r
196 context.forEach((name, value) -> {
\r
197 inputContext.setAttribute(name, value);
\r
200 TransformationUtils.printMap(context);
\r
201 configModelService.assignInParamsFromModel(inputContext, inparams);
\r
202 ConfigGeneratorNode configGeneratorNode = new ConfigGeneratorNode(configResourceService, configModelService);
\r
204 Map<String, Object> componentContext = new HashMap<>();
\r
205 configGeneratorNode.process(inparams, inputContext, componentContext);
\r
207 Assert.assertEquals("Failed to generate Configuration Status as Failure",
\r
208 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS,
\r
209 inputContext.getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS));
\r
210 Assert.assertNotNull("Failed to generate Configuration", inputContext
\r
211 .getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG));
\r
213 logger.trace("Generated Configuration:\n " + inputContext
\r
214 .getAttribute("generate-configuration." + ConfigGeneratorConstant.OUTPUT_PARAM_GENERATED_CONFIG));
\r
218 public void testTemplateContentNDataForMask() throws Exception {
\r