From 6e96359492029ca9309ea367372ab81bf396bdde Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Tue, 3 Dec 2019 10:15:31 -0500 Subject: Move deploy/undeploy REST classes Moved classes from rest.depundep to rest, as they will no longer be specific to deploy/undeploy. Change-Id: Iea7c4e35fcd00ed83e678f7cd980e0aa9659f18e Issue-ID: POLICY-1866 Signed-off-by: Jim Hahn --- .../onap/policy/pap/main/rest/ProviderSuper.java | 276 ++++++ .../onap/policy/pap/main/rest/TestGroupData.java | 103 +++ .../main/rest/TestPdpGroupDeleteControllerV1.java | 104 +++ .../pap/main/rest/TestPdpGroupDeleteProvider.java | 297 +++++++ .../main/rest/TestPdpGroupDeployControllerV1.java | 112 +++ .../pap/main/rest/TestPdpGroupDeployProvider.java | 932 +++++++++++++++++++++ .../pap/main/rest/TestPolicyUndeployerImpl.java | 200 +++++ .../policy/pap/main/rest/TestProviderBase.java | 392 +++++++++ .../onap/policy/pap/main/rest/TestSessionData.java | 662 +++++++++++++++ .../pap/main/rest/depundep/ProviderSuper.java | 276 ------ .../pap/main/rest/depundep/TestGroupData.java | 103 --- .../depundep/TestPdpGroupDeleteControllerV1.java | 105 --- .../rest/depundep/TestPdpGroupDeleteProvider.java | 297 ------- .../depundep/TestPdpGroupDeployControllerV1.java | 113 --- .../rest/depundep/TestPdpGroupDeployProvider.java | 932 --------------------- .../rest/depundep/TestPolicyUndeployerImpl.java | 200 ----- .../pap/main/rest/depundep/TestProviderBase.java | 392 --------- .../pap/main/rest/depundep/TestSessionData.java | 662 --------------- 18 files changed, 3078 insertions(+), 3080 deletions(-) create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestGroupData.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyUndeployerImpl.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java create mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestGroupData.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteControllerV1.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployControllerV1.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPolicyUndeployerImpl.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java delete mode 100644 main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java (limited to 'main/src/test') 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 new file mode 100644 index 00000000..852516b7 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/ProviderSuper.java @@ -0,0 +1,276 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.Before; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.common.utils.coder.Coder; +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.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.provider.PolicyModelsProvider; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.pap.main.PapConstants; +import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper; +import org.onap.policy.pap.main.comm.PdpModifyRequestMap; +import org.onap.policy.pap.main.notification.PolicyNotifier; + +/** + * Super class for TestPdpGroupDeployProviderXxx classes. + */ +public class ProviderSuper { + private static final Coder coder = new StandardCoder(); + + @Mock + protected PolicyModelsProvider dao; + + @Mock + protected PolicyNotifier notifier; + + + /** + * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups(). + */ + @Captor + private ArgumentCaptor> updateCaptor; + + protected Object lockit; + protected PdpModifyRequestMap reqmap; + protected PolicyModelsProviderFactoryWrapper daofact; + protected ToscaPolicy policy1; + + + /** + * Configures DAO, captors, and various mocks. + */ + @Before + public void setUp() throws Exception { + + Registry.newRegistry(); + + MockitoAnnotations.initMocks(this); + + reqmap = mock(PdpModifyRequestMap.class); + + lockit = new Object(); + daofact = mock(PolicyModelsProviderFactoryWrapper.class); + policy1 = loadPolicy("policy.json"); + + when(daofact.create()).thenReturn(dao); + + List groups = loadGroups("groups.json"); + + when(dao.getFilteredPdpGroups(any())).thenReturn(groups); + + when(dao.createPdpGroups(any())).thenAnswer(answer -> answer.getArgumentAt(0, List.class)); + when(dao.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgumentAt(0, List.class)); + + Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit); + Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap); + Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact); + Registry.register(PapConstants.REG_POLICY_NOTIFIER, notifier); + } + + protected void assertGroup(List groups, String name) { + PdpGroup group = groups.remove(0); + + assertEquals(name, group.getName()); + } + + protected void assertUpdateIgnorePolicy(List updates, String groupName, String pdpType, String pdpName) { + + PdpUpdate update = updates.remove(0); + + assertEquals(groupName, update.getPdpGroup()); + assertEquals(pdpType, update.getPdpSubgroup()); + assertEquals(pdpName, update.getName()); + } + + /** + * Gets the input to the create() method. + * + * @return the input that was passed to the dao.updatePdpGroups() method + * @throws Exception if an error occurred + */ + protected List getGroupCreates() throws Exception { + verify(dao).createPdpGroups(updateCaptor.capture()); + + return copyList(updateCaptor.getValue()); + } + + /** + * Gets the input to the update() method. + * + * @return the input that was passed to the dao.updatePdpGroups() method + * @throws Exception if an error occurred + */ + protected List getGroupUpdates() throws Exception { + verify(dao).updatePdpGroups(updateCaptor.capture()); + + return copyList(updateCaptor.getValue()); + } + + /** + * Gets the state-changes that were added to the request map. + * + * @param count the number of times the method is expected to have been called + * @return the state-changes that were added to the request map + */ + protected List getStateChangeRequests(int count) { + ArgumentCaptor captor = ArgumentCaptor.forClass(PdpStateChange.class); + + verify(reqmap, times(count)).addRequest(any(), captor.capture()); + + return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList()); + } + + /** + * Gets the updates that were added to the request map. + * + * @param count the number of times the method is expected to have been called + * @return the updates that were added to the request map + */ + protected List getUpdateRequests(int count) { + ArgumentCaptor captor = ArgumentCaptor.forClass(PdpUpdate.class); + + verify(reqmap, times(count)).addRequest(captor.capture(), any()); + + return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList()); + } + + /** + * Copies a list and sorts it by group name. + * + * @param source source list to copy + * @return a copy of the source list + */ + private List copyList(List source) { + List newlst = new ArrayList<>(source); + Collections.sort(newlst, (left, right) -> left.getName().compareTo(right.getName())); + return newlst; + } + + /** + * Loads a list of groups. + * + * @param fileName name of the file from which to load + * @return a list of groups + */ + protected List loadGroups(String fileName) { + return loadPdpGroups(fileName).getGroups(); + } + + /** + * Loads a PdpGroups. + * + * @param fileName name of the file from which to load + * @return a PdpGroups + */ + protected PdpGroups loadPdpGroups(String fileName) { + return loadFile(fileName, PdpGroups.class); + } + + /** + * Loads a group. + * + * @param fileName name of the file from which to load + * @return a group + */ + protected PdpGroup loadGroup(String fileName) { + return loadFile(fileName, PdpGroup.class); + } + + /** + * Loads a list of policies. + * + * @param fileName name of the file from which to load + * @return a list of policies + */ + protected List loadPolicies(String fileName) { + return loadFile(fileName, PolicyList.class).policies; + } + + /** + * Loads a policy. + * + * @param fileName name of the file from which to load + * @return a policy + */ + protected ToscaPolicy loadPolicy(String fileName) { + return loadFile(fileName, ToscaPolicy.class); + } + + /** + * Loads a policy type. + * + * @param fileName name of the file from which to load + * @return a policy type + */ + protected ToscaPolicyType loadPolicyType(String fileName) { + return loadFile(fileName, ToscaPolicyType.class); + } + + /** + * Loads an object from a JSON file. + * + * @param fileName name of the file from which to load + * @param clazz the class of the object to be loaded + * @return the object that was loaded from the file + */ + protected T loadFile(String fileName, Class clazz) { + File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName)); + try { + return coder.decode(propFile, clazz); + + } catch (CoderException e) { + throw new RuntimeException(e); + } + } + + /** + * 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. + */ + private static class PolicyList { + private List policies; + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestGroupData.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestGroupData.java new file mode 100644 index 00000000..5eb5611f --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestGroupData.java @@ -0,0 +1,103 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.models.pdp.concepts.PdpGroup; + +public class TestGroupData { + private static final String NAME = "my-name"; + + private PdpGroup oldGroup; + private PdpGroup newGroup; + private GroupData data; + + /** + * Sets up. + */ + @Before + public void setUp() { + oldGroup = new PdpGroup(); + oldGroup.setName(NAME); + + newGroup = new PdpGroup(oldGroup); + + data = new GroupData(oldGroup); + } + + @Test + public void testNew() { + data = new GroupData(oldGroup, true); + assertSame(oldGroup, data.getGroup()); + + assertFalse(data.isUnchanged()); + assertTrue(data.isNew()); + assertFalse(data.isUpdated()); + + data.update(newGroup); + assertFalse(data.isUnchanged()); + assertTrue(data.isNew()); + assertFalse(data.isUpdated()); + assertSame(newGroup, data.getGroup()); + + // repeat with a new group + newGroup = new PdpGroup(oldGroup); + data.update(newGroup); + assertFalse(data.isUnchanged()); + assertTrue(data.isNew()); + assertFalse(data.isUpdated()); + assertSame(newGroup, data.getGroup()); + } + + @Test + public void testUpdateOnly() { + assertTrue(data.isUnchanged()); + assertFalse(data.isUpdated()); + assertSame(oldGroup, data.getGroup()); + + data.update(newGroup); + + assertFalse(data.isUnchanged()); + assertTrue(data.isUpdated()); + assertFalse(data.isNew()); + assertSame(newGroup, data.getGroup()); + + // repeat + newGroup = new PdpGroup(oldGroup); + data.update(newGroup); + assertFalse(data.isUnchanged()); + assertTrue(data.isUpdated()); + assertFalse(data.isNew()); + assertSame(newGroup, data.getGroup()); + + // incorrect name + newGroup = new PdpGroup(oldGroup); + newGroup.setName("other"); + assertThatIllegalArgumentException().isThrownBy(() -> data.update(newGroup)) + .withMessage("expected group my-name, but received other"); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java new file mode 100644 index 00000000..0f1ab1f8 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteControllerV1.java @@ -0,0 +1,104 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 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. + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.junit.Assert.assertEquals; + +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.Response; +import org.junit.Test; +import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse; + +/** + * Note: this tests failure cases; success cases are tested by tests in the "e2e" package. + */ +public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer { + + private static final String GROUP_NOT_FOUND = "group not found"; + private static final String DELETE_GROUP_ENDPOINT = "pdps/groups"; + private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies"; + + @Test + public void testSwagger() throws Exception { + super.testSwagger(DELETE_GROUP_ENDPOINT + "/{name}"); + + super.testSwagger(DELETE_POLICIES_ENDPOINT + "/{name}"); + super.testSwagger(DELETE_POLICIES_ENDPOINT + "/{name}/versions/{version}"); + } + + @Test + public void testDeleteGroup() throws Exception { + String uri = DELETE_GROUP_ENDPOINT + "/my-name"; + + Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.delete(); + PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); + assertEquals(GROUP_NOT_FOUND, resp.getErrorDetails()); + + rawresp = invocationBuilder.delete(); + resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); + assertEquals(GROUP_NOT_FOUND, resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.delete()); + } + + @Test + public void testDeletePolicy() throws Exception { + String uri = DELETE_POLICIES_ENDPOINT + "/my-name"; + + Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.delete(); + PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); + assertEquals("cannot find policy: my-name null", resp.getErrorDetails()); + + rawresp = invocationBuilder.delete(); + resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); + assertEquals("cannot find policy: my-name null", resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.delete()); + } + + @Test + public void testDeletePolicyVersion() throws Exception { + String uri = DELETE_POLICIES_ENDPOINT + "/my-name/versions/3"; + + Invocation.Builder invocationBuilder = sendRequest(uri); + Response rawresp = invocationBuilder.delete(); + PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); + assertEquals("cannot find policy: my-name 3", resp.getErrorDetails()); + + rawresp = invocationBuilder.delete(); + resp = rawresp.readEntity(PdpGroupDeleteResponse.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); + assertEquals("cannot find policy: my-name 3", resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(uri, req -> req.delete()); + } +} 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 new file mode 100644 index 00000000..ae356e83 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeleteProvider.java @@ -0,0 +1,297 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import javax.ws.rs.core.Response.Status; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; +import org.onap.policy.pap.main.rest.ProviderBase.Updater; + +public class TestPdpGroupDeleteProvider extends ProviderSuper { + private static final String EXPECTED_EXCEPTION = "expected exception"; + private static final String GROUP1_NAME = "groupA"; + + @Mock + private SessionData session; + + @Captor + private ArgumentCaptor> pdpCaptor; + + private MyProvider prov; + private ToscaPolicyIdentifierOptVersion optIdent; + private ToscaPolicyIdentifierOptVersion fullIdent; + private ToscaPolicyIdentifier ident; + private Updater updater; + + + @AfterClass + public static void tearDownAfterClass() { + Registry.newRegistry(); + } + + /** + * Configures mocks and objects. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + + super.setUp(); + + ident = policy1.getIdentifier(); + optIdent = new ToscaPolicyIdentifierOptVersion(ident.getName(), null); + fullIdent = new ToscaPolicyIdentifierOptVersion(ident.getName(), ident.getVersion()); + + prov = new MyProvider(); + + updater = prov.makeUpdater(session, policy1, fullIdent); + } + + @Test + public void testDeleteGroup_Inctive() throws Exception { + PdpGroup group = loadGroup("deleteGroup.json"); + + when(session.getGroup(GROUP1_NAME)).thenReturn(group); + + prov.deleteGroup(GROUP1_NAME); + + verify(session).deleteGroupFromDb(group); + + // should be no PDP requests + verify(session, never()).addRequests(any(), any()); + } + + @Test + public void testDeleteGroup_Active() throws Exception { + PdpGroup group = loadGroup("deleteGroup.json"); + + group.setPdpGroupState(PdpState.ACTIVE); + + when(session.getGroup(GROUP1_NAME)).thenReturn(group); + + assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isInstanceOf(PfModelException.class) + .hasMessage("group is still ACTIVE"); + } + + @Test + public void testDeleteGroup_NotFound() throws Exception { + assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isInstanceOf(PfModelException.class) + .hasMessage("group not found") + .extracting(ex -> ((PfModelException) ex).getErrorResponse().getResponseCode()) + .isEqualTo(Status.NOT_FOUND); + } + + @Test + public void testDeleteGroup_Inactive() throws Exception { + PdpGroup group = loadGroup("deleteGroup.json"); + + when(session.getGroup(GROUP1_NAME)).thenReturn(group); + + prov.deleteGroup(GROUP1_NAME); + + verify(session).deleteGroupFromDb(group); + + // should done no requests for the PDPs + verify(session, never()).addRequests(any(), any()); + } + + @Test + public void testDeleteGroup_DaoEx() throws Exception { + PdpGroup group = loadGroup("deleteGroup.json"); + + when(session.getGroup(GROUP1_NAME)).thenReturn(group); + + PfModelException ex = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); + doThrow(ex).when(session).deleteGroupFromDb(group); + + assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isSameAs(ex); + } + + @Test + public void testUndeploy_testUndeployPolicy() throws Exception { + prov.undeploy(optIdent); + } + + /** + * Tests using a real provider, just to verify end-to-end functionality. + * + * @throws Exception if an error occurs + */ + @Test + public void testUndeploy_Full() throws Exception { + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); + + PdpGroup group = loadGroup("undeploy.json"); + + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group)); + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); + + new PdpGroupDeleteProvider().undeploy(fullIdent); + + // should have updated the old group + List updates = getGroupUpdates(); + assertEquals(1, updates.size()); + assertSame(group, updates.get(0)); + assertEquals(PdpState.ACTIVE, group.getPdpGroupState()); + + // should be one less item in the new subgroup + assertEquals(2, group.getPdpSubgroups().get(0).getPolicies().size()); + + // should have updated the PDPs + List requests = getUpdateRequests(1); + assertEquals(1, requests.size()); + PdpUpdate req = requests.get(0); + assertEquals("pdpA", req.getName()); + assertEquals(GROUP1_NAME, req.getPdpGroup()); + assertEquals("pdpTypeA", req.getPdpSubgroup()); + assertEquals(Arrays.asList(policy1, policy1), req.getPolicies()); + } + + @Test + public void testUndeployPolicy_NotFound() throws Exception { + when(session.isUnchanged()).thenReturn(true); + + assertThatThrownBy(() -> prov.undeploy(optIdent)).isInstanceOf(PfModelException.class) + .hasMessage("policy does not appear in any PDP group: policyA null"); + } + + @Test + public void testUndeployPolicy_DaoEx() throws Exception { + PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); + + prov = spy(prov); + doThrow(exc).when(prov).processPolicy(any(), any()); + + assertThatThrownBy(() -> prov.undeploy(optIdent)).isSameAs(exc); + } + + @Test + public void testUndeployPolicy_RtEx() throws Exception { + RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION); + + prov = spy(prov); + doThrow(exc).when(prov).processPolicy(any(), any()); + + assertThatThrownBy(() -> prov.undeploy(optIdent)).isSameAs(exc); + } + + @Test + public void testMakeUpdater_WithVersion() throws PfModelException { + /* + * this group has two matching policies and one policy with a different name. + */ + PdpGroup group = loadGroup("undeploy.json"); + + PdpSubGroup subgroup = group.getPdpSubgroups().get(0); + int origSize = subgroup.getPolicies().size(); + + // invoke updater - matching both name and version + assertTrue(updater.apply(group, subgroup)); + + // identified policy should have been removed + assertEquals(origSize - 1, subgroup.getPolicies().size()); + assertFalse(subgroup.getPolicies().contains(ident)); + + verify(session).trackUndeploy(eq(ident), pdpCaptor.capture()); + assertEquals("[pdpA]", pdpCaptor.getValue().toString()); + } + + @Test + public void testMakeUpdater_NullVersion() throws PfModelException { + /* + * this group has two matching policies and one policy with a different name. + */ + PdpGroup group = loadGroup("undeploy.json"); + + PdpSubGroup subgroup = group.getPdpSubgroups().get(0); + int origSize = subgroup.getPolicies().size(); + + // invoke updater - matching the name, but with a null (i.e., wild-card) version + updater = prov.makeUpdater(session, policy1, optIdent); + assertTrue(updater.apply(group, subgroup)); + + // identified policy should have been removed + assertEquals(origSize - 2, subgroup.getPolicies().size()); + assertFalse(subgroup.getPolicies().contains(ident)); + } + + @Test + public void testMakeUpdater_NotFound() throws PfModelException { + /* + * this group has one policy with a different name and one with a different + * version, but not the policy of interest. + */ + PdpGroup group = loadGroup("undeployMakeUpdaterGroupNotFound.json"); + + PdpSubGroup subgroup = group.getPdpSubgroups().get(0); + int origSize = subgroup.getPolicies().size(); + + // invoke updater + assertFalse(updater.apply(group, subgroup)); + + // should be unchanged + assertEquals(origSize, subgroup.getPolicies().size()); + } + + + private class MyProvider extends PdpGroupDeleteProvider { + + @Override + protected void process(T request, BiConsumerWithEx processor) throws PfModelException { + processor.accept(session, request); + } + + @Override + protected void processPolicy(SessionData data, ToscaPolicyIdentifierOptVersion desiredPolicy) + throws PfModelException { + // do nothing + } + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java new file mode 100644 index 00000000..a26213d7 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java @@ -0,0 +1,112 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019 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. + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Arrays; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.junit.Test; +import org.onap.policy.models.pap.concepts.PdpDeployPolicies; +import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; + +/** + * Note: this tests failure cases; success cases are tested by tests in the "e2e" package. + */ +public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { + + private static final String DEPLOY_GROUP_ENDPOINT = "pdps"; + private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies"; + + @Test + public void testSwagger() throws Exception { + super.testSwagger(DEPLOY_GROUP_ENDPOINT); + super.testSwagger(DEPLOY_POLICIES_ENDPOINT); + } + + @Test + public void testDeployGroup() throws Exception { + Entity entgrp = makePdpGroupEntity(); + + Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT); + Response rawresp = invocationBuilder.post(entgrp); + PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); + assertNotNull(resp.getErrorDetails()); + + rawresp = invocationBuilder.post(entgrp); + resp = rawresp.readEntity(PdpGroupDeployResponse.class); + assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); + assertNotNull(resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(DEPLOY_GROUP_ENDPOINT, req -> req.post(entgrp)); + } + + @Test + public void testDeployPolicies() throws Exception { + Entity entgrp = makePdpPoliciesEntity(); + + Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT); + Response rawresp = invocationBuilder.post(entgrp); + PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); + assertNotNull(resp.getErrorDetails()); + + rawresp = invocationBuilder.post(entgrp); + resp = rawresp.readEntity(PdpGroupDeployResponse.class); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); + assertNotNull(resp.getErrorDetails()); + + // verify it fails when no authorization info is included + checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp)); + } + + private Entity makePdpGroupEntity() { + PdpSubGroup subgrp = new PdpSubGroup(); + subgrp.setPdpType("drools"); + + PdpGroup group = new PdpGroup(); + group.setName("drools-group"); + group.setDescription("my description"); + group.setPdpSubgroups(Arrays.asList(subgrp)); + + return Entity.entity(group, MediaType.APPLICATION_JSON); + } + + private Entity makePdpPoliciesEntity() { + ToscaPolicyIdentifierOptVersion pol1 = new ToscaPolicyIdentifierOptVersion("policy-a", "1"); + ToscaPolicyIdentifierOptVersion pol2 = new ToscaPolicyIdentifierOptVersion("policy-b", null); + + PdpDeployPolicies policies = new PdpDeployPolicies(); + policies.setPolicies(Arrays.asList(pol1, pol2)); + + return Entity.entity(policies, MediaType.APPLICATION_JSON); + } +} 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 new file mode 100644 index 00000000..39a11396 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployProvider.java @@ -0,0 +1,932 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Collectors; +import javax.ws.rs.core.Response.Status; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.pap.concepts.PdpDeployPolicies; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; + +public class TestPdpGroupDeployProvider extends ProviderSuper { + private static final String EXPECTED_EXCEPTION = "expected exception"; + + private static final String POLICY2_NAME = "policyB"; + private static final String POLICY1_VERSION = "1.2.3"; + private static final String GROUP1_NAME = "groupA"; + private static final String PDP1_TYPE = "pdpTypeA"; + private static final String PDP2_TYPE = "pdpTypeB"; + private static final String PDP4_TYPE = "pdpTypeD"; + private static final String PDP2 = "pdpB"; + private static final String PDP4 = "pdpD"; + + private PdpGroupDeployProvider prov; + + + @AfterClass + public static void tearDownAfterClass() { + Registry.newRegistry(); + } + + /** + * Configures mocks and objects. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + + super.setUp(); + + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); + when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json"))); + + prov = new PdpGroupDeployProvider(); + } + + @Test + public void testCreateOrUpdateGroups() throws Exception { + prov.createOrUpdateGroups(loadPdpGroups("emptyGroups.json")); + + // no groups, so no action should have been taken + assertNoGroupAction(); + } + + @Test + public void testCreateOrUpdateGroups_InvalidRequest() throws Exception { + assertThatThrownBy(() -> prov.createOrUpdateGroups(new PdpGroups())).isInstanceOf(PfModelException.class) + .hasMessageContaining("is null"); + + assertNoGroupAction(); + } + + @Test + public void testCreateOrUpdate_Invalid() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("pdpGroupState"); + + assertNoGroupAction(); + } + + @Test + public void testAddGroup() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup group = groups.getGroups().get(0); + group.setPdpGroupState(PdpState.PASSIVE); + + prov.createOrUpdateGroups(groups); + + // should not have updated the state + assertEquals(PdpState.PASSIVE, group.getPdpGroupState()); + + assertSame(group, getGroupCreates().get(0)); + } + + @Test + public void testAddGroup_Invalid() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("pdpGroupState"); + + assertNoGroupAction(); + } + + @Test + public void testAddGroup_InvalidSubGroup() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + + // policy won't match supported type + groups.getGroups().get(0).getPdpSubgroups().get(0).getSupportedPolicyTypes().get(0).setVersion("99.99.99"); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("supported policy"); + + assertNoGroupAction(); + } + + @Test + public void testValidateGroupOnly_NullState() throws PfModelException { + PdpGroups groups = loadPdpGroups("createGroups.json"); + groups.getGroups().get(0).setPdpGroupState(null); + prov.createOrUpdateGroups(groups); + } + + @Test + public void testValidateGroupOnly_Active() throws PfModelException { + PdpGroups groups = loadPdpGroups("createGroups.json"); + groups.getGroups().get(0).setPdpGroupState(PdpState.ACTIVE); + prov.createOrUpdateGroups(groups); + } + + @Test + public void testValidateGroupOnly_Passive() throws PfModelException { + PdpGroups groups = loadPdpGroups("createGroups.json"); + groups.getGroups().get(0).setPdpGroupState(PdpState.PASSIVE); + prov.createOrUpdateGroups(groups); + } + + @Test + public void testValidateGroupOnly_Invalid() { + PdpGroups groups = loadPdpGroups("createGroups.json"); + groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("pdpGroupState"); + } + + @Test + public void testUpdateGroup() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + + // DB group = new group + PdpGroup group = new PdpGroup(groups.getGroups().get(0)); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + assertNoGroupAction(); + } + + @Test + public void testUpdateGroup_PropertiesChanged() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + + PdpGroup group = new PdpGroup(groups.getGroups().get(0)); + group.setProperties(new TreeMap<>()); + + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("properties"); + + assertNoGroupAction(); + } + + @Test + public void testUpdateGroup_NewDescription() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + group.setDescription("old description"); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + assertGroupUpdateOnly(group); + + assertEquals(group.getDescription(), "my description"); + assertEquals(newgrp.toString(), group.toString()); + } + + @Test + public void testUpdateGroup_NewState() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + group.setPdpGroupState(PdpState.TEST); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + assertGroupUpdateOnly(group); + + assertEquals(PdpState.ACTIVE, group.getPdpGroupState()); + assertEquals(newgrp.toString(), group.toString()); + } + + @Test + public void testUpdateGroup_NewSubGroup() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + PdpGroup newgrp = groups.getGroups().get(0); + assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdateOnly(group); + } + + @Test + public void testUpdateGroup_UpdatedSubGroup() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + // something different in this subgroup + group.getPdpSubgroups().get(0).setDesiredInstanceCount(10); + + prov.createOrUpdateGroups(groups); + + assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdateOnly(group); + } + + @Test + public void testUpdateGroup_notifyPdpsDelSubGroups() throws Exception { + PdpGroup dbgroup = new PdpGroup(loadPdpGroups("createGroupsDelSub.json").getGroups().get(0)); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); + + PdpGroups groups = loadPdpGroups("createGroups.json"); + + prov.createOrUpdateGroups(groups); + + // verify that DB group was updated + List updates = getGroupUpdates(); + assertEquals(1, updates.size()); + dbgroup = updates.get(0); + + PdpGroup newgrp = groups.getGroups().get(0); + + Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); + Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies()); + + assertEquals(newgrp.toString(), dbgroup.toString()); + + // no deployment notifications + verify(notifier, never()).addDeploymentData(any()); + + // should have notified of deleted subgroup's policies/PDPs + ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier).addUndeploymentData(captor.capture()); + assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB, pdpD]"); + + // this requires a PDP UPDATE message + List pdpUpdates = getUpdateRequests(2); + assertEquals(2, pdpUpdates.size()); + + PdpUpdate pdpUpdate = pdpUpdates.get(0); + assertEquals(PDP2, pdpUpdate.getName()); + assertNull(pdpUpdate.getPdpGroup()); + + pdpUpdate = pdpUpdates.get(1); + assertEquals(PDP4, pdpUpdate.getName()); + assertNull(pdpUpdate.getPdpGroup()); + + // it also requires a PDP STATE-CHANGE message + List changes = getStateChangeRequests(2); + assertEquals(2, changes.size()); + + PdpStateChange change = changes.get(0); + assertEquals(PDP2, change.getName()); + assertEquals(PdpState.PASSIVE, change.getState()); + + change = changes.get(1); + assertEquals(PDP4, change.getName()); + assertEquals(PdpState.PASSIVE, change.getState()); + } + + @Test + public void testUpdateGroup_MultipleChanges() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); + subgrp.setDesiredInstanceCount(30); + subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION)); + subgrp.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("typeX.*", "9.8.7")); + + when(dao.getFilteredPolicyList(any())) + .thenReturn(loadPolicies("createGroupNewPolicy.json")) + .thenReturn(loadPolicies("daoPolicyList.json")) + .thenReturn(loadPolicies("createGroupNewPolicy.json")); + + prov.createOrUpdateGroups(groups); + + Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); + Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); + + assertEquals(newgrp.toString(), group.toString()); + + // this requires a PDP UPDATE message + assertGroupUpdate(group, subgrp); + } + + @Test + public void testUpdateField_Unchanged() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + assertNoGroupAction(); + } + + @Test + public void testUpdateField_WasNull() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + group.setDescription(null); + + prov.createOrUpdateGroups(groups); + + assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdateOnly(group); + } + + @Test + public void testUpdateField_NowNull() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + newgrp.setDescription(null); + + prov.createOrUpdateGroups(groups); + + assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdateOnly(group); + } + + @Test + public void testUpdateField_Changed() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + newgrp.setDescription(group.getDescription() + "-changed"); + + prov.createOrUpdateGroups(groups); + + assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdateOnly(group); + } + + @Test + public void testAddSubGroup() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + PdpGroup newgrp = groups.getGroups().get(0); + + PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1); + newsub.setCurrentInstanceCount(0); + newsub.setPdpInstances(new ArrayList<>(0)); + + assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdateOnly(group); + } + + /** + * Tests addSubgroup() when the new subgroup has a wild-card policy type. + * + * @throws Exception if an error occurs + */ + @Test + public void testAddSubGroupWildCardPolicyType() throws Exception { + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json")); + when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList()); + + PdpGroups groups = loadPdpGroups("createGroupsWildCard.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + PdpGroup newgrp = groups.getGroups().get(0); + + PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1); + newsub.setCurrentInstanceCount(0); + newsub.setPdpInstances(new ArrayList<>(0)); + + assertEquals(newgrp.toString(), group.toString()); + } + + /** + * Tests addSubgroup() when the new subgroup has a wild-card policy type, but the + * policy doesn't have a matching type. + * + * @throws PfModelException if an error occurs + */ + @Test + public void testAddSubGroupWildCardPolicyTypeUnmatched() throws PfModelException { + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCardUnmatched.json")); + when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList()); + + PdpGroups groups = loadPdpGroups("createGroupsWildCard.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class); + } + + @Test + public void testAddSubGroup_ValidationPolicyTypeNotFound() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy type"); + } + + @Test + public void testAddSubGroup_ValidationPolicyTypeDaoEx() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); + when(dao.getPolicyTypeList(any(), any())).thenThrow(exc); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc); + } + + @Test + public void testAddSubGroup_ValidationPolicyNotFound() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSubNotFound.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy"); + } + + @Test + public void testAddSubGroup_ValidationPolicyDaoEx() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsNewSubNotFound.json"); + PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); + when(dao.getFilteredPolicyList(any())).thenThrow(exc); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc); + } + + @Test + public void testAddSubGroup_ValidateVersionPrefixMatch() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup dbgroup = new PdpGroup(newgrp); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); + + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) + .thenReturn(loadPolicies("daoPolicyList.json")) + .thenReturn(loadPolicies("createGroupNewPolicy.json")); + + PdpGroups reqgroups = loadPdpGroups("createGroupsVersPrefix.json"); + + prov.createOrUpdateGroups(reqgroups); + + Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); + Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies()); + + assertEquals(newgrp.toString(), dbgroup.toString()); + } + + @Test + public void testAddSubGroup_ValidateVersionPrefixMismatch() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup dbgroup = new PdpGroup(newgrp); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); + + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); + + + PdpGroups reqgroups = loadPdpGroups("createGroupsVersPrefixMismatch.json"); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(reqgroups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("different version already deployed"); + + assertNoGroupAction(); + } + + @Test + public void testUpdateSubGroup_Invalid() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + // change properties + newgrp.getPdpSubgroups().get(0).setProperties(new TreeMap<>()); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("properties"); + + assertNoGroupAction(); + } + + @Test + public void testUpdateSubGroup_SupportedPolicies() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes() + .add(new ToscaPolicyTypeIdentifier("typeX.*", "9.8.7")); + + prov.createOrUpdateGroups(groups); + + assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdateOnly(group); + } + + @Test + public void testUpdateSubGroup_DesiredCount() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + newgrp.getPdpSubgroups().get(0).setDesiredInstanceCount(20); + + prov.createOrUpdateGroups(groups); + + assertEquals(newgrp.toString(), group.toString()); + assertGroupUpdateOnly(group); + } + + @Test + public void testUpdateSubGroup_Policies() throws Exception { + PdpGroups groups = loadPdpGroups("createGroupsDelPolicy.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); + + // delete second policy + subgrp.setPolicies(subgrp.getPolicies().subList(0, 1)); + + // add new policy + ToscaPolicyIdentifier policyId2 = new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION); + subgrp.getPolicies().add(policyId2); + + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) + .thenReturn(loadPolicies("daoPolicyList.json")) + .thenReturn(loadPolicies("daoPolicyListDelPolicy.json")) + .thenReturn(loadPolicies("createGroupNewPolicy.json")); + + prov.createOrUpdateGroups(groups); + + Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); + Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); + + assertEquals(newgrp.toString(), group.toString()); + + // should have notified of added policy/PDPs + ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier).addDeploymentData(captor.capture()); + assertDeploymentData(captor, policyId2, "[pdpA]"); + + // should have notified of deleted policy/PDPs + captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier).addUndeploymentData(captor.capture()); + assertDeploymentData(captor, new ToscaPolicyIdentifier("ToBeDeleted", POLICY1_VERSION), "[pdpA]"); + + // this requires a PDP UPDATE message + assertGroupUpdate(group, subgrp); + } + + @Test + public void testUpdateSubGroup_Unchanged() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + prov.createOrUpdateGroups(groups); + + Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); + Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); + + assertEquals(newgrp.toString(), group.toString()); + + // no notifications + verify(notifier, never()).addDeploymentData(any()); + verify(notifier, never()).addUndeploymentData(any()); + + // no group updates + assertNoGroupAction(); + } + + @Test + public void testUpdateSubGroup_PolicyVersionMismatch() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup dbgroup = new PdpGroup(newgrp); + when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); + + // arrange for DB policy version to be different + PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0); + dbsubgrp.getPolicies().get(0).setVersion("9.9.9"); + + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("different version already deployed"); + + assertNoGroupAction(); + } + + @Test + public void testValidateSubGroup_PropertiesMismatch() throws Exception { + PdpGroups groups = loadPdpGroups("createGroups.json"); + PdpGroup newgrp = groups.getGroups().get(0); + PdpGroup group = new PdpGroup(newgrp); + when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); + + newgrp.setProperties(new TreeMap<>()); + + assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) + .hasMessageContaining("properties"); + + assertNoGroupAction(); + } + + @Test + public void testDeployPolicies() throws PfModelException { + prov.deployPolicies(loadEmptyRequest()); + } + + /** + * Tests deployPolicies() when the supported policy type uses a wild-card. + * + * @throws Exception if an error occurs + */ + @Test + public void testDeployPoliciesWildCard() throws Exception { + when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("deployPoliciesWildCard.json")); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json")); + when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); + + policy1.setName("policy.some"); + policy1.setVersion(POLICY1_VERSION); + policy1.setType("some.type"); + policy1.setTypeVersion("100.2.3"); + + PdpDeployPolicies depreq = loadRequest(); + depreq.getPolicies().get(0).setName("policy.some"); + + prov.deployPolicies(depreq); + + assertGroup(getGroupUpdates(), GROUP1_NAME); + + 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, policy1.getIdentifier(), "[pdpB]"); + + // no undeployment notifications + verify(notifier, never()).addUndeploymentData(any()); + } + + @Test + public void testDeploySimplePolicies() throws Exception { + prov.deployPolicies(loadEmptyRequest()); + } + + @Test + public void testDeploySimplePolicies_DaoEx() throws Exception { + PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); + when(dao.getFilteredPdpGroups(any())).thenThrow(exc); + + assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc); + } + + @Test + public void testDeploySimplePolicies_DaoPfRtEx() throws Exception { + PfModelRuntimeException exc = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); + when(dao.getFilteredPdpGroups(any())).thenThrow(exc); + + assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc); + } + + @Test + public void testDeploySimplePolicies_RuntimeEx() throws Exception { + RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION); + when(dao.getFilteredPolicyList(any())).thenThrow(exc); + + assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class).hasCause(exc); + } + + @Test + public void testDeploySimplePolicies_NoGroups() throws Exception { + when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("emptyGroups.json")); + + assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class) + .hasMessage("policy not supported by any PDP group: policyA 1.2.3"); + } + + @Test + public void testMakeUpdater() throws Exception { + /* + * Each subgroup has a different PDP type and name. + * + * Type is not supported by the first subgroup. + * + * Second subgroup matches. + * + * Third subgroup already contains the policy. + * + * Last subgroup matches. + */ + + when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao.json")); + + prov.deployPolicies(loadRequest()); + + assertGroup(getGroupUpdates(), GROUP1_NAME); + + List requests = getUpdateRequests(2); + assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); + assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4); + + // should have notified of added policy/PDPs + ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); + verify(notifier).addDeploymentData(captor.capture()); + assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB, pdpD]"); + + // no undeployment notifications + verify(notifier, never()).addUndeploymentData(any()); + } + + @Test + public void testMakeUpdater_PolicyVersionMismatch() throws Exception { + + // subgroup has a different version of the Policy + when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao_DiffVers.json")); + + assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelRuntimeException.class) + .hasMessageContaining("pdpTypeC").hasMessageContaining("different version already deployed"); + + verify(dao, never()).createPdpGroups(any()); + verify(dao, never()).updatePdpGroups(any()); + verify(reqmap, never()).addRequest(any(PdpUpdate.class)); + } + + @Test + public void testMakeUpdater_NoPdps() throws Exception { + + // subgroup has no PDPs + when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroup_NoPdpsDao.json")); + + assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelRuntimeException.class) + .hasMessage("group " + GROUP1_NAME + " subgroup " + PDP1_TYPE + " has no active PDPs"); + + verify(dao, never()).createPdpGroups(any()); + verify(dao, never()).updatePdpGroups(any()); + verify(reqmap, never()).addRequest(any(PdpUpdate.class)); + } + + + protected void assertUpdate(List updates, String groupName, String pdpType, String pdpName) { + + PdpUpdate update = updates.remove(0); + + assertEquals(groupName, update.getPdpGroup()); + assertEquals(pdpType, update.getPdpSubgroup()); + assertEquals(pdpName, update.getName()); + assertTrue(update.getPolicies().contains(policy1)); + } + + private void assertNoGroupAction() throws Exception { + verify(dao, never()).createPdpGroups(any()); + verify(dao, never()).updatePdpGroups(any()); + verify(reqmap, never()).addRequest(any(), any()); + } + + private void assertGroupUpdate(PdpGroup group, PdpSubGroup subgrp) throws Exception { + verify(dao, never()).createPdpGroups(any()); + + assertEquals(0, getStateChangeRequests(1).size()); + + List pdpUpdates = getUpdateRequests(1); + assertEquals(1, pdpUpdates.size()); + + PdpUpdate pdpUpdate = pdpUpdates.get(0); + assertEquals("pdpA", pdpUpdate.getName()); + assertEquals(group.getName(), pdpUpdate.getPdpGroup()); + + assertEquals(subgrp.getPdpType(), pdpUpdate.getPdpSubgroup()); + + List pdpPolicies = + pdpUpdate.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()); + Collections.sort(pdpPolicies); + + assertEquals(subgrp.getPolicies().toString(), pdpPolicies.toString()); + + List updates = getGroupUpdates(); + assertEquals(Arrays.asList(group), updates); + } + + private void assertGroupUpdateOnly(PdpGroup group) throws Exception { + verify(dao, never()).createPdpGroups(any()); + verify(reqmap, never()).addRequest(any(), any()); + + List updates = getGroupUpdates(); + assertEquals(Arrays.asList(group), updates); + } + + private void assertDeploymentData(ArgumentCaptor captor, ToscaPolicyIdentifier policyId, + String expectedPdps) { + PolicyPdpNotificationData data = captor.getValue(); + assertEquals(policyId, data.getPolicyId()); + assertEquals(policy1.getTypeIdentifier(), data.getPolicyType()); + assertEquals(expectedPdps, new TreeSet<>(data.getPdps()).toString()); + } + + /** + * Loads a standard request. + * + * @return a standard request + */ + protected PdpDeployPolicies loadRequest() { + return loadRequest("request.json"); + } + + /** + * Loads a request from a JSON file. + * + * @param fileName name of the file from which to load + * @return the request that was loaded + */ + protected PdpDeployPolicies loadRequest(String fileName) { + return loadFile(fileName, PdpDeployPolicies.class); + } + + /** + * Loads an empty request. + * + * @return an empty request + */ + protected PdpDeployPolicies loadEmptyRequest() { + return loadRequest("emptyRequest.json"); + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyUndeployerImpl.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyUndeployerImpl.java new file mode 100644 index 00000000..1a54fe5f --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPolicyUndeployerImpl.java @@ -0,0 +1,200 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; + +public class TestPolicyUndeployerImpl extends ProviderSuper { + private static final String MY_GROUP = "my-group"; + private static final String MY_SUBGROUP = "my-subgroup"; + private static final String MY_SUBGROUP0 = "my-subgroup-0"; + private static final String PDP1 = "my-pdp-a"; + + @Mock + private SessionData session; + + @Captor + private ArgumentCaptor> pdpCaptor; + + private ToscaPolicyIdentifier ident1; + private ToscaPolicyIdentifier ident2; + private ToscaPolicyIdentifier ident3; + private ToscaPolicyIdentifier ident4; + private PdpGroup group; + private PdpSubGroup subgroup; + private MyProvider prov; + + + @AfterClass + public static void tearDownAfterClass() { + Registry.newRegistry(); + } + + /** + * Configures mocks and objects. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + + super.setUp(); + + ident1 = new ToscaPolicyIdentifier("ident-a", "2.3.1"); + ident2 = new ToscaPolicyIdentifier("ident-b", "2.3.2"); + ident3 = new ToscaPolicyIdentifier("ident-c", "2.3.3"); + ident4 = new ToscaPolicyIdentifier("ident-d", "2.3.4"); + + group = new PdpGroup(); + group.setName(MY_GROUP); + + subgroup = new PdpSubGroup(); + subgroup.setPdpType(MY_SUBGROUP); + + Pdp pdp1 = new Pdp(); + pdp1.setInstanceId(PDP1); + + subgroup.setPdpInstances(Arrays.asList(pdp1)); + + // this subgroup should never be touched + PdpSubGroup subgroup0 = new PdpSubGroup(); + subgroup0.setPdpType(MY_SUBGROUP0); + subgroup0.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4))); + subgroup.setPdpInstances(Arrays.asList(pdp1)); + + group.setPdpSubgroups(Arrays.asList(subgroup0, subgroup)); + + when(session.getGroup(MY_GROUP)).thenReturn(group); + when(session.getPolicy(any())).thenReturn(policy1); + + prov = new MyProvider(); + } + + @Test + public void testUndeployPolicies() throws PfModelException { + subgroup.setPolicies(new LinkedList<>(Arrays.asList(ident1, ident2, ident3, ident4))); + + prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2)); + + // group should have been updated + verify(session).update(group); + + // subgroup should only have remaining policies + assertEquals(Arrays.asList(ident3, ident4).toString(), subgroup.getPolicies().toString()); + + // should have generated PDP-UPDATE for the PDP + verify(session).addUpdate(any()); + } + + /** + * Tests undeployPolicies() when the policies do not exist in the subgroup. + */ + @Test + public void testUndeployPoliciesUnchanged() throws PfModelException { + List origlist = Arrays.asList(ident3, ident4); + subgroup.setPolicies(new LinkedList<>(origlist)); + + prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2)); + + // group NOT should have been updated + verify(session, never()).update(group); + + // subgroup's policies should be unchanged + assertEquals(origlist.toString(), subgroup.getPolicies().toString()); + + // should NOT have generated PDP-UPDATE for the PDP + verify(session, never()).addUpdate(any()); + } + + /** + * Tests undeployPolicies() when the group is not found. + */ + @Test + public void testUndeployPoliciesGroupNotFound() throws PfModelException { + // force exception to be thrown if the list is changed + subgroup.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4))); + + when(session.getGroup(any())).thenReturn(null); + + prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2)); + + // group should have been updated + verify(session, never()).update(group); + + // should have generated PDP-UPDATE for the PDP + verify(session, never()).addUpdate(any()); + } + + /** + * Tests undeployPolicies() when the subgroup is not found. + */ + @Test + public void testUndeployPoliciesSubGroupNotFound() throws PfModelException { + // force exception to be thrown if the list is changed + subgroup.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4))); + + subgroup.setPdpType(MY_SUBGROUP + "X"); + + prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2)); + + // group should have been updated + verify(session, never()).update(group); + + // should have generated PDP-UPDATE for the PDP + verify(session, never()).addUpdate(any()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testMakeUpdater() { + prov.makeUpdater(null, null, null); + } + + + private class MyProvider extends PolicyUndeployerImpl { + + @Override + protected void process(T request, BiConsumerWithEx processor) throws PfModelException { + processor.accept(session, request); + } + } +} 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 new file mode 100644 index 00000000..b6f56ba0 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestProviderBase.java @@ -0,0 +1,392 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +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; +import org.onap.policy.models.pap.concepts.PdpDeployPolicies; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; +import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; +import org.powermock.reflect.Whitebox; + +public class TestProviderBase extends ProviderSuper { + private static final String EXPECTED_EXCEPTION = "expected exception"; + + private static final String POLICY1_NAME = "policyA"; + private static final String POLICY1_VERSION = "1.2.3"; + private static final String GROUP1_NAME = "groupA"; + private static final String GROUP2_NAME = "groupB"; + private static final String PDP1_TYPE = "pdpTypeA"; + private static final String PDP2_TYPE = "pdpTypeB"; + private static final String PDP3_TYPE = "pdpTypeC"; + private static final String PDP4_TYPE = "pdpTypeD"; + private static final String PDP1 = "pdpA"; + private static final String PDP2 = "pdpB"; + private static final String PDP3 = "pdpC"; + private static final String PDP4 = "pdpD"; + + private MyProvider prov; + + + @AfterClass + public static void tearDownAfterClass() { + Registry.newRegistry(); + } + + /** + * Configures mocks and objects. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + + super.setUp(); + + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); + + prov = new MyProvider(); + } + + @Test + public void testProviderBase() { + assertSame(lockit, Whitebox.getInternalState(prov, "updateLock")); + assertSame(reqmap, Whitebox.getInternalState(prov, "requestMap")); + assertSame(daofact, Whitebox.getInternalState(prov, "daoFactory")); + } + + @Test + public void testProcess() throws Exception { + prov.process(loadRequest(), this::handle); + + assertGroup(getGroupUpdates(), GROUP1_NAME); + + 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()); + } + + @Test + public void testProcess_CreateEx() throws Exception { + PfModelException ex = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); + when(daofact.create()).thenThrow(ex); + + assertThatThrownBy(() -> prov.process(loadEmptyRequest(), this::handle)).isSameAs(ex); + } + + @Test + public void testProcess_PfRtEx() throws Exception { + PfModelRuntimeException ex = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); + when(daofact.create()).thenThrow(ex); + + assertThatThrownBy(() -> prov.process(loadEmptyRequest(), this::handle)).isSameAs(ex); + } + + @Test + public void testProcess_RuntimeEx() throws Exception { + RuntimeException ex = new RuntimeException(EXPECTED_EXCEPTION); + when(daofact.create()).thenThrow(ex); + + assertThatThrownBy(() -> prov.process(loadEmptyRequest(), this::handle)).isInstanceOf(PfModelException.class) + .hasMessage("request failed").hasCause(ex); + } + + @Test + public void testProcessPolicy_NoGroups() throws Exception { + when(dao.getFilteredPdpGroups(any())).thenReturn(Collections.emptyList()); + + SessionData session = new SessionData(dao); + ToscaPolicyIdentifierOptVersion ident = new ToscaPolicyIdentifierOptVersion(POLICY1_NAME, POLICY1_VERSION); + assertThatThrownBy(() -> prov.processPolicy(session, ident)).isInstanceOf(PfModelException.class) + .hasMessage("policy not supported by any PDP group: policyA 1.2.3"); + + } + + @Test + public void testGetPolicy() throws Exception { + PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); + when(dao.getFilteredPolicyList(any())).thenThrow(exc); + + assertThatThrownBy(() -> prov.process(loadRequest(), this::handle)).isInstanceOf(PfModelRuntimeException.class) + .hasCause(exc); + } + + @Test + public void testGetPolicy_NotFound() throws Exception { + when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); + + assertThatThrownBy(() -> prov.process(loadRequest(), this::handle)).isInstanceOf(PfModelRuntimeException.class) + .hasMessage("cannot find policy: policyA 1.2.3") + .extracting(ex -> ((PfModelRuntimeException) ex).getErrorResponse().getResponseCode()) + .isEqualTo(Status.NOT_FOUND); + } + + @Test + public void testGetGroup() throws Exception { + when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("getGroupDao.json")) + .thenReturn(loadGroups("groups.json")); + + prov.process(loadRequest(), this::handle); + + assertGroup(getGroupUpdates(), GROUP1_NAME); + } + + @Test + public void testUpgradeGroup() throws Exception { + /* + * Each subgroup has a different PDP type and name. + * + * Type is not supported by the first subgroup. + * + * Second subgroup matches. + * + * Third subgroup already contains the policy. + * + * Last subgroup matches. + */ + + when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao.json")); + + prov.clear(); + prov.add(false, true, false, true); + + prov.process(loadRequest(), this::handle); + + assertGroup(getGroupUpdates(), GROUP1_NAME); + + List requests = getUpdateRequests(2); + assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); + assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4); + } + + @Test + public void testUpgradeGroup_Multiple() throws Exception { + /* + * Policy data in the DB: policy1=type1, policy2=type2, policy3=type3, + * policy4=type1 + * + * Group data in the DB: group1=(type1=pdp1, type3=pdp3) group2=(type2=pdp2) + * + * Request specifies: policy1, policy2, policy3, policy4 + * + * Should create new versions of group1 and group2. + * + * Should update old versions of group1 and group2. Should also update new version + * of group1 twice. + * + * Should generate updates to pdp1, pdp2, and pdp3. + */ + + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")) + .thenReturn(loadPolicies("upgradeGroupPolicy2.json")) + .thenReturn(loadPolicies("upgradeGroupPolicy3.json")) + .thenReturn(loadPolicies("upgradeGroupPolicy4.json")); + + List groups1 = loadGroups("upgradeGroupGroup1.json"); + List groups2 = loadGroups("upgradeGroupGroup2.json"); + + /* + * these are in pairs of (get-group, get-max-group) matching each policy in the + * request + */ + // @formatter:off + when(dao.getFilteredPdpGroups(any())) + .thenReturn(groups1).thenReturn(groups1) + .thenReturn(groups2).thenReturn(groups2) + .thenReturn(groups1).thenReturn(groups1) + .thenReturn(groups1).thenReturn(groups1); + // @formatter:on + + // multiple policies in the request + PdpDeployPolicies request = loadFile("updateGroupReqMultiple.json", PdpDeployPolicies.class); + + prov.process(request, (data, deploy) -> { + for (ToscaPolicyIdentifierOptVersion policy : deploy.getPolicies()) { + handle(data, policy); + } + }); + + // verify updates + List changes = getGroupUpdates(); + assertGroup(changes, GROUP1_NAME); + assertGroup(changes, GROUP2_NAME); + + List requests = getUpdateRequests(3); + assertUpdateIgnorePolicy(requests, GROUP1_NAME, PDP1_TYPE, PDP1); + assertUpdateIgnorePolicy(requests, GROUP2_NAME, PDP2_TYPE, PDP2); + assertUpdateIgnorePolicy(requests, GROUP1_NAME, PDP3_TYPE, PDP3); + } + + @Test + public void testUpgradeGroup_NothingUpdated() throws Exception { + prov.clear(); + prov.add(false); + + prov.process(loadRequest(), this::handle); + + verify(dao, never()).createPdpGroups(any()); + verify(dao, never()).updatePdpGroups(any()); + verify(reqmap, never()).addRequest(any(PdpUpdate.class)); + } + + + protected void assertUpdate(List updates, String groupName, String pdpType, String pdpName) { + + PdpUpdate update = updates.remove(0); + + assertEquals(groupName, update.getPdpGroup()); + assertEquals(pdpType, update.getPdpSubgroup()); + assertEquals(pdpName, update.getName()); + assertTrue(update.getPolicies().contains(policy1)); + } + + /** + * Loads a standard request. + * + * @return a standard request + */ + protected ToscaPolicyIdentifierOptVersion loadRequest() { + return loadRequest("requestBase.json"); + } + + /** + * Loads a request from a JSON file. + * + * @param fileName name of the file from which to load + * @return the request that was loaded + */ + protected ToscaPolicyIdentifierOptVersion loadRequest(String fileName) { + return loadFile(fileName, ToscaPolicyIdentifierOptVersion.class); + } + + /** + * Loads an empty request. + * + * @return an empty request + */ + protected ToscaPolicyIdentifierOptVersion loadEmptyRequest() { + return loadRequest("emptyRequestBase.json"); + } + + /** + * Handles a request by invoking the provider's processPolicy method. + * + * @param data session data + * @param request request to be handled + * @throws PfModelException if an error occurred + */ + private void handle(SessionData data, ToscaPolicyIdentifierOptVersion request) throws PfModelException { + prov.processPolicy(data, request); + } + + + private static class MyProvider extends ProviderBase { + /** + * Used to determine whether or not to make an update when + * {@link #makeUpdater(ToscaPolicy)} is called. The updater function removes an + * item from this queue each time it is invoked. + */ + private final Queue shouldUpdate = new LinkedList<>(); + + /** + * Constructs the object and queues up several successful updates. + */ + public MyProvider() { + for (int x = 0; x < 10; ++x) { + shouldUpdate.add(true); + } + } + + public void clear() { + shouldUpdate.clear(); + } + + public void add(Boolean... update) { + shouldUpdate.addAll(Arrays.asList(update)); + } + + @Override + protected Updater makeUpdater(SessionData data, ToscaPolicy policy, + ToscaPolicyIdentifierOptVersion desiredPolicy) { + + return (group, subgroup) -> { + if (shouldUpdate.remove()) { + // queue indicated that the update should succeed + subgroup.getPolicies().add(policy.getIdentifier()); + + data.trackDeploy(policy.getIdentifier(), Collections.singleton(PDP1)); + data.trackUndeploy(policy.getIdentifier(), Collections.singleton(PDP2)); + + ToscaPolicyIdentifier ident2 = new ToscaPolicyIdentifier(POLICY1_NAME, "9.9.9"); + data.trackDeploy(ident2, Collections.singleton(PDP3)); + data.trackUndeploy(ident2, Collections.singleton(PDP4)); + return true; + + } else { + // queue indicated that no update should be made this time + return false; + } + }; + } + } +} 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 new file mode 100644 index 00000000..34b7c61f --- /dev/null +++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestSessionData.java @@ -0,0 +1,662 @@ +/* + * ============LICENSE_START======================================================= + * ONAP PAP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pap.main.rest; + +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +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.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; + +public class TestSessionData extends ProviderSuper { + private static final String GROUP_NAME = "groupA"; + 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 POLICY_VERSION_PREFIX = "1.2."; + private static final String POLICY_NAME = "myPolicy"; + private static final String POLICY_VERSION = POLICY_VERSION_PREFIX + "3"; + private static final String POLICY_TYPE = "myType"; + private static final String POLICY_TYPE_VERSION = "10.20.30"; + private static final String EXPECTED_EXCEPTION = "expected exception"; + + private SessionData session; + private ToscaPolicyIdentifierOptVersion ident; + private ToscaPolicyTypeIdentifier type; + private ToscaPolicyTypeIdentifier type2; + private PdpGroup group1; + private PdpGroup group2; + + /** + * Initializes mocks and a session. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + super.setUp(); + + ident = new ToscaPolicyIdentifierOptVersion(POLICY_NAME, POLICY_VERSION); + type = new ToscaPolicyTypeIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION); + type2 = new ToscaPolicyTypeIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION + "0"); + group1 = loadGroup("group1.json"); + group2 = loadGroup("group2.json"); + + session = new SessionData(dao); + } + + @Test + public void testGetPolicyType() throws Exception { + ToscaPolicyType policy1 = makePolicyType(POLICY_TYPE, POLICY_TYPE_VERSION); + when(dao.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenReturn(Arrays.asList(policy1)); + + assertSame(policy1, session.getPolicyType(type)); + + // retrieve a second time - should use cache + assertSame(policy1, session.getPolicyType(type)); + } + + @Test + public void testGetPolicyType_NotFound() throws Exception { + when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); + + assertNull(session.getPolicyType(type)); + } + + @Test + public void testGetPolicyType_DaoEx() throws Exception { + PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION); + when(dao.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenThrow(ex); + + assertThatThrownBy(() -> session.getPolicyType(type)).isSameAs(ex); + } + + @Test + public void testGetPolicy_NullVersion() throws Exception { + ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); + + ident.setVersion(null); + assertSame(policy1, session.getPolicy(ident)); + + ToscaPolicyFilter filter = getPolicyFilter(); + assertEquals(POLICY_NAME, filter.getName()); + assertEquals(ToscaPolicyFilter.LATEST_VERSION, filter.getVersion()); + assertEquals(null, filter.getVersionPrefix()); + + // retrieve a second time using full version - should use cache + assertSame(policy1, session.getPolicy(new ToscaPolicyIdentifierOptVersion(policy1.getIdentifier()))); + verify(dao).getFilteredPolicyList(any()); + } + + @Test + public void testGetPolicy_MajorVersion() throws Exception { + ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); + + ident.setVersion("1"); + assertSame(policy1, session.getPolicy(ident)); + + ToscaPolicyFilter filter = getPolicyFilter(); + assertEquals(POLICY_NAME, filter.getName()); + assertEquals(ToscaPolicyFilter.LATEST_VERSION, filter.getVersion()); + assertEquals("1.", filter.getVersionPrefix()); + + // retrieve a second time using full version - should use cache + assertSame(policy1, session.getPolicy(new ToscaPolicyIdentifierOptVersion(policy1.getIdentifier()))); + verify(dao).getFilteredPolicyList(any()); + } + + @Test + public void testGetPolicy_MajorMinorVersion() throws Exception { + ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); + + ident.setVersion(POLICY_VERSION); + assertSame(policy1, session.getPolicy(ident)); + + ToscaPolicyFilter filter = getPolicyFilter(); + assertEquals(POLICY_NAME, filter.getName()); + assertEquals(POLICY_VERSION, filter.getVersion()); + assertEquals(null, filter.getVersionPrefix()); + + // retrieve a second time using full version - should use cache + assertSame(policy1, session.getPolicy(new ToscaPolicyIdentifierOptVersion(policy1.getIdentifier()))); + verify(dao).getFilteredPolicyList(any()); + } + + @Test + public void testGetPolicy_NotFound() throws Exception { + when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); + + assertNull(session.getPolicy(ident)); + } + + @Test + public void testGetPolicy_DaoEx() throws Exception { + PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION); + when(dao.getFilteredPolicyList(any())).thenThrow(ex); + + assertThatThrownBy(() -> session.getPolicy(ident)).isSameAs(ex); + } + + @Test + public void testIsVersionPrefix() { + assertTrue(SessionData.isVersionPrefix("1")); + assertTrue(SessionData.isVersionPrefix("12")); + assertTrue(SessionData.isVersionPrefix("1.2")); + assertTrue(SessionData.isVersionPrefix("1.23")); + + assertFalse(SessionData.isVersionPrefix("1.")); + assertFalse(SessionData.isVersionPrefix("1.2.")); + assertFalse(SessionData.isVersionPrefix("1.2.3")); + assertFalse(SessionData.isVersionPrefix("1.2.3.")); + assertFalse(SessionData.isVersionPrefix("1.2.3.4")); + } + + @Test + public void testAddRequests_testGetPdpStateChanges_testGetPdpUpdates() { + // pre-load with a update and state-change for other PDPs + PdpUpdate update2 = makeUpdate(PDP2); + session.addUpdate(update2); + + PdpStateChange change3 = makeStateChange(PDP3); + session.addStateChange(change3); + + // add requests + PdpUpdate update = makeUpdate(PDP1); + PdpStateChange change = makeStateChange(PDP1); + session.addRequests(update, change); + verifyRequests(update, update2, change, change3); + + /* + * repeat with a new pair + */ + update = makeUpdate(PDP1); + change = makeStateChange(PDP1); + session.addRequests(update, change); + verifyRequests(update, update2, change, change3); + + // just make an update this time + update = makeUpdate(PDP1); + session.addUpdate(update); + verifyRequests(update, update2, change, change3); + } + + private void verifyRequests(PdpUpdate update, PdpUpdate update2, PdpStateChange change, PdpStateChange change3) { + List> requests = sort(session.getPdpRequests(), this::compare); + assertEquals(3, requests.size()); + + System.out.println(requests); + System.out.println(update); + + Iterator> reqiter = requests.iterator(); + Pair pair = reqiter.next(); + assertSame(update, pair.getLeft()); + assertSame(change, pair.getRight()); + + pair = reqiter.next(); + assertSame(update2, pair.getLeft()); + assertSame(null, pair.getRight()); + + pair = reqiter.next(); + assertSame(null, pair.getLeft()); + assertSame(change3, pair.getRight()); + + // verify individual lists + List updates = Arrays.asList(update, update2); + assertEquals(sort(updates, this::compare), sort(session.getPdpUpdates(), this::compare)); + + List changes = Arrays.asList(change, change3); + assertEquals(sort(changes, this::compare), sort(session.getPdpStateChanges(), this::compare)); + } + + @Test + public void testAddRequests_MismatchedNames() { + PdpUpdate update = makeUpdate(PDP1); + PdpStateChange change = makeStateChange(PDP2); + assertThatIllegalArgumentException().isThrownBy(() -> session.addRequests(update, change)) + .withMessage("PDP name mismatch pdp_1, pdp_2"); + } + + @Test + public void testAddUpdate_testGetPdpUpdates() { + // several different updates, but one duplicate + PdpUpdate update1 = makeUpdate(PDP1); + session.addUpdate(update1); + + PdpUpdate update2 = makeUpdate(PDP2); + session.addUpdate(update2); + + PdpUpdate update3 = makeUpdate(PDP3); + session.addUpdate(update3); + + List lst = sort(getUpdateRequests(), this::compare); + assertEquals(Arrays.asList(update1, update2, update3).toString(), lst.toString()); + + // overwrite one + update2 = makeUpdate(PDP2); + session.addUpdate(update2); + + lst = sort(getUpdateRequests(), this::compare); + assertEquals(Arrays.asList(update1, update2, update3).toString(), lst.toString()); + } + + @Test + public void testAddStateChange_testGetPdpStateChanges() { + // several different changes, but one duplicate + PdpStateChange change1 = makeStateChange(PDP1); + session.addStateChange(change1); + + PdpStateChange change2 = makeStateChange(PDP2); + session.addStateChange(change2); + + PdpStateChange change3 = makeStateChange(PDP3); + session.addStateChange(change3); + + List lst = sort(getStateChangeRequests(), this::compare); + assertEquals(Arrays.asList(change1, change2, change3).toString(), lst.toString()); + + // overwrite one + change2 = makeStateChange(PDP2); + session.addStateChange(change2); + + lst = sort(getStateChangeRequests(), this::compare); + assertEquals(Arrays.asList(change1, change2, change3).toString(), lst.toString()); + } + + private ToscaPolicyType makePolicyType(String name, String version) { + ToscaPolicyType type = new ToscaPolicyType(); + + type.setName(name); + type.setVersion(version); + + return type; + } + + private ToscaPolicy makePolicy(String name, String version) { + ToscaPolicy policy = new ToscaPolicy(); + + policy.setName(name); + policy.setVersion(version); + + return policy; + } + + @Test + public void testCreate() throws Exception { + assertTrue(session.isUnchanged()); + + session.create(group1); + assertSame(group1, session.getGroup(group1.getName())); + assertFalse(session.isUnchanged()); + + // can add another + session.create(group2); + assertSame(group1, session.getGroup(group1.getName())); + assertSame(group2, session.getGroup(group2.getName())); + assertFalse(session.isUnchanged()); + + // cannot overwrite + assertThatIllegalStateException().isThrownBy(() -> session.create(group1)) + .withMessage("group already cached: groupA"); + } + + @Test + public void testUpdate() throws Exception { + assertTrue(session.isUnchanged()); + + // force the groups into the cache + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2)); + session.getActivePdpGroupsByPolicyType(type); + + /* + * try group 1 + */ + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); + PdpGroup newgrp = new PdpGroup(group1); + session.update(newgrp); + assertFalse(session.isUnchanged()); + + // repeat + newgrp = new PdpGroup(group1); + session.update(newgrp); + assertFalse(session.isUnchanged()); + + /* + * try group 2 + */ + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group2)); + newgrp = new PdpGroup(group2); + session.update(newgrp); + assertFalse(session.isUnchanged()); + + // repeat + newgrp = new PdpGroup(group2); + session.update(newgrp); + assertFalse(session.isUnchanged()); + } + + @Test + public void testUpdate_NotInCache() throws Exception { + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); + + assertThatIllegalStateException().isThrownBy(() -> session.update(new PdpGroup(group1))) + .withMessage("group not cached: groupA"); + } + + @Test + public void testGetGroup() throws Exception { + when(dao.getPdpGroups(GROUP_NAME)).thenReturn(Arrays.asList(group1)); + + assertSame(group1, session.getGroup(GROUP_NAME)); + verify(dao).getPdpGroups(any()); + + // repeat + assertSame(group1, session.getGroup(GROUP_NAME)); + + // should not access dao again + verify(dao, times(1)).getPdpGroups(any()); + } + + @Test + public void testGetGroup_NotFound() throws Exception { + when(dao.getPdpGroups(GROUP_NAME)).thenReturn(Collections.emptyList()); + + assertNull(session.getGroup(GROUP_NAME)); + verify(dao).getPdpGroups(any()); + + // repeat + assertNull(session.getGroup(GROUP_NAME)); + + // SHOULD access dao again + verify(dao, times(2)).getPdpGroups(GROUP_NAME); + + // find it this time + when(dao.getPdpGroups(GROUP_NAME)).thenReturn(Arrays.asList(group1)); + assertSame(group1, session.getGroup(GROUP_NAME)); + verify(dao, times(3)).getPdpGroups(GROUP_NAME); + } + + @Test + public void testGetGroup_DaoEx() throws Exception { + PfModelException ex = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); + when(dao.getPdpGroups(GROUP_NAME)).thenThrow(ex); + + assertThatThrownBy(() -> session.getGroup(GROUP_NAME)).isSameAs(ex); + } + + @Test + public void testGetActivePdpGroupsByPolicyType() throws Exception { + List groups = Arrays.asList(group1, group2); + when(dao.getFilteredPdpGroups(any())).thenReturn(groups); + + // repeat + assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); + assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); + assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); + + // only invoked once - should have used the cache for the rest + verify(dao, times(1)).getFilteredPdpGroups(any()); + } + + @Test + public void testAddGroup() throws Exception { + List groups = Arrays.asList(group1, group2); + when(dao.getFilteredPdpGroups(any())).thenReturn(groups); + + // query by each type + assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); + assertEquals(groups, session.getActivePdpGroupsByPolicyType(type2)); + + // invoked once for each type + verify(dao, times(2)).getFilteredPdpGroups(any()); + + // repeat - should be no more invocations + assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); + assertEquals(groups, session.getActivePdpGroupsByPolicyType(type2)); + verify(dao, times(2)).getFilteredPdpGroups(any()); + } + + @Test + public void testUpdateDb() throws Exception { + // force the groups into the cache + PdpGroup group3 = loadGroup("group3.json"); + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2, group3)); + session.getActivePdpGroupsByPolicyType(type); + + // create groups 4 & 5 + PdpGroup group4 = loadGroup("group4.json"); + session.create(group4); + + PdpGroup group5 = loadGroup("group5.json"); + session.create(group5); + + // update group 1 + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); + PdpGroup newgrp1 = new PdpGroup(group1); + session.update(newgrp1); + + // another update + newgrp1 = new PdpGroup(newgrp1); + session.update(newgrp1); + + // update group 3 + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group3)); + PdpGroup newgrp3 = new PdpGroup(group3); + session.update(newgrp3); + + // update group 5 + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group5)); + PdpGroup newgrp5 = new PdpGroup(group5); + session.update(newgrp5); + + // push the changes to the DB + session.updateDb(); + + // expect one create for groups 4 & 5 (group5 replaced by newgrp5) + List creates = getGroupCreates(); + assertEquals(2, creates.size()); + assertSame(group4, creates.get(0)); + assertSame(newgrp5, creates.get(1)); + + // expect one update for groups 1 & 3 + List updates = getGroupUpdates(); + assertEquals(2, updates.size()); + assertSame(newgrp1, updates.get(0)); + assertSame(newgrp3, updates.get(1)); + } + + @Test + public void testUpdateDb_Empty() throws Exception { + // force data into the cache + when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2)); + session.getActivePdpGroupsByPolicyType(type); + + session.updateDb(); + verify(dao, never()).createPdpGroups(any()); + verify(dao, never()).updatePdpGroups(any()); + } + + @Test + public void testDeleteGroupFromDb() throws Exception { + session.deleteGroupFromDb(group1); + + verify(dao).deletePdpGroup(group1.getName()); + } + + @Test + public void testTrackDeploy() throws PfModelException { + testTrack(session::getDeployData, session::getUndeployData, session::trackDeploy); + } + + /** + * Tests trackDeploy() when there is something in the undeployed list. + * + * @throws PfModelException if an error occurs + */ + @Test + public void testTrackDeployRemoveUndeploy() throws PfModelException { + testTrack(session::getDeployData, session::getUndeployData, session::trackUndeploy, session::trackDeploy); + } + + @Test + public void testTrackUndeploy() throws PfModelException { + testTrack(session::getUndeployData, session::getDeployData, session::trackUndeploy); + } + + /** + * Tests trackUndeploy() when there is something in the deployed list. + * + * @throws PfModelException if an error occurs + */ + @Test + public void testTrackUndeployRemoveUndeploy() throws PfModelException { + testTrack(session::getUndeployData, session::getDeployData, session::trackDeploy, session::trackUndeploy); + } + + protected void testTrack(Supplier> expected, + Supplier> unexpected, TrackEx... trackFuncs) + throws PfModelException { + + ToscaPolicy policy = makePolicy(POLICY_NAME, POLICY_VERSION); + policy.setType(POLICY_TYPE); + policy.setTypeVersion(POLICY_TYPE_VERSION); + + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy)); + + ToscaPolicyIdentifier policyId = new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION); + List pdps = Arrays.asList(PDP1, PDP2); + + for (TrackEx trackFunc : trackFuncs) { + trackFunc.accept(policyId, pdps); + } + + // "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()); + + PolicyPdpNotificationData data = dataList.iterator().next(); + assertEquals(policyId, data.getPolicyId()); + assertEquals(type, data.getPolicyType()); + assertEquals("[pdp_1, pdp_2]", new TreeSet<>(data.getPdps()).toString()); + } + + private PdpUpdate makeUpdate(String pdpName) { + PdpUpdate update = new PdpUpdate(); + + update.setName(pdpName); + + return update; + } + + private PdpStateChange makeStateChange(String pdpName) { + PdpStateChange change = new PdpStateChange(); + + change.setName(pdpName); + + return change; + } + + private ToscaPolicyFilter getPolicyFilter() throws Exception { + ArgumentCaptor captor = ArgumentCaptor.forClass(ToscaPolicyFilter.class); + verify(dao).getFilteredPolicyList(captor.capture()); + + return captor.getValue(); + } + + private List getUpdateRequests() { + return session.getPdpUpdates(); + } + + private List getStateChangeRequests() { + return session.getPdpStateChanges(); + } + + private List sort(Collection collection, Comparator comparator) { + List lst = new ArrayList<>(collection); + Collections.sort(lst, comparator); + + return lst; + } + + private int compare(Pair left, Pair right) { + return getName(left).compareTo(getName(right)); + } + + private int compare(PdpUpdate left, PdpUpdate right) { + return left.getName().compareTo(right.getName()); + } + + private int compare(PdpStateChange left, PdpStateChange right) { + return left.getName().compareTo(right.getName()); + } + + private String getName(Pair pair) { + return (pair.getKey() != null ? pair.getKey().getName() : pair.getValue().getName()); + } + + @FunctionalInterface + private static interface TrackEx { + public void accept(ToscaPolicyIdentifier policyId, Collection pdps) throws PfModelException; + } +} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java deleted file mode 100644 index 3f29fb56..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java +++ /dev/null @@ -1,276 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; -import org.junit.Before; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.onap.policy.common.utils.coder.Coder; -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.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpGroups; -import org.onap.policy.models.pdp.concepts.PdpStateChange; -import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; -import org.onap.policy.pap.main.PapConstants; -import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper; -import org.onap.policy.pap.main.comm.PdpModifyRequestMap; -import org.onap.policy.pap.main.notification.PolicyNotifier; - -/** - * Super class for TestPdpGroupDeployProviderXxx classes. - */ -public class ProviderSuper { - private static final Coder coder = new StandardCoder(); - - @Mock - protected PolicyModelsProvider dao; - - @Mock - protected PolicyNotifier notifier; - - - /** - * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups(). - */ - @Captor - private ArgumentCaptor> updateCaptor; - - protected Object lockit; - protected PdpModifyRequestMap reqmap; - protected PolicyModelsProviderFactoryWrapper daofact; - protected ToscaPolicy policy1; - - - /** - * Configures DAO, captors, and various mocks. - */ - @Before - public void setUp() throws Exception { - - Registry.newRegistry(); - - MockitoAnnotations.initMocks(this); - - reqmap = mock(PdpModifyRequestMap.class); - - lockit = new Object(); - daofact = mock(PolicyModelsProviderFactoryWrapper.class); - policy1 = loadPolicy("policy.json"); - - when(daofact.create()).thenReturn(dao); - - List groups = loadGroups("groups.json"); - - when(dao.getFilteredPdpGroups(any())).thenReturn(groups); - - when(dao.createPdpGroups(any())).thenAnswer(answer -> answer.getArgumentAt(0, List.class)); - when(dao.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgumentAt(0, List.class)); - - Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit); - Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap); - Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact); - Registry.register(PapConstants.REG_POLICY_NOTIFIER, notifier); - } - - protected void assertGroup(List groups, String name) { - PdpGroup group = groups.remove(0); - - assertEquals(name, group.getName()); - } - - protected void assertUpdateIgnorePolicy(List updates, String groupName, String pdpType, String pdpName) { - - PdpUpdate update = updates.remove(0); - - assertEquals(groupName, update.getPdpGroup()); - assertEquals(pdpType, update.getPdpSubgroup()); - assertEquals(pdpName, update.getName()); - } - - /** - * Gets the input to the create() method. - * - * @return the input that was passed to the dao.updatePdpGroups() method - * @throws Exception if an error occurred - */ - protected List getGroupCreates() throws Exception { - verify(dao).createPdpGroups(updateCaptor.capture()); - - return copyList(updateCaptor.getValue()); - } - - /** - * Gets the input to the update() method. - * - * @return the input that was passed to the dao.updatePdpGroups() method - * @throws Exception if an error occurred - */ - protected List getGroupUpdates() throws Exception { - verify(dao).updatePdpGroups(updateCaptor.capture()); - - return copyList(updateCaptor.getValue()); - } - - /** - * Gets the state-changes that were added to the request map. - * - * @param count the number of times the method is expected to have been called - * @return the state-changes that were added to the request map - */ - protected List getStateChangeRequests(int count) { - ArgumentCaptor captor = ArgumentCaptor.forClass(PdpStateChange.class); - - verify(reqmap, times(count)).addRequest(any(), captor.capture()); - - return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList()); - } - - /** - * Gets the updates that were added to the request map. - * - * @param count the number of times the method is expected to have been called - * @return the updates that were added to the request map - */ - protected List getUpdateRequests(int count) { - ArgumentCaptor captor = ArgumentCaptor.forClass(PdpUpdate.class); - - verify(reqmap, times(count)).addRequest(captor.capture(), any()); - - return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList()); - } - - /** - * Copies a list and sorts it by group name. - * - * @param source source list to copy - * @return a copy of the source list - */ - private List copyList(List source) { - List newlst = new ArrayList<>(source); - Collections.sort(newlst, (left, right) -> left.getName().compareTo(right.getName())); - return newlst; - } - - /** - * Loads a list of groups. - * - * @param fileName name of the file from which to load - * @return a list of groups - */ - protected List loadGroups(String fileName) { - return loadPdpGroups(fileName).getGroups(); - } - - /** - * Loads a PdpGroups. - * - * @param fileName name of the file from which to load - * @return a PdpGroups - */ - protected PdpGroups loadPdpGroups(String fileName) { - return loadFile(fileName, PdpGroups.class); - } - - /** - * Loads a group. - * - * @param fileName name of the file from which to load - * @return a group - */ - protected PdpGroup loadGroup(String fileName) { - return loadFile(fileName, PdpGroup.class); - } - - /** - * Loads a list of policies. - * - * @param fileName name of the file from which to load - * @return a list of policies - */ - protected List loadPolicies(String fileName) { - return loadFile(fileName, PolicyList.class).policies; - } - - /** - * Loads a policy. - * - * @param fileName name of the file from which to load - * @return a policy - */ - protected ToscaPolicy loadPolicy(String fileName) { - return loadFile(fileName, ToscaPolicy.class); - } - - /** - * Loads a policy type. - * - * @param fileName name of the file from which to load - * @return a policy type - */ - protected ToscaPolicyType loadPolicyType(String fileName) { - return loadFile(fileName, ToscaPolicyType.class); - } - - /** - * Loads an object from a JSON file. - * - * @param fileName name of the file from which to load - * @param clazz the class of the object to be loaded - * @return the object that was loaded from the file - */ - protected T loadFile(String fileName, Class clazz) { - File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName)); - try { - return coder.decode(propFile, clazz); - - } catch (CoderException e) { - throw new RuntimeException(e); - } - } - - /** - * 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. - */ - private static class PolicyList { - private List policies; - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestGroupData.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestGroupData.java deleted file mode 100644 index 92b30b2e..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestGroupData.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.models.pdp.concepts.PdpGroup; - -public class TestGroupData { - private static final String NAME = "my-name"; - - private PdpGroup oldGroup; - private PdpGroup newGroup; - private GroupData data; - - /** - * Sets up. - */ - @Before - public void setUp() { - oldGroup = new PdpGroup(); - oldGroup.setName(NAME); - - newGroup = new PdpGroup(oldGroup); - - data = new GroupData(oldGroup); - } - - @Test - public void testNew() { - data = new GroupData(oldGroup, true); - assertSame(oldGroup, data.getGroup()); - - assertFalse(data.isUnchanged()); - assertTrue(data.isNew()); - assertFalse(data.isUpdated()); - - data.update(newGroup); - assertFalse(data.isUnchanged()); - assertTrue(data.isNew()); - assertFalse(data.isUpdated()); - assertSame(newGroup, data.getGroup()); - - // repeat with a new group - newGroup = new PdpGroup(oldGroup); - data.update(newGroup); - assertFalse(data.isUnchanged()); - assertTrue(data.isNew()); - assertFalse(data.isUpdated()); - assertSame(newGroup, data.getGroup()); - } - - @Test - public void testUpdateOnly() { - assertTrue(data.isUnchanged()); - assertFalse(data.isUpdated()); - assertSame(oldGroup, data.getGroup()); - - data.update(newGroup); - - assertFalse(data.isUnchanged()); - assertTrue(data.isUpdated()); - assertFalse(data.isNew()); - assertSame(newGroup, data.getGroup()); - - // repeat - newGroup = new PdpGroup(oldGroup); - data.update(newGroup); - assertFalse(data.isUnchanged()); - assertTrue(data.isUpdated()); - assertFalse(data.isNew()); - assertSame(newGroup, data.getGroup()); - - // incorrect name - newGroup = new PdpGroup(oldGroup); - newGroup.setName("other"); - assertThatIllegalArgumentException().isThrownBy(() -> data.update(newGroup)) - .withMessage("expected group my-name, but received other"); - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteControllerV1.java deleted file mode 100644 index 670d1e05..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteControllerV1.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 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. - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.junit.Assert.assertEquals; - -import javax.ws.rs.client.Invocation; -import javax.ws.rs.core.Response; -import org.junit.Test; -import org.onap.policy.models.pap.concepts.PdpGroupDeleteResponse; -import org.onap.policy.pap.main.rest.CommonPapRestServer; - -/** - * Note: this tests failure cases; success cases are tested by tests in the "e2e" package. - */ -public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer { - - private static final String GROUP_NOT_FOUND = "group not found"; - private static final String DELETE_GROUP_ENDPOINT = "pdps/groups"; - private static final String DELETE_POLICIES_ENDPOINT = "pdps/policies"; - - @Test - public void testSwagger() throws Exception { - super.testSwagger(DELETE_GROUP_ENDPOINT + "/{name}"); - - super.testSwagger(DELETE_POLICIES_ENDPOINT + "/{name}"); - super.testSwagger(DELETE_POLICIES_ENDPOINT + "/{name}/versions/{version}"); - } - - @Test - public void testDeleteGroup() throws Exception { - String uri = DELETE_GROUP_ENDPOINT + "/my-name"; - - Invocation.Builder invocationBuilder = sendRequest(uri); - Response rawresp = invocationBuilder.delete(); - PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - assertEquals(GROUP_NOT_FOUND, resp.getErrorDetails()); - - rawresp = invocationBuilder.delete(); - resp = rawresp.readEntity(PdpGroupDeleteResponse.class); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - assertEquals(GROUP_NOT_FOUND, resp.getErrorDetails()); - - // verify it fails when no authorization info is included - checkUnauthRequest(uri, req -> req.delete()); - } - - @Test - public void testDeletePolicy() throws Exception { - String uri = DELETE_POLICIES_ENDPOINT + "/my-name"; - - Invocation.Builder invocationBuilder = sendRequest(uri); - Response rawresp = invocationBuilder.delete(); - PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - assertEquals("cannot find policy: my-name null", resp.getErrorDetails()); - - rawresp = invocationBuilder.delete(); - resp = rawresp.readEntity(PdpGroupDeleteResponse.class); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - assertEquals("cannot find policy: my-name null", resp.getErrorDetails()); - - // verify it fails when no authorization info is included - checkUnauthRequest(uri, req -> req.delete()); - } - - @Test - public void testDeletePolicyVersion() throws Exception { - String uri = DELETE_POLICIES_ENDPOINT + "/my-name/versions/3"; - - Invocation.Builder invocationBuilder = sendRequest(uri); - Response rawresp = invocationBuilder.delete(); - PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - assertEquals("cannot find policy: my-name 3", resp.getErrorDetails()); - - rawresp = invocationBuilder.delete(); - resp = rawresp.readEntity(PdpGroupDeleteResponse.class); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - assertEquals("cannot find policy: my-name 3", resp.getErrorDetails()); - - // verify it fails when no authorization info is included - checkUnauthRequest(uri, req -> req.delete()); - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java deleted file mode 100644 index cac16808..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java +++ /dev/null @@ -1,297 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Arrays; -import java.util.List; -import java.util.Set; -import javax.ws.rs.core.Response.Status; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; -import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.models.pdp.enums.PdpState; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; -import org.onap.policy.pap.main.rest.depundep.ProviderBase.Updater; - -public class TestPdpGroupDeleteProvider extends ProviderSuper { - private static final String EXPECTED_EXCEPTION = "expected exception"; - private static final String GROUP1_NAME = "groupA"; - - @Mock - private SessionData session; - - @Captor - private ArgumentCaptor> pdpCaptor; - - private MyProvider prov; - private ToscaPolicyIdentifierOptVersion optIdent; - private ToscaPolicyIdentifierOptVersion fullIdent; - private ToscaPolicyIdentifier ident; - private Updater updater; - - - @AfterClass - public static void tearDownAfterClass() { - Registry.newRegistry(); - } - - /** - * Configures mocks and objects. - * - * @throws Exception if an error occurs - */ - @Before - public void setUp() throws Exception { - - super.setUp(); - - ident = policy1.getIdentifier(); - optIdent = new ToscaPolicyIdentifierOptVersion(ident.getName(), null); - fullIdent = new ToscaPolicyIdentifierOptVersion(ident.getName(), ident.getVersion()); - - prov = new MyProvider(); - - updater = prov.makeUpdater(session, policy1, fullIdent); - } - - @Test - public void testDeleteGroup_Inctive() throws Exception { - PdpGroup group = loadGroup("deleteGroup.json"); - - when(session.getGroup(GROUP1_NAME)).thenReturn(group); - - prov.deleteGroup(GROUP1_NAME); - - verify(session).deleteGroupFromDb(group); - - // should be no PDP requests - verify(session, never()).addRequests(any(), any()); - } - - @Test - public void testDeleteGroup_Active() throws Exception { - PdpGroup group = loadGroup("deleteGroup.json"); - - group.setPdpGroupState(PdpState.ACTIVE); - - when(session.getGroup(GROUP1_NAME)).thenReturn(group); - - assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isInstanceOf(PfModelException.class) - .hasMessage("group is still ACTIVE"); - } - - @Test - public void testDeleteGroup_NotFound() throws Exception { - assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isInstanceOf(PfModelException.class) - .hasMessage("group not found") - .extracting(ex -> ((PfModelException) ex).getErrorResponse().getResponseCode()) - .isEqualTo(Status.NOT_FOUND); - } - - @Test - public void testDeleteGroup_Inactive() throws Exception { - PdpGroup group = loadGroup("deleteGroup.json"); - - when(session.getGroup(GROUP1_NAME)).thenReturn(group); - - prov.deleteGroup(GROUP1_NAME); - - verify(session).deleteGroupFromDb(group); - - // should done no requests for the PDPs - verify(session, never()).addRequests(any(), any()); - } - - @Test - public void testDeleteGroup_DaoEx() throws Exception { - PdpGroup group = loadGroup("deleteGroup.json"); - - when(session.getGroup(GROUP1_NAME)).thenReturn(group); - - PfModelException ex = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); - doThrow(ex).when(session).deleteGroupFromDb(group); - - assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isSameAs(ex); - } - - @Test - public void testUndeploy_testUndeployPolicy() throws Exception { - prov.undeploy(optIdent); - } - - /** - * Tests using a real provider, just to verify end-to-end functionality. - * - * @throws Exception if an error occurs - */ - @Test - public void testUndeploy_Full() throws Exception { - when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); - - PdpGroup group = loadGroup("undeploy.json"); - - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group)); - when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); - - new PdpGroupDeleteProvider().undeploy(fullIdent); - - // should have updated the old group - List updates = getGroupUpdates(); - assertEquals(1, updates.size()); - assertSame(group, updates.get(0)); - assertEquals(PdpState.ACTIVE, group.getPdpGroupState()); - - // should be one less item in the new subgroup - assertEquals(2, group.getPdpSubgroups().get(0).getPolicies().size()); - - // should have updated the PDPs - List requests = getUpdateRequests(1); - assertEquals(1, requests.size()); - PdpUpdate req = requests.get(0); - assertEquals("pdpA", req.getName()); - assertEquals(GROUP1_NAME, req.getPdpGroup()); - assertEquals("pdpTypeA", req.getPdpSubgroup()); - assertEquals(Arrays.asList(policy1, policy1), req.getPolicies()); - } - - @Test - public void testUndeployPolicy_NotFound() throws Exception { - when(session.isUnchanged()).thenReturn(true); - - assertThatThrownBy(() -> prov.undeploy(optIdent)).isInstanceOf(PfModelException.class) - .hasMessage("policy does not appear in any PDP group: policyA null"); - } - - @Test - public void testUndeployPolicy_DaoEx() throws Exception { - PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); - - prov = spy(prov); - doThrow(exc).when(prov).processPolicy(any(), any()); - - assertThatThrownBy(() -> prov.undeploy(optIdent)).isSameAs(exc); - } - - @Test - public void testUndeployPolicy_RtEx() throws Exception { - RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION); - - prov = spy(prov); - doThrow(exc).when(prov).processPolicy(any(), any()); - - assertThatThrownBy(() -> prov.undeploy(optIdent)).isSameAs(exc); - } - - @Test - public void testMakeUpdater_WithVersion() throws PfModelException { - /* - * this group has two matching policies and one policy with a different name. - */ - PdpGroup group = loadGroup("undeploy.json"); - - PdpSubGroup subgroup = group.getPdpSubgroups().get(0); - int origSize = subgroup.getPolicies().size(); - - // invoke updater - matching both name and version - assertTrue(updater.apply(group, subgroup)); - - // identified policy should have been removed - assertEquals(origSize - 1, subgroup.getPolicies().size()); - assertFalse(subgroup.getPolicies().contains(ident)); - - verify(session).trackUndeploy(eq(ident), pdpCaptor.capture()); - assertEquals("[pdpA]", pdpCaptor.getValue().toString()); - } - - @Test - public void testMakeUpdater_NullVersion() throws PfModelException { - /* - * this group has two matching policies and one policy with a different name. - */ - PdpGroup group = loadGroup("undeploy.json"); - - PdpSubGroup subgroup = group.getPdpSubgroups().get(0); - int origSize = subgroup.getPolicies().size(); - - // invoke updater - matching the name, but with a null (i.e., wild-card) version - updater = prov.makeUpdater(session, policy1, optIdent); - assertTrue(updater.apply(group, subgroup)); - - // identified policy should have been removed - assertEquals(origSize - 2, subgroup.getPolicies().size()); - assertFalse(subgroup.getPolicies().contains(ident)); - } - - @Test - public void testMakeUpdater_NotFound() throws PfModelException { - /* - * this group has one policy with a different name and one with a different - * version, but not the policy of interest. - */ - PdpGroup group = loadGroup("undeployMakeUpdaterGroupNotFound.json"); - - PdpSubGroup subgroup = group.getPdpSubgroups().get(0); - int origSize = subgroup.getPolicies().size(); - - // invoke updater - assertFalse(updater.apply(group, subgroup)); - - // should be unchanged - assertEquals(origSize, subgroup.getPolicies().size()); - } - - - private class MyProvider extends PdpGroupDeleteProvider { - - @Override - protected void process(T request, BiConsumerWithEx processor) throws PfModelException { - processor.accept(session, request); - } - - @Override - protected void processPolicy(SessionData data, ToscaPolicyIdentifierOptVersion desiredPolicy) - throws PfModelException { - // do nothing - } - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployControllerV1.java deleted file mode 100644 index 8c01e029..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployControllerV1.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 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. - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.Arrays; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import org.junit.Test; -import org.onap.policy.models.pap.concepts.PdpDeployPolicies; -import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; -import org.onap.policy.pap.main.rest.CommonPapRestServer; - -/** - * Note: this tests failure cases; success cases are tested by tests in the "e2e" package. - */ -public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer { - - private static final String DEPLOY_GROUP_ENDPOINT = "pdps"; - private static final String DEPLOY_POLICIES_ENDPOINT = "pdps/policies"; - - @Test - public void testSwagger() throws Exception { - super.testSwagger(DEPLOY_GROUP_ENDPOINT); - super.testSwagger(DEPLOY_POLICIES_ENDPOINT); - } - - @Test - public void testDeployGroup() throws Exception { - Entity entgrp = makePdpGroupEntity(); - - Invocation.Builder invocationBuilder = sendRequest(DEPLOY_GROUP_ENDPOINT); - Response rawresp = invocationBuilder.post(entgrp); - PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class); - assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); - assertNotNull(resp.getErrorDetails()); - - rawresp = invocationBuilder.post(entgrp); - resp = rawresp.readEntity(PdpGroupDeployResponse.class); - assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), rawresp.getStatus()); - assertNotNull(resp.getErrorDetails()); - - // verify it fails when no authorization info is included - checkUnauthRequest(DEPLOY_GROUP_ENDPOINT, req -> req.post(entgrp)); - } - - @Test - public void testDeployPolicies() throws Exception { - Entity entgrp = makePdpPoliciesEntity(); - - Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT); - Response rawresp = invocationBuilder.post(entgrp); - PdpGroupDeployResponse resp = rawresp.readEntity(PdpGroupDeployResponse.class); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - assertNotNull(resp.getErrorDetails()); - - rawresp = invocationBuilder.post(entgrp); - resp = rawresp.readEntity(PdpGroupDeployResponse.class); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rawresp.getStatus()); - assertNotNull(resp.getErrorDetails()); - - // verify it fails when no authorization info is included - checkUnauthRequest(DEPLOY_POLICIES_ENDPOINT, req -> req.post(entgrp)); - } - - private Entity makePdpGroupEntity() { - PdpSubGroup subgrp = new PdpSubGroup(); - subgrp.setPdpType("drools"); - - PdpGroup group = new PdpGroup(); - group.setName("drools-group"); - group.setDescription("my description"); - group.setPdpSubgroups(Arrays.asList(subgrp)); - - return Entity.entity(group, MediaType.APPLICATION_JSON); - } - - private Entity makePdpPoliciesEntity() { - ToscaPolicyIdentifierOptVersion pol1 = new ToscaPolicyIdentifierOptVersion("policy-a", "1"); - ToscaPolicyIdentifierOptVersion pol2 = new ToscaPolicyIdentifierOptVersion("policy-b", null); - - PdpDeployPolicies policies = new PdpDeployPolicies(); - policies.setPolicies(Arrays.asList(pol1, pol2)); - - return Entity.entity(policies, MediaType.APPLICATION_JSON); - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java deleted file mode 100644 index 60f9d2e8..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java +++ /dev/null @@ -1,932 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.TreeMap; -import java.util.TreeSet; -import java.util.stream.Collectors; -import javax.ws.rs.core.Response.Status; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.pap.concepts.PdpDeployPolicies; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpGroups; -import org.onap.policy.models.pdp.concepts.PdpStateChange; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; -import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.models.pdp.enums.PdpState; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; -import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; - -public class TestPdpGroupDeployProvider extends ProviderSuper { - private static final String EXPECTED_EXCEPTION = "expected exception"; - - private static final String POLICY2_NAME = "policyB"; - private static final String POLICY1_VERSION = "1.2.3"; - private static final String GROUP1_NAME = "groupA"; - private static final String PDP1_TYPE = "pdpTypeA"; - private static final String PDP2_TYPE = "pdpTypeB"; - private static final String PDP4_TYPE = "pdpTypeD"; - private static final String PDP2 = "pdpB"; - private static final String PDP4 = "pdpD"; - - private PdpGroupDeployProvider prov; - - - @AfterClass - public static void tearDownAfterClass() { - Registry.newRegistry(); - } - - /** - * Configures mocks and objects. - * - * @throws Exception if an error occurs - */ - @Before - public void setUp() throws Exception { - - super.setUp(); - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); - when(dao.getPolicyTypeList("typeA", "100.2.3")).thenReturn(Arrays.asList(loadPolicyType("daoPolicyType.json"))); - - prov = new PdpGroupDeployProvider(); - } - - @Test - public void testCreateOrUpdateGroups() throws Exception { - prov.createOrUpdateGroups(loadPdpGroups("emptyGroups.json")); - - // no groups, so no action should have been taken - assertNoGroupAction(); - } - - @Test - public void testCreateOrUpdateGroups_InvalidRequest() throws Exception { - assertThatThrownBy(() -> prov.createOrUpdateGroups(new PdpGroups())).isInstanceOf(PfModelException.class) - .hasMessageContaining("is null"); - - assertNoGroupAction(); - } - - @Test - public void testCreateOrUpdate_Invalid() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("pdpGroupState"); - - assertNoGroupAction(); - } - - @Test - public void testAddGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup group = groups.getGroups().get(0); - group.setPdpGroupState(PdpState.PASSIVE); - - prov.createOrUpdateGroups(groups); - - // should not have updated the state - assertEquals(PdpState.PASSIVE, group.getPdpGroupState()); - - assertSame(group, getGroupCreates().get(0)); - } - - @Test - public void testAddGroup_Invalid() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("pdpGroupState"); - - assertNoGroupAction(); - } - - @Test - public void testAddGroup_InvalidSubGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - - // policy won't match supported type - groups.getGroups().get(0).getPdpSubgroups().get(0).getSupportedPolicyTypes().get(0).setVersion("99.99.99"); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("supported policy"); - - assertNoGroupAction(); - } - - @Test - public void testValidateGroupOnly_NullState() throws PfModelException { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(null); - prov.createOrUpdateGroups(groups); - } - - @Test - public void testValidateGroupOnly_Active() throws PfModelException { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.ACTIVE); - prov.createOrUpdateGroups(groups); - } - - @Test - public void testValidateGroupOnly_Passive() throws PfModelException { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.PASSIVE); - prov.createOrUpdateGroups(groups); - } - - @Test - public void testValidateGroupOnly_Invalid() { - PdpGroups groups = loadPdpGroups("createGroups.json"); - groups.getGroups().get(0).setPdpGroupState(PdpState.TERMINATED); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("pdpGroupState"); - } - - @Test - public void testUpdateGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - - // DB group = new group - PdpGroup group = new PdpGroup(groups.getGroups().get(0)); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - assertNoGroupAction(); - } - - @Test - public void testUpdateGroup_PropertiesChanged() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - - PdpGroup group = new PdpGroup(groups.getGroups().get(0)); - group.setProperties(new TreeMap<>()); - - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("properties"); - - assertNoGroupAction(); - } - - @Test - public void testUpdateGroup_NewDescription() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - group.setDescription("old description"); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - assertGroupUpdateOnly(group); - - assertEquals(group.getDescription(), "my description"); - assertEquals(newgrp.toString(), group.toString()); - } - - @Test - public void testUpdateGroup_NewState() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - group.setPdpGroupState(PdpState.TEST); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - assertGroupUpdateOnly(group); - - assertEquals(PdpState.ACTIVE, group.getPdpGroupState()); - assertEquals(newgrp.toString(), group.toString()); - } - - @Test - public void testUpdateGroup_NewSubGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - PdpGroup newgrp = groups.getGroups().get(0); - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - - @Test - public void testUpdateGroup_UpdatedSubGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - // something different in this subgroup - group.getPdpSubgroups().get(0).setDesiredInstanceCount(10); - - prov.createOrUpdateGroups(groups); - - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - - @Test - public void testUpdateGroup_notifyPdpsDelSubGroups() throws Exception { - PdpGroup dbgroup = new PdpGroup(loadPdpGroups("createGroupsDelSub.json").getGroups().get(0)); - when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); - - PdpGroups groups = loadPdpGroups("createGroups.json"); - - prov.createOrUpdateGroups(groups); - - // verify that DB group was updated - List updates = getGroupUpdates(); - assertEquals(1, updates.size()); - dbgroup = updates.get(0); - - PdpGroup newgrp = groups.getGroups().get(0); - - Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); - Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies()); - - assertEquals(newgrp.toString(), dbgroup.toString()); - - // no deployment notifications - verify(notifier, never()).addDeploymentData(any()); - - // should have notified of deleted subgroup's policies/PDPs - ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier).addUndeploymentData(captor.capture()); - assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB, pdpD]"); - - // this requires a PDP UPDATE message - List pdpUpdates = getUpdateRequests(2); - assertEquals(2, pdpUpdates.size()); - - PdpUpdate pdpUpdate = pdpUpdates.get(0); - assertEquals(PDP2, pdpUpdate.getName()); - assertNull(pdpUpdate.getPdpGroup()); - - pdpUpdate = pdpUpdates.get(1); - assertEquals(PDP4, pdpUpdate.getName()); - assertNull(pdpUpdate.getPdpGroup()); - - // it also requires a PDP STATE-CHANGE message - List changes = getStateChangeRequests(2); - assertEquals(2, changes.size()); - - PdpStateChange change = changes.get(0); - assertEquals(PDP2, change.getName()); - assertEquals(PdpState.PASSIVE, change.getState()); - - change = changes.get(1); - assertEquals(PDP4, change.getName()); - assertEquals(PdpState.PASSIVE, change.getState()); - } - - @Test - public void testUpdateGroup_MultipleChanges() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); - subgrp.setDesiredInstanceCount(30); - subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION)); - subgrp.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("typeX.*", "9.8.7")); - - when(dao.getFilteredPolicyList(any())) - .thenReturn(loadPolicies("createGroupNewPolicy.json")) - .thenReturn(loadPolicies("daoPolicyList.json")) - .thenReturn(loadPolicies("createGroupNewPolicy.json")); - - prov.createOrUpdateGroups(groups); - - Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); - Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); - - assertEquals(newgrp.toString(), group.toString()); - - // this requires a PDP UPDATE message - assertGroupUpdate(group, subgrp); - } - - @Test - public void testUpdateField_Unchanged() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - assertNoGroupAction(); - } - - @Test - public void testUpdateField_WasNull() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - group.setDescription(null); - - prov.createOrUpdateGroups(groups); - - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - - @Test - public void testUpdateField_NowNull() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - newgrp.setDescription(null); - - prov.createOrUpdateGroups(groups); - - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - - @Test - public void testUpdateField_Changed() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - newgrp.setDescription(group.getDescription() + "-changed"); - - prov.createOrUpdateGroups(groups); - - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - - @Test - public void testAddSubGroup() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - PdpGroup newgrp = groups.getGroups().get(0); - - PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1); - newsub.setCurrentInstanceCount(0); - newsub.setPdpInstances(new ArrayList<>(0)); - - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - - /** - * Tests addSubgroup() when the new subgroup has a wild-card policy type. - * - * @throws Exception if an error occurs - */ - @Test - public void testAddSubGroupWildCardPolicyType() throws Exception { - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json")); - when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList()); - - PdpGroups groups = loadPdpGroups("createGroupsWildCard.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - PdpGroup newgrp = groups.getGroups().get(0); - - PdpSubGroup newsub = newgrp.getPdpSubgroups().get(1); - newsub.setCurrentInstanceCount(0); - newsub.setPdpInstances(new ArrayList<>(0)); - - assertEquals(newgrp.toString(), group.toString()); - } - - /** - * Tests addSubgroup() when the new subgroup has a wild-card policy type, but the - * policy doesn't have a matching type. - * - * @throws PfModelException if an error occurs - */ - @Test - public void testAddSubGroupWildCardPolicyTypeUnmatched() throws PfModelException { - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCardUnmatched.json")); - when(dao.getPolicyTypeList("some.*", "2.3.4")).thenReturn(Collections.emptyList()); - - PdpGroups groups = loadPdpGroups("createGroupsWildCard.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class); - } - - @Test - public void testAddSubGroup_ValidationPolicyTypeNotFound() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy type"); - } - - @Test - public void testAddSubGroup_ValidationPolicyTypeDaoEx() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSub.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); - when(dao.getPolicyTypeList(any(), any())).thenThrow(exc); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc); - } - - @Test - public void testAddSubGroup_ValidationPolicyNotFound() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSubNotFound.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).hasMessageContaining("unknown policy"); - } - - @Test - public void testAddSubGroup_ValidationPolicyDaoEx() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsNewSubNotFound.json"); - PdpGroup group = loadPdpGroups("createGroups.json").getGroups().get(0); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); - when(dao.getFilteredPolicyList(any())).thenThrow(exc); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isSameAs(exc); - } - - @Test - public void testAddSubGroup_ValidateVersionPrefixMatch() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup dbgroup = new PdpGroup(newgrp); - when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) - .thenReturn(loadPolicies("daoPolicyList.json")) - .thenReturn(loadPolicies("createGroupNewPolicy.json")); - - PdpGroups reqgroups = loadPdpGroups("createGroupsVersPrefix.json"); - - prov.createOrUpdateGroups(reqgroups); - - Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); - Collections.sort(dbgroup.getPdpSubgroups().get(0).getPolicies()); - - assertEquals(newgrp.toString(), dbgroup.toString()); - } - - @Test - public void testAddSubGroup_ValidateVersionPrefixMismatch() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup dbgroup = new PdpGroup(newgrp); - when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); - - - PdpGroups reqgroups = loadPdpGroups("createGroupsVersPrefixMismatch.json"); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(reqgroups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("different version already deployed"); - - assertNoGroupAction(); - } - - @Test - public void testUpdateSubGroup_Invalid() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - // change properties - newgrp.getPdpSubgroups().get(0).setProperties(new TreeMap<>()); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("properties"); - - assertNoGroupAction(); - } - - @Test - public void testUpdateSubGroup_SupportedPolicies() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - newgrp.getPdpSubgroups().get(0).getSupportedPolicyTypes() - .add(new ToscaPolicyTypeIdentifier("typeX.*", "9.8.7")); - - prov.createOrUpdateGroups(groups); - - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - - @Test - public void testUpdateSubGroup_DesiredCount() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - newgrp.getPdpSubgroups().get(0).setDesiredInstanceCount(20); - - prov.createOrUpdateGroups(groups); - - assertEquals(newgrp.toString(), group.toString()); - assertGroupUpdateOnly(group); - } - - @Test - public void testUpdateSubGroup_Policies() throws Exception { - PdpGroups groups = loadPdpGroups("createGroupsDelPolicy.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); - - // delete second policy - subgrp.setPolicies(subgrp.getPolicies().subList(0, 1)); - - // add new policy - ToscaPolicyIdentifier policyId2 = new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION); - subgrp.getPolicies().add(policyId2); - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("createGroupNewPolicy.json")) - .thenReturn(loadPolicies("daoPolicyList.json")) - .thenReturn(loadPolicies("daoPolicyListDelPolicy.json")) - .thenReturn(loadPolicies("createGroupNewPolicy.json")); - - prov.createOrUpdateGroups(groups); - - Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); - Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); - - assertEquals(newgrp.toString(), group.toString()); - - // should have notified of added policy/PDPs - ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier).addDeploymentData(captor.capture()); - assertDeploymentData(captor, policyId2, "[pdpA]"); - - // should have notified of deleted policy/PDPs - captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier).addUndeploymentData(captor.capture()); - assertDeploymentData(captor, new ToscaPolicyIdentifier("ToBeDeleted", POLICY1_VERSION), "[pdpA]"); - - // this requires a PDP UPDATE message - assertGroupUpdate(group, subgrp); - } - - @Test - public void testUpdateSubGroup_Unchanged() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - prov.createOrUpdateGroups(groups); - - Collections.sort(newgrp.getPdpSubgroups().get(0).getPolicies()); - Collections.sort(group.getPdpSubgroups().get(0).getPolicies()); - - assertEquals(newgrp.toString(), group.toString()); - - // no notifications - verify(notifier, never()).addDeploymentData(any()); - verify(notifier, never()).addUndeploymentData(any()); - - // no group updates - assertNoGroupAction(); - } - - @Test - public void testUpdateSubGroup_PolicyVersionMismatch() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup dbgroup = new PdpGroup(newgrp); - when(dao.getPdpGroups(dbgroup.getName())).thenReturn(Arrays.asList(dbgroup)); - - // arrange for DB policy version to be different - PdpSubGroup dbsubgrp = dbgroup.getPdpSubgroups().get(0); - dbsubgrp.getPolicies().get(0).setVersion("9.9.9"); - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("different version already deployed"); - - assertNoGroupAction(); - } - - @Test - public void testValidateSubGroup_PropertiesMismatch() throws Exception { - PdpGroups groups = loadPdpGroups("createGroups.json"); - PdpGroup newgrp = groups.getGroups().get(0); - PdpGroup group = new PdpGroup(newgrp); - when(dao.getPdpGroups(group.getName())).thenReturn(Arrays.asList(group)); - - newgrp.setProperties(new TreeMap<>()); - - assertThatThrownBy(() -> prov.createOrUpdateGroups(groups)).isInstanceOf(PfModelException.class) - .hasMessageContaining("properties"); - - assertNoGroupAction(); - } - - @Test - public void testDeployPolicies() throws PfModelException { - prov.deployPolicies(loadEmptyRequest()); - } - - /** - * Tests deployPolicies() when the supported policy type uses a wild-card. - * - * @throws Exception if an error occurs - */ - @Test - public void testDeployPoliciesWildCard() throws Exception { - when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("deployPoliciesWildCard.json")); - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyListWildCard.json")); - when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); - - policy1.setName("policy.some"); - policy1.setVersion(POLICY1_VERSION); - policy1.setType("some.type"); - policy1.setTypeVersion("100.2.3"); - - PdpDeployPolicies depreq = loadRequest(); - depreq.getPolicies().get(0).setName("policy.some"); - - prov.deployPolicies(depreq); - - assertGroup(getGroupUpdates(), GROUP1_NAME); - - 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, policy1.getIdentifier(), "[pdpB]"); - - // no undeployment notifications - verify(notifier, never()).addUndeploymentData(any()); - } - - @Test - public void testDeploySimplePolicies() throws Exception { - prov.deployPolicies(loadEmptyRequest()); - } - - @Test - public void testDeploySimplePolicies_DaoEx() throws Exception { - PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); - when(dao.getFilteredPdpGroups(any())).thenThrow(exc); - - assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc); - } - - @Test - public void testDeploySimplePolicies_DaoPfRtEx() throws Exception { - PfModelRuntimeException exc = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); - when(dao.getFilteredPdpGroups(any())).thenThrow(exc); - - assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isSameAs(exc); - } - - @Test - public void testDeploySimplePolicies_RuntimeEx() throws Exception { - RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION); - when(dao.getFilteredPolicyList(any())).thenThrow(exc); - - assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class).hasCause(exc); - } - - @Test - public void testDeploySimplePolicies_NoGroups() throws Exception { - when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("emptyGroups.json")); - - assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelException.class) - .hasMessage("policy not supported by any PDP group: policyA 1.2.3"); - } - - @Test - public void testMakeUpdater() throws Exception { - /* - * Each subgroup has a different PDP type and name. - * - * Type is not supported by the first subgroup. - * - * Second subgroup matches. - * - * Third subgroup already contains the policy. - * - * Last subgroup matches. - */ - - when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao.json")); - - prov.deployPolicies(loadRequest()); - - assertGroup(getGroupUpdates(), GROUP1_NAME); - - List requests = getUpdateRequests(2); - assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); - assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4); - - // should have notified of added policy/PDPs - ArgumentCaptor captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class); - verify(notifier).addDeploymentData(captor.capture()); - assertDeploymentData(captor, policy1.getIdentifier(), "[pdpB, pdpD]"); - - // no undeployment notifications - verify(notifier, never()).addUndeploymentData(any()); - } - - @Test - public void testMakeUpdater_PolicyVersionMismatch() throws Exception { - - // subgroup has a different version of the Policy - when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao_DiffVers.json")); - - assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelRuntimeException.class) - .hasMessageContaining("pdpTypeC").hasMessageContaining("different version already deployed"); - - verify(dao, never()).createPdpGroups(any()); - verify(dao, never()).updatePdpGroups(any()); - verify(reqmap, never()).addRequest(any(PdpUpdate.class)); - } - - @Test - public void testMakeUpdater_NoPdps() throws Exception { - - // subgroup has no PDPs - when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroup_NoPdpsDao.json")); - - assertThatThrownBy(() -> prov.deployPolicies(loadRequest())).isInstanceOf(PfModelRuntimeException.class) - .hasMessage("group " + GROUP1_NAME + " subgroup " + PDP1_TYPE + " has no active PDPs"); - - verify(dao, never()).createPdpGroups(any()); - verify(dao, never()).updatePdpGroups(any()); - verify(reqmap, never()).addRequest(any(PdpUpdate.class)); - } - - - protected void assertUpdate(List updates, String groupName, String pdpType, String pdpName) { - - PdpUpdate update = updates.remove(0); - - assertEquals(groupName, update.getPdpGroup()); - assertEquals(pdpType, update.getPdpSubgroup()); - assertEquals(pdpName, update.getName()); - assertTrue(update.getPolicies().contains(policy1)); - } - - private void assertNoGroupAction() throws Exception { - verify(dao, never()).createPdpGroups(any()); - verify(dao, never()).updatePdpGroups(any()); - verify(reqmap, never()).addRequest(any(), any()); - } - - private void assertGroupUpdate(PdpGroup group, PdpSubGroup subgrp) throws Exception { - verify(dao, never()).createPdpGroups(any()); - - assertEquals(0, getStateChangeRequests(1).size()); - - List pdpUpdates = getUpdateRequests(1); - assertEquals(1, pdpUpdates.size()); - - PdpUpdate pdpUpdate = pdpUpdates.get(0); - assertEquals("pdpA", pdpUpdate.getName()); - assertEquals(group.getName(), pdpUpdate.getPdpGroup()); - - assertEquals(subgrp.getPdpType(), pdpUpdate.getPdpSubgroup()); - - List pdpPolicies = - pdpUpdate.getPolicies().stream().map(ToscaPolicy::getIdentifier).collect(Collectors.toList()); - Collections.sort(pdpPolicies); - - assertEquals(subgrp.getPolicies().toString(), pdpPolicies.toString()); - - List updates = getGroupUpdates(); - assertEquals(Arrays.asList(group), updates); - } - - private void assertGroupUpdateOnly(PdpGroup group) throws Exception { - verify(dao, never()).createPdpGroups(any()); - verify(reqmap, never()).addRequest(any(), any()); - - List updates = getGroupUpdates(); - assertEquals(Arrays.asList(group), updates); - } - - private void assertDeploymentData(ArgumentCaptor captor, ToscaPolicyIdentifier policyId, - String expectedPdps) { - PolicyPdpNotificationData data = captor.getValue(); - assertEquals(policyId, data.getPolicyId()); - assertEquals(policy1.getTypeIdentifier(), data.getPolicyType()); - assertEquals(expectedPdps, new TreeSet<>(data.getPdps()).toString()); - } - - /** - * Loads a standard request. - * - * @return a standard request - */ - protected PdpDeployPolicies loadRequest() { - return loadRequest("request.json"); - } - - /** - * Loads a request from a JSON file. - * - * @param fileName name of the file from which to load - * @return the request that was loaded - */ - protected PdpDeployPolicies loadRequest(String fileName) { - return loadFile(fileName, PdpDeployPolicies.class); - } - - /** - * Loads an empty request. - * - * @return an empty request - */ - protected PdpDeployPolicies loadEmptyRequest() { - return loadRequest("emptyRequest.json"); - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPolicyUndeployerImpl.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPolicyUndeployerImpl.java deleted file mode 100644 index 5ccb7714..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPolicyUndeployerImpl.java +++ /dev/null @@ -1,200 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Arrays; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.pdp.concepts.Pdp; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpSubGroup; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; - -public class TestPolicyUndeployerImpl extends ProviderSuper { - private static final String MY_GROUP = "my-group"; - private static final String MY_SUBGROUP = "my-subgroup"; - private static final String MY_SUBGROUP0 = "my-subgroup-0"; - private static final String PDP1 = "my-pdp-a"; - - @Mock - private SessionData session; - - @Captor - private ArgumentCaptor> pdpCaptor; - - private ToscaPolicyIdentifier ident1; - private ToscaPolicyIdentifier ident2; - private ToscaPolicyIdentifier ident3; - private ToscaPolicyIdentifier ident4; - private PdpGroup group; - private PdpSubGroup subgroup; - private MyProvider prov; - - - @AfterClass - public static void tearDownAfterClass() { - Registry.newRegistry(); - } - - /** - * Configures mocks and objects. - * - * @throws Exception if an error occurs - */ - @Before - public void setUp() throws Exception { - - super.setUp(); - - ident1 = new ToscaPolicyIdentifier("ident-a", "2.3.1"); - ident2 = new ToscaPolicyIdentifier("ident-b", "2.3.2"); - ident3 = new ToscaPolicyIdentifier("ident-c", "2.3.3"); - ident4 = new ToscaPolicyIdentifier("ident-d", "2.3.4"); - - group = new PdpGroup(); - group.setName(MY_GROUP); - - subgroup = new PdpSubGroup(); - subgroup.setPdpType(MY_SUBGROUP); - - Pdp pdp1 = new Pdp(); - pdp1.setInstanceId(PDP1); - - subgroup.setPdpInstances(Arrays.asList(pdp1)); - - // this subgroup should never be touched - PdpSubGroup subgroup0 = new PdpSubGroup(); - subgroup0.setPdpType(MY_SUBGROUP0); - subgroup0.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4))); - subgroup.setPdpInstances(Arrays.asList(pdp1)); - - group.setPdpSubgroups(Arrays.asList(subgroup0, subgroup)); - - when(session.getGroup(MY_GROUP)).thenReturn(group); - when(session.getPolicy(any())).thenReturn(policy1); - - prov = new MyProvider(); - } - - @Test - public void testUndeployPolicies() throws PfModelException { - subgroup.setPolicies(new LinkedList<>(Arrays.asList(ident1, ident2, ident3, ident4))); - - prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2)); - - // group should have been updated - verify(session).update(group); - - // subgroup should only have remaining policies - assertEquals(Arrays.asList(ident3, ident4).toString(), subgroup.getPolicies().toString()); - - // should have generated PDP-UPDATE for the PDP - verify(session).addUpdate(any()); - } - - /** - * Tests undeployPolicies() when the policies do not exist in the subgroup. - */ - @Test - public void testUndeployPoliciesUnchanged() throws PfModelException { - List origlist = Arrays.asList(ident3, ident4); - subgroup.setPolicies(new LinkedList<>(origlist)); - - prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2)); - - // group NOT should have been updated - verify(session, never()).update(group); - - // subgroup's policies should be unchanged - assertEquals(origlist.toString(), subgroup.getPolicies().toString()); - - // should NOT have generated PDP-UPDATE for the PDP - verify(session, never()).addUpdate(any()); - } - - /** - * Tests undeployPolicies() when the group is not found. - */ - @Test - public void testUndeployPoliciesGroupNotFound() throws PfModelException { - // force exception to be thrown if the list is changed - subgroup.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4))); - - when(session.getGroup(any())).thenReturn(null); - - prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2)); - - // group should have been updated - verify(session, never()).update(group); - - // should have generated PDP-UPDATE for the PDP - verify(session, never()).addUpdate(any()); - } - - /** - * Tests undeployPolicies() when the subgroup is not found. - */ - @Test - public void testUndeployPoliciesSubGroupNotFound() throws PfModelException { - // force exception to be thrown if the list is changed - subgroup.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4))); - - subgroup.setPdpType(MY_SUBGROUP + "X"); - - prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2)); - - // group should have been updated - verify(session, never()).update(group); - - // should have generated PDP-UPDATE for the PDP - verify(session, never()).addUpdate(any()); - } - - @Test(expected = UnsupportedOperationException.class) - public void testMakeUpdater() { - prov.makeUpdater(null, null, null); - } - - - private class MyProvider extends PolicyUndeployerImpl { - - @Override - protected void process(T request, BiConsumerWithEx processor) throws PfModelException { - processor.accept(session, request); - } - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java deleted file mode 100644 index 9370a7bd..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java +++ /dev/null @@ -1,392 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Arrays; -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; -import org.onap.policy.models.pap.concepts.PdpDeployPolicies; -import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; -import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; -import org.powermock.reflect.Whitebox; - -public class TestProviderBase extends ProviderSuper { - private static final String EXPECTED_EXCEPTION = "expected exception"; - - private static final String POLICY1_NAME = "policyA"; - private static final String POLICY1_VERSION = "1.2.3"; - private static final String GROUP1_NAME = "groupA"; - private static final String GROUP2_NAME = "groupB"; - private static final String PDP1_TYPE = "pdpTypeA"; - private static final String PDP2_TYPE = "pdpTypeB"; - private static final String PDP3_TYPE = "pdpTypeC"; - private static final String PDP4_TYPE = "pdpTypeD"; - private static final String PDP1 = "pdpA"; - private static final String PDP2 = "pdpB"; - private static final String PDP3 = "pdpC"; - private static final String PDP4 = "pdpD"; - - private MyProvider prov; - - - @AfterClass - public static void tearDownAfterClass() { - Registry.newRegistry(); - } - - /** - * Configures mocks and objects. - * - * @throws Exception if an error occurs - */ - @Before - public void setUp() throws Exception { - - super.setUp(); - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); - - prov = new MyProvider(); - } - - @Test - public void testProviderBase() { - assertSame(lockit, Whitebox.getInternalState(prov, "updateLock")); - assertSame(reqmap, Whitebox.getInternalState(prov, "requestMap")); - assertSame(daofact, Whitebox.getInternalState(prov, "daoFactory")); - } - - @Test - public void testProcess() throws Exception { - prov.process(loadRequest(), this::handle); - - assertGroup(getGroupUpdates(), GROUP1_NAME); - - 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()); - } - - @Test - public void testProcess_CreateEx() throws Exception { - PfModelException ex = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); - when(daofact.create()).thenThrow(ex); - - assertThatThrownBy(() -> prov.process(loadEmptyRequest(), this::handle)).isSameAs(ex); - } - - @Test - public void testProcess_PfRtEx() throws Exception { - PfModelRuntimeException ex = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); - when(daofact.create()).thenThrow(ex); - - assertThatThrownBy(() -> prov.process(loadEmptyRequest(), this::handle)).isSameAs(ex); - } - - @Test - public void testProcess_RuntimeEx() throws Exception { - RuntimeException ex = new RuntimeException(EXPECTED_EXCEPTION); - when(daofact.create()).thenThrow(ex); - - assertThatThrownBy(() -> prov.process(loadEmptyRequest(), this::handle)).isInstanceOf(PfModelException.class) - .hasMessage("request failed").hasCause(ex); - } - - @Test - public void testProcessPolicy_NoGroups() throws Exception { - when(dao.getFilteredPdpGroups(any())).thenReturn(Collections.emptyList()); - - SessionData session = new SessionData(dao); - ToscaPolicyIdentifierOptVersion ident = new ToscaPolicyIdentifierOptVersion(POLICY1_NAME, POLICY1_VERSION); - assertThatThrownBy(() -> prov.processPolicy(session, ident)).isInstanceOf(PfModelException.class) - .hasMessage("policy not supported by any PDP group: policyA 1.2.3"); - - } - - @Test - public void testGetPolicy() throws Exception { - PfModelException exc = new PfModelException(Status.CONFLICT, EXPECTED_EXCEPTION); - when(dao.getFilteredPolicyList(any())).thenThrow(exc); - - assertThatThrownBy(() -> prov.process(loadRequest(), this::handle)).isInstanceOf(PfModelRuntimeException.class) - .hasCause(exc); - } - - @Test - public void testGetPolicy_NotFound() throws Exception { - when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); - - assertThatThrownBy(() -> prov.process(loadRequest(), this::handle)).isInstanceOf(PfModelRuntimeException.class) - .hasMessage("cannot find policy: policyA 1.2.3") - .extracting(ex -> ((PfModelRuntimeException) ex).getErrorResponse().getResponseCode()) - .isEqualTo(Status.NOT_FOUND); - } - - @Test - public void testGetGroup() throws Exception { - when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("getGroupDao.json")) - .thenReturn(loadGroups("groups.json")); - - prov.process(loadRequest(), this::handle); - - assertGroup(getGroupUpdates(), GROUP1_NAME); - } - - @Test - public void testUpgradeGroup() throws Exception { - /* - * Each subgroup has a different PDP type and name. - * - * Type is not supported by the first subgroup. - * - * Second subgroup matches. - * - * Third subgroup already contains the policy. - * - * Last subgroup matches. - */ - - when(dao.getFilteredPdpGroups(any())).thenReturn(loadGroups("upgradeGroupDao.json")); - - prov.clear(); - prov.add(false, true, false, true); - - prov.process(loadRequest(), this::handle); - - assertGroup(getGroupUpdates(), GROUP1_NAME); - - List requests = getUpdateRequests(2); - assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); - assertUpdate(requests, GROUP1_NAME, PDP4_TYPE, PDP4); - } - - @Test - public void testUpgradeGroup_Multiple() throws Exception { - /* - * Policy data in the DB: policy1=type1, policy2=type2, policy3=type3, - * policy4=type1 - * - * Group data in the DB: group1=(type1=pdp1, type3=pdp3) group2=(type2=pdp2) - * - * Request specifies: policy1, policy2, policy3, policy4 - * - * Should create new versions of group1 and group2. - * - * Should update old versions of group1 and group2. Should also update new version - * of group1 twice. - * - * Should generate updates to pdp1, pdp2, and pdp3. - */ - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")) - .thenReturn(loadPolicies("upgradeGroupPolicy2.json")) - .thenReturn(loadPolicies("upgradeGroupPolicy3.json")) - .thenReturn(loadPolicies("upgradeGroupPolicy4.json")); - - List groups1 = loadGroups("upgradeGroupGroup1.json"); - List groups2 = loadGroups("upgradeGroupGroup2.json"); - - /* - * these are in pairs of (get-group, get-max-group) matching each policy in the - * request - */ - // @formatter:off - when(dao.getFilteredPdpGroups(any())) - .thenReturn(groups1).thenReturn(groups1) - .thenReturn(groups2).thenReturn(groups2) - .thenReturn(groups1).thenReturn(groups1) - .thenReturn(groups1).thenReturn(groups1); - // @formatter:on - - // multiple policies in the request - PdpDeployPolicies request = loadFile("updateGroupReqMultiple.json", PdpDeployPolicies.class); - - prov.process(request, (data, deploy) -> { - for (ToscaPolicyIdentifierOptVersion policy : deploy.getPolicies()) { - handle(data, policy); - } - }); - - // verify updates - List changes = getGroupUpdates(); - assertGroup(changes, GROUP1_NAME); - assertGroup(changes, GROUP2_NAME); - - List requests = getUpdateRequests(3); - assertUpdateIgnorePolicy(requests, GROUP1_NAME, PDP1_TYPE, PDP1); - assertUpdateIgnorePolicy(requests, GROUP2_NAME, PDP2_TYPE, PDP2); - assertUpdateIgnorePolicy(requests, GROUP1_NAME, PDP3_TYPE, PDP3); - } - - @Test - public void testUpgradeGroup_NothingUpdated() throws Exception { - prov.clear(); - prov.add(false); - - prov.process(loadRequest(), this::handle); - - verify(dao, never()).createPdpGroups(any()); - verify(dao, never()).updatePdpGroups(any()); - verify(reqmap, never()).addRequest(any(PdpUpdate.class)); - } - - - protected void assertUpdate(List updates, String groupName, String pdpType, String pdpName) { - - PdpUpdate update = updates.remove(0); - - assertEquals(groupName, update.getPdpGroup()); - assertEquals(pdpType, update.getPdpSubgroup()); - assertEquals(pdpName, update.getName()); - assertTrue(update.getPolicies().contains(policy1)); - } - - /** - * Loads a standard request. - * - * @return a standard request - */ - protected ToscaPolicyIdentifierOptVersion loadRequest() { - return loadRequest("requestBase.json"); - } - - /** - * Loads a request from a JSON file. - * - * @param fileName name of the file from which to load - * @return the request that was loaded - */ - protected ToscaPolicyIdentifierOptVersion loadRequest(String fileName) { - return loadFile(fileName, ToscaPolicyIdentifierOptVersion.class); - } - - /** - * Loads an empty request. - * - * @return an empty request - */ - protected ToscaPolicyIdentifierOptVersion loadEmptyRequest() { - return loadRequest("emptyRequestBase.json"); - } - - /** - * Handles a request by invoking the provider's processPolicy method. - * - * @param data session data - * @param request request to be handled - * @throws PfModelException if an error occurred - */ - private void handle(SessionData data, ToscaPolicyIdentifierOptVersion request) throws PfModelException { - prov.processPolicy(data, request); - } - - - private static class MyProvider extends ProviderBase { - /** - * Used to determine whether or not to make an update when - * {@link #makeUpdater(ToscaPolicy)} is called. The updater function removes an - * item from this queue each time it is invoked. - */ - private final Queue shouldUpdate = new LinkedList<>(); - - /** - * Constructs the object and queues up several successful updates. - */ - public MyProvider() { - for (int x = 0; x < 10; ++x) { - shouldUpdate.add(true); - } - } - - public void clear() { - shouldUpdate.clear(); - } - - public void add(Boolean... update) { - shouldUpdate.addAll(Arrays.asList(update)); - } - - @Override - protected Updater makeUpdater(SessionData data, ToscaPolicy policy, - ToscaPolicyIdentifierOptVersion desiredPolicy) { - - return (group, subgroup) -> { - if (shouldUpdate.remove()) { - // queue indicated that the update should succeed - subgroup.getPolicies().add(policy.getIdentifier()); - - data.trackDeploy(policy.getIdentifier(), Collections.singleton(PDP1)); - data.trackUndeploy(policy.getIdentifier(), Collections.singleton(PDP2)); - - ToscaPolicyIdentifier ident2 = new ToscaPolicyIdentifier(POLICY1_NAME, "9.9.9"); - data.trackDeploy(ident2, Collections.singleton(PDP3)); - data.trackUndeploy(ident2, Collections.singleton(PDP4)); - return true; - - } else { - // queue indicated that no update should be made this time - return false; - } - }; - } - } -} diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java deleted file mode 100644 index 4d353a6e..00000000 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java +++ /dev/null @@ -1,662 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP PAP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.rest.depundep; - -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; -import static org.assertj.core.api.Assertions.assertThatIllegalStateException; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -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.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.concepts.PdpStateChange; -import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; -import org.onap.policy.pap.main.notification.PolicyPdpNotificationData; - -public class TestSessionData extends ProviderSuper { - private static final String GROUP_NAME = "groupA"; - 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 POLICY_VERSION_PREFIX = "1.2."; - private static final String POLICY_NAME = "myPolicy"; - private static final String POLICY_VERSION = POLICY_VERSION_PREFIX + "3"; - private static final String POLICY_TYPE = "myType"; - private static final String POLICY_TYPE_VERSION = "10.20.30"; - private static final String EXPECTED_EXCEPTION = "expected exception"; - - private SessionData session; - private ToscaPolicyIdentifierOptVersion ident; - private ToscaPolicyTypeIdentifier type; - private ToscaPolicyTypeIdentifier type2; - private PdpGroup group1; - private PdpGroup group2; - - /** - * Initializes mocks and a session. - * - * @throws Exception if an error occurs - */ - @Before - public void setUp() throws Exception { - super.setUp(); - - ident = new ToscaPolicyIdentifierOptVersion(POLICY_NAME, POLICY_VERSION); - type = new ToscaPolicyTypeIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION); - type2 = new ToscaPolicyTypeIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION + "0"); - group1 = loadGroup("group1.json"); - group2 = loadGroup("group2.json"); - - session = new SessionData(dao); - } - - @Test - public void testGetPolicyType() throws Exception { - ToscaPolicyType policy1 = makePolicyType(POLICY_TYPE, POLICY_TYPE_VERSION); - when(dao.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenReturn(Arrays.asList(policy1)); - - assertSame(policy1, session.getPolicyType(type)); - - // retrieve a second time - should use cache - assertSame(policy1, session.getPolicyType(type)); - } - - @Test - public void testGetPolicyType_NotFound() throws Exception { - when(dao.getPolicyTypeList(any(), any())).thenReturn(Collections.emptyList()); - - assertNull(session.getPolicyType(type)); - } - - @Test - public void testGetPolicyType_DaoEx() throws Exception { - PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION); - when(dao.getPolicyTypeList(POLICY_TYPE, POLICY_TYPE_VERSION)).thenThrow(ex); - - assertThatThrownBy(() -> session.getPolicyType(type)).isSameAs(ex); - } - - @Test - public void testGetPolicy_NullVersion() throws Exception { - ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); - when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); - - ident.setVersion(null); - assertSame(policy1, session.getPolicy(ident)); - - ToscaPolicyFilter filter = getPolicyFilter(); - assertEquals(POLICY_NAME, filter.getName()); - assertEquals(ToscaPolicyFilter.LATEST_VERSION, filter.getVersion()); - assertEquals(null, filter.getVersionPrefix()); - - // retrieve a second time using full version - should use cache - assertSame(policy1, session.getPolicy(new ToscaPolicyIdentifierOptVersion(policy1.getIdentifier()))); - verify(dao).getFilteredPolicyList(any()); - } - - @Test - public void testGetPolicy_MajorVersion() throws Exception { - ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); - when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); - - ident.setVersion("1"); - assertSame(policy1, session.getPolicy(ident)); - - ToscaPolicyFilter filter = getPolicyFilter(); - assertEquals(POLICY_NAME, filter.getName()); - assertEquals(ToscaPolicyFilter.LATEST_VERSION, filter.getVersion()); - assertEquals("1.", filter.getVersionPrefix()); - - // retrieve a second time using full version - should use cache - assertSame(policy1, session.getPolicy(new ToscaPolicyIdentifierOptVersion(policy1.getIdentifier()))); - verify(dao).getFilteredPolicyList(any()); - } - - @Test - public void testGetPolicy_MajorMinorVersion() throws Exception { - ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); - when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); - - ident.setVersion(POLICY_VERSION); - assertSame(policy1, session.getPolicy(ident)); - - ToscaPolicyFilter filter = getPolicyFilter(); - assertEquals(POLICY_NAME, filter.getName()); - assertEquals(POLICY_VERSION, filter.getVersion()); - assertEquals(null, filter.getVersionPrefix()); - - // retrieve a second time using full version - should use cache - assertSame(policy1, session.getPolicy(new ToscaPolicyIdentifierOptVersion(policy1.getIdentifier()))); - verify(dao).getFilteredPolicyList(any()); - } - - @Test - public void testGetPolicy_NotFound() throws Exception { - when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); - - assertNull(session.getPolicy(ident)); - } - - @Test - public void testGetPolicy_DaoEx() throws Exception { - PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, EXPECTED_EXCEPTION); - when(dao.getFilteredPolicyList(any())).thenThrow(ex); - - assertThatThrownBy(() -> session.getPolicy(ident)).isSameAs(ex); - } - - @Test - public void testIsVersionPrefix() { - assertTrue(SessionData.isVersionPrefix("1")); - assertTrue(SessionData.isVersionPrefix("12")); - assertTrue(SessionData.isVersionPrefix("1.2")); - assertTrue(SessionData.isVersionPrefix("1.23")); - - assertFalse(SessionData.isVersionPrefix("1.")); - assertFalse(SessionData.isVersionPrefix("1.2.")); - assertFalse(SessionData.isVersionPrefix("1.2.3")); - assertFalse(SessionData.isVersionPrefix("1.2.3.")); - assertFalse(SessionData.isVersionPrefix("1.2.3.4")); - } - - @Test - public void testAddRequests_testGetPdpStateChanges_testGetPdpUpdates() { - // pre-load with a update and state-change for other PDPs - PdpUpdate update2 = makeUpdate(PDP2); - session.addUpdate(update2); - - PdpStateChange change3 = makeStateChange(PDP3); - session.addStateChange(change3); - - // add requests - PdpUpdate update = makeUpdate(PDP1); - PdpStateChange change = makeStateChange(PDP1); - session.addRequests(update, change); - verifyRequests(update, update2, change, change3); - - /* - * repeat with a new pair - */ - update = makeUpdate(PDP1); - change = makeStateChange(PDP1); - session.addRequests(update, change); - verifyRequests(update, update2, change, change3); - - // just make an update this time - update = makeUpdate(PDP1); - session.addUpdate(update); - verifyRequests(update, update2, change, change3); - } - - private void verifyRequests(PdpUpdate update, PdpUpdate update2, PdpStateChange change, PdpStateChange change3) { - List> requests = sort(session.getPdpRequests(), this::compare); - assertEquals(3, requests.size()); - - System.out.println(requests); - System.out.println(update); - - Iterator> reqiter = requests.iterator(); - Pair pair = reqiter.next(); - assertSame(update, pair.getLeft()); - assertSame(change, pair.getRight()); - - pair = reqiter.next(); - assertSame(update2, pair.getLeft()); - assertSame(null, pair.getRight()); - - pair = reqiter.next(); - assertSame(null, pair.getLeft()); - assertSame(change3, pair.getRight()); - - // verify individual lists - List updates = Arrays.asList(update, update2); - assertEquals(sort(updates, this::compare), sort(session.getPdpUpdates(), this::compare)); - - List changes = Arrays.asList(change, change3); - assertEquals(sort(changes, this::compare), sort(session.getPdpStateChanges(), this::compare)); - } - - @Test - public void testAddRequests_MismatchedNames() { - PdpUpdate update = makeUpdate(PDP1); - PdpStateChange change = makeStateChange(PDP2); - assertThatIllegalArgumentException().isThrownBy(() -> session.addRequests(update, change)) - .withMessage("PDP name mismatch pdp_1, pdp_2"); - } - - @Test - public void testAddUpdate_testGetPdpUpdates() { - // several different updates, but one duplicate - PdpUpdate update1 = makeUpdate(PDP1); - session.addUpdate(update1); - - PdpUpdate update2 = makeUpdate(PDP2); - session.addUpdate(update2); - - PdpUpdate update3 = makeUpdate(PDP3); - session.addUpdate(update3); - - List lst = sort(getUpdateRequests(), this::compare); - assertEquals(Arrays.asList(update1, update2, update3).toString(), lst.toString()); - - // overwrite one - update2 = makeUpdate(PDP2); - session.addUpdate(update2); - - lst = sort(getUpdateRequests(), this::compare); - assertEquals(Arrays.asList(update1, update2, update3).toString(), lst.toString()); - } - - @Test - public void testAddStateChange_testGetPdpStateChanges() { - // several different changes, but one duplicate - PdpStateChange change1 = makeStateChange(PDP1); - session.addStateChange(change1); - - PdpStateChange change2 = makeStateChange(PDP2); - session.addStateChange(change2); - - PdpStateChange change3 = makeStateChange(PDP3); - session.addStateChange(change3); - - List lst = sort(getStateChangeRequests(), this::compare); - assertEquals(Arrays.asList(change1, change2, change3).toString(), lst.toString()); - - // overwrite one - change2 = makeStateChange(PDP2); - session.addStateChange(change2); - - lst = sort(getStateChangeRequests(), this::compare); - assertEquals(Arrays.asList(change1, change2, change3).toString(), lst.toString()); - } - - private ToscaPolicyType makePolicyType(String name, String version) { - ToscaPolicyType type = new ToscaPolicyType(); - - type.setName(name); - type.setVersion(version); - - return type; - } - - private ToscaPolicy makePolicy(String name, String version) { - ToscaPolicy policy = new ToscaPolicy(); - - policy.setName(name); - policy.setVersion(version); - - return policy; - } - - @Test - public void testCreate() throws Exception { - assertTrue(session.isUnchanged()); - - session.create(group1); - assertSame(group1, session.getGroup(group1.getName())); - assertFalse(session.isUnchanged()); - - // can add another - session.create(group2); - assertSame(group1, session.getGroup(group1.getName())); - assertSame(group2, session.getGroup(group2.getName())); - assertFalse(session.isUnchanged()); - - // cannot overwrite - assertThatIllegalStateException().isThrownBy(() -> session.create(group1)) - .withMessage("group already cached: groupA"); - } - - @Test - public void testUpdate() throws Exception { - assertTrue(session.isUnchanged()); - - // force the groups into the cache - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2)); - session.getActivePdpGroupsByPolicyType(type); - - /* - * try group 1 - */ - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); - PdpGroup newgrp = new PdpGroup(group1); - session.update(newgrp); - assertFalse(session.isUnchanged()); - - // repeat - newgrp = new PdpGroup(group1); - session.update(newgrp); - assertFalse(session.isUnchanged()); - - /* - * try group 2 - */ - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group2)); - newgrp = new PdpGroup(group2); - session.update(newgrp); - assertFalse(session.isUnchanged()); - - // repeat - newgrp = new PdpGroup(group2); - session.update(newgrp); - assertFalse(session.isUnchanged()); - } - - @Test - public void testUpdate_NotInCache() throws Exception { - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); - - assertThatIllegalStateException().isThrownBy(() -> session.update(new PdpGroup(group1))) - .withMessage("group not cached: groupA"); - } - - @Test - public void testGetGroup() throws Exception { - when(dao.getPdpGroups(GROUP_NAME)).thenReturn(Arrays.asList(group1)); - - assertSame(group1, session.getGroup(GROUP_NAME)); - verify(dao).getPdpGroups(any()); - - // repeat - assertSame(group1, session.getGroup(GROUP_NAME)); - - // should not access dao again - verify(dao, times(1)).getPdpGroups(any()); - } - - @Test - public void testGetGroup_NotFound() throws Exception { - when(dao.getPdpGroups(GROUP_NAME)).thenReturn(Collections.emptyList()); - - assertNull(session.getGroup(GROUP_NAME)); - verify(dao).getPdpGroups(any()); - - // repeat - assertNull(session.getGroup(GROUP_NAME)); - - // SHOULD access dao again - verify(dao, times(2)).getPdpGroups(GROUP_NAME); - - // find it this time - when(dao.getPdpGroups(GROUP_NAME)).thenReturn(Arrays.asList(group1)); - assertSame(group1, session.getGroup(GROUP_NAME)); - verify(dao, times(3)).getPdpGroups(GROUP_NAME); - } - - @Test - public void testGetGroup_DaoEx() throws Exception { - PfModelException ex = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION); - when(dao.getPdpGroups(GROUP_NAME)).thenThrow(ex); - - assertThatThrownBy(() -> session.getGroup(GROUP_NAME)).isSameAs(ex); - } - - @Test - public void testGetActivePdpGroupsByPolicyType() throws Exception { - List groups = Arrays.asList(group1, group2); - when(dao.getFilteredPdpGroups(any())).thenReturn(groups); - - // repeat - assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); - assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); - assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); - - // only invoked once - should have used the cache for the rest - verify(dao, times(1)).getFilteredPdpGroups(any()); - } - - @Test - public void testAddGroup() throws Exception { - List groups = Arrays.asList(group1, group2); - when(dao.getFilteredPdpGroups(any())).thenReturn(groups); - - // query by each type - assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); - assertEquals(groups, session.getActivePdpGroupsByPolicyType(type2)); - - // invoked once for each type - verify(dao, times(2)).getFilteredPdpGroups(any()); - - // repeat - should be no more invocations - assertEquals(groups, session.getActivePdpGroupsByPolicyType(type)); - assertEquals(groups, session.getActivePdpGroupsByPolicyType(type2)); - verify(dao, times(2)).getFilteredPdpGroups(any()); - } - - @Test - public void testUpdateDb() throws Exception { - // force the groups into the cache - PdpGroup group3 = loadGroup("group3.json"); - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2, group3)); - session.getActivePdpGroupsByPolicyType(type); - - // create groups 4 & 5 - PdpGroup group4 = loadGroup("group4.json"); - session.create(group4); - - PdpGroup group5 = loadGroup("group5.json"); - session.create(group5); - - // update group 1 - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); - PdpGroup newgrp1 = new PdpGroup(group1); - session.update(newgrp1); - - // another update - newgrp1 = new PdpGroup(newgrp1); - session.update(newgrp1); - - // update group 3 - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group3)); - PdpGroup newgrp3 = new PdpGroup(group3); - session.update(newgrp3); - - // update group 5 - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group5)); - PdpGroup newgrp5 = new PdpGroup(group5); - session.update(newgrp5); - - // push the changes to the DB - session.updateDb(); - - // expect one create for groups 4 & 5 (group5 replaced by newgrp5) - List creates = getGroupCreates(); - assertEquals(2, creates.size()); - assertSame(group4, creates.get(0)); - assertSame(newgrp5, creates.get(1)); - - // expect one update for groups 1 & 3 - List updates = getGroupUpdates(); - assertEquals(2, updates.size()); - assertSame(newgrp1, updates.get(0)); - assertSame(newgrp3, updates.get(1)); - } - - @Test - public void testUpdateDb_Empty() throws Exception { - // force data into the cache - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2)); - session.getActivePdpGroupsByPolicyType(type); - - session.updateDb(); - verify(dao, never()).createPdpGroups(any()); - verify(dao, never()).updatePdpGroups(any()); - } - - @Test - public void testDeleteGroupFromDb() throws Exception { - session.deleteGroupFromDb(group1); - - verify(dao).deletePdpGroup(group1.getName()); - } - - @Test - public void testTrackDeploy() throws PfModelException { - testTrack(session::getDeployData, session::getUndeployData, session::trackDeploy); - } - - /** - * Tests trackDeploy() when there is something in the undeployed list. - * - * @throws PfModelException if an error occurs - */ - @Test - public void testTrackDeployRemoveUndeploy() throws PfModelException { - testTrack(session::getDeployData, session::getUndeployData, session::trackUndeploy, session::trackDeploy); - } - - @Test - public void testTrackUndeploy() throws PfModelException { - testTrack(session::getUndeployData, session::getDeployData, session::trackUndeploy); - } - - /** - * Tests trackUndeploy() when there is something in the deployed list. - * - * @throws PfModelException if an error occurs - */ - @Test - public void testTrackUndeployRemoveUndeploy() throws PfModelException { - testTrack(session::getUndeployData, session::getDeployData, session::trackDeploy, session::trackUndeploy); - } - - protected void testTrack(Supplier> expected, - Supplier> unexpected, TrackEx... trackFuncs) - throws PfModelException { - - ToscaPolicy policy = makePolicy(POLICY_NAME, POLICY_VERSION); - policy.setType(POLICY_TYPE); - policy.setTypeVersion(POLICY_TYPE_VERSION); - - when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy)); - - ToscaPolicyIdentifier policyId = new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION); - List pdps = Arrays.asList(PDP1, PDP2); - - for (TrackEx trackFunc : trackFuncs) { - trackFunc.accept(policyId, pdps); - } - - // "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()); - - PolicyPdpNotificationData data = dataList.iterator().next(); - assertEquals(policyId, data.getPolicyId()); - assertEquals(type, data.getPolicyType()); - assertEquals("[pdp_1, pdp_2]", new TreeSet<>(data.getPdps()).toString()); - } - - private PdpUpdate makeUpdate(String pdpName) { - PdpUpdate update = new PdpUpdate(); - - update.setName(pdpName); - - return update; - } - - private PdpStateChange makeStateChange(String pdpName) { - PdpStateChange change = new PdpStateChange(); - - change.setName(pdpName); - - return change; - } - - private ToscaPolicyFilter getPolicyFilter() throws Exception { - ArgumentCaptor captor = ArgumentCaptor.forClass(ToscaPolicyFilter.class); - verify(dao).getFilteredPolicyList(captor.capture()); - - return captor.getValue(); - } - - private List getUpdateRequests() { - return session.getPdpUpdates(); - } - - private List getStateChangeRequests() { - return session.getPdpStateChanges(); - } - - private List sort(Collection collection, Comparator comparator) { - List lst = new ArrayList<>(collection); - Collections.sort(lst, comparator); - - return lst; - } - - private int compare(Pair left, Pair right) { - return getName(left).compareTo(getName(right)); - } - - private int compare(PdpUpdate left, PdpUpdate right) { - return left.getName().compareTo(right.getName()); - } - - private int compare(PdpStateChange left, PdpStateChange right) { - return left.getName().compareTo(right.getName()); - } - - private String getName(Pair pair) { - return (pair.getKey() != null ? pair.getKey().getName() : pair.getValue().getName()); - } - - @FunctionalInterface - private static interface TrackEx { - public void accept(ToscaPolicyIdentifier policyId, Collection pdps) throws PfModelException; - } -} -- cgit 1.2.3-korg