From 1e896c1a2d599c02a4494e868cac5fe5399f819f Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 24 Oct 2019 11:17:14 -0400 Subject: Add trackers for generating notifications Issue-ID: POLICY-1841 Signed-off-by: Jim Hahn Change-Id: I7d6fe29707685e4711120b6a1e8448f25870a0ef Signed-off-by: Jim Hahn --- .../pap/main/notification/PolicyCommonSupport.java | 94 +++++++ .../main/notification/PolicyCommonTrackerTest.java | 297 +++++++++++++++++++++ .../main/notification/PolicyDeployTrackerTest.java | 120 +++++++++ .../notification/PolicyUndeployTrackerTest.java | 118 ++++++++ 4 files changed, 629 insertions(+) create mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java (limited to 'main/src/test/java') diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java new file mode 100644 index 00000000..14f3055c --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java @@ -0,0 +1,94 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.notification; + +import java.util.Arrays; +import java.util.List; +import org.junit.Before; +import org.onap.policy.models.pap.concepts.PolicyStatus; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; + +/** + * Super class for policy notification test classes. + */ +public class PolicyCommonSupport { + protected static final String MAP_FIELD = "policy2data"; + protected static final String PDP1 = "pdp-1"; + protected static final String PDP2 = "pdp-2"; + protected static final String PDP3 = "pdp-3"; + protected static final String PDP4 = "pdp-4"; + + protected ToscaPolicyTypeIdentifier type; + protected ToscaPolicyIdentifier policy1; + protected ToscaPolicyIdentifier policy2; + protected ToscaPolicyIdentifier policy3; + protected ToscaPolicyIdentifier policy4; + + /** + * Creates various objects. + */ + @Before + public void setUp() { + type = new ToscaPolicyTypeIdentifier("my-type", "3.2.1"); + policy1 = new ToscaPolicyIdentifier("my-id-a", "1.2.0"); + policy2 = new ToscaPolicyIdentifier("my-id-b", "1.2.1"); + policy3 = new ToscaPolicyIdentifier("my-id-c", "1.2.2"); + policy4 = new ToscaPolicyIdentifier("my-id-d", "1.2.3"); + } + + /** + * Makes notification data. + * + * @param policyId ID of the policy with which the data should be associated + * @param pdps PDPs to be included within the data + * @return a new notification data structure + */ + protected PolicyPdpNotificationData makeData(ToscaPolicyIdentifier policyId, String... pdps) { + PolicyPdpNotificationData data = new PolicyPdpNotificationData(policyId, type); + data.addAll(Arrays.asList(pdps)); + return data; + } + + /** + * Extracts the counts from the sets contained within tracker data. + * + * @param data data from which to extract the sets + * @return a list containing the number of successes, failures, and incomplete PDPs, + * in that order + */ + protected List getCounts(PolicyTrackerData data) { + PolicyStatus status = new PolicyStatus(); + data.putValuesInto(status); + return getCounts(status); + } + + /** + * Extracts the counts from within a status notification. + * + * @param status status from which to extract the counts + * @return a list containing the number of successes, failures, and incomplete PDPs, + * in that order + */ + protected List getCounts(PolicyStatus status) { + return Arrays.asList(status.getSuccessCount(), status.getFailureCount(), status.getIncompleteCount()); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java new file mode 100644 index 00000000..cfcf4471 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java @@ -0,0 +1,297 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.notification; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.models.pap.concepts.PolicyStatus; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.powermock.reflect.Whitebox; + +public class PolicyCommonTrackerTest extends PolicyCommonSupport { + + private MyTracker tracker; + private Map map; + + /** + * Creates various objects, including {@link #tracker}. + */ + @Before + public void setUp() { + super.setUp(); + + tracker = new MyTracker(); + } + + @Test + public void testAddData() { + tracker.addData(makeData(policy1, PDP1, PDP2)); + assertEquals(1, map.size()); + + PolicyTrackerData data = map.get(policy1); + assertNotNull(data); + assertFalse(data.isComplete()); + + // add same policy - should still have the same data object + tracker.addData(makeData(policy1, PDP1, PDP3)); + assertEquals(1, map.size()); + assertSame(data, map.get(policy1)); + assertFalse(data.isComplete()); + + // add another policy + tracker.addData(makeData(policy2, PDP2)); + assertEquals(2, map.size()); + + // data for policy 1 is unchanged + assertSame(data, map.get(policy1)); + assertFalse(data.isComplete()); + + // policy 2 should have its own data + assertTrue(map.get(policy2) != data); + data = map.get(policy2); + assertNotNull(data); + assertFalse(data.isComplete()); + } + + /** + * Tests removeData() when the policy isn't in the map. + */ + @Test + public void testRemoveDataUnknownPolicy() { + tracker.addData(makeData(policy1, PDP1, PDP2)); + tracker.addData(makeData(policy2, PDP1, PDP3)); + + // remove a policy that isn't in the map + List statusList = new ArrayList<>(); + tracker.removeData(makeData(policy3, PDP1), statusList); + assertTrue(statusList.isEmpty()); + assertEquals(2, map.size()); + } + + /** + * Tests removeData() when only some PDPs are removed from the policy. + */ + @Test + public void testRemoveDataRemoveSomePdps() { + tracker.addData(makeData(policy1, PDP1, PDP2)); + tracker.addData(makeData(policy2, PDP1, PDP3)); + + // remove some PDPs from a policy - no notifications and no changes to the map + List statusList = new ArrayList<>(); + tracker.removeData(makeData(policy2, PDP1), statusList); + assertTrue(statusList.isEmpty()); + assertTrue(map.containsKey(policy1)); + assertTrue(map.containsKey(policy2)); + } + + /** + * Tests removeData() when the subclass indicates that the policy should NOT be removed. + */ + @Test + public void testRemoveDataDoNotRemovePolicy() { + tracker = new MyTracker() { + @Override + protected boolean shouldRemove(PolicyTrackerData data) { + return false; + } + }; + + tracker.addData(makeData(policy1, PDP1, PDP2)); + tracker.addData(makeData(policy2, PDP1, PDP3)); + + // remove all the PDPs from one policy, but do NOT remove the policy + List statusList = new ArrayList<>(); + tracker.removeData(makeData(policy2, PDP1, PDP3), statusList); + assertEquals(1, statusList.size()); + assertEquals(policy2, statusList.get(0).getPolicy()); + assertTrue(map.containsKey(policy1)); + assertTrue(map.containsKey(policy2)); + } + + /** + * Tests removeData() when the subclass indicates that the policy should be removed. + */ + @Test + public void testRemoveDataRemovePolicy() { + tracker.addData(makeData(policy1, PDP1, PDP2)); + tracker.addData(makeData(policy2, PDP1, PDP3)); + + // remove all the PDPs from one policy, and remove the policy + List statusList = new ArrayList<>(); + tracker.removeData(makeData(policy1, PDP1, PDP2, PDP3), statusList); + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + assertFalse(map.containsKey(policy1)); + assertTrue(map.containsKey(policy2)); + } + + @Test + public void testRemovePdp() { + tracker.addData(makeData(policy1, PDP1)); + tracker.addData(makeData(policy2, PDP1, PDP3)); + tracker.addData(makeData(policy3, PDP1, PDP2, PDP3)); + tracker.addData(makeData(policy4, PDP4, PDP2, PDP3)); + + List statusList = new ArrayList<>(); + tracker.removePdp(PDP1, statusList); + + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + + assertEquals(3, map.size()); + assertFalse(map.containsKey(policy1)); + assertEquals("[0, 0, 1]", getCounts(map.get(policy2)).toString()); + assertEquals("[0, 0, 2]", getCounts(map.get(policy3)).toString()); + assertEquals("[0, 0, 3]", getCounts(map.get(policy4)).toString()); + } + + @Test + public void testProcessResponse() { + tracker.addData(makeData(policy1, PDP1)); + tracker.addData(makeData(policy2, PDP1, PDP3)); + tracker.addData(makeData(policy3, PDP1, PDP2, PDP3)); + tracker.addData(makeData(policy4, PDP4, PDP2, PDP3)); + + List statusList = new ArrayList<>(); + tracker.processResponse(PDP1, Arrays.asList(policy3), statusList); + + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + + assertEquals(3, map.size()); + assertFalse(map.containsKey(policy1)); + assertEquals("[0, 1, 1]", getCounts(map.get(policy2)).toString()); + assertEquals("[1, 0, 2]", getCounts(map.get(policy3)).toString()); + assertEquals("[0, 0, 3]", getCounts(map.get(policy4)).toString()); + } + + @Test + public void testUpdateMap() { + tracker.addData(makeData(policy1, PDP1)); + tracker.addData(makeData(policy2, PDP1, PDP3)); + tracker.addData(makeData(policy3, PDP1, PDP2, PDP3)); + tracker.addData(makeData(policy4, PDP4, PDP2, PDP3)); + + List statusList = new ArrayList<>(); + tracker.processResponse(PDP1, Arrays.asList(policy3), statusList); + + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + + assertEquals(3, map.size()); + assertFalse(map.containsKey(policy1)); + assertEquals("[0, 1, 1]", getCounts(map.get(policy2)).toString()); + assertEquals("[1, 0, 2]", getCounts(map.get(policy3)).toString()); + assertEquals("[0, 0, 3]", getCounts(map.get(policy4)).toString()); + } + + /** + * Tests updateMap() when the policy should NOT be removed. + */ + @Test + public void testUpdateMapDoNotRemove() { + tracker = new MyTracker() { + @Override + protected boolean shouldRemove(PolicyTrackerData data) { + return false; + } + }; + + tracker.addData(makeData(policy1, PDP1, PDP2)); + + // indicate that PDP2 has succeeded + tracker.processResponse(PDP2, Arrays.asList(policy1), new ArrayList<>(0)); + + // indicate that PDP1 has succeeded + List statusList = new ArrayList<>(); + tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); + + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + + assertEquals(1, map.size()); + assertTrue(map.containsKey(policy1)); + assertEquals("[2, 0, 0]", getCounts(map.get(policy1)).toString()); + } + + /** + * Tests updateMap() when the policy SHOULD be removed. + */ + @Test + public void testUpdateMapRemovePolicy() { + tracker.addData(makeData(policy1, PDP1, PDP2)); + + // indicate that PDP2 has succeeded + tracker.processResponse(PDP2, Arrays.asList(policy1), new ArrayList<>(0)); + + // indicate that PDP1 has succeeded + List statusList = new ArrayList<>(); + tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); + + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString()); + + assertTrue(map.isEmpty()); + } + + @Test + public void testMakeStatus() { + tracker.addData(makeData(policy1, PDP1, PDP2)); + + // indicate that PDP2 has succeeded + List statusList = new ArrayList<>(); + tracker.processResponse(PDP2, Arrays.asList(policy1), statusList); + assertTrue(statusList.isEmpty()); + + // indicate that PDP1 has failed + tracker.processResponse(PDP1, Arrays.asList(policy2), statusList); + assertEquals(1, statusList.size()); + assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString()); + } + + private class MyTracker extends PolicyCommonTracker { + + public MyTracker() { + map = Whitebox.getInternalState(this, MAP_FIELD); + } + + @Override + protected boolean updateData(String pdp, PolicyTrackerData data, boolean stillActive) { + return (stillActive ? data.success(pdp) : data.fail(pdp)); + } + + @Override + protected boolean shouldRemove(PolicyTrackerData data) { + return data.isComplete(); + } + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java new file mode 100644 index 00000000..bd093141 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java @@ -0,0 +1,120 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.notification; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +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; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.models.pap.concepts.PolicyStatus; + +public class PolicyDeployTrackerTest extends PolicyCommonSupport { + + @Mock + private PolicyTrackerData data; + + private PolicyDeployTracker tracker; + + /** + * Creates various objects, including {@link #tracker}. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + super.setUp(); + + tracker = new PolicyDeployTracker(); + } + + @Test + public void test() { + tracker.addData(makeData(policy1, PDP1, PDP2)); + + // indicate that PDP2 has succeeded + tracker.processResponse(PDP2, Arrays.asList(policy1), new ArrayList<>(0)); + + // indicate that PDP1 has succeeded + List statusList = new ArrayList<>(); + tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); + + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString()); + + // indicate that PDP1 has failed - should get a notification, if still in the map + statusList.clear(); + tracker.processResponse(PDP1, Collections.emptyList(), statusList); + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString()); + } + + @Test + public void testUpdateData() { + // when success returns false + assertFalse(tracker.updateData(PDP1, data, true)); + verify(data).success(PDP1); + verify(data, never()).fail(any()); + + // when inactive + assertFalse(tracker.updateData(PDP1, data, false)); + verify(data).success(PDP1); + verify(data).fail(any()); + + // when success & fail return true + when(data.success(PDP1)).thenReturn(true); + when(data.fail(PDP1)).thenReturn(true); + assertTrue(tracker.updateData(PDP1, data, true)); + verify(data, times(2)).success(PDP1); + verify(data, times(1)).fail(PDP1); + + // when inactive + assertTrue(tracker.updateData(PDP1, data, false)); + verify(data, times(2)).success(PDP1); + verify(data, times(2)).fail(PDP1); + } + + @Test + public void testShouldRemove() { + // when data is complete, but not empty + when(data.isComplete()).thenReturn(true); + when(data.isEmpty()).thenReturn(false); + assertFalse(tracker.shouldRemove(data)); + + // when data is empty + when(data.isEmpty()).thenReturn(true); + assertTrue(tracker.shouldRemove(data)); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java new file mode 100644 index 00000000..d837cbad --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java @@ -0,0 +1,118 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.notification; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +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; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.models.pap.concepts.PolicyStatus; + +public class PolicyUndeployTrackerTest extends PolicyCommonSupport { + + @Mock + private PolicyTrackerData data; + + private PolicyUndeployTracker tracker; + + /** + * Creates various objects, including {@link #tracker}. + */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + super.setUp(); + + tracker = new PolicyUndeployTracker(); + } + + @Test + public void test() { + tracker.addData(makeData(policy1, PDP1, PDP2)); + + // indicate that PDP2 has been undeployed + tracker.processResponse(PDP2, Collections.emptyList(), new ArrayList<>(0)); + + // indicate that PDP1 has been undeployed + List statusList = new ArrayList<>(); + tracker.processResponse(PDP1, Collections.emptyList(), statusList); + + assertEquals(1, statusList.size()); + assertEquals(policy1, statusList.get(0).getPolicy()); + assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString()); + + // indicate that PDP1 has been re-deployed - should not get a notification, + // because policy + // is gone + statusList.clear(); + tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); + assertTrue(statusList.isEmpty()); + } + + @Test + public void testUpdateData() { + // when success returns false + assertFalse(tracker.updateData(PDP1, data, false)); + verify(data).success(PDP1); + verify(data, never()).fail(any()); + + // when inactive + assertFalse(tracker.updateData(PDP1, data, true)); + verify(data).success(PDP1); + verify(data).fail(any()); + + // when success & fail return true + when(data.success(PDP1)).thenReturn(true); + when(data.fail(PDP1)).thenReturn(true); + assertTrue(tracker.updateData(PDP1, data, false)); + verify(data, times(2)).success(PDP1); + verify(data, times(1)).fail(PDP1); + + // when inactive + assertTrue(tracker.updateData(PDP1, data, true)); + verify(data, times(2)).success(PDP1); + verify(data, times(2)).fail(PDP1); + } + + @Test + public void testShouldRemove() { + // when data is not complete + assertFalse(tracker.shouldRemove(data)); + + // when data is complete + when(data.isComplete()).thenReturn(true); + assertTrue(tracker.shouldRemove(data)); + } +} -- cgit 1.2.3-korg