From 6ccec5265d3431a1ca3265876b3df7bb422d9b62 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Wed, 27 Mar 2019 22:27:14 +0000 Subject: [PATCH] Add support for legacy guard policies Support for legacy guard policies added. Support for translation of all legacy policies to TOSCA format now complete. Fix merge problems with dummy provider implementation. Issue-ID: POLICY-1095 Change-Id: I3dd1775b78d39078a884e1834502b832ff40be18 Signed-off-by: liamfallon --- .../policies/vDNS.policy.guard.minmax.input.json | 2 +- .../policy/models/pdp/provider/PdpProvider.java | 6 +- models-provider/pom.xml | 7 + .../models/provider/PolicyModelsProvider.java | 49 ++-- .../impl/DatabasePolicyModelsProviderImpl.java | 29 +- .../impl/DummyPolicyModelsProviderImpl.java | 32 ++- .../impl/DatabasePolicyModelsProviderTest.java | 320 +++++++++------------ .../models/provider/impl/DummyBadProviderImpl.java | 20 +- .../impl/DummyPolicyModelsProviderTest.java | 153 +++------- .../impl/PolicyLegacyGuardPersistenceTest.java | 143 +++++++++ .../PolicyLegacyOperationalPersistenceTest.java | 15 +- .../legacy/concepts/LegacyGuardPolicyContent.java | 80 ++++++ ...uardPolicy.java => LegacyGuardPolicyInput.java} | 9 +- .../legacy/concepts/LegacyGuardPolicyOutput.java | 38 +++ .../legacy/mapping/LegacyGuardPolicyMapper.java | 151 ++++++++++ .../tosca/legacy/provider/LegacyProvider.java | 84 +++++- .../PlainToscaServiceTemplateMapperTest.java | 18 +- .../legacy/concepts/LegacyGuardPolicyTest.java | 12 +- .../models/tosca/legacy/concepts/TestPojos.java | 52 ++++ .../provider/LegacyProvider4LegacyGuardTest.java | 295 +++++++++++++++++++ ...a => LegacyProvider4LegacyOperationalTest.java} | 168 ++++------- .../LegacyOperationalPolicySerializationTest.java | 15 +- .../MonitoringPolicySerializationTest.java | 5 +- 23 files changed, 1179 insertions(+), 524 deletions(-) rename models-pap/src/main/java/org/onap/policy/models/pap/provider/PapProvider.java => models-pdp/src/main/java/org/onap/policy/models/pdp/provider/PdpProvider.java (95%) create mode 100644 models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java rename models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/{LegacyGuardPolicy.java => LegacyGuardPolicyInput.java} (85%) create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java create mode 100644 models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java rename models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/{LegacyProviderTest.java => LegacyProvider4LegacyOperationalTest.java} (60%) diff --git a/models-examples/src/main/resources/policies/vDNS.policy.guard.minmax.input.json b/models-examples/src/main/resources/policies/vDNS.policy.guard.minmax.input.json index b44a855fe..cd7587c64 100644 --- a/models-examples/src/main/resources/policies/vDNS.policy.guard.minmax.input.json +++ b/models-examples/src/main/resources/policies/vDNS.policy.guard.minmax.input.json @@ -1,6 +1,6 @@ { "policy-id" : "guard.minmax.scaleout", - "contents" : { + "content" : { "actor": "SO", "recipe": "VF Module Create", "targets": ".*", diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/provider/PapProvider.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/provider/PdpProvider.java similarity index 95% rename from models-pap/src/main/java/org/onap/policy/models/pap/provider/PapProvider.java rename to models-pdp/src/main/java/org/onap/policy/models/pdp/provider/PdpProvider.java index 4b80bea29..57feba620 100644 --- a/models-pap/src/main/java/org/onap/policy/models/pap/provider/PapProvider.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/provider/PdpProvider.java @@ -18,20 +18,20 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.models.pap.provider; +package org.onap.policy.models.pdp.provider; import lombok.NonNull; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.dao.PfDao; -import org.onap.policy.models.pap.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpGroups; /** * This class provides the provision of information on PAP concepts in the database to callers. * * @author Liam Fallon (liam.fallon@est.tech) */ -public class PapProvider { +public class PdpProvider { /** * Get PDP groups. * diff --git a/models-provider/pom.xml b/models-provider/pom.xml index b6021b003..cbe19389d 100644 --- a/models-provider/pom.xml +++ b/models-provider/pom.xml @@ -79,5 +79,12 @@ mockito-all test + + + org.assertj + assertj-core + test + + diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java index 9bc49837e..103d761df 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java @@ -20,18 +20,21 @@ package org.onap.policy.models.provider; +import java.util.Map; + import lombok.NonNull; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.pap.concepts.PdpGroups; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; /** - * This interface describes the operations that are provided to users and components for reading - * objects from and writing objects to the database. + * This interface describes the operations that are provided to users and components for reading objects from and + * writing objects to the database. * * @author Liam Fallon (liam.fallon@est.tech) */ @@ -46,9 +49,8 @@ public interface PolicyModelsProvider extends AutoCloseable { /** * Get policy types. * - * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key - * name returns all policy types. A null key version returns all versions of the policy - * type name specified in the key. + * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key name returns all policy + * types. A null key version returns all versions of the policy type name specified in the key. * @return the policy types found * @throws PfModelException on errors getting policy types */ @@ -57,8 +59,7 @@ public interface PolicyModelsProvider extends AutoCloseable { /** * Create policy types. * - * @param serviceTemplate the service template containing the definition of the policy types to - * be created + * @param serviceTemplate the service template containing the definition of the policy types to be created * @return the TOSCA service template containing the created policy types * @throws PfModelException on errors creating policy types */ @@ -68,8 +69,7 @@ public interface PolicyModelsProvider extends AutoCloseable { /** * Create policy types. * - * @param serviceTemplate the service template containing the definition of the policy types to - * be modified + * @param serviceTemplate the service template containing the definition of the policy types to be modified * @return the TOSCA service template containing the modified policy types * @throws PfModelException on errors updating policy types */ @@ -79,8 +79,8 @@ public interface PolicyModelsProvider extends AutoCloseable { /** * Delete policy types. * - * @param policyTypeKey the policy type key for the policy types to be deleted, if the version - * of the key is null, all versions of the policy type are deleted. + * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null, + * all versions of the policy type are deleted. * @return the TOSCA service template containing the policy types that were deleted * @throws PfModelException on errors deleting policy types */ @@ -89,8 +89,8 @@ public interface PolicyModelsProvider extends AutoCloseable { /** * Get policies. * - * @param policyKey the policy key for the policies to be retrieved. The parent name and version - * must be specified. A null local name returns all policies for a parent policy type. + * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified. + * A null local name returns all policies for a parent policy type. * @return the policies found * @throws PfModelException on errors getting policies */ @@ -99,8 +99,7 @@ public interface PolicyModelsProvider extends AutoCloseable { /** * Create policies. * - * @param serviceTemplate the service template containing the definitions of the new policies to - * be created. + * @param serviceTemplate the service template containing the definitions of the new policies to be created. * @return the TOSCA service template containing the policy types that were created * @throws PfModelException on errors creating policies */ @@ -111,8 +110,7 @@ public interface PolicyModelsProvider extends AutoCloseable { /** * Update policies. * - * @param serviceTemplate the service template containing the definitions of the policies to be - * updated. + * @param serviceTemplate the service template containing the definitions of the policies to be updated. * @return the TOSCA service template containing the policies that were updated * @throws PfModelException on errors updating policies */ @@ -173,7 +171,7 @@ public interface PolicyModelsProvider extends AutoCloseable { * @return the policies found * @throws PfModelException on errors getting policies */ - public LegacyGuardPolicy getGuardPolicy(@NonNull final String policyId) throws PfModelException; + public Map getGuardPolicy(@NonNull final String policyId) throws PfModelException; /** * Create legacy guard policy. @@ -182,8 +180,8 @@ public interface PolicyModelsProvider extends AutoCloseable { * @return the created policy * @throws PfModelException on errors creating policies */ - public LegacyGuardPolicy createGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) - throws PfModelException; + public Map createGuardPolicy( + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException; /** * Update legacy guard policy. @@ -192,8 +190,8 @@ public interface PolicyModelsProvider extends AutoCloseable { * @return the updated policy * @throws PfModelException on errors updating policies */ - public LegacyGuardPolicy updateGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) - throws PfModelException; + public Map updateGuardPolicy( + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException; /** * Delete legacy guard policy. @@ -202,7 +200,8 @@ public interface PolicyModelsProvider extends AutoCloseable { * @return the deleted policy * @throws PfModelException on errors deleting policies */ - public LegacyGuardPolicy deleteGuardPolicy(@NonNull final String policyId) throws PfModelException; + public Map deleteGuardPolicy(@NonNull final String policyId) + throws PfModelException; /** * Get PDP groups. diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java index 43f75d2a9..078126243 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java @@ -23,6 +23,7 @@ package org.onap.policy.models.provider.impl; import java.sql.Connection; import java.sql.DriverManager; import java.util.Base64; +import java.util.Map; import javax.ws.rs.core.Response; @@ -35,11 +36,12 @@ import org.onap.policy.models.dao.DaoParameters; import org.onap.policy.models.dao.PfDao; import org.onap.policy.models.dao.PfDaoFactory; import org.onap.policy.models.dao.impl.DefaultPfDao; -import org.onap.policy.models.pap.concepts.PdpGroups; -import org.onap.policy.models.pap.provider.PapProvider; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.provider.PdpProvider; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.legacy.provider.LegacyProvider; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; @@ -214,27 +216,28 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { } @Override - public LegacyGuardPolicy getGuardPolicy(@NonNull final String policyId) throws PfModelException { + public Map getGuardPolicy(@NonNull final String policyId) throws PfModelException { assertInitilized(); return new LegacyProvider().getGuardPolicy(pfDao, policyId); } @Override - public LegacyGuardPolicy createGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) - throws PfModelException { + public Map createGuardPolicy( + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { assertInitilized(); return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy); } @Override - public LegacyGuardPolicy updateGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) - throws PfModelException { + public Map updateGuardPolicy( + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { assertInitilized(); return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy); } @Override - public LegacyGuardPolicy deleteGuardPolicy(@NonNull final String policyId) throws PfModelException { + public Map deleteGuardPolicy(@NonNull final String policyId) + throws PfModelException { assertInitilized(); return new LegacyProvider().deleteGuardPolicy(pfDao, policyId); } @@ -242,25 +245,25 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { @Override public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException { assertInitilized(); - return new PapProvider().getPdpGroups(pfDao, pdpGroupFilter); + return new PdpProvider().getPdpGroups(pfDao, pdpGroupFilter); } @Override public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException { assertInitilized(); - return new PapProvider().createPdpGroups(pfDao, pdpGroups); + return new PdpProvider().createPdpGroups(pfDao, pdpGroups); } @Override public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException { assertInitilized(); - return new PapProvider().updatePdpGroups(pfDao, pdpGroups); + return new PdpProvider().updatePdpGroups(pfDao, pdpGroups); } @Override public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException { assertInitilized(); - return new PapProvider().deletePdpGroups(pfDao, pdpGroupFilter); + return new PdpProvider().deletePdpGroups(pfDao, pdpGroupFilter); } /** diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java index 8d833a53d..36fb01c27 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java @@ -23,16 +23,20 @@ package org.onap.policy.models.provider.impl; import com.google.gson.Gson; +import java.util.HashMap; +import java.util.Map; + import javax.ws.rs.core.Response; import lombok.NonNull; import org.onap.policy.common.utils.resources.ResourceUtils; 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.pap.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler; @@ -49,8 +53,7 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { * * @param parameters the parameters for the provider */ - public DummyPolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) { - } + public DummyPolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {} @Override public void init() throws PfModelException { @@ -130,25 +133,26 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { } @Override - public LegacyGuardPolicy getGuardPolicy(@NonNull final String policyId) throws PfModelException { - return new LegacyGuardPolicy(); + public Map getGuardPolicy(@NonNull final String policyId) throws PfModelException { + return new HashMap<>(); } @Override - public LegacyGuardPolicy createGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) - throws PfModelException { - return legacyGuardPolicy; + public Map createGuardPolicy( + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + return new HashMap<>(); } @Override - public LegacyGuardPolicy updateGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) - throws PfModelException { - return legacyGuardPolicy; + public Map updateGuardPolicy( + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + return new HashMap<>(); } @Override - public LegacyGuardPolicy deleteGuardPolicy(@NonNull final String policyId) throws PfModelException { - return new LegacyGuardPolicy(); + public Map deleteGuardPolicy(@NonNull final String policyId) + throws PfModelException { + return new HashMap<>(); } @Override diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java index e4ecb9d1d..1631d9bb1 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -20,9 +20,8 @@ package org.onap.policy.models.provider.impl; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import java.util.Base64; @@ -30,11 +29,11 @@ import java.util.Base64; import org.junit.Before; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.pap.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.slf4j.Logger; @@ -69,12 +68,11 @@ public class DatabasePolicyModelsProviderTest { new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist"); - try { + + assertThatThrownBy(() -> { databaseProvider.init(); - fail("test should throw an exception"); - } catch (Exception pfme) { - assertEquals("could not connect to database with URL \"jdbc://www.acmecorp.nonexist\"", pfme.getMessage()); - } + }).hasMessage("could not connect to database with URL \"jdbc://www.acmecorp.nonexist\""); + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); try { @@ -85,13 +83,13 @@ public class DatabasePolicyModelsProviderTest { } parameters.setPersistenceUnit("WileECoyote"); - try { + + String errorMessage = "could not create Data Access Object (DAO) using url " + + "\"jdbc:h2:mem:testdb\" and persistence unit \"WileECoyote\""; + assertThatThrownBy(() -> { databaseProvider.init(); - fail("test should throw an exception"); - } catch (Exception pfme) { - assertEquals("could not create Data Access Object (DAO) using url " - + "\"jdbc:h2:mem:testdb\" and persistence unit \"WileECoyote\"", pfme.getMessage()); - } + }).hasMessage(errorMessage); + parameters.setPersistenceUnit("ToscaConceptTest"); try { @@ -107,15 +105,12 @@ public class DatabasePolicyModelsProviderTest { fail("test shold not throw an exception here"); } - try { + assertThatThrownBy(() -> { DatabasePolicyModelsProviderImpl databaseProviderImpl = (DatabasePolicyModelsProviderImpl) databaseProvider; databaseProvider.init(); databaseProviderImpl.setConnection(new DummyConnection()); databaseProvider.close(); - fail("test should throw an exception"); - } catch (Exception pfme) { - assertEquals("could not close connection to database with URL \"jdbc:h2:mem:testdb\"", pfme.getMessage()); - } + }).hasMessage("could not close connection to database with URL \"jdbc:h2:mem:testdb\""); } @Test @@ -124,144 +119,99 @@ public class DatabasePolicyModelsProviderTest { new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); databaseProvider.init(); - try { + assertThatThrownBy(() -> { databaseProvider.getPolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyTypeKey is marked @NonNull but is null"); + + + assertThatThrownBy(() -> { databaseProvider.createPolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updatePolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deletePolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyTypeKey is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { databaseProvider.getPolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyKey is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createPolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updatePolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deletePolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyKey is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { databaseProvider.getOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyId is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updateOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deleteOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { databaseProvider.getGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyId is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updateGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deleteGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { databaseProvider.getPdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroupFilter is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createPdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroups is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updatePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroups is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deletePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("pdpGroupFilter is marked @NonNull but is null"); databaseProvider.close(); + } @Test public void testProviderMethodsNotInit() throws Exception { PolicyModelsProvider databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); - try { + + assertThatThrownBy(() -> { databaseProvider.getPolicyTypes(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy models provider is not initilaized", npe.getMessage()); - } + }).hasMessage("policy models provider is not initilaized"); } @Test @@ -270,81 +220,69 @@ public class DatabasePolicyModelsProviderTest { new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { databaseProvider.init(); - try { + assertThatThrownBy(() -> { databaseProvider.getPolicyTypes(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy type not found: NULL:0.0.0", npe.getMessage()); - } - try { + }).hasMessage("policy type not found: NULL:0.0.0"); + + assertThatThrownBy(() -> { databaseProvider.createPolicyTypes(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("no policy types specified on service template", npe.getMessage()); - } - try { + }).hasMessage("no policy types specified on service template"); + + assertThatThrownBy(() -> { databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("no policy types specified on service template", npe.getMessage()); - } - try { + }).hasMessage("no policy types specified on service template"); + + assertThatThrownBy(() -> { databaseProvider.deletePolicyTypes(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy type not found: NULL:0.0.0", npe.getMessage()); - } + }).hasMessage("policy type not found: NULL:0.0.0"); - try { + assertThatThrownBy(() -> { databaseProvider.getPolicies(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy not found: NULL:0.0.0", npe.getMessage()); - } - try { + }).hasMessage("policy not found: NULL:0.0.0"); + + assertThatThrownBy(() -> { databaseProvider.createPolicies(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("topology template not specified on service template", npe.getMessage()); - } - try { + }).hasMessage("topology template not specified on service template"); + + assertThatThrownBy(() -> { databaseProvider.updatePolicies(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("topology template not specified on service template", npe.getMessage()); - } - try { + }).hasMessage("topology template not specified on service template"); + + assertThatThrownBy(() -> { databaseProvider.deletePolicies(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy not found: NULL:0.0.0", npe.getMessage()); - } - - try { - assertNull(databaseProvider.getOperationalPolicy("policy_id")); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("no policy found for policy ID: policy_id", npe.getMessage()); - } - try { - assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy())); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("name is marked @NonNull but is null", npe.getMessage()); - } - try { - assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy())); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("no policy found for policy ID: null", npe.getMessage()); - } - try { - assertNull(databaseProvider.deleteOperationalPolicy("policy_id")); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("no policy found for policy ID: policy_id", npe.getMessage()); - } - - assertNull(databaseProvider.getGuardPolicy("policy_id")); - assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy())); - assertNull(databaseProvider.updateGuardPolicy(new LegacyGuardPolicy())); - assertNull(databaseProvider.deleteGuardPolicy("policy_id")); + }).hasMessage("policy not found: NULL:0.0.0"); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy("policy_id"); + }).hasMessage("no policy found for policy ID: policy_id"); + + assertThatThrownBy(() -> { + databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()); + }).hasMessage("no policy found for policy ID: null"); + + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy("policy_id"); + }).hasMessage("no policy found for policy ID: policy_id"); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy("policy_id"); + }).hasMessage("no policy found for policy ID: policy_id"); + + assertThatThrownBy(() -> { + databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput()); + }).hasMessage("policy type for guard policy \"null\" unknown"); + + assertThatThrownBy(() -> { + databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput()); + }).hasMessage("policy type for guard policy \"null\" unknown"); + + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy("policy_id"); + }).hasMessage("no policy found for policy ID: policy_id"); assertNotNull(databaseProvider.getPdpGroups("filter")); assertNotNull(databaseProvider.createPdpGroups(new PdpGroups())); diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java index fb0a2416e..f8602e65c 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java @@ -20,6 +20,8 @@ package org.onap.policy.models.provider.impl; +import java.util.Map; + import javax.ws.rs.core.Response; import lombok.NonNull; @@ -27,9 +29,10 @@ 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.pap.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; @@ -47,8 +50,7 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { public void close() throws Exception {} @Override - public void init() throws PfModelException { - } + public void init() throws PfModelException {} @Override public ToscaServiceTemplate getPolicyTypes(@NonNull PfConceptKey policyTypeKey) throws PfModelException { @@ -115,22 +117,24 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { } @Override - public LegacyGuardPolicy getGuardPolicy(@NonNull String policyId) throws PfModelException { + public Map getGuardPolicy(@NonNull String policyId) throws PfModelException { return null; } @Override - public LegacyGuardPolicy createGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { + public Map createGuardPolicy(@NonNull LegacyGuardPolicyInput legacyGuardPolicy) + throws PfModelException { return null; } @Override - public LegacyGuardPolicy updateGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { + public Map updateGuardPolicy(@NonNull LegacyGuardPolicyInput legacyGuardPolicy) + throws PfModelException { return null; } @Override - public LegacyGuardPolicy deleteGuardPolicy(@NonNull String policyId) throws PfModelException { + public Map deleteGuardPolicy(@NonNull String policyId) throws PfModelException { return null; } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java index bf3382fdc..de61dc0ab 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java @@ -20,17 +20,18 @@ package org.onap.policy.models.provider.impl; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.pap.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; @@ -86,8 +87,8 @@ public class DummyPolicyModelsProviderTest { assertNotNull(dummyProvider.deleteOperationalPolicy("policy_id")); assertNotNull(dummyProvider.getGuardPolicy("policy_id")); - assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicy())); - assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicy())); + assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicyInput())); + assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput())); assertNotNull(dummyProvider.deleteGuardPolicy("policy_id")); assertNotNull(dummyProvider.getPdpGroups("filter")); @@ -95,134 +96,70 @@ public class DummyPolicyModelsProviderTest { assertNotNull(dummyProvider.updatePdpGroups(new PdpGroups())); assertNotNull(dummyProvider.deletePdpGroups("filter")); - try { + assertThatThrownBy(() -> { dummyProvider.getPolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyTypeKey is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createPolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updatePolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deletePolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyTypeKey is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { dummyProvider.getPolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyKey is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createPolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updatePolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deletePolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyKey is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { dummyProvider.getOperationalPolicy(null); - - - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyId is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updateOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deleteOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { dummyProvider.getGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyId is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updateGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deleteGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - - try { - + }).hasMessage("policyId is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.getPdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroupFilter is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createPdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroups is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updatePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroups is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deletePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("pdpGroupFilter is marked @NonNull but is null"); dummyProvider.close(); } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java new file mode 100644 index 000000000..2cb787d00 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java @@ -0,0 +1,143 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.provider.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.Base64; +import java.util.Map; + +import lombok.NonNull; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProvider; +import org.onap.policy.models.provider.PolicyModelsProviderFactory; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test persistence of monitoring policies to and from the database. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PolicyLegacyGuardPersistenceTest { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyGuardPersistenceTest.class); + + private StandardCoder standardCoder; + + private PolicyModelsProvider databaseProvider; + + // @formatter:off + private String[] policyInputResourceNames = { + "policies/vDNS.policy.guard.frequency.input.json", + "policies/vDNS.policy.guard.minmax.input.json" + }; + + private String[] policyOutputResourceNames = { + "policies/vDNS.policy.guard.frequency.output.json", + "policies/vDNS.policy.guard.minmax.output.json" + }; + // @formatter:on + + /** + * Initialize provider. + * + * @throws PfModelException on exceptions in the tests + */ + @Before + public void setupParameters() throws PfModelException { + PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); + parameters.setDatabaseUser("policy"); + parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); + parameters.setPersistenceUnit("ToscaConceptTest"); + + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider.init(); + } + + /** + * Set up standard coder. + */ + @Before + public void setupStandardCoder() { + standardCoder = new StandardCoder(); + } + + @After + public void teardown() throws Exception { + databaseProvider.close(); + } + + @Test + public void testPolicyPersistence() { + try { + for (int i = 0; i < policyInputResourceNames.length; i++) { + String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]); + String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]); + testJsonStringPolicyPersistence(policyInputString, policyOutputString); + } + } catch (Exception exc) { + LOGGER.warn("error processing policies", exc); + fail("test should not throw an exception"); + } + } + + /** + * Check persistence of a policy. + * + * @param policyInputString the policy as a string + * @param policyOutputString the expected output string + * @throws Exception any exception thrown + */ + public void testJsonStringPolicyPersistence(@NonNull final String policyInputString, + final String policyOutputString) throws Exception { + LegacyGuardPolicyInput gip = standardCoder.decode(policyInputString, LegacyGuardPolicyInput.class); + + assertNotNull(gip); + + Map createdGopm = databaseProvider.createGuardPolicy(gip); + assertEquals(gip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(gip.getContent(), + createdGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + + Map gotGopm = databaseProvider.getGuardPolicy(gip.getPolicyId()); + assertEquals(gip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(gip.getContent(), + gotGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + + String actualRetrievedJson = standardCoder.encode(gotGopm); + + // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters + assertEquals(policyOutputString.replaceAll("\\s+", ""), actualRetrievedJson.replaceAll("\\s+", "")); + } +} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java index 90d00fc58..60d577cb9 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java @@ -24,8 +24,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; -import com.google.gson.Gson; - import java.util.Base64; import lombok.NonNull; @@ -33,6 +31,7 @@ import lombok.NonNull; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; @@ -51,7 +50,7 @@ public class PolicyLegacyOperationalPersistenceTest { // Logger for this class private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyOperationalPersistenceTest.class); - private Gson gson; + private StandardCoder standardCoder; private PolicyModelsProvider databaseProvider; @@ -87,11 +86,11 @@ public class PolicyLegacyOperationalPersistenceTest { } /** - * Set up GSON. + * Set up standard coder. */ @Before - public void setupGson() { - gson = new Gson(); + public void setupStandardCoder() { + standardCoder = new StandardCoder(); } @After @@ -122,7 +121,7 @@ public class PolicyLegacyOperationalPersistenceTest { */ public void testJsonStringPolicyPersistence(@NonNull final String policyInputString, final String policyOutputString) throws Exception { - LegacyOperationalPolicy lop = gson.fromJson(policyInputString, LegacyOperationalPolicy.class); + LegacyOperationalPolicy lop = standardCoder.decode(policyInputString, LegacyOperationalPolicy.class); assertNotNull(lop); @@ -132,7 +131,7 @@ public class PolicyLegacyOperationalPersistenceTest { LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId()); assertEquals(gotLop, lop); - String actualRetrievedJson = gson.toJson(gotLop); + String actualRetrievedJson = standardCoder.encode(gotLop); // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters assertEquals( diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java new file mode 100644 index 000000000..b46737d2b --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyContent.java @@ -0,0 +1,80 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.concepts; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.Response; + +import lombok.Data; + +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Content object of a Legacy Guard Policy. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Data +public class LegacyGuardPolicyContent { + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class); + + private String actor; + private String recipe; + private String targets; + private String clname; + private String limit; + private String timeWindow; + private String timeUnits; + private String min; + private String max; + private String guardActiveStart; + private String guardActiveEnd; + + /** + * Get contents as a map. + * + * @return the contents as a map. + */ + public Map getAsPropertyMap() { + final Map propertyMap = new HashMap<>(); + + try { + for (Field field : this.getClass().getDeclaredFields()) { + if (field.get(this) != null && field.getType().equals(String.class)) { + propertyMap.put(field.getName(), (String)field.get(this)); + } + } + } catch (Exception exc) { + String errorMessage = "could not convert content to a property map"; + LOGGER.warn(errorMessage, exc); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc); + + } + + return propertyMap; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java similarity index 85% rename from models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java rename to models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java index 59715e4f9..18853c100 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyInput.java @@ -21,7 +21,7 @@ package org.onap.policy.models.tosca.legacy.concepts; -import java.util.Map; +import com.google.gson.annotations.SerializedName; import lombok.Data; @@ -29,15 +29,16 @@ import lombok.Data; * Definition of a legacy guard policy stored as a TOSCA policy. * * @author Liam Fallon (liam.fallon@est.tech) - * @author Chenfei Gao (cgao@research.att.com) */ @Data -public class LegacyGuardPolicy { +public class LegacyGuardPolicyInput { + @SerializedName("policy-id") private String policyId; + @SerializedName("policy-version") private String policyVersion; - private Map content; + private LegacyGuardPolicyContent content; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java new file mode 100644 index 000000000..2fe3d88e4 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyOutput.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.concepts; + +import java.util.Map; + +import lombok.Data; + +/** + * Body of a legacy guard policy output. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@Data +public class LegacyGuardPolicyOutput { + private String type; + private String version; + private Map metadata; + private Map properties; +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java new file mode 100644 index 000000000..141b4e783 --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java @@ -0,0 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.mapping; + +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.ws.rs.core.Response; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies; +import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate; +import org.onap.policy.models.tosca.simple.mapping.ToscaServiceTemplateMapper; +import org.onap.policy.models.tosca.utils.ToscaUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class maps a legacy guard policy to and from a TOSCA service template. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyGuardPolicyMapper + implements ToscaServiceTemplateMapper> { + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class); + + private static final Map GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>(); + + static { + GUARD_POLICY_TYPE_MAP.put("guard.frequency.scaleout", + new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0")); + GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout", + new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); + GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout", + new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0")); + GUARD_POLICY_TYPE_MAP.put("guard.blacklist", + new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0")); + } + + @Override + public ToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) { + PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId()); + if (guardPolicyType == null) { + String errorMessage = + "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + String version = legacyGuardPolicyInput.getPolicyVersion(); + if (version != null) { + version = version + ".0.0"; + } else { + version = guardPolicyType.getVersion(); + } + + PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version); + + final ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey); + toscaPolicy.setType(guardPolicyType); + toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap()); + + final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + serviceTemplate.setToscaDefinitionsVersion("tosca_simimport java.util.HashMap;\n" + "ple_yaml_1_0"); + + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate()); + + serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy); + + return serviceTemplate; + } + + @Override + public Map fromToscaServiceTemplate(final ToscaServiceTemplate serviceTemplate) { + ToscaUtils.assertPoliciesExist(serviceTemplate); + + final Map legacyGuardPolicyOutputMap = new LinkedHashMap<>(); + + for (ToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().values()) { + + final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput(); + legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName()); + legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion()); + + final Map metadata = new LinkedHashMap<>(); + metadata.put("policy-id", toscaPolicy.getKey().getName()); + metadata.put("policy-version", toscaPolicy.getKey().getMajorVersion()); + legacyGuardPolicyOutput.setMetadata(metadata); + + if (toscaPolicy.getProperties() == null) { + String errorMessage = "no properties defined on TOSCA policy"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent(); + // @formatter:off + content.setActor( toscaPolicy.getProperties().get("actor")); + content.setClname( toscaPolicy.getProperties().get("clname")); + content.setGuardActiveEnd( toscaPolicy.getProperties().get("guardActiveEnd")); + content.setGuardActiveStart(toscaPolicy.getProperties().get("guardActiveStart")); + content.setLimit( toscaPolicy.getProperties().get("limit")); + content.setMax( toscaPolicy.getProperties().get("max")); + content.setMin( toscaPolicy.getProperties().get("min")); + content.setRecipe( toscaPolicy.getProperties().get("recipe")); + content.setTargets( toscaPolicy.getProperties().get("targets")); + content.setTimeUnits( toscaPolicy.getProperties().get("timeUnits")); + content.setTimeWindow( toscaPolicy.getProperties().get("timeWindow")); + // @formatter:on + + final Map propertiesMap = new LinkedHashMap<>(); + propertiesMap.put("content", content); + legacyGuardPolicyOutput.setProperties(propertiesMap); + + if (toscaPolicy.getProperties() == null) { + String errorMessage = "property \"Content\" not defined on TOSCA policy"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput); + } + + return legacyGuardPolicyOutputMap; + } +} 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 42343e1df..04a010f08 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 @@ -22,6 +22,7 @@ package org.onap.policy.models.tosca.legacy.provider; import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.ws.rs.core.Response; @@ -30,8 +31,10 @@ import lombok.NonNull; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; +import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper; import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper; import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy; @@ -193,9 +196,24 @@ public class LegacyProvider { * @return the policies found * @throws PfModelException on errors getting policies */ - public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) + public Map getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) throws PfModelException { - return null; + + ToscaPolicy 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 + ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy); + + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); } /** @@ -206,9 +224,15 @@ public class LegacyProvider { * @return the created policy * @throws PfModelException on errors creating policies */ - public LegacyGuardPolicy createGuardPolicy(@NonNull final PfDao dao, - @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { - return null; + public Map createGuardPolicy(@NonNull final PfDao dao, + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + + ToscaServiceTemplate incomingServiceTemplate = + new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy); + ToscaServiceTemplate outgoingingServiceTemplate = + new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); + + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); } /** @@ -219,9 +243,15 @@ public class LegacyProvider { * @return the updated policy * @throws PfModelException on errors updating policies */ - public LegacyGuardPolicy updateGuardPolicy(@NonNull final PfDao dao, - @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { - return null; + public Map updateGuardPolicy(@NonNull final PfDao dao, + @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + + ToscaServiceTemplate incomingServiceTemplate = + new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy); + ToscaServiceTemplate outgoingingServiceTemplate = + new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate); + + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate); } @@ -233,9 +263,39 @@ public class LegacyProvider { * @return the deleted policy * @throws PfModelException on errors deleting policies */ - public LegacyGuardPolicy deleteGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) - throws PfModelException { - return null; + public Map 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 policyList = dao.getAll(ToscaPolicy.class); + + // Find the latest policy that matches the ID + List policyDeleteList = new ArrayList<>(); + + for (ToscaPolicy 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 + ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies()); + + for (ToscaPolicy deletePolicy : policyDeleteList) { + dao.delete(deletePolicy); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), + deletePolicy); + } + + return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); } /** diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java index e9223b350..74b88e790 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java @@ -25,11 +25,9 @@ package org.onap.policy.models.tosca.authorative.mapping; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; -import java.io.IOException; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.PlainToscaServiceTemplate; @@ -43,21 +41,21 @@ import org.yaml.snakeyaml.Yaml; */ public class PlainToscaServiceTemplateMapperTest { - private Gson defaultGson; + private StandardCoder standardCoder; private PlainToscaServiceTemplateMapper mapper; @Before public void setUp() { - defaultGson = new Gson(); + standardCoder = new StandardCoder(); mapper = new PlainToscaServiceTemplateMapper(); } @Test - public void testPlainToscaPolicies() throws JsonSyntaxException, IOException { + public void testPlainToscaPolicies() throws Exception { try { String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"); - PlainToscaServiceTemplate plainPolicies = defaultGson.fromJson(inputJson, PlainToscaServiceTemplate.class); + PlainToscaServiceTemplate plainPolicies = standardCoder.decode(inputJson, PlainToscaServiceTemplate.class); ToscaServiceTemplate internalPolicies = mapper.toToscaServiceTemplate(plainPolicies); assertTrue(internalPolicies.validate(new PfValidationResult()).isValid()); PlainToscaServiceTemplate plainPolicies2 = mapper.fromToscaServiceTemplate(internalPolicies); @@ -69,15 +67,15 @@ public class PlainToscaServiceTemplateMapperTest { } @Test - public void testPlainToscaPolicyTypes() throws JsonSyntaxException, IOException { + public void testPlainToscaPolicyTypes() throws Exception { try { Yaml yaml = new Yaml(); String inputYaml = ResourceUtils.getResourceAsString( "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml"); Object yamlObject = yaml.load(inputYaml); - String yamlAsJsonString = defaultGson.toJson(yamlObject); + String yamlAsJsonString = standardCoder.encode(yamlObject); - PlainToscaServiceTemplate plainPolicyTypes = defaultGson.fromJson(yamlAsJsonString, + PlainToscaServiceTemplate plainPolicyTypes = standardCoder.decode(yamlAsJsonString, PlainToscaServiceTemplate.class); ToscaServiceTemplate internalPolicyTypes = mapper.toToscaServiceTemplate(plainPolicyTypes); assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java index 764ce063f..5720b5594 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java @@ -27,22 +27,24 @@ import java.util.HashMap; import java.util.Map; import org.junit.Test; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; public class LegacyGuardPolicyTest { @Test public void test() { - LegacyGuardPolicy guard = new LegacyGuardPolicy(); + LegacyGuardPolicyInput guard = new LegacyGuardPolicyInput(); assertNotNull(guard); guard.setPolicyId("guard.frequency"); assertEquals("guard.frequency", guard.getPolicyId()); guard.setPolicyVersion("1"); assertEquals("1", guard.getPolicyVersion()); - Map content = new HashMap<>(); - content.put("actor", "SO"); + Map body = new HashMap<>(); + body.put("actor", "SO"); + LegacyGuardPolicyContent content = new LegacyGuardPolicyContent(); + content.setActor("SO"); guard.setContent(content); - assertEquals(1, guard.getContent().size()); + assertEquals("SO", guard.getContent().getActor()); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java new file mode 100644 index 000000000..f35a4e668 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.concepts; + +import com.openpojo.reflection.filters.FilterPackageInfo; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.GetterMustExistRule; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit tests of all pojos. + * + * @author Chenfei Gao (cgao@research.att.com) + * + */ +public class TestPojos { + + private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.legacy.concepts"; + + @Test + public void testPojos() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()) + .with(new SetterMustExistRule()).with(new GetterMustExistRule()).with(new SetterTester()) + .with(new GetterTester()).build(); + validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + } +} 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 new file mode 100644 index 000000000..2fdc3aae7 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java @@ -0,0 +1,295 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.legacy.provider; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; + +/** + * Test the {@link LegacyProvider} class for legacy guard policies. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyProvider4LegacyGuardTest { + private Connection connection; + private PfDao pfDao; + private StandardCoder standardCoder; + + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database + // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance + connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); + + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); + + // Use the persistence unit ToscaConceptTest to test towards the h2 database + // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + /** + * Set up standard coder. + */ + @Before + public void setupStandardCoder() { + standardCoder = new StandardCoder(); + } + + @After + public void teardown() throws Exception { + pfDao.close(); + connection.close(); + } + + @Test + public void testPoliciesGet() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(null, ""); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(pfDao, 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"); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + Map createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); + + assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map gotGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + String expectedJsonOutput = + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json"); + String actualJsonOutput = standardCoder.encode(gotGopm); + + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + } + + @Test + public void testPolicyCreate() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(pfDao, null); + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + Map createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); + + assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map gotGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + String expectedJsonOutput = + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json"); + String actualJsonOutput = standardCoder.encode(gotGopm); + + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + } + + + @Test + public void testPolicyUpdate() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().updateGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().updateGuardPolicy(pfDao, null); + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput()); + }).hasMessage("policy type for guard policy \"null\" unknown"); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + Map createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); + assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map gotGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + originalGip.getContent().setRecipe("Roast Turkey"); + Map updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip); + assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map gotUpdatedGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + assertEquals("Roast Turkey", + gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe()); + } + + + @Test + public void testPoliciesDelete() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(null, ""); + }).hasMessage("dao 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, "I Dont Exist"); + }).hasMessage("no policy found for policy ID: I Dont Exist"); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + Map createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); + assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map gotGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + String expectedJsonOutput = + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json"); + String actualJsonOutput = standardCoder.encode(gotGopm); + + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + + Map deletedGopm = + new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId()); + 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"); + + LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput(); + otherGip.setPolicyId("guard.blacklist"); + otherGip.setPolicyVersion("1"); + otherGip.setContent(new LegacyGuardPolicyContent()); + + Map createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip); + assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next()); + assertEquals(otherGip.getContent(), + 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"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java similarity index 60% rename from models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java rename to models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java index 271e019d9..d198bd4f5 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java @@ -20,11 +20,9 @@ package org.onap.policy.models.tosca.legacy.provider; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import com.google.gson.Gson; import java.sql.Connection; import java.sql.DriverManager; @@ -32,8 +30,8 @@ import java.sql.DriverManager; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.dao.DaoParameters; import org.onap.policy.models.dao.PfDao; import org.onap.policy.models.dao.PfDaoFactory; @@ -41,15 +39,14 @@ import org.onap.policy.models.dao.impl.DefaultPfDao; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; /** - * Test the {@link LegacyProvider} class. + * Test the {@link LegacyProvider} class for legacy operational policies. * * @author Liam Fallon (liam.fallon@est.tech) */ -public class LegacyProviderTest { +public class LegacyProvider4LegacyOperationalTest { private Connection connection; private PfDao pfDao; - private Gson gson; - + private StandardCoder standardCoder; /** * Set up the DAO towards the database. @@ -74,11 +71,11 @@ public class LegacyProviderTest { } /** - * Set up GSON. + * Set up standard coder. */ @Before - public void setupGson() { - gson = new Gson(); + public void setupStandardCoder() { + standardCoder = new StandardCoder(); } @After @@ -88,37 +85,25 @@ public class LegacyProviderTest { } @Test - public void testPoliciesGet() throws PfModelException { - try { + public void testPoliciesGet() throws Exception { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(null, ""); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("policyId is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist"); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: I Dont Exist", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: I Dont Exist"); LegacyOperationalPolicy originalLop = - gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -132,9 +117,9 @@ public class LegacyProviderTest { assertEquals(gotLop, originalLop); String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); - String actualJsonOutput = gson.toJson(gotLop); + String actualJsonOutput = standardCoder.encode(gotLop); - assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); @@ -142,30 +127,21 @@ public class LegacyProviderTest { } @Test - public void testPolicyCreate() throws PfModelException { - try { + public void testPolicyCreate() throws Exception { + assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); LegacyOperationalPolicy originalLop = - gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -179,44 +155,32 @@ public class LegacyProviderTest { assertEquals(gotLop, originalLop); String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); - String actualJsonOutput = gson.toJson(gotLop); + String actualJsonOutput = standardCoder.encode(gotLop); - assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); } @Test - public void testPolicyUpdate() throws PfModelException { - try { + public void testPolicyUpdate() throws Exception { + assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: null", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: null"); LegacyOperationalPolicy originalLop = - gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -239,38 +203,26 @@ public class LegacyProviderTest { @Test - public void testPoliciesDelete() throws PfModelException { - try { + public void testPoliciesDelete() throws Exception { + assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(null, ""); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("policyId is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist"); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: I Dont Exist", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: I Dont Exist"); LegacyOperationalPolicy originalLop = - gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -283,20 +235,17 @@ public class LegacyProviderTest { assertEquals(gotLop, originalLop); String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); - String actualJsonOutput = gson.toJson(gotLop); + String actualJsonOutput = standardCoder.encode(gotLop); - assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); LegacyOperationalPolicy deletedLop = new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId()); - assertEquals(deletedLop, originalLop); + assertEquals(originalLop, deletedLop); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: operational.restart", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: operational.restart"); LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy(); otherLop.setPolicyId("another-policy"); @@ -306,12 +255,9 @@ public class LegacyProviderTest { LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop); assertEquals(otherLop, createdOtherLop); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: operational.restart", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: operational.restart"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java index 5d1fa42ad..c13583178 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java @@ -24,13 +24,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; - -import java.io.IOException; - import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; @@ -48,18 +44,19 @@ public class LegacyOperationalPolicySerializationTest { // Logger for this class private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicySerializationTest.class); - private Gson gson; + private StandardCoder standardCoder; @Before public void setUp() { - gson = new Gson(); + standardCoder = new StandardCoder(); } @Test - public void testJsonDeserialization() throws JsonSyntaxException, IOException { + public void testJsonDeserialization() throws Exception { String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"); - LegacyOperationalPolicy legacyOperationalPolicy = gson.fromJson(vcpePolicyJson, LegacyOperationalPolicy.class); + LegacyOperationalPolicy legacyOperationalPolicy = + standardCoder.decode(vcpePolicyJson, LegacyOperationalPolicy.class); ToscaServiceTemplate serviceTemplate = new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java index 505e90e28..833e06a82 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java @@ -36,6 +36,7 @@ import java.io.IOException; import java.util.Map; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfValidationResult; @@ -129,12 +130,12 @@ public class MonitoringPolicySerializationTest { } private ToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) - throws JsonSyntaxException, IOException { + throws Exception { Yaml yaml = new Yaml(); String policyYaml = ResourceUtils.getResourceAsString(resourcePath); Object yamlObject = yaml.load(policyYaml); - String yamlAsJsonString = new Gson().toJson(yamlObject); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class); return serviceTemplate; } -- 2.16.6