2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.xacml.rest;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.when;
27 import com.att.research.xacml.util.XACMLProperties;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.ObjectWriter;
31 import java.io.ByteArrayInputStream;
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;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
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;
46 import org.apache.commons.io.IOUtils;
47 import org.hibernate.SessionFactory;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.junit.rules.TemporaryFolder;
53 import org.mockito.Mockito;
54 import org.onap.policy.common.logging.OnapLoggingContext;
55 import org.onap.policy.pap.xacml.rest.components.ConfigPolicy;
56 import org.onap.policy.pap.xacml.rest.components.Policy;
57 import org.onap.policy.pap.xacml.rest.components.PolicyDbDao;
58 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTest;
59 import org.onap.policy.pap.xacml.rest.components.PolicyDbDaoTransaction;
60 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
61 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
62 import org.onap.policy.rest.adapter.PolicyRestAdapter;
63 import org.onap.policy.rest.dao.PolicyDbException;
64 import org.onap.policy.xacml.std.pap.StdEngine;
65 import org.onap.policy.xacml.std.pap.StdPDP;
66 import org.springframework.mock.web.DelegatingServletInputStream;
67 import org.springframework.mock.web.MockHttpServletResponse;
68 import org.springframework.mock.web.MockServletConfig;
70 public class ConsoleAndApiServiceTest {
71 private static final String TESTGRP5 = "testgrp5";
72 private static final String POLICY_ID = "policyId";
73 private static final String TESTGRP1 = "testgrp1";
74 private static final String TESTGROUP2 = "testgroup2";
75 private static final String DEFAULT = "default";
76 private static final String PDPS = "pdps";
77 private static final String TESTGRP2 = "testgrp2";
78 private static final String POLICY_NAME = "com.Config_SampleTest1206.1.xml";
79 private static final String PUT = "PUT";
80 private static final String POST = "POST";
81 private static final String DEVL = "DEVL";
82 private static final String TESTGRP4 = "testgrp4";
83 private static final String API2 = "api";
84 private static final String API = "API";
85 private static final String GROUP_DESCRIPTION = "groupDescription";
86 private static final String GROUP_NAME = "groupName";
87 private static final String PDP_ID = "pdpId";
88 private static final String USER_ID = "userId";
89 private static final String APIFLAG = "apiflag";
90 private static final String ENVIRONMENT_HEADER = "Environment";
91 private static final OnapLoggingContext logContext = Mockito.mock(OnapLoggingContext.class);
92 private static PolicyDbDao dbd;
93 private static StdEngine stdEngine = null;
94 private static SessionFactory sessionFactory = null;
95 private static List<String> headers = new ArrayList<>();
96 private static ConsoleAndApiService consoleAndApi;
97 private static MockServletConfig servletConfig;
98 private static XACMLPapServlet pap;
99 private HttpServletRequest httpServletRequest;
100 private HttpServletResponse httpServletResponse;
102 public TemporaryFolder folder = new TemporaryFolder();
105 * Sets the up before class.
107 * @throws Exception the exception
110 public static void setUpBeforeClass() throws Exception {
111 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pap.properties");
112 sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("testConsoleApi");
113 PolicyDbDao.setJunit(true);
114 dbd = PolicyDbDao.getPolicyDbDaoInstance();
115 PolicyDbDao.setJunit(true);
116 consoleAndApi = new ConsoleAndApiService();
118 servletConfig = Mockito.mock(MockServletConfig.class);
119 System.setProperty("com.sun.management.jmxremote.port", "9993");
120 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
121 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
122 .thenReturn("src/test/resources/xacml.pap.properties");
123 pap = new XACMLPapServlet();
124 pap.init(servletConfig);
130 * @throws Exception the exception
133 public void setUp() throws Exception {
134 httpServletRequest = Mockito.mock(HttpServletRequest.class);
135 httpServletResponse = new MockHttpServletResponse();
136 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
137 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
138 CommonClassDaoImpl.setSessionfactory(sessionFactory);
139 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
140 File tmpGrpDir = folder.newFolder("src", "test", "resources", "grpTest");
141 stdEngine = new StdEngine(tmpGrpDir.toPath());
142 dbd.setPapEngine(stdEngine);
146 public void testGroupCreation() throws IOException {
147 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
148 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
149 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
150 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
151 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
152 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP1);
153 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP1, logContext, stdEngine);
154 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
159 public void testGroupNotExistInDb() throws IOException {
160 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
161 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
162 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
163 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
164 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn("testgrpNotExist");
165 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, "testgrpNotExist", logContext, stdEngine);
166 assertTrue(HttpServletResponse.SC_INTERNAL_SERVER_ERROR == httpServletResponse.getStatus());
171 public void testGroupChange() throws IOException {
172 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
173 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
174 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
175 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
176 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
177 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP2);
178 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine);
179 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
181 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
182 Mockito.when(httpServletRequest.getParameter(DEFAULT)).thenReturn(DEFAULT);
183 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP2, logContext, stdEngine);
184 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
189 public void testPushPolicy() throws Exception {
190 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
191 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
192 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
193 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
194 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
195 Mockito.when(httpServletRequest.getParameter(POLICY_ID)).thenReturn(POLICY_NAME);
196 stdEngine = new StdEngine(Paths.get(PDPS));
197 dbd.setPapEngine(stdEngine);
198 populatePolicyInDb();
200 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine);
201 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
206 public void testCreatePolicy() throws Exception {
207 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
208 Mockito.when(httpServletRequest.getMethod()).thenReturn(PUT);
209 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
210 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
211 Mockito.when(httpServletRequest.getParameter("policy")).thenReturn(POLICY_NAME);
212 stdEngine = new StdEngine(Paths.get(PDPS));
213 dbd.setPapEngine(stdEngine);
215 consoleAndApi.doAcPut(httpServletRequest, httpServletResponse, DEFAULT, logContext, stdEngine);
216 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
221 public void testCreateAndMovePdp() throws Exception {
222 // create two groups, create a pdp on one group and then move it to another group
223 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
224 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
225 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
226 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
227 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn("test");
228 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
229 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
230 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
231 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP5);
232 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP5, logContext, stdEngine);
233 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
235 Mockito.when(httpServletRequest.getParameter(GROUP_DESCRIPTION)).thenReturn(null);
236 Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
237 Mockito.when(httpServletRequest.getMethod()).thenReturn(PUT);
238 httpServletResponse = new MockHttpServletResponse();
239 StdPDP newPdp = new StdPDP("http://localhost:4344/pdp/", "newpdp", "new desc", 9999);
240 ObjectWriter ow = new ObjectMapper().writer();
241 when(httpServletRequest.getInputStream()).thenReturn(new DelegatingServletInputStream(
242 new ByteArrayInputStream(ow.writeValueAsString(newPdp).getBytes(StandardCharsets.UTF_8))));
243 consoleAndApi.doAcPut(httpServletRequest, httpServletResponse, TESTGRP5, logContext, stdEngine);
244 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
246 httpServletRequest = Mockito.mock(HttpServletRequest.class);
247 httpServletResponse = new MockHttpServletResponse();
248 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
249 Mockito.when(httpServletRequest.getMethod()).thenReturn(POST);
250 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
251 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
252 Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
253 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
254 consoleAndApi.doAcPost(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
255 assertTrue(HttpServletResponse.SC_NO_CONTENT == httpServletResponse.getStatus());
257 httpServletRequest = Mockito.mock(HttpServletRequest.class);
258 httpServletResponse = new MockHttpServletResponse();
259 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn(DEVL);
260 Mockito.when(httpServletRequest.getMethod()).thenReturn("DELETE");
261 Mockito.when(httpServletRequest.getParameter(APIFLAG)).thenReturn(API2);
262 Mockito.when(httpServletRequest.getParameter(USER_ID)).thenReturn(API);
263 Mockito.when(httpServletRequest.getParameter(PDP_ID)).thenReturn("http://localhost:4344/pdp/");
264 Mockito.when(httpServletRequest.getParameter(GROUP_NAME)).thenReturn(TESTGRP4);
265 consoleAndApi.doAcDelete(httpServletRequest, httpServletResponse, TESTGRP4, logContext, stdEngine);
266 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();