2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.pap.xacml.rest.controller;
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;
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;
34 import javax.servlet.http.HttpServletRequest;
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;
47 public class BRMSDictionaryControllerTest {
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;
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");
79 public void testGetBRMSParamDictionaryByNameEntityData(){
80 logger.info("testGetBRMSParamDictionaryByNameEntityData: Entering");
81 BRMSDictionaryController.setCommonClassDao(commonClassDao);
82 controller.getBRMSParamDictionaryByNameEntityData(response);
84 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
85 logger.info("response.getContentAsString(): " + response.getContentAsString());
86 } catch (UnsupportedEncodingException e) {
87 fail("Exception: " + e);
89 logger.info("testGetBRMSParamDictionaryByNameEntityData: exit");
93 public void testGetBRMSParamDictionaryEntityData() {
94 logger.info("testGetBRMSParamDictionaryEntityData: Entering");
95 controller.getBRMSParamDictionaryEntityData(response);
97 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
98 logger.info("response.getContentAsString(): " + response.getContentAsString());
99 } catch (UnsupportedEncodingException e) {
100 fail("Exception: " + e);
102 logger.info("testGetBRMSParamDictionaryEntityData: exit");
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());
115 logger.error("Exception"+ e);
117 logger.info("testSaveBRMSParamDictionary: exit");
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);
132 logger.info("testRemoveBRMSParamDictionary: exit");
136 public void testGetBRMSDependencyDictionaryByNameEntityData(){
137 logger.info("testGetBRMSDependencyDictionaryByNameEntityData: Entering");
138 BRMSDictionaryController.setCommonClassDao(commonClassDao);
139 controller.getBRMSDependencyDictionaryByNameEntityData(response);
141 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
142 logger.info("response.getContentAsString(): " + response.getContentAsString());
143 } catch (UnsupportedEncodingException e) {
144 fail("Exception: " + e);
146 logger.info("testGetBRMSDependencyDictionaryByNameEntityData: exit");
150 public void testGetBRMSDependencyDictionaryEntityData(){
151 logger.info("testGetBRMSDependencyDictionaryEntityData: Entering");
152 BRMSDictionaryController.setCommonClassDao(commonClassDao);
153 controller.getBRMSDependencyDictionaryEntityData(response);
155 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
156 logger.info("response.getContentAsString(): " + response.getContentAsString());
157 } catch (UnsupportedEncodingException e) {
158 fail("Exception: " + e);
161 logger.info("testGetBRMSDependencyDictionaryEntityData: exit");
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);
176 logger.info("testSaveBRMSDependencyDictionary: exit");
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);
193 logger.info("testRemoveBRMSDependencyDictionary: exit");
197 public void testGetBRMSControllerDictionaryByNameEntityData(){
198 logger.info("testGetBRMSControllerDictionaryByNameEntityData: Entering");
199 MockHttpServletResponse response = new MockHttpServletResponse();
200 BRMSDictionaryController.setCommonClassDao(commonClassDao);
201 controller.getBRMSControllerDictionaryByNameEntityData(response);
203 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
204 logger.info("response.getContentAsString(): " + response.getContentAsString());
205 } catch (UnsupportedEncodingException e) {
206 fail("Exception: " + e);
208 logger.info("testGetBRMSControllerDictionaryByNameEntityData: exit");
212 public void testGetBRMSControllerDictionaryEntityData(){
213 logger.info("testGetBRMSControllerDictionaryEntityData: Entering");
214 MockHttpServletResponse response = new MockHttpServletResponse();
215 BRMSDictionaryController.setCommonClassDao(commonClassDao);
216 controller.getBRMSControllerDictionaryEntityData(response);
218 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
219 logger.info("response.getContentAsString(): " + response.getContentAsString());
220 } catch (UnsupportedEncodingException e) {
221 fail("Exception: " + e);
223 logger.info("testGetBRMSControllerDictionaryEntityData: exit");
227 public void testSaveBRMSControllerDictionary() {
228 logger.info("testSaveBRMSControllerDictionary: Entering");
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);
241 logger.info("testSaveBRMSControllerDictionary: exit");
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"));
256 } catch (Exception e) {
257 fail("Exception: " + e);
259 logger.info("testRemoveBRMSControllerDictionary: exit");