From 54cc5c118de45023692c72388b6389d184a658f7 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 9 May 2019 11:28:28 -0400 Subject: Return error when deleting non-existent policy When undeploying a policy, if a policy's type is found in a group, but the policy is not, then PAP returns 200. It should return an error instead. Change-Id: I88273fdbd5d49095e8248ab9267cccc529af0ae0 Issue-ID: POLICY-1758 Signed-off-by: Jim Hahn --- .../main/rest/depundep/PdpGroupDeleteProvider.java | 5 +++++ .../policy/pap/main/rest/depundep/SessionData.java | 9 +++++++++ .../rest/depundep/TestPdpGroupDeleteProvider.java | 21 ++++++++++++++------- .../pap/main/rest/depundep/TestProviderBase.java | 7 +++---- .../pap/main/rest/depundep/TestSessionData.java | 7 ++++++- .../pap/main/rest/e2e/PdpGroupDeleteTest.java | 8 ++++---- 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java index 6df713d3..15620f7b 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java @@ -104,6 +104,11 @@ public class PdpGroupDeleteProvider extends ProviderBase { try { processPolicy(data, ident); + if (data.isUnchanged()) { + throw new PfModelException(Status.BAD_REQUEST, "policy does not appear in any PDP group: " + + ident.getName() + " " + ident.getVersion()); + } + } catch (PfModelException | RuntimeException e) { // no need to log the error object here, as it will be logged by the invoker logger.warn("failed to undeploy policy: {}", ident); diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java index a76d6e13..11b17e45 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java @@ -206,6 +206,15 @@ public class SessionData { pdpRequests.compute(change.getName(), (name, data) -> Pair.of((data == null ? null : data.getLeft()), change)); } + /** + * Determines if any changes were made due to the REST call. + * + * @return {@code true} if nothing was changed, {@code false} if something was changed + */ + public boolean isUnchanged() { + return pdpRequests.isEmpty(); + } + /** * Gets the accumulated PDP requests. * diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java index 72765ce6..8ef9b653 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java @@ -113,10 +113,9 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { @Test public void testDeleteGroup_NotFound() throws Exception { assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isInstanceOf(PfModelException.class) - .hasMessage("group not found").matches(thr -> { - PfModelException ex = (PfModelException) thr; - return (ex.getErrorResponse().getResponseCode() == Status.NOT_FOUND); - }); + .hasMessage("group not found") + .extracting(ex -> ((PfModelException) ex).getErrorResponse().getResponseCode()) + .isEqualTo(Status.NOT_FOUND); } @Test @@ -146,7 +145,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { } @Test - public void testUndeploy_testDeletePolicy() throws Exception { + public void testUndeploy_testUndeployPolicy() throws Exception { prov.undeploy(optIdent); } @@ -186,7 +185,15 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { } @Test - public void testDeletePolicy_DaoEx() throws Exception { + public void testUndeployPolicy_NotFound() throws Exception { + when(session.isUnchanged()).thenReturn(true); + + assertThatThrownBy(() -> prov.undeploy(optIdent)).isInstanceOf(PfModelException.class) + .hasMessage("policy does not appear in any PDP group: policyA null"); + } + + @Test + public void testUndeployPolicy_DaoEx() throws Exception { PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); prov = spy(prov); @@ -196,7 +203,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { } @Test - public void testDeletePolicy_RtEx() throws Exception { + public void testUndeployPolicy_RtEx() throws Exception { RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION); prov = spy(prov); diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java index 55c6f3d4..c171e946 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java @@ -155,10 +155,9 @@ public class TestProviderBase extends ProviderSuper { when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); assertThatThrownBy(() -> prov.process(loadRequest(), this::handle)).isInstanceOf(PfModelRuntimeException.class) - .hasMessage("cannot find policy: policyA 1.2.3").matches(thr -> { - PfModelRuntimeException exc = (PfModelRuntimeException) thr; - return (exc.getErrorResponse().getResponseCode() == Status.NOT_FOUND); - }); + .hasMessage("cannot find policy: policyA 1.2.3") + .extracting(ex -> ((PfModelRuntimeException) ex).getErrorResponse().getResponseCode()) + .isEqualTo(Status.NOT_FOUND); } @Test diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java index f586d167..e7027c51 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java @@ -24,8 +24,10 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -187,7 +189,9 @@ public class TestSessionData extends ProviderSuper { } @Test - public void testAddRequests_testGetPdpStateChanges_testGetPdpUpdates() { + public void testIsUnchanged_testAddRequests_testGetPdpStateChanges_testGetPdpUpdates() { + assertTrue(session.isUnchanged()); + // pre-load with a update and state-change for other PDPs PdpUpdate update2 = makeUpdate(PDP2); session.addUpdate(update2); @@ -199,6 +203,7 @@ public class TestSessionData extends ProviderSuper { PdpUpdate update = makeUpdate(PDP1); PdpStateChange change = makeStateChange(PDP1); session.addRequests(update, change); + assertFalse(session.isUnchanged()); verifyRequests(update, update2, change, change3); /* diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java index 463e8d6e..68e70287 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java @@ -124,8 +124,8 @@ public class PdpGroupDeleteTest extends End2EndBase { rawresp = invocationBuilder.delete(); resp = rawresp.readEntity(PdpGroupDeleteResponse.class); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - assertNull(resp.getErrorDetails()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); + assertEquals("policy does not appear in any PDP group: onap.restart.tcaB null", resp.getErrorDetails()); } @Test @@ -162,7 +162,7 @@ public class PdpGroupDeleteTest extends End2EndBase { rawresp = invocationBuilder.delete(); resp = rawresp.readEntity(PdpGroupDeleteResponse.class); - assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - assertNull(resp.getErrorDetails()); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); + assertEquals("policy does not appear in any PDP group: onap.restart.tcaC 1.0.0", resp.getErrorDetails()); } } -- cgit 1.2.3-korg