Modify ONAP PAP REST classes basic checkstyle
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / DecisionPolicyDictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.pap.xacml.rest.controller;
21
22 import static org.junit.Assert.*;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.io.BufferedReader;
27 import java.io.StringReader;
28 import java.io.UnsupportedEncodingException;
29
30 import javax.servlet.http.HttpServletRequest;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.springframework.mock.web.MockHttpServletResponse;
41
42 public class DecisionPolicyDictionaryControllerTest {
43     private static Logger logger = FlexLogger.getLogger(DecisionPolicyDictionaryControllerTest.class);
44     private static CommonClassDao commonClassDao;
45     private String jsonString = null;
46     private HttpServletRequest request = null;
47     private DecisionPolicyDictionaryController controller = null;
48
49     @Before
50     public void setUp() throws Exception {
51         logger.info("setUp: Entering");
52         commonClassDao = Mockito.mock(CommonClassDao.class);
53         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
54         
55         jsonString = "{\"attributeDictionaryDatas\": {\"error\": \"\",  \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
56                 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
57                 + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
58                 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
59                 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
60                 + "     \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
61                 + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
62                 + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
63
64         BufferedReader br = new BufferedReader(new StringReader(jsonString));
65         
66         //--- mock the getReader() call
67         when(request.getReader()).thenReturn(br);   
68         
69         controller = new DecisionPolicyDictionaryController(commonClassDao);
70         new DictionaryUtils(commonClassDao);
71         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
72         mock(DictionaryUtils.class);
73         logger.info("setUp: exit");
74     }
75
76     @After
77     public void tearDown() throws Exception {
78     }
79
80     @Test
81     public void testGetSettingsDictionaryByNameEntityData() {
82         logger.info("testGetSettingsDictionaryByNameEntityData: Entering");
83
84         MockHttpServletResponse response =  new MockHttpServletResponse();
85
86         controller.getSettingsDictionaryByNameEntityData(request, response);
87
88         try {
89             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
90             logger.info("response.getContentAsString(): " + response.getContentAsString());
91         } catch (UnsupportedEncodingException e) {
92             logger.error("Exception Occured"+e);
93             fail("Exception: " + e);
94         }
95
96         logger.info("testGetSettingsDictionaryByNameEntityData: exit");
97
98     }
99
100     @Test
101     public void testGetSettingsDictionaryEntityData() {
102         logger.info("testGetSettingsDictionaryEntityData: Entering");
103
104         MockHttpServletResponse response =  new MockHttpServletResponse();
105
106         controller.getSettingsDictionaryEntityData(response);
107
108         try {
109             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
110             logger.info("response.getContentAsString(): " + response.getContentAsString());
111         } catch (UnsupportedEncodingException e) {
112             logger.error("Exception Occured"+e);
113             fail("Exception: " + e);
114         }
115
116         logger.info("testGetSettingsDictionaryEntityData: exit");
117
118     }
119
120     @Test
121     public void testSaveSettingsDictionary() {
122         logger.info("testSaveSettingsDictionary: Entering");
123
124         MockHttpServletResponse response =  new MockHttpServletResponse();
125         request = mock(HttpServletRequest.class);
126         
127         try {
128             // mock the getReader() call
129             jsonString = "{\"settingsDictionaryData\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},\"description\":\"test\",\"priority\":\"High\"}, \"userid\":\"test\"}";
130
131             BufferedReader br = new BufferedReader(new StringReader(jsonString));
132             when(request.getReader()).thenReturn(br);
133             controller.saveSettingsDictionary(request, response);
134             logger.info("response.getContentAsString(): " + response.getContentAsString());
135             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
136
137         } catch (Exception e) {
138             logger.error("Exception Occured"+e);
139             fail("Exception: " + e);
140         }
141
142         logger.info("testSaveSettingsDictionary: exit");
143
144     }
145
146     @Test
147     public void testRemoveSettingsDictionary() {
148         logger.info("testRemoveSettingsDictionary: Entering");
149
150         MockHttpServletResponse response =  new MockHttpServletResponse();
151         request = mock(HttpServletRequest.class);
152
153         try {
154             // mock the getReader() call
155             jsonString = "{\"data\": {\"modelName\": \"test\",  \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
156                     + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
157                     + " \"version\": \"\",\"createdBy\": \"someone\",   \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
158                     + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"        },"
159                     + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
160                     + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
161                     + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
162                     + " \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
163             BufferedReader br = new BufferedReader(new StringReader(jsonString));
164             when(request.getReader()).thenReturn(br);
165             controller.removeSettingsDictionary(request, response);
166             logger.info("response.getContentAsString(): " + response.getContentAsString());
167             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
168
169         } catch (Exception e) {
170             logger.error("Exception Occured"+e);
171             fail("Exception: " + e);
172         }
173
174         logger.info("testRemoveSettingsDictionary: exit");
175
176     }
177
178     @Test
179     public void testGetRainyDayDictionaryByNameEntityData() {
180         logger.info("testGetRainyDayDictionaryByNameEntityData: Entering");
181
182         MockHttpServletResponse response =  new MockHttpServletResponse();
183
184         controller.getRainyDayDictionaryByNameEntityData(request, response);
185
186         try {
187             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
188             logger.info("response.getContentAsString(): " + response.getContentAsString());
189         } catch (UnsupportedEncodingException e) {
190             logger.error("Exception Occured"+e);
191             fail("Exception: " + e);
192         }
193
194         logger.info("testGetRainyDayDictionaryByNameEntityData: exit");
195
196     }
197
198     @Test
199     public void testGetRainyDayDictionaryEntityData() {
200         logger.info("testGetRainyDayDictionaryEntityData: Entering");
201
202         MockHttpServletResponse response =  new MockHttpServletResponse();
203
204         controller.getRainyDayDictionaryEntityData(response);
205
206         try {
207             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
208             logger.info("response.getContentAsString(): " + response.getContentAsString());
209         } catch (UnsupportedEncodingException e) {
210             logger.error("Exception Occured"+e);
211             fail("Exception: " + e);
212         }
213
214         logger.info("testGetRainyDayDictionaryEntityData: exit");
215
216     }
217
218     @Test
219     public void testSaveRainyDayDictionary() {
220         logger.info("testSaveRainyDayDictionary: Entering");
221
222         MockHttpServletResponse response =  new MockHttpServletResponse();
223         request = mock(HttpServletRequest.class);
224         
225         try {
226             // mock the getReader() call
227             jsonString = "{\"rainyDayDictionaryData\":{\"bbid\":\"BB2\",\"workstep\":\"1\",\"userDataTypeValues\":[{\"$$hashKey\":\"object:233\",\"treatment\":\"test1\"},{\"$$hashKey\":\"object:239\",\"treatment\":\"test2\"}]},\"userid\":\"mm117s\"}";
228
229             BufferedReader br = new BufferedReader(new StringReader(jsonString));
230             when(request.getReader()).thenReturn(br);
231             controller.saveRainyDayDictionary(request, response);
232             logger.info("response.getContentAsString(): " + response.getContentAsString());
233             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
234
235         } catch (Exception e) {
236             logger.error("Exception Occured"+e);
237             fail("Exception: " + e);
238         }
239
240         logger.info("testSaveRainyDayDictionary: exit");
241
242     }
243
244     @Test
245     public void testRemoveRainyDayDictionary() {
246         logger.info("testRemoveRainyDayDictionary: Entering");
247
248         MockHttpServletResponse response =  new MockHttpServletResponse();
249         request = mock(HttpServletRequest.class);
250
251         try {
252             // mock the getReader() call
253             jsonString = "{\"data\": {\"modelName\": \"test\",  \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
254                     + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
255                     + " \"version\": \"\",\"createdBy\": \"someone\",   \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
256                     + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"        },"
257                     + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
258                     + " \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
259                     + " \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
260                     + " \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
261             BufferedReader br = new BufferedReader(new StringReader(jsonString));
262             when(request.getReader()).thenReturn(br);
263             controller.removeRainyDayDictionary(request, response);
264             logger.info("response.getContentAsString(): " + response.getContentAsString());
265             assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
266
267         } catch (Exception e) {
268             logger.error("Exception Occured"+e);
269             fail("Exception: " + e);
270         }
271
272         logger.info("testRemoveRainyDayDictionary: exit");
273
274     }
275
276 }