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.features.params.service;
\r
20 import static org.mockito.Matchers.any;
\r
21 import java.io.File;
\r
22 import java.nio.charset.Charset;
\r
23 import java.util.Arrays;
\r
24 import java.util.HashMap;
\r
25 import java.util.Map;
\r
26 import org.apache.commons.io.FileUtils;
\r
27 import org.apache.commons.lang3.StringUtils;
\r
28 import org.apache.sling.testing.mock.osgi.MockOsgi;
\r
29 import org.junit.After;
\r
30 import org.junit.Before;
\r
31 import org.junit.Test;
\r
32 import org.junit.runner.RunWith;
\r
33 import org.mockito.Mock;
\r
34 import org.mockito.Mockito;
\r
35 import org.mockito.MockitoAnnotations;
\r
36 import org.mockito.invocation.InvocationOnMock;
\r
37 import org.mockito.runners.MockitoJUnitRunner;
\r
38 import org.mockito.stubbing.Answer;
\r
39 import org.onap.ccsdk.features.data.adaptor.domain.TransactionLog;
\r
40 import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService;
\r
41 import org.onap.ccsdk.features.model.ConfigModelConstant;
\r
42 import org.onap.ccsdk.features.model.service.ComponentNodeDelegate;
\r
43 import org.onap.ccsdk.features.model.service.ComponentNodeServiceImpl;
\r
44 import org.onap.ccsdk.features.model.service.ConfigModelService;
\r
45 import org.onap.ccsdk.features.model.service.ConfigModelServiceImpl;
\r
46 import org.onap.ccsdk.features.model.utils.TransformationUtils;
\r
47 import org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorService;
\r
48 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
\r
49 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
\r
50 import org.osgi.framework.BundleContext;
\r
51 import com.att.eelf.configuration.EELFLogger;
\r
52 import com.att.eelf.configuration.EELFManager;
\r
54 @RunWith(MockitoJUnitRunner.class)
\r
55 public class ComponentNodeTest {
\r
56 private static EELFLogger logger = EELFManager.getInstance().getLogger(ComponentNodeTest.class);
\r
58 private ConfigResourceService configResourceService;
\r
61 private ConfigRestAdaptorService configRestAdaptorService;
\r
63 BundleContext bundleContext = MockOsgi.newBundleContext();
\r
66 public void before() {
\r
67 MockitoAnnotations.initMocks(this);
\r
69 MockComponentNode mockSvcLogicPlugin = new MockComponentNode();
\r
70 bundleContext.registerService(MockComponentNode.class, mockSvcLogicPlugin, null);
\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
83 } catch (SvcLogicException e) {
\r
84 // TODO Auto-generated catch block
\r
85 e.printStackTrace();
\r
90 public void after() {
\r
95 public void testProcess() {
\r
98 String serviceTemplateContent = FileUtils.readFileToString(
\r
99 new File("src/test/resources/componentnode/default.json"), Charset.defaultCharset());
\r
100 ConfigModelService configModelService = new ConfigModelServiceImpl(configRestAdaptorService);
\r
102 Map<String, String> map = new HashMap<>();
\r
103 configModelService.convertServiceTemplate2Properties(serviceTemplateContent, map);
\r
105 SvcLogicContext ctx = new SvcLogicContext();
\r
106 map.forEach((name, value) -> {
\r
107 if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) {
\r
108 ctx.setAttribute(name, value);
\r
111 ctx.setAttribute("vnf-id", "1234");
\r
113 ComponentNodeServiceImpl componentNodeService =
\r
114 new ComponentNodeServiceImpl(bundleContext, configResourceService, configRestAdaptorService);
\r
116 ComponentNodeDelegate componentNodeDelegate = new ComponentNodeDelegate(componentNodeService);
\r
117 Map<String, String> inParams = new HashMap<>();
\r
118 inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "generate-configuration");
\r
120 componentNodeDelegate.process(inParams, ctx);
\r
121 TransformationUtils.printMap(inParams);
\r
123 } catch (Exception e) {
\r
124 e.printStackTrace();
\r
129 @Test(expected = SvcLogicException.class)
\r
130 public void testFailure() throws Exception {
\r
131 ComponentNodeServiceImpl componentNodeService =
\r
132 new ComponentNodeServiceImpl(bundleContext, configResourceService, configRestAdaptorService);
\r
133 ComponentNodeDelegate componentNodeDelegate = new ComponentNodeDelegate(componentNodeService);
\r
135 Map<String, String> inParams = new HashMap<String, String>();
\r
136 inParams.put(ConfigModelConstant.PROPERTY_SELECTOR, "generate-configuration");
\r
137 SvcLogicContext ctx = new SvcLogicContext();
\r
138 componentNodeDelegate.process(inParams, ctx);
\r