Merge "Reformat POLICY-SDK-APP test cases"
[policy/engine.git] / POLICY-SDK-APP / src / test / java / org / onap / policy / admin / PolicyRestControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.policy.admin;
23
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.StringReader;
30 import java.util.ArrayList;
31 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.policy.controller.CreateFirewallController;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.ActionList;
40 import org.onap.policy.rest.jpa.AddressGroup;
41 import org.onap.policy.rest.jpa.FWTagPicker;
42 import org.onap.policy.rest.jpa.GroupServiceList;
43 import org.onap.policy.rest.jpa.PrefixList;
44 import org.onap.policy.rest.jpa.SecurityZone;
45 import org.onap.policy.rest.jpa.ServiceList;
46 import org.onap.policy.rest.jpa.TermList;
47 import org.onap.portalsdk.core.domain.User;
48 import org.onap.portalsdk.core.util.SystemProperties;
49 import org.springframework.mock.web.MockHttpServletResponse;
50
51 public class PolicyRestControllerTest {
52
53     private String clRequestString;
54     private String fwRequestString;
55     private String fwViewRequestString;
56     private HttpServletRequest request;
57     private MockHttpServletResponse response;
58     private static CommonClassDao commonClassDao;
59     private List<Object> prefixListData;
60     private List<Object> actionListData;
61     private List<Object> serviceListData;
62     private List<Object> addressGroupData;
63     private List<Object> securityZoneData;
64     private List<Object> serviceGroupData;
65     private List<Object> tagListData;
66     private List<Object> termListData;
67
68     @Before
69     public void setUp() throws Exception {
70         commonClassDao = mock(CommonClassDao.class);
71         HttpSession mockSession = mock(HttpSession.class);
72         request = mock(HttpServletRequest.class);
73         response = new MockHttpServletResponse();
74         User user = new User();
75         user.setOrgUserId("Test");
76         Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name")))
77                 .thenReturn(user);
78         Mockito.when(request.getSession(false)).thenReturn(mockSession);
79         clRequestString =
80                 "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
81                         + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
82                         + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
83                         + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
84                         + "\"tempModel\":{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\","
85                         + "\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
86                         + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
87                         + "\"$$hashKey\":\"object:1439\",\"policy\":{\"policyType\":\"Config\","
88                         + "\"configPolicyType\":\"ClosedLoop_Fault\",\"triggerTrapSignatures\":[1,1,2,3],"
89                         + "\"triggerfaultSignatures\":[1,1,2,3],\"traptriggerSignatures\":[{\"id\":\"Trap1\","
90                         + "\"$$hashKey\":\"object:1526\"},{\"id\":\"Trap2\",\"$$hashKey\":\"object:1534\"}],"
91                         + "\"connecttriggerSignatures\":[{\"id\":\"C1\",\"$$hashKey\":\"object:1554\","
92                         + "\"notBox\":\"NOT\",\"connectTrap1\":\"Trap1\",\"trapCount1\":\"12\","
93                         + "\"operatorBox\":\"AND\",\"connectTrap2\":\"Trap2\",\"trapCount2\":\"14\"}],"
94                         + "\"faulttriggerSignatures\":[{\"id\":\"Fault1\",\"$$hashKey\":\"object:1566\"},"
95                         + "{\"id\":\"Fault2\",\"$$hashKey\":\"object:1575\"}],\"connectVerificationSignatures\":"
96                         + "[{\"id\":\"C1\",\"$$hashKey\":\"object:1595\",\"notBox\":\"NOT\","
97                         + "\"connectTrap1\":\"Fault1\",\"trapCount1\":\"11\",\"operatorBox\":\"AND\","
98                         + "\"connectTrap2\":\"Fault2\",\"trapCount2\":\"12\"}],\"jsonBodyData\":"
99                         + "{\"trapMaxAge\":\"300\",\"vnfType\":\"Test\",\"closedLoopPolicyStatus\":\"Active\","
100                         + "\"vUSP\":true,\"trinity\":true,\"vDNS\":true,\"mcr\":true,\"gamma\":true,"
101                         + "\"actions\":\"Test\",\"timeInterval\":\"11\",\"timeOutvPRO\":\"11\",\"timeOutRuby\":\"11\","
102                         + "\"retrys\":\"1\",\"agingWindow\":\"12\",\"geoLink\":\"test\","
103                         + "\"emailAddress\":\"aa@test.com\",\"pepName\":\"Test\",\"pepAction\":\"test\","
104                         + "\"conditions\":\"SEND\"},\"policyName\":\"SampleTest\","
105                         + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
106                         + "\"guard\":\"True\",\"onapName\":\"SampleDemo\",\"ttlDate\":\"14/09/2017\","
107                         + "\"clearTimeOut\":\"123\",\"trapMaxAge\":\"11\",\"verificationclearTimeOut\":\"13\"}},"
108                         + "\"trapData\":{\"trap1\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1528\","
109                         + "\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],"
110                         + "\"trap2\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1536\",\"notBox\":\"NOT\","
111                         + "\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\","
112                         + "\"$$hashKey\":\"object:1542\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
113                         + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A3\","
114                         + "\"$$hashKey\":\"object:1548\",\"notBox\":\"NOT\",\"trigger1\":\"A1\","
115                         + "\"operatorBox\":\"OR\",\"trigger2\":\"A2\"}]},\"faultData\":{\"trap1\":[{\"id\":\"A1\","
116                         + "\"$$hashKey\":\"object:1568\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
117                         + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],\"trap2\":[{\"id\":\"A1\","
118                         + "\"$$hashKey\":\"object:1577\",\"notBox\":\"NOT\",\"trigger1\":\"Test\","
119                         + "\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\",\"$$hashKey\":"
120                         + "\"object:1583\",\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"OR\","
121                         + "\"trigger2\":\"Test\"},{\"id\":\"A3\",\"$$hashKey\":\"object:1589\",\"notBox\":\"NOT\","
122                         + "\"trigger1\":\"A1\",\"operatorBox\":\"AND\",\"trigger2\":\"A2\"}]}}";
123
124
125         fwRequestString =
126                 "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
127                         + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
128                         + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
129                         + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"tempModel\":"
130                         + "{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,"
131                         + "\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\","
132                         + "\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"$$hashKey\":\"object:260\","
133                         + "\"policy\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\","
134                         + "\"attributes\":[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\","
135                         + "\"value\":\"Test\"}],\"fwattributes\":[],\"policyName\":\"SampleTest\","
136                         + "\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
137                         + "\"guard\":\"True\",\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\","
138                         + "\"securityZone\":\"Test\"}}}";
139
140         fwViewRequestString =
141                 "{\"policyData\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\",\"attributes\":"
142                         + "[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\",\"value\":\"Test\"}],"
143                         + "\"fwattributes\":[],\"policyName\":\"SampleTest\",\"policyDescription\":\"SampleTest\","
144                         + "\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\",\"guard\":\"True\","
145                         + "\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\",\"securityZone\":\"Test\"}}";
146
147
148
149         prefixListData = new ArrayList<>();
150         PrefixList prefixList = new PrefixList();
151         prefixList.setPrefixListName("Test");
152         prefixList.setPrefixListValue("10.10.10.10/12");
153         prefixListData.add(prefixList);
154         when(commonClassDao.getData(PrefixList.class)).thenReturn(prefixListData);
155
156         actionListData = new ArrayList<>();
157         ActionList actionList = new ActionList();
158         actionList.setActionName("Test");
159         actionListData.add(actionList);
160         when(commonClassDao.getData(ActionList.class)).thenReturn(actionListData);
161
162         serviceListData = new ArrayList<>();
163         ServiceList serviceList = new ServiceList();
164         serviceList.setServiceName("Test");
165         serviceList.setServiceType("SERVICE");
166         serviceList.setServiceTransProtocol("Test");
167         serviceList.setServiceAppProtocol("Test");
168         serviceList.setServicePorts("8080");
169         serviceListData.add(serviceList);
170         when(commonClassDao.getData(ServiceList.class)).thenReturn(serviceListData);
171
172         addressGroupData = new ArrayList<>();
173         AddressGroup addressGroup = new AddressGroup();
174         addressGroup.setGroupName("Group_Test");
175         addressGroup.setServiceList("Test");
176         addressGroupData.add(addressGroup);
177         when(commonClassDao.getData(AddressGroup.class)).thenReturn(addressGroupData);
178
179         securityZoneData = new ArrayList<>();
180         SecurityZone securityZone = new SecurityZone();
181         securityZone.setZoneName("Test");
182         securityZone.setZoneValue("Test");
183         securityZoneData.add(securityZone);
184         when(commonClassDao.getData(SecurityZone.class)).thenReturn(securityZoneData);
185
186         serviceGroupData = new ArrayList<>();
187         GroupServiceList serviceGroup = new GroupServiceList();
188         serviceGroup.setGroupName("Group_Test");
189         serviceGroup.setServiceList("Test");
190         serviceGroupData.add(serviceGroup);
191         when(commonClassDao.getData(GroupServiceList.class)).thenReturn(serviceGroupData);
192
193         tagListData = new ArrayList<>();
194         FWTagPicker fwPicker = new FWTagPicker();
195         fwPicker.setTagPickerName("Test");
196         fwPicker.setTagValues("Test:8080");
197         tagListData.add(fwPicker);
198         when(commonClassDao.getData(FWTagPicker.class)).thenReturn(tagListData);
199
200         termListData = new ArrayList<>();
201         TermList termList = new TermList();
202         termList.setTermName("Test");
203         termList.setFromZones("Test");
204         termList.setToZones("Test");
205         termList.setSrcIPList("Test");
206         termList.setDestIPList("Test");
207         termList.setSrcPortList("Test");
208         termList.setDestPortList("Test");
209         termList.setAction("Test");
210         termListData.add(termList);
211         when(commonClassDao.getData(TermList.class)).thenReturn(termListData);
212         when(commonClassDao.getDataById(TermList.class, "termName", "Test"))
213                 .thenReturn(termListData);
214     }
215
216     @Test
217     public final void testPolicyCreationController() {
218         PolicyRestController controller = new PolicyRestController();
219         BufferedReader reader = new BufferedReader(new StringReader(clRequestString));
220         try {
221             Mockito.when(request.getReader()).thenReturn(reader);
222             controller.policyCreationController(request, response);
223         } catch (IOException e) {
224             fail();
225         }
226         PolicyRestController controller1 = new PolicyRestController();
227         CreateFirewallController.setCommonClassDao(commonClassDao);
228         BufferedReader reader1 = new BufferedReader(new StringReader(fwRequestString));
229         try {
230             Mockito.when(request.getReader()).thenReturn(reader1);
231             controller1.policyCreationController(request, response);
232         } catch (IOException e) {
233             fail();
234         }
235
236         CreateFirewallController fwController = new CreateFirewallController();
237         CreateFirewallController.setCommonClassDao(commonClassDao);
238         BufferedReader reader2 = new BufferedReader(new StringReader(fwViewRequestString));
239         try {
240             Mockito.when(request.getReader()).thenReturn(reader2);
241             fwController.setFWViewRule(request, response);
242         } catch (IOException e) {
243             fail();
244         }
245     }
246
247     @Test
248     public final void testDeleteElasticData() {
249         PolicyRestController controller = new PolicyRestController();
250         try {
251             controller.deleteElasticData("Test");
252         } catch (Exception e) {
253             fail();
254         }
255     }
256 }