Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / assignment-provider / src / test / java / org / onap / ccsdk / config / assignment / processor / InputResourceProcessorTest.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \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
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \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
16  */\r
17 \r
18 package org.onap.ccsdk.config.assignment.processor;\r
19 \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.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.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.assignment.service.ConfigResourceAssignmentTestUtils;\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.model.ConfigModelConstant;\r
42 import org.onap.ccsdk.config.model.data.ResourceAssignment;\r
43 import org.onap.ccsdk.config.model.data.dict.ResourceDefinition;\r
44 import org.onap.ccsdk.config.model.utils.TransformationUtils;\r
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
46 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
47 import com.att.eelf.configuration.EELFLogger;\r
48 import com.att.eelf.configuration.EELFManager;\r
49 \r
50 @RunWith(MockitoJUnitRunner.class)\r
51 public class InputResourceProcessorTest {\r
52 \r
53     private static EELFLogger logger = EELFManager.getInstance().getLogger(InputResourceProcessorTest.class);\r
54 \r
55     @Mock\r
56     private ConfigResourceService configResourceService;\r
57 \r
58     @SuppressWarnings("unchecked")\r
59     @Before\r
60     public void before() {\r
61         MockitoAnnotations.initMocks(this);\r
62 \r
63         try {\r
64             Mockito.doAnswer(new Answer<Void>() {\r
65                 @Override\r
66                 public Void answer(InvocationOnMock invocationOnMock) throws Throwable {\r
67                     Object[] args = invocationOnMock.getArguments();\r
68                     if (args != null) {\r
69                         logger.trace("Transaction info " + Arrays.asList(args));\r
70                     }\r
71                     return null;\r
72                 }\r
73             }).when(configResourceService).save(any(TransactionLog.class));\r
74 \r
75         } catch (SvcLogicException e) {\r
76             e.printStackTrace();\r
77         }\r
78     }\r
79 \r
80     @Test\r
81     public void testInputSimpleProcess() throws Exception {\r
82         logger.info(" *******************************  testInputSimpleProcess  ***************************");\r
83 \r
84         String recipeName = "sample-recipe";\r
85 \r
86         String resourceassignmentContent = FileUtils.readFileToString(\r
87                 new File("src/test/resources/mapping/input/resource-assignments-simple.json"),\r
88                 Charset.defaultCharset());\r
89         List<ResourceAssignment> batchResourceAssignment =\r
90                 TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);\r
91 \r
92         String dictionaryContent = FileUtils.readFileToString(\r
93                 new File("src/test/resources/mapping/default/default-simple.json"), Charset.defaultCharset());\r
94         Map<String, ResourceDefinition> dictionaries =\r
95                 ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);\r
96 \r
97         InputResourceProcessor inputResourceProcessor = new InputResourceProcessor(configResourceService);\r
98         Map<String, Object> componentContext = new HashMap<>();\r
99         componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);\r
100         componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);\r
101         componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");\r
102         componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);\r
103         componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");\r
104 \r
105         Map<String, String> inParams = new HashMap<>();\r
106         SvcLogicContext ctx = new SvcLogicContext();\r
107         ctx.setAttribute("country", "US");\r
108         ctx.setAttribute("port", "830");\r
109         ctx.setAttribute("voip-enabled", "true");\r
110 \r
111         inputResourceProcessor.process(inParams, ctx, componentContext);\r
112         logger.trace(" componentContext " + componentContext);\r
113 \r
114         Assert.assertEquals("Failed to populate default recipe country value ", "US",\r
115                 componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.country"));\r
116         Assert.assertEquals("Failed to populate default dictionary country value ", "US",\r
117                 componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.country"));\r
118 \r
119         Assert.assertEquals("Failed to populate default recipe port value ", 830,\r
120                 componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.port"));\r
121         Assert.assertEquals("Failed to populate default dictionary port value ", 830,\r
122                 componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.port"));\r
123 \r
124         Assert.assertEquals("Failed to populate default recipe voip-enabled value ", true,\r
125                 componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.voip-enabled"));\r
126         Assert.assertEquals("Failed to populate default dictionary voip-enabled value ", true,\r
127                 componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.voip-enabled"));\r
128 \r
129     }\r
130 \r
131     @Test\r
132     public void testInputComplexProcess() throws Exception {\r
133         logger.info(" *******************************  testInputComplexProcess  ***************************");\r
134 \r
135         String recipeName = "sample-recipe";\r
136 \r
137         String resourceassignmentContent = FileUtils.readFileToString(\r
138                 new File("src/test/resources/mapping/input/resource-assignments-complex.json"),\r
139                 Charset.defaultCharset());\r
140         List<ResourceAssignment> batchResourceAssignment =\r
141                 TransformationUtils.getListfromJson(resourceassignmentContent, ResourceAssignment.class);\r
142 \r
143         String dictionaryContent = FileUtils.readFileToString(\r
144                 new File("src/test/resources/mapping/input/input-complex.json"), Charset.defaultCharset());\r
145         Map<String, ResourceDefinition> dictionaries =\r
146                 ConfigResourceAssignmentTestUtils.getMapfromJson(dictionaryContent);\r
147 \r
148         InputResourceProcessor inputResourceProcessor = new InputResourceProcessor(configResourceService);\r
149         Map<String, Object> componentContext = new HashMap<>();\r
150         componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment);\r
151         componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName);\r
152         componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, "sample-template");\r
153         componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries);\r
154         componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".profile_name", "sample");\r
155 \r
156         Map<String, String> inParams = new HashMap<>();\r
157         SvcLogicContext ctx = new SvcLogicContext();\r
158         String datatypeContent = FileUtils.readFileToString(\r
159                 new File("src/test/resources/mapping/input/dt-location.json"), Charset.defaultCharset());\r
160         ctx.setAttribute("data_types.dt-location", datatypeContent);\r
161 \r
162         String samplelocation = FileUtils.readFileToString(\r
163                 new File("src/test/resources/mapping/input/sample-location.json"), Charset.defaultCharset());\r
164         ctx.setAttribute("location", samplelocation);\r
165 \r
166         inputResourceProcessor.process(inParams, ctx, componentContext);\r
167 \r
168         logger.trace(" componentContext " + componentContext);\r
169         logger.trace(" Resource Mapping " + TransformationUtils.getJson(batchResourceAssignment, true));\r
170 \r
171         Assert.assertNotNull("Failed to populate input recipe location value ",\r
172                 componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.location"));\r
173         Assert.assertNotNull("Failed to populate input dictionary location  value ",\r
174                 componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.location"));\r
175 \r
176     }\r
177 \r
178 }\r