Adding JUNITs for ONAP-PAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / BRMSDictionaryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 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.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.io.BufferedReader;
30 import java.io.StringReader;
31 import java.io.UnsupportedEncodingException;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import javax.servlet.http.HttpServletRequest;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mockito;
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
42 import org.onap.policy.rest.dao.CommonClassDao;
43 import org.onap.policy.rest.jpa.BRMSParamTemplate;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.springframework.mock.web.MockHttpServletResponse;
46
47 public class BRMSDictionaryControllerTest {
48
49         private static Logger logger = FlexLogger.getLogger(BRMSDictionaryControllerTest.class);
50         private static CommonClassDao commonClassDao;
51         private String jsonString = null;
52         private HttpServletRequest request = null;
53         private BRMSDictionaryController controller = null;
54         private MockHttpServletResponse response = null;
55
56         @Before
57         public void setUp() throws Exception {
58                 logger.info("setUp: Entering");
59                 commonClassDao = Mockito.mock(CommonClassDao.class);
60                 UserInfo userInfo = new UserInfo();
61                 userInfo.setUserLoginId("testUserId");
62                 userInfo.setUserName("John");
63                 when(commonClassDao.getEntityItem(UserInfo.class, "userLoginId", "testing")).thenReturn(userInfo);
64                 List<String>  brms = new ArrayList<String>();
65                 brms.add("BRMS-Model");
66                 when(commonClassDao.getDataByColumn(BRMSParamTemplate.class, "name")).thenReturn(brms);
67                 doNothing().when(commonClassDao).delete(new BRMSParamTemplate());
68                 doNothing().when(commonClassDao).save(new BRMSParamTemplate());
69                 controller = new BRMSDictionaryController();
70                 request = Mockito.mock(HttpServletRequest.class);
71                 response =  new MockHttpServletResponse();  
72                 logger.info("setUp: exit");
73         }
74
75         @Test
76         public void testGetUserInfo() {
77                 logger.info("testGetUserInfo: Entering");               
78                 UserInfo userInfo = controller.getUserInfo("testing");
79                 logger.info("userInfo.getUserName() : " + userInfo.getUserName());
80                 assertEquals("John", userInfo.getUserName());
81                 logger.info("testGetUserInfo: exit");           
82         }
83
84         @Test
85         public void testGetBRMSParamDictionaryByNameEntityData(){
86                 logger.info("testGetBRMSParamDictionaryByNameEntityData: Entering");
87                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
88                 controller.getBRMSParamDictionaryByNameEntityData(response);
89                 try {
90                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
91                         logger.info("response.getContentAsString(): " + response.getContentAsString());
92                 } catch (UnsupportedEncodingException e) {
93                         fail("Exception: " + e);
94                 }
95                 logger.info("testGetBRMSParamDictionaryByNameEntityData: exit");
96         }
97
98         @Test
99         public void testGetBRMSParamDictionaryEntityData() {
100                 logger.info("testGetBRMSParamDictionaryEntityData: Entering");
101                 controller.getBRMSParamDictionaryEntityData(response);
102                 try {
103                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
104                         logger.info("response.getContentAsString(): " + response.getContentAsString());
105                 } catch (UnsupportedEncodingException e) {
106                         fail("Exception: " + e);
107                 }
108                 logger.info("testGetBRMSParamDictionaryEntityData: exit");
109         }
110
111         @Test
112         public void testSaveBRMSParamDictionary() {
113                 logger.info("testSaveBRMSParamDictionary: Entering");
114                 jsonString = "{\"brmsParamDictionaryData\": {\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
115                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
116                         when(request.getReader()).thenReturn(br);
117                         controller.saveBRMSParamDictionary(request, response);
118                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryData"));
119                         logger.info("response.getContentAsString(): " + response.getContentAsString());
120                 }catch(Exception e){
121                         logger.error("Exception"+ e);
122                 } 
123                 logger.info("testSaveBRMSParamDictionary: exit");
124         }
125
126         @Test
127         public void testRemoveBRMSParamDictionary() {
128                 logger.info("testRemoveBRMSParamDictionary: Entering");
129                 jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
130                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
131                         when(request.getReader()).thenReturn(br);
132                         controller.removeBRMSParamDictionary(request, response);
133                         logger.info("response.getContentAsString(): " + response.getContentAsString());
134                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
135                 } catch (Exception e) {
136                         fail("Exception: " + e);
137                 }
138                 logger.info("testRemoveBRMSParamDictionary: exit");
139         }
140
141         @Test
142         public void testGetBRMSDependencyDictionaryByNameEntityData(){
143                 logger.info("testGetBRMSDependencyDictionaryByNameEntityData: Entering");
144                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
145                 controller.getBRMSDependencyDictionaryByNameEntityData(response);
146                 try {
147                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
148                         logger.info("response.getContentAsString(): " + response.getContentAsString());
149                 } catch (UnsupportedEncodingException e) {
150                         fail("Exception: " + e);
151                 }
152                 logger.info("testGetBRMSDependencyDictionaryByNameEntityData: exit");
153         }
154
155         @Test
156         public void testGetBRMSDependencyDictionaryEntityData(){
157                 logger.info("testGetBRMSDependencyDictionaryEntityData: Entering");
158                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
159                 controller.getBRMSDependencyDictionaryEntityData(response);
160                 try {
161                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
162                         logger.info("response.getContentAsString(): " + response.getContentAsString());
163                 } catch (UnsupportedEncodingException e) {
164                         fail("Exception: " + e);
165                 }
166
167                 logger.info("testGetBRMSDependencyDictionaryEntityData: exit");
168         }
169
170         @Test
171         public void testSaveBRMSDependencyDictionary() {
172                 logger.info("testSaveBRMSDependencyDictionary: Entering");
173                         jsonString = "{\"brmsDependencyDictionaryData\": {\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
174                         try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
175                                 when(request.getReader()).thenReturn(br);                   
176                                 controller.saveBRMSDependencyDictionary(request, response);
177                                 logger.info("response.getContentAsString(): " + response.getContentAsString());
178                                 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryData"));
179                         } catch (Exception e) {
180                         fail("Exception: " + e);
181                 }
182                 logger.info("testSaveBRMSDependencyDictionary: exit");
183         }
184
185         @Test
186         public void testRemoveBRMSDependencyDictionary() {
187                 logger.info("testRemoveBRMSDependencyDictionary: Entering");
188                 MockHttpServletResponse response =  new MockHttpServletResponse();
189                 request = mock(HttpServletRequest.class);   
190                 jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
191                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
192                         when(request.getReader()).thenReturn(br);                   
193                         controller.removeBRMSDependencyDictionary(request, response);
194                         logger.info("response.getContentAsString(): " + response.getContentAsString());
195                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
196                 } catch (Exception e) {
197                         fail("Exception: " + e);
198                 }
199                 logger.info("testRemoveBRMSDependencyDictionary: exit");
200         }
201
202         @Test
203         public void testGetBRMSControllerDictionaryByNameEntityData(){
204                 logger.info("testGetBRMSControllerDictionaryByNameEntityData: Entering");
205                 MockHttpServletResponse response =  new MockHttpServletResponse();
206                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
207                 controller.getBRMSControllerDictionaryByNameEntityData(response);
208                 try {
209                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
210                         logger.info("response.getContentAsString(): " + response.getContentAsString());
211                 } catch (UnsupportedEncodingException e) {
212                         fail("Exception: " + e);
213                 }
214                 logger.info("testGetBRMSControllerDictionaryByNameEntityData: exit");
215         }
216
217         @Test
218         public void testGetBRMSControllerDictionaryEntityData(){
219                 logger.info("testGetBRMSControllerDictionaryEntityData: Entering");
220                 MockHttpServletResponse response =  new MockHttpServletResponse();
221                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
222                 controller.getBRMSControllerDictionaryEntityData(response);
223                 try {
224                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
225                         logger.info("response.getContentAsString(): " + response.getContentAsString());
226                 } catch (UnsupportedEncodingException e) {
227                         fail("Exception: " + e);
228                 }
229                 logger.info("testGetBRMSControllerDictionaryEntityData: exit");
230         }
231
232         @Test
233         public void testSaveBRMSControllerDictionary() {
234                 logger.info("testSaveBRMSControllerDictionary: Entering");
235
236                 MockHttpServletResponse response =  new MockHttpServletResponse();
237                 request = mock(HttpServletRequest.class);   
238                 jsonString = "{\"brmsControllerDictionaryData\": {\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
239                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
240                         when(request.getReader()).thenReturn(br);                   
241                         controller.saveBRMSControllerDictionary(request, response);
242                         logger.info("response.getContentAsString(): " + response.getContentAsString());
243                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryData"));
244                 } catch (Exception e) {
245                         fail("Exception: " + e);
246                 }
247                 logger.info("testSaveBRMSControllerDictionary: exit");
248         }
249
250         @Test
251         public void testRemoveBRMSControllerDictionary() {
252                 logger.info("testRemoveBRMSControllerDictionary: Entering");
253                 MockHttpServletResponse response =  new MockHttpServletResponse();
254                 request = mock(HttpServletRequest.class);
255                 jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
256                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
257                         when(request.getReader()).thenReturn(br);                   
258                         controller.removeBRMSControllerDictionary(request, response);
259                         logger.info("response.getContentAsString(): " + response.getContentAsString());
260                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
261
262                 } catch (Exception e) {
263                         fail("Exception: " + e);
264                 }
265                 logger.info("testRemoveBRMSControllerDictionary: exit");
266         }
267 }
268