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