From 2a76364ed54301623718756d6088fc675516f9d0 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 10 Apr 2019 13:58:05 -0400 Subject: Don't change group version number Modified code to update a PdpGroup, adding or removing policies, without changing the version number. Change-Id: I10031dff5f6d9c7e568605a8d73af6fe3c740901 Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn --- .../policy/pap/main/rest/depundep/GroupData.java | 60 +++-------- .../pap/main/rest/depundep/ProviderBase.java | 59 ++--------- .../policy/pap/main/rest/depundep/SessionData.java | 55 ++-------- .../pap/main/rest/depundep/ProviderSuper.java | 21 +--- .../pap/main/rest/depundep/TestGroupData.java | 49 +++------ .../rest/depundep/TestPdpGroupDeleteProvider.java | 12 +-- .../rest/depundep/TestPdpGroupDeployProvider.java | 6 +- .../pap/main/rest/depundep/TestProviderBase.java | 22 ++-- .../pap/main/rest/depundep/TestSessionData.java | 116 ++++----------------- .../test/resources/simpleDeploy/getGroupDao.json | 61 +---------- 10 files changed, 71 insertions(+), 390 deletions(-) diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/GroupData.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/GroupData.java index 9345f341..25a886bd 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/GroupData.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/GroupData.java @@ -21,10 +21,7 @@ package org.onap.policy.pap.main.rest.depundep; import lombok.Getter; -import lombok.Setter; -import org.onap.policy.common.utils.validation.Version; import org.onap.policy.models.pdp.concepts.PdpGroup; -import org.onap.policy.models.pdp.enums.PdpState; /** * PdpGroup data, which includes the old group, that's in the DB, and possibly a new @@ -32,19 +29,12 @@ import org.onap.policy.models.pdp.enums.PdpState; */ @Getter public class GroupData { - private final PdpGroup oldGroup; + private PdpGroup group; /** - * Starts out pointing to {@link #oldGroup}, but then changed to point to the new - * group, if {@link #setNewGroup(PdpGroup)} is invoked. + * {@code True} if the group has been updated, {@code false} otherwise. */ - private PdpGroup currentGroup; - - /** - * Latest version of this group. - */ - @Setter - private Version latestVersion; + private boolean updated = false; /** @@ -53,47 +43,21 @@ public class GroupData { * @param group the group that is in the DB */ public GroupData(PdpGroup group) { - this.oldGroup = group; - this.currentGroup = group; + this.group = group; } /** - * Determines if a new version of this group has been created (i.e., - * {@link #setNewGroup(PdpGroup)} has been invoked. + * Updates the group to the new value. * - * @return {@code true} if a new version of the group has been created, {@code false} - * otherwise + * @param newGroup the updated group */ - public boolean isNew() { - return (currentGroup != oldGroup); - } - - /** - * Updates to a new group. - * - * @param newGroup the new group - * @throws IllegalArgumentException if the new group has a different name than the old - * group - * @throws IllegalStateException if {@link #setLatestVersion(Version)} has not been - * invoked yet - */ - public void setNewGroup(PdpGroup newGroup) { - if (!currentGroup.getName().equals(newGroup.getName())) { - throw new IllegalArgumentException("attempt to change group name from " + currentGroup.getName() + " to " - + newGroup.getName()); - } - - if (currentGroup == oldGroup) { - // first time to create a new group - bump the version - if (latestVersion == null) { - throw new IllegalStateException("latestVersion not set for group: " + oldGroup.getName()); - } - - latestVersion = latestVersion.newVersion(); - oldGroup.setPdpGroupState(PdpState.PASSIVE); + public void update(PdpGroup newGroup) { + if (!this.group.getName().equals(newGroup.getName())) { + throw new IllegalArgumentException( + "expected group " + this.group.getName() + ", but received " + newGroup.getName()); } - currentGroup = newGroup; - currentGroup.setVersion(latestVersion.toString()); + this.updated = true; + this.group = newGroup; } } diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java index b220bf80..d3519197 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/ProviderBase.java @@ -22,15 +22,12 @@ package org.onap.policy.pap.main.rest.depundep; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.Map; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.stream.Collectors; import javax.ws.rs.core.Response; import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.common.utils.validation.Version; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pap.concepts.SimpleResponse; import org.onap.policy.models.pdp.concepts.Pdp; @@ -203,8 +200,7 @@ public abstract class ProviderBase { protected abstract BiFunction makeUpdater(ToscaPolicy policy); /** - * Finds the active PDP group(s) with the highest version that supports the given - * policy type. + * Finds the active PDP group(s) that supports the given policy type. * * @param data session data * @param policyType the policy type of interest @@ -214,28 +210,8 @@ public abstract class ProviderBase { */ private Collection getGroups(SessionData data, ToscaPolicyTypeIdentifier policyType) throws PfModelException { - // build a map containing the group with the highest version for each name - Map name2data = new HashMap<>(); - for (PdpGroup group : data.getActivePdpGroupsByPolicyType(policyType)) { - Version vers = Version.makeVersion("PdpGroup", group.getName(), group.getVersion()); - if (vers == null) { - continue; - } - - GroupVersion grpdata = name2data.get(group.getName()); - - if (grpdata == null) { - // not in the map yet - name2data.put(group.getName(), new GroupVersion(group, vers)); - - } else if (vers.compareTo(grpdata.version) >= 0) { - // higher version - grpdata.replace(group, vers); - } - } - - return name2data.values().stream().map(grpdata -> grpdata.group).collect(Collectors.toList()); + return data.getActivePdpGroupsByPolicyType(policyType); } /** @@ -243,19 +219,18 @@ public abstract class ProviderBase { * * @param data session data * @param policy policy to be added to or removed from the group - * @param oldGroup the original group, to be updated + * @param group the original group, to be updated * @param updater function to update a group * @throws PfModelException if a DAO error occurred */ - private void upgradeGroup(SessionData data, ToscaPolicy policy, PdpGroup oldGroup, + private void upgradeGroup(SessionData data, ToscaPolicy policy, PdpGroup group, BiFunction updater) throws PfModelException { - PdpGroup newGroup = new PdpGroup(oldGroup); boolean updated = false; - for (PdpSubGroup subgroup : newGroup.getPdpSubgroups()) { + for (PdpSubGroup subgroup : group.getPdpSubgroups()) { - if (!updater.apply(newGroup, subgroup)) { + if (!updater.apply(group, subgroup)) { continue; } @@ -266,14 +241,14 @@ public abstract class ProviderBase { * assume that the PDP is, too, thus no need for a STATE-CHANGE. */ for (Pdp pdpInstance : subgroup.getPdpInstances()) { - data.addUpdate(makeUpdate(data, newGroup, subgroup, pdpInstance)); + data.addUpdate(makeUpdate(data, group, subgroup, pdpInstance)); } } if (updated) { // something changed - data.setNewGroup(newGroup); + data.update(group); } } @@ -298,22 +273,4 @@ public abstract class ProviderBase { return update; } - - /** - * Data associated with a group. Used to find the maximum version for a given group. - */ - private static class GroupVersion { - private PdpGroup group; - private Version version; - - public GroupVersion(PdpGroup group, Version version) { - this.group = group; - this.version = version; - } - - public void replace(PdpGroup group, Version version) { - this.group = group; - this.version = version; - } - } } diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java index 7da8a0c8..6b42b19b 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java @@ -26,7 +26,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.onap.policy.common.utils.validation.Version; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -136,17 +135,6 @@ public class SessionData { return pdpUpdates.values(); } - /** - * Determines if a group has been newly created as part of this REST call. - * - * @param group name to the group of interest - * @return {@code true} if the group has been newly created, {@code false} otherwise - */ - public boolean isNewlyCreated(String group) { - GroupData data = groupCache.get(group); - return (data != null && data.isNew()); - } - /** * Gets the policy having the given name and the maximum version. * @@ -175,42 +163,18 @@ public class SessionData { } /** - * Adds a new version of a group to the cache. + * Updates a group. * - * @param newGroup the new group to be added - * @throws IllegalStateException if the old group has not been loaded into the cache - * yet - * @throws PfModelException if an error occurs + * @param newGroup the updated group */ - public void setNewGroup(PdpGroup newGroup) throws PfModelException { + public void update(PdpGroup newGroup) { String name = newGroup.getName(); GroupData data = groupCache.get(name); if (data == null) { throw new IllegalStateException("group not cached: " + name); } - if (data.getLatestVersion() != null) { - // already have the latest version - data.setNewGroup(newGroup); - return; - } - - // must determine the latest version of this group, regardless of its state - PdpGroupFilter filter = PdpGroupFilter.builder().name(name).version(PdpGroupFilter.LATEST_VERSION).build(); - List groups = dao.getFilteredPdpGroups(filter); - if (groups.isEmpty()) { - throw new PolicyPapRuntimeException("cannot find group: " + name); - } - - PdpGroup group = groups.get(0); - Version vers = Version.makeVersion("PdpGroup", group.getName(), group.getVersion()); - if (vers == null) { - // none of the versions are numeric - start with zero and increment from there - vers = new Version(0, 0, 0); - } - - data.setLatestVersion(vers); - data.setNewGroup(newGroup); + data.update(newGroup); } /** @@ -223,11 +187,11 @@ public class SessionData { public List getActivePdpGroupsByPolicyType(ToscaPolicyTypeIdentifier type) throws PfModelException { List data = type2groups.get(type); if (data != null) { - return data.stream().map(GroupData::getCurrentGroup).collect(Collectors.toList()); + return data.stream().map(GroupData::getGroup).collect(Collectors.toList()); } PdpGroupFilter filter = PdpGroupFilter.builder().policyTypeList(Collections.singletonList(type)) - .groupState(PdpState.ACTIVE).build(); + .groupState(PdpState.ACTIVE).build(); List groups = dao.getFilteredPdpGroups(filter); @@ -262,13 +226,12 @@ public class SessionData { */ public void updateDb() throws PfModelException { List updatedGroups = - groupCache.values().stream().filter(GroupData::isNew).collect(Collectors.toList()); + groupCache.values().stream().filter(GroupData::isUpdated).collect(Collectors.toList()); if (updatedGroups.isEmpty()) { return; } - // create new groups BEFORE we deactivate the old groups - dao.createPdpGroups(updatedGroups.stream().map(GroupData::getCurrentGroup).collect(Collectors.toList())); - dao.updatePdpGroups(updatedGroups.stream().map(GroupData::getOldGroup).collect(Collectors.toList())); + // update the groups + dao.updatePdpGroups(updatedGroups.stream().map(GroupData::getGroup).collect(Collectors.toList())); } } diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java index 4cc69176..65b1234f 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/ProviderSuper.java @@ -59,12 +59,6 @@ public class ProviderSuper { @Mock protected PolicyModelsProvider dao; - /** - * Used to capture input to dao.createPdpGroups(). - */ - @Captor - private ArgumentCaptor> createCaptor; - /** * Used to capture input to dao.updatePdpGroups(). @@ -108,11 +102,10 @@ public class ProviderSuper { Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact); } - protected void assertGroup(List groups, String name, String version) { + protected void assertGroup(List groups, String name) { PdpGroup group = groups.remove(0); assertEquals(name, group.getName()); - assertEquals(version, group.getVersion()); } protected void assertUpdateIgnorePolicy(List updates, String groupName, String pdpType, String pdpName) { @@ -124,18 +117,6 @@ public class ProviderSuper { assertEquals(pdpName, update.getName()); } - /** - * Gets the input to the method. - * - * @return the input that was passed to the dao.createPdpGroups() method - * @throws Exception if an error occurred - */ - protected List getGroupCreates() throws Exception { - verify(dao).createPdpGroups(createCaptor.capture()); - - return copyList(createCaptor.getValue()); - } - /** * Gets the input to the method. * 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 index a0d4989f..8313d193 100644 --- 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 @@ -21,25 +21,20 @@ 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.junit.Assert.assertEquals; 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.common.utils.validation.Version; import org.onap.policy.models.pdp.concepts.PdpGroup; public class TestGroupData { - private static final String NEW_VERSION = "2.0.0"; private static final String NAME = "my-name"; private PdpGroup oldGroup; private PdpGroup newGroup; private GroupData data; - private Version version; /** * Sets up. @@ -51,47 +46,29 @@ public class TestGroupData { newGroup = new PdpGroup(oldGroup); - version = new Version(1, 2, 3); - data = new GroupData(oldGroup); } @Test public void test() { - assertFalse(data.isNew()); - assertSame(oldGroup, data.getOldGroup()); - assertSame(oldGroup, data.getCurrentGroup()); + assertFalse(data.isUpdated()); + assertSame(oldGroup, data.getGroup()); - data.setLatestVersion(version); - data.setNewGroup(newGroup); + data.update(newGroup); - assertTrue(data.isNew()); - assertSame(oldGroup, data.getOldGroup()); - assertSame(newGroup, data.getCurrentGroup()); - assertEquals(NEW_VERSION, data.getLatestVersion().toString()); - assertEquals(NEW_VERSION, newGroup.getVersion()); + assertTrue(data.isUpdated()); + assertSame(newGroup, data.getGroup()); // repeat newGroup = new PdpGroup(oldGroup); - data.setNewGroup(newGroup); - assertSame(oldGroup, data.getOldGroup()); - assertSame(newGroup, data.getCurrentGroup()); - assertEquals(NEW_VERSION, data.getLatestVersion().toString()); - assertEquals(NEW_VERSION, newGroup.getVersion()); - } - - @Test - public void testSetNewGroup_DifferentName() { - newGroup.setName("different-name"); + data.update(newGroup); + assertTrue(data.isUpdated()); + assertSame(newGroup, data.getGroup()); - data.setLatestVersion(version); - assertThatIllegalArgumentException().isThrownBy(() -> data.setNewGroup(newGroup)) - .withMessage("attempt to change group name from my-name to different-name"); - } - - @Test - public void testSetNewGroup_VersionNotSet() { - assertThatIllegalStateException().isThrownBy(() -> data.setNewGroup(newGroup)) - .withMessage("latestVersion not set for group: my-name"); + // 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/TestPdpGroupDeleteProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java index 31dfb398..dbb7951e 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeleteProvider.java @@ -128,18 +128,10 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { List updates = getGroupUpdates(); assertEquals(1, updates.size()); assertSame(group, updates.get(0)); - assertEquals(PdpState.PASSIVE, group.getPdpGroupState()); - - // should have created a new group - List creates = getGroupCreates(); - assertEquals(1, creates.size()); - PdpGroup group2 = creates.get(0); - assertEquals(group.getName(), group2.getName()); - assertEquals(PdpState.ACTIVE, group2.getPdpGroupState()); + assertEquals(PdpState.ACTIVE, group.getPdpGroupState()); // should be one less item in the new group - assertEquals(group.getPdpSubgroups().get(0).getPolicies().size() - 1, - group2.getPdpSubgroups().get(0).getPolicies().size()); + assertEquals(2, group.getPdpSubgroups().get(0).getPolicies().size()); // should have updated the PDPs List requests = getUpdateRequests(1); diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java index 018f117c..8ca205cd 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestPdpGroupDeployProvider.java @@ -41,7 +41,6 @@ import org.onap.policy.models.pap.concepts.PdpDeployPolicies; import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.pdp.concepts.PdpUpdate; -import org.onap.policy.pap.main.rest.depundep.PdpGroupDeployProvider; public class TestPdpGroupDeployProvider extends ProviderSuper { private static final String EXPECTED_EXCEPTION = "expected exception"; @@ -50,8 +49,6 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { 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 GROUP1_VERSION = "200.2.3"; - private static final String GROUP1_NEW_VERSION = "201.0.0"; private static final String PDP1_TYPE = "pdpTypeA"; private static final String PDP2_TYPE = "pdpTypeB"; private static final String PDP4_TYPE = "pdpTypeD"; @@ -149,8 +146,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { assertEquals(Status.OK, pair.getLeft()); assertNull(pair.getRight().getErrorDetails()); - assertGroup(getGroupUpdates(), GROUP1_NAME, GROUP1_VERSION); - assertGroup(getGroupCreates(), GROUP1_NAME, GROUP1_NEW_VERSION); + assertGroup(getGroupUpdates(), GROUP1_NAME); List requests = getUpdateRequests(2); assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java index 883b3d55..01b29b10 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestProviderBase.java @@ -61,8 +61,6 @@ public class TestProviderBase extends ProviderSuper { 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 GROUP1_VERSION = "200.2.3"; - private static final String GROUP1_NEW_VERSION = "201.0.0"; private static final String GROUP2_NAME = "groupB"; private static final String PDP1_TYPE = "pdpTypeA"; private static final String PDP2_TYPE = "pdpTypeB"; @@ -109,8 +107,7 @@ public class TestProviderBase extends ProviderSuper { assertEquals(Status.OK, pair.getLeft()); assertNull(pair.getRight().getErrorDetails()); - assertGroup(getGroupUpdates(), GROUP1_NAME, GROUP1_VERSION); - assertGroup(getGroupCreates(), GROUP1_NAME, GROUP1_NEW_VERSION); + assertGroup(getGroupUpdates(), GROUP1_NAME); assertUpdate(getUpdateRequests(1), GROUP1_NAME, PDP1_TYPE, PDP1); } @@ -197,8 +194,7 @@ public class TestProviderBase extends ProviderSuper { assertEquals(Status.OK, pair.getLeft()); assertNull(pair.getRight().getErrorDetails()); - assertGroup(getGroupUpdates(), GROUP1_NAME, GROUP1_VERSION); - assertGroup(getGroupCreates(), GROUP1_NAME, GROUP1_NEW_VERSION); + assertGroup(getGroupUpdates(), GROUP1_NAME); } @Test @@ -224,8 +220,7 @@ public class TestProviderBase extends ProviderSuper { assertEquals(Status.OK, pair.getLeft()); assertNull(pair.getRight().getErrorDetails()); - assertGroup(getGroupUpdates(), GROUP1_NAME, GROUP1_VERSION); - assertGroup(getGroupCreates(), GROUP1_NAME, GROUP1_NEW_VERSION); + assertGroup(getGroupUpdates(), GROUP1_NAME); List requests = getUpdateRequests(2); assertUpdate(requests, GROUP1_NAME, PDP2_TYPE, PDP2); @@ -282,15 +277,10 @@ public class TestProviderBase extends ProviderSuper { assertEquals(Status.OK, pair.getLeft()); assertNull(pair.getRight().getErrorDetails()); - // verify creates - List changes = getGroupCreates(); - assertGroup(changes, GROUP1_NAME, GROUP1_NEW_VERSION); - assertGroup(changes, GROUP2_NAME, "301.0.0"); - // verify updates - changes = getGroupUpdates(); - assertGroup(changes, GROUP1_NAME, GROUP1_VERSION); - assertGroup(changes, GROUP2_NAME, "300.2.3"); + List changes = getGroupUpdates(); + assertGroup(changes, GROUP1_NAME); + assertGroup(changes, GROUP2_NAME); List requests = getUpdateRequests(3); assertUpdateIgnorePolicy(requests, GROUP1_NAME, PDP1_TYPE, PDP1); diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java index 1c2b8233..fd351c89 100644 --- a/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java +++ b/main/src/test/java/org/onap/policy/pap/main/rest/depundep/TestSessionData.java @@ -23,9 +23,7 @@ package org.onap.policy.pap.main.rest.depundep; 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.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; @@ -47,15 +45,10 @@ 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.ToscaPolicyTypeIdentifier; -import org.onap.policy.pap.main.PolicyPapRuntimeException; public class TestSessionData extends ProviderSuper { - private static final String GROUP_VERSION_PREFIX = "9.8."; private static final String GROUP_NAME = "groupA"; - private static final String GROUP_VERSION = GROUP_VERSION_PREFIX + "7"; - private static final String GROUP_NEW_VERSION = "10.0.0"; private static final String GROUP_NAME2 = "groupB"; - private static final String GROUP_VERSION2 = GROUP_VERSION_PREFIX + "6"; private static final String PDP1 = "pdp_1"; private static final String PDP2 = "pdp_2"; private static final String PDP3 = "pdp_3"; @@ -85,8 +78,8 @@ public class TestSessionData extends ProviderSuper { ident = new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION); type = new ToscaPolicyTypeIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION); type2 = new ToscaPolicyTypeIdentifier(POLICY_TYPE, POLICY_TYPE_VERSION + "0"); - group1 = makeGroup(GROUP_NAME, GROUP_VERSION); - group2 = makeGroup(GROUP_NAME2, GROUP_VERSION2); + group1 = makeGroup(GROUP_NAME); + group2 = makeGroup(GROUP_NAME2); session = new SessionData(dao); } @@ -190,51 +183,16 @@ public class TestSessionData extends ProviderSuper { assertThatThrownBy(() -> session.getPolicyMaxVersion(POLICY_NAME)).hasMessage("cannot find policy: myPolicy"); } - @Test - public void testIsNewlyCreated_testCreatePdpGroup() throws Exception { - assertFalse(session.isNewlyCreated(GROUP_NAME)); - - // cause the group to be loaded into the cache - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); - session.getActivePdpGroupsByPolicyType(type); - - // not new yet - assertFalse(session.isNewlyCreated(GROUP_NAME)); - - // update it - session.setNewGroup(new PdpGroup(group1)); - assertTrue(session.isNewlyCreated(GROUP_NAME)); - - /* - * now try group2 - */ - assertFalse(session.isNewlyCreated(GROUP_NAME2)); - - // cause the group to be loaded into the cache - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group2)); - session.getActivePdpGroupsByPolicyType(type2); - - // not new yet - assertFalse(session.isNewlyCreated(GROUP_NAME2)); - assertTrue(session.isNewlyCreated(GROUP_NAME)); - - // update it - session.setNewGroup(new PdpGroup(group2)); - assertTrue(session.isNewlyCreated(GROUP_NAME2)); - assertTrue(session.isNewlyCreated(GROUP_NAME)); - } - - private PdpGroup makeGroup(String name, String version) { + private PdpGroup makeGroup(String name) { PdpGroup group = new PdpGroup(); group.setName(name); - group.setVersion(version); return group; } @Test - public void testSetNewGroup() throws Exception { + public void testUpdate() throws Exception { // force the groups into the cache when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2)); session.getActivePdpGroupsByPolicyType(type); @@ -244,65 +202,32 @@ public class TestSessionData extends ProviderSuper { */ when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); PdpGroup newgrp = new PdpGroup(group1); - session.setNewGroup(newgrp); - assertEquals(GROUP_NEW_VERSION, newgrp.getVersion().toString()); + session.update(newgrp); - // repeat - version should be unchanged + // repeat newgrp = new PdpGroup(group1); - session.setNewGroup(newgrp); - assertEquals(GROUP_NEW_VERSION, newgrp.getVersion().toString()); + session.update(newgrp); /* * try group 2 */ when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group2)); newgrp = new PdpGroup(group2); - session.setNewGroup(newgrp); - assertEquals(GROUP_NEW_VERSION, newgrp.getVersion().toString()); + session.update(newgrp); - // repeat - version should be unchanged + // repeat newgrp = new PdpGroup(group2); - session.setNewGroup(newgrp); - assertEquals(GROUP_NEW_VERSION, newgrp.getVersion().toString()); - - // should have queried the DB once by type and twice by for latest version - verify(dao, times(3)).getFilteredPdpGroups(any()); + session.update(newgrp); } @Test - public void testSetNewGroup_NotInCache() throws Exception { + public void testUpdate_NotInCache() throws Exception { when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); - assertThatIllegalStateException().isThrownBy(() -> session.setNewGroup(new PdpGroup(group1))) + assertThatIllegalStateException().isThrownBy(() -> session.update(new PdpGroup(group1))) .withMessage("group not cached: groupA"); } - @Test - public void testSetNewGroup_NotFound() throws Exception { - // force the group into the cache - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); - session.getActivePdpGroupsByPolicyType(type); - - // query for latest version will return an empty list - when(dao.getFilteredPdpGroups(any())).thenReturn(Collections.emptyList()); - - assertThatThrownBy(() -> session.setNewGroup(new PdpGroup(group1))) - .isInstanceOf(PolicyPapRuntimeException.class).hasMessage("cannot find group: groupA"); - } - - @Test - public void testSetNewGroup_InvalidVersion() throws Exception { - // force the groups into the cache - group1.setVersion("invalid version"); - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); - session.getActivePdpGroupsByPolicyType(type); - - when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); - PdpGroup newgrp = new PdpGroup(group1); - session.setNewGroup(newgrp); - assertEquals("1.0.0", newgrp.getVersion().toString()); - } - @Test public void testGetActivePdpGroupsByPolicyType() throws Exception { List groups = Arrays.asList(group1, group2); @@ -338,36 +263,31 @@ public class TestSessionData extends ProviderSuper { @Test public void testUpdateDb() throws Exception { // force the groups into the cache - PdpGroup group3 = makeGroup("groupC", GROUP_VERSION2); + PdpGroup group3 = makeGroup("groupC"); when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1, group2, group3)); session.getActivePdpGroupsByPolicyType(type); // update group 1 when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group1)); PdpGroup newgrp1 = new PdpGroup(group1); - session.setNewGroup(newgrp1); + session.update(newgrp1); // another update newgrp1 = new PdpGroup(newgrp1); - session.setNewGroup(newgrp1); + session.update(newgrp1); // update group 3 when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group3)); PdpGroup newgrp3 = new PdpGroup(group3); - session.setNewGroup(newgrp3); + session.update(newgrp3); // push the changes to the DB session.updateDb(); - // expect one create for groups 1 & 3 - List changes = getGroupCreates(); + // expect one update for groups 1 & 3 + List changes = getGroupUpdates(); assertSame(newgrp1, changes.get(0)); assertSame(newgrp3, changes.get(1)); - - // expect one update for groups 1 & 3 - changes = getGroupUpdates(); - assertSame(group1, changes.get(0)); - assertSame(group3, changes.get(1)); } @Test diff --git a/main/src/test/resources/simpleDeploy/getGroupDao.json b/main/src/test/resources/simpleDeploy/getGroupDao.json index 65d0907a..7fd7684b 100644 --- a/main/src/test/resources/simpleDeploy/getGroupDao.json +++ b/main/src/test/resources/simpleDeploy/getGroupDao.json @@ -16,67 +16,8 @@ { "instanceId": "pdpA" } - ] - } - ] - }, - { - "name": "groupA", - "version": "200.2.3", - "pdpSubgroups": [ - { - "pdpType": "pdpTypeA", - "supportedPolicyTypes": [ - { - "name": "typeA", - "version": "100.2.3" - } ], - "pdpInstances": [ - { - "instanceId": "pdpA" - } - ] - } - ] - }, - { - "name": "groupA", - "version": "0.0.1", - "pdpSubgroups": [ - { - "pdpType": "pdpTypeA", - "supportedPolicyTypes": [ - { - "name": "typeA", - "version": "100.2.3" - } - ], - "pdpInstances": [ - { - "instanceId": "pdpA" - } - ] - } - ] - }, - { - "name": "groupA", - "version": "non-numeric-version", - "pdpSubgroups": [ - { - "pdpType": "pdpTypeA", - "supportedPolicyTypes": [ - { - "name": "typeA", - "version": "100.2.3" - } - ], - "pdpInstances": [ - { - "instanceId": "pdpA" - } - ] + "policies": [] } ] } -- cgit 1.2.3-korg