diff options
author | jh7358 <jh7358@att.com> | 2019-10-16 11:51:46 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-10-29 11:09:10 -0400 |
commit | 4f3fe21aa98a196ef1ddd62e1ac84a6c2e4c13cf (patch) | |
tree | 682a1ce5779a094318a0143aeb993ffb4d3ff59d /main/src/test/java | |
parent | 45c8e61888711a3d6f5913d1d7ddf640ff995b12 (diff) |
Generate notifications when policies change
Updated existing PAP code to make use of new notification classes.
Change-Id: I4637ad92ac4f432f215cfc837e672c75074d88b5
Issue-ID: POLICY-1841
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'main/src/test/java')
13 files changed, 275 insertions, 32 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/CommonRequestBase.java b/main/src/test/java/org/onap/policy/pap/main/comm/CommonRequestBase.java index 0337161e..f10abdda 100644 --- a/main/src/test/java/org/onap/policy/pap/main/comm/CommonRequestBase.java +++ b/main/src/test/java/org/onap/policy/pap/main/comm/CommonRequestBase.java @@ -49,6 +49,7 @@ import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper; import org.onap.policy.pap.main.comm.msgdata.RequestListener; import org.onap.policy.pap.main.comm.msgdata.StateChangeReq; import org.onap.policy.pap.main.comm.msgdata.UpdateReq; +import org.onap.policy.pap.main.notification.PolicyNotifier; import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams; import org.onap.policy.pap.main.parameters.PdpParameters; import org.onap.policy.pap.main.parameters.PdpStateChangeParameters; @@ -70,6 +71,7 @@ public class CommonRequestBase { protected static final int RETRIES = 1; protected Publisher<PdpMessage> publisher; + protected PolicyNotifier notifier; protected RequestIdDispatcher<PdpStatus> dispatcher; protected Object lock; protected TimerManager timers; @@ -90,6 +92,7 @@ public class CommonRequestBase { @SuppressWarnings("unchecked") public void setUp() throws Exception { publisher = mock(Publisher.class); + notifier = mock(PolicyNotifier.class); dispatcher = mock(RequestIdDispatcher.class); lock = new Object(); timers = mock(TimerManager.class); @@ -121,12 +124,12 @@ public class CommonRequestBase { when(updateParams.getMaxRetryCount()).thenReturn(RETRIES); when(pdpParams.getUpdateParameters()).thenReturn(updateParams); - reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPublisher(publisher) + reqParams = new RequestParams().setMaxRetryCount(RETRIES).setModifyLock(lock).setPdpPublisher(publisher) .setResponseDispatcher(dispatcher).setTimers(timers); - mapParams = new PdpModifyRequestMapParams().setModifyLock(lock).setPublisher(publisher) - .setResponseDispatcher(dispatcher).setDaoFactory(daoFactory).setUpdateTimers(timers) - .setStateChangeTimers(timers).setParams(pdpParams); + mapParams = new PdpModifyRequestMapParams().setModifyLock(lock).setPdpPublisher(publisher) + .setPolicyNotifier(notifier).setResponseDispatcher(dispatcher).setDaoFactory(daoFactory) + .setUpdateTimers(timers).setStateChangeTimers(timers).setParams(pdpParams); } /** diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java index 92f5c5f3..d0bc200f 100644 --- a/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/comm/PdpModifyRequestMapTest.java @@ -305,6 +305,9 @@ public class PdpModifyRequestMapTest extends CommonRequestBase { // should have stopped publishing verify(requests).stopPublishing(); + // should have generated a notification + verify(notifier).removePdp(PDP1); + // should have published a new update PdpMessage msg2 = getSingletons(3).get(1).getMessage(); assertNotNull(msg2); diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/PdpRequestsTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/PdpRequestsTest.java index 1bf73225..ccb13fea 100644 --- a/main/src/test/java/org/onap/policy/pap/main/comm/PdpRequestsTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/comm/PdpRequestsTest.java @@ -50,7 +50,7 @@ public class PdpRequestsTest extends CommonRequestBase { update = makeUpdateReq(PDP1, MY_GROUP, MY_SUBGROUP); change = makeStateChangeReq(PDP1, MY_STATE); - data = new PdpRequests(PDP1); + data = new PdpRequests(PDP1, notifier); } @Test @@ -62,6 +62,7 @@ public class PdpRequestsTest extends CommonRequestBase { public void testAddSingleton() { data.addSingleton(update); + verify(update).setNotifier(notifier); verify(update).startPublishing(any()); } diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/RequestImplTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/RequestImplTest.java index 3d90fcbb..e47f8792 100644 --- a/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/RequestImplTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/RequestImplTest.java @@ -383,7 +383,7 @@ public class RequestImplTest extends CommonRequestBase { @Test public void testResetRetryCount_testBumpRetryCount() { - req = new MyRequest(new RequestParams().setMaxRetryCount(2).setModifyLock(lock).setPublisher(publisher) + req = new MyRequest(new RequestParams().setMaxRetryCount(2).setModifyLock(lock).setPdpPublisher(publisher) .setResponseDispatcher(dispatcher).setTimers(timers), MY_REQ_NAME, msg); req.setListener(listener); diff --git a/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java b/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java index a7987701..80ed0fb1 100644 --- a/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/comm/msgdata/UpdateReqTest.java @@ -25,9 +25,13 @@ 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.verify; import java.util.ArrayList; import java.util.Arrays; +import java.util.Set; import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; @@ -45,6 +49,7 @@ public class UpdateReqTest extends CommonRequestBase { /** * Sets up. + * * @throws Exception if an error occurs */ @Before @@ -62,6 +67,7 @@ public class UpdateReqTest extends CommonRequestBase { update.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList())); data = new UpdateReq(reqParams, MY_REQ_NAME, update); + data.setNotifier(notifier); } @Test @@ -73,6 +79,7 @@ public class UpdateReqTest extends CommonRequestBase { @Test public void testCheckResponse() { assertNull(data.checkResponse(response)); + verifyResponse(); // both policy lists null update.setPolicies(null); @@ -85,6 +92,7 @@ public class UpdateReqTest extends CommonRequestBase { response.setName(null); assertEquals("null PDP name", data.checkResponse(response)); + verifyNoResponse(); } @Test @@ -92,6 +100,7 @@ public class UpdateReqTest extends CommonRequestBase { update.setName(null); assertEquals(null, data.checkResponse(response)); + verifyResponse(); } @Test @@ -99,6 +108,7 @@ public class UpdateReqTest extends CommonRequestBase { response.setPdpGroup(DIFFERENT); assertEquals("group does not match", data.checkResponse(response)); + verifyResponse(); } @Test @@ -106,6 +116,7 @@ public class UpdateReqTest extends CommonRequestBase { response.setPdpSubgroup(DIFFERENT); assertEquals("subgroup does not match", data.checkResponse(response)); + verifyResponse(); } @Test @@ -116,6 +127,7 @@ public class UpdateReqTest extends CommonRequestBase { response.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList())); assertEquals("policies do not match", data.checkResponse(response)); + verifyResponse(); } @Test @@ -123,6 +135,7 @@ public class UpdateReqTest extends CommonRequestBase { update.setPolicies(null); assertEquals("policies do not match", data.checkResponse(response)); + verifyResponse(); } @Test @@ -130,6 +143,7 @@ public class UpdateReqTest extends CommonRequestBase { response.setPolicies(null); assertEquals("policies do not match", data.checkResponse(response)); + verifyResponse(); } @Test @@ -222,6 +236,16 @@ public class UpdateReqTest extends CommonRequestBase { assertTrue(data.getPriority() > new StateChangeReq(reqParams, MY_REQ_NAME, new PdpStateChange()).getPriority()); } + @SuppressWarnings("unchecked") + private void verifyResponse() { + verify(notifier).processResponse(any(), any(Set.class)); + } + + @SuppressWarnings("unchecked") + private void verifyNoResponse() { + verify(notifier, never()).processResponse(any(), any(Set.class)); + } + /** * Makes an update message. * diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpModifyRequestMapParams.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpModifyRequestMapParams.java index b9dde727..629cf24c 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpModifyRequestMapParams.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestPdpModifyRequestMapParams.java @@ -27,18 +27,23 @@ import static org.mockito.Mockito.mock; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher; +import org.onap.policy.models.pdp.concepts.PdpMessage; import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper; import org.onap.policy.pap.main.comm.Publisher; import org.onap.policy.pap.main.comm.TimerManager; +import org.onap.policy.pap.main.notification.PolicyNotifier; public class TestPdpModifyRequestMapParams { private PdpModifyRequestMapParams params; - private Publisher pub; + private Publisher<PdpMessage> pub; private RequestIdDispatcher<PdpStatus> disp; private Object lock; private PdpParameters pdpParams; private TimerManager updTimers; private TimerManager stateTimers; + private PolicyModelsProviderFactoryWrapper dao; + private PolicyNotifier notifier; /** * Sets up the objects and creates an empty {@link #params}. @@ -52,19 +57,24 @@ public class TestPdpModifyRequestMapParams { pdpParams = mock(PdpParameters.class); updTimers = mock(TimerManager.class); stateTimers = mock(TimerManager.class); + dao = mock(PolicyModelsProviderFactoryWrapper.class); + notifier = mock(PolicyNotifier.class); - params = new PdpModifyRequestMapParams().setModifyLock(lock).setPublisher(pub).setResponseDispatcher(disp) - .setParams(pdpParams).setStateChangeTimers(stateTimers).setUpdateTimers(updTimers); + params = new PdpModifyRequestMapParams().setModifyLock(lock).setPdpPublisher(pub).setResponseDispatcher(disp) + .setParams(pdpParams).setStateChangeTimers(stateTimers).setUpdateTimers(updTimers) + .setDaoFactory(dao).setPolicyNotifier(notifier); } @Test public void testGettersSetters() { - assertSame(pub, params.getPublisher()); + assertSame(pub, params.getPdpPublisher()); assertSame(disp, params.getResponseDispatcher()); assertSame(lock, params.getModifyLock()); assertSame(pdpParams, params.getParams()); assertSame(updTimers, params.getUpdateTimers()); assertSame(stateTimers, params.getStateChangeTimers()); + assertSame(dao, params.getDaoFactory()); + assertSame(notifier, params.getPolicyNotifier()); } @Test @@ -75,7 +85,7 @@ public class TestPdpModifyRequestMapParams { @Test public void testValidate_MissingPublisher() { - assertThatIllegalArgumentException().isThrownBy(() -> params.setPublisher(null).validate()) + assertThatIllegalArgumentException().isThrownBy(() -> params.setPdpPublisher(null).validate()) .withMessageContaining("publisher"); } @@ -108,4 +118,16 @@ public class TestPdpModifyRequestMapParams { assertThatIllegalArgumentException().isThrownBy(() -> params.setUpdateTimers(null).validate()) .withMessageContaining("update"); } + + @Test + public void testValidate_MissingDaoFactory() { + assertThatIllegalArgumentException().isThrownBy(() -> params.setDaoFactory(null).validate()) + .withMessageContaining("DAO"); + } + + @Test + public void testValidate_MissingNotifier() { + assertThatIllegalArgumentException().isThrownBy(() -> params.setPolicyNotifier(null).validate()) + .withMessageContaining("notifier"); + } } diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/TestRequestParams.java b/main/src/test/java/org/onap/policy/pap/main/parameters/TestRequestParams.java index b4855e74..7c5a3954 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/TestRequestParams.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/TestRequestParams.java @@ -28,6 +28,7 @@ import static org.mockito.Mockito.mock; import org.junit.Before; import org.junit.Test; import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher; +import org.onap.policy.models.pdp.concepts.PdpMessage; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.pap.main.comm.Publisher; import org.onap.policy.pap.main.comm.TimerManager; @@ -36,7 +37,7 @@ public class TestRequestParams { private static final int RETRIES = 1; private RequestParams params; - private Publisher pub; + private Publisher<PdpMessage> pub; private RequestIdDispatcher<PdpStatus> disp; private Object lock; private TimerManager timers; @@ -52,15 +53,15 @@ public class TestRequestParams { lock = new Object(); timers = mock(TimerManager.class); - params = new RequestParams().setModifyLock(lock).setPublisher(pub).setResponseDispatcher(disp).setTimers(timers) - .setMaxRetryCount(RETRIES); + params = new RequestParams().setModifyLock(lock).setPdpPublisher(pub).setResponseDispatcher(disp) + .setTimers(timers).setMaxRetryCount(RETRIES); } @Test public void testGettersSetters() { - assertSame(params, params.setModifyLock(lock).setPublisher(pub).setResponseDispatcher(disp)); + assertSame(params, params.setModifyLock(lock).setPdpPublisher(pub).setResponseDispatcher(disp)); - assertSame(pub, params.getPublisher()); + assertSame(pub, params.getPdpPublisher()); assertSame(disp, params.getResponseDispatcher()); assertSame(lock, params.getModifyLock()); assertSame(timers, params.getTimers()); @@ -87,7 +88,7 @@ public class TestRequestParams { @Test public void testValidate_MissingPublisher() { - assertThatIllegalArgumentException().isThrownBy(() -> params.setPublisher(null).validate()) + assertThatIllegalArgumentException().isThrownBy(() -> params.setPdpPublisher(null).validate()) .withMessageContaining("publisher"); } 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 2fca6848..3f29fb56 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 @@ -52,6 +52,7 @@ 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; +import org.onap.policy.pap.main.notification.PolicyNotifier; /** * Super class for TestPdpGroupDeployProviderXxx classes. @@ -62,6 +63,9 @@ public class ProviderSuper { @Mock protected PolicyModelsProvider dao; + @Mock + protected PolicyNotifier notifier; + /** * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups(). @@ -103,6 +107,7 @@ public class ProviderSuper { Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit); Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap); Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact); + Registry.register(PapConstants.REG_POLICY_NOTIFIER, notifier); } protected void assertGroup(List<PdpGroup> groups, String name) { 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 a577e07b..cac16808 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 @@ -26,8 +26,8 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -35,11 +35,14 @@ import static org.mockito.Mockito.when; import java.util.Arrays; import java.util.List; -import java.util.function.BiFunction; +import java.util.Set; import javax.ws.rs.core.Response.Status; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; @@ -48,17 +51,23 @@ import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; +import org.onap.policy.pap.main.rest.depundep.ProviderBase.Updater; public class TestPdpGroupDeleteProvider extends ProviderSuper { private static final String EXPECTED_EXCEPTION = "expected exception"; private static final String GROUP1_NAME = "groupA"; - private MyProvider prov; + @Mock private SessionData session; + + @Captor + private ArgumentCaptor<Set<String>> pdpCaptor; + + private MyProvider prov; private ToscaPolicyIdentifierOptVersion optIdent; private ToscaPolicyIdentifierOptVersion fullIdent; private ToscaPolicyIdentifier ident; - private BiFunction<PdpGroup, PdpSubGroup, Boolean> updater; + private Updater updater; @AfterClass @@ -76,14 +85,13 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { super.setUp(); - session = mock(SessionData.class); ident = policy1.getIdentifier(); optIdent = new ToscaPolicyIdentifierOptVersion(ident.getName(), null); fullIdent = new ToscaPolicyIdentifierOptVersion(ident.getName(), ident.getVersion()); prov = new MyProvider(); - updater = prov.makeUpdater(policy1, fullIdent); + updater = prov.makeUpdater(session, policy1, fullIdent); } @Test @@ -215,7 +223,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { } @Test - public void testMakeUpdater_WithVersion() { + public void testMakeUpdater_WithVersion() throws PfModelException { /* * this group has two matching policies and one policy with a different name. */ @@ -230,10 +238,13 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { // identified policy should have been removed assertEquals(origSize - 1, subgroup.getPolicies().size()); assertFalse(subgroup.getPolicies().contains(ident)); + + verify(session).trackUndeploy(eq(ident), pdpCaptor.capture()); + assertEquals("[pdpA]", pdpCaptor.getValue().toString()); } @Test - public void testMakeUpdater_NullVersion() { + public void testMakeUpdater_NullVersion() throws PfModelException { /* * this group has two matching policies and one policy with a different name. */ @@ -243,7 +254,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { int origSize = subgroup.getPolicies().size(); // invoke updater - matching the name, but with a null (i.e., wild-card) version - updater = prov.makeUpdater(policy1, optIdent); + updater = prov.makeUpdater(session, policy1, optIdent); assertTrue(updater.apply(group, subgroup)); // identified policy should have been removed @@ -252,7 +263,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { } @Test - public void testMakeUpdater_NotFound() { + public void testMakeUpdater_NotFound() throws PfModelException { /* * this group has one policy with a different name and one with a different * version, but not the policy of interest. 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 90ea1658..c2d368ad 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 @@ -35,11 +35,13 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.TreeMap; +import java.util.TreeSet; import java.util.stream.Collectors; import javax.ws.rs.core.Response.Status; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; @@ -53,6 +55,7 @@ import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; public class TestPdpGroupDeployProvider extends ProviderSuper { private static final String EXPECTED_EXCEPTION = "expected exception"; @@ -279,6 +282,14 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertEquals(newgrp.toString(), dbgroup.toString()); + // no deployment notifications + verify(notifier, never()).addDeploymentData(any()); + + // should have notified of deleted subgroup's policies/PDPs + ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier).addUndeploymentData(captor.capture()); + assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB, pdpD]"); + // this requires a PDP UPDATE message List<PdpUpdate> pdpUpdates = getUpdateRequests(2); assertEquals(2, pdpUpdates.size()); @@ -540,16 +551,23 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { @Test public void testUpdateSubGroup_Policies() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroups groups = loadPdpGroups("createGroupsDelPolicy.json"); PdpGroup newgrp = groups.getGroups().get(0); PdpGroup group = new PdpGroup(newgrp); when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); - subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION)); + + // delete second policy + subgrp.setPolicies(subgrp.getPolicies().subList(0, 1)); + + // add new policy + ToscaPolicyIdentifier policyId2 = new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION); + subgrp.getPolicies().add(policyId2); when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) .thenReturn(loadPolicies("daoPolicyList.json")) + .thenReturn(loadPolicies("daoPolicyListDelPolicy.json")) .thenReturn(loadPolicies("createGroupNewPolicy.json")); prov.createOrUpdateGroups(groups); @@ -559,11 +577,43 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertEquals(newgrp.toString(), group.toString()); + // should have notified of added policy/PDPs + ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier).addDeploymentData(captor.capture()); + assertDeploymentData(captor, policyId2, "[pdpA]"); + + // should have notified of deleted policy/PDPs + captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier).addUndeploymentData(captor.capture()); + assertDeploymentData(captor, new ToscaPolicyIdentifier("ToBeDeleted", POLICY1_VERSION), "[pdpA]"); + // this requires a PDP UPDATE message assertGroupUpdate(group, subgrp); } @Test + public void testUpdateSubGroup_Unchanged() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); + Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); + + assertEquals(newgrp.toString(), group.toString()); + + // no notifications + verify(notifier, never()).addDeploymentData(any()); + verify(notifier, never()).addUndeploymentData(any()); + + // no group updates + assertNoGroupAction(); + } + + @Test public void testUpdateSubGroup_PolicyVersionMismatch() throws Exception { PdpGroups groups = loadPdpGroups("createGroups.json"); PdpGroup newgrp = groups.getGroups().get(0); @@ -662,6 +712,14 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { List<PdpUpdate> requests = getUpdateRequests(2); assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4); + + // should have notified of added policy/PDPs + ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier).addDeploymentData(captor.capture()); + assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB, pdpD]"); + + // no undeployment notifications + verify(notifier, never()).addUndeploymentData(any()); } @Test @@ -741,6 +799,14 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertEquals(Arrays.asList(group), updates); } + private void assertDeploymentData(ArgumentCaptor<PolicyPdpNotificationData> captor, ToscaPolicyIdentifier policyId, + String expectedPdps) { + PolicyPdpNotificationData data = captor.getValue(); + assertEquals(policyId, data.getPolicyId()); + assertEquals(policy1.getTypeIdentifier(), data.getPolicyType()); + assertEquals(expectedPdps, new TreeSet<>(data.getPdps()).toString()); + } + /** * Loads a standard request. * 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 3a91363f..9370a7bd 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 @@ -26,6 +26,7 @@ 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; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -34,20 +35,22 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Queue; -import java.util.function.BiFunction; +import java.util.TreeSet; import javax.ws.rs.core.Response.Status; import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.pap.concepts.PdpDeployPolicies; import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; 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.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; +import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; import org.powermock.reflect.Whitebox; public class TestProviderBase extends ProviderSuper { @@ -103,6 +106,24 @@ public class TestProviderBase extends ProviderSuper { assertGroup(getGroupUpdates(), GROUP1_NAME); assertUpdate(getUpdateRequests(1), GROUP1_NAME, PDP1_TYPE, PDP1); + + ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier, times(2)).addDeploymentData(captor.capture()); + assertNotifier(captor, PDP1, PDP3); + + captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier, times(2)).addUndeploymentData(captor.capture()); + assertNotifier(captor, PDP2, PDP4); + } + + private void assertNotifier(ArgumentCaptor<PolicyPdpNotificationData> captor, String firstPdp, String secondPdp) { + assertEquals(1, captor.getAllValues().get(0).getPdps().size()); + assertEquals(1, captor.getAllValues().get(1).getPdps().size()); + + // ensure the order by using a TreeSet + TreeSet<String> pdps = new TreeSet<>(captor.getAllValues().get(0).getPdps()); + pdps.addAll(captor.getAllValues().get(1).getPdps()); + assertEquals("[" + firstPdp + ", " + secondPdp + "]", pdps.toString()); } @Test @@ -345,13 +366,20 @@ public class TestProviderBase extends ProviderSuper { } @Override - protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy, + protected Updater makeUpdater(SessionData data, ToscaPolicy policy, ToscaPolicyIdentifierOptVersion desiredPolicy) { return (group, subgroup) -> { if (shouldUpdate.remove()) { // queue indicated that the update should succeed subgroup.getPolicies().add(policy.getIdentifier()); + + data.trackDeploy(policy.getIdentifier(), Collections.singleton(PDP1)); + data.trackUndeploy(policy.getIdentifier(), Collections.singleton(PDP2)); + + ToscaPolicyIdentifier ident2 = new ToscaPolicyIdentifier(POLICY1_NAME, "9.9.9"); + data.trackDeploy(ident2, Collections.singleton(PDP3)); + data.trackUndeploy(ident2, Collections.singleton(PDP4)); return true; } else { 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 d7d9b677..4d353a6e 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 @@ -41,6 +41,8 @@ import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; +import java.util.TreeSet; +import java.util.function.Supplier; import javax.ws.rs.core.Response.Status; import org.apache.commons.lang3.tuple.Pair; import org.junit.Before; @@ -52,9 +54,11 @@ import org.onap.policy.models.pdp.concepts.PdpStateChange; 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.ToscaPolicyIdentifier; 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; +import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; public class TestSessionData extends ProviderSuper { private static final String GROUP_NAME = "groupA"; @@ -533,6 +537,70 @@ public class TestSessionData extends ProviderSuper { verify(dao).deletePdpGroup(group1.getName()); } + @Test + public void testTrackDeploy() throws PfModelException { + testTrack(session::getDeployData, session::getUndeployData, session::trackDeploy); + } + + /** + * Tests trackDeploy() when there is something in the undeployed list. + * + * @throws PfModelException if an error occurs + */ + @Test + public void testTrackDeployRemoveUndeploy() throws PfModelException { + testTrack(session::getDeployData, session::getUndeployData, session::trackUndeploy, session::trackDeploy); + } + + @Test + public void testTrackUndeploy() throws PfModelException { + testTrack(session::getUndeployData, session::getDeployData, session::trackUndeploy); + } + + /** + * Tests trackUndeploy() when there is something in the deployed list. + * + * @throws PfModelException if an error occurs + */ + @Test + public void testTrackUndeployRemoveUndeploy() throws PfModelException { + testTrack(session::getUndeployData, session::getDeployData, session::trackDeploy, session::trackUndeploy); + } + + protected void testTrack(Supplier<Collection<PolicyPdpNotificationData>> expected, + Supplier<Collection<PolicyPdpNotificationData>> unexpected, TrackEx... trackFuncs) + throws PfModelException { + + ToscaPolicy policy = makePolicy(POLICY_NAME, POLICY_VERSION); + policy.setType(POLICY_TYPE); + policy.setTypeVersion(POLICY_TYPE_VERSION); + + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy)); + + ToscaPolicyIdentifier policyId = new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION); + List<String> pdps = Arrays.asList(PDP1, PDP2); + + for (TrackEx trackFunc : trackFuncs) { + trackFunc.accept(policyId, pdps); + } + + // "unexpected" list should be empty of any PDPs + Collection<PolicyPdpNotificationData> dataList = unexpected.get(); + assertTrue(dataList.size() <= 1); + if (!dataList.isEmpty()) { + PolicyPdpNotificationData data = dataList.iterator().next(); + assertTrue(data.getPdps().isEmpty()); + } + + dataList = expected.get(); + assertEquals(1, dataList.size()); + + PolicyPdpNotificationData data = dataList.iterator().next(); + assertEquals(policyId, data.getPolicyId()); + assertEquals(type, data.getPolicyType()); + assertEquals("[pdp_1, pdp_2]", new TreeSet<>(data.getPdps()).toString()); + } + private PdpUpdate makeUpdate(String pdpName) { PdpUpdate update = new PdpUpdate(); @@ -586,4 +654,9 @@ public class TestSessionData extends ProviderSuper { private String getName(Pair<PdpUpdate, PdpStateChange> pair) { return (pair.getKey() != null ? pair.getKey().getName() : pair.getValue().getName()); } + + @FunctionalInterface + private static interface TrackEx { + public void accept(ToscaPolicyIdentifier policyId, Collection<String> pdps) throws PfModelException; + } } diff --git a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java index 13c06098..19668b31 100644 --- a/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java +++ b/main/src/test/java/org/onap/policy/pap/main/startstop/TestPapActivator.java @@ -36,6 +36,8 @@ import org.onap.policy.common.utils.services.Registry; import org.onap.policy.pap.main.PapConstants; import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.comm.PdpModifyRequestMap; +import org.onap.policy.pap.main.comm.PdpTracker; +import org.onap.policy.pap.main.notification.PolicyNotifier; import org.onap.policy.pap.main.parameters.CommonTestData; import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.onap.policy.pap.main.parameters.PapParameterHandler; @@ -92,6 +94,8 @@ public class TestPapActivator { assertNotNull(Registry.get(PapConstants.REG_PDP_MODIFY_LOCK, Object.class)); assertNotNull(Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class)); assertNotNull(Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class)); + assertNotNull(Registry.get(PapConstants.REG_PDP_TRACKER, PdpTracker.class)); + assertNotNull(Registry.get(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class)); // repeat - should throw an exception assertThatIllegalStateException().isThrownBy(() -> activator.start()); @@ -109,6 +113,8 @@ public class TestPapActivator { assertNull(Registry.getOrDefault(PapConstants.REG_PDP_MODIFY_LOCK, Object.class, null)); assertNull(Registry.getOrDefault(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class, null)); assertNull(Registry.getOrDefault(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class, null)); + assertNull(Registry.getOrDefault(PapConstants.REG_PDP_TRACKER, PdpTracker.class, null)); + assertNull(Registry.getOrDefault(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class, null)); // repeat - should throw an exception assertThatIllegalStateException().isThrownBy(() -> activator.stop()); |