diff options
Diffstat (limited to 'models-tosca/src')
4 files changed, 190 insertions, 147 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java index a394cec3e..23406b473 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java @@ -20,7 +20,6 @@ package org.onap.policy.models.tosca.legacy.provider; -import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -28,6 +27,7 @@ import javax.ws.rs.core.Response; import lombok.NonNull; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; @@ -53,36 +53,25 @@ public class LegacyProvider { private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class); private static final String FIRST_POLICY_VERSION = "1"; + private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; // Recurring constants - private static final String NO_POLICY_FOUND_FOR_POLICY_ID = "no policy found for policy ID: "; + private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: "; /** * Get legacy operational policy. * * @param dao the DAO to use to access the database * @param policyId ID of the policy. + * @param policyVersion version of the policy. * @return the policies found * @throws PfModelException on errors getting policies */ - public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId) - throws PfModelException { + public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId, + final String policyVersion) throws PfModelException { - JpaToscaPolicy newestPolicy = getLatestPolicy(dao, policyId); - - if (newestPolicy == null) { - String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - // Create the structure of the TOSCA service template to contain the policy type - JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); - serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy); - - return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + return new LegacyOperationalPolicyMapper() + .fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion)); } /** @@ -125,22 +114,10 @@ public class LegacyProvider { public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao, @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { - // We need to find the latest policy and use the major version, if there is no policy with this ID, then - // we have an error - JpaToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId()); - - if (newestPolicy == null) { - String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + legacyOperationalPolicy.getPolicyId(); - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } else { - legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion())); - } - JpaToscaServiceTemplate incomingServiceTemplate = new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy); JpaToscaServiceTemplate outgoingingServiceTemplate = - new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); + new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate); return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); } @@ -150,42 +127,15 @@ public class LegacyProvider { * * @param dao the DAO to use to access the database * @param policyId ID of the policy. + * @param policyVersion version of the policy. * @return the deleted policy * @throws PfModelException on errors deleting policies */ - public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId) - throws PfModelException { - - // Get all the policies in the database and check the policy ID against the policies returned - List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class); - - // Find the latest policy that matches the ID - List<JpaToscaPolicy> policyDeleteList = new ArrayList<>(); - - for (JpaToscaPolicy policy : policyList) { - if (policyId.equals(policy.getKey().getName())) { - policyDeleteList.add(policy); - } - } - - if (policyDeleteList.isEmpty()) { - String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - // Create the structure of the TOSCA service template to contain the policy type - JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); - serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); - - for (JpaToscaPolicy deletePolicy : policyDeleteList) { - dao.delete(deletePolicy); - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), - deletePolicy); - } + public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId, + @NonNull final String policyVersion) throws PfModelException { - return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + return new LegacyOperationalPolicyMapper() + .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion)); } /** @@ -193,27 +143,14 @@ public class LegacyProvider { * * @param dao the DAO to use to access the database * @param policyId ID of the policy. + * @param policyVersion version of the policy. * @return the policies found * @throws PfModelException on errors getting policies */ - public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) - throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId, + final String policyVersion) throws PfModelException { - JpaToscaPolicy newestPolicy = getLatestPolicy(dao, policyId); - - if (newestPolicy == null) { - String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - - // Create the structure of the TOSCA service template to contain the policy type - JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); - serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); - serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy); - - return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion)); } /** @@ -249,7 +186,7 @@ public class LegacyProvider { JpaToscaServiceTemplate incomingServiceTemplate = new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy); JpaToscaServiceTemplate outgoingingServiceTemplate = - new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); + new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate); return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); } @@ -260,26 +197,37 @@ public class LegacyProvider { * * @param dao the DAO to use to access the database * @param policyId ID of the policy. + * @param policyVersion version of the policy. * @return the deleted policy * @throws PfModelException on errors deleting policies */ public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao, - @NonNull final String policyId) throws PfModelException { - - // Get all the policies in the database and check the policy ID against the policies returned - List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class); + @NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException { - // Find the latest policy that matches the ID - List<JpaToscaPolicy> policyDeleteList = new ArrayList<>(); + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion)); + } - for (JpaToscaPolicy policy : policyList) { - if (policyId.equals(policy.getKey().getName())) { - policyDeleteList.add(policy); - } + /** + * Get the JPA Policy for a policy ID and version. + * + * @param dao The DAO to search + * @param policyId the policy ID to search for + * @param policyVersion the policy version to search for + * @return the JPA policy found + * @throws PfModelRuntimeException if a policy is not found + */ + private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId, + final String policyVersion) { + JpaToscaPolicy foundPolicy = null; + if (policyVersion == null) { + foundPolicy = getLatestPolicy(dao, policyId); + } else { + foundPolicy = dao.get(JpaToscaPolicy.class, + new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX)); } - if (policyDeleteList.isEmpty()) { - String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId; + if (foundPolicy == null) { + String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion; LOGGER.warn(errorMessage); throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } @@ -288,14 +236,41 @@ public class LegacyProvider { JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(foundPolicy.getKey(), foundPolicy); + + return serviceTemplate; + } + + /** + * Delete a legacy policy. + * + * @param dao the DAO to use for the deletion + * @param policyId the policy ID + * @param policyVersion the policy version + * @return a service template containing the policy that has been deleted + */ + private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId, + final String policyVersion) { + + final JpaToscaPolicy deletePolicy = + dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX)); - for (JpaToscaPolicy deletePolicy : policyDeleteList) { - dao.delete(deletePolicy); - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), - deletePolicy); + if (deletePolicy == null) { + String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } - return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + // Delete the policy + dao.delete(deletePolicy); + + // Create the structure of the TOSCA service template to contain the policy type + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), deletePolicy); + + return serviceTemplate; } /** @@ -325,4 +300,5 @@ public class LegacyProvider { } return newestPolicy; } + } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java index eebacd1d6..30ad2b246 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java @@ -186,9 +186,17 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) { super.fromAuthorative(toscaPolicy); - type.setName(toscaPolicy.getType()); - type.setVersion(toscaPolicy.getTypeVersion()); - if (type.getVersion() == null) { + if (toscaPolicy.getType() != null) { + type.setName(toscaPolicy.getType()); + } + else { + type.setName(PfKey.NULL_KEY_NAME); + } + + if (toscaPolicy.getTypeVersion() != null) { + type.setVersion(toscaPolicy.getTypeVersion()); + } + else { type.setVersion(PfKey.NULL_KEY_VERSION); } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java index 2aadcd7b2..9487ed8aa 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java @@ -98,20 +98,20 @@ public class LegacyProvider4LegacyGuardTest { @Test public void testPoliciesGet() throws Exception { assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(null, null); + new LegacyProvider().getGuardPolicy(null, null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(null, ""); + new LegacyProvider().getGuardPolicy(null, null, ""); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, null); + new LegacyProvider().getGuardPolicy(pfDao, null, null); }).hasMessage("policyId is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist"); - }).hasMessage("no policy found for policy ID: I Dont Exist"); + new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist", null); + }).hasMessage("no policy found for policy: I Dont Exist:null"); createPolicyTypes(); @@ -128,7 +128,7 @@ public class LegacyProvider4LegacyGuardTest { createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); Map<String, LegacyGuardPolicyOutput> gotGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); assertEquals(originalGip.getContent(), @@ -139,6 +139,20 @@ public class LegacyProvider4LegacyGuardTest { String actualJsonOutput = standardCoder.encode(gotGopm); assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + + gotGopm = new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "1"); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + actualJsonOutput = standardCoder.encode(gotGopm); + + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), "2"); + }).hasMessage("no policy found for policy: guard.frequency.scaleout:2"); } @Test @@ -170,7 +184,7 @@ public class LegacyProvider4LegacyGuardTest { createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); Map<String, LegacyGuardPolicyOutput> gotGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); assertEquals(originalGip.getContent(), @@ -244,7 +258,7 @@ public class LegacyProvider4LegacyGuardTest { createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); Map<String, LegacyGuardPolicyOutput> gotGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); assertEquals(originalGip.getContent(), @@ -257,7 +271,7 @@ public class LegacyProvider4LegacyGuardTest { updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next()); assertEquals(originalGip.getContent(), gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); @@ -269,21 +283,36 @@ public class LegacyProvider4LegacyGuardTest { @Test public void testPoliciesDelete() throws Exception { assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(null, null); + new LegacyProvider().deleteGuardPolicy(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(null, null, ""); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(null, "", null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(null, ""); + new LegacyProvider().deleteGuardPolicy(null, "", ""); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(pfDao, null); + new LegacyProvider().deleteGuardPolicy(pfDao, null, null); }).hasMessage("policyId is marked @NonNull but is null"); + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(pfDao, null, ""); + }).hasMessage("policyId is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(pfDao, "", null); + }).hasMessage("policyVersion is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist"); - }).hasMessage("no policy found for policy ID: I Dont Exist"); + new LegacyProvider().deleteGuardPolicy(pfDao, "IDontExist", ""); + }).hasMessage("no policy found for policy: IDontExist:"); createPolicyTypes(); @@ -299,7 +328,7 @@ public class LegacyProvider4LegacyGuardTest { createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); Map<String, LegacyGuardPolicyOutput> gotGopm = - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); assertEquals(originalGip.getContent(), @@ -311,15 +340,19 @@ public class LegacyProvider4LegacyGuardTest { assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), null); + }).hasMessage("policyVersion is marked @NonNull but is null"); + Map<String, LegacyGuardPolicyOutput> deletedGopm = - new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId()); + new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), "1"); assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next()); assertEquals(originalGip.getContent(), deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); - }).hasMessage("no policy found for policy ID: guard.frequency.scaleout"); + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); + }).hasMessage("no policy found for policy: guard.frequency.scaleout:null"); LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput(); otherGip.setPolicyId("guard.blacklist.b0"); @@ -332,8 +365,8 @@ public class LegacyProvider4LegacyGuardTest { createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next()); assertThatThrownBy(() -> { - new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); - }).hasMessage("no policy found for policy ID: guard.frequency.scaleout"); + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId(), null); + }).hasMessage("no policy found for policy: guard.frequency.scaleout:null"); } private void createPolicyTypes() throws CoderException, PfModelException { diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java index 7ab5c581e..17b912826 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java @@ -72,7 +72,7 @@ public class LegacyProvider4LegacyOperationalTest { jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); - daoParameters.setJdbcProperties(jdbcProperties ); + daoParameters.setJdbcProperties(jdbcProperties); pfDao = new PfDaoFactory().createPfDao(daoParameters); pfDao.init(daoParameters); @@ -94,20 +94,20 @@ public class LegacyProvider4LegacyOperationalTest { @Test public void testPoliciesGet() throws Exception { assertThatThrownBy(() -> { - new LegacyProvider().getOperationalPolicy(null, null); + new LegacyProvider().getOperationalPolicy(null, null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().getOperationalPolicy(null, ""); + new LegacyProvider().getOperationalPolicy(null, "", null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().getOperationalPolicy(pfDao, null); + new LegacyProvider().getOperationalPolicy(pfDao, null, null); }).hasMessage("policyId is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist"); - }).hasMessage("no policy found for policy ID: I Dont Exist"); + new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist", null); + }).hasMessage("no policy found for policy: I Dont Exist:null"); createPolicyTypes(); @@ -121,7 +121,8 @@ public class LegacyProvider4LegacyOperationalTest { assertEquals(originalLop, createdLop); - LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + LegacyOperationalPolicy gotLop = + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null); assertEquals(gotLop, originalLop); @@ -131,7 +132,8 @@ public class LegacyProvider4LegacyOperationalTest { assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); - LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + LegacyOperationalPolicy gotLopV2 = + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null); assertEquals(gotLopV2, createdLopV2); } @@ -161,7 +163,8 @@ public class LegacyProvider4LegacyOperationalTest { assertEquals(originalLop, createdLop); - LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + LegacyOperationalPolicy gotLop = + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null); assertEquals(gotLop, originalLop); @@ -187,7 +190,7 @@ public class LegacyProvider4LegacyOperationalTest { assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy()); - }).hasMessage("no policy found for policy ID: null"); + }).hasMessage("name is marked @NonNull but is null"); createPolicyTypes(); @@ -200,7 +203,8 @@ public class LegacyProvider4LegacyOperationalTest { LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); assertEquals(originalLop, createdLop); - LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + LegacyOperationalPolicy gotLop = + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null); assertEquals(gotLop, originalLop); originalLop.setContent("Some New Content"); @@ -208,7 +212,7 @@ public class LegacyProvider4LegacyOperationalTest { assertEquals(originalLop, updatedLop); LegacyOperationalPolicy gotUpdatedLop = - new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null); assertEquals(gotUpdatedLop, originalLop); assertEquals("Some New Content", gotUpdatedLop.getContent()); } @@ -216,21 +220,38 @@ public class LegacyProvider4LegacyOperationalTest { @Test public void testPoliciesDelete() throws Exception { assertThatThrownBy(() -> { - new LegacyProvider().deleteOperationalPolicy(null, null); + new LegacyProvider().deleteOperationalPolicy(null, null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().deleteOperationalPolicy(null, ""); + new LegacyProvider().deleteOperationalPolicy(null, null, ""); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().deleteOperationalPolicy(pfDao, null); + new LegacyProvider().deleteOperationalPolicy(null, "", null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteOperationalPolicy(null, "", ""); + + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteOperationalPolicy(pfDao, null, null); }).hasMessage("policyId is marked @NonNull but is null"); assertThatThrownBy(() -> { - new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist"); - }).hasMessage("no policy found for policy ID: I Dont Exist"); + new LegacyProvider().deleteOperationalPolicy(pfDao, null, ""); + }).hasMessage("policyId is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteOperationalPolicy(pfDao, "", null); + }).hasMessage("policyVersion is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", ""); + }).hasMessage("no policy found for policy: IDontExist:"); createPolicyTypes(); @@ -243,7 +264,8 @@ public class LegacyProvider4LegacyOperationalTest { LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); assertEquals(originalLop, createdLop); - LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); + LegacyOperationalPolicy gotLop = + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null); assertEquals(gotLop, originalLop); @@ -252,13 +274,17 @@ public class LegacyProvider4LegacyOperationalTest { assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + assertThatThrownBy(() -> { + new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), null); + }).hasMessage("policyVersion is marked @NonNull but is null"); + LegacyOperationalPolicy deletedLop = - new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId()); + new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), "1"); assertEquals(originalLop, deletedLop); assertThatThrownBy(() -> { - new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); - }).hasMessage("no policy found for policy ID: operational.restart"); + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null); + }).hasMessage("no policy found for policy: operational.restart:null"); LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy(); otherLop.setPolicyId("another-policy"); @@ -269,13 +295,13 @@ public class LegacyProvider4LegacyOperationalTest { assertEquals(otherLop, createdOtherLop); assertThatThrownBy(() -> { - new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); - }).hasMessage("no policy found for policy ID: operational.restart"); + new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null); + }).hasMessage("no policy found for policy: operational.restart:null"); } private void createPolicyTypes() throws CoderException, PfModelException { - Object yamlObject = new Yaml().load( - ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml")); + Object yamlObject = new Yaml() + .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml")); String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate toscaServiceTemplatePolicyType = |