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.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;
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;
35 import javax.servlet.http.HttpServletRequest;
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;
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 logger.info("setUp: exit");
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");
85 public void testGetBRMSParamDictionaryByNameEntityData(){
86 logger.info("testGetBRMSParamDictionaryByNameEntityData: Entering");
87 BRMSDictionaryController.setCommonClassDao(commonClassDao);
88 controller.getBRMSParamDictionaryByNameEntityData(response);
90 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
91 logger.info("response.getContentAsString(): " + response.getContentAsString());
92 } catch (UnsupportedEncodingException e) {
93 fail("Exception: " + e);
95 logger.info("testGetBRMSParamDictionaryByNameEntityData: exit");
99 public void testGetBRMSParamDictionaryEntityData() {
100 logger.info("testGetBRMSParamDictionaryEntityData: Entering");
101 controller.getBRMSParamDictionaryEntityData(response);
103 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsParamDictionaryDatas"));
104 logger.info("response.getContentAsString(): " + response.getContentAsString());
105 } catch (UnsupportedEncodingException e) {
106 fail("Exception: " + e);
108 logger.info("testGetBRMSParamDictionaryEntityData: exit");
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());
121 logger.error("Exception"+ e);
123 logger.info("testSaveBRMSParamDictionary: exit");
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);
138 logger.info("testRemoveBRMSParamDictionary: exit");
142 public void testGetBRMSDependencyDictionaryByNameEntityData(){
143 logger.info("testGetBRMSDependencyDictionaryByNameEntityData: Entering");
144 BRMSDictionaryController.setCommonClassDao(commonClassDao);
145 controller.getBRMSDependencyDictionaryByNameEntityData(response);
147 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
148 logger.info("response.getContentAsString(): " + response.getContentAsString());
149 } catch (UnsupportedEncodingException e) {
150 fail("Exception: " + e);
152 logger.info("testGetBRMSDependencyDictionaryByNameEntityData: exit");
156 public void testGetBRMSDependencyDictionaryEntityData(){
157 logger.info("testGetBRMSDependencyDictionaryEntityData: Entering");
158 BRMSDictionaryController.setCommonClassDao(commonClassDao);
159 controller.getBRMSDependencyDictionaryEntityData(response);
161 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsDependencyDictionaryDatas"));
162 logger.info("response.getContentAsString(): " + response.getContentAsString());
163 } catch (UnsupportedEncodingException e) {
164 fail("Exception: " + e);
167 logger.info("testGetBRMSDependencyDictionaryEntityData: exit");
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);
182 logger.info("testSaveBRMSDependencyDictionary: exit");
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);
199 logger.info("testRemoveBRMSDependencyDictionary: exit");
203 public void testGetBRMSControllerDictionaryByNameEntityData(){
204 logger.info("testGetBRMSControllerDictionaryByNameEntityData: Entering");
205 MockHttpServletResponse response = new MockHttpServletResponse();
206 BRMSDictionaryController.setCommonClassDao(commonClassDao);
207 controller.getBRMSControllerDictionaryByNameEntityData(response);
209 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
210 logger.info("response.getContentAsString(): " + response.getContentAsString());
211 } catch (UnsupportedEncodingException e) {
212 fail("Exception: " + e);
214 logger.info("testGetBRMSControllerDictionaryByNameEntityData: exit");
218 public void testGetBRMSControllerDictionaryEntityData(){
219 logger.info("testGetBRMSControllerDictionaryEntityData: Entering");
220 MockHttpServletResponse response = new MockHttpServletResponse();
221 BRMSDictionaryController.setCommonClassDao(commonClassDao);
222 controller.getBRMSControllerDictionaryEntityData(response);
224 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("brmsControllerDictionaryDatas"));
225 logger.info("response.getContentAsString(): " + response.getContentAsString());
226 } catch (UnsupportedEncodingException e) {
227 fail("Exception: " + e);
229 logger.info("testGetBRMSControllerDictionaryEntityData: exit");
233 public void testSaveBRMSControllerDictionary() {
234 logger.info("testSaveBRMSControllerDictionary: Entering");
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);
247 logger.info("testSaveBRMSControllerDictionary: exit");
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"));
262 } catch (Exception e) {
263 fail("Exception: " + e);
265 logger.info("testRemoveBRMSControllerDictionary: exit");