bad043d0f4b34f3e9079d3f4613ea810f7d01313
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / ConsoleAndApiServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 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;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.when;
26 import com.att.research.xacml.util.XACMLProperties;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.ObjectWriter;
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
41 import org.apache.commons.io.IOUtils;
42 import org.hibernate.SessionFactory;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.mockito.Mockito;
48 import org.onap.policy.common.logging.ONAPLoggingContext;
49 import org.onap.policy.pap.xacml.rest.components.ConfigPolicy;
50 import org.onap.policy.pap.xacml.rest.components.Policy;
51 import org.onap.policy.pap.xacml.rest.components.PolicyDBDao;
52 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTest;
53 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTransaction;
54 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
55 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
56 import org.onap.policy.rest.adapter.PolicyRestAdapter;
57 import org.onap.policy.xacml.std.pap.StdEngine;
58 import org.onap.policy.xacml.std.pap.StdPDP;
59 import org.springframework.mock.web.DelegatingServletInputStream;
60 import org.springframework.mock.web.MockHttpServletResponse;
61 import org.springframework.mock.web.MockServletConfig;
62
63 public class ConsoleAndApiServiceTest {
64     private static final String TESTGRP5 = "testgrp5";
65     private static final String POLICY_ID = "policyId";
66     private static final String TESTGRP1 = "testgrp1";
67     private static final String TESTGROUP2 = "testgroup2";
68     private static final String DEFAULT = "default";
69     private static final String PDPS = "pdps";
70     private static final String TESTGRP2 = "testgrp2";
71     private static final String POLICY_NAME = "com.Config_SampleTest1206.1.xml";
72     private static final String PUT = "PUT";
73     private static final String POST = "POST";
74     private static final String DEVL = "DEVL";
75     private static final String TESTGRP4 = "testgrp4";
76     private static final String API2 = "api";
77     private static final String API = "API";
78     private static final String GROUP_DESCRIPTION = "groupDescription";
79     private static final String GROUP_NAME = "groupName";
80     private static final String PDP_ID = "pdpId";
81     private static final String USER_ID = "userId";
82     private static final String APIFLAG = "apiflag";
83     private static final String ENVIRONMENT_HEADER = "Environment";
84     private static PolicyDBDao dbd;
85     private static Path repository;
86     private static StdEngine stdEngine = null;
87     private static SessionFactory sessionFactory = null;
88     private static List<String> headers = new ArrayList<>();
89     private static ConsoleAndApiService consoleAndApi;
90     private static MockServletConfig servletConfig;
91     private static XACMLPapServlet pap;
92     private HttpServletRequest httpServletRequest;
93     private HttpServletResponse httpServletResponse;
94     private static final ONAPLoggingContext logContext = Mockito.mock(ONAPLoggingContext.class);
95
96     /**
97      * Sets the up before class.
98      *
99      * @throws Exception the exception
100      */
101     @BeforeClass
102     public static void setUpBeforeClass() throws Exception {
103         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pap.properties");
104         try {
105             sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testConsoleApi");
106         } catch (Exception e) {
107             Assert.fail();
108         }
109
110         PolicyDBDao.setJunit(true);
111         dbd = PolicyDBDao.getPolicyDBDaoInstance();
112         PolicyDBDao.setJunit(true);
113
114         consoleAndApi = new ConsoleAndApiService();
115
116         servletConfig = Mockito.mock(MockServletConfig.class);
117         System.setProperty("com.sun.management.jmxremote.port", "9993");
118         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
119         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
120                 .thenReturn("src/test/resources/xacml.pap.properties");
121         pap = new XACMLPapServlet();
122         pap.init(servletConfig);
123     }
124
125
126     /**
127      * Sets the up.
128      *
129      * @throws Exception the exception
130      */
131     @Before
132     public void setUp() throws Exception {
133         httpServletRequest = Mockito.mock(HttpServletRequest.class);
134         httpServletResponse = new MockHttpServletResponse();
135         Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
136         Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
137         CommonClassDaoImpl.setSessionfactory(sessionFactory);
138         PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
139         repository = Paths.get("src/test/resources/pdps");
140         stdEngine = new StdEngine(repository);
141         dbd.setPapEngine(stdEngine);
142     }
143
144     @Test
145     public void testGroupCreation() {
146         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
147         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
148         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
149         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
150         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
151         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP1);
152         try {
153             consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP1, logContext, stdEngine);
154             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
155         } catch (IOException e) {
156             fail(e.getMessage());
157         }
158     }
159
160     @Test
161     public void testGroupNotExistInDb() {
162         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
163         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
164         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
165         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
166         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn("testgrpNotExist");
167         try {
168             consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, "testgrpNotExist", logContext, stdEngine);
169             assertTrue(HttpServletResponse.SC_INTERNAL_SERVER_ERROR == httpServletResponse.getStatus());
170         } catch (IOException e) {
171             fail(e.getMessage());
172         }
173     }
174
175     @Test
176     public void testGroupChange() {
177         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
178         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
179         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
180         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
181         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
182         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP2);
183         try {
184             consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine);
185             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
186         } catch (IOException e) {
187             fail(e.getMessage());
188         }
189
190         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
191         Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(DEFAULT);
192         try {
193             consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine);
194             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
195         } catch (IOException e) {
196             fail(e.getMessage());
197         }
198     }
199
200     @Test
201     public void testPushPolicy() throws Exception {
202         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
203         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
204         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
205         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
206         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
207         Mockito.when(httpServletRequest.getParameter(POLICY_ID)).thenReturn(POLICY_NAME);
208         repository = Paths.get(PDPS);
209         stdEngine = new StdEngine(repository);
210         dbd.setPapEngine(stdEngine);
211         populatePolicyInDb();
212
213         try {
214             consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine);
215             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
216         } catch (IOException e) {
217             fail(e.getMessage());
218         }
219     }
220
221     @Test
222     public void testCreatePolicy() throws Exception {
223         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
224         Mockito.when(httpServletRequest.getMethod()).thenReturn(PUT);
225         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
226         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
227         Mockito.when(httpServletRequest.getParameter("policy")).thenReturn(POLICY_NAME);
228         repository = Paths.get(PDPS);
229         stdEngine = new StdEngine(repository);
230         dbd.setPapEngine(stdEngine);
231
232         try {
233             consoleAndApi.doAcPut(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine);
234             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
235         } catch (IOException e) {
236             fail(e.getMessage());
237         }
238     }
239
240     @Test
241     public void testCreateAndMovePdp() throws Exception {
242         // create two groups, create a pdp on one group and then move it to another group
243         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
244         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
245         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
246         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
247         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
248         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
249         try {
250             consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
251             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
252             Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP5);
253             consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP5, logContext, stdEngine);
254             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
255         } catch (IOException e) {
256             fail(e.getMessage());
257         }
258
259         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
260         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
261         Mockito.when(httpServletRequest.getMethod()).thenReturn(PUT);
262         httpServletResponse = new MockHttpServletResponse();
263         StdPDP newPdp = new StdPDP("http://localhost:4344/pdp/", "newpdp", "new desc", 9999);
264         ObjectWriter ow = new ObjectMapper().writer();
265         when(httpServletRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(
266                 new ByteArrayInputStream(ow.writeValueAsString(newPdp).getBytes(StandardCharsets.UTF_8))));
267         try {
268             consoleAndApi.doAcPut(httpServletRequest, httpServletResponse, TESTGRP5, logContext, stdEngine);
269             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
270         } catch (IOException e) {
271             fail(e.getMessage());
272         }
273
274         httpServletRequest = Mockito.mock(HttpServletRequest.class);
275         httpServletResponse = new MockHttpServletResponse();
276         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
277         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
278         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
279         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
280         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
281         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
282         try {
283             consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
284             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
285         } catch (IOException e) {
286             fail(e.getMessage());
287         }
288
289         httpServletRequest = Mockito.mock(HttpServletRequest.class);
290         httpServletResponse = new MockHttpServletResponse();
291         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
292         Mockito.when(httpServletRequest.getMethod()).thenReturn("DELETE");
293         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
294         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
295         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
296         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
297         try {
298             consoleAndApi.doAcDelete(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
299             assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
300         } catch (IOException e) {
301             fail(e.getMessage());
302         }
303
304     }
305
306     @Test
307     public void testGet() throws Exception {
308         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
309         Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
310         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
311         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
312         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
313         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn("");
314         try {
315             consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
316             assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
317             httpServletResponse = new MockHttpServletResponse();
318             Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(DEFAULT);
319             consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
320             assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
321             Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn(null);
322             Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(null);
323             consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
324             assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
325             Mockito.when(httpServletRequest.getParameter("getPDPGroup")).thenReturn(TESTGROUP2);
326             Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGROUP2);
327             consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, TESTGROUP2, logContext, stdEngine);
328             assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
329         } catch (IOException e) {
330             fail(e.getMessage());
331         }
332
333     }
334
335     private static void populatePolicyInDb() {
336         CommonClassDaoImpl.setSessionfactory(sessionFactory);
337         PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
338         Policy policyObject = new ConfigPolicy();
339         policyObject.policyAdapter = new PolicyRestAdapter();
340         policyObject.policyAdapter.setConfigName("testpolicyhandle");
341         policyObject.policyAdapter.setPolicyDescription("my description");
342         policyObject.policyAdapter.setConfigBodyData("this is my test config file");
343         policyObject.policyAdapter.setPolicyName("SampleTest1206");
344         policyObject.policyAdapter.setConfigType(ConfigPolicy.OTHER_CONFIG);
345         policyObject.policyAdapter.setPolicyType("Config");
346         policyObject.policyAdapter.setDomainDir("com");
347         policyObject.policyAdapter.setVersion("1");
348         policyObject.policyAdapter.setHighestVersion(1);
349         PolicyType policyTypeObject = new PolicyType();
350         policyObject.policyAdapter.setPolicyData(policyTypeObject);
351         ClassLoader classLoader = ConsoleAndApiServiceTest.class.getClassLoader();
352         PolicyType policyConfig = new PolicyType();
353         policyConfig.setVersion("1");
354         policyConfig.setPolicyId("");
355         policyConfig.setTarget(new TargetType());
356         policyObject.policyAdapter.setData(policyConfig);
357         try {
358             policyObject.policyAdapter
359                     .setParentPath(IOUtils.toString(classLoader.getResourceAsStream("Config_SampleTest1206.1.xml")));
360         } catch (Exception e2) {
361             fail();
362         }
363
364         PolicyDBDaoTransaction transaction = dbd.getNewTransaction();
365         try {
366             transaction.createPolicy(policyObject, API);
367             transaction.commitTransaction();
368         } catch (Exception e) {
369             transaction.rollbackTransaction();
370             Assert.fail();
371         }
372     }
373 }