From 7d8509acbf1a6ce1c0dc8bbd87e0a793420cafd4 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 15 Apr 2019 10:19:26 -0400 Subject: Support integer policy-version The deploy/undeploy APIs used by CLAMP only pass the major number when specifying a policy-version. Modified the code to handle policy-versions of the form, major or major.minor. Change-Id: I3251df162984f287bd1430b8e46da675b4c265ee Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn --- .../main/rest/depundep/PdpGroupDeployProvider.java | 2 +- .../pap/main/rest/depundep/ProviderBase.java | 11 +-- .../policy/pap/main/rest/depundep/SessionData.java | 85 ++++++++++--------- .../depundep/TestPdpGroupDeleteControllerV1.java | 4 +- .../rest/depundep/TestPdpGroupDeleteProvider.java | 6 +- .../rest/depundep/TestPdpGroupDeployProvider.java | 11 +-- .../pap/main/rest/depundep/TestProviderBase.java | 36 ++------- .../pap/main/rest/depundep/TestSessionData.java | 94 +++++++++++++--------- .../simpleDeploy/getPolicyReqNullVersion.json | 7 -- 9 files changed, 116 insertions(+), 140 deletions(-) delete mode 100644 main/src/test/resources/simpleDeploy/getPolicyReqNullVersion.json (limited to 'main/src') diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java index cb2d1e34..3d44a0cd 100644 --- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java +++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeployProvider.java @@ -351,7 +351,7 @@ public class PdpGroupDeployProvider extends ProviderBase BeanValidationResult result = new BeanValidationResult(subgrp.getPdpType(), subgrp); for (ToscaPolicyIdentifier ident : subgrp.getPolicies()) { - ToscaPolicy policy = data.getPolicy(ident); + ToscaPolicy policy = data.getPolicy(new ToscaPolicyIdentifierOptVersion(ident)); if (!subgrp.getSupportedPolicyTypes().contains(policy.getTypeIdentifier())) { result.addResult(new ObjectValidationResult("policy", ident, ValidationStatus.INVALID, 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 5e0fb71a..b7575df3 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 @@ -37,7 +37,6 @@ import org.onap.policy.models.pdp.concepts.PdpSubGroup; 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.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.pap.main.PapConstants; @@ -155,12 +154,7 @@ public abstract class ProviderBase { private ToscaPolicy getPolicy(SessionData data, ToscaPolicyIdentifierOptVersion desiredPolicy) throws PfModelException { - if (desiredPolicy.isNullVersion()) { - return data.getPolicyMaxVersion(desiredPolicy.getName()); - - } else { - return data.getPolicy(new ToscaPolicyIdentifier(desiredPolicy.getName(), desiredPolicy.getVersion())); - } + return data.getPolicy(desiredPolicy); } /** @@ -276,7 +270,8 @@ public abstract class ProviderBase { update.setDescription(group.getDescription()); update.setPdpGroup(group.getName()); update.setPdpSubgroup(subgroup.getPdpType()); - update.setPolicies(subgroup.getPolicies().stream().map(data::getPolicy).collect(Collectors.toList())); + update.setPolicies(subgroup.getPolicies().stream().map(ToscaPolicyIdentifierOptVersion::new) + .map(data::getPolicy).collect(Collectors.toList())); return update; } 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 0537c80b..b7aff765 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 @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.lang3.tuple.Pair; import org.onap.policy.models.base.PfModelException; @@ -36,7 +37,8 @@ import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.provider.PolicyModelsProvider; 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.ToscaPolicyFilter.ToscaPolicyFilterBuilder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.pap.main.PolicyPapRuntimeException; @@ -44,6 +46,14 @@ import org.onap.policy.pap.main.PolicyPapRuntimeException; * Data used during a single REST call when updating PDP policies. */ public class SessionData { + /** + * If a version string matches this, then it is just a prefix (i.e., major or major.minor). + */ + private static final Pattern VERSION_PREFIX_PAT = Pattern.compile("[^.]+(?:[.][^.]*)?"); + + /** + * DB provider. + */ private final PolicyModelsProvider dao; /** @@ -66,13 +76,7 @@ public class SessionData { /** * Maps a policy's identifier to the policy. */ - private final Map policyCache = new HashMap<>(); - - /** - * Maps a policy name to its latest policy. Every policy appearing within this map has - * a corresponding entry in {@link #policyCache}. - */ - private final Map latestPolicy = new HashMap<>(); + private final Map policyCache = new HashMap<>(); /** @@ -88,28 +92,50 @@ public class SessionData { * Gets the policy, referenced by an identifier. Loads it from the cache, if possible. * Otherwise, gets it from the DB. * - * @param ident policy identifier + * @param desiredPolicy policy identifier * @return the specified policy * @throws PolicyPapRuntimeException if an error occurs */ - public ToscaPolicy getPolicy(ToscaPolicyIdentifier ident) { + public ToscaPolicy getPolicy(ToscaPolicyIdentifierOptVersion desiredPolicy) { - return policyCache.computeIfAbsent(ident, key -> { + ToscaPolicy policy = policyCache.computeIfAbsent(desiredPolicy, key -> { try { - List lst = dao.getPolicyList(ident.getName(), ident.getVersion()); + ToscaPolicyFilterBuilder filterBuilder = ToscaPolicyFilter.builder().name(desiredPolicy.getName()); + + String version = desiredPolicy.getVersion(); + if (version == null) { + // no version specified - get the latest + filterBuilder.version(ToscaPolicyFilter.LATEST_VERSION); + + } else if (VERSION_PREFIX_PAT.matcher(version).matches()) { + // version prefix provided - match the prefix and then pick the latest + filterBuilder.versionPrefix(version + ".").version(ToscaPolicyFilter.LATEST_VERSION); + + } else { + // must be an exact match + filterBuilder.version(version); + } + + + List lst = dao.getFilteredPolicyList(filterBuilder.build()); if (lst.isEmpty()) { - throw new PolicyPapRuntimeException( - "cannot find policy: " + ident.getName() + " " + ident.getVersion()); + throw new PolicyPapRuntimeException("cannot find policy: " + desiredPolicy.getName() + " " + + desiredPolicy.getVersion()); } return lst.get(0); } catch (PfModelException e) { - throw new PolicyPapRuntimeException("cannot get policy: " + ident.getName() + " " + ident.getVersion(), - e); + throw new PolicyPapRuntimeException( + "cannot get policy: " + desiredPolicy.getName() + " " + desiredPolicy.getVersion(), e); } }); + + // desired version may have only been a prefix - cache with full identifier, too + policyCache.putIfAbsent(new ToscaPolicyIdentifierOptVersion(policy.getIdentifier()), policy); + + return policy; } /** @@ -176,33 +202,6 @@ public class SessionData { .collect(Collectors.toList()); } - /** - * Gets the policy having the given name and the maximum version. - * - * @param name name of the desired policy - * @return the desired policy, or {@code null} if there is no policy with given name - * @throws PfModelException if an error occurs - */ - public ToscaPolicy getPolicyMaxVersion(String name) throws PfModelException { - ToscaPolicy policy = latestPolicy.get(name); - if (policy != null) { - return policy; - } - - ToscaPolicyFilter filter = - ToscaPolicyFilter.builder().name(name).version(ToscaPolicyFilter.LATEST_VERSION).build(); - List policies = dao.getFilteredPolicyList(filter); - if (policies.isEmpty()) { - throw new PolicyPapRuntimeException("cannot find policy: " + name); - } - - policy = policies.get(0); - policyCache.putIfAbsent(policy.getIdentifier(), policy); - latestPolicy.put(name, policy); - - return policy; - } - /** * Creates a group. * 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 index 458ca9e7..f7390553 100644 --- 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 @@ -70,12 +70,12 @@ public class TestPdpGroupDeleteControllerV1 extends CommonPapRestServer { Response rawresp = invocationBuilder.delete(); PdpGroupDeleteResponse resp = rawresp.readEntity(PdpGroupDeleteResponse.class); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus()); - assertEquals("cannot find policy: my-name", resp.getErrorDetails()); + assertEquals("cannot find policy: my-name null", resp.getErrorDetails()); rawresp = invocationBuilder.delete(); resp = rawresp.readEntity(PdpGroupDeleteResponse.class); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rawresp.getStatus()); - assertEquals("cannot find policy: my-name", resp.getErrorDetails()); + assertEquals("cannot find policy: my-name null", 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 index c0b08644..5824b4b0 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 @@ -56,8 +56,6 @@ import org.onap.policy.pap.main.PolicyPapRuntimeException; public class TestPdpGroupDeleteProvider 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 MyProvider prov; @@ -169,9 +167,7 @@ public class TestPdpGroupDeleteProvider extends ProviderSuper { PdpGroup group = loadGroup("undeploy.json"); when(dao.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group)); - - when(dao.getPolicyList(POLICY1_NAME, "1.2.300")).thenReturn(Arrays.asList(policy1)); - when(dao.getPolicyList("policyB", POLICY1_VERSION)).thenReturn(Arrays.asList(policy1)); + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); Pair pair = new PdpGroupDeleteProvider().undeploy(optIdent); assertEquals(Status.OK, pair.getLeft()); 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 cad73d92..12c2adad 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 @@ -58,7 +58,6 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { private static final String EXPECTED_EXCEPTION = "expected exception"; private static final Object REQUEST_FAILED_MSG = "request failed"; - private static final String POLICY1_NAME = "policyA"; private static final String POLICY2_NAME = "policyB"; private static final String POLICY1_VERSION = "1.2.3"; private static final String GROUP1_NAME = "groupA"; @@ -86,7 +85,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { super.setUp(); - when(dao.getPolicyList(POLICY1_NAME, POLICY1_VERSION)).thenReturn(loadPolicies("daoPolicyList.json")); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); prov = new PdpGroupDeployProvider(); } @@ -279,7 +278,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION)); subgrp.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("typeX", "9.8.7")); - when(dao.getPolicyList(POLICY2_NAME, POLICY1_VERSION)).thenReturn(loadPolicies("createGroupNewPolicy.json")); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")) + .thenReturn(loadPolicies("createGroupNewPolicy.json")); assertEquals(Status.OK, prov.createOrUpdateGroups(groups).getLeft()); @@ -424,7 +424,8 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { PdpSubGroup subgrp = newgrp.getPdpSubgroups().get(0); subgrp.getPolicies().add(new ToscaPolicyIdentifier(POLICY2_NAME, POLICY1_VERSION)); - when(dao.getPolicyList(POLICY2_NAME, POLICY1_VERSION)).thenReturn(loadPolicies("createGroupNewPolicy.json")); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")) + .thenReturn(loadPolicies("createGroupNewPolicy.json")); assertEquals(Status.OK, prov.createOrUpdateGroups(groups).getLeft()); @@ -478,7 +479,7 @@ public class TestPdpGroupDeployProvider extends ProviderSuper { @Test public void testDeploySimplePolicies_RuntimeEx() throws Exception { - when(dao.getPolicyList(any(), any())).thenThrow(new RuntimeException(EXPECTED_EXCEPTION)); + when(dao.getFilteredPolicyList(any())).thenThrow(new RuntimeException(EXPECTED_EXCEPTION)); Pair pair = prov.deployPolicies(loadRequest()); assertEquals(Status.INTERNAL_SERVER_ERROR, pair.getLeft()); 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 01b29b10..bec93e2d 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 @@ -89,7 +89,7 @@ public class TestProviderBase extends ProviderSuper { super.setUp(); - when(dao.getPolicyList(POLICY1_NAME, POLICY1_VERSION)).thenReturn(loadPolicies("daoPolicyList.json")); + when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); prov = new MyProvider(); } @@ -156,33 +156,7 @@ public class TestProviderBase extends ProviderSuper { assertEquals(Status.OK, pair.getLeft()); assertNull(pair.getRight().getErrorDetails()); - verify(dao).getPolicyList(any(), any()); - verify(dao, never()).getFilteredPolicyList(any()); - } - - @Test - public void testGetPolicy_NullVersion() throws Exception { - // only allow this query once - when(dao.getPolicyList(POLICY1_NAME, POLICY1_VERSION)).thenReturn(loadPolicies("daoPolicyList.json")) - .thenThrow(new RuntimeException(EXPECTED_EXCEPTION)); - - when(dao.getFilteredPolicyList(any())).thenReturn(loadPolicies("daoPolicyList.json")); - - Pair pair = prov.process(loadRequest("getPolicyReqNullVersion.json"), this::handle); - assertEquals(Status.OK, pair.getLeft()); - assertNull(pair.getRight().getErrorDetails()); - verify(dao).getFilteredPolicyList(any()); - verify(dao, never()).getPolicies(any(), any()); - } - - @Test - public void testGetPolicy_NotFound() throws Exception { - when(dao.getPolicyList(any(), any())).thenReturn(Collections.emptyList()); - - Pair pair = prov.process(loadRequest(), this::handle); - assertEquals(Status.INTERNAL_SERVER_ERROR, pair.getLeft()); - assertEquals("cannot find policy: " + POLICY1_NAME + " " + POLICY1_VERSION, pair.getRight().getErrorDetails()); } @Test @@ -245,10 +219,10 @@ public class TestProviderBase extends ProviderSuper { * Should generate updates to pdp1, pdp2, and pdp3. */ - when(dao.getPolicyList(POLICY1_NAME, POLICY1_VERSION)).thenReturn(loadPolicies("daoPolicyList.json")); - when(dao.getPolicyList("policyB", POLICY1_VERSION)).thenReturn(loadPolicies("upgradeGroupPolicy2.json")); - when(dao.getPolicyList("policyC", POLICY1_VERSION)).thenReturn(loadPolicies("upgradeGroupPolicy3.json")); - when(dao.getPolicyList("policyD", POLICY1_VERSION)).thenReturn(loadPolicies("upgradeGroupPolicy4.json")); + 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"); 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 2eac4324..e74f9f86 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 @@ -43,12 +43,14 @@ 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.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.pap.main.PolicyPapRuntimeException; @@ -60,13 +62,12 @@ public class TestSessionData extends ProviderSuper { 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_VERSION2 = POLICY_VERSION_PREFIX + "4"; 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 ToscaPolicyIdentifier ident; + private ToscaPolicyIdentifierOptVersion ident; private ToscaPolicyTypeIdentifier type; private ToscaPolicyTypeIdentifier type2; private PdpGroup group1; @@ -81,7 +82,7 @@ public class TestSessionData extends ProviderSuper { public void setUp() throws Exception { super.setUp(); - ident = new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION); + 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"); @@ -91,32 +92,62 @@ public class TestSessionData extends ProviderSuper { } @Test - public void testGetPolicy() throws Exception { + public void testGetPolicy_NullVersion() throws Exception { ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); - when(dao.getPolicyList(POLICY_NAME, POLICY_VERSION)).thenReturn(Arrays.asList(policy1)); + when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); - ToscaPolicy policy2 = makePolicy(POLICY_NAME, POLICY_VERSION2); - when(dao.getPolicyList(POLICY_NAME, POLICY_VERSION2)).thenReturn(Arrays.asList(policy2)); + ident.setVersion(null); + assertSame(policy1, session.getPolicy(ident)); - ToscaPolicyIdentifier ident2 = new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION2); + ToscaPolicyFilter filter = getPolicyFilter(); + assertEquals(POLICY_NAME, filter.getName()); + assertEquals(ToscaPolicyFilter.LATEST_VERSION, filter.getVersion()); + assertEquals(null, filter.getVersionPrefix()); - assertSame(policy1, session.getPolicy(ident)); - assertSame(policy2, session.getPolicy(ident2)); + // retrieve a second time using full version - should use cache + assertSame(policy1, session.getPolicy(new ToscaPolicyIdentifierOptVersion(policy1.getIdentifier()))); + verify(dao).getFilteredPolicyList(any()); + } - // repeat + @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)); - assertSame(policy2, session.getPolicy(ident2)); + 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)); - assertSame(policy2, session.getPolicy(ident2)); - // should have only invoked this once for each policy - verify(dao, times(2)).getPolicyList(any(), any()); + 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.getPolicyList(any(), any())).thenReturn(Collections.emptyList()); + when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); assertThatThrownBy(() -> session.getPolicy(ident)).hasMessage("cannot find policy: myPolicy 1.2.3"); } @@ -124,7 +155,7 @@ public class TestSessionData extends ProviderSuper { @Test public void testGetPolicy_DaoEx() throws Exception { PfModelException ex = new PfModelException(Status.INTERNAL_SERVER_ERROR, "expected exception"); - when(dao.getPolicyList(any(), any())).thenThrow(ex); + when(dao.getFilteredPolicyList(any())).thenThrow(ex); assertThatThrownBy(() -> session.getPolicy(ident)).hasMessage("cannot get policy: myPolicy 1.2.3").hasCause(ex); } @@ -249,26 +280,6 @@ public class TestSessionData extends ProviderSuper { return policy; } - @Test - public void testGetPolicyMaxVersion() throws Exception { - ToscaPolicy policy1 = makePolicy(POLICY_NAME, POLICY_VERSION); - - when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1)); - - assertSame(policy1, session.getPolicyMaxVersion(POLICY_NAME)); - assertSame(policy1, session.getPolicyMaxVersion(POLICY_NAME)); - assertSame(policy1, session.getPolicyMaxVersion(POLICY_NAME)); - - // should have only invoked DAO once; used cache for other requests - verify(dao, times(1)).getFilteredPolicyList(any()); - } - - @Test - public void testGetPolicyMaxVersion_NotFound() throws Exception { - when(dao.getFilteredPolicyList(any())).thenReturn(Collections.emptyList()); - assertThatThrownBy(() -> session.getPolicyMaxVersion(POLICY_NAME)).hasMessage("cannot find policy: myPolicy"); - } - @Test public void testCreate() throws Exception { session.create(group1); @@ -478,6 +489,13 @@ public class TestSessionData extends ProviderSuper { 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(); } diff --git a/main/src/test/resources/simpleDeploy/getPolicyReqNullVersion.json b/main/src/test/resources/simpleDeploy/getPolicyReqNullVersion.json deleted file mode 100644 index 5f9f8c9e..00000000 --- a/main/src/test/resources/simpleDeploy/getPolicyReqNullVersion.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "policies": [ - { - "policy-id": "policyA" - } - ] -} -- cgit 1.2.3-korg