Update css file name in conf.py
[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-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.xacml.rest;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
26
27 import com.att.research.xacml.util.XACMLProperties;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.ObjectWriter;
30
31 import java.io.ByteArrayInputStream;
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.charset.StandardCharsets;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
45
46 import org.apache.commons.io.IOUtils;
47 import org.hibernate.SessionFactory;
48 import org.junit.AfterClass;
49 import org.junit.Before;
50 import org.junit.BeforeClass;
51 import org.junit.Rule;
52 import org.junit.Test;
53 import org.junit.rules.TemporaryFolder;
54 import org.mockito.Mockito;
55 import org.onap.policy.common.logging.OnapLoggingContext;
56 import org.onap.policy.pap.xacml.rest.components.ConfigPolicy;
57 import org.onap.policy.pap.xacml.rest.components.Policy;
58 import org.onap.policy.pap.xacml.rest.components.PolicyDbDao;
59 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTest;
60 import org.onap.policy.pap.xacml.rest.components.PolicyDbDaoTransaction;
61 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
62 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
63 import org.onap.policy.rest.adapter.PolicyRestAdapter;
64 import org.onap.policy.rest.dao.PolicyDbException;
65 import org.onap.policy.xacml.std.pap.StdEngine;
66 import org.onap.policy.xacml.std.pap.StdPDP;
67 import org.springframework.mock.web.DelegatingServletInputStream;
68 import org.springframework.mock.web.MockHttpServletResponse;
69 import org.springframework.mock.web.MockServletConfig;
70
71 public class ConsoleAndApiServiceTest {
72     private static final String TESTGRP5 = "testgrp5";
73     private static final String POLICY_ID = "policyId";
74     private static final String TESTGRP1 = "testgrp1";
75     private static final String TESTGROUP2 = "testgroup2";
76     private static final String DEFAULT = "default";
77     private static final String PDPS = "pdps";
78     private static final String TESTGRP2 = "testgrp2";
79     private static final String POLICY_NAME = "com.Config_SampleTest1206.1.xml";
80     private static final String PUT = "PUT";
81     private static final String POST = "POST";
82     private static final String DEVL = "DEVL";
83     private static final String TESTGRP4 = "testgrp4";
84     private static final String API2 = "api";
85     private static final String API = "API";
86     private static final String GROUP_DESCRIPTION = "groupDescription";
87     private static final String GROUP_NAME = "groupName";
88     private static final String PDP_ID = "pdpId";
89     private static final String USER_ID = "userId";
90     private static final String APIFLAG = "apiflag";
91     private static final String ENVIRONMENT_HEADER = "Environment";
92     private static final OnapLoggingContext logContext = Mockito.mock(OnapLoggingContext.class);
93     private static PolicyDbDao dbd;
94     private static StdEngine stdEngine = null;
95     private static SessionFactory sessionFactory = null;
96     private static List<String> headers = new ArrayList<>();
97     private static ConsoleAndApiService consoleAndApi;
98     private static MockServletConfig servletConfig;
99     private static XACMLPapServlet pap;
100     private HttpServletRequest httpServletRequest;
101     private HttpServletResponse httpServletResponse;
102     @Rule
103     public TemporaryFolder folder = new TemporaryFolder();
104
105     /**
106      * Sets the up before class.
107      *
108      * @throws Exception the exception
109      */
110     @BeforeClass
111     public static void setUpBeforeClass() throws Exception {
112         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pap.properties");
113         sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testConsoleApi");
114         PolicyDbDao.setJunit(true);
115         dbd = PolicyDbDao.getPolicyDbDaoInstance();
116         PolicyDbDao.setJunit(true);
117         consoleAndApi = new ConsoleAndApiService();
118
119         servletConfig = Mockito.mock(MockServletConfig.class);
120         System.setProperty("com.sun.management.jmxremote.port", "9993");
121         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
122         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
123                 .thenReturn("src/test/resources/xacml.pap.properties");
124         pap = new XACMLPapServlet();
125         pap.init(servletConfig);
126     }
127
128     @AfterClass
129     public static void after() {
130         pap.destroy();
131     }
132
133     /**
134      * Sets the up.
135      *
136      * @throws Exception the exception
137      */
138     @Before
139     public void setUp() throws Exception {
140         httpServletRequest = Mockito.mock(HttpServletRequest.class);
141         httpServletResponse = new MockHttpServletResponse();
142         Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
143         Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
144         CommonClassDaoImpl.setSessionfactory(sessionFactory);
145         PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
146         File tmpGrpDir = folder.newFolder("src", "test", "resources", "grpTest");
147         stdEngine = new StdEngine(tmpGrpDir.toPath());
148         dbd.setPapEngine(stdEngine);
149     }
150
151     @Test
152     public void testGroupCreation() throws IOException {
153         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
154         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
155         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
156         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
157         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
158         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP1);
159         consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP1, logContext, stdEngine);
160         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
161
162     }
163
164     @Test
165     public void testGroupNotExistInDb() throws IOException {
166         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
167         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
168         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
169         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
170         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn("testgrpNotExist");
171         consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, "testgrpNotExist", logContext, stdEngine);
172         assertTrue(HttpServletResponse.SC_INTERNAL_SERVER_ERROR == httpServletResponse.getStatus());
173
174     }
175
176     @Test
177     public void testGroupChange() throws IOException {
178         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
179         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
180         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
181         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
182         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
183         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP2);
184         consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine);
185         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
186
187         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
188         Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(DEFAULT);
189         consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine);
190         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
191
192     }
193
194     @Test
195     public void testPushPolicy() throws Exception {
196         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
197         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
198         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
199         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
200         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
201         Mockito.when(httpServletRequest.getParameter(POLICY_ID)).thenReturn(POLICY_NAME);
202         stdEngine = new StdEngine(Paths.get(PDPS));
203         dbd.setPapEngine(stdEngine);
204         populatePolicyInDb();
205
206         consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine);
207         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
208
209     }
210
211     @Test
212     public void testCreatePolicy() throws Exception {
213         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
214         Mockito.when(httpServletRequest.getMethod()).thenReturn(PUT);
215         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
216         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
217         Mockito.when(httpServletRequest.getParameter("policy")).thenReturn(POLICY_NAME);
218         stdEngine = new StdEngine(Paths.get(PDPS));
219         dbd.setPapEngine(stdEngine);
220
221         consoleAndApi.doAcPut(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine);
222         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
223
224     }
225
226     @Test
227     public void testCreateAndMovePdp() throws Exception {
228         // create two groups, create a pdp on one group and then move it to another group
229         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
230         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
231         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
232         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
233         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
234         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
235         consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
236         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
237         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP5);
238         consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP5, logContext, stdEngine);
239         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
240
241         Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
242         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
243         Mockito.when(httpServletRequest.getMethod()).thenReturn(PUT);
244         httpServletResponse = new MockHttpServletResponse();
245         StdPDP newPdp = new StdPDP("http://localhost:4344/pdp/", "newpdp", "new desc", 9999);
246         ObjectWriter ow = new ObjectMapper().writer();
247         when(httpServletRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(
248                 new ByteArrayInputStream(ow.writeValueAsString(newPdp).getBytes(StandardCharsets.UTF_8))));
249         consoleAndApi.doAcPut(httpServletRequest, httpServletResponse, TESTGRP5, logContext, stdEngine);
250         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
251
252         httpServletRequest = Mockito.mock(HttpServletRequest.class);
253         httpServletResponse = new MockHttpServletResponse();
254         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
255         Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
256         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
257         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
258         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
259         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
260         consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
261         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
262
263         httpServletRequest = Mockito.mock(HttpServletRequest.class);
264         httpServletResponse = new MockHttpServletResponse();
265         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
266         Mockito.when(httpServletRequest.getMethod()).thenReturn("DELETE");
267         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
268         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
269         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
270         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
271         consoleAndApi.doAcDelete(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
272         assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
273
274     }
275
276     @Test
277     public void testGet() throws Exception {
278         stdEngine = new StdEngine(Paths.get("src", "test", "resources", "pdps"));
279         dbd.setPapEngine(stdEngine);
280         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
281         Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
282         Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
283         Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
284         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
285         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn("");
286         consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
287         assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
288         httpServletResponse = new MockHttpServletResponse();
289         Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(DEFAULT);
290         consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
291         assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
292         Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn(null);
293         Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(null);
294         consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
295         assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
296         Mockito.when(httpServletRequest.getParameter("getPDPGroup")).thenReturn(TESTGROUP2);
297         Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGROUP2);
298         consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, TESTGROUP2, logContext, stdEngine);
299         assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
300     }
301
302     private static void populatePolicyInDb() throws IOException, PolicyDbException {
303         CommonClassDaoImpl.setSessionfactory(sessionFactory);
304         PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
305         Policy policyObject = new ConfigPolicy();
306         policyObject.policyAdapter = new PolicyRestAdapter();
307         policyObject.policyAdapter.setConfigName("testpolicyhandle");
308         policyObject.policyAdapter.setPolicyDescription("my description");
309         policyObject.policyAdapter.setConfigBodyData("this is my test config file");
310         policyObject.policyAdapter.setPolicyName("SampleTest1206");
311         policyObject.policyAdapter.setConfigType(ConfigPolicy.OTHER_CONFIG);
312         policyObject.policyAdapter.setPolicyType("Config");
313         policyObject.policyAdapter.setDomainDir("com");
314         policyObject.policyAdapter.setVersion("1");
315         policyObject.policyAdapter.setHighestVersion(1);
316         PolicyType policyTypeObject = new PolicyType();
317         policyObject.policyAdapter.setPolicyData(policyTypeObject);
318
319         PolicyType policyConfig = new PolicyType();
320         policyConfig.setVersion("1");
321         policyConfig.setPolicyId("");
322         policyConfig.setTarget(new TargetType());
323         policyObject.policyAdapter.setData(policyConfig);
324         policyObject.policyAdapter.setParentPath(IOUtils.toString(
325                 ConsoleAndApiServiceTest.class.getClassLoader().getResourceAsStream("Config_SampleTest1206.1.xml")));
326
327         PolicyDbDaoTransaction transaction = dbd.getNewTransaction();
328         transaction.createPolicy(policyObject, API);
329         transaction.commitTransaction();
330     }
331 }