Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / controller / SafePolicyControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
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;
28
29 import java.io.BufferedReader;
30 import java.io.StringReader;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.servlet.http.HttpServletRequest;
35
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;
47
48 public class SafePolicyControllerTest {
49
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;
58
59     @Before
60     public void setUp() throws Exception {
61         logger.info("setUp: Entering");
62         commonClassDao = Mockito.mock(CommonClassDao.class);
63
64         data = new ArrayList<>();
65         data.add("Test");
66
67         userInfo = new UserInfo();
68         userInfo.setUserLoginId("Test");
69         userInfo.setUserName("Test");
70
71         doNothing().when(commonClassDao).delete(new RiskType());
72         doNothing().when(commonClassDao).save(new RiskType());
73
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");
82     }
83
84     @Test
85     public void testGetRiskTypeDictionaryByNameEntityData() {
86         when(commonClassDao.getDataByColumn(RiskType.class, "name")).thenReturn(data);
87         controller.getRiskTypeDictionaryByNameEntityData(response);
88         try {
89             assertTrue(response.getContentAsString() != null
90                     && response.getContentAsString().contains("riskTypeDictionaryDatas"));
91         } catch (Exception e) {
92             fail();
93             logger.error(e.getMessage(), e);
94         }
95     }
96
97     @Test
98     public void testGetRiskTypeDictionaryEntityData() {
99         when(commonClassDao.getData(RiskType.class)).thenReturn(new ArrayList<>());
100         controller.getRiskTypeDictionaryEntityData(response);
101         try {
102             assertTrue(response.getContentAsString() != null
103                     && response.getContentAsString().contains("riskTypeDictionaryDatas"));
104         } catch (Exception e) {
105             fail();
106             logger.error(e.getMessage(), e);
107         }
108     }
109
110     @Test
111     public void testGetSafePolicyWarningEntityDataByName() {
112         when(commonClassDao.getDataByColumn(SafePolicyWarning.class, "name")).thenReturn(data);
113         controller.getSafePolicyWarningEntityDataByName(response);
114         try {
115             assertTrue(response.getContentAsString() != null
116                     && response.getContentAsString().contains("safePolicyWarningDatas"));
117         } catch (Exception e) {
118             fail();
119             logger.error(e.getMessage(), e);
120         }
121     }
122
123     @Test
124     public void testGetSafePolicyWarningeEntityData() {
125         when(commonClassDao.getData(SafePolicyWarning.class)).thenReturn(new ArrayList<>());
126         controller.getSafePolicyWarningeEntityData(response);
127         try {
128             assertTrue(response.getContentAsString() != null
129                     && response.getContentAsString().contains("safePolicyWarningDatas"));
130         } catch (Exception e) {
131             fail();
132             logger.error(e.getMessage(), e);
133         }
134     }
135
136     @Test
137     public void testSaveRiskTypeDictionary() {
138         jsonString =
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);
147         }
148     }
149
150     @Test
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);
160         }
161     }
162
163     @Test
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);
173         }
174     }
175
176     @Test
177     public void testSaveSafePolicyWarningDictionary() {
178         jsonString =
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);
187         }
188     }
189
190     @Test
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);
200         }
201     }
202
203     @Test
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);
213         }
214     }
215 }