diff options
Diffstat (limited to 'models-provider/src/test/java/org')
8 files changed, 1251 insertions, 4 deletions
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(); + } + } } } |