0a60e18313e31455f1eecf91f4b6d29915528c3d
[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.assertTrue;
23 import static org.junit.Assert.fail;
24 import static org.mockito.Mockito.doNothing;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.io.BufferedReader;
29 import java.io.StringReader;
30 import java.io.UnsupportedEncodingException;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.policy.common.logging.flexlogger.FlexLogger;
40 import org.onap.policy.common.logging.flexlogger.Logger;
41 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
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                 new DictionaryUtils(commonClassDao);
73         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
74         mock(DictionaryUtils.class);
75                 logger.info("setUp: exit");
76         }
77
78         @Test
79         public void testGetBRMSParamDictionaryByNameEntityData(){
80                 logger.info("testGetBRMSParamDictionaryByNameEntityData: Entering");
81                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
82                 controller.getBRMSParamDictionaryByNameEntityData(response);
83                 try {
84                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
85                         logger.info("response.getContentAsString(): " + response.getContentAsString());
86                 } catch (UnsupportedEncodingException e) {
87                         fail("Exception: " + e);
88                 }
89                 logger.info("testGetBRMSParamDictionaryByNameEntityData: exit");
90         }
91
92         @Test
93         public void testGetBRMSParamDictionaryEntityData() {
94                 logger.info("testGetBRMSParamDictionaryEntityData: Entering");
95                 controller.getBRMSParamDictionaryEntityData(response);
96                 try {
97                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
98                         logger.info("response.getContentAsString(): " + response.getContentAsString());
99                 } catch (UnsupportedEncodingException e) {
100                         fail("Exception: " + e);
101                 }
102                 logger.info("testGetBRMSParamDictionaryEntityData: exit");
103         }
104
105         @Test
106         public void testSaveBRMSParamDictionary() {
107                 logger.info("testSaveBRMSParamDictionary: Entering");
108                 jsonString = "{\"brmsParamDictionaryData\": {\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
109                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
110                         when(request.getReader()).thenReturn(br);
111                         controller.saveBRMSParamDictionary(request, response);
112                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryData"));
113                         logger.info("response.getContentAsString(): " + response.getContentAsString());
114                 }catch(Exception e){
115                         logger.error("Exception"+ e);
116                 } 
117                 logger.info("testSaveBRMSParamDictionary: exit");
118         }
119
120         @Test
121         public void testRemoveBRMSParamDictionary() {
122                 logger.info("testRemoveBRMSParamDictionary: Entering");
123                 jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
124                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
125                         when(request.getReader()).thenReturn(br);
126                         controller.removeBRMSParamDictionary(request, response);
127                         logger.info("response.getContentAsString(): " + response.getContentAsString());
128                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
129                 } catch (Exception e) {
130                         fail("Exception: " + e);
131                 }
132                 logger.info("testRemoveBRMSParamDictionary: exit");
133         }
134
135         @Test
136         public void testGetBRMSDependencyDictionaryByNameEntityData(){
137                 logger.info("testGetBRMSDependencyDictionaryByNameEntityData: Entering");
138                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
139                 controller.getBRMSDependencyDictionaryByNameEntityData(response);
140                 try {
141                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
142                         logger.info("response.getContentAsString(): " + response.getContentAsString());
143                 } catch (UnsupportedEncodingException e) {
144                         fail("Exception: " + e);
145                 }
146                 logger.info("testGetBRMSDependencyDictionaryByNameEntityData: exit");
147         }
148
149         @Test
150         public void testGetBRMSDependencyDictionaryEntityData(){
151                 logger.info("testGetBRMSDependencyDictionaryEntityData: Entering");
152                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
153                 controller.getBRMSDependencyDictionaryEntityData(response);
154                 try {
155                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
156                         logger.info("response.getContentAsString(): " + response.getContentAsString());
157                 } catch (UnsupportedEncodingException e) {
158                         fail("Exception: " + e);
159                 }
160
161                 logger.info("testGetBRMSDependencyDictionaryEntityData: exit");
162         }
163
164         @Test
165         public void testSaveBRMSDependencyDictionary() {
166                 logger.info("testSaveBRMSDependencyDictionary: Entering");
167                         jsonString = "{\"brmsDependencyDictionaryData\": {\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
168                         try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
169                                 when(request.getReader()).thenReturn(br);                   
170                                 controller.saveBRMSDependencyDictionary(request, response);
171                                 logger.info("response.getContentAsString(): " + response.getContentAsString());
172                                 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryData"));
173                         } catch (Exception e) {
174                         fail("Exception: " + e);
175                 }
176                 logger.info("testSaveBRMSDependencyDictionary: exit");
177         }
178
179         @Test
180         public void testRemoveBRMSDependencyDictionary() {
181                 logger.info("testRemoveBRMSDependencyDictionary: Entering");
182                 MockHttpServletResponse response =  new MockHttpServletResponse();
183                 request = mock(HttpServletRequest.class);   
184                 jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
185                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
186                         when(request.getReader()).thenReturn(br);                   
187                         controller.removeBRMSDependencyDictionary(request, response);
188                         logger.info("response.getContentAsString(): " + response.getContentAsString());
189                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
190                 } catch (Exception e) {
191                         fail("Exception: " + e);
192                 }
193                 logger.info("testRemoveBRMSDependencyDictionary: exit");
194         }
195
196         @Test
197         public void testGetBRMSControllerDictionaryByNameEntityData(){
198                 logger.info("testGetBRMSControllerDictionaryByNameEntityData: Entering");
199                 MockHttpServletResponse response =  new MockHttpServletResponse();
200                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
201                 controller.getBRMSControllerDictionaryByNameEntityData(response);
202                 try {
203                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
204                         logger.info("response.getContentAsString(): " + response.getContentAsString());
205                 } catch (UnsupportedEncodingException e) {
206                         fail("Exception: " + e);
207                 }
208                 logger.info("testGetBRMSControllerDictionaryByNameEntityData: exit");
209         }
210
211         @Test
212         public void testGetBRMSControllerDictionaryEntityData(){
213                 logger.info("testGetBRMSControllerDictionaryEntityData: Entering");
214                 MockHttpServletResponse response =  new MockHttpServletResponse();
215                 BRMSDictionaryController.setCommonClassDao(commonClassDao);     
216                 controller.getBRMSControllerDictionaryEntityData(response);
217                 try {
218                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
219                         logger.info("response.getContentAsString(): " + response.getContentAsString());
220                 } catch (UnsupportedEncodingException e) {
221                         fail("Exception: " + e);
222                 }
223                 logger.info("testGetBRMSControllerDictionaryEntityData: exit");
224         }
225
226         @Test
227         public void testSaveBRMSControllerDictionary() {
228                 logger.info("testSaveBRMSControllerDictionary: Entering");
229
230                 MockHttpServletResponse response =  new MockHttpServletResponse();
231                 request = mock(HttpServletRequest.class);   
232                 jsonString = "{\"brmsControllerDictionaryData\": {\"ruleName\": \"test\",\"rule\": \"test\"},\"userid\": \"testName\"}";
233                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
234                         when(request.getReader()).thenReturn(br);                   
235                         controller.saveBRMSControllerDictionary(request, response);
236                         logger.info("response.getContentAsString(): " + response.getContentAsString());
237                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryData"));
238                 } catch (Exception e) {
239                         fail("Exception: " + e);
240                 }
241                 logger.info("testSaveBRMSControllerDictionary: exit");
242         }
243
244         @Test
245         public void testRemoveBRMSControllerDictionary() {
246                 logger.info("testRemoveBRMSControllerDictionary: Entering");
247                 MockHttpServletResponse response =  new MockHttpServletResponse();
248                 request = mock(HttpServletRequest.class);
249                 jsonString = "{\"data\": {\"ruleName\": \"test\",\"rule\": \"test\"}}";
250                 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
251                         when(request.getReader()).thenReturn(br);                   
252                         controller.removeBRMSControllerDictionary(request, response);
253                         logger.info("response.getContentAsString(): " + response.getContentAsString());
254                         assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
255
256                 } catch (Exception e) {
257                         fail("Exception: " + e);
258                 }
259                 logger.info("testRemoveBRMSControllerDictionary: exit");
260         }
261 }
262