2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-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.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
29 import com.att.research.xacml.api.pap.PAPException;
30 import com.att.research.xacml.util.XACMLProperties;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.mockrunner.mock.web.MockHttpServletRequest;
33 import com.mockrunner.mock.web.MockHttpServletResponse;
34 import java.io.IOException;
35 import java.util.Properties;
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
43 public class XACMLPapServletTest {
44 String systemKey = "xacml.properties";
45 String oldProperty = System.getProperty(systemKey);
49 // Set the system property temporarily
50 System.setProperty(systemKey, "xacml.pap.properties");
54 public void testSetAndGet() {
55 String testVal = "testVal";
56 XACMLPapServlet servlet = new XACMLPapServlet();
58 assertNotNull(servlet);
59 assertNotNull(XACMLPapServlet.getConfigHome());
60 assertNotNull(XACMLPapServlet.getActionHome());
61 assertEquals(XACMLPapServlet.getPersistenceUnit(), "XACML-PAP-REST");
63 XACMLPapServlet.setPapDbDriver(testVal);
64 assertEquals(XACMLPapServlet.getPapDbDriver(), testVal);
65 XACMLPapServlet.setPapDbUrl(testVal);
66 assertEquals(XACMLPapServlet.getPapDbUrl(), testVal);
67 XACMLPapServlet.setPapDbUser(testVal);
68 assertEquals(XACMLPapServlet.getPapDbUser(), testVal);
69 XACMLPapServlet.setPapDbPassword(testVal);
70 assertEquals(XACMLPapServlet.getPapDbPassword(), testVal);
71 XACMLPapServlet.setMsOnapName(testVal);
72 assertEquals(XACMLPapServlet.getMsOnapName(), testVal);
73 XACMLPapServlet.setMsPolicyName(testVal);
74 assertEquals(XACMLPapServlet.getMsPolicyName(), testVal);
78 public void testMethods() throws ServletException, IOException, PAPException {
79 XACMLPapServlet servlet = new XACMLPapServlet();
81 ObjectMapper mapper = new ObjectMapper();
82 HttpServletResponse response = new MockHttpServletResponse();
83 assertThatCode(() -> XACMLPapServlet.mapperWriteValue(mapper, response, "hello")).doesNotThrowAnyException();
84 assertThatCode(() -> XACMLPapServlet.mapperWriteValue(null, null, null)).doesNotThrowAnyException();
85 assertNull(XACMLPapServlet.getPapUrl());
87 HttpServletRequest request = new MockHttpServletRequest();
88 assertThatThrownBy(() -> servlet.doDelete(request, response)).isInstanceOf(NullPointerException.class);
90 StringBuffer urlPath = new StringBuffer("http://foo");
91 Properties policies = new Properties();
92 servlet.populatePolicyURL(urlPath, policies);
93 assertNull(policies.getProperty("foo.url"));
95 policies.setProperty(XACMLProperties.PROP_ROOTPOLICIES, "foo");
96 policies.setProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, "bar");
97 servlet.populatePolicyURL(urlPath, policies);
98 assertEquals("http://foo?id=foo", policies.getProperty("foo.url"));
102 public void after() {
103 // Restore the original system property
104 if (oldProperty != null) {
105 System.setProperty(systemKey, oldProperty);
107 System.clearProperty(systemKey);