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.util.ArrayList;
31 import java.util.List;
33 import javax.servlet.http.HttpServletRequest;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
39 import org.onap.policy.common.logging.flexlogger.Logger;
40 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.rest.jpa.RiskType;
43 import org.onap.policy.rest.jpa.SafePolicyWarning;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.springframework.mock.web.MockHttpServletResponse;
47 public class SafePolicyControllerTest {
49 private static Logger logger = FlexLogger.getLogger(SafePolicyControllerTest.class);
50 private static CommonClassDao commonClassDao;
51 private String jsonString = null;
52 private HttpServletRequest request = null;
53 private SafePolicyController controller = null;
54 private MockHttpServletResponse response = null;
55 private UserInfo userInfo;
56 private List<String> data;
59 public void setUp() throws Exception {
60 logger.info("setUp: Entering");
61 commonClassDao = Mockito.mock(CommonClassDao.class);
63 data = new ArrayList<>();
66 userInfo = new UserInfo();
67 userInfo.setUserLoginId("Test");
68 userInfo.setUserName("Test");
70 doNothing().when(commonClassDao).delete(new RiskType());
71 doNothing().when(commonClassDao).save(new RiskType());
73 controller = new SafePolicyController();
74 controller.setCommonClassDao(commonClassDao);
75 new DictionaryUtils(commonClassDao);
76 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
77 mock(DictionaryUtils.class);
78 request = Mockito.mock(HttpServletRequest.class);
79 response = new MockHttpServletResponse();
80 logger.info("setUp: exit");
84 public void testGetRiskTypeDictionaryByNameEntityData(){
85 when(commonClassDao.getDataByColumn(RiskType.class, "name")).thenReturn(data);
86 controller.getRiskTypeDictionaryByNameEntityData(response);
88 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
89 } catch (Exception e) {
91 logger.error(e.getMessage(),e);
96 public void testGetRiskTypeDictionaryEntityData(){
97 when(commonClassDao.getData(RiskType.class)).thenReturn(new ArrayList<>());
98 controller.getRiskTypeDictionaryEntityData(response);
100 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
101 } catch (Exception e) {
103 logger.error(e.getMessage(),e);
108 public void testGetSafePolicyWarningEntityDataByName(){
109 when(commonClassDao.getDataByColumn(SafePolicyWarning.class, "name")).thenReturn(data);
110 controller.getSafePolicyWarningEntityDataByName(response);
112 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
113 } catch (Exception e) {
115 logger.error(e.getMessage(),e);
120 public void testGetSafePolicyWarningeEntityData(){
121 when(commonClassDao.getData(SafePolicyWarning.class)).thenReturn(new ArrayList<>());
122 controller.getSafePolicyWarningeEntityData(response);
124 assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
125 } catch (Exception e) {
127 logger.error(e.getMessage(),e);
132 public void testSaveRiskTypeDictionary(){
133 jsonString = "{\"userid\":\"demo\",\"riskTypeDictionaryData\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
134 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
135 when(request.getReader()).thenReturn(br);
136 controller.saveRiskTypeDictionary(request, response);
137 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
139 logger.error("Exception"+ e);
144 public void testUpdateRiskTypeDictionary(){
145 jsonString = "{\"userid\":\"demo\",\"riskTypeDictionaryData\":{\"description\":\"test\",\"name\":\"Test\"}}";
146 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
147 when(request.getReader()).thenReturn(br);
148 controller.saveRiskTypeDictionary(request, response);
149 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
151 logger.error("Exception"+ e);
156 public void testRemoveRiskTypeDictionary(){
157 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
158 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
159 when(request.getReader()).thenReturn(br);
160 controller.removeRiskTypeDictionary(request, response);
161 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
163 logger.error("Exception"+ e);
168 public void testSaveSafePolicyWarningDictionary(){
169 jsonString = "{\"userid\":\"demo\",\"safePolicyWarningData\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
170 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
171 when(request.getReader()).thenReturn(br);
172 controller.saveSafePolicyWarningDictionary(request, response);
173 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
175 logger.error("Exception"+ e);
180 public void testUpdateSafePolicyWarningDictionary(){
181 jsonString = "{\"userid\":\"demo\",\"safePolicyWarningData\":{\"description\":\"test\",\"name\":\"Test\"}}";
182 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
183 when(request.getReader()).thenReturn(br);
184 controller.saveSafePolicyWarningDictionary(request, response);
185 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
187 logger.error("Exception"+ e);
192 public void testRemoveSafePolicyWarningDictionary(){
193 jsonString = "{\"userid\":\"demo\",\"data\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}";
194 try(BufferedReader br = new BufferedReader(new StringReader(jsonString))){
195 when(request.getReader()).thenReturn(br);
196 controller.removeSafePolicyWarningDictionary(request, response);
197 assertTrue( response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
199 logger.error("Exception"+ e);