From cccfb11b59becaaf86adc4c88600bd70f2519b0d Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Tue, 23 Apr 2019 13:27:55 -0400 Subject: Validate supported types When a group is created, PAP should verify that the "supported types" exist in the DB. Address potential sonar issue. Address potential sonar issue in similar block of code. Change-Id: Ib830550bc37d4ebe42c8782f3f874e463f3f51c2 Issue-ID: POLICY-1688 Signed-off-by: Jim Hahn --- .../pap/main/rest/depundep/ProviderSuper.java | 11 ++++++ .../rest/depundep/TestPdpGroupDeployProvider.java | 27 +++++++++++++-- .../pap/main/rest/depundep/TestProviderBase.java | 11 ++++++ .../pap/main/rest/depundep/TestSessionData.java | 40 ++++++++++++++++++++-- 4 files changed, 85 insertions(+), 4 deletions(-) (limited to 'main/src/test/java') diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java index 256d3af0..2fca6848 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java @@ -48,6 +48,7 @@ import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper; import org.onap.policy.pap.main.comm.PdpModifyRequestMap; @@ -233,6 +234,16 @@ public class ProviderSuper { return loadFile(fileName, ToscaPolicy.class); } + /** + * Loads a policy type. + * + * @param fileName name of the file from which to load + * @return a policy type + */ + protected ToscaPolicyType loadPolicyType(String fileName) { + return loadFile(fileName, ToscaPolicyType.class); + } + /** * Loads an object from a JSON file. * diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java index 406345c6..81664176 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java @@ -83,6 +83,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { super.setUp(); when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); + when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json"))); prov = new PdpGroupDeployProvider(); } @@ -356,14 +357,36 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertGroupUpdateOnly(group); } + @Test + public void testAddSubGroup_ValidationPolicyTypeNotFound() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy type"); + } + + @Test + public void testAddSubGroup_ValidationPolicyTypeDaoEx() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); + when(dao.getPolicyTypeList(any(), any())).thenThrow(exc); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc); + } + @Test public void testAddSubGroup_ValidationPolicyNotFound() throws Exception { PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - PfModelException exc = new PfModelException(Status.NOT_FOUND, EXPECTED_EXCEPTION); - when(dao.getFilteredPolicyList(any())).thenThrow(exc); + when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy"); } 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 e8ddd821..55c6f3d4 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 @@ -150,6 +150,17 @@ public class TestProviderBase extends ProviderSuper { .hasCause(exc); } + @Test + public void testGetPolicy_NotFound() throws Exception { + 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); + }); + } + @Test public void testGetGroup() throws Exception { when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("getGroupDao.json")) 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 17acd5a2..f586d167 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 @@ -51,6 +51,7 @@ import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; public class TestSessionData extends ProviderSuper { @@ -90,6 +91,32 @@ public class TestSessionData extends ProviderSuper { session = new SessionData(dao); } + @Test + public void testGetPolicyType() throws Exception { + ToscaPolicyType policy1 = makePolicyType(POLICY_TYPE, POLICY_TYPE_VERSION); + when(dao.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenReturn(Arrays.asList(policy1)); + + assertSame(policy1, session.getPolicyType(type)); + + // retrieve a second time - should use cache + assertSame(policy1, session.getPolicyType(type)); + } + + @Test + public void testGetPolicyType_NotFound() throws Exception { + when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); + + assertNull(session.getPolicyType(type)); + } + + @Test + public void testGetPolicyType_DaoEx() throws Exception { + PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION); + when(dao.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenThrow(ex); + + assertThatThrownBy(() -> session.getPolicyType(type)).isSameAs(ex); + } + @Test public void testGetPolicy_NullVersion() throws Exception { ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); @@ -148,12 +175,12 @@ public class TestSessionData extends ProviderSuper { public void testGetPolicy_NotFound() throws Exception { when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); - assertThatThrownBy(() -> session.getPolicy(ident)).hasMessage("cannot find policy: myPolicy 1.2.3"); + assertNull(session.getPolicy(ident)); } @Test public void testGetPolicy_DaoEx() throws Exception { - PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, "expected exception"); + PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION); when(dao.getFilteredPolicyList(any())).thenThrow(ex); assertThatThrownBy(() -> session.getPolicy(ident)).isSameAs(ex); @@ -270,6 +297,15 @@ public class TestSessionData extends ProviderSuper { assertEquals(Arrays.asList(change1, change2, change3).toString(), lst.toString()); } + private ToscaPolicyType makePolicyType(String name, String version) { + ToscaPolicyType type = new ToscaPolicyType(); + + type.setName(name); + type.setVersion(version); + + return type; + } + private ToscaPolicy makePolicy(String name, String version) { ToscaPolicy policy = new ToscaPolicy(); -- cgit 1.2.3-korg