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.assignment.service;
\r
20 import java.io.File;
\r
21 import java.nio.charset.Charset;
\r
22 import java.util.HashMap;
\r
23 import java.util.Map;
\r
24 import org.apache.commons.io.FileUtils;
\r
25 import org.apache.commons.lang3.StringUtils;
\r
26 import org.apache.sling.testing.mock.osgi.MockOsgi;
\r
27 import org.junit.Assert;
\r
28 import org.junit.Before;
\r
29 import org.junit.Rule;
\r
30 import org.junit.Test;
\r
31 import org.junit.rules.ExpectedException;
\r
32 import org.junit.runner.RunWith;
\r
33 import org.mockito.Mock;
\r
34 import org.mockito.MockitoAnnotations;
\r
35 import org.mockito.runners.MockitoJUnitRunner;
\r
36 import org.onap.ccsdk.config.assignment.ConfigAssignmentConstants;
\r
37 import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;
\r
38 import org.onap.ccsdk.config.generator.service.ConfigGeneratorService;
\r
39 import org.onap.ccsdk.config.generator.service.ConfigGeneratorServiceImpl;
\r
40 import org.onap.ccsdk.config.model.ConfigModelConstant;
\r
41 import org.onap.ccsdk.config.model.service.ComponentNodeDelegate;
\r
42 import org.onap.ccsdk.config.model.service.ComponentNodeService;
\r
43 import org.onap.ccsdk.config.model.service.ComponentNodeServiceImpl;
\r
44 import org.onap.ccsdk.config.model.service.ConfigModelService;
\r
45 import org.onap.ccsdk.config.model.service.ConfigModelServiceImpl;
\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.osgi.framework.BundleContext;
\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 ConfigAssignmentNodeTest {
\r
55 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentNodeTest.class);
\r
58 public ExpectedException thrown = ExpectedException.none();
\r
61 private ConfigResourceService configResourceService;
\r
64 private ConfigRestAdaptorService configRestAdaptorService;
\r
66 private ConfigModelService configModelService;
\r
68 private ComponentNodeService componentNodeService;
\r
70 private ConfigGeneratorService configGeneratorService;
\r
72 BundleContext bundleContext = MockOsgi.newBundleContext();
\r
74 @SuppressWarnings("unchecked")
\r
76 public void before() throws Exception {
\r
77 MockitoAnnotations.initMocks(this);
\r
79 * ConfigAssignmentNode configAssignmentNode = new ConfigAssignmentNode(componentNodeService,
\r
80 * configResourceService, configModelService, configRestAdaptorService, configGeneratorService);
\r
81 * bundleContext.registerService(ConfigAssignmentNode.class, configAssignmentNode, null);
\r
86 public void testResourceAssignment() throws Exception {
\r
87 ConfigResourceAssignmentTestUtils.injectTransactionLogSaveMock(configResourceService);
\r
89 ConfigResourceAssignmentTestUtils.injectConfigModelMock(configRestAdaptorService, "resource_assignment");
\r
91 ConfigResourceAssignmentTestUtils.injectResourceDictionaryMock(configRestAdaptorService,
\r
92 "assignments/empty-dictionary.json");
\r
94 ConfigResourceAssignmentTestUtils.injectConfigResourceSaveMock(configResourceService);
\r
96 componentNodeService =
\r
97 new ComponentNodeServiceImpl(bundleContext, configResourceService, configRestAdaptorService);
\r
98 configModelService = new ConfigModelServiceImpl(configRestAdaptorService);
\r
99 configGeneratorService = new ConfigGeneratorServiceImpl(configResourceService);
\r
101 ConfigAssignmentNode configAssignmentNode = new ConfigAssignmentNode(configResourceService,
\r
102 configRestAdaptorService, configModelService, componentNodeService, configGeneratorService);
\r
104 String inputContent = FileUtils.readFileToString(
\r
105 new File("src/test/resources/service_templates/input/input.json"), Charset.defaultCharset());
\r
107 Map<String, String> inParams = new HashMap<>();
\r
108 inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "test");
\r
109 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_REQUEST_ID, "1234");
\r
110 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_ID, "resourceid-1234");
\r
111 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_TYPE, "vnf-type");
\r
112 inParams.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_NAME, "vpe-201802-baseconfig");
\r
113 inParams.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_VERSION, "1.0.0");
\r
114 inParams.put(ConfigModelConstant.PROPERTY_ACTION_NAME, "resource-assignment-action");
\r
115 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_INPUT_DATA, inputContent);
\r
116 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_TEMPLATE_NAMES, "[\"base-config-template\"]");
\r
118 // Populate the SvcContext ( Simulation)
\r
119 SvcLogicContext svcLogicContext = new SvcLogicContext();
\r
120 Map<String, String> context = new HashMap<>();
\r
121 context.put(ConfigModelConstant.PROPERTY_ACTION_NAME, "resource-assignment-action");
\r
122 context = configModelService.prepareContext(context, inputContent, "vpe-201802-baseconfig", "1.0.0");
\r
123 context.forEach((key, value) -> svcLogicContext.setAttribute(key, value));
\r
125 Map<String, Object> componentContext = new HashMap<>();
\r
126 configAssignmentNode.process(inParams, svcLogicContext, componentContext);
\r
127 Assert.assertNotNull("Failed to get response status", svcLogicContext.getAttribute("test.status"));
\r
132 public void testSimplePreview() throws Exception {
\r
133 ConfigResourceAssignmentTestUtils.injectTransactionLogSaveMock(configResourceService);
\r
135 ConfigResourceAssignmentTestUtils.injectConfigModelMock(configRestAdaptorService, "resource_assignment");
\r
137 ConfigResourceAssignmentTestUtils.injectResourceDictionaryMock(configRestAdaptorService,
\r
138 "assignments/empty-dictionary.json");
\r
140 ConfigResourceAssignmentTestUtils.injectConfigResourceSaveMock(configResourceService);
\r
142 componentNodeService =
\r
143 new ComponentNodeServiceImpl(bundleContext, configResourceService, configRestAdaptorService);
\r
144 configModelService = new ConfigModelServiceImpl(configRestAdaptorService);
\r
145 configGeneratorService = new ConfigGeneratorServiceImpl(configResourceService);
\r
147 ConfigAssignmentNode configAssignmentNode = new ConfigAssignmentNode(configResourceService,
\r
148 configRestAdaptorService, configModelService, componentNodeService, configGeneratorService);
\r
150 String inputContent = FileUtils.readFileToString(
\r
151 new File("src/test/resources/service_templates/input/input.json"), Charset.defaultCharset());
\r
153 Map<String, String> inParams = new HashMap<>();
\r
154 inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "test");
\r
155 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_REQUEST_ID, "1234");
\r
156 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_ID, "resourceid-1234");
\r
157 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_TYPE, "vnf-type");
\r
158 inParams.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_NAME, "vpe-201802-baseconfig");
\r
159 inParams.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_VERSION, "1.0.0");
\r
160 inParams.put(ConfigModelConstant.PROPERTY_ACTION_NAME, "resource-assignment-action");
\r
161 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_INPUT_DATA, inputContent);
\r
162 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_TEMPLATE_NAMES, "[\"base-config-template\"]");
\r
163 SvcLogicContext ctx = new SvcLogicContext();
\r
164 configAssignmentNode.process(inParams, ctx);
\r
165 Assert.assertNotNull("Failed to get response mashed Content",
\r
166 ctx.getAttribute("test.mashed-data.base-config-template"));
\r
170 public void testComplexPreview() throws Exception {
\r
171 ConfigResourceAssignmentTestUtils.injectTransactionLogSaveMock(configResourceService);
\r
173 ConfigResourceAssignmentTestUtils.injectConfigModelMock(configRestAdaptorService, "vpe-201802-baseconfig");
\r
175 ConfigResourceAssignmentTestUtils.injectResourceDictionaryMock(configRestAdaptorService,
\r
176 "service_templates/vpe-201802-baseconfig/dict.json");
\r
178 ConfigResourceAssignmentTestUtils.injectConfigResourceSaveMock(configResourceService);
\r
180 componentNodeService =
\r
181 new ComponentNodeServiceImpl(bundleContext, configResourceService, configRestAdaptorService);
\r
182 configModelService = new ConfigModelServiceImpl(configRestAdaptorService);
\r
183 configGeneratorService = new ConfigGeneratorServiceImpl(configResourceService);
\r
185 ConfigAssignmentNode configAssignmentNode = new ConfigAssignmentNode(configResourceService,
\r
186 configRestAdaptorService, configModelService, componentNodeService, configGeneratorService);
\r
188 String inputContent = FileUtils.readFileToString(
\r
189 new File("src/test/resources/service_templates/vpe-201802-baseconfig/input-complex.json"),
\r
190 Charset.defaultCharset());
\r
192 Map<String, String> inParams = new HashMap<>();
\r
193 inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "complex-test");
\r
194 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_REQUEST_ID, "request-1234");
\r
195 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_ID, "resourceid-1234");
\r
196 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_TYPE, "vnf-type");
\r
197 inParams.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_NAME, "vpe-201802-baseconfig");
\r
198 inParams.put(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_VERSION, "1.0.0");
\r
199 inParams.put(ConfigModelConstant.PROPERTY_ACTION_NAME, "resource-assignment-action");
\r
200 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_INPUT_DATA, inputContent);
\r
201 inParams.put(ConfigAssignmentConstants.INPUT_PARAM_TEMPLATE_NAMES, "[\"base-config-template\"]");
\r
202 SvcLogicContext ctx = new SvcLogicContext();
\r
203 configAssignmentNode.process(inParams, ctx);
\r
204 Assert.assertNotNull("Failed to get response mashed Content",
\r
205 ctx.getAttribute("complex-test.mashed-data.base-config-template"));
\r
210 public void inputValidator() {
\r
211 SvcLogicContext ctx = new SvcLogicContext();
\r
214 logger.info(" ******************************* inputValidator ***************************");
\r
215 String serviceTemplateContent = FileUtils.readFileToString(
\r
216 new File("src/test/resources/service_templates/resource_assignment.json"),
\r
217 Charset.defaultCharset());
\r
219 String inputcontent = FileUtils.readFileToString(
\r
220 new File("src/test/resources/service_templates/input/inputValidateTest.json"),
\r
221 Charset.defaultCharset());
\r
223 Map<String, String> context = new HashMap<>();
\r
224 configModelService.prepareContext(context, inputcontent, serviceTemplateContent);
\r
226 // TransformationUtils.printMap(context);
\r
228 context.forEach((name, value) -> {
\r
229 if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) {
\r
230 ctx.setAttribute(name, value);
\r
234 ComponentNodeDelegate componentNodeDelegate = new ComponentNodeDelegate(componentNodeService);
\r
235 Map<String, String> inParams = new HashMap<>();
\r
236 inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "resource-assignment");
\r
237 componentNodeDelegate.process(inParams, ctx);
\r
239 } catch (Exception e) {
\r
240 logger.error("Failed in inputValidator" + e.getMessage());
\r
241 logger.info("** ctx.getAttribute Check for **" + ctx.getAttribute("resource-assignment.error-message"));
\r