diff options
Diffstat (limited to 'models-provider')
15 files changed, 396 insertions, 259 deletions
diff --git a/models-provider/pom.xml b/models-provider/pom.xml index fb0e302dd..41a5a2cfe 100644 --- a/models-provider/pom.xml +++ b/models-provider/pom.xml @@ -69,12 +69,6 @@ </dependency> <dependency> - <groupId>org.mariadb.jdbc</groupId> - <artifactId>mariadb-java-client</artifactId> - <scope>test</scope> - </dependency> - - <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <scope>test</scope> 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 cf40a57f9..a7d414533 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 @@ -278,11 +278,10 @@ public interface PolicyModelsProvider extends AutoCloseable { * Get PDP groups. * * @param name the name of the policy to get, null to get all PDP groups - * @param version the version of the policy to get, null to get all versions of a PDP group * @return the PDP groups found * @throws PfModelException on errors getting PDP groups */ - public List<PdpGroup> getPdpGroups(final String name, final String version) throws PfModelException; + public List<PdpGroup> getPdpGroups(final String name) throws PfModelException; /** * Get filtered PDP groups. @@ -315,55 +314,50 @@ public interface PolicyModelsProvider extends AutoCloseable { * Update a PDP subgroup. * * @param pdpGroupName the name of the PDP group of the PDP subgroup - * @param pdpGroupVersion the version of the PDP group of the PDP subgroup * @param pdpSubGroup the PDP subgroup to be updated * @throws PfModelException on errors updating PDP subgroups */ - public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion, - @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException; + public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup) + throws PfModelException; /** * Update a PDP. * * @param pdpGroupName the name of the PDP group of the PDP subgroup - * @param pdpGroupVersion the version of the PDP group of the PDP subgroup * @param pdpSubGroup the PDP subgroup to be updated * @param pdp the PDP to be updated * @throws PfModelException on errors updating PDP subgroups */ - public void updatePdp(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion, - @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) throws PfModelException; + public void updatePdp(@NonNull final String pdpGroupName, @NonNull final String pdpSubGroup, @NonNull final Pdp pdp) + throws PfModelException; /** * Delete a PDP group. * * @param name the name of the policy to get, null to get all PDP groups - * @param version the version of the policy to get, null to get all versions of a PDP group * @return the PDP group deleted * @throws PfModelException on errors deleting PDP groups */ - public PdpGroup deletePdpGroup(@NonNull final String name, @NonNull final String version) throws PfModelException; + public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException; /** * Get PDP statistics. * * @param name the name of the PDP group to get statistics for, null to get all PDP groups - * @param version the version of the PDP group to get statistics for, null to get all versions of a PDP group * @return the statistics found * @throws PfModelException on errors getting statistics */ - public List<PdpStatistics> getPdpStatistics(final String name, final String version) throws PfModelException; + public List<PdpStatistics> getPdpStatistics(final String name) throws PfModelException; /** * Update PDP statistics for a PDP. * * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for - * @param pdpGroupVersion the version of the PDP group containing the PDP that the statistics are for * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for * @param pdpInstanceId the instance ID of the PDP to update statistics for + * @param pdpStatistics the PDP statistics * @throws PfModelException on errors updating statistics */ - public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion, - @NonNull final String pdpType, @NonNull final String pdpInstanceId, - @NonNull final PdpStatistics pdppStatistics) throws PfModelException; + public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpType, + @NonNull final String pdpInstanceId, @NonNull final PdpStatistics pdpStatistics) throws PfModelException; } diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java index 5abafed22..65aa72eb2 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java @@ -55,6 +55,7 @@ public class PolicyModelsProviderParameters implements ParameterGroup { private String name; private String implementation = DEFAULT_IMPLEMENTATION; + private String databaseDriver; private String databaseUrl; private String databaseUser; private String databasePassword; @@ -73,6 +74,11 @@ public class PolicyModelsProviderParameters implements ParameterGroup { "a PolicyModelsProvider implementation must be specified"); } + if (!ParameterValidationUtils.validateStringParameter(databaseDriver)) { + validationResult.setResult("databaseUrl", ValidationStatus.INVALID, + "a driver must be specified for the JDBC connection to the database"); + } + if (!ParameterValidationUtils.validateStringParameter(databaseUrl)) { validationResult.setResult("databaseUrl", ValidationStatus.INVALID, "a URL must be specified for the JDBC connection to the database"); 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 2fe52e935..a6e8f325b 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 @@ -20,16 +20,16 @@ package org.onap.policy.models.provider.impl; -import java.sql.Connection; -import java.sql.DriverManager; import java.util.Base64; import java.util.List; import java.util.Map; +import java.util.Properties; import javax.ws.rs.core.Response; import lombok.NonNull; +import org.eclipse.persistence.config.PersistenceUnitProperties; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.DaoParameters; @@ -64,12 +64,12 @@ import org.slf4j.LoggerFactory; * @author Liam Fallon (liam.fallon@est.tech) */ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class); private final PolicyModelsProviderParameters parameters; // Database connection and the DAO for reading and writing Policy Framework concepts - private Connection connection; private PfDao pfDao; /** @@ -86,30 +86,31 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(), parameters.getPersistenceUnit()); - if (connection != null || pfDao != null) { + if (pfDao != null) { String errorMessage = "provider is already initialized"; LOGGER.warn(errorMessage); throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage); } - // Decode the password using Base64 - String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword())); - - // Connect to the database, call does not implement AutoCloseable for try-with-resources - try { - connection = DriverManager.getConnection(parameters.getDatabaseUrl(), parameters.getDatabaseUser(), - decodedPassword); - } catch (Exception exc) { - String errorMessage = "could not connect to database with URL \"" + parameters.getDatabaseUrl() + "\""; - LOGGER.warn(errorMessage, exc); - throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc); - } - // Parameters for the DAO final DaoParameters daoParameters = new DaoParameters(); daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); daoParameters.setPersistenceUnit(parameters.getPersistenceUnit()); + // Decode the password using Base64 + String decodedPassword = new String(Base64.getDecoder().decode(parameters.getDatabasePassword())); + + // @formatter:off + Properties jdbcProperties = new Properties(); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, parameters.getDatabaseDriver()); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, parameters.getDatabaseUrl()); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, parameters.getDatabaseUser()); + jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, decodedPassword); + // @formatter:on + + daoParameters.setJdbcProperties(jdbcProperties); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); try { pfDao = new PfDaoFactory().createPfDao(daoParameters); pfDao.init(daoParameters); @@ -133,20 +134,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { pfDao = null; } - if (connection != null) { - try { - connection.close(); - } catch (Exception exc) { - - String errorMessage = - "could not close connection to database with URL \"" + parameters.getDatabaseUrl() + "\""; - LOGGER.warn(errorMessage, exc); - throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc); - } finally { - connection = null; - } - } - LOGGER.debug("closed the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(), parameters.getPersistenceUnit()); } @@ -297,9 +284,9 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { } @Override - public List<PdpGroup> getPdpGroups(final String name, final String version) throws PfModelException { + public List<PdpGroup> getPdpGroups(final String name) throws PfModelException { assertInitilized(); - return new PdpProvider().getPdpGroups(pfDao, name, version); + return new PdpProvider().getPdpGroups(pfDao, name); } @Override @@ -321,37 +308,36 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { } @Override - public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion, - @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException { + public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup) + throws PfModelException { assertInitilized(); - new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup); + new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup); } @Override - public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion, - @NonNull String pdpSubGroup, @NonNull Pdp pdp) throws PfModelException { - new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpGroupVersion, pdpSubGroup, pdp); + public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp) + throws PfModelException { + new PdpProvider().updatePdp(pfDao, pdpGroupName, pdpSubGroup, pdp); } @Override - public PdpGroup deletePdpGroup(@NonNull final String name, @NonNull final String version) throws PfModelException { + public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException { assertInitilized(); - return new PdpProvider().deletePdpGroup(pfDao, name, version); + return new PdpProvider().deletePdpGroup(pfDao, name); } @Override - public List<PdpStatistics> getPdpStatistics(final String name, final String version) throws PfModelException { + public List<PdpStatistics> getPdpStatistics(final String name) throws PfModelException { assertInitilized(); - return new PdpProvider().getPdpStatistics(pfDao, name, version); + return new PdpProvider().getPdpStatistics(pfDao, name); } @Override - public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion, - @NonNull final String pdpType, @NonNull final String pdpInstanceId, - @NonNull final PdpStatistics pdppStatistics) throws PfModelException { + public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpType, + @NonNull final String pdpInstanceId, @NonNull final PdpStatistics pdpStatistics) throws PfModelException { assertInitilized(); - new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpGroupVersion, pdpType, pdpInstanceId, - pdppStatistics); + new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpType, pdpInstanceId, + pdpStatistics); } /** @@ -364,13 +350,4 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); } } - - /** - * Hook for unit test mocking of database connection. - * - * @param client the mocked client - */ - protected void setConnection(final Connection connection) { - this.connection = connection; - } } 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 0bf529730..9b1ca7669 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 @@ -27,7 +27,6 @@ import java.util.List; import java.util.Map; import javax.ws.rs.core.Response; -import lombok.NonNull; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; @@ -61,7 +60,7 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { * * @param parameters the parameters for the provider */ - public DummyPolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {} + public DummyPolicyModelsProviderImpl(final PolicyModelsProviderParameters parameters) {} @Override public void init() throws PfModelException { @@ -84,30 +83,27 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { } @Override - public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException { - return null; + public ToscaServiceTemplate getFilteredPolicyTypes(ToscaPolicyTypeFilter filter) throws PfModelException { + return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json"); } @Override - public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter) { + public List<ToscaPolicyType> getFilteredPolicyTypeList(ToscaPolicyTypeFilter filter) { return new ArrayList<>(); } @Override - public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate) - throws PfModelException { + public ToscaServiceTemplate createPolicyTypes(final ToscaServiceTemplate serviceTemplate) throws PfModelException { return serviceTemplate; } @Override - public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate) - throws PfModelException { + public ToscaServiceTemplate updatePolicyTypes(final ToscaServiceTemplate serviceTemplate) throws PfModelException { return serviceTemplate; } @Override - public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version) - throws PfModelException { + public ToscaServiceTemplate deletePolicyType(final String name, final String version) throws PfModelException { return getDummyResponse("dummyimpl/DummyToscaPolicyTypeDeleteResponse.json"); } @@ -122,125 +118,118 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { } @Override - public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException { - return null; + public ToscaServiceTemplate getFilteredPolicies(ToscaPolicyFilter filter) throws PfModelException { + return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json"); } @Override - public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException { + public List<ToscaPolicy> getFilteredPolicyList(ToscaPolicyFilter filter) throws PfModelException { return new ArrayList<>(); } @Override - public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate) - throws PfModelException { + public ToscaServiceTemplate createPolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException { return serviceTemplate; } @Override - public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate) - throws PfModelException { + public ToscaServiceTemplate updatePolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException { return serviceTemplate; } @Override - public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version) - throws PfModelException { + public ToscaServiceTemplate deletePolicy(final String name, final String version) throws PfModelException { return getDummyResponse("dummyimpl/DummyToscaPolicyDeleteResponse.json"); } @Override - public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException { + public LegacyOperationalPolicy getOperationalPolicy(final String policyId) throws PfModelException { return new LegacyOperationalPolicy(); } @Override - public LegacyOperationalPolicy createOperationalPolicy( - @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { + public LegacyOperationalPolicy createOperationalPolicy(final LegacyOperationalPolicy legacyOperationalPolicy) + throws PfModelException { return legacyOperationalPolicy; } @Override - public LegacyOperationalPolicy updateOperationalPolicy( - @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { + public LegacyOperationalPolicy updateOperationalPolicy(final LegacyOperationalPolicy legacyOperationalPolicy) + throws PfModelException { return legacyOperationalPolicy; } @Override - public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException { + public LegacyOperationalPolicy deleteOperationalPolicy(final String policyId) throws PfModelException { return new LegacyOperationalPolicy(); } @Override - public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(final String policyId) throws PfModelException { return new HashMap<>(); } @Override - public Map<String, LegacyGuardPolicyOutput> createGuardPolicy( - @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(final LegacyGuardPolicyInput legacyGuardPolicy) + throws PfModelException { return new HashMap<>(); } @Override - public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy( - @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(final LegacyGuardPolicyInput legacyGuardPolicy) + throws PfModelException { return new HashMap<>(); } @Override - public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId) - throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(final String policyId) throws PfModelException { return new HashMap<>(); } @Override - public List<PdpGroup> getPdpGroups(final String name, final String version) throws PfModelException { + public List<PdpGroup> getPdpGroups(final String name) throws PfModelException { return new ArrayList<>(); } @Override - public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException { + public List<PdpGroup> getFilteredPdpGroups(PdpGroupFilter filter) throws PfModelException { return new ArrayList<>(); } @Override - public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException { + public List<PdpGroup> createPdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException { return new ArrayList<>(); } @Override - public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException { + public List<PdpGroup> updatePdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException { return new ArrayList<>(); } @Override - public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion, - @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException { + public void updatePdpSubGroup(final String pdpGroupName, final PdpSubGroup pdpSubGroup) throws PfModelException { // Not implemented } @Override - public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion, - @NonNull String pdpSubGroup, @NonNull Pdp pdp) throws PfModelException { + public void updatePdp(String pdpGroupName, String pdpSubGroup, Pdp pdp) throws PfModelException { // Not implemented } @Override - public PdpGroup deletePdpGroup(@NonNull final String name, @NonNull final String version) throws PfModelException { + public PdpGroup deletePdpGroup(final String name) throws PfModelException { return null; } @Override - public List<PdpStatistics> getPdpStatistics(final String name, final String version) throws PfModelException { + public List<PdpStatistics> getPdpStatistics(final String name) throws PfModelException { return new ArrayList<>(); } @Override - public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion, - @NonNull final String pdpType, @NonNull final String pdpInstanceId, - @NonNull final PdpStatistics pdppStatistics) throws PfModelException { + public void updatePdpStatistics(final String pdpGroupName, final String pdpType, final String pdpInstanceId, + final PdpStatistics pdppStatistics) throws PfModelException { // Not implemented } @@ -250,7 +239,7 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { * @param fileName the file name containing the dummy response * @return the ToscaServiceTemplate with the dummy response */ - protected ToscaServiceTemplate getDummyResponse(@NonNull final String fileName) { + protected ToscaServiceTemplate getDummyResponse(final String fileName) { StandardCoder standardCoder = new StandardCoder(); ToscaServiceTemplate serviceTemplate; diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderParametersTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderParametersTest.java index 626d2bf5d..51771ff9a 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderParametersTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderParametersTest.java @@ -36,6 +36,7 @@ public class PolicyModelsProviderParametersTest { @Test public void testParameters() { PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + pars.setDatabaseDriver("MichaelsShumacher"); pars.setDatabaseUrl("jdbc://www.acmecorp/roadrunner"); pars.setPersistenceUnit("WileECoyote"); @@ -62,5 +63,12 @@ public class PolicyModelsProviderParametersTest { pars.setPersistenceUnit("WileECoyote"); result = pars.validate(); assertTrue(result.isValid()); + + pars.setDatabaseDriver(null); + result = pars.validate(); + assertFalse(result.isValid()); + pars.setDatabaseDriver("MichaelsShumacher"); + result = pars.validate(); + assertTrue(result.isValid()); } } 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 38a5ae114..ccdf45af4 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 @@ -27,13 +27,23 @@ import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Base64; +import java.util.List; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroupFilter; +import org.onap.policy.models.pdp.concepts.PdpStatistics; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpState; 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.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; @@ -56,41 +66,39 @@ public class DatabasePolicyModelsProviderTest { @Before public void setupParameters() { parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); parameters.setDatabaseUser("policy"); parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); parameters.setPersistenceUnit("ToscaConceptTest"); - } @Test public void testInitAndClose() throws Exception { + assertThatThrownBy(() -> { + new DatabasePolicyModelsProviderImpl(null); + }).hasMessage("parameters is marked @NonNull but is null"); + PolicyModelsProvider databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist"); - assertThatThrownBy(() -> { - databaseProvider.close(); - databaseProvider.init(); - }).hasMessage("could not connect to database with URL \"jdbc://www.acmecorp.nonexist\""); - - parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); - try { - databaseProvider.init(); databaseProvider.close(); + databaseProvider.init(); } catch (Exception pfme) { fail("test shold not throw an exception here"); } + databaseProvider.close(); + + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); parameters.setPersistenceUnit("WileECoyote"); - String errorMessage = "could not create Data Access Object (DAO) using url " - + "\"jdbc:h2:mem:testdb\" and persistence unit \"WileECoyote\""; assertThatThrownBy(() -> { databaseProvider.init(); - }).hasMessage(errorMessage); + }).hasMessageContaining("could not create Data Access Object (DAO)"); parameters.setPersistenceUnit("ToscaConceptTest"); @@ -98,40 +106,40 @@ public class DatabasePolicyModelsProviderTest { databaseProvider.init(); databaseProvider.close(); } catch (Exception pfme) { + pfme.printStackTrace(); fail("test shold not throw an exception here"); } + assertThatThrownBy(() -> { + databaseProvider.init(); + databaseProvider.init(); + }).hasMessage("provider is already initialized"); + try { databaseProvider.close(); } catch (Exception pfme) { fail("test shold not throw an exception here"); } - assertThatThrownBy(() -> { - DatabasePolicyModelsProviderImpl databaseProviderImpl = (DatabasePolicyModelsProviderImpl) databaseProvider; - databaseProvider.init(); - databaseProviderImpl.setConnection(new DummyConnection()); + try { databaseProvider.close(); - }).hasMessage("could not close connection to database with URL \"jdbc:h2:mem:testdb\""); + } catch (Exception pfme) { + fail("test shold not throw an exception here"); + } } - @Ignore @Test public void testProviderMethodsNull() throws Exception { PolicyModelsProvider databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); assertThatThrownBy(() -> { - databaseProvider.getPolicyTypes(null, null); - }).hasMessage("name is marked @NonNull but is null"); - - assertThatThrownBy(() -> { - databaseProvider.getPolicyTypes("aaa", null); - }).hasMessage("version is marked @NonNull but is null"); + databaseProvider.getFilteredPolicyTypes(null); + }).hasMessage("filter is marked @NonNull but is null"); assertThatThrownBy(() -> { - databaseProvider.getPolicyTypes(null, "aaa"); - }).hasMessage("name is marked @NonNull but is null"); + databaseProvider.getFilteredPolicyTypeList(null); + }).hasMessage("filter is marked @NonNull but is null"); assertThatThrownBy(() -> { databaseProvider.createPolicyTypes(null); @@ -154,16 +162,12 @@ public class DatabasePolicyModelsProviderTest { }).hasMessage("name is marked @NonNull but is null"); assertThatThrownBy(() -> { - databaseProvider.getPolicies(null, null); - }).hasMessage("name is marked @NonNull but is null"); + databaseProvider.getFilteredPolicies(null); + }).hasMessage("filter is marked @NonNull but is null"); assertThatThrownBy(() -> { - databaseProvider.getPolicies(null, "aaa"); - }).hasMessage("name is marked @NonNull but is null"); - - assertThatThrownBy(() -> { - databaseProvider.getPolicies("aaa", null); - }).hasMessage("version is marked @NonNull but is null"); + databaseProvider.getFilteredPolicyList(null); + }).hasMessage("filter is marked @NonNull but is null"); assertThatThrownBy(() -> { databaseProvider.createPolicies(null); @@ -218,6 +222,10 @@ public class DatabasePolicyModelsProviderTest { }).hasMessage("policyId is marked @NonNull but is null"); assertThatThrownBy(() -> { + databaseProvider.getFilteredPdpGroups(null); + }).hasMessage("filter is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createPdpGroups(null); }).hasMessage("pdpGroups is marked @NonNull but is null"); @@ -226,11 +234,120 @@ public class DatabasePolicyModelsProviderTest { }).hasMessage("pdpGroups is marked @NonNull but is null"); assertThatThrownBy(() -> { - databaseProvider.deletePdpGroup(null, null); + databaseProvider.updatePdpSubGroup(null, null); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(null, new PdpSubGroup()); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup("name", null); + }).hasMessage("pdpSubGroup is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup("name", new PdpSubGroup()); + }).hasMessage("parameter \"localName\" is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, null, null); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, null, new Pdp()); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, "sub", null); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, "sub", new Pdp()); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp("name", null, null); + }).hasMessage("pdpSubGroup is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp("name", null, new Pdp()); + }).hasMessage("pdpSubGroup is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp("name", "sub", null); + }).hasMessage("pdp is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp("name", "sub", new Pdp()); + }).hasMessage("parameter \"localName\" is null"); + + assertThatThrownBy(() -> { + databaseProvider.deletePdpGroup(null); }).hasMessage("name is marked @NonNull but is null"); - databaseProvider.close(); + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null, null, null, null); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null, null, null, new PdpStatistics()); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null, null, "Instance", null); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null, null, "Instance", new PdpStatistics()); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null, "type", null, null); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null, "type", null, new PdpStatistics()); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null, "type", "Instance", null); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null, "type", "Instance", new PdpStatistics()); + }).hasMessage("pdpGroupName is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics("name", null, null, null); + }).hasMessage("pdpType is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics("name", null, null, new PdpStatistics()); + }).hasMessage("pdpType is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics("name", null, "Instance", null); + }).hasMessage("pdpType is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics("name", null, "Instance", new PdpStatistics()); + }).hasMessage("pdpType is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics("name", "type", null, null); + }).hasMessage("pdpInstanceId is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics("name", "type", null, new PdpStatistics()); + }).hasMessage("pdpInstanceId is marked @NonNull but is null"); + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics("name", "type", "Instance", null); + }).hasMessage("pdpStatistics is marked @NonNull but is null"); + + databaseProvider.updatePdpStatistics("name", "type", "Instance", new PdpStatistics()); + + databaseProvider.close(); } @Test @@ -245,15 +362,16 @@ public class DatabasePolicyModelsProviderTest { }).hasMessage("policy models provider is not initilaized"); } - @Ignore @Test public void testProviderMethods() { try (PolicyModelsProvider databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { - assertThatThrownBy(() -> { - databaseProvider.getPolicyTypes("name", "version"); - }).hasMessage("policy type not found: name:version"); + assertEquals(0, databaseProvider.getPolicyTypes("name", "version").getPolicyTypes().get(0).size()); + assertEquals(0, databaseProvider.getPolicyTypeList("name", "version").size()); + assertEquals(0, databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build()) + .getPolicyTypes().get(0).size()); + assertEquals(0, databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).size()); assertThatThrownBy(() -> { databaseProvider.createPolicyTypes(new ToscaServiceTemplate()); @@ -263,13 +381,16 @@ public class DatabasePolicyModelsProviderTest { databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()); }).hasMessage("no policy types specified on service template"); - assertThatThrownBy(() -> { - databaseProvider.deletePolicyType("name", "version"); - }).hasMessage("policy type not found: name:version"); + assertEquals(0, databaseProvider.deletePolicyType("name", "version").getPolicyTypes().get(0).size()); - assertThatThrownBy(() -> { - databaseProvider.getPolicies("name", "version"); - }).hasMessage("policy not found: name:version"); + assertEquals(0, databaseProvider.deletePolicyType("name", "version").getPolicyTypes().get(0).size()); + + assertEquals(0, databaseProvider.getPolicies("name", "version").getToscaTopologyTemplate().getPolicies() + .get(0).size()); + assertEquals(0, databaseProvider.getPolicyList("name", "version").size()); + assertEquals(0, databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()) + .getToscaTopologyTemplate().getPolicies().get(0).size()); + assertEquals(0, databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).size()); assertThatThrownBy(() -> { databaseProvider.createPolicies(new ToscaServiceTemplate()); @@ -279,9 +400,8 @@ public class DatabasePolicyModelsProviderTest { databaseProvider.updatePolicies(new ToscaServiceTemplate()); }).hasMessage("topology template not specified on service template"); - assertThatThrownBy(() -> { - databaseProvider.deletePolicy("name", "version"); - }).hasMessage("policy not found: name:version"); + assertEquals(0, databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate().getPolicies() + .get(0).size()); assertThatThrownBy(() -> { databaseProvider.getOperationalPolicy("policy_id"); @@ -315,15 +435,60 @@ public class DatabasePolicyModelsProviderTest { databaseProvider.deleteGuardPolicy("policy_id"); }).hasMessage("no policy found for policy ID: policy_id"); - assertEquals(0, databaseProvider.getPdpGroups("name", "version").size()); + assertEquals(0, databaseProvider.getPdpGroups("name").size()); + assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName("group"); + pdpGroup.setVersion("1.2.3"); + pdpGroup.setPdpGroupState(PdpState.ACTIVE); + pdpGroup.setPdpSubgroups(new ArrayList<>()); + List<PdpGroup> groupList = new ArrayList<>(); + groupList.add(pdpGroup); + + PdpSubGroup pdpSubGroup = new PdpSubGroup(); + pdpSubGroup.setPdpType("type"); + pdpSubGroup.setDesiredInstanceCount(123); + pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9")); + pdpGroup.getPdpSubgroups().add(pdpSubGroup); + + Pdp pdp = new Pdp(); + pdp.setInstanceId("type-0"); + pdp.setMessage("Hello"); + pdp.setPdpState(PdpState.ACTIVE); + pdp.setHealthy(PdpHealthStatus.UNKNOWN); + pdpSubGroup.setPdpInstances(new ArrayList<>()); + pdpSubGroup.getPdpInstances().add(pdp); + + assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getDesiredInstanceCount()); + assertEquals(1, databaseProvider.getPdpGroups("group").size()); + + pdpSubGroup.setDesiredInstanceCount(234); + databaseProvider.updatePdpSubGroup("group", pdpSubGroup); + assertEquals(234, databaseProvider.getPdpGroups("group").get(0).getPdpSubgroups() + .get(0).getDesiredInstanceCount()); + + assertEquals("Hello", databaseProvider.getPdpGroups("group").get(0).getPdpSubgroups() + .get(0).getPdpInstances().get(0).getMessage()); + pdp.setMessage("Howdy"); + databaseProvider.updatePdp("group", "type", pdp); + assertEquals("Howdy", databaseProvider.getPdpGroups("group").get(0).getPdpSubgroups() + .get(0).getPdpInstances().get(0).getMessage()); + assertThatThrownBy(() -> { - databaseProvider.deletePdpGroup("name", "version"); - }).hasMessage("delete of PDP group \"name:version\" failed, PDP group does not exist"); + databaseProvider.deletePdpGroup("name"); + }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist"); + + assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup("group").getName()); + + assertEquals(0, databaseProvider.getPdpStatistics(null).size()); + databaseProvider.updatePdpStatistics("group", "type", "type-0", new PdpStatistics()); } catch (Exception exc) { LOGGER.warn("test should not throw an exception", exc); fail("test should not throw an exception"); 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 61f88741c..3e182c02d 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 @@ -147,7 +147,7 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { } @Override - public List<PdpGroup> getPdpGroups(String name, String version) throws PfModelException { + public List<PdpGroup> getPdpGroups(String name) throws PfModelException { return null; } @@ -162,11 +162,11 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { } @Override - public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion, @NonNull String pdpSubGroup, + public void updatePdp(@NonNull String pdpGroupName, @NonNull String pdpSubGroup, @NonNull Pdp pdp) throws PfModelException {} @Override - public PdpGroup deletePdpGroup(@NonNull String name, @NonNull String verison) throws PfModelException { + public PdpGroup deletePdpGroup(@NonNull String name) throws PfModelException { return null; } @@ -207,15 +207,15 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { } @Override - public void updatePdpSubGroup(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion, + public void updatePdpSubGroup(@NonNull String pdpGroupName, @NonNull PdpSubGroup pdpSubGroup) throws PfModelException {} @Override - public List<PdpStatistics> getPdpStatistics(String name, String version) throws PfModelException { + public List<PdpStatistics> getPdpStatistics(String name) throws PfModelException { return null; } @Override - public void updatePdpStatistics(@NonNull String pdpGroupName, @NonNull String pdpGroupVersion, + public void updatePdpStatistics(@NonNull String pdpGroupName, @NonNull String pdpType, @NonNull String pdpInstanceId, @NonNull PdpStatistics pdppStatistics) {} } 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 01da44924..9f02fd7a4 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,7 +20,6 @@ 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.assertNull; @@ -30,9 +29,15 @@ import static org.junit.Assert.fail; import java.util.ArrayList; import org.junit.Test; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroupFilter; +import org.onap.policy.models.pdp.concepts.PdpStatistics; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; 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.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; @@ -74,11 +79,17 @@ public class DummyPolicyModelsProviderTest { dummyProvider.init(); assertNotNull(dummyProvider.getPolicyTypes("name", "version")); + assertNotNull(dummyProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())); + assertNotNull(dummyProvider.getPolicyTypeList("name", "version")); + assertNotNull(dummyProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build())); assertNotNull(dummyProvider.createPolicyTypes(new ToscaServiceTemplate())); assertNotNull(dummyProvider.updatePolicyTypes(new ToscaServiceTemplate())); assertNotNull(dummyProvider.deletePolicyType("name", "version")); assertNotNull(dummyProvider.getPolicies("name", "version")); + assertNotNull(dummyProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())); + assertNotNull(dummyProvider.getPolicyList("name", "version")); + assertNotNull(dummyProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build())); assertNotNull(dummyProvider.createPolicies(new ToscaServiceTemplate())); assertNotNull(dummyProvider.updatePolicies(new ToscaServiceTemplate())); assertNotNull(dummyProvider.deletePolicy("name", "version")); @@ -93,47 +104,16 @@ public class DummyPolicyModelsProviderTest { assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput())); assertNotNull(dummyProvider.deleteGuardPolicy("policy_id")); - assertTrue(dummyProvider.getPdpGroups("name", "version").isEmpty()); + assertTrue(dummyProvider.getPdpGroups("name").isEmpty()); + assertTrue(dummyProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).isEmpty()); assertTrue(dummyProvider.createPdpGroups(new ArrayList<>()).isEmpty()); assertTrue(dummyProvider.updatePdpGroups(new ArrayList<>()).isEmpty()); - assertNull(dummyProvider.deletePdpGroup("name", "version")); - - - assertThatThrownBy(() -> { - dummyProvider.getOperationalPolicy(null); - }).hasMessage("policyId is marked @NonNull but is null"); - assertThatThrownBy(() -> { - dummyProvider.createOperationalPolicy(null); - }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); - assertThatThrownBy(() -> { - dummyProvider.updateOperationalPolicy(null); - }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); - assertThatThrownBy(() -> { - dummyProvider.deleteOperationalPolicy(null); - }).hasMessage("policyId is marked @NonNull but is null"); - - assertThatThrownBy(() -> { - dummyProvider.getGuardPolicy(null); - }).hasMessage("policyId is marked @NonNull but is null"); - assertThatThrownBy(() -> { - dummyProvider.createGuardPolicy(null); - }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); - assertThatThrownBy(() -> { - dummyProvider.updateGuardPolicy(null); - }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); - assertThatThrownBy(() -> { - dummyProvider.deleteGuardPolicy(null); - }).hasMessage("policyId is marked @NonNull but is null"); - - assertThatThrownBy(() -> { - dummyProvider.createPdpGroups(null); - }).hasMessage("pdpGroups is marked @NonNull but is null"); - assertThatThrownBy(() -> { - dummyProvider.updatePdpGroups(null); - }).hasMessage("pdpGroups is marked @NonNull but is null"); - assertThatThrownBy(() -> { - dummyProvider.deletePdpGroup(null, null); - }).hasMessage("name is marked @NonNull but is null"); + assertNull(dummyProvider.deletePdpGroup("name")); + + dummyProvider.updatePdpSubGroup("name", new PdpSubGroup()); + dummyProvider.updatePdp("name", "type", new Pdp()); + dummyProvider.updatePdpStatistics("name", "type", "type-0", new PdpStatistics()); + assertTrue(dummyProvider.getPdpStatistics("name").isEmpty()); dummyProvider.close(); } @@ -159,7 +139,7 @@ public class DummyPolicyModelsProviderTest { resp.getBadDummyResponse2(); fail("test should throw an exception"); } catch (Exception npe) { - assertEquals("fileName is marked @NonNull but is null", npe.getMessage()); + assertEquals("error serializing object", npe.getMessage()); } finally { if (resp != null) { resp.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 index 47f099a73..741ae8998 100644 --- 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 @@ -76,6 +76,7 @@ public class PolicyLegacyGuardPersistenceTest { @Before public void setupParameters() throws PfModelException { PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); parameters.setDatabaseUser("policy"); parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); @@ -134,6 +135,16 @@ public class PolicyLegacyGuardPersistenceTest { assertEquals(gip.getContent(), gotGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + Map<String, LegacyGuardPolicyOutput> updatedGopm = databaseProvider.updateGuardPolicy(gip); + assertEquals(gip.getPolicyId(), updatedGopm.keySet().iterator().next()); + assertEquals(gip.getContent(), + updatedGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + + Map<String, LegacyGuardPolicyOutput> deletedGopm = databaseProvider.deleteGuardPolicy(gip.getPolicyId()); + assertEquals(gip.getPolicyId(), deletedGopm.keySet().iterator().next()); + assertEquals(gip.getContent(), + deletedGopm.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 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 65e6a2f54..2e33a11a0 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 @@ -76,6 +76,7 @@ public class PolicyLegacyOperationalPersistenceTest { @Before public void setupParameters() throws PfModelException { PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); parameters.setDatabaseUser("policy"); parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); @@ -130,6 +131,12 @@ public class PolicyLegacyOperationalPersistenceTest { LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId()); assertEquals(gotLop, lop); + LegacyOperationalPolicy updatedLop = databaseProvider.updateOperationalPolicy(lop); + assertEquals(gotLop, updatedLop); + + LegacyOperationalPolicy deletedLop = databaseProvider.deleteOperationalPolicy(lop.getPolicyId()); + assertEquals(gotLop, deletedLop); + String actualRetrievedJson = standardCoder.encode(gotLop); // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java index 468d4d74b..668f63497 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyPersistenceTest.java @@ -41,6 +41,7 @@ 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.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,6 +85,7 @@ public class PolicyPersistenceTest { @Before public void setupParameters() throws PfModelException { PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); parameters.setDatabaseUser("policy"); parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); @@ -142,13 +144,25 @@ public class PolicyPersistenceTest { assertNotNull(serviceTemplate); databaseProvider.createPolicies(serviceTemplate); + databaseProvider.updatePolicies(serviceTemplate); for (Map<String, ToscaPolicy> policyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) { for (ToscaPolicy policy : policyMap.values()) { - ToscaServiceTemplate goToscaServiceTemplate = + ToscaServiceTemplate gotToscaServiceTemplate = databaseProvider.getPolicies(policy.getName(), policy.getVersion()); - assertEquals(goToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) + assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) + .get(policy.getName()).getType(), policy.getType()); + + gotToscaServiceTemplate = databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build()); + + assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) + .get(policy.getName()).getType(), policy.getType()); + + gotToscaServiceTemplate = databaseProvider.getFilteredPolicies( + ToscaPolicyFilter.builder().name(policy.getName()).version(policy.getVersion()).build()); + + assertEquals(gotToscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0) .get(policy.getName()).getType(), policy.getType()); } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java index 8f05244b9..613c5a2c6 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java @@ -83,6 +83,7 @@ public class PolicyToscaPersistenceTest { @Before public void setupParameters() throws PfModelException { PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); parameters.setDatabaseUser("policy"); parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java index 7f90a0b58..7b541e128 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyTypePersistenceTest.java @@ -41,6 +41,7 @@ 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.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,6 +84,7 @@ public class PolicyTypePersistenceTest { @Before public void setupParameters() throws PfModelException { PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); parameters.setDatabaseUser("policy"); parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); @@ -105,7 +107,7 @@ public class PolicyTypePersistenceTest { } @Test - public void testPolicyPersistence() { + public void testPolicyTypePersistence() { try { for (String policyTypeResourceName : policyTypeResourceNames) { String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName); @@ -117,7 +119,7 @@ public class PolicyTypePersistenceTest { } } } catch (Exception exc) { - LOGGER.warn("error processing policies", exc); + LOGGER.warn("error processing policy types", exc); fail("test should not throw an exception"); } } @@ -142,11 +144,29 @@ public class PolicyTypePersistenceTest { ToscaPolicyType inPolicyType = serviceTemplate.getPolicyTypes().get(0).values().iterator().next(); databaseProvider.createPolicyTypes(serviceTemplate); + databaseProvider.updatePolicyTypes(serviceTemplate); List<ToscaPolicyType> policyTypeList = databaseProvider.getPolicyTypeList(inPolicyType.getName(), inPolicyType.getVersion()); + policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder() + .name(inPolicyType.getName()).version(inPolicyType.getVersion()).build()); + + assertEquals(1, policyTypeList.size()); + assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName()); + + policyTypeList = databaseProvider + .getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().name(inPolicyType.getName()).build()); + assertEquals(1, policyTypeList.size()); assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName()); + + policyTypeList = databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()); + assertEquals(2, policyTypeList.size()); + assertEquals(inPolicyType.getName(), policyTypeList.get(0).getName()); + + for (ToscaPolicyType policyType: databaseProvider.getPolicyTypeList(null, null)) { + databaseProvider.deletePolicyType(policyType.getName(), policyType.getVersion()); + } } } diff --git a/models-provider/src/test/resources/META-INF/persistence.xml b/models-provider/src/test/resources/META-INF/persistence.xml index c7d6d1e4b..7b5bc14b9 100644 --- a/models-provider/src/test/resources/META-INF/persistence.xml +++ b/models-provider/src/test/resources/META-INF/persistence.xml @@ -33,34 +33,9 @@ <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class> <properties> - <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /> - <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb" /> - <property name="javax.persistence.jdbc.user" value="policy" /> - <property name="javax.persistence.jdbc.password" value="P01icY" /> <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> <property name="eclipselink.logging.level" value="INFO" /> - </properties> - </persistence-unit> - - <persistence-unit name="ToscaConceptMariaDBTest" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - - <class>org.onap.policy.models.dao.converters.CDataConditioner</class> - <class>org.onap.policy.models.dao.converters.Uuid2String</class> - <class>org.onap.policy.models.base.PfConceptKey</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class> - <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup</class> - <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup</class> - <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class> - - <properties> - <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" /> - <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/policy" /> - <property name="javax.persistence.jdbc.user" value="policy" /> - <property name="javax.persistence.jdbc.password" value="P01icY" /> - <property name="javax.persistence.schema-generation.database.action" value="create" /> <!-- property name="eclipselink.logging.level" value="ALL" /> <property name="eclipselink.logging.level.jpa" value="ALL" /> @@ -72,10 +47,6 @@ <property name="eclipselink.logging.level.server" value="ALL" /> <property name="eclipselink.logging.level.query" value="ALL" /> <property name="eclipselink.logging.level.properties" value="ALL" /--> - - <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> - <property name="eclipselink.ddl-generation.output-mode" value="database" /> - <property name="eclipselink.logging.level" value="INFO" /> </properties> </persistence-unit> </persistence> |