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.processor;
\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.ArrayList;
\r
24 import java.util.Arrays;
\r
25 import java.util.HashMap;
\r
26 import java.util.List;
\r
27 import java.util.Map;
\r
28 import org.apache.commons.io.FileUtils;
\r
29 import org.junit.Assert;
\r
30 import org.junit.Before;
\r
31 import org.junit.Test;
\r
32 import org.junit.runner.RunWith;
\r
33 import org.mockito.Matchers;
\r
34 import org.mockito.Mock;
\r
35 import org.mockito.Mockito;
\r
36 import org.mockito.MockitoAnnotations;
\r
37 import org.mockito.invocation.InvocationOnMock;
\r
38 import org.mockito.runners.MockitoJUnitRunner;
\r
39 import org.mockito.stubbing.Answer;
\r
40 import org.onap.ccsdk.config.assignment.service.ConfigResourceAssignmentTestUtils;
\r
41 import org.onap.ccsdk.config.data.adaptor.domain.TransactionLog;
\r
42 import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService;
\r
43 import org.onap.ccsdk.config.model.ConfigModelConstant;
\r
44 import org.onap.ccsdk.config.model.data.ResourceAssignment;
\r
45 import org.onap.ccsdk.config.model.data.dict.ResourceDefinition;
\r
46 import org.onap.ccsdk.config.model.utils.TransformationUtils;
\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 DBResourceProcessorTest {
\r
55 private static EELFLogger logger = EELFManager.getInstance().getLogger(DBResourceProcessorTest.class);
\r
58 private ConfigResourceService configResourceService;
\r
60 @SuppressWarnings("unchecked")
\r
62 public void before() {
\r
63 MockitoAnnotations.initMocks(this);
\r
66 Mockito.doAnswer(new Answer<Void>() {
\r
68 public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
\r
69 Object[] args = invocationOnMock.getArguments();
\r
71 logger.trace("Transaction info " + Arrays.asList(args));
\r
75 }).when(configResourceService).save(any(TransactionLog.class));
\r
77 } catch (SvcLogicException e) {
\r
78 e.printStackTrace();
\r
83 public void testDbSimpleProcess() throws Exception {
\r
84 logger.info(" ******************************* testDbSimpleProcess ***************************");
\r
86 Mockito.doAnswer(new Answer<List<Map<String, Object>>>() {
\r
88 public List<Map<String, Object>> answer(InvocationOnMock invocationOnMock) throws Throwable {
\r
89 Object[] args = invocationOnMock.getArguments();
\r
90 List<Map<String, Object>> results = new ArrayList<>();
\r
92 logger.info("Query " + Arrays.asList(args));
\r
93 Map<String, Object> record = new HashMap<>();
\r
94 record.put("country", "US");
\r
95 results.add(record);
\r
99 }).when(configResourceService).query(Matchers.anyString(), Matchers.<Map<String, Object>>any());
\r
101 String recipeName = "sample-recipe";
\r
103 String resourceassignmentContent = FileUtils.readFileToString(
\r
104 new File("src/test/resources/mapping/db/resource-assignments-simple.json"), Charset.defaultCharset());
\r
105 List<ResourceAssignment> batchResourceAssignment =
\r
106 TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);
\r
108 String dictionaryContent = FileUtils.readFileToString(new File("src/test/resources/mapping/db/db-simple.json"),
\r
109 Charset.defaultCharset());
\r
110 Map<String, ResourceDefinition> dictionaries =
\r
111 ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);
\r
112 DBResourceProcessor dbResourceProcessor = new DBResourceProcessor(configResourceService);
\r
113 Map<String, Object> componentContext = new HashMap<>();
\r
114 componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);
\r
115 componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);
\r
116 componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");
\r
117 componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);
\r
119 componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");
\r
121 Map<String, String> inParams = new HashMap<>();
\r
122 SvcLogicContext ctx = new SvcLogicContext();
\r
123 dbResourceProcessor.process(inParams, ctx, componentContext);
\r
125 logger.info(" Context " + componentContext);
\r
130 public void testDbComplexProcess() throws Exception {
\r
131 logger.info(" ******************************* testDbComplexProcess ***************************");
\r
133 Mockito.doAnswer(new Answer<List<Map<String, Object>>>() {
\r
135 public List<Map<String, Object>> answer(InvocationOnMock invocationOnMock) throws Throwable {
\r
136 Object[] args = invocationOnMock.getArguments();
\r
137 List<Map<String, Object>> results = new ArrayList<>();
\r
138 if (args != null) {
\r
139 logger.info("Query " + Arrays.asList(args));
\r
140 Map<String, Object> record = new HashMap<>();
\r
141 record.put("db-country", "US");
\r
142 record.put("db-state", "NJ");
\r
143 results.add(record);
\r
147 }).when(configResourceService).query(Matchers.anyString(), Matchers.<Map<String, Object>>any());
\r
149 String recipeName = "sample-recipe";
\r
151 String resourceassignmentContent = FileUtils.readFileToString(
\r
152 new File("src/test/resources/mapping/db/resource-assignments-complex.json"), Charset.defaultCharset());
\r
153 List<ResourceAssignment> batchResourceAssignment =
\r
154 TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);
\r
156 String dictionaryContent = FileUtils.readFileToString(new File("src/test/resources/mapping/db/db-complex.json"),
\r
157 Charset.defaultCharset());
\r
158 Map<String, ResourceDefinition> dictionaries =
\r
159 ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);
\r
160 DBResourceProcessor dbResourceProcessor = new DBResourceProcessor(configResourceService);
\r
161 Map<String, Object> componentContext = new HashMap<>();
\r
162 componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);
\r
163 componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);
\r
164 componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");
\r
165 componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);
\r
166 componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");
\r
168 Map<String, String> inParams = new HashMap<>();
\r
169 SvcLogicContext ctx = new SvcLogicContext();
\r
170 String datatypeContent = FileUtils.readFileToString(new File("src/test/resources/mapping/db/dt-location.json"),
\r
171 Charset.defaultCharset());
\r
172 ctx.setAttribute("data_types.dt-location", datatypeContent);
\r
173 dbResourceProcessor.process(inParams, ctx, componentContext);
\r
178 public void testDbArrayComplexProcess() throws Exception {
\r
179 logger.info(" ******************************* testDbArrayComplexProcess ***************************");
\r
180 Mockito.doAnswer(new Answer<List<Map<String, Object>>>() {
\r
182 public List<Map<String, Object>> answer(InvocationOnMock invocationOnMock) throws Throwable {
\r
183 Object[] args = invocationOnMock.getArguments();
\r
184 List<Map<String, Object>> results = new ArrayList<>();
\r
185 if (args != null) {
\r
186 logger.info("Query " + Arrays.asList(args));
\r
187 Map<String, Object> record = new HashMap<>();
\r
188 record.put("db-country", "US");
\r
189 record.put("db-state", "NJ");
\r
190 results.add(record);
\r
192 Map<String, Object> record2 = new HashMap<>();
\r
193 record2.put("db-country", "INDIA");
\r
194 record2.put("db-state", "TN");
\r
195 results.add(record2);
\r
199 }).when(configResourceService).query(Matchers.anyString(), Matchers.<Map<String, Object>>any());
\r
201 String recipeName = "sample-recipe";
\r
203 String resourceassignmentContent = FileUtils.readFileToString(
\r
204 new File("src/test/resources/mapping/db/resource-assignments-array.json"), Charset.defaultCharset());
\r
205 List<ResourceAssignment> batchResourceAssignment =
\r
206 TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);
\r
208 String dictionaryContent = FileUtils.readFileToString(new File("src/test/resources/mapping/db/db-array.json"),
\r
209 Charset.defaultCharset());
\r
210 Map<String, ResourceDefinition> dictionaries =
\r
211 ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);
\r
212 DBResourceProcessor dbResourceProcessor = new DBResourceProcessor(configResourceService);
\r
213 Map<String, Object> componentContext = new HashMap<>();
\r
214 componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);
\r
215 componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);
\r
216 componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");
\r
217 componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);
\r
218 componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");
\r
220 Map<String, String> inParams = new HashMap<>();
\r
221 SvcLogicContext ctx = new SvcLogicContext();
\r
222 String datatypeContent = FileUtils.readFileToString(new File("src/test/resources/mapping/db/dt-location.json"),
\r
223 Charset.defaultCharset());
\r
224 ctx.setAttribute("data_types.dt-location", datatypeContent);
\r
225 dbResourceProcessor.process(inParams, ctx, componentContext);
\r
226 logger.info("Component Context = ({})", componentContext);
\r
227 Assert.assertNotNull("faile dto populate Array Complex response ", componentContext);
\r