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.when;
27 import java.io.BufferedReader;
28 import java.io.StringReader;
29 import java.util.ArrayList;
30 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.RiskType;
41 import org.onap.policy.rest.jpa.SafePolicyWarning;
42 import org.onap.policy.rest.jpa.UserInfo;
43 import org.springframework.mock.web.MockHttpServletResponse;
45 public class SafePolicyControllerTest {
47 private static Logger logger = FlexLogger.getLogger(SafePolicyControllerTest.class);
48 private static CommonClassDao commonClassDao;
49 private String jsonString = null;
50 private HttpServletRequest request = null;
51 private SafePolicyController controller = null;
52 private MockHttpServletResponse response = null;
53 private UserInfo userInfo;
54 private List<String> data;
57 public void setUp() throws Exception {
58 logger.info("setUp: Entering");
59 commonClassDao = Mockito.mock(CommonClassDao.class);
61 data = new ArrayList<>();
64 userInfo = new UserInfo();
65 userInfo.setUserLoginId("Test");
66 userInfo.setUserName("Test");
68 doNothing().when(commonClassDao).delete(new RiskType());
69 doNothing().when(commonClassDao).save(new RiskType());
71 controller = new SafePolicyController();
72 controller.setCommonClassDao(commonClassDao);
74 request = Mockito.mock(HttpServletRequest.class);
75 response = new MockHttpServletResponse();
76 logger.info("setUp: exit");
80 public void testGetRiskTypeDictionaryByNameEntityData(){
81 when(commonClassDao.getDataByColumn(RiskType.class, "name")).thenReturn(data);
82 controller.getRiskTypeDictionaryByNameEntityData(response);
84 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
85 } catch (Exception e) {
87 logger.error(e.getMessage(),e);
92 public void testGetRiskTypeDictionaryEntityData(){
93 when(commonClassDao.getData(RiskType.class)).thenReturn(new ArrayList<>());
94 controller.getRiskTypeDictionaryEntityData(response);
96 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
97 } catch (Exception e) {
99 logger.error(e.getMessage(),e);
104 public void testGetSafePolicyWarningEntityDataByName(){
105 when(commonClassDao.getDataByColumn(SafePolicyWarning.class, "name")).thenReturn(data);
106 controller.getSafePolicyWarningEntityDataByName(response);
108 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
109 } catch (Exception e) {
111 logger.error(e.getMessage(),e);
116 public void testGetSafePolicyWarningeEntityData(){
117 when(commonClassDao.getData(SafePolicyWarning.class)).thenReturn(new ArrayList<>());
118 controller.getSafePolicyWarningeEntityData(response);
120 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
121 } catch (Exception e) {
123 logger.error(e.getMessage(),e);
128 public void testSaveRiskTypeDictionary(){
129 jsonString = "{\"userid\":\"demo\",\"riskTypeDictionaryData\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
130 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
131 when(request.getReader()).thenReturn(br);
132 controller.saveRiskTypeDictionary(request, response);
133 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
135 logger.error("Exception"+ e);
140 public void testUpdateRiskTypeDictionary(){
141 jsonString = "{\"userid\":\"demo\",\"riskTypeDictionaryData\":{\"description\":\"test\",\"name\":\"Test\"}}";
142 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
143 when(request.getReader()).thenReturn(br);
144 controller.saveRiskTypeDictionary(request, response);
145 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
147 logger.error("Exception"+ e);
152 public void testRemoveRiskTypeDictionary(){
153 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
154 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
155 when(request.getReader()).thenReturn(br);
156 controller.removeRiskTypeDictionary(request, response);
157 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
159 logger.error("Exception"+ e);
164 public void testSaveSafePolicyWarningDictionary(){
165 jsonString = "{\"userid\":\"demo\",\"safePolicyWarningData\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
166 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
167 when(request.getReader()).thenReturn(br);
168 controller.saveSafePolicyWarningDictionary(request, response);
169 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
171 logger.error("Exception"+ e);
176 public void testUpdateSafePolicyWarningDictionary(){
177 jsonString = "{\"userid\":\"demo\",\"safePolicyWarningData\":{\"description\":\"test\",\"name\":\"Test\"}}";
178 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
179 when(request.getReader()).thenReturn(br);
180 controller.saveSafePolicyWarningDictionary(request, response);
181 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
183 logger.error("Exception"+ e);
188 public void testRemoveSafePolicyWarningDictionary(){
189 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
190 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
191 when(request.getReader()).thenReturn(br);
192 controller.removeSafePolicyWarningDictionary(request, response);
193 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
195 logger.error("Exception"+ e);