ae1840ebb37b6a09d617701cccf82c889e4d7f13
[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 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 package org.onap.policy.pap.xacml.rest.controller;
21
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;
27
28 import java.io.BufferedReader;
29 import java.io.StringReader;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import javax.servlet.http.HttpServletRequest;
34
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;
46
47 public class SafePolicyControllerTest {
48
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;
57
58     @Before
59     public void setUp() throws Exception {
60         logger.info("setUp: Entering");
61         commonClassDao = Mockito.mock(CommonClassDao.class);
62
63         data = new ArrayList<>();
64         data.add("Test");
65
66         userInfo = new UserInfo();
67         userInfo.setUserLoginId("Test");
68         userInfo.setUserName("Test");
69
70         doNothing().when(commonClassDao).delete(new RiskType());
71         doNothing().when(commonClassDao).save(new RiskType());
72
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");
81     }
82
83     @Test
84     public void testGetRiskTypeDictionaryByNameEntityData(){
85         when(commonClassDao.getDataByColumn(RiskType.class, "name")).thenReturn(data);
86         controller.getRiskTypeDictionaryByNameEntityData(response);
87         try {
88             assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
89         } catch (Exception e) {
90             fail();
91             logger.error(e.getMessage(),e);
92         }
93     }
94
95     @Test
96     public void testGetRiskTypeDictionaryEntityData(){
97         when(commonClassDao.getData(RiskType.class)).thenReturn(new ArrayList<>());
98         controller.getRiskTypeDictionaryEntityData(response);
99         try {
100             assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas"));
101         } catch (Exception e) {
102             fail();
103             logger.error(e.getMessage(),e);
104         }
105     }
106
107     @Test
108     public void testGetSafePolicyWarningEntityDataByName(){
109         when(commonClassDao.getDataByColumn(SafePolicyWarning.class, "name")).thenReturn(data);
110         controller.getSafePolicyWarningEntityDataByName(response);
111         try {
112             assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
113         } catch (Exception e) {
114             fail();
115             logger.error(e.getMessage(),e);
116         }
117     }
118
119     @Test
120     public void testGetSafePolicyWarningeEntityData(){
121         when(commonClassDao.getData(SafePolicyWarning.class)).thenReturn(new ArrayList<>());
122         controller.getSafePolicyWarningeEntityData(response);
123         try {
124             assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("safePolicyWarningDatas"));
125         } catch (Exception e) {
126             fail();
127             logger.error(e.getMessage(),e);
128         }
129     }
130
131     @Test
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"));
138         }catch(Exception e){
139             logger.error("Exception"+ e);
140         }
141     }
142
143     @Test
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"));
150         }catch(Exception e){
151             logger.error("Exception"+ e);
152         }
153     }
154
155     @Test
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"));
162         }catch(Exception e){
163             logger.error("Exception"+ e);
164         }
165     }
166
167     @Test
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"));
174         }catch(Exception e){
175             logger.error("Exception"+ e);
176         }
177     }
178
179     @Test
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"));
186         }catch(Exception e){
187             logger.error("Exception"+ e);
188         }
189     }
190
191     @Test
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"));
198         }catch(Exception e){
199             logger.error("Exception"+ e);
200         }
201     }
202 }