Return error when deleting non-existent policy 75/87375/2
authorJim Hahn <jrh3@att.com>
Thu, 9 May 2019 15:28:28 +0000 (11:28 -0400)
committerJim Hahn <jrh3@att.com>
Thu, 9 May 2019 15:50:28 +0000 (11:50 -0400)
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 <jrh3@att.com>
main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java
main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java
main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java
main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java
main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java
main/src/test/java/org/onap/policy/pap/main/rest/e2e/PdpGroupDeleteTest.java

index 6df713d..15620f7 100644 (file)
@@ -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);
index a76d6e1..11b17e4 100644 (file)
@@ -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.
      *
index 72765ce..8ef9b65 100644 (file)
@@ -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);
index 55c6f3d..c171e94 100644 (file)
@@ -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
index f586d16..e7027c5 100644 (file)
@@ -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);
 
         /*
index 463e8d6..68e7028 100644 (file)
@@ -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());
     }
 }