7e8d1420124e50a8ef71ecfdf26df3d5d3dfa3a0
[ccsdk/features.git] /
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 DefaultResourceProcessorTest {\r
52 \r
53     private static EELFLogger logger = EELFManager.getInstance().getLogger(DefaultResourceProcessorTest.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 testDefaultSimpleProcess() throws Exception {\r
82         logger.info(" *******************************  testDefaultSimpleProcess  ***************************");\r
83 \r
84         String recipeName = "sample-recipe";\r
85 \r
86         String resourceassignmentContent = FileUtils.readFileToString(\r
87                 new File("src/test/resources/mapping/default/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         DefaultResourceProcessor defaultResourceProcessor = new DefaultResourceProcessor(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         defaultResourceProcessor.process(inParams, ctx, componentContext);\r
108         logger.trace(" componentContext " + componentContext);\r
109 \r
110         Assert.assertEquals("Failed to populate default country value ", "US",\r
111                 componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.country"));\r
112         Assert.assertEquals("Failed to populate default country value ", "US",\r
113                 componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.country"));\r
114 \r
115         Assert.assertEquals("Failed to populate default port value ", 830,\r
116                 componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.port"));\r
117         Assert.assertEquals("Failed to populate default port value ", 830,\r
118                 componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.port"));\r
119 \r
120         Assert.assertEquals("Failed to populate default voip-enabled value ", true,\r
121                 componentContext.get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + "sample-recipe.voip-enabled"));\r
122         Assert.assertEquals("Failed to populate default voip-enabled value ", true,\r
123                 componentContext.get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + "sample-recipe.voip-enabled"));\r
124 \r
125     }\r
126 \r
127 }\r