Merge "Fix audit log to contain ending timestamp"
[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  * 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.admin;
21
22 import static org.junit.Assert.fail;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.StringReader;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.controller.CreateFirewallController;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.ActionList;
41 import org.onap.policy.rest.jpa.AddressGroup;
42 import org.onap.policy.rest.jpa.FWTagPicker;
43 import org.onap.policy.rest.jpa.GroupServiceList;
44 import org.onap.policy.rest.jpa.PrefixList;
45 import org.onap.policy.rest.jpa.SecurityZone;
46 import org.onap.policy.rest.jpa.ServiceList;
47 import org.onap.policy.rest.jpa.TermList;
48 import org.onap.portalsdk.core.domain.User;
49 import org.onap.portalsdk.core.util.SystemProperties;
50 import org.springframework.mock.web.MockHttpServletResponse;
51
52 public class PolicyRestControllerTest {
53
54         private String clRequestString;
55         private String fwRequestString;
56         private String fwViewRequestString;
57         private HttpServletRequest request;
58         private MockHttpServletResponse response;
59         private static CommonClassDao commonClassDao;
60         private List<Object> prefixListData;
61         private List<Object> actionListData;
62         private List<Object> serviceListData;
63         private List<Object> addressGroupData;
64         private List<Object> securityZoneData;
65         private List<Object> serviceGroupData;
66         private List<Object> tagListData;
67         private List<Object> termListData;
68         
69         @Before
70         public void setUp() throws Exception {
71                 commonClassDao = mock(CommonClassDao.class);
72                 HttpSession mockSession = mock(HttpSession.class);
73                 request = mock(HttpServletRequest.class);  
74                 response =  new MockHttpServletResponse();
75                 User user = new User();
76                 user.setOrgUserId("Test");
77                 Mockito.when(mockSession.getAttribute(SystemProperties.getProperty("user_attribute_name"))).thenReturn(user);
78                 Mockito.when(request.getSession(false)).thenReturn(mockSession);
79                 clRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\","
80                                 + "\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\","
81                                 + "\"version\":\"\",\"createdBy\":\"Demo\",\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},"
82                                 + "\"tempModel\":{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":\"dir\",\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\","
83                                 + "\"version\":\"\",\"createdBy\":\"Demo\",\"modifiedBy\":\"Demo\",\"content\":\"\",\"recursive\":false},\"$$hashKey\":\"object:1439\","
84                                 + "\"policy\":{\"policyType\":\"Config\",\"configPolicyType\":\"ClosedLoop_Fault\",\"triggerTrapSignatures\":[1,1,2,3],"
85                                 + "\"triggerfaultSignatures\":[1,1,2,3],\"traptriggerSignatures\":[{\"id\":\"Trap1\",\"$$hashKey\":\"object:1526\"},"
86                                 + "{\"id\":\"Trap2\",\"$$hashKey\":\"object:1534\"}],\"connecttriggerSignatures\":[{\"id\":\"C1\",\"$$hashKey\":\"object:1554\","
87                                 + "\"notBox\":\"NOT\",\"connectTrap1\":\"Trap1\",\"trapCount1\":\"12\",\"operatorBox\":\"AND\",\"connectTrap2\":\"Trap2\","
88                                 + "\"trapCount2\":\"14\"}],\"faulttriggerSignatures\":[{\"id\":\"Fault1\",\"$$hashKey\":\"object:1566\"},{\"id\":\"Fault2\","
89                                 + "\"$$hashKey\":\"object:1575\"}],\"connectVerificationSignatures\":[{\"id\":\"C1\",\"$$hashKey\":\"object:1595\","
90                                 + "\"notBox\":\"NOT\",\"connectTrap1\":\"Fault1\",\"trapCount1\":\"11\",\"operatorBox\":\"AND\",\"connectTrap2\":\"Fault2\","
91                                 + "\"trapCount2\":\"12\"}],\"jsonBodyData\":{\"trapMaxAge\":\"300\",\"vnfType\":\"Test\",\"closedLoopPolicyStatus\":\"Active\","
92                                 + "\"vUSP\":true,\"trinity\":true,\"vDNS\":true,\"mcr\":true,\"gamma\":true,\"actions\":\"Test\",\"timeInterval\":\"11\","
93                                 + "\"timeOutvPRO\":\"11\",\"timeOutRuby\":\"11\",\"retrys\":\"1\",\"agingWindow\":\"12\",\"geoLink\":\"test\","
94                                 + "\"emailAddress\":\"aa@test.com\",\"pepName\":\"Test\",\"pepAction\":\"test\",\"conditions\":\"SEND\"},"
95                                 + "\"policyName\":\"SampleTest\",\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\","
96                                 + "\"guard\":\"True\",\"onapName\":\"SampleDemo\",\"ttlDate\":\"14/09/2017\",\"clearTimeOut\":\"123\",\"trapMaxAge\":\"11\","
97                                 + "\"verificationclearTimeOut\":\"13\"}},\"trapData\":{\"trap1\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1528\","
98                                 + "\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],\"trap2\":[{\"id\":\"A1\","
99                                 + "\"$$hashKey\":\"object:1536\",\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},"
100                                 + "{\"id\":\"A2\",\"$$hashKey\":\"object:1542\",\"notBox\":\"NOT\",\"trigger1\":\"A1\",\"operatorBox\":\"AND\",\"trigger2\":"
101                                 + "\"Test\"},{\"id\":\"A3\",\"$$hashKey\":\"object:1548\",\"notBox\":\"NOT\",\"trigger1\":\"A1\",\"operatorBox\":\"OR\","
102                                 + "\"trigger2\":\"A2\"}]},\"faultData\":{\"trap1\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1568\",\"notBox\":\"NOT\","
103                                 + "\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"}],\"trap2\":[{\"id\":\"A1\",\"$$hashKey\":\"object:1577\","
104                                 + "\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"AND\",\"trigger2\":\"Test\"},{\"id\":\"A2\",\"$$hashKey\":"
105                                 + "\"object:1583\",\"notBox\":\"NOT\",\"trigger1\":\"Test\",\"operatorBox\":\"OR\",\"trigger2\":\"Test\"},{\"id\":\"A3"
106                                 + "\",\"$$hashKey\":\"object:1589\",\"notBox\":\"NOT\",\"trigger1\":\"A1\",\"operatorBox\":\"AND\",\"trigger2\":\"A2\"}]}}";
107                 
108                 
109                 fwRequestString = "{\"policyData\":{\"error\":\"\",\"inprocess\":false,\"model\":{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],"
110                                 + "\"type\":\"dir\",\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\",\"modifiedBy\":"
111                                 + "\"Demo\",\"content\":\"\",\"recursive\":false},\"tempModel\":{\"name\":\"com\",\"subScopename\":\"\",\"path\":[],\"type\":"
112                                 + "\"dir\",\"size\":0,\"date\":\"2017-06-01T15:45:36.000Z\",\"version\":\"\",\"createdBy\":\"Demo\",\"modifiedBy\":\"Demo\","
113                                 + "\"content\":\"\",\"recursive\":false},\"$$hashKey\":\"object:260\",\"policy\":{\"policyType\":\"Config\",\"configPolicyType"
114                                 + "\":\"Firewall Config\",\"attributes\":[{\"id\":\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\",\"value\":\"Test\"}],"
115                                 + "\"fwattributes\":[],\"policyName\":\"SampleTest\",\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\","
116                                 + "\"riskLevel\":\"1\",\"guard\":\"True\",\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\",\"securityZone\":\"Test\"}}}";
117                 
118                 fwViewRequestString = "{\"policyData\":{\"policyType\":\"Config\",\"configPolicyType\":\"Firewall Config\",\"attributes\":[{\"id\":"
119                                 + "\"choice1\",\"$$hashKey\":\"object:338\",\"key\":\"Test\",\"value\":\"Test\"}],\"fwattributes\":[],\"policyName\":"
120                                 + "\"SampleTest\",\"policyDescription\":\"SampleTest\",\"riskType\":\"SampleRiskType\",\"riskLevel\":\"1\",\"guard\":\"True\","
121                                 + "\"configName\":\"SampleTest\",\"ttlDate\":\"14/09/2017\",\"securityZone\":\"Test\"}}";
122
123
124
125                 prefixListData = new ArrayList<>();
126                 PrefixList prefixList = new PrefixList();
127                 prefixList.setPrefixListName("Test");
128                 prefixList.setPrefixListValue("10.10.10.10/12");
129                 prefixListData.add(prefixList);
130                 when(commonClassDao.getData(PrefixList.class)).thenReturn(prefixListData);
131                 
132                 actionListData = new ArrayList<>();
133                 ActionList actionList = new ActionList();
134                 actionList.setActionName("Test");
135                 actionListData.add(actionList);
136                 when(commonClassDao.getData(ActionList.class)).thenReturn(actionListData);
137                         
138                 serviceListData = new ArrayList<>();
139                 ServiceList serviceList = new ServiceList();
140                 serviceList.setServiceName("Test");
141                 serviceList.setServiceType("SERVICE");
142                 serviceList.setServiceTransProtocol("Test");
143                 serviceList.setServiceAppProtocol("Test");
144                 serviceList.setServicePorts("8080");
145                 serviceListData.add(serviceList);
146                 when(commonClassDao.getData(ServiceList.class)).thenReturn(serviceListData);
147                 
148                 addressGroupData = new ArrayList<>();
149                 AddressGroup addressGroup = new AddressGroup();
150                 addressGroup.setGroupName("Group_Test");
151                 addressGroup.setServiceList("Test");
152                 addressGroupData.add(addressGroup);
153                 when(commonClassDao.getData(AddressGroup.class)).thenReturn(addressGroupData);
154                 
155                 securityZoneData = new ArrayList<>();
156                 SecurityZone securityZone = new SecurityZone();
157                 securityZone.setZoneName("Test");
158                 securityZone.setZoneValue("Test");
159                 securityZoneData.add(securityZone);
160                 when(commonClassDao.getData(SecurityZone.class)).thenReturn(securityZoneData);
161                 
162                 serviceGroupData = new ArrayList<>();
163                 GroupServiceList serviceGroup = new GroupServiceList();
164                 serviceGroup.setGroupName("Group_Test");
165                 serviceGroup.setServiceList("Test");
166                 serviceGroupData.add(serviceGroup);
167                 when(commonClassDao.getData(GroupServiceList.class)).thenReturn(serviceGroupData);
168                 
169                 tagListData = new ArrayList<>();
170                 FWTagPicker fwPicker = new FWTagPicker();
171                 fwPicker.setTagPickerName("Test");
172                 fwPicker.setTagValues("Test:8080");
173                 tagListData.add(fwPicker);
174                 when(commonClassDao.getData(FWTagPicker.class)).thenReturn(tagListData);
175                 
176                 termListData = new ArrayList<>();
177                 TermList termList = new TermList();
178                 termList.setTermName("Test");
179                 termList.setFromZones("Test");
180                 termList.setToZones("Test");
181                 termList.setSrcIPList("Test");
182                 termList.setDestIPList("Test");
183                 termList.setSrcPortList("Test");
184                 termList.setDestPortList("Test");
185                 termList.setAction("Test");
186                 termListData.add(termList);
187                 when(commonClassDao.getData(TermList.class)).thenReturn(termListData);
188                 when(commonClassDao.getDataById(TermList.class, "termName", "Test")).thenReturn(termListData);
189         }
190
191         @Test
192         public final void testPolicyCreationController() {
193                 PolicyRestController controller = new PolicyRestController();
194                 BufferedReader reader = new BufferedReader(new StringReader(clRequestString));
195                 try {
196                         Mockito.when(request.getReader()).thenReturn(reader);
197                         controller.policyCreationController(request, response); 
198                 } catch (IOException e) {
199                         fail();
200                 }
201                 PolicyRestController controller1 = new PolicyRestController();
202                 CreateFirewallController.setCommonClassDao(commonClassDao);
203                 BufferedReader reader1 = new BufferedReader(new StringReader(fwRequestString));
204                 try {
205                         Mockito.when(request.getReader()).thenReturn(reader1);
206                         controller1.policyCreationController(request, response);        
207                 } catch (IOException e) {
208                         fail();
209                 }
210                 
211                 CreateFirewallController fwController = new CreateFirewallController();
212                 CreateFirewallController.setCommonClassDao(commonClassDao);
213                 BufferedReader reader2 = new BufferedReader(new StringReader(fwViewRequestString));
214                 try {
215                         Mockito.when(request.getReader()).thenReturn(reader2);
216                         fwController.setFWViewRule(request, response);  
217                 } catch (IOException e) {
218                         fail();
219                 }
220         }
221
222         @Test
223         public final void testDeleteElasticData() {
224                 PolicyRestController controller = new PolicyRestController();
225                 try {
226                         controller.deleteElasticData("Test");
227                 } catch (Exception e) {
228                         fail();
229                 }
230         }
231
232 }