2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest;
23 import static org.junit.Assert.assertTrue;
24 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;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
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.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Rule;
46 import org.junit.Test;
47 import org.junit.rules.TemporaryFolder;
48 import org.mockito.Mockito;
49 import org.onap.policy.common.logging.ONAPLoggingContext;
50 import org.onap.policy.pap.xacml.rest.components.ConfigPolicy;
51 import org.onap.policy.pap.xacml.rest.components.Policy;
52 import org.onap.policy.pap.xacml.rest.components.PolicyDBDao;
53 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTest;
54 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTransaction;
55 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
56 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
57 import org.onap.policy.rest.adapter.PolicyRestAdapter;
58 import org.onap.policy.rest.dao.PolicyDBException;
59 import org.onap.policy.xacml.std.pap.StdEngine;
60 import org.onap.policy.xacml.std.pap.StdPDP;
61 import org.springframework.mock.web.DelegatingServletInputStream;
62 import org.springframework.mock.web.MockHttpServletResponse;
63 import org.springframework.mock.web.MockServletConfig;
65 public class ConsoleAndApiServiceTest {
66 private static final String TESTGRP5 = "testgrp5";
67 private static final String POLICY_ID = "policyId";
68 private static final String TESTGRP1 = "testgrp1";
69 private static final String TESTGROUP2 = "testgroup2";
70 private static final String DEFAULT = "default";
71 private static final String PDPS = "pdps";
72 private static final String TESTGRP2 = "testgrp2";
73 private static final String POLICY_NAME = "com.Config_SampleTest1206.1.xml";
74 private static final String PUT = "PUT";
75 private static final String POST = "POST";
76 private static final String DEVL = "DEVL";
77 private static final String TESTGRP4 = "testgrp4";
78 private static final String API2 = "api";
79 private static final String API = "API";
80 private static final String GROUP_DESCRIPTION = "groupDescription";
81 private static final String GROUP_NAME = "groupName";
82 private static final String PDP_ID = "pdpId";
83 private static final String USER_ID = "userId";
84 private static final String APIFLAG = "apiflag";
85 private static final String ENVIRONMENT_HEADER = "Environment";
86 private static final ONAPLoggingContext logContext = Mockito.mock(ONAPLoggingContext.class);
87 private static PolicyDBDao dbd;
88 private static StdEngine stdEngine = null;
89 private static SessionFactory sessionFactory = null;
90 private static List<String> headers = new ArrayList<>();
91 private static ConsoleAndApiService consoleAndApi;
92 private static MockServletConfig servletConfig;
93 private static XACMLPapServlet pap;
94 private HttpServletRequest httpServletRequest;
95 private HttpServletResponse httpServletResponse;
97 public TemporaryFolder folder = new TemporaryFolder();
100 * Sets the up before class.
102 * @throws Exception the exception
105 public static void setUpBeforeClass() throws Exception {
106 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pap.properties");
107 sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testConsoleApi");
108 PolicyDBDao.setJunit(true);
109 dbd = PolicyDBDao.getPolicyDBDaoInstance();
110 PolicyDBDao.setJunit(true);
111 consoleAndApi = new ConsoleAndApiService();
113 servletConfig = Mockito.mock(MockServletConfig.class);
114 System.setProperty("com.sun.management.jmxremote.port", "9993");
115 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
116 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
117 .thenReturn("src/test/resources/xacml.pap.properties");
118 pap = new XACMLPapServlet();
119 pap.init(servletConfig);
126 * @throws Exception the exception
129 public void setUp() throws Exception {
130 httpServletRequest = Mockito.mock(HttpServletRequest.class);
131 httpServletResponse = new MockHttpServletResponse();
132 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
133 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
134 CommonClassDaoImpl.setSessionfactory(sessionFactory);
135 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
136 File tmpGrpDir = folder.newFolder("src", "test", "resources", "grpTest");
137 stdEngine = new StdEngine(tmpGrpDir.toPath());
138 dbd.setPapEngine(stdEngine);
142 public void testGroupCreation() throws IOException {
143 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
144 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
145 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
146 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
147 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
148 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP1);
149 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP1, logContext, stdEngine);
150 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
155 public void testGroupNotExistInDb() throws IOException {
156 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
157 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
158 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
159 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
160 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn("testgrpNotExist");
161 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, "testgrpNotExist", logContext, stdEngine);
162 assertTrue(HttpServletResponse.SC_INTERNAL_SERVER_ERROR == httpServletResponse.getStatus());
167 public void testGroupChange() throws IOException {
168 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
169 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
170 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
171 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
172 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
173 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP2);
174 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine);
175 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
177 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
178 Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(DEFAULT);
179 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine);
180 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
185 public void testPushPolicy() throws Exception {
186 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
187 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
188 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
189 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
190 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
191 Mockito.when(httpServletRequest.getParameter(POLICY_ID)).thenReturn(POLICY_NAME);
192 stdEngine = new StdEngine(Paths.get(PDPS));
193 dbd.setPapEngine(stdEngine);
194 populatePolicyInDb();
196 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine);
197 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
202 public void testCreatePolicy() throws Exception {
203 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
204 Mockito.when(httpServletRequest.getMethod()).thenReturn(PUT);
205 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
206 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
207 Mockito.when(httpServletRequest.getParameter("policy")).thenReturn(POLICY_NAME);
208 stdEngine = new StdEngine(Paths.get(PDPS));
209 dbd.setPapEngine(stdEngine);
211 consoleAndApi.doAcPut(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine);
212 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
217 public void testCreateAndMovePdp() throws Exception {
218 // create two groups, create a pdp on one group and then move it to another group
219 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
220 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
221 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
222 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
223 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
224 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
225 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
226 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
227 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP5);
228 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP5, logContext, stdEngine);
229 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
232 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
233 Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
234 Mockito.when(httpServletRequest.getMethod()).thenReturn(PUT);
235 httpServletResponse = new MockHttpServletResponse();
236 StdPDP newPdp = new StdPDP("http://localhost:4344/pdp/", "newpdp", "new desc", 9999);
237 ObjectWriter ow = new ObjectMapper().writer();
238 when(httpServletRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(
239 new ByteArrayInputStream(ow.writeValueAsString(newPdp).getBytes(StandardCharsets.UTF_8))));
240 consoleAndApi.doAcPut(httpServletRequest, httpServletResponse, TESTGRP5, logContext, stdEngine);
241 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
244 httpServletRequest = Mockito.mock(HttpServletRequest.class);
245 httpServletResponse = new MockHttpServletResponse();
246 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
247 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
248 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
249 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
250 Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
251 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
252 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
253 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
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());
271 public void testGet() throws Exception {
272 stdEngine = new StdEngine(Paths.get("src", "test", "resources", "pdps"));
273 dbd.setPapEngine(stdEngine);
274 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
275 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
276 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
277 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
278 Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
279 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn("");
280 consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
281 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
282 httpServletResponse = new MockHttpServletResponse();
283 Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(DEFAULT);
284 consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
285 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
286 Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn(null);
287 Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(null);
288 consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, "", logContext, stdEngine);
289 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
290 Mockito.when(httpServletRequest.getParameter("getPDPGroup")).thenReturn(TESTGROUP2);
291 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGROUP2);
292 consoleAndApi.doAcGet(httpServletRequest, httpServletResponse, TESTGROUP2, logContext, stdEngine);
293 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
296 private static void populatePolicyInDb() throws IOException, PolicyDBException {
297 CommonClassDaoImpl.setSessionfactory(sessionFactory);
298 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
299 Policy policyObject = new ConfigPolicy();
300 policyObject.policyAdapter = new PolicyRestAdapter();
301 policyObject.policyAdapter.setConfigName("testpolicyhandle");
302 policyObject.policyAdapter.setPolicyDescription("my description");
303 policyObject.policyAdapter.setConfigBodyData("this is my test config file");
304 policyObject.policyAdapter.setPolicyName("SampleTest1206");
305 policyObject.policyAdapter.setConfigType(ConfigPolicy.OTHER_CONFIG);
306 policyObject.policyAdapter.setPolicyType("Config");
307 policyObject.policyAdapter.setDomainDir("com");
308 policyObject.policyAdapter.setVersion("1");
309 policyObject.policyAdapter.setHighestVersion(1);
310 PolicyType policyTypeObject = new PolicyType();
311 policyObject.policyAdapter.setPolicyData(policyTypeObject);
313 PolicyType policyConfig = new PolicyType();
314 policyConfig.setVersion("1");
315 policyConfig.setPolicyId("");
316 policyConfig.setTarget(new TargetType());
317 policyObject.policyAdapter.setData(policyConfig);
318 policyObject.policyAdapter.setParentPath(IOUtils.toString(
319 ConsoleAndApiServiceTest.class.getClassLoader().getResourceAsStream("Config_SampleTest1206.1.xml")));
321 PolicyDBDaoTransaction transaction = dbd.getNewTransaction();
322 transaction.createPolicy(policyObject, API);
323 transaction.commitTransaction();