diff options
Diffstat (limited to 'ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler')
3 files changed, 125 insertions, 29 deletions
diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandlerTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandlerTest.java index 008f2629e..885230da4 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandlerTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DeleteHandlerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-REST * ================================================================================ - * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2020 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. @@ -23,11 +23,15 @@ package org.onap.policy.pap.xacml.rest.handler; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; + import com.mockrunner.mock.web.MockHttpServletRequest; import com.mockrunner.mock.web.MockHttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.hibernate.Session; @@ -41,14 +45,19 @@ import org.onap.policy.common.logging.OnapLoggingContext; import org.onap.policy.pap.xacml.rest.XACMLPapServlet; import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl; import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController; +import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.PolicyEntity; +import org.onap.policy.rest.jpa.PolicyVersion; import org.onap.policy.xacml.api.pap.PAPPolicyEngine; import org.onap.policy.xacml.std.pap.StdEngine; import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) +@PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"}) +@PrepareForTest(DeleteHandler.class) public class DeleteHandlerTest { @Before public void setUp() { @@ -93,31 +102,114 @@ public class DeleteHandlerTest { PolicyElasticSearchController controller = Mockito.mock(PolicyElasticSearchController.class); PowerMockito.whenNew(PolicyElasticSearchController.class).withNoArguments().thenReturn(controller); - // Test deletion from PAP - MockHttpServletResponse response = new MockHttpServletResponse(); - try { - handler.doApiDeleteFromPap(request, response); - } catch (Exception ex) { - fail("Not expecting an exception: " + ex); - } + // Test deletion from PAP + MockHttpServletResponse response = new MockHttpServletResponse(); + handler.doApiDeleteFromPap(request, response); // Test deletion from PDP OnapLoggingContext loggingContext = Mockito.mock(OnapLoggingContext.class); - try { - handler.doApiDeleteFromPdp(request, response, loggingContext); - } - catch (Exception ex) { - fail("Not expecting an exception: " + ex); - } - - // Test delete entity - PolicyEntity policyEntity = new PolicyEntity(); - policyEntity.setPolicyName("testVal"); - String result = DeleteHandler.deletePolicyEntityData(policyEntity); - assertEquals(result, "success"); - - // Test check entity - List<?> peResult = Collections.emptyList(); - assertEquals(DeleteHandler.checkPolicyGroupEntity(peResult), false); - } + handler.doApiDeleteFromPdp(request, response, loggingContext); + + // Test delete entity + PolicyEntity policyEntity = new PolicyEntity(); + policyEntity.setPolicyName("testVal"); + String result = DeleteHandler.deletePolicyEntityData(policyEntity); + assertEquals(result, "success"); + + // Test check entity + List<?> peResult = Collections.emptyList(); + assertEquals(DeleteHandler.checkPolicyGroupEntity(peResult), false); + } + + @Test + public void testDoDeletePap() throws IOException { + CommonClassDao dao = Mockito.mock(CommonClassDao.class); + DeleteHandler handler = new DeleteHandler(dao); + + // Request #1 + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); + MockHttpServletResponse response = new MockHttpServletResponse(); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #2 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Action_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #3 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #4 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Bar_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #5 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #6 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Action_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #7 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Mock dao + List<PolicyVersion> pePVs = new ArrayList<PolicyVersion>(); + PolicyVersion pv = new PolicyVersion(); + pePVs.add(pv); + List<Object> peObjs = new ArrayList<Object>(pePVs); + List<PolicyEntity> peEnts = new ArrayList<PolicyEntity>(); + PolicyEntity peEnt = new PolicyEntity(); + peEnts.add(peEnt); + List<Object> peEntObjs = new ArrayList<Object>(peEnts); + Mockito.when(dao.getDataByQuery(eq("Select p from PolicyVersion p where p.policyName=:pname"), any())) + .thenReturn(peObjs); + Mockito.when( + dao.getDataByQuery(eq("SELECT p FROM PolicyEntity p WHERE p.policyName=:pName and p.scope=:pScope"), any())) + .thenReturn(peEntObjs); + + // Request #8 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + + // Request #9 + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Decision_name.1.xml\", \"deleteCondition\": \"Current Version\"\n, \"deleteCondition\": \"All Versions\"}\n"); + handler.doApiDeleteFromPap(request, response); + assertTrue(response.containsHeader("error")); + } + + @Test(expected = NullPointerException.class) + public void testDoDeletePdp() throws IOException { + CommonClassDao dao = Mockito.mock(CommonClassDao.class); + DeleteHandler handler = new DeleteHandler(dao); + OnapLoggingContext loggingContext = new OnapLoggingContext(); + + // Mock request + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setBodyContent( + "{\n\"PAPPolicyType\": \"StdPAPPolicy\", \"policyName\": \"foo.Config_name.1.xml\", \"deleteCondition\": \"All Versions\"\n}\n"); + MockHttpServletResponse response = new MockHttpServletResponse(); + + handler.doApiDeleteFromPdp(request, response, loggingContext); + } } diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DictionaryHandlerTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DictionaryHandlerTest.java index ca6860ae8..8c35c17ef 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DictionaryHandlerTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/DictionaryHandlerTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP-PAP-REST * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Modifications Copyright (C) 2019 Samsung * ================================================================================ @@ -23,6 +23,7 @@ package org.onap.policy.pap.xacml.rest.handler; import static org.junit.Assert.assertNull; + import org.junit.Test; public class DictionaryHandlerTest { diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/PushPolicyHandlerTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/PushPolicyHandlerTest.java index 3daed2455..ae6235f0e 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/PushPolicyHandlerTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/handler/PushPolicyHandlerTest.java @@ -25,13 +25,17 @@ import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; + import com.mockrunner.mock.web.MockHttpServletResponse; + import java.io.File; import java.util.ArrayList; import java.util.List; + import javax.script.SimpleBindings; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + import org.junit.Test; import org.mockito.Mockito; import org.onap.policy.rest.dao.CommonClassDao; @@ -40,7 +44,6 @@ import org.onap.policy.xacml.api.pap.OnapPDPGroup; import org.onap.policy.xacml.std.pap.StdPDPGroup; import org.onap.policy.xacml.std.pap.StdPDPPolicy; - public class PushPolicyHandlerTest { @Test public void testGetsAndChecks() { |