From 4e5bcfe2a7aeccebcea55c0146a38a931567ea75 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 11 Jan 2021 10:53:54 -0500 Subject: Make PAP stateless Removed old code that tracked the status using a cache. Modified the code to use the status found in the DB instead. Issue-ID: POLICY-2648 Change-Id: I4f1f5201eff7b3352631f50d67c40349ac7f2014 Signed-off-by: Jim Hahn --- .../pap/main/comm/PdpModifyRequestMapTest.java | 41 ++- .../pap/main/comm/msgdata/UpdateReqTest.java | 12 +- .../pap/main/notification/PolicyCommonSupport.java | 94 ------ .../main/notification/PolicyCommonTrackerTest.java | 347 -------------------- .../main/notification/PolicyDeployTrackerTest.java | 250 --------------- .../pap/main/notification/PolicyNotifierTest.java | 245 +++------------ .../PolicyPdpNotificationDataTest.java | 121 ------- .../main/notification/PolicyTrackerDataTest.java | 350 --------------------- .../notification/PolicyUndeployTrackerTest.java | 264 ---------------- .../onap/policy/pap/main/rest/ProviderSuper.java | 13 +- .../rest/TestPdpGroupCreateOrUpdateProvider.java | 6 +- .../pap/main/rest/TestPdpGroupDeleteProvider.java | 4 +- .../pap/main/rest/TestPdpGroupDeployProvider.java | 50 +-- .../pap/main/rest/TestPolicyStatusProvider.java | 152 +++++++++ .../policy/pap/main/rest/TestProviderBase.java | 36 +-- .../onap/policy/pap/main/rest/TestSessionData.java | 89 +++--- .../onap/policy/pap/main/rest/e2e/End2EndBase.java | 30 +- .../policy/pap/main/rest/e2e/PolicyStatusTest.java | 12 +- 18 files changed, 332 insertions(+), 1784 deletions(-) delete mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationDataTest.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java (limited to 'main/src/test/java/org') 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 114d50e6..65708aa6 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2020-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,6 +21,7 @@ package org.onap.policy.pap.main.comm; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -51,9 +52,13 @@ import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pap.concepts.PolicyNotification; +import org.onap.policy.models.pap.concepts.PolicyStatus; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpMessage; +import org.onap.policy.models.pdp.concepts.PdpPolicyStatus; +import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpSubGroup; @@ -82,6 +87,9 @@ public class PdpModifyRequestMapTest extends CommonRequestBase { @Captor private ArgumentCaptor> updateCaptor; + @Captor + private ArgumentCaptor notificationCaptor; + /** * Used to capture input to undeployer.undeploy(). */ @@ -360,7 +368,22 @@ public class PdpModifyRequestMapTest extends CommonRequestBase { PdpGroup group = makeGroup(MY_GROUP); group.setPdpSubgroups(Arrays.asList(makeSubGroup(MY_SUBGROUP, PDP1))); + final ToscaConceptIdentifier policy1 = new ToscaConceptIdentifier("MyPolicy", "10.20.30"); + final ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "10.20.30"); + + // @formatter:off when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group)); + when(dao.getGroupPolicyStatus(any())).thenReturn(Arrays.asList( + PdpPolicyStatus.builder() + .pdpGroup(MY_GROUP) + .pdpType(MY_SUBGROUP) + .pdpId(PDP1) + .policy(policy1) + .policyType(policyType) + .deploy(true) + .state(State.SUCCESS) + .build())); + // @formatter:on // indicate retries exhausted invokeLastRetryHandler(1); @@ -368,8 +391,17 @@ public class PdpModifyRequestMapTest extends CommonRequestBase { // should have stopped publishing verify(requests).stopPublishing(); - // should have generated a notification - verify(notifier).removePdp(PDP1); + // should have generated a notification; yes, it should go into the "added" set + verify(notifier).publish(notificationCaptor.capture()); + assertThat(notificationCaptor.getValue().getDeleted()).isEmpty(); + assertThat(notificationCaptor.getValue().getAdded()).hasSize(1); + + PolicyStatus status = notificationCaptor.getValue().getAdded().get(0); + assertThat(status.getPolicy()).isEqualTo(policy1); + assertThat(status.getPolicyType()).isEqualTo(policyType); + assertThat(status.getFailureCount()).isZero(); + assertThat(status.getIncompleteCount()).isZero(); + assertThat(status.getSuccessCount()).isZero(); // should have removed from the group List groups = getGroupUpdates(); @@ -390,6 +422,9 @@ public class PdpModifyRequestMapTest extends CommonRequestBase { // should not have done any updates verify(dao, never()).updatePdpGroups(any()); + + // and no publishes + verify(notifier, never()).publish(any()); } @Test 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 db955155..ca6a62ca 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 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. @@ -115,7 +115,7 @@ public class UpdateReqTest extends CommonRequestBase { assertEquals("group does not match", data.checkResponse(response)); assertTrue(data.getUndeployPolicies().isEmpty()); - verifyResponse(); + verifyNoResponse(); } @Test @@ -124,7 +124,7 @@ public class UpdateReqTest extends CommonRequestBase { assertEquals("subgroup does not match", data.checkResponse(response)); assertTrue(data.getUndeployPolicies().isEmpty()); - verifyResponse(); + verifyNoResponse(); } @Test @@ -137,7 +137,7 @@ public class UpdateReqTest extends CommonRequestBase { assertEquals(null, data.checkResponse(response)); assertTrue(data.getUndeployPolicies().isEmpty()); - verifyResponse(); + verifyNoResponse(); } @Test @@ -277,12 +277,12 @@ public class UpdateReqTest extends CommonRequestBase { @SuppressWarnings("unchecked") private void verifyResponse() { - verify(notifier).processResponse(any(), any(Set.class)); + verify(notifier).processResponse(any(), any(), any(Set.class), any(Set.class)); } @SuppressWarnings("unchecked") private void verifyNoResponse() { - verify(notifier, never()).processResponse(any(), any(Set.class)); + verify(notifier, never()).processResponse(any(), any(), any(Set.class), any(Set.class)); } /** 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 deleted file mode 100644 index de1848d4..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonSupport.java +++ /dev/null @@ -1,94 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021 Nordix Foundation. - * ================================================================================ - * 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.ToscaConceptIdentifier; - -/** - * 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 ToscaConceptIdentifier type; - protected ToscaConceptIdentifier policy1; - protected ToscaConceptIdentifier policy2; - protected ToscaConceptIdentifier policy3; - protected ToscaConceptIdentifier policy4; - - /** - * Creates various objects. - */ - @Before - public void setUp() { - type = new ToscaConceptIdentifier("my-type", "3.2.1"); - policy1 = new ToscaConceptIdentifier("my-id-a", "1.2.0"); - policy2 = new ToscaConceptIdentifier("my-id-b", "1.2.1"); - policy3 = new ToscaConceptIdentifier("my-id-c", "1.2.2"); - policy4 = new ToscaConceptIdentifier("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(ToscaConceptIdentifier 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 deleted file mode 100644 index b80b80d7..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyCommonTrackerTest.java +++ /dev/null @@ -1,347 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021 Nordix Foundation. - * ================================================================================ - * 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.assertNotSame; -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 java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.models.pap.concepts.PolicyStatus; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.powermock.reflect.Whitebox; - -public class PolicyCommonTrackerTest extends PolicyCommonSupport { - - private MyTracker tracker; - private Map map; - - /** - * Creates various objects, including {@link #tracker}. - */ - @Override - @Before - public void setUp() { - super.setUp(); - - tracker = new MyTracker(); - } - - @Test - public void testGetStatus() { - tracker.addData(makeData(policy1, PDP1, PDP2)); - tracker.addData(makeData(policy2, PDP2)); - - List statusList = tracker.getStatus(); - assertEquals(2, statusList.size()); - - Set names = statusList.stream().map(PolicyStatus::getPolicyId).collect(Collectors.toSet()); - assertTrue(names.contains(policy1.getName())); - assertTrue(names.contains(policy2.getName())); - } - - @Test - public void testGetStatusString() { - tracker.addData(makeData(policy1, PDP1, PDP2)); - tracker.addData(makeData(policy2, PDP2)); - - policy3 = new ToscaConceptIdentifier(policy1.getName(), policy1.getVersion() + "0"); - tracker.addData(makeData(policy3, PDP3)); - - List statusList = tracker.getStatus(policy1.getName()); - assertEquals(2, statusList.size()); - - Set idents = - statusList.stream().map(PolicyStatus::getPolicy).collect(Collectors.toSet()); - assertTrue(idents.contains(policy1)); - assertTrue(idents.contains(policy3)); - } - - @Test - public void testGetStatusToscaConceptIdentifier() { - tracker.addData(makeData(policy1, PDP1, PDP2)); - tracker.addData(makeData(policy2, PDP2)); - - policy3 = new ToscaConceptIdentifier(policy1.getName(), policy1.getVersion() + "0"); - tracker.addData(makeData(policy3, PDP3)); - - Optional status = tracker.getStatus(policy1); - assertTrue(status.isPresent()); - - assertEquals(policy1, status.get().getPolicy()); - - // check not-found case - status = tracker.getStatus(policy4); - assertFalse(status.isPresent()); - } - - @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 - assertNotSame(data, map.get(policy2)); - 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 - tracker.removeData(makeData(policy3, PDP1)); - 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 changes to the map - tracker.removeData(makeData(policy2, PDP1)); - 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 - tracker.removeData(makeData(policy2, PDP1, PDP3)); - 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 - tracker.removeData(makeData(policy1, PDP1, PDP2, PDP3)); - 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(type, statusList.get(0).getPolicyType()); - - 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(type, statusList.get(0).getPolicyType()); - - 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(type, statusList.get(0).getPolicyType()); - - 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(type, statusList.get(0).getPolicyType()); - - 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(type, statusList.get(0).getPolicyType()); - 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 deleted file mode 100644 index 742d87d5..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyDeployTrackerTest.java +++ /dev/null @@ -1,250 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019-2020 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(); - } - - /** - * Simple test with one PDP that immediately responds with success. - */ - @Test - public void testSimpleImmediateSuccess() { - tracker.addData(makeData(policy1, PDP1)); - - // 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(type, statusList.get(0).getPolicyType()); - assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has succeeded again - should be no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - - // indicate failure - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(1, statusList.size()); - assertEquals("[0, 1, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has failed again - should be no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(0, statusList.size()); - - // indicate that PDP1 has succeeded again - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString()); - } - - /** - * Simple test with one PDP that immediately responds with success. - */ - @Test - public void testSimpleImmediateFail() { - tracker.addData(makeData(policy1, PDP1)); - - // indicate that PDP1 has failed - List statusList = new ArrayList<>(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(1, statusList.size()); - assertEquals("[0, 1, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has failed again - should be no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(0, statusList.size()); - - // indicate success - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - - assertEquals(1, statusList.size()); - assertEquals(policy1, statusList.get(0).getPolicy()); - assertEquals(type, statusList.get(0).getPolicyType()); - assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has succeeded again - should be no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - } - - /** - * Simple test where PDP is removed and then it responds. - */ - @Test - public void testSimpleRemove() { - tracker.addData(makeData(policy1, PDP1)); - - // remove the PDP - List statusList = new ArrayList<>(); - tracker.removePdp(PDP1, statusList); - assertEquals(1, statusList.size()); - assertEquals(policy1, statusList.get(0).getPolicy()); - assertEquals(type, statusList.get(0).getPolicyType()); - assertEquals("[0, 0, 0]", getCounts(statusList.get(0)).toString()); - - /* - * indicate that PDP1 has succeeded - should be no message since PDP was removed - * from the policy - */ - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - - /* - * indicate that PDP1 has failed - should be no message since PDP was removed - * from the policy - */ - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(0, statusList.size()); - } - - /** - * Test with multiple PDPs. - */ - @Test - public void testMulti() { - tracker.addData(makeData(policy1, PDP1, PDP2)); - - // indicate that PDP2 has succeeded - List statusList = new ArrayList<>(); - tracker.processResponse(PDP2, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - - // indicate that PDP1 has succeeded - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - - assertEquals(1, statusList.size()); - assertEquals(policy1, statusList.get(0).getPolicy()); - assertEquals(type, statusList.get(0).getPolicyType()); - assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has failed - should get a notification - statusList.clear(); - tracker.processResponse(PDP1, Collections.emptyList(), statusList); - assertEquals(1, statusList.size()); - assertEquals(policy1, statusList.get(0).getPolicy()); - assertEquals(type, statusList.get(0).getPolicyType()); - assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has succeeded - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(1, statusList.size()); - assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString()); - - // remove PDP2 - expect message - statusList.clear(); - tracker.removePdp(PDP2, statusList); - assertEquals(1, statusList.size()); - assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString()); - - // re-add PDP2 - tracker.addData(makeData(policy1, PDP2)); - - // indicate that PDP2 has succeeded; PDP1 should still be ok - statusList.clear(); - tracker.processResponse(PDP2, Arrays.asList(policy1), statusList); - assertEquals(1, statusList.size()); - assertEquals("[2, 0, 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/PolicyNotifierTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java index 5e64c939..a3665958 100644 --- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyNotifierTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,44 +21,39 @@ package org.onap.policy.pap.main.notification; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; 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.Iterator; -import java.util.List; -import java.util.Optional; +import java.util.Set; +import javax.ws.rs.core.Response.Status; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.mockito.stubbing.Answer; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pap.concepts.PolicyNotification; import org.onap.policy.models.pap.concepts.PolicyStatus; -import org.onap.policy.models.pdp.concepts.Pdp; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper; import org.onap.policy.pap.main.PolicyPapRuntimeException; import org.onap.policy.pap.main.comm.Publisher; import org.onap.policy.pap.main.comm.QueueToken; -public class PolicyNotifierTest extends PolicyCommonSupport { +public class PolicyNotifierTest { + private static final String GROUP_A = "groupA"; + private static final String PDP1 = "pdp-1"; + private static final ToscaConceptIdentifier policy1 = new ToscaConceptIdentifier("policy1", "1.2.3"); + private static final ToscaConceptIdentifier policy2 = new ToscaConceptIdentifier("policy2", "1.2.3"); @Mock private Publisher publisher; @@ -70,10 +65,7 @@ public class PolicyNotifierTest extends PolicyCommonSupport { private PolicyModelsProvider dao; @Mock - private PolicyDeployTracker deploy; - - @Mock - private PolicyUndeployTracker undeploy; + private DeploymentStatus tracker; @Mock private PolicyStatus status1; @@ -95,16 +87,13 @@ public class PolicyNotifierTest extends PolicyCommonSupport { /** * Creates various objects, including {@link #notifier}. */ - @Override @Before public void setUp() { MockitoAnnotations.initMocks(this); - super.setUp(); - try { when(daoFactory.create()).thenReturn(dao); - when(dao.getPdpGroups(null)).thenReturn(Collections.emptyList()); + when(dao.getGroupPolicyStatus(anyString())).thenReturn(Collections.emptyList()); notifier = new MyNotifier(publisher); @@ -114,154 +103,31 @@ public class PolicyNotifierTest extends PolicyCommonSupport { } @Test - public void testLoadPoliciesPolicyModelsProviderFactoryWrapper() throws PfModelException { - final PdpGroup group1 = makeGroup("my group #1", makeSubGroup("sub #1 A", 2, policy1, policy4), - makeSubGroup("sub #1 B", 1, policy2)); - - // one policy is a duplicate - final PdpGroup group2 = makeGroup("my group #2", makeSubGroup("sub #2 A", 1, policy1, policy3)); - - when(dao.getPdpGroups(null)).thenReturn(Arrays.asList(group1, group2)); - - ToscaConceptIdentifier type2 = new ToscaConceptIdentifier("my other type", "8.8.8"); - - // note: no mapping for policy4 - when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(makePolicy(policy1, type), - makePolicy(policy2, type2), makePolicy(policy3, type))); - - // load it - notifier = new MyNotifier(publisher); - - ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - - // should have added policy1, policy2, policy1 (duplicate), policy3, but not - // policy4 - verify(deploy, times(4)).addData(captor.capture()); - - Iterator iter = captor.getAllValues().iterator(); - PolicyPdpNotificationData data = iter.next(); - assertEquals(policy1, data.getPolicyId()); - assertEquals(type, data.getPolicyType()); - assertEquals("[sub #1 A 0, sub #1 A 1]", data.getPdps().toString()); - - data = iter.next(); - assertEquals(policy2, data.getPolicyId()); - assertEquals(type2, data.getPolicyType()); - assertEquals("[sub #1 B 0]", data.getPdps().toString()); - - data = iter.next(); - assertEquals(policy1, data.getPolicyId()); - assertEquals(type, data.getPolicyType()); - assertEquals("[sub #2 A 0]", data.getPdps().toString()); - - data = iter.next(); - assertEquals(policy3, data.getPolicyId()); - assertEquals(type, data.getPolicyType()); - assertEquals("[sub #2 A 0]", data.getPdps().toString()); - } - - private ToscaPolicy makePolicy(ToscaConceptIdentifier policyId, ToscaConceptIdentifier type) { - ToscaPolicy policy = new ToscaPolicy(); - - policy.setName(policyId.getName()); - policy.setVersion(policyId.getVersion()); - policy.setType(type.getName()); - policy.setTypeVersion(type.getVersion()); - - return policy; - } - - private PdpGroup makeGroup(String name, PdpSubGroup... subgrps) { - final PdpGroup group = new PdpGroup(); - group.setName(name); - - group.setPdpSubgroups(Arrays.asList(subgrps)); - - return group; - } - - private PdpSubGroup makeSubGroup(String name, int numPdps, ToscaConceptIdentifier... policies) { - final PdpSubGroup subgrp = new PdpSubGroup(); - subgrp.setPdpType(name); - subgrp.setPdpInstances(new ArrayList<>(numPdps)); - - for (int x = 0; x < numPdps; ++x) { - Pdp pdp = new Pdp(); - pdp.setInstanceId(name + " " + x); - - subgrp.getPdpInstances().add(pdp); - } - - subgrp.setPolicies(Arrays.asList(policies)); - - return subgrp; - } - - @Test - public void testGetStatus() { - List statusList = Arrays.asList(status1); - when(deploy.getStatus()).thenReturn(statusList); - - assertSame(statusList, notifier.getStatus()); - } - - @Test - public void testGetStatusString() { - List statusList = Arrays.asList(status1); - when(deploy.getStatus("a policy")).thenReturn(statusList); - - assertSame(statusList, notifier.getStatus("a policy")); - } - - @Test - public void testGetStatusToscaConceptIdentifier() { - Optional status = Optional.of(status1); - when(deploy.getStatus(policy1)).thenReturn(status); - - assertSame(status, notifier.getStatus(policy1)); - } - - @Test - public void testAddDeploymentData() { - PolicyPdpNotificationData data = makeData(policy1, PDP1, PDP2); - notifier.addDeploymentData(data); - - verify(deploy).addData(data); - verify(undeploy).removeData(eq(data)); - } - - @Test - public void testAddUndeploymentData() { - PolicyPdpNotificationData data = makeData(policy1, PDP1, PDP2); - notifier.addUndeploymentData(data); - - verify(undeploy).addData(data); - verify(deploy).removeData(eq(data)); - } + public void testProcessResponseString() throws PfModelException { + Set expected = Set.of(policy1); + Set actual = Set.of(policy2); + + // add a status to the notification when tracker.flush(notif) is called + doAnswer(invocation -> { + PolicyNotification notif = invocation.getArgument(0); + notif.getAdded().add(new PolicyStatus()); + return null; + }).when(tracker).flush(any()); - @Test - public void testProcessResponseString() { - doAnswer(addStatus(2, status1, status2)).when(deploy).processResponse(eq(PDP1), any(), any()); - doAnswer(addStatus(2, status3, status4)).when(undeploy).processResponse(eq(PDP1), any(), any()); + notifier.processResponse(PDP1, GROUP_A, expected, actual); - List activePolicies = Arrays.asList(policy1, policy2); - notifier.processResponse(PDP1, activePolicies); + verify(tracker).loadByGroup(GROUP_A); + verify(tracker).completeDeploy(PDP1, expected, actual); + verify(tracker).flush(any()); - PolicyNotification notification = getNotification(); - assertEquals(Arrays.asList(status1, status2), notification.getAdded()); - assertEquals(Arrays.asList(status3, status4), notification.getDeleted()); + verify(publisher).enqueue(any()); } @Test - public void testRemovePdp() { - doAnswer(addStatus(1, status1, status2)).when(deploy).removePdp(eq(PDP1), any()); - doAnswer(addStatus(1, status3, status4)).when(undeploy).removePdp(eq(PDP1), any()); - - notifier.removePdp(PDP1); + public void testProcessResponseString_Ex() throws PfModelException { + doThrow(new PfModelException(Status.BAD_REQUEST, "expected exception")).when(tracker).loadByGroup(anyString()); - PolicyNotification notification = getNotification(); - assertEquals(Arrays.asList(status1, status2), notification.getAdded()); - assertEquals(Arrays.asList(status3, status4), notification.getDeleted()); + assertThatCode(() -> notifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of())).doesNotThrowAnyException(); } /** @@ -269,8 +135,7 @@ public class PolicyNotifierTest extends PolicyCommonSupport { */ @Test public void testPublishEmpty() { - notifier.removePdp(PDP1); - + notifier.publish(new PolicyNotification()); verify(publisher, never()).enqueue(any()); } @@ -279,45 +144,20 @@ public class PolicyNotifierTest extends PolicyCommonSupport { */ @Test public void testPublishNotEmpty() { - doAnswer(addStatus(1, status1, status2)).when(deploy).removePdp(eq(PDP1), any()); + PolicyNotification notif = new PolicyNotification(); + notif.getAdded().add(new PolicyStatus()); - notifier.removePdp(PDP1); + notifier.publish(notif); verify(publisher).enqueue(any()); } @Test - public void testMakeDeploymentTracker_testMakeUndeploymentTracker() throws PfModelException { + public void testMakeDeploymentTracker() throws PfModelException { // make real object, which will invoke the real makeXxx() methods - new PolicyNotifier(publisher, daoFactory).removePdp(PDP1); + new PolicyNotifier(publisher, daoFactory).processResponse(PDP1, GROUP_A, Set.of(), Set.of()); - verify(publisher, never()).enqueue(any()); - } - - /** - * Creates an answer that adds status updates to a status list. - * - * @param listIndex index of the status list within the argument list - * @param status status updates to be added - * @return an answer that adds the given status updates - */ - private Answer addStatus(int listIndex, PolicyStatus... status) { - return invocation -> { - @SuppressWarnings("unchecked") - List statusList = invocation.getArgument(listIndex, List.class); - statusList.addAll(Arrays.asList(status)); - return null; - }; - } - - /** - * Gets the notification that was published. - * - * @return the notification that was published - */ - private PolicyNotification getNotification() { - verify(publisher).enqueue(notifyCaptor.capture()); - return notifyCaptor.getValue().get(); + verify(dao).getGroupPolicyStatus(GROUP_A); } @@ -328,13 +168,8 @@ public class PolicyNotifierTest extends PolicyCommonSupport { } @Override - protected PolicyDeployTracker makeDeploymentTracker() { - return deploy; - } - - @Override - protected PolicyUndeployTracker makeUndeploymentTracker() { - return undeploy; + protected DeploymentStatus makeDeploymentTracker(PolicyModelsProvider dao) { + return tracker; } } } diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationDataTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationDataTest.java deleted file mode 100644 index 67a3cb1f..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyPdpNotificationDataTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021 Nordix Foundation. - * ================================================================================ - * 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.assertSame; -import static org.junit.Assert.assertTrue; - -import java.util.Arrays; -import java.util.Collections; -import java.util.TreeSet; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; - -/** - * Note: this wraps the PDPs in a TreeSet so that the content can be verified without - * worrying about order. - */ -public class PolicyPdpNotificationDataTest { - private static final String PDP1 = "pdp-1"; - private static final String PDP2 = "pdp-2"; - private static final String PDP3 = "pdp-3"; - private static final String PDP4 = "pdp-4"; - - private ToscaConceptIdentifier policyId; - private ToscaConceptIdentifier policyType; - private PolicyPdpNotificationData data; - - /** - * Creates various objects, including {@link #data}. - */ - @Before - public void setUp() { - policyId = new ToscaConceptIdentifier("my-id", "1.2.3"); - policyType = new ToscaConceptIdentifier("my-type", "3.2.1"); - - data = new PolicyPdpNotificationData(policyId, policyType); - } - - @Test - public void testPolicyPdpNotificationData() { - assertSame(policyId, data.getPolicyId()); - assertSame(policyType, data.getPolicyType()); - assertTrue(data.getPdps().isEmpty()); - } - - @Test - public void testIsEmpty() { - assertTrue(data.isEmpty()); - - data.add(PDP1); - assertFalse(data.isEmpty()); - - data.add(PDP2); - data.add(PDP3); - assertFalse(data.isEmpty()); - - data.removeAll(Arrays.asList(PDP1, PDP3)); - assertFalse(data.isEmpty()); - - data.removeAll(Arrays.asList(PDP2)); - assertTrue(data.isEmpty()); - } - - @Test - public void testAdd() { - data.add(PDP1); - assertEquals("[pdp-1]", new TreeSet<>(data.getPdps()).toString()); - - data.add(PDP3); - assertEquals("[pdp-1, pdp-3]", new TreeSet<>(data.getPdps()).toString()); - } - - @Test - public void testAddAll() { - // verify we can add an empty list - data.addAll(Collections.emptyList()); - assertTrue(data.getPdps().isEmpty()); - - // try a non-empty list - data.addAll(Arrays.asList(PDP1, PDP3)); - assertEquals("[pdp-1, pdp-3]", new TreeSet<>(data.getPdps()).toString()); - } - - @Test - public void testRemoveAll() { - // verify we can remove an empty list - data.removeAll(Collections.emptyList()); - assertTrue(data.getPdps().isEmpty()); - - // now test with non-empty lists - data.addAll(Arrays.asList(PDP1, PDP2, PDP3, PDP4)); - - data.removeAll(Arrays.asList(PDP1, PDP3)); - assertEquals("[pdp-2, pdp-4]", new TreeSet<>(data.getPdps()).toString()); - - data.removeAll(Arrays.asList(PDP2, PDP4)); - assertEquals("[]", new TreeSet<>(data.getPdps()).toString()); - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java b/main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java deleted file mode 100644 index 2d27b75e..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java +++ /dev/null @@ -1,350 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021 Nordix Foundation. - * ================================================================================ - * 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.assertSame; -import static org.junit.Assert.assertTrue; - -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.models.pap.concepts.PolicyStatus; -import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; - -public class PolicyTrackerDataTest { - - private static final ToscaConceptIdentifier TYPE = new ToscaConceptIdentifier("my-type", "1.2.3"); - private static final String PDP1 = "pdp-1"; - private static final String PDP2 = "pdp-2"; - private static final String PDP3 = "pdp-3"; - private static final String PDP4 = "pdp-4"; - private static final String PDP5 = "pdp-5"; - private static final String PDP6 = "pdp-6"; - - private Collection fullSet; - private PolicyTrackerData data; - - @Before - public void setUp() { - fullSet = Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6); - data = new PolicyTrackerData(TYPE); - } - - @Test - public void testPolicyTrackerData_testGetPolicyType() { - assertSame(TYPE, data.getPolicyType()); - } - - @Test - public void testIsComplete() { - assertTrue(data.isComplete()); - - data.addPdps(Arrays.asList(PDP1, PDP2)); - assertFalse(data.isComplete()); - - data.success(PDP1); - assertFalse(data.isComplete()); - - data.fail(PDP2); - assertTrue(data.isComplete()); - } - - @Test - public void testAllSucceeded() { - assertTrue(data.allSucceeded()); - - data.addPdps(Arrays.asList(PDP1, PDP2)); - assertFalse(data.allSucceeded()); - - data.success(PDP1); - assertFalse(data.allSucceeded()); - - data.fail(PDP2); - assertFalse(data.allSucceeded()); - - data.success(PDP2); - assertTrue(data.allSucceeded()); - - data.fail(PDP2); - assertFalse(data.allSucceeded()); - - data.success(PDP2); - assertTrue(data.allSucceeded()); - } - - @Test - public void testIsEmpty() { - assertTrue(data.isEmpty()); - - data.addPdps(Arrays.asList(PDP1, PDP2)); - assertFalse(data.isEmpty()); - - data.success(PDP1); - assertFalse(data.isEmpty()); - - data.fail(PDP2); - assertFalse(data.isEmpty()); - - data.removePdp(PDP1); - assertFalse(data.isEmpty()); - - data.removePdp(PDP2); - assertTrue(data.isEmpty()); - } - - @Test - public void testPutValuesInto() { - data.addPdps(fullSet); - data.success(PDP1); - data.fail(PDP2); - data.fail(PDP3); - - PolicyStatus status = new PolicyStatus(); - data.putValuesInto(status); - - assertEquals(1, status.getSuccessCount()); - assertEquals(2, status.getFailureCount()); - assertEquals(3, status.getIncompleteCount()); - } - - @Test - public void testAddPdps_testSuccess_testFail() { - data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4)); - assertEquals("[0, 0, 4]", getCounts().toString()); - - data.success(PDP1); - assertEquals("[1, 0, 3]", getCounts().toString()); - - data.success(PDP2); - assertEquals("[2, 0, 2]", getCounts().toString()); - - // repeat - data.success(PDP2); - assertEquals("[2, 0, 2]", getCounts().toString()); - - data.fail(PDP3); - assertEquals("[2, 1, 1]", getCounts().toString()); - - // repeat - data.fail(PDP3); - assertEquals("[2, 1, 1]", getCounts().toString()); - - data.addPdps(Arrays.asList(PDP2, PDP3, PDP4, PDP5)); - - // PDP1 is still success - assertEquals("[1, 0, 4]", getCounts().toString()); - } - - @Test - public void testRemovePdps() { - data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6)); - data.success(PDP1); - data.success(PDP2); - data.fail(PDP3); - data.fail(PDP4); - assertFalse(data.removePdps(Arrays.asList(PDP1, PDP3, PDP5))); - assertEquals("[1, 1, 1]", getCounts().toString()); - - assertTrue(data.removePdps(Arrays.asList(PDP6))); - assertEquals("[1, 1, 0]", getCounts().toString()); - } - - /** - * Tests removePdps(), where nothing is removed from the "incomplete" set. - */ - @Test - public void testRemovePdpsNoIncompleteRemove() { - assertFalse(data.removePdps(Arrays.asList(PDP1, PDP2))); - assertEquals("[0, 0, 0]", getCounts().toString()); - } - - /** - * Tests removePdps(), where remaining incomplete items are removed. - */ - @Test - public void testRemovePdpsAllComplete() { - data.addPdps(Arrays.asList(PDP1)); - assertTrue(data.removePdps(Arrays.asList(PDP1))); - - data.addPdps(Arrays.asList(PDP1, PDP2, PDP3)); - assertFalse(data.removePdps(Arrays.asList(PDP1))); - assertTrue(data.removePdps(Arrays.asList(PDP2, PDP3))); - } - - /** - * Tests removePdps() with more variations. - */ - @Test - public void testRemovePdpsVariations() { - data.addPdps(Arrays.asList(PDP1, PDP2, PDP3)); - data.success(PDP1); - data.fail(PDP2); - assertEquals("[1, 1, 1]", getCounts().toString()); - - // remove PDP1, which checks removal from "success" set, while incomplete - assertFalse(data.removePdps(Arrays.asList(PDP1))); - assertEquals("[0, 1, 1]", getCounts().toString()); - - // remove PDP2, which checks removal from "failure" set, while incomplete - assertFalse(data.removePdps(Arrays.asList(PDP2))); - assertEquals("[0, 0, 1]", getCounts().toString()); - - // re-add 1 & 2 - data.addPdps(Arrays.asList(PDP1, PDP2)); - data.success(PDP1); - data.fail(PDP2); - assertEquals("[1, 1, 1]", getCounts().toString()); - - // remove PDP3, which checks removal from "incomplete" set - assertTrue(data.removePdps(Arrays.asList(PDP3))); - assertEquals("[1, 1, 0]", getCounts().toString()); - - // remove PDP1, which checks removal from "success" set, while complete - assertTrue(data.removePdps(Arrays.asList(PDP1))); - assertEquals("[0, 1, 0]", getCounts().toString()); - - // remove PDP2, which checks removal from "failure" set, while complete - assertTrue(data.removePdps(Arrays.asList(PDP2))); - assertEquals("[0, 0, 0]", getCounts().toString()); - - // re-add 1 and then remove it again - data.addPdps(Arrays.asList(PDP1)); - assertTrue(data.removePdps(Arrays.asList(PDP1))); - assertEquals("[0, 0, 0]", getCounts().toString()); - } - - @Test - public void testRemovePdp() { - data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6)); - data.success(PDP1); - data.success(PDP2); - data.fail(PDP3); - data.fail(PDP4); - - assertFalse(data.removePdp(PDP1)); - assertEquals("[1, 2, 2]", getCounts().toString()); - - assertFalse(data.removePdp(PDP2)); - assertEquals("[0, 2, 2]", getCounts().toString()); - - assertFalse(data.removePdp(PDP3)); - assertEquals("[0, 1, 2]", getCounts().toString()); - - assertFalse(data.removePdp(PDP4)); - assertEquals("[0, 0, 2]", getCounts().toString()); - - assertFalse(data.removePdp(PDP5)); - assertEquals("[0, 0, 1]", getCounts().toString()); - - assertTrue(data.removePdp(PDP6)); - assertEquals("[0, 0, 0]", getCounts().toString()); - } - - /** - * Tests removePdps(), where nothing is removed from the "incomplete" set. - */ - @Test - public void testRemovePdpNoIncompleteRemove() { - assertFalse(data.removePdp(PDP1)); - assertEquals("[0, 0, 0]", getCounts().toString()); - } - - /** - * Tests removePdps(), where remaining incomplete items are removed. - */ - @Test - public void testRemovePdpAllComplete() { - data.addPdps(Arrays.asList(PDP1, PDP2)); - assertFalse(data.removePdp(PDP1)); - - assertTrue(data.removePdp(PDP2)); - } - - /** - * Tests removePdp() with more variations. - */ - @Test - public void testRemovePdpVariations() { - data.addPdps(Arrays.asList(PDP1, PDP2, PDP3)); - data.success(PDP1); - data.fail(PDP2); - assertEquals("[1, 1, 1]", getCounts().toString()); - - // remove PDP1, which checks removal from "success" set, while incomplete - assertFalse(data.removePdp(PDP1)); - assertEquals("[0, 1, 1]", getCounts().toString()); - - // remove PDP2, which checks removal from "failure" set, while incomplete - assertFalse(data.removePdp(PDP2)); - assertEquals("[0, 0, 1]", getCounts().toString()); - - // re-add 1 & 2 - data.addPdps(Arrays.asList(PDP1, PDP2)); - data.success(PDP1); - data.fail(PDP2); - assertEquals("[1, 1, 1]", getCounts().toString()); - - // remove PDP3, which checks removal from "incomplete" set - assertTrue(data.removePdp(PDP3)); - assertEquals("[1, 1, 0]", getCounts().toString()); - - // remove PDP1, which checks removal from "success" set, while complete - assertTrue(data.removePdp(PDP1)); - assertEquals("[0, 1, 0]", getCounts().toString()); - - // remove PDP2, which checks removal from "failure" set, while complete - assertTrue(data.removePdp(PDP2)); - assertEquals("[0, 0, 0]", getCounts().toString()); - - // re-add 1 and then remove it again - data.addPdps(Arrays.asList(PDP1)); - assertTrue(data.removePdp(PDP1)); - assertEquals("[0, 0, 0]", getCounts().toString()); - } - - @Test - public void testComplete() { - // attempt to remove a PDP that isn't in the data - assertFalse(data.success(PDP1)); - - // remove one that was incomplete - data.addPdps(Arrays.asList(PDP1)); - assertTrue(data.success(PDP1)); - - // move from one set to the other - assertTrue(data.fail(PDP1)); - - // already in the correct set - assertFalse(data.fail(PDP1)); - } - - private List getCounts() { - PolicyStatus status = new PolicyStatus(); - data.putValuesInto(status); - - return Arrays.asList(status.getSuccessCount(), status.getFailureCount(), status.getIncompleteCount()); - } -} 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 deleted file mode 100644 index 7d3f54fb..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/notification/PolicyUndeployTrackerTest.java +++ /dev/null @@ -1,264 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019-2020 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(); - } - - /** - * Simple test with one PDP that immediately responds with success. - */ - @Test - public void testSimpleImmediateSuccess() { - tracker.addData(makeData(policy1, PDP1)); - - // indicate that PDP1 has succeeded (i.e., undeployed) - List statusList = new ArrayList<>(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - - assertEquals(1, statusList.size()); - assertEquals(policy1, statusList.get(0).getPolicy()); - assertEquals(type, statusList.get(0).getPolicyType()); - assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has succeeded again - should be no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(0, statusList.size()); - - // indicate failure (i.e., still deployed) - no output, because no longer tracked - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - - // indicate that PDP1 has failed again - still no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - - // indicate that PDP1 has succeeded again - still no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(0, statusList.size()); - } - - /** - * Simple test with one PDP that immediately responds with success. - */ - @Test - public void testSimpleImmediateFail() { - tracker.addData(makeData(policy1, PDP1)); - - // indicate that PDP1 has failed (i.e., still deployed) - List statusList = new ArrayList<>(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(1, statusList.size()); - assertEquals("[0, 1, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has failed again - should be no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - - // indicate success (i.e., undeployed) - tracker.processResponse(PDP1, Arrays.asList(), statusList); - - assertEquals(1, statusList.size()); - assertEquals(policy1, statusList.get(0).getPolicy()); - assertEquals(type, statusList.get(0).getPolicyType()); - assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString()); - - // indicate that PDP1 has succeeded again - should be no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(0, statusList.size()); - - // indicate that PDP1 has failed again - still no output - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - } - - /** - * Simple test where PDP is removed and then it responds. - */ - @Test - public void testSimpleRemove() { - tracker.addData(makeData(policy1, PDP1)); - - // remove the PDP - List statusList = new ArrayList<>(); - tracker.removePdp(PDP1, statusList); - assertEquals(1, statusList.size()); - assertEquals(policy1, statusList.get(0).getPolicy()); - assertEquals(type, statusList.get(0).getPolicyType()); - assertEquals("[0, 0, 0]", getCounts(statusList.get(0)).toString()); - - /* - * indicate that PDP1 has succeeded (i.e., undeployed) - should be no message - * since PDP was removed from the policy - */ - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(0, statusList.size()); - - /* - * indicate that PDP1 has failed (i.e., still deployed) - should be no message - * since PDP was removed from the policy - */ - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - assertEquals(0, statusList.size()); - } - - /** - * Test with multiple PDPs. - */ - @Test - public void testMulti() { - 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(type, statusList.get(0).getPolicyType()); - 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 with multiple PDPs, and one is removed while it is in a failure state. - */ - @Test - public void testMultiRemove() { - tracker.addData(makeData(policy1, PDP1, PDP2)); - - // indicate that PDP2 has been undeployed - tracker.processResponse(PDP2, Collections.emptyList(), new ArrayList<>(0)); - - // indicate that PDP1 has failed (i.e., still deployed) - List statusList = new ArrayList<>(); - tracker.processResponse(PDP1, Arrays.asList(policy1), statusList); - - assertEquals(1, statusList.size()); - assertEquals(policy1, statusList.get(0).getPolicy()); - assertEquals(type, statusList.get(0).getPolicyType()); - assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString()); - - // remove PDP1 - expect message AND policy should be removed - statusList.clear(); - tracker.removePdp(PDP1, statusList); - assertEquals(1, statusList.size()); - assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString()); - - // re-add PDP1 - tracker.addData(makeData(policy1, PDP1)); - - // indicate that PDP1 has succeeded; policy is now new, so doesn't include PDP2 - statusList.clear(); - tracker.processResponse(PDP1, Arrays.asList(), statusList); - assertEquals(1, statusList.size()); - assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString()); - } - - @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 has succeeded - when(data.allSucceeded()).thenReturn(true); - assertTrue(tracker.shouldRemove(data)); - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java index a0fa196e..4241b41f 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 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. @@ -20,6 +20,7 @@ package org.onap.policy.pap.main.rest; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -42,6 +43,7 @@ import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pap.concepts.PolicyNotification; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.pdp.concepts.PdpStateChange; @@ -266,6 +268,15 @@ public class ProviderSuper { } } + /** + * Verifies that an empty notification was published. + */ + protected void checkEmptyNotification() { + ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyNotification.class); + verify(notifier).publish(captor.capture()); + assertThat(captor.getValue().isEmpty()).isTrue(); + } + /** * Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap * the list and decode it into the wrapper before extracting the list contents. diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java index a753db1e..64534530 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupCreateOrUpdateProvider.java @@ -3,6 +3,7 @@ * ONAP PAP * ================================================================================ * Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -284,7 +285,7 @@ public class TestPdpGroupCreateOrUpdateProvider extends ProviderSuper { assertEquals(newgrp.toString(), dbgroup.toString()); // no deployment notifications - verify(notifier, never()).addDeploymentData(any()); + checkEmptyNotification(); // this requires a PDP UPDATE message List pdpUpdates = getUpdateRequests(2); @@ -519,8 +520,7 @@ public class TestPdpGroupCreateOrUpdateProvider extends ProviderSuper { assertEquals(newgrp.toString(), group.toString()); // no notifications - verify(notifier, never()).addDeploymentData(any()); - verify(notifier, never()).addUndeploymentData(any()); + checkEmptyNotification(); // no group updates assertNoGroupAction(); diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java index 99aa3542..fc71c62c 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2020-2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -242,7 +242,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { assertEquals(origSize - 1, subgroup.getPolicies().size()); assertFalse(subgroup.getPolicies().contains(ident)); - verify(session).trackUndeploy(eq(ident), pdpCaptor.capture()); + verify(session).trackUndeploy(eq(ident), pdpCaptor.capture(), eq(group.getName()), eq(subgroup.getPdpType())); assertEquals("[pdpA]", pdpCaptor.getValue().toString()); } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java index b6763008..a7f48ff3 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,7 +27,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.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; @@ -35,13 +34,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -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; @@ -56,7 +53,6 @@ import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; public class TestPdpGroupDeployProvider extends ProviderSuper { private static final String EXPECTED_EXCEPTION = "expected exception"; @@ -164,7 +160,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { } /** - * Tests updateGroupPolicies when policies are being added and deleted in the same subgroup. + * Tests updateGroupPolicies when policies are being added and deleted in the same + * subgroup. */ @Test public void testUpdateGroupPoliciesAddAndDelete() throws Exception { @@ -353,14 +350,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertEquals(newgrp.toString(), group.toString()); - // should have notified of added policy/PDPs - ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier, times(2)).addDeploymentData(captor.capture()); - assertDeploymentData(captor.getAllValues().get(0), policyId2, "[pdpA]"); - assertDeploymentData(captor.getAllValues().get(1), policyId3, "[pdpA]"); - - // should NOT have notified of any deleted policy/PDPs - verify(notifier, never()).addUndeploymentData(any()); + // nothing is complete - notification should be empty + checkEmptyNotification(); // this requires a PDP UPDATE message assertGroupUpdate(newgrp, subgrp); @@ -426,8 +417,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertEquals(newgrp.toString(), group.toString()); // no notifications - verify(notifier, never()).addDeploymentData(any()); - verify(notifier, never()).addUndeploymentData(any()); + checkEmptyNotification(); // no group updates assertNoGroupAction(); @@ -472,8 +462,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); - assertThatThrownBy(() -> prov.updateGroupPolicies(groups)) - .isInstanceOf(PfModelException.class) + assertThatThrownBy(() -> prov.updateGroupPolicies(groups)).isInstanceOf(PfModelException.class) .hasMessageContaining(newgrp.getPdpSubgroups().get(0).getPolicies().get(0).getName()) .hasMessageContaining("not a supported policy for the subgroup"); @@ -511,13 +500,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { List requests = getUpdateRequests(1); assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); - // should have notified of added policy/PDPs - ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier).addDeploymentData(captor.capture()); - assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB]"); - - // no undeployment notifications - verify(notifier, never()).addUndeploymentData(any()); + // nothing is complete - notification should be empty + checkEmptyNotification(); } @Test @@ -581,13 +565,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4); - // should have notified of added policy/PDPs - ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier).addDeploymentData(captor.capture()); - assertDeploymentData(captor.getValue(), policy1.getIdentifier(), "[pdpB, pdpD]"); - - // no undeployment notifications - verify(notifier, never()).addUndeploymentData(any()); + // nothing is complete - notification should be empty + checkEmptyNotification(); } @Test @@ -661,13 +640,6 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertEquals(Arrays.asList(group), updates); } - private void assertDeploymentData(PolicyPdpNotificationData data, ToscaConceptIdentifier policyId, - String expectedPdps) { - 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/TestPolicyStatusProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java new file mode 100644 index 00000000..44c8d828 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyStatusProvider.java @@ -0,0 +1,152 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 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.rest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import lombok.NonNull; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pap.concepts.PolicyStatus; +import org.onap.policy.models.pdp.concepts.PdpPolicyStatus; +import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.PdpPolicyStatusBuilder; +import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; + +public class TestPolicyStatusProvider extends ProviderSuper { + private static final String MY_GROUP = "MyGroup"; + private static final String MY_PDP_TYPE = "MySubGroup"; + private static final @NonNull String VERSION = "1.2.3"; + private static final String PDP_A = "pdpA"; + private static final String PDP_B = "pdpB"; + private static final String PDP_C = "pdpC"; + private static final ToscaConceptIdentifier POLICY_TYPE = new ToscaConceptIdentifier("MyPolicyType", VERSION); + private static final ToscaConceptIdentifier POLICY_A = new ToscaConceptIdentifier("MyPolicyA", VERSION); + private static final ToscaConceptIdentifier POLICY_B = new ToscaConceptIdentifier("MyPolicyB", VERSION); + private static final ToscaConceptIdentifier POLICY_C = new ToscaConceptIdentifier("MyPolicyC", VERSION); + + private PolicyStatusProvider prov; + + + @AfterClass + public static void tearDownAfterClass() { + Registry.newRegistry(); + } + + /** + * Configures mocks and objects. + * + * @throws Exception if an error occurs + */ + @Override + @Before + public void setUp() throws Exception { + + super.setUp(); + + prov = new PolicyStatusProvider(); + } + + @Test + public void testGetStatus_testAccumulate() throws PfModelException { + + PdpPolicyStatusBuilder builder = PdpPolicyStatus.builder().pdpGroup(MY_GROUP).pdpType(MY_PDP_TYPE) + .policyType(POLICY_TYPE).state(State.WAITING); + + PdpPolicyStatus notDeployed = builder.deploy(false).policy(POLICY_B).pdpId(PDP_A).build(); + + // remaining policies are deployed + builder.deploy(true); + + // @formatter:off + when(dao.getAllPolicyStatus()).thenReturn(List.of( + builder.policy(POLICY_A).pdpId(PDP_A).build(), + builder.policy(POLICY_A).pdpId(PDP_B).build(), + notDeployed, + builder.policy(POLICY_C).pdpId(PDP_A).build() + )); + // @formatter:on + + List result = new ArrayList<>(prov.getStatus()); + Collections.sort(result, (rec1, rec2) -> rec1.getPolicy().compareTo(rec2.getPolicy())); + + assertThat(result).hasSize(2); + + Iterator iter = result.iterator(); + + PolicyStatus status = iter.next(); + assertThat(status.getPolicy()).isEqualTo(POLICY_A); + assertThat(status.getPolicyType()).isEqualTo(POLICY_TYPE); + assertThat(status.getIncompleteCount()).isEqualTo(2); + assertThat(status.getFailureCount()).isZero(); + assertThat(status.getSuccessCount()).isZero(); + + status = iter.next(); + assertThat(status.getPolicy()).isEqualTo(POLICY_C); + assertThat(status.getPolicyType()).isEqualTo(POLICY_TYPE); + assertThat(status.getIncompleteCount()).isEqualTo(1); + assertThat(status.getFailureCount()).isZero(); + assertThat(status.getSuccessCount()).isZero(); + } + + @Test + public void testGetStatusToscaConceptIdentifierOptVersion() throws PfModelException { + + PdpPolicyStatusBuilder builder = PdpPolicyStatus.builder().pdpGroup(MY_GROUP).pdpType(MY_PDP_TYPE) + .policy(POLICY_A).policyType(POLICY_TYPE); + + PdpPolicyStatus notDeployed = builder.deploy(false).pdpId(PDP_B).state(State.FAILURE).build(); + + // remaining policies are deployed + builder.deploy(true).state(State.WAITING); + + ToscaConceptIdentifierOptVersion optIdent = new ToscaConceptIdentifierOptVersion(POLICY_A); + + // @formatter:off + when(dao.getAllPolicyStatus(optIdent)).thenReturn(List.of( + builder.policy(POLICY_A).pdpId(PDP_A).build(), + notDeployed, + builder.policy(POLICY_A).pdpId(PDP_C).build() + )); + // @formatter:on + + List result = new ArrayList<>(prov.getStatus(optIdent)); + assertThat(result).hasSize(1); + + Iterator iter = result.iterator(); + + PolicyStatus status = iter.next(); + assertThat(status.getPolicy()).isEqualTo(POLICY_A); + assertThat(status.getPolicyType()).isEqualTo(POLICY_TYPE); + assertThat(status.getIncompleteCount()).isEqualTo(2); + assertThat(status.getFailureCount()).isZero(); + assertThat(status.getSuccessCount()).isZero(); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java index 060173bb..b2c67c93 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,7 +27,6 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.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; @@ -36,12 +35,10 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Queue; -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; @@ -52,7 +49,6 @@ import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; import org.powermock.reflect.Whitebox; public class TestProviderBase extends ProviderSuper { @@ -110,23 +106,7 @@ public class TestProviderBase extends ProviderSuper { assertUpdate(getUpdateRequests(1), GROUP1_NAME, PDP1_TYPE, PDP1); - ArgumentCaptor 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 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 pdps = new TreeSet<>(captor.getAllValues().get(0).getPdps()); - pdps.addAll(captor.getAllValues().get(1).getPdps()); - assertEquals("[" + firstPdp + ", " + secondPdp + "]", pdps.toString()); + checkEmptyNotification(); } @Test @@ -376,15 +356,17 @@ public class TestProviderBase extends ProviderSuper { return (group, subgroup) -> { if (shouldUpdate.remove()) { + ToscaConceptIdentifier ident1 = policy.getIdentifier(); + // queue indicated that the update should succeed - subgroup.getPolicies().add(policy.getIdentifier()); + subgroup.getPolicies().add(ident1); - data.trackDeploy(policy.getIdentifier(), Collections.singleton(PDP1)); - data.trackUndeploy(policy.getIdentifier(), Collections.singleton(PDP2)); + data.trackDeploy(ident1, Collections.singleton(PDP1), GROUP1_NAME, PDP1_TYPE); + data.trackUndeploy(ident1, Collections.singleton(PDP2), GROUP1_NAME, PDP2_TYPE); ToscaConceptIdentifier ident2 = new ToscaConceptIdentifier(POLICY1_NAME, "9.9.9"); - data.trackDeploy(ident2, Collections.singleton(PDP3)); - data.trackUndeploy(ident2, Collections.singleton(PDP4)); + data.trackDeploy(ident2, Collections.singleton(PDP3), GROUP1_NAME, PDP3_TYPE); + data.trackUndeploy(ident2, Collections.singleton(PDP4), GROUP1_NAME, PDP4_TYPE); return true; } else { diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java index a2914cfa..3fbe4785 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,6 +21,7 @@ package org.onap.policy.pap.main.rest; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -30,6 +31,8 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -42,26 +45,27 @@ 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; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pap.concepts.PolicyNotification; import org.onap.policy.models.pdp.concepts.PdpGroup; 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.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; 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.ToscaPolicyType; -import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; +import org.onap.policy.pap.main.notification.DeploymentStatus; public class TestSessionData extends ProviderSuper { private static final String GROUP_NAME = "groupA"; + private static final String PDP_TYPE = "MySubGroup"; private static final String PDP1 = "pdp_1"; private static final String PDP2 = "pdp_2"; private static final String PDP3 = "pdp_3"; @@ -505,7 +509,9 @@ public class TestSessionData extends ProviderSuper { session.update(newgrp5); // push the changes to the DB - session.updateDb(); + PolicyNotification notif = new PolicyNotification(); + session.updateDb(notif); + assertThat(notif.getAdded()).isEmpty(); // expect one create for groups 4 & 5 (group5 replaced by newgrp5) List creates = getGroupCreates(); @@ -526,7 +532,10 @@ public class TestSessionData extends ProviderSuper { when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2)); session.getActivePdpGroupsByPolicyType(type); - session.updateDb(); + PolicyNotification notif = new PolicyNotification(); + session.updateDb(notif); + assertThat(notif.getAdded()).isEmpty(); + verify(dao, never()).createPdpGroups(any()); verify(dao, never()).updatePdpGroups(any()); } @@ -540,38 +549,26 @@ public class TestSessionData extends ProviderSuper { @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); + testTrack(true); } @Test public void testTrackUndeploy() throws PfModelException { - testTrack(session::getUndeployData, session::getDeployData, session::trackUndeploy); + testTrack(false); } - /** - * 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> expected, - Supplier> unexpected, TrackEx... trackFuncs) + protected void testTrack(boolean deploy) throws PfModelException { + DeploymentStatus status = mock(DeploymentStatus.class); + + session = new SessionData(dao) { + @Override + protected DeploymentStatus makeDeploymentStatus(PolicyModelsProvider dao) { + return status; + } + }; + ToscaPolicy policy = makePolicy(POLICY_NAME, POLICY_VERSION); policy.setType(POLICY_TYPE); policy.setTypeVersion(POLICY_TYPE_VERSION); @@ -581,25 +578,20 @@ public class TestSessionData extends ProviderSuper { ToscaConceptIdentifier policyId = new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION); List pdps = Arrays.asList(PDP1, PDP2); - for (TrackEx trackFunc : trackFuncs) { - trackFunc.accept(policyId, pdps); + if (deploy) { + session.trackDeploy(policyId, pdps, GROUP_NAME, PDP_TYPE); + } else { + session.trackUndeploy(policyId, pdps, GROUP_NAME, PDP_TYPE); } - // "unexpected" list should be empty of any PDPs - Collection 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()); + // should be called just once + verify(status).deleteDeployment(any(), anyBoolean()); + verify(status).deleteDeployment(policyId, !deploy); - PolicyPdpNotificationData data = dataList.iterator().next(); - assertEquals(policyId, data.getPolicyId()); - assertEquals(type, data.getPolicyType()); - assertEquals("[pdp_1, pdp_2]", new TreeSet<>(data.getPdps()).toString()); + // should be called for each PDP + verify(status, times(2)).deploy(any(), any(), any(), any(), any(), anyBoolean()); + verify(status).deploy(PDP1, policyId, policy.getTypeIdentifier(), GROUP_NAME, PDP_TYPE, deploy); + verify(status).deploy(PDP2, policyId, policy.getTypeIdentifier(), GROUP_NAME, PDP_TYPE, deploy); } private PdpUpdate makeUpdate(String pdpName) { @@ -655,9 +647,4 @@ public class TestSessionData extends ProviderSuper { private String getName(Pair pair) { return (pair.getKey() != null ? pair.getKey().getName() : pair.getValue().getName()); } - - @FunctionalInterface - private static interface TrackEx { - public void accept(ToscaConceptIdentifier policyId, Collection pdps) throws PfModelException; - } } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java index 3f4015b1..1ecea074 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/End2EndBase.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -37,6 +37,7 @@ import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpPolicyStatus; import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -77,17 +78,7 @@ public class End2EndBase extends CommonPapRestServer { */ @BeforeClass public static void setUpBeforeClass() throws Exception { - setUpBeforeClass(true); - } - - /** - * Starts Main, if specified, and connects to the DB. - * - * @param shouldStart {@code true} if Main should be started, {@code false} otherwise - * @throws Exception if an error occurs - */ - public static void setUpBeforeClass(final boolean shouldStart) throws Exception { - CommonPapRestServer.setUpBeforeClass(shouldStart); + CommonPapRestServer.setUpBeforeClass(true); final PapParameterGroup params = new StandardCoder().decode(new File(CONFIG_FILE), PapParameterGroup.class); daoFactory = new PolicyModelsProviderFactoryWrapper(params.getDatabaseProviderParameters()); @@ -194,6 +185,17 @@ public class End2EndBase extends CommonPapRestServer { return dbConn.getFilteredPdpStatistics(instanceId, groupName, subGroupName, null, null, null, 0); } + /** + * Adds PdpPolicyStatus records to the DB. + * + * @param jsonFile name of the JSON file specifying the data to be loaded + * @throws PfModelException if a DAO error occurs + */ + public static void addPdpPolicyStatus(final String jsonFile) throws PfModelException { + final PolicyStatusRecords data = loadJsonFile(jsonFile, PolicyStatusRecords.class); + dbConn.cudPolicyStatus(data.records, null, null); + } + /** * Loads an object from a YAML file. * @@ -246,4 +248,8 @@ public class End2EndBase extends CommonPapRestServer { throw new RuntimeException(e); } } + + public class PolicyStatusRecords { + private List records; + } } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java index afabb892..19a5f990 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyStatusTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 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. @@ -40,14 +40,8 @@ public class PolicyStatusTest extends End2EndBase { */ @BeforeClass public static void setUpBeforeClass() throws Exception { - // don't start Main until AFTER we add the policies to the DB - End2EndBase.setUpBeforeClass(false); - - addToscaPolicyTypes("monitoring.policy-type.yaml"); - addToscaPolicies("monitoring.policy.yaml"); - addGroups("policyStatus.json"); - - startMain(); + End2EndBase.setUpBeforeClass(); + addPdpPolicyStatus("policyStatus.json"); } @Test -- cgit 1.2.3-korg