2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
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.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.RiskType;
44 import org.onap.policy.rest.jpa.SafePolicyWarning;
45 import org.onap.policy.rest.jpa.UserInfo;
46 import org.springframework.mock.web.MockHttpServletResponse;
48 public class SafePolicyControllerTest {
50 private static Logger logger = FlexLogger.getLogger(SafePolicyControllerTest.class);
51 private static CommonClassDao commonClassDao;
52 private String jsonString = null;
53 private HttpServletRequest request = null;
54 private SafePolicyController controller = null;
55 private MockHttpServletResponse response = null;
56 private UserInfo userInfo;
57 private List<String> data;
60 public void setUp() throws Exception {
61 logger.info("setUp: Entering");
62 commonClassDao = Mockito.mock(CommonClassDao.class);
64 data = new ArrayList<>();
67 userInfo = new UserInfo();
68 userInfo.setUserLoginId("Test");
69 userInfo.setUserName("Test");
71 doNothing().when(commonClassDao).delete(new RiskType());
72 doNothing().when(commonClassDao).save(new RiskType());
74 controller = new SafePolicyController();
75 controller.setCommonClassDao(commonClassDao);
76 new DictionaryUtils(commonClassDao);
77 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
78 mock(DictionaryUtils.class);
79 request = Mockito.mock(HttpServletRequest.class);
80 response = new MockHttpServletResponse();
81 logger.info("setUp: exit");
85 public void testGetRiskTypeDictionaryByNameEntityData() {
86 when(commonClassDao.getDataByColumn(RiskType.class, "name")).thenReturn(data);
87 controller.getRiskTypeDictionaryByNameEntityData(response);
89 assertTrue(response.getContentAsString() != null
90 && response.getContentAsString().contains("riskTypeDictionaryDatas"));
91 } catch (Exception e) {
93 logger.error(e.getMessage(), e);
98 public void testGetRiskTypeDictionaryEntityData() {
99 when(commonClassDao.getData(RiskType.class)).thenReturn(new ArrayList<>());
100 controller.getRiskTypeDictionaryEntityData(response);
102 assertTrue(response.getContentAsString() != null
103 && response.getContentAsString().contains("riskTypeDictionaryDatas"));
104 } catch (Exception e) {
106 logger.error(e.getMessage(), e);
111 public void testGetSafePolicyWarningEntityDataByName() {
112 when(commonClassDao.getDataByColumn(SafePolicyWarning.class, "name")).thenReturn(data);
113 controller.getSafePolicyWarningEntityDataByName(response);
115 assertTrue(response.getContentAsString() != null
116 && response.getContentAsString().contains("safePolicyWarningDatas"));
117 } catch (Exception e) {
119 logger.error(e.getMessage(), e);
124 public void testGetSafePolicyWarningeEntityData() {
125 when(commonClassDao.getData(SafePolicyWarning.class)).thenReturn(new ArrayList<>());
126 controller.getSafePolicyWarningeEntityData(response);
128 assertTrue(response.getContentAsString() != null
129 && response.getContentAsString().contains("safePolicyWarningDatas"));
130 } catch (Exception e) {
132 logger.error(e.getMessage(), e);
137 public void testSaveRiskTypeDictionary() {
139 "{\"userid\":\"demo\",\"riskTypeDictionaryData\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
140 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
141 when(request.getReader()).thenReturn(br);
142 controller.saveRiskTypeDictionary(request, response);
143 assertTrue(response.getContentAsString() != null
144 && response.getContentAsString().contains("riskTypeDictionaryDatas"));
145 } catch (Exception e) {
146 logger.error("Exception" + e);
151 public void testUpdateRiskTypeDictionary() {
152 jsonString = "{\"userid\":\"demo\",\"riskTypeDictionaryData\":{\"description\":\"test\",\"name\":\"Test\"}}";
153 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
154 when(request.getReader()).thenReturn(br);
155 controller.saveRiskTypeDictionary(request, response);
156 assertTrue(response.getContentAsString() != null
157 && response.getContentAsString().contains("riskTypeDictionaryDatas"));
158 } catch (Exception e) {
159 logger.error("Exception" + e);
164 public void testRemoveRiskTypeDictionary() {
165 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
166 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
167 when(request.getReader()).thenReturn(br);
168 controller.removeRiskTypeDictionary(request, response);
169 assertTrue(response.getContentAsString() != null
170 && response.getContentAsString().contains("riskTypeDictionaryDatas"));
171 } catch (Exception e) {
172 logger.error("Exception" + e);
177 public void testSaveSafePolicyWarningDictionary() {
179 "{\"userid\":\"demo\",\"safePolicyWarningData\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
180 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
181 when(request.getReader()).thenReturn(br);
182 controller.saveSafePolicyWarningDictionary(request, response);
183 assertTrue(response.getContentAsString() != null
184 && response.getContentAsString().contains("safePolicyWarningDatas"));
185 } catch (Exception e) {
186 logger.error("Exception" + e);
191 public void testUpdateSafePolicyWarningDictionary() {
192 jsonString = "{\"userid\":\"demo\",\"safePolicyWarningData\":{\"description\":\"test\",\"name\":\"Test\"}}";
193 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
194 when(request.getReader()).thenReturn(br);
195 controller.saveSafePolicyWarningDictionary(request, response);
196 assertTrue(response.getContentAsString() != null
197 && response.getContentAsString().contains("safePolicyWarningDatas"));
198 } catch (Exception e) {
199 logger.error("Exception" + e);
204 public void testRemoveSafePolicyWarningDictionary() {
205 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
206 try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) {
207 when(request.getReader()).thenReturn(br);
208 controller.removeSafePolicyWarningDictionary(request, response);
209 assertTrue(response.getContentAsString() != null
210 && response.getContentAsString().contains("safePolicyWarningDatas"));
211 } catch (Exception e) {
212 logger.error("Exception" + e);