New commit to add Junits to PAP-REST
[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 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.rest.dao.CommonClassDao;
39 import org.springframework.mock.web.MockHttpServletResponse;
40
41 public class DecisionPolicyDictionaryControllerTest {
42         private static Logger logger = FlexLogger.getLogger(DecisionPolicyDictionaryControllerTest.class);
43         private static CommonClassDao commonClassDao;
44         private String jsonString = null;
45         private HttpServletRequest request = null;
46         private DecisionPolicyDictionaryController controller = null;
47         
48         @Before
49         public void setUp() throws Exception {
50                 logger.info("setUp: Entering");
51         commonClassDao = Mockito.mock(CommonClassDao.class);
52             HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
53         
54                 jsonString = "{\"attributeDictionaryDatas\": {\"error\": \"\",  \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
55                                 + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
56                                 + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
57                                 + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
58                                 + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
59                                 + "     \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
60                                 + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
61                                 + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
62
63         BufferedReader br = new BufferedReader(new StringReader(jsonString));
64         
65         //--- mock the getReader() call
66         when(request.getReader()).thenReturn(br);   
67         
68         controller = new DecisionPolicyDictionaryController(commonClassDao);
69         
70         logger.info("setUp: exit");
71         }
72
73         @After
74         public void tearDown() throws Exception {
75         }
76
77         @Test
78         public void testGetSettingsDictionaryByNameEntityData() {
79                 logger.info("testGetSettingsDictionaryByNameEntityData: Entering");
80
81                 MockHttpServletResponse response =  new MockHttpServletResponse();
82
83                 controller.getSettingsDictionaryByNameEntityData(request, response);
84                 
85                 try {
86                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
87                         logger.info("response.getContentAsString(): " + response.getContentAsString());
88                 } catch (UnsupportedEncodingException e) {
89                         logger.error("Exception Occured"+e);
90                         fail("Exception: " + e);
91                 }
92                 
93                 logger.info("testGetSettingsDictionaryByNameEntityData: exit"); 
94                 
95         }
96
97         @Test
98         public void testGetSettingsDictionaryEntityData() {
99                 logger.info("testGetSettingsDictionaryEntityData: Entering");
100
101                 MockHttpServletResponse response =  new MockHttpServletResponse();
102
103                 controller.getSettingsDictionaryEntityData(response);
104                 
105                 try {
106                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
107                         logger.info("response.getContentAsString(): " + response.getContentAsString());
108                 } catch (UnsupportedEncodingException e) {
109                         logger.error("Exception Occured"+e);
110                         fail("Exception: " + e);
111                 }
112                 
113                 logger.info("testGetSettingsDictionaryEntityData: exit");
114                 
115         }
116
117         @Test
118         public void testSaveSettingsDictionary() {
119                 logger.info("testSaveSettingsDictionary: Entering");
120
121                 MockHttpServletResponse response =  new MockHttpServletResponse();
122             request = mock(HttpServletRequest.class);  
123         
124                 try {
125                     // mock the getReader() call
126                         jsonString = "{\"settingsDictionaryData\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},\"description\":\"test\",\"priority\":\"High\"}, \"userid\":\"test\"}";
127                         
128                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
129                         when(request.getReader()).thenReturn(br);                   
130                         controller.saveSettingsDictionary(request, response);
131                         logger.info("response.getContentAsString(): " + response.getContentAsString());
132                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
133
134                 } catch (Exception e) {
135                         logger.error("Exception Occured"+e);
136                         fail("Exception: " + e);
137                 }
138                 
139                 logger.info("testSaveSettingsDictionary: exit");        
140                 
141         }
142
143         @Test
144         public void testRemoveSettingsDictionary() {
145                 logger.info("testRemoveSettingsDictionary: Entering");
146
147                 MockHttpServletResponse response =  new MockHttpServletResponse();
148             request = mock(HttpServletRequest.class);   
149         
150                 try {
151                     // mock the getReader() call
152                         jsonString = "{\"data\": {\"modelName\": \"test\",      \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
153                                         + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
154                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
155                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
156                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
157                                         + "     \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
158                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
159                                         + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
160                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
161                         when(request.getReader()).thenReturn(br);                   
162                         controller.removeSettingsDictionary(request, response);
163                         logger.info("response.getContentAsString(): " + response.getContentAsString());
164                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("settingsDictionaryDatas"));
165
166                 } catch (Exception e) {
167                         logger.error("Exception Occured"+e);
168                         fail("Exception: " + e);
169                 }
170                 
171                 logger.info("testRemoveSettingsDictionary: exit");      
172                 
173         }
174
175         @Test
176         public void testGetRainyDayDictionaryByNameEntityData() {
177                 logger.info("testGetRainyDayDictionaryByNameEntityData: Entering");
178
179                 MockHttpServletResponse response =  new MockHttpServletResponse();
180
181                 controller.getRainyDayDictionaryByNameEntityData(request, response);
182                 
183                 try {
184                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
185                         logger.info("response.getContentAsString(): " + response.getContentAsString());
186                 } catch (UnsupportedEncodingException e) {
187                         logger.error("Exception Occured"+e);
188                         fail("Exception: " + e);
189                 }
190                 
191                 logger.info("testGetRainyDayDictionaryByNameEntityData: exit");         
192                 
193         }
194
195         @Test
196         public void testGetRainyDayDictionaryEntityData() {
197                 logger.info("testGetRainyDayDictionaryEntityData: Entering");
198
199                 MockHttpServletResponse response =  new MockHttpServletResponse();
200
201                 controller.getRainyDayDictionaryEntityData(request, response);
202                 
203                 try {
204                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
205                         logger.info("response.getContentAsString(): " + response.getContentAsString());
206                 } catch (UnsupportedEncodingException e) {
207                         logger.error("Exception Occured"+e);
208                         fail("Exception: " + e);
209                 }
210                 
211                 logger.info("testGetRainyDayDictionaryEntityData: exit");       
212                 
213         }
214
215         @Test
216         public void testSaveRainyDayDictionary() {
217                 logger.info("testSaveRainyDayDictionary: Entering");
218
219                 MockHttpServletResponse response =  new MockHttpServletResponse();
220             request = mock(HttpServletRequest.class);  
221         
222                 try {
223                     // mock the getReader() call
224                         jsonString = "{\"rainyDayDictionaryData\":{\"bbid\":\"BB2\",\"workstep\":\"1\",\"userDataTypeValues\":[{\"$$hashKey\":\"object:233\",\"treatment\":\"test1\"},{\"$$hashKey\":\"object:239\",\"treatment\":\"test2\"}]},\"userid\":\"mm117s\"}";
225                         
226                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
227                         when(request.getReader()).thenReturn(br);                   
228                         controller.saveRainyDayDictionary(request, response);
229                         logger.info("response.getContentAsString(): " + response.getContentAsString());
230                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
231
232                 } catch (Exception e) {
233                         logger.error("Exception Occured"+e);
234                         fail("Exception: " + e);
235                 }
236                 
237                 logger.info("testSaveRainyDayDictionary: exit");                
238                 
239         }
240
241         @Test
242         public void testRemoveRainyDayDictionary() {
243                 logger.info("testRemoveRainyDayDictionary: Entering");
244
245                 MockHttpServletResponse response =  new MockHttpServletResponse();
246             request = mock(HttpServletRequest.class);   
247         
248                 try {
249                     // mock the getReader() call
250                         jsonString = "{\"data\": {\"modelName\": \"test\",      \"inprocess\": false,\"model\": {\"name\": \"testingdata\", "
251                                         + " \"subScopename\": \"\",\"path\": [],\"type\": \"dir\",\"size\": 0,\"date\": \"2017-04-12T21:26:57.000Z\", "
252                                         + " \"version\": \"\",\"createdBy\": \"someone\",       \"modifiedBy\": \"someone\",    \"content\": \"\",\"recursive\": false},"
253                                         + " \"tempModel\": {\"name\": \"testingdata\",\"subScopename\": \"\"    },"
254                                         + " \"policy\": {\"policyType\": \"Config\",\"configPolicyType\": \"Micro Service\",\"policyName\": \"may1501\", "
255                                         + "     \"policyDescription\": \"testing input\", \"ecompName\": \"RaviTest\",\"guard\": \"False\",\"riskType\": \"Risk12345\",\"riskLevel\": \"2\","
256                                         + "     \"priority\": \"6\",\"serviceType\": \"DkatPolicyBody\",\"version\": \"1707.41.02\",\"ruleGridData\": [ [\"fileId\"]],\"ttlDate\": null}}, "
257                                         + "     \"policyJSON\": {\"pmTableName\": \"test\",     \"dmdTopic\": \"1\",\"fileId\": \"56\"} }";
258                         BufferedReader br = new BufferedReader(new StringReader(jsonString));
259                         when(request.getReader()).thenReturn(br);                   
260                         controller.removeRainyDayDictionary(request, response);
261                         logger.info("response.getContentAsString(): " + response.getContentAsString());
262                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("rainyDayDictionaryDatas"));
263
264                 } catch (Exception e) {
265                         logger.error("Exception Occured"+e);
266                         fail("Exception: " + e);
267                 }
268                 
269                 logger.info("testRemoveRainyDayDictionary: exit");              
270                 
271         }
272
273 }