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