From 2f41a033ed0eae09aa1624d8287687de8a972a5d Mon Sep 17 00:00:00 2001 From: bobbymander Date: Mon, 25 Nov 2019 14:56:57 -0500 Subject: Couple of JUnit additions for PAP-REST Issue-ID: POLICY-2130 Change-Id: Id7252df4a244e579d599a5d2631c60ddb9ccfab3 Signed-off-by: bobbymander --- .../policy/pap/xacml/rest/XACMLPapServletTest.java | 63 +++++++++-- .../rest/policycontroller/PolicyCreationTest.java | 115 +++++++++++++++++++++ 2 files changed, 167 insertions(+), 11 deletions(-) create mode 100644 ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java (limited to 'ONAP-PAP-REST/src/test/java/org/onap') diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/XACMLPapServletTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/XACMLPapServletTest.java index 8e93ee476..420e380b6 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/XACMLPapServletTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/XACMLPapServletTest.java @@ -20,32 +20,46 @@ package org.onap.policy.pap.xacml.rest; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import com.att.research.xacml.api.pap.PAPException; +import com.att.research.xacml.util.XACMLProperties; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.mockrunner.mock.web.MockHttpServletRequest; +import com.mockrunner.mock.web.MockHttpServletResponse; +import java.io.IOException; +import java.util.Properties; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.junit.After; +import org.junit.Before; import org.junit.Test; -import org.onap.policy.pap.xacml.rest.XACMLPapServlet; public class XACMLPapServletTest { + String systemKey = "xacml.properties"; + String oldProperty = System.getProperty(systemKey); + + @Before + public void setup() { + // Set the system property temporarily + System.setProperty(systemKey, "xacml.pap.properties"); + } + @Test public void testSetAndGet() { - String systemKey = "xacml.properties"; String testVal = "testVal"; XACMLPapServlet servlet = new XACMLPapServlet(); - // Set the system property temporarily - String oldProperty = System.getProperty(systemKey); - System.setProperty(systemKey, "xacml.pap.properties"); - + assertNotNull(servlet); assertNotNull(XACMLPapServlet.getConfigHome()); assertNotNull(XACMLPapServlet.getActionHome()); assertEquals(XACMLPapServlet.getPersistenceUnit(), "XACML-PAP-REST"); - // assertNull(XACMLPapServlet.getEmf()); - // assertNull(XACMLPapServlet.getPDPFile()); - // assertNull(XACMLPapServlet.getPAPEngine()); - // assertNull(servlet.getIa()); - XACMLPapServlet.setPapDbDriver(testVal); assertEquals(XACMLPapServlet.getPapDbDriver(), testVal); XACMLPapServlet.setPapDbUrl(testVal); @@ -58,7 +72,34 @@ public class XACMLPapServletTest { assertEquals(XACMLPapServlet.getMsOnapName(), testVal); XACMLPapServlet.setMsPolicyName(testVal); assertEquals(XACMLPapServlet.getMsPolicyName(), testVal); + } + + @Test + public void testMethods() throws ServletException, IOException, PAPException { + XACMLPapServlet servlet = new XACMLPapServlet(); + + ObjectMapper mapper = new ObjectMapper(); + HttpServletResponse response = new MockHttpServletResponse(); + assertThatCode(() -> XACMLPapServlet.mapperWriteValue(mapper, response, "hello")).doesNotThrowAnyException(); + assertThatCode(() -> XACMLPapServlet.mapperWriteValue(null, null, null)).doesNotThrowAnyException(); + assertNull(XACMLPapServlet.getPapUrl()); + + HttpServletRequest request = new MockHttpServletRequest(); + assertThatThrownBy(() -> servlet.doDelete(request, response)).isInstanceOf(NullPointerException.class); + + StringBuffer urlPath = new StringBuffer("http://foo"); + Properties policies = new Properties(); + servlet.populatePolicyURL(urlPath, policies); + assertNull(policies.getProperty("foo.url")); + + policies.setProperty(XACMLProperties.PROP_ROOTPOLICIES, "foo"); + policies.setProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, "bar"); + servlet.populatePolicyURL(urlPath, policies); + assertEquals("http://foo?id=foo", policies.getProperty("foo.url")); + } + @After + public void after() { // Restore the original system property if (oldProperty != null) { System.setProperty(systemKey, oldProperty); diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java new file mode 100644 index 000000000..08c1659f9 --- /dev/null +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreationTest.java @@ -0,0 +1,115 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PAP-REST + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.xacml.rest.policycontroller; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; + +import com.mockrunner.mock.web.MockHttpServletRequest; +import java.util.ArrayList; +import java.util.List; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.policy.rest.adapter.PolicyRestAdapter; +import org.onap.policy.rest.dao.CommonClassDao; +import org.onap.policy.rest.jpa.PolicyVersion; +import org.springframework.http.HttpStatus; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.mock.web.MockHttpServletResponse; + +public class PolicyCreationTest { + @Test + public void testCreation() { + CommonClassDao dao = Mockito.mock(CommonClassDao.class); + PolicyVersion version = new PolicyVersion(); + version.setPolicyName("policyname"); + Mockito.when(dao.getEntityItem(eq(PolicyVersion.class), eq("policyName"), anyString())).thenReturn(version); + PolicyCreation.setCommonClassDao(dao); + assertEquals(dao, PolicyCreation.getCommonClassDao()); + PolicyCreation creation = new PolicyCreation(dao); + assertEquals(dao, PolicyCreation.getCommonClassDao()); + + HttpServletRequest req = new MockHttpServletRequest(); + Exception cause = new Exception("oops"); + HttpMessageNotReadableException exception = new HttpMessageNotReadableException("oops", cause); + assertEquals(HttpStatus.BAD_REQUEST, + creation.messageNotReadableExceptionHandler(req, exception).getStatusCode()); + + HttpServletResponse response = new MockHttpServletResponse(); + PolicyRestAdapter policyData = new PolicyRestAdapter(); + policyData.setPolicyType("Config"); + policyData.setConfigPolicyType("ClosedLoop_Fault"); + policyData.setDomainDir("domain"); + policyData.setPolicyName("policyname"); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + + version.setHigherVersion(1); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + + policyData.setEditPolicy(true); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + + policyData.setEditPolicy(false); + version.setHigherVersion(0); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + + policyData.setEditPolicy(true); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + + version.setHigherVersion(1); + policyData.setConfigPolicyType("Firewall Config"); + assertThatThrownBy(() -> creation.savePolicy(policyData, response)) + .isInstanceOf(IllegalArgumentException.class); + + policyData.setConfigPolicyType("BRMS_Raw"); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + policyData.setConfigPolicyType("BRMS_Param"); + assertThatThrownBy(() -> creation.savePolicy(policyData, response)) + .isInstanceOf(IllegalArgumentException.class); + policyData.setConfigPolicyType("Base"); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + policyData.setConfigPolicyType("ClosedLoop_PM"); + assertThatThrownBy(() -> creation.savePolicy(policyData, response)) + .isInstanceOf(IllegalArgumentException.class); + policyData.setConfigPolicyType("Micro Service"); + assertThatThrownBy(() -> creation.savePolicy(policyData, response)) + .isInstanceOf(IllegalArgumentException.class); + policyData.setConfigPolicyType("Optimization"); + assertThatThrownBy(() -> creation.savePolicy(policyData, response)) + .isInstanceOf(IllegalArgumentException.class); + + policyData.setPolicyType("Action"); + List choices = new ArrayList(); + policyData.setRuleAlgorithmschoices(choices); + assertThatCode(() -> creation.savePolicy(policyData, response)).doesNotThrowAnyException(); + + policyData.setPolicyType("Decision"); + List settings = new ArrayList(); + policyData.setSettings(settings); + assertThatThrownBy(() -> creation.savePolicy(policyData, response)) + .isInstanceOf(IllegalArgumentException.class); + } +} -- cgit 1.2.3-korg