diff options
21 files changed, 2034 insertions, 97 deletions
diff --git a/models-decisions/pom.xml b/models-decisions/pom.xml index 230c5e490..ca7458a9a 100644 --- a/models-decisions/pom.xml +++ b/models-decisions/pom.xml @@ -27,7 +27,7 @@ <version>2.0.0-SNAPSHOT</version> </parent> - <artifactId>models-decisions</artifactId> + <artifactId>policy-models-decisions</artifactId> <name>${project.artifactId}</name> <description>The models for Policy Decision API's.</description> diff --git a/models-pap/pom.xml b/models-pap/pom.xml index a437d545b..9257b9bda 100644 --- a/models-pap/pom.xml +++ b/models-pap/pom.xml @@ -1,7 +1,6 @@ <!-- ============LICENSE_START======================================================= Copyright (C) 2019 Nordix Foundation. - Modifications 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. @@ -28,7 +27,7 @@ <version>2.0.0-SNAPSHOT</version> </parent> - <artifactId>models-pap</artifactId> + <artifactId>policy-models-pap</artifactId> <name>${project.artifactId}</name> <description>The models for Policy Administration (PAP) REST API's.</description> @@ -43,10 +42,15 @@ <artifactId>policy-models-base</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.onap.policy.models</groupId> + <artifactId>policy-models-dao</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> </dependency> </dependencies> -</project>
\ No newline at end of file +</project> diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/provider/PapProvider.java b/models-pap/src/main/java/org/onap/policy/models/pap/provider/PapProvider.java new file mode 100644 index 000000000..4b80bea29 --- /dev/null +++ b/models-pap/src/main/java/org/onap/policy/models/pap/provider/PapProvider.java @@ -0,0 +1,86 @@ +/*- + * ============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.pap.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; + +/** + * 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 { + /** + * Get PDP groups. + * + * @param dao the DAO to use to access the database + * @param pdpGroupFilter a filter for the get + * @return the PDP groups found + * @throws PfModelException on errors getting PDP groups + */ + public PdpGroups getPdpGroups(@NonNull final PfDao dao, @NonNull final String pdpGroupFilter) + throws PfModelException { + return new PdpGroups(); + } + + /** + * Creates PDP groups. + * + * @param dao the DAO to use to access the database + * @param pdpGroups a specification of the PDP groups to create + * @return the PDP groups created + * @throws PfModelException on errors creating PDP groups + */ + public PdpGroups createPdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroups pdpGroups) + throws PfModelException { + return new PdpGroups(); + } + + /** + * Updates PDP groups. + * + * @param dao the DAO to use to access the database + * @param pdpGroups a specification of the PDP groups to update + * @return the PDP groups updated + * @throws PfModelException on errors updating PDP groups + */ + public PdpGroups updatePdpGroups(@NonNull final PfDao dao, @NonNull final PdpGroups pdpGroups) + throws PfModelException { + return new PdpGroups(); + } + + /** + * Delete PDP groups. + * + * @param dao the DAO to use to access the database + * @param pdpGroupFilter a filter for the get + * @return the PDP groups deleted + * @throws PfModelException on errors deleting PDP groups + */ + public PdpGroups deletePdpGroups(@NonNull final PfDao dao, @NonNull final String pdpGroupFilter) + throws PfModelException { + return new PdpGroups(); + } +} diff --git a/models-provider/pom.xml b/models-provider/pom.xml index 44756beb9..b6021b003 100644 --- a/models-provider/pom.xml +++ b/models-provider/pom.xml @@ -57,6 +57,12 @@ </dependency> <dependency> + <groupId>org.onap.policy.models</groupId> + <artifactId>policy-models-pap</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> @@ -68,5 +74,10 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> 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 fa666c0ab..9bc49837e 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 @@ -24,6 +24,7 @@ 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.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; @@ -34,7 +35,13 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; * * @author Liam Fallon (liam.fallon@est.tech) */ -public interface PolicyModelsProvider { +public interface PolicyModelsProvider extends AutoCloseable { + /** + * Open the policy model provider initializing whatever internal handling it needs. + * + * @throws PfModelException on errors opening the models provider + */ + public void init() throws PfModelException; /** * Get policy types. @@ -200,34 +207,36 @@ public interface PolicyModelsProvider { /** * Get PDP groups. * - * @param somePdpGroupFilter a filter for the get + * @param pdpGroupFilter a filter for the get * @return the PDP groups found * @throws PfModelException on errors getting PDP groups */ - public Object getPdpGroups(@NonNull final Object somePdpGroupFilter) throws PfModelException; + public PdpGroups getPdpGroups(@NonNull final String pdpGroupFilter) throws PfModelException; /** * Creates PDP groups. * - * @param somePdpGroupSpecification a specification for the PDP group + * @param pdpGroups a specification of the PDP groups to create + * @return the PDP groups created * @throws PfModelException on errors creating PDP groups */ - public Object createPdpGroups(@NonNull final Object somePdpGroupSpecification) throws PfModelException; - + public PdpGroups createPdpGroups(@NonNull final PdpGroups pdpGroups) throws PfModelException; /** * Updates PDP groups. * - * @param somePdpGroupSpecification a specification for the PDP group + * @param pdpGroups a specification of the PDP groups to update + * @return the PDP groups updated * @throws PfModelException on errors updating PDP groups */ - public Object updatePdpGroups(@NonNull final Object somePdpGroupSpecification) throws PfModelException; + public PdpGroups updatePdpGroups(@NonNull final PdpGroups pdpGroups) throws PfModelException; /** * Delete PDP groups. * - * @param somePdpGroupFilter a filter for the get + * @param pdpGroupFilter a filter for the get + * @return the PDP groups deleted * @throws PfModelException on errors deleting PDP groups */ - public Object deletePdpGroups(@NonNull final Object somePdpGroupFilter) throws PfModelException; + public PdpGroups deletePdpGroups(@NonNull final String pdpGroupFilter) throws PfModelException; } diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java index b4b5f1ddc..718668b97 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderFactory.java @@ -20,7 +20,14 @@ package org.onap.policy.models.provider; -import org.onap.policy.models.provider.impl.DummyPolicyModelsProviderImpl; +import javax.ws.rs.core.Response; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * A factory for creating PolicyModelsProvider objects using the default Policy Framework implementation. @@ -28,11 +35,44 @@ import org.onap.policy.models.provider.impl.DummyPolicyModelsProviderImpl; * @author Liam Fallon (liam.fallon@est.tech) */ public class PolicyModelsProviderFactory { + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class); /** * Creates a new PolicyModelsProvider object from its implementation. + * + * @param parameters The parameters for the implementation of the PolicyModelProvider + * @throws PfModelException on errors creating an implementation of the PolicyModelProvider */ - public PolicyModelsProvider createPolicyModelsProvider() { - return new DummyPolicyModelsProviderImpl(); + public PolicyModelsProvider createPolicyModelsProvider(@NonNull final PolicyModelsProviderParameters parameters) + throws PfModelException { + // Get the class for the PolicyModelsProvider + Class<?> implementationClass = null; + try { + // Check if the implementation class is on the classpath + implementationClass = Class.forName(parameters.getImplementation()); + } catch (final Exception exc) { + String errorMessage = "could not find implementation of the \"PolicyModelsProvider\" interface \"" + + parameters.getImplementation() + "\""; + LOGGER.warn(errorMessage, exc); + throw new PfModelException(Response.Status.NOT_FOUND, errorMessage, exc); + } + + // It is, now check if it is a PolicyModelsProvider + if (!PolicyModelsProvider.class.isAssignableFrom(implementationClass)) { + String errorMessage = "the class \"" + implementationClass.getCanonicalName() + + "\" is not an implementation of the \"PolicyModelsProvider\" interface"; + LOGGER.warn(errorMessage); + throw new PfModelException(Response.Status.BAD_REQUEST, errorMessage); + } + + try { + return (PolicyModelsProvider) implementationClass.getConstructor(PolicyModelsProviderParameters.class) + .newInstance(parameters); + } catch (Exception exc) { + String errorMessage = + "could not create an instance of PolicyModelsProvider \"" + parameters.getImplementation() + "\""; + LOGGER.warn(errorMessage, exc); + throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, exc); + } } } 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 new file mode 100644 index 000000000..5abafed22 --- /dev/null +++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProviderParameters.java @@ -0,0 +1,88 @@ +/*- + * ============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; + +import lombok.Data; + +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl; + +// @formatter:off +/** + * Class to hold all the plugin handler parameters. + * + * <p>The following parameters are defined: + * <ol> + * <li>name: A name for the parameters. + * <li>implementation: The implementation of the PolicyModelsProvider to use for writing and reading concepts, + * defaults to {@link DatabasePolicyModelsProviderImpl} and may not be null + * <li>databaseUrl: The JDBC URL for the database, mandatory. + * <li>databaseUser: The user id to use for connecting to the database, optional, defaults to null. + * <li>databasePassword: The password to use for connecting to the database encoded in Base64, optional, + * defaults to null. + * <li>persistenceUnit: The persistence unit refined in META-INF/persistence.xml to use for connecting + * to the database, mandatory. + * </ol> + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +//@formatter:on + +@Data +public class PolicyModelsProviderParameters implements ParameterGroup { + private static final String DEFAULT_IMPLEMENTATION = DatabasePolicyModelsProviderImpl.class.getCanonicalName(); + + private String name; + private String implementation = DEFAULT_IMPLEMENTATION; + private String databaseUrl; + private String databaseUser; + private String databasePassword; + private String persistenceUnit; + + /** + * Validate the model provider parameters. + * + */ + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + + if (!ParameterValidationUtils.validateStringParameter(implementation)) { + validationResult.setResult("implementation", ValidationStatus.INVALID, + "a PolicyModelsProvider implementation must be specified"); + } + + if (!ParameterValidationUtils.validateStringParameter(databaseUrl)) { + validationResult.setResult("databaseUrl", ValidationStatus.INVALID, + "a URL must be specified for the JDBC connection to the database"); + } + + if (!ParameterValidationUtils.validateStringParameter(persistenceUnit)) { + validationResult.setResult("persistenceUnit", ValidationStatus.INVALID, + "a persistence unit must be specified for connecting to the database"); + } + + return validationResult; + } +} 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 8136a75af..970aa8fef 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,125 +20,266 @@ package org.onap.policy.models.provider.impl; +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.Base64; + +import javax.ws.rs.core.Response; + import lombok.NonNull; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.dao.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.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.LegacyOperationalPolicy; +import org.onap.policy.models.tosca.legacy.provider.LegacyToscaProvider; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework - * that works towards a relational database. + * This class provides an implementation of the Policy Models Provider for the ONAP Policy Framework that works towards + * a relational database. * * @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; + + /** + * Constructor that takes the parameters. + * + * @param parameters the parameters for the provider + */ + public DatabasePolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) { + this.parameters = parameters; + } + + @Override + public void init() throws PfModelException { + LOGGER.debug("opening the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(), + parameters.getPersistenceUnit()); + + // 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()); + + try { + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } catch (Exception exc) { + String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl() + + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\""; + LOGGER.warn(errorMessage, exc); + + this.close(); + throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc); + } + } + + @Override + public void close() throws PfModelException { + LOGGER.debug("closing the database connection to {} using persistence unit {}", parameters.getDatabaseUrl(), + parameters.getPersistenceUnit()); + + if (pfDao != null) { + pfDao.close(); + 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()); + } @Override - public ToscaServiceTemplate getPolicyTypes(@NonNull PfConceptKey policyTypeKey) throws PfModelException { - return null; + public ToscaServiceTemplate getPolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException { + assertInitilized(); + return new SimpleToscaProvider().getPolicyTypes(pfDao, policyTypeKey); } @Override - public ToscaServiceTemplate createPolicyTypes(@NonNull ToscaServiceTemplate serviceTemplate) + public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - return null; + assertInitilized(); + return new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); } @Override - public ToscaServiceTemplate updatePolicyTypes(@NonNull ToscaServiceTemplate serviceTemplate) + public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - return null; + assertInitilized(); + return new SimpleToscaProvider().updatePolicyTypes(pfDao, serviceTemplate); } @Override - public ToscaServiceTemplate deletePolicyTypes(@NonNull PfConceptKey policyTypeKey) throws PfModelException { - return null; + public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException { + assertInitilized(); + return new SimpleToscaProvider().deletePolicyTypes(pfDao, policyTypeKey); } @Override - public ToscaServiceTemplate getPolicies(@NonNull PfConceptKey policyKey) throws PfModelException { - return null; + public ToscaServiceTemplate getPolicies(@NonNull final PfConceptKey policyKey) throws PfModelException { + assertInitilized(); + return new SimpleToscaProvider().getPolicies(pfDao, policyKey); } @Override - public ToscaServiceTemplate createPolicies(@NonNull ToscaServiceTemplate serviceTemplate) throws PfModelException { - return null; + public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException { + assertInitilized(); + return new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate); } @Override - public ToscaServiceTemplate updatePolicies(@NonNull ToscaServiceTemplate serviceTemplate) throws PfModelException { - return null; + public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException { + assertInitilized(); + return new SimpleToscaProvider().updatePolicies(pfDao, serviceTemplate); } @Override - public ToscaServiceTemplate deletePolicies(@NonNull PfConceptKey policyKey) throws PfModelException { - return null; + public ToscaServiceTemplate deletePolicies(@NonNull final PfConceptKey policyKey) throws PfModelException { + assertInitilized(); + return new SimpleToscaProvider().deletePolicies(pfDao, policyKey); } @Override - public LegacyOperationalPolicy getOperationalPolicy(@NonNull String policyId) throws PfModelException { - return null; + public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException { + assertInitilized(); + return new LegacyToscaProvider().getOperationalPolicy(pfDao, policyId); } @Override - public LegacyOperationalPolicy createOperationalPolicy(@NonNull LegacyOperationalPolicy legacyOperationalPolicy) - throws PfModelException { - return null; + public LegacyOperationalPolicy createOperationalPolicy( + @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { + assertInitilized(); + return new LegacyToscaProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy); } @Override - public LegacyOperationalPolicy updateOperationalPolicy(@NonNull LegacyOperationalPolicy legacyOperationalPolicy) - throws PfModelException { - return null; + public LegacyOperationalPolicy updateOperationalPolicy( + @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { + assertInitilized(); + return new LegacyToscaProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy); } @Override - public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull String policyId) throws PfModelException { - return null; + public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException { + assertInitilized(); + return new LegacyToscaProvider().deleteOperationalPolicy(pfDao, policyId); } @Override - public LegacyGuardPolicy getGuardPolicy(@NonNull String policyId) throws PfModelException { - return null; + public LegacyGuardPolicy getGuardPolicy(@NonNull final String policyId) throws PfModelException { + assertInitilized(); + return new LegacyToscaProvider().getGuardPolicy(pfDao, policyId); } @Override - public LegacyGuardPolicy createGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { - return null; + public LegacyGuardPolicy createGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) + throws PfModelException { + assertInitilized(); + return new LegacyToscaProvider().createGuardPolicy(pfDao, legacyGuardPolicy); } @Override - public LegacyGuardPolicy updateGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { - return null; + public LegacyGuardPolicy updateGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) + throws PfModelException { + assertInitilized(); + return new LegacyToscaProvider().updateGuardPolicy(pfDao, legacyGuardPolicy); } @Override - public LegacyGuardPolicy deleteGuardPolicy(@NonNull String policyId) throws PfModelException { - return null; + public LegacyGuardPolicy deleteGuardPolicy(@NonNull final String policyId) throws PfModelException { + assertInitilized(); + return new LegacyToscaProvider().deleteGuardPolicy(pfDao, policyId); } @Override - public Object getPdpGroups(@NonNull Object somePdpGroupFilter) throws PfModelException { - return null; + public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException { + assertInitilized(); + return new PapProvider().getPdpGroups(pfDao, pdpGroupFilter); } @Override - public Object createPdpGroups(@NonNull Object somePdpGroupSpecification) throws PfModelException { - return null; + public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException { + assertInitilized(); + return new PapProvider().createPdpGroups(pfDao, pdpGroups); } @Override - public Object updatePdpGroups(@NonNull Object somePdpGroupSpecification) throws PfModelException { - return null; + public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException { + assertInitilized(); + return new PapProvider().updatePdpGroups(pfDao, pdpGroups); } @Override - public Object deletePdpGroups(@NonNull Object somePdpGroupFilter) throws PfModelException { - return null; + public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException { + assertInitilized(); + return new PapProvider().deletePdpGroups(pfDao, pdpGroupFilter); } + /** + * Check if the model provider is initialized. + */ + private void assertInitilized() { + if (pfDao == null) { + String errorMessage = "policy models provider is not initilaized"; + LOGGER.warn(errorMessage); + 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 3d57c543a..d8750192c 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 @@ -30,121 +30,145 @@ import org.onap.policy.common.utils.resources.TextFileUtils; 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.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.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler; /** - * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy - * Framework. + * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework. * * @author Liam Fallon (liam.fallon@est.tech) */ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { + /** + * Constructor that takes the parameters. + * + * @param parameters the parameters for the provider + */ + public DummyPolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) { + } + + @Override + public void init() throws PfModelException { + // Not required on the dummy provider + } + + @Override + public void close() { + // Not required on the dummy provider + } + @Override - public ToscaServiceTemplate getPolicyTypes(@NonNull PfConceptKey policyTypeKey) throws PfModelException { + public ToscaServiceTemplate getPolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException { return getDummyResponse("src/main/resources/dummyimpl/DummyToscaPolicyTypeGetResponse.json"); } @Override - public ToscaServiceTemplate createPolicyTypes(@NonNull ToscaServiceTemplate serviceTemplate) + public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { return serviceTemplate; } @Override - public ToscaServiceTemplate updatePolicyTypes(@NonNull ToscaServiceTemplate serviceTemplate) + public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { return serviceTemplate; } @Override - public ToscaServiceTemplate deletePolicyTypes(@NonNull PfConceptKey policyTypeKey) throws PfModelException { + public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException { return getDummyResponse("src/main/resources/dummyimpl/DummyToscaPolicyTypeDeleteResponse.json"); } @Override - public ToscaServiceTemplate getPolicies(@NonNull PfConceptKey policyKey) throws PfModelException { + public ToscaServiceTemplate getPolicies(@NonNull final PfConceptKey policyKey) throws PfModelException { return getDummyResponse("src/main/resources/dummyimpl/DummyToscaPolicyGetResponse.json"); } @Override - public ToscaServiceTemplate createPolicies(@NonNull ToscaServiceTemplate serviceTemplate) throws PfModelException { + public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException { return serviceTemplate; } @Override - public ToscaServiceTemplate updatePolicies(@NonNull ToscaServiceTemplate serviceTemplate) throws PfModelException { + public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate) + throws PfModelException { return serviceTemplate; } @Override - public ToscaServiceTemplate deletePolicies(@NonNull PfConceptKey policyKey) throws PfModelException { + public ToscaServiceTemplate deletePolicies(@NonNull final PfConceptKey policyKey) throws PfModelException { return getDummyResponse("src/main/resources/dummyimpl/DummyToscaPolicyDeleteResponse.json"); } @Override - public LegacyOperationalPolicy getOperationalPolicy(@NonNull String policyId) throws PfModelException { + + public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException { return new LegacyOperationalPolicy(); } @Override - public LegacyOperationalPolicy createOperationalPolicy(@NonNull LegacyOperationalPolicy legacyOperationalPolicy) - throws PfModelException { + public LegacyOperationalPolicy createOperationalPolicy( + @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { return legacyOperationalPolicy; } @Override - public LegacyOperationalPolicy updateOperationalPolicy(@NonNull LegacyOperationalPolicy legacyOperationalPolicy) - throws PfModelException { + public LegacyOperationalPolicy updateOperationalPolicy( + @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { return legacyOperationalPolicy; } @Override - public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull String policyId) throws PfModelException { + public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException { return new LegacyOperationalPolicy(); } @Override - public LegacyGuardPolicy getGuardPolicy(@NonNull String policyId) throws PfModelException { + public LegacyGuardPolicy getGuardPolicy(@NonNull final String policyId) throws PfModelException { return new LegacyGuardPolicy(); } @Override - public LegacyGuardPolicy createGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { + public LegacyGuardPolicy createGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) + throws PfModelException { return legacyGuardPolicy; } @Override - public LegacyGuardPolicy updateGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { + public LegacyGuardPolicy updateGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy) + throws PfModelException { return legacyGuardPolicy; } @Override - public LegacyGuardPolicy deleteGuardPolicy(@NonNull String policyId) throws PfModelException { + public LegacyGuardPolicy deleteGuardPolicy(@NonNull final String policyId) throws PfModelException { return new LegacyGuardPolicy(); } @Override - public Object getPdpGroups(@NonNull Object somePdpGroupFilter) throws PfModelException { - return null; + public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException { + return new PdpGroups(); } @Override - public Object createPdpGroups(@NonNull Object somePdpGroupSpecification) throws PfModelException { - return null; + public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException { + return new PdpGroups(); } @Override - public Object updatePdpGroups(@NonNull Object somePdpGroupSpecification) throws PfModelException { - return null; + public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException { + return new PdpGroups(); } @Override - public Object deletePdpGroups(@NonNull Object somePdpGroupFilter) throws PfModelException { - return null; + public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException { + return new PdpGroups(); } /** @@ -153,7 +177,7 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { * @param fileName the file name containing the dummy response * @return the ToscaServiceTemplate with the dummy response */ - private ToscaServiceTemplate getDummyResponse(@NonNull final String fileName) { + protected ToscaServiceTemplate getDummyResponse(@NonNull final String fileName) { Gson gson = new ToscaServiceTemplateMessageBodyHandler().getGson(); ToscaServiceTemplate serviceTemplate; diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderFactoryTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderFactoryTest.java new file mode 100644 index 000000000..628d9fc53 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderFactoryTest.java @@ -0,0 +1,90 @@ +/*- + * ============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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import lombok.ToString; + +import org.junit.Test; + +/** + * Test the {@link PolicyModelsProviderFactory} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@ToString +public class PolicyModelsProviderFactoryTest { + + @Test + public void testFactory() { + PolicyModelsProviderFactory factory = new PolicyModelsProviderFactory(); + + try { + factory.createPolicyModelsProvider(null); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("parameters is marked @NonNull but is null", exc.getMessage()); + } + + try { + PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + pars.setImplementation(null); + factory.createPolicyModelsProvider(pars); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("could not find implementation of the \"PolicyModelsProvider\" interface \"null\"", + exc.getMessage()); + } + + try { + PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + pars.setImplementation("com.acmecorp.RoadRunner"); + factory.createPolicyModelsProvider(pars); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("could not find implementation of the \"PolicyModelsProvider\" " + + "interface \"com.acmecorp.RoadRunner\"", exc.getMessage()); + } + + try { + PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + pars.setImplementation("java.lang.String"); + factory.createPolicyModelsProvider(pars); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals( + "the class \"java.lang.String\" is not an implementation of the \"PolicyModelsProvider\" interface", + exc.getMessage()); + } + + try { + PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + pars.setImplementation("org.onap.policy.models.provider.impl.DummyBadProviderImpl"); + factory.createPolicyModelsProvider(pars); + fail("test should throw an exception here"); + } catch (Exception exc) { + assertEquals("could not create an instance of PolicyModelsProvider " + + "\"org.onap.policy.models.provider.impl.DummyBadProviderImpl\"", exc.getMessage()); + } + } +} 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 new file mode 100644 index 000000000..626d2bf5d --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderParametersTest.java @@ -0,0 +1,66 @@ +/*- + * ============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; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +/** + * Test of {@link PolicyModelsProviderParameters} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PolicyModelsProviderParametersTest { + + @Test + public void testParameters() { + PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + pars.setDatabaseUrl("jdbc://www.acmecorp/roadrunner"); + pars.setPersistenceUnit("WileECoyote"); + + GroupValidationResult result = pars.validate(); + assertTrue(result.isValid()); + + pars.setImplementation(null); + result = pars.validate(); + assertFalse(result.isValid()); + pars.setImplementation("An Implementation"); + result = pars.validate(); + assertTrue(result.isValid()); + + pars.setDatabaseUrl(null); + result = pars.validate(); + assertFalse(result.isValid()); + pars.setDatabaseUrl("jdbc://www.acmecorp/roadrunner"); + result = pars.validate(); + assertTrue(result.isValid()); + + pars.setPersistenceUnit(null); + result = pars.validate(); + assertFalse(result.isValid()); + pars.setPersistenceUnit("WileECoyote"); + result = pars.validate(); + assertTrue(result.isValid()); + } +} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/TestPojos.java b/models-provider/src/test/java/org/onap/policy/models/provider/TestPojos.java new file mode 100644 index 000000000..67b314cff --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/TestPojos.java @@ -0,0 +1,56 @@ +/*- + * ============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; + +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; + +/** + * Class to perform unit tests of all pojos. + * + * @author liam.fallon@est.tech) + * + */ +public class TestPojos { + + private static final String POJO_PACKAGE = "org.onap.policy.models.provider"; + + @Test + public void testPojos() { + // @formatter:off + final Validator validator = ValidatorBuilder + .create() + .with(new SetterMustExistRule()) + .with(new GetterMustExistRule()) + .with(new SetterTester()) + .with(new GetterTester()) + .build(); + validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + // :formatter:on + } +} 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 new file mode 100644 index 000000000..3b30f98f5 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -0,0 +1,298 @@ +/*- + * ============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.assertNull; +import static org.junit.Assert.fail; + +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.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.LegacyOperationalPolicy; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; + +/** + * Test the database models provider implementation. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DatabasePolicyModelsProviderTest { + PolicyModelsProviderParameters parameters; + + /** + * Initialize parameters. + */ + @Before + public void setupParameters() { + parameters = new PolicyModelsProviderParameters(); + 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 { + PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + + parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist"); + try { + 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()); + } + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); + + try { + databaseProvider.init(); + databaseProvider.close(); + } catch (Exception pfme) { + fail("test shold not throw an exception here"); + } + + parameters.setPersistenceUnit("WileECoyote"); + try { + 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()); + } + parameters.setPersistenceUnit("ToscaConceptTest"); + + try { + databaseProvider.init(); + databaseProvider.close(); + } catch (Exception pfme) { + fail("test shold not throw an exception here"); + } + + try { + databaseProvider.close(); + } catch (Exception pfme) { + fail("test shold not throw an exception here"); + } + + try { + 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()); + } + } + + @Test + public void testProviderMethodsNull() throws Exception { + PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider.init(); + + try { + databaseProvider.getPolicyTypes(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.createPolicyTypes(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.updatePolicyTypes(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.deletePolicyTypes(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); + } + + try { + databaseProvider.getPolicies(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.createPolicies(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.updatePolicies(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.deletePolicies(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); + } + + try { + databaseProvider.getOperationalPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.createOperationalPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.updateOperationalPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.deleteOperationalPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); + } + + try { + databaseProvider.getGuardPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.createGuardPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.updateGuardPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.deleteGuardPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); + } + + try { + databaseProvider.getPdpGroups(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.createPdpGroups(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.updatePdpGroups(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); + } + try { + databaseProvider.deletePdpGroups(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); + } + + databaseProvider.close(); + } + + @Test + public void testProviderMethodsNotInit() throws Exception { + PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + try { + databaseProvider.getPolicyTypes(new PfConceptKey()); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policy models provider is not initilaized", npe.getMessage()); + } + } + + @Test + public void testProviderMethods() { + try (PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { + databaseProvider.init(); + + assertNull(databaseProvider.getPolicyTypes(new PfConceptKey())); + assertNull(databaseProvider.createPolicyTypes(new ToscaServiceTemplate())); + assertNull(databaseProvider.updatePolicyTypes(new ToscaServiceTemplate())); + assertNull(databaseProvider.deletePolicyTypes(new PfConceptKey())); + + assertNull(databaseProvider.getPolicies(new PfConceptKey())); + assertNull(databaseProvider.createPolicies(new ToscaServiceTemplate())); + assertNull(databaseProvider.updatePolicies(new ToscaServiceTemplate())); + assertNull(databaseProvider.deletePolicies(new PfConceptKey())); + + assertNull(databaseProvider.getOperationalPolicy("policy_id")); + assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy())); + assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy())); + assertNull(databaseProvider.deleteOperationalPolicy("policy_id")); + + assertNull(databaseProvider.getGuardPolicy("policy_id")); + assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy())); + assertNull(databaseProvider.updateGuardPolicy(new LegacyGuardPolicy())); + assertNull(databaseProvider.deleteGuardPolicy("policy_id")); + + assertNotNull(databaseProvider.getPdpGroups("filter")); + assertNotNull(databaseProvider.createPdpGroups(new PdpGroups())); + assertNotNull(databaseProvider.updatePdpGroups(new PdpGroups())); + assertNotNull(databaseProvider.deletePdpGroups("filter")); + + } catch (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 new file mode 100644 index 000000000..fb0a2416e --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java @@ -0,0 +1,156 @@ +/*- + * ============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 javax.ws.rs.core.Response; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.pap.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.LegacyOperationalPolicy; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; + +/** + * Dummy implementation of {@link PolicyModelsProvider} with bad constructor. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyBadProviderImpl implements PolicyModelsProvider { + public DummyBadProviderImpl() { + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, "Bad Request"); + } + + @Override + public void close() throws Exception {} + + @Override + public void init() throws PfModelException { + } + + @Override + public ToscaServiceTemplate getPolicyTypes(@NonNull PfConceptKey policyTypeKey) throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate createPolicyTypes(@NonNull ToscaServiceTemplate serviceTemplate) + throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate updatePolicyTypes(@NonNull ToscaServiceTemplate serviceTemplate) + throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate deletePolicyTypes(@NonNull PfConceptKey policyTypeKey) throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate getPolicies(@NonNull PfConceptKey policyKey) throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate createPolicies(@NonNull ToscaServiceTemplate serviceTemplate) throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate updatePolicies(@NonNull ToscaServiceTemplate serviceTemplate) throws PfModelException { + return null; + } + + @Override + public ToscaServiceTemplate deletePolicies(@NonNull PfConceptKey policyKey) throws PfModelException { + return null; + } + + @Override + public LegacyOperationalPolicy getOperationalPolicy(@NonNull String policyId) throws PfModelException { + return null; + } + + @Override + public LegacyOperationalPolicy createOperationalPolicy(@NonNull LegacyOperationalPolicy legacyOperationalPolicy) + throws PfModelException { + return null; + } + + @Override + public LegacyOperationalPolicy updateOperationalPolicy(@NonNull LegacyOperationalPolicy legacyOperationalPolicy) + throws PfModelException { + return null; + } + + @Override + public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull String policyId) throws PfModelException { + return null; + } + + @Override + public LegacyGuardPolicy getGuardPolicy(@NonNull String policyId) throws PfModelException { + return null; + } + + @Override + public LegacyGuardPolicy createGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { + return null; + } + + @Override + public LegacyGuardPolicy updateGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { + return null; + } + + @Override + public LegacyGuardPolicy deleteGuardPolicy(@NonNull String policyId) throws PfModelException { + return null; + } + + @Override + public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException { + return null; + } + + @Override + public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException { + return null; + } + + @Override + public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException { + return null; + } + + @Override + public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException { + return null; + } +} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyConnection.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyConnection.java new file mode 100644 index 000000000..776e7f53f --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyConnection.java @@ -0,0 +1,322 @@ +/*- + * ============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 java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executor; + +/** + * Dummy database connection. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyConnection implements Connection { + + @Override + public boolean isWrapperFor(Class<?> iface) throws SQLException { + return false; + } + + @Override + public <T> T unwrap(Class<T> iface) throws SQLException { + return null; + } + + @Override + public void abort(Executor executor) throws SQLException { + return; + } + + @Override + public void clearWarnings() throws SQLException { + return; + } + + @Override + public void close() throws SQLException { + throw new SQLException("Bad Request"); + } + + @Override + public void commit() throws SQLException { + return; + } + + @Override + public Array createArrayOf(String typeName, Object[] elements) throws SQLException { + return null; + } + + @Override + public Blob createBlob() throws SQLException { + return null; + } + + @Override + public Clob createClob() throws SQLException { + return null; + } + + @Override + public NClob createNClob() throws SQLException { + return null; + } + + @Override + public SQLXML createSQLXML() throws SQLException { + return null; + } + + @Override + public Statement createStatement() throws SQLException { + return null; + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { + return null; + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + return null; + } + + @Override + public Struct createStruct(String typeName, Object[] attributes) throws SQLException { + return null; + } + + @Override + public boolean getAutoCommit() throws SQLException { + return false; + } + + @Override + public String getCatalog() throws SQLException { + return null; + } + + @Override + public Properties getClientInfo() throws SQLException { + return null; + } + + @Override + public String getClientInfo(String name) throws SQLException { + return null; + } + + @Override + public int getHoldability() throws SQLException { + return 0; + } + + @Override + public DatabaseMetaData getMetaData() throws SQLException { + return null; + } + + @Override + public int getNetworkTimeout() throws SQLException { + return 0; + } + + @Override + public String getSchema() throws SQLException { + return null; + } + + @Override + public int getTransactionIsolation() throws SQLException { + return 0; + } + + @Override + public Map<String, Class<?>> getTypeMap() throws SQLException { + return null; + } + + @Override + public SQLWarning getWarnings() throws SQLException { + return null; + } + + @Override + public boolean isClosed() throws SQLException { + return false; + } + + @Override + public boolean isReadOnly() throws SQLException { + return false; + } + + @Override + public boolean isValid(int timeout) throws SQLException { + return false; + } + + @Override + public String nativeSQL(String sql) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, + int resultSetHoldability) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) + throws SQLException { + return null; + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, + int resultSetHoldability) throws SQLException { + return null; + } + + @Override + public void releaseSavepoint(Savepoint savepoint) throws SQLException { + return; + } + + @Override + public void rollback() throws SQLException { + return; + } + + @Override + public void rollback(Savepoint savepoint) throws SQLException { + return; + } + + @Override + public void setAutoCommit(boolean autoCommit) throws SQLException { + return; + } + + @Override + public void setCatalog(String catalog) throws SQLException { + return; + } + + @Override + public void setClientInfo(Properties properties) throws SQLClientInfoException { + return; + } + + @Override + public void setClientInfo(String name, String value) throws SQLClientInfoException { + return; + } + + @Override + public void setHoldability(int holdability) throws SQLException { + return; + } + + @Override + public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { + return; + } + + @Override + public void setReadOnly(boolean readOnly) throws SQLException { + return; + } + + @Override + public Savepoint setSavepoint() throws SQLException { + return null; + } + + @Override + public Savepoint setSavepoint(String name) throws SQLException { + return null; + } + + @Override + public void setSchema(String schema) throws SQLException { + return; + } + + @Override + public void setTransactionIsolation(int level) throws SQLException { + return; + } + + @Override + public void setTypeMap(Map<String, Class<?>> map) throws SQLException { + return; + } +} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderSubImpl.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderSubImpl.java new file mode 100644 index 000000000..9b68dd037 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderSubImpl.java @@ -0,0 +1,50 @@ +/*- + * ============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 lombok.NonNull; + +import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; + +/** + * Sub class to check getDummyResponse() method in base class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyPolicyModelsProviderSubImpl extends DummyPolicyModelsProviderImpl { + /** + * Constructor. + * + * @param parameters the parameters + */ + public DummyPolicyModelsProviderSubImpl(@NonNull PolicyModelsProviderParameters parameters) { + super(parameters); + } + + public ToscaServiceTemplate getBadDummyResponse1() { + return super.getDummyResponse("/i/dont/exist"); + } + + public ToscaServiceTemplate getBadDummyResponse2() { + return super.getDummyResponse(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 f7fe5b96c..bf3382fdc 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 @@ -22,28 +22,237 @@ 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 org.junit.Test; 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.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.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; /** - * Test the dummy moldes provider implementation. + * Test the dummy models provider implementation. * * @author Liam Fallon (liam.fallon@est.tech) */ public class DummyPolicyModelsProviderTest { @Test - public void test() throws PfModelException { - PolicyModelsProvider dummyProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(); + public void testProvider() throws Exception { + PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setImplementation(DummyPolicyModelsProviderImpl.class.getCanonicalName()); + parameters.setDatabaseUrl("jdbc:dummy"); + parameters.setPersistenceUnit("dummy"); + + PolicyModelsProvider dummyProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + + dummyProvider.init(); ToscaServiceTemplate serviceTemplate = dummyProvider.getPolicies(new PfConceptKey()); assertNotNull(serviceTemplate); assertEquals("onap.vcpe.tca:1.0.0", serviceTemplate.getTopologyTemplate().getPolicies().get("onap.vcpe.tca").getId()); + + dummyProvider.close(); + } + + @Test + public void testProviderMethods() throws Exception { + PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setImplementation(DummyPolicyModelsProviderImpl.class.getCanonicalName()); + parameters.setDatabaseUrl("jdbc:dummy"); + parameters.setPersistenceUnit("dummy"); + + PolicyModelsProvider dummyProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + dummyProvider.init(); + + assertNotNull(dummyProvider.getPolicyTypes(new PfConceptKey())); + assertNotNull(dummyProvider.createPolicyTypes(new ToscaServiceTemplate())); + assertNotNull(dummyProvider.updatePolicyTypes(new ToscaServiceTemplate())); + assertNotNull(dummyProvider.deletePolicyTypes(new PfConceptKey())); + + assertNotNull(dummyProvider.getPolicies(new PfConceptKey())); + assertNotNull(dummyProvider.createPolicies(new ToscaServiceTemplate())); + assertNotNull(dummyProvider.updatePolicies(new ToscaServiceTemplate())); + assertNotNull(dummyProvider.deletePolicies(new PfConceptKey())); + + assertNotNull(dummyProvider.getOperationalPolicy("policy_id")); + assertNotNull(dummyProvider.createOperationalPolicy(new LegacyOperationalPolicy())); + assertNotNull(dummyProvider.updateOperationalPolicy(new LegacyOperationalPolicy())); + assertNotNull(dummyProvider.deleteOperationalPolicy("policy_id")); + + assertNotNull(dummyProvider.getGuardPolicy("policy_id")); + assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicy())); + assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicy())); + assertNotNull(dummyProvider.deleteGuardPolicy("policy_id")); + + assertNotNull(dummyProvider.getPdpGroups("filter")); + assertNotNull(dummyProvider.createPdpGroups(new PdpGroups())); + assertNotNull(dummyProvider.updatePdpGroups(new PdpGroups())); + assertNotNull(dummyProvider.deletePdpGroups("filter")); + + try { + dummyProvider.getPolicyTypes(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.createPolicyTypes(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.updatePolicyTypes(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.deletePolicyTypes(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); + } + + try { + dummyProvider.getPolicies(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.createPolicies(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.updatePolicies(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.deletePolicies(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); + } + + try { + dummyProvider.getOperationalPolicy(null); + + + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.createOperationalPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.updateOperationalPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.deleteOperationalPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); + } + + try { + dummyProvider.getGuardPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.createGuardPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.updateGuardPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.deleteGuardPolicy(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); + } + + try { + + + dummyProvider.getPdpGroups(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.createPdpGroups(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.updatePdpGroups(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); + } + try { + dummyProvider.deletePdpGroups(null); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); + } + + dummyProvider.close(); + } + + @Test + public void testDummyResponse() { + DummyPolicyModelsProviderSubImpl resp = null; + + try { + resp = new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters()); + resp.getBadDummyResponse1(); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("error serializing object", npe.getMessage()); + } finally { + if (resp != null) { + resp.close(); + } + } + + try { + resp = new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters()); + resp.getBadDummyResponse2(); + fail("test should throw an exception"); + } catch (Exception npe) { + assertEquals("fileName is marked @NonNull but is null", npe.getMessage()); + } finally { + if (resp != null) { + resp.close(); + } + } } } diff --git a/models-tosca/pom.xml b/models-tosca/pom.xml index 019c5b3fd..5658a1c0e 100644 --- a/models-tosca/pom.xml +++ b/models-tosca/pom.xml @@ -41,6 +41,12 @@ <dependency> <groupId>org.onap.policy.models</groupId> + <artifactId>policy-models-dao</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.onap.policy.models</groupId> <artifactId>policy-models-examples</artifactId> <version>${project.version}</version> </dependency> diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java index 2c0d5084d..2f87020be 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java @@ -52,20 +52,20 @@ public class LegacyOperationalPolicyMapper // TODO: Find out how to parse the PolicyType from the content // TODO: Check if this is the correct way to set the policy type version - toscaPolicy.setType(new PfConceptKey("SomeDerivedPolicyType", "1.0.0")); + toscaPolicy.setType(new PfConceptKey("SomeDerivedPolicyType", "1.0.1")); Map<String, String> propertyMap = new HashMap<>(); toscaPolicy.setProperties(propertyMap); toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent()); - PfConceptKey serviceTemplateKey = new PfConceptKey("ServiceTemplate", "1.0.0"); + PfConceptKey serviceTemplateKey = new PfConceptKey("ServiceTemplate", "1.0.2"); ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey); serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0"); PfReferenceKey topologyTemplateKey = new PfReferenceKey(serviceTemplateKey, "TopolocyTemplate"); serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate(topologyTemplateKey)); - PfConceptKey policiesKey = new PfConceptKey("Policies", "1.0.0"); + PfConceptKey policiesKey = new PfConceptKey("Policies", "1.0.3"); serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies(policiesKey)); serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java new file mode 100644 index 000000000..da9d929df --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java @@ -0,0 +1,139 @@ +/*- + * ============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 lombok.NonNull; + +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; + +/** + * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyToscaProvider { + /** + * Get legacy operational policy. + * + * @param dao the DAO to use to access the database + * @param policyId ID of the policy. + * @return the policies found + * @throws PfModelException on errors getting policies + */ + public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId) + throws PfModelException { + return null; + } + + /** + * Create legacy operational policy. + * + * @param dao the DAO to use to access the database + * @param legacyOperationalPolicy the definition of the policy to be created. + * @return the created policy + * @throws PfModelException on errors creating policies + */ + public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao, + @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { + return null; + } + + /** + * Update legacy operational policy. + * + * @param dao the DAO to use to access the database + * @param legacyOperationalPolicy the definition of the policy to be updated + * @return the updated policy + * @throws PfModelException on errors updating policies + */ + public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao, + @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { + return null; + } + + /** + * Delete legacy operational policy. + * + * @param dao the DAO to use to access the database + * @param policyId ID of the policy. + * @return the deleted policy + * @throws PfModelException on errors deleting policies + */ + public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId) + throws PfModelException { + return null; + } + + /** + * Get legacy guard policy. + * + * @param dao the DAO to use to access the database + * @param policyId ID of the policy. + * @return the policies found + * @throws PfModelException on errors getting policies + */ + public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId) + throws PfModelException { + return null; + } + + /** + * Create legacy guard policy. + * + * @param dao the DAO to use to access the database + * @param legacyGuardPolicy the definition of the policy to be created. + * @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; + } + + /** + * Update legacy guard policy. + * + * @param dao the DAO to use to access the database + * @param legacyGuardPolicy the definition of the policy to be updated + * @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; + } + + /** + * Delete legacy guard policy. + * + * @param dao the DAO to use to access the database + * @param policyId ID of the policy. + * @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; + } +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java new file mode 100644 index 000000000..3d563a1cc --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -0,0 +1,142 @@ +/*- + * ============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.simple.provider; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; + +/** + * This class provides the provision of information on TOSCA concepts in the database to callers. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class SimpleToscaProvider { + /** + * Get policy types. + * + * @param dao the DAO to use to access the database + * @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 + */ + public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey) + throws PfModelException { + return null; + } + + /** + * Create policy types. + * + * @param dao the DAO to use to access the database + * @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 + */ + public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + return null; + } + + /** + * Create policy types. + * + * @param dao the DAO to use to access the database + * @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 + */ + public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + return null; + } + + /** + * Delete policy types. + * + * @param dao the DAO to use to access the database + * @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 + */ + public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey) + throws PfModelException { + return null; + } + + /** + * Get policies. + * + * @param dao the DAO to use to access the database + * @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 + */ + public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey) + throws PfModelException { + return null; + } + + /** + * Create policies. + * + * @param dao the DAO to use to access the database + * @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 + */ + public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + return null; + } + + /** + * Update policies. + * + * @param dao the DAO to use to access the database + * @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 + */ + public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao, + @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { + return null; + } + + /** + * Delete policies. + * + * @param dao the DAO to use to access the database + * @param policyKey the policy key + * @return the TOSCA service template containing the policy types that were deleted + * @throws PfModelException on errors deleting policies + */ + public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey) + throws PfModelException { + return null; + } +} |