diff options
Diffstat (limited to 'models-provider/src/test/java')
5 files changed, 336 insertions, 315 deletions
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java index e4ecb9d1d..1631d9bb1 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -20,9 +20,8 @@ package org.onap.policy.models.provider.impl; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import java.util.Base64; @@ -30,11 +29,11 @@ import java.util.Base64; import org.junit.Before; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.pap.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; import org.slf4j.Logger; @@ -69,12 +68,11 @@ public class DatabasePolicyModelsProviderTest { new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist"); - try { + + assertThatThrownBy(() -> { databaseProvider.init(); - fail("test should throw an exception"); - } catch (Exception pfme) { - assertEquals("could not connect to database with URL \"jdbc://www.acmecorp.nonexist\"", pfme.getMessage()); - } + }).hasMessage("could not connect to database with URL \"jdbc://www.acmecorp.nonexist\""); + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); try { @@ -85,13 +83,13 @@ public class DatabasePolicyModelsProviderTest { } parameters.setPersistenceUnit("WileECoyote"); - try { + + String errorMessage = "could not create Data Access Object (DAO) using url " + + "\"jdbc:h2:mem:testdb\" and persistence unit \"WileECoyote\""; + assertThatThrownBy(() -> { databaseProvider.init(); - fail("test should throw an exception"); - } catch (Exception pfme) { - assertEquals("could not create Data Access Object (DAO) using url " - + "\"jdbc:h2:mem:testdb\" and persistence unit \"WileECoyote\"", pfme.getMessage()); - } + }).hasMessage(errorMessage); + parameters.setPersistenceUnit("ToscaConceptTest"); try { @@ -107,15 +105,12 @@ public class DatabasePolicyModelsProviderTest { fail("test shold not throw an exception here"); } - try { + assertThatThrownBy(() -> { DatabasePolicyModelsProviderImpl databaseProviderImpl = (DatabasePolicyModelsProviderImpl) databaseProvider; databaseProvider.init(); databaseProviderImpl.setConnection(new DummyConnection()); databaseProvider.close(); - fail("test should throw an exception"); - } catch (Exception pfme) { - assertEquals("could not close connection to database with URL \"jdbc:h2:mem:testdb\"", pfme.getMessage()); - } + }).hasMessage("could not close connection to database with URL \"jdbc:h2:mem:testdb\""); } @Test @@ -124,144 +119,99 @@ public class DatabasePolicyModelsProviderTest { new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); databaseProvider.init(); - try { + assertThatThrownBy(() -> { databaseProvider.getPolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyTypeKey is marked @NonNull but is null"); + + + assertThatThrownBy(() -> { databaseProvider.createPolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updatePolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deletePolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyTypeKey is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { databaseProvider.getPolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyKey is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createPolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updatePolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deletePolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyKey is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { databaseProvider.getOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyId is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updateOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deleteOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { databaseProvider.getGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyId is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updateGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deleteGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { databaseProvider.getPdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroupFilter is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.createPdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroups is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.updatePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroups is marked @NonNull but is null"); + + assertThatThrownBy(() -> { databaseProvider.deletePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("pdpGroupFilter is marked @NonNull but is null"); databaseProvider.close(); + } @Test public void testProviderMethodsNotInit() throws Exception { PolicyModelsProvider databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); - try { + + assertThatThrownBy(() -> { databaseProvider.getPolicyTypes(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy models provider is not initilaized", npe.getMessage()); - } + }).hasMessage("policy models provider is not initilaized"); } @Test @@ -270,81 +220,69 @@ public class DatabasePolicyModelsProviderTest { new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { databaseProvider.init(); - try { + assertThatThrownBy(() -> { databaseProvider.getPolicyTypes(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy type not found: NULL:0.0.0", npe.getMessage()); - } - try { + }).hasMessage("policy type not found: NULL:0.0.0"); + + assertThatThrownBy(() -> { databaseProvider.createPolicyTypes(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("no policy types specified on service template", npe.getMessage()); - } - try { + }).hasMessage("no policy types specified on service template"); + + assertThatThrownBy(() -> { databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("no policy types specified on service template", npe.getMessage()); - } - try { + }).hasMessage("no policy types specified on service template"); + + assertThatThrownBy(() -> { databaseProvider.deletePolicyTypes(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy type not found: NULL:0.0.0", npe.getMessage()); - } + }).hasMessage("policy type not found: NULL:0.0.0"); - try { + assertThatThrownBy(() -> { databaseProvider.getPolicies(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy not found: NULL:0.0.0", npe.getMessage()); - } - try { + }).hasMessage("policy not found: NULL:0.0.0"); + + assertThatThrownBy(() -> { databaseProvider.createPolicies(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("topology template not specified on service template", npe.getMessage()); - } - try { + }).hasMessage("topology template not specified on service template"); + + assertThatThrownBy(() -> { databaseProvider.updatePolicies(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("topology template not specified on service template", npe.getMessage()); - } - try { + }).hasMessage("topology template not specified on service template"); + + assertThatThrownBy(() -> { databaseProvider.deletePolicies(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy not found: NULL:0.0.0", npe.getMessage()); - } - - try { - assertNull(databaseProvider.getOperationalPolicy("policy_id")); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("no policy found for policy ID: policy_id", npe.getMessage()); - } - try { - assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy())); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("name is marked @NonNull but is null", npe.getMessage()); - } - try { - assertNull(databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy())); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("no policy found for policy ID: null", npe.getMessage()); - } - try { - assertNull(databaseProvider.deleteOperationalPolicy("policy_id")); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("no policy found for policy ID: policy_id", npe.getMessage()); - } - - assertNull(databaseProvider.getGuardPolicy("policy_id")); - assertNull(databaseProvider.createGuardPolicy(new LegacyGuardPolicy())); - assertNull(databaseProvider.updateGuardPolicy(new LegacyGuardPolicy())); - assertNull(databaseProvider.deleteGuardPolicy("policy_id")); + }).hasMessage("policy not found: NULL:0.0.0"); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy("policy_id"); + }).hasMessage("no policy found for policy ID: policy_id"); + + assertThatThrownBy(() -> { + databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()); + }).hasMessage("name is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()); + }).hasMessage("no policy found for policy ID: null"); + + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy("policy_id"); + }).hasMessage("no policy found for policy ID: policy_id"); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy("policy_id"); + }).hasMessage("no policy found for policy ID: policy_id"); + + assertThatThrownBy(() -> { + databaseProvider.createGuardPolicy(new LegacyGuardPolicyInput()); + }).hasMessage("policy type for guard policy \"null\" unknown"); + + assertThatThrownBy(() -> { + databaseProvider.updateGuardPolicy(new LegacyGuardPolicyInput()); + }).hasMessage("policy type for guard policy \"null\" unknown"); + + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy("policy_id"); + }).hasMessage("no policy found for policy ID: policy_id"); assertNotNull(databaseProvider.getPdpGroups("filter")); assertNotNull(databaseProvider.createPdpGroups(new PdpGroups())); diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java index fb0a2416e..f8602e65c 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java @@ -20,6 +20,8 @@ package org.onap.policy.models.provider.impl; +import java.util.Map; + import javax.ws.rs.core.Response; import lombok.NonNull; @@ -27,9 +29,10 @@ import lombok.NonNull; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.pap.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; @@ -47,8 +50,7 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { public void close() throws Exception {} @Override - public void init() throws PfModelException { - } + public void init() throws PfModelException {} @Override public ToscaServiceTemplate getPolicyTypes(@NonNull PfConceptKey policyTypeKey) throws PfModelException { @@ -115,22 +117,24 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { } @Override - public LegacyGuardPolicy getGuardPolicy(@NonNull String policyId) throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull String policyId) throws PfModelException { return null; } @Override - public LegacyGuardPolicy createGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull LegacyGuardPolicyInput legacyGuardPolicy) + throws PfModelException { return null; } @Override - public LegacyGuardPolicy updateGuardPolicy(@NonNull LegacyGuardPolicy legacyGuardPolicy) throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull LegacyGuardPolicyInput legacyGuardPolicy) + throws PfModelException { return null; } @Override - public LegacyGuardPolicy deleteGuardPolicy(@NonNull String policyId) throws PfModelException { + public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull String policyId) throws PfModelException { return null; } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java index bf3382fdc..de61dc0ab 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java @@ -20,17 +20,18 @@ package org.onap.policy.models.provider.impl; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; -import org.onap.policy.models.pap.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; @@ -86,8 +87,8 @@ public class DummyPolicyModelsProviderTest { assertNotNull(dummyProvider.deleteOperationalPolicy("policy_id")); assertNotNull(dummyProvider.getGuardPolicy("policy_id")); - assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicy())); - assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicy())); + assertNotNull(dummyProvider.createGuardPolicy(new LegacyGuardPolicyInput())); + assertNotNull(dummyProvider.updateGuardPolicy(new LegacyGuardPolicyInput())); assertNotNull(dummyProvider.deleteGuardPolicy("policy_id")); assertNotNull(dummyProvider.getPdpGroups("filter")); @@ -95,134 +96,70 @@ public class DummyPolicyModelsProviderTest { assertNotNull(dummyProvider.updatePdpGroups(new PdpGroups())); assertNotNull(dummyProvider.deletePdpGroups("filter")); - try { + assertThatThrownBy(() -> { dummyProvider.getPolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyTypeKey is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createPolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updatePolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deletePolicyTypes(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyTypeKey is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyTypeKey is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { dummyProvider.getPolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyKey is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createPolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updatePolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("serviceTemplate is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deletePolicies(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyKey is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyKey is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { dummyProvider.getOperationalPolicy(null); - - - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyId is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updateOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deleteOperationalPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { dummyProvider.getGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("policyId is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updateGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("legacyGuardPolicy is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deleteGuardPolicy(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policyId is marked @NonNull but is null", npe.getMessage()); - } - - try { - + }).hasMessage("policyId is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.getPdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroupFilter is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.createPdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroups is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.updatePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroups is marked @NonNull but is null", npe.getMessage()); - } - try { + }).hasMessage("pdpGroups is marked @NonNull but is null"); + assertThatThrownBy(() -> { dummyProvider.deletePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessage("pdpGroupFilter is marked @NonNull but is null"); dummyProvider.close(); } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java new file mode 100644 index 000000000..2cb787d00 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyGuardPersistenceTest.java @@ -0,0 +1,143 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.provider.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.Base64; +import java.util.Map; + +import lombok.NonNull; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProvider; +import org.onap.policy.models.provider.PolicyModelsProviderFactory; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test persistence of monitoring policies to and from the database. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PolicyLegacyGuardPersistenceTest { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyGuardPersistenceTest.class); + + private StandardCoder standardCoder; + + private PolicyModelsProvider databaseProvider; + + // @formatter:off + private String[] policyInputResourceNames = { + "policies/vDNS.policy.guard.frequency.input.json", + "policies/vDNS.policy.guard.minmax.input.json" + }; + + private String[] policyOutputResourceNames = { + "policies/vDNS.policy.guard.frequency.output.json", + "policies/vDNS.policy.guard.minmax.output.json" + }; + // @formatter:on + + /** + * Initialize provider. + * + * @throws PfModelException on exceptions in the tests + */ + @Before + public void setupParameters() throws PfModelException { + PolicyModelsProviderParameters parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); + parameters.setDatabaseUser("policy"); + parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); + parameters.setPersistenceUnit("ToscaConceptTest"); + + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider.init(); + } + + /** + * Set up standard coder. + */ + @Before + public void setupStandardCoder() { + standardCoder = new StandardCoder(); + } + + @After + public void teardown() throws Exception { + databaseProvider.close(); + } + + @Test + public void testPolicyPersistence() { + try { + for (int i = 0; i < policyInputResourceNames.length; i++) { + String policyInputString = ResourceUtils.getResourceAsString(policyInputResourceNames[i]); + String policyOutputString = ResourceUtils.getResourceAsString(policyOutputResourceNames[i]); + testJsonStringPolicyPersistence(policyInputString, policyOutputString); + } + } catch (Exception exc) { + LOGGER.warn("error processing policies", exc); + fail("test should not throw an exception"); + } + } + + /** + * Check persistence of a policy. + * + * @param policyInputString the policy as a string + * @param policyOutputString the expected output string + * @throws Exception any exception thrown + */ + public void testJsonStringPolicyPersistence(@NonNull final String policyInputString, + final String policyOutputString) throws Exception { + LegacyGuardPolicyInput gip = standardCoder.decode(policyInputString, LegacyGuardPolicyInput.class); + + assertNotNull(gip); + + Map<String, LegacyGuardPolicyOutput> createdGopm = databaseProvider.createGuardPolicy(gip); + assertEquals(gip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(gip.getContent(), + createdGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + + Map<String, LegacyGuardPolicyOutput> gotGopm = databaseProvider.getGuardPolicy(gip.getPolicyId()); + assertEquals(gip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(gip.getContent(), + gotGopm.get(gip.getPolicyId()).getProperties().values().iterator().next()); + + String actualRetrievedJson = standardCoder.encode(gotGopm); + + // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters + assertEquals(policyOutputString.replaceAll("\\s+", ""), actualRetrievedJson.replaceAll("\\s+", "")); + } +} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java index 90d00fc58..60d577cb9 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyLegacyOperationalPersistenceTest.java @@ -24,8 +24,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; -import com.google.gson.Gson; - import java.util.Base64; import lombok.NonNull; @@ -33,6 +31,7 @@ import lombok.NonNull; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; @@ -51,7 +50,7 @@ public class PolicyLegacyOperationalPersistenceTest { // Logger for this class private static final Logger LOGGER = LoggerFactory.getLogger(PolicyLegacyOperationalPersistenceTest.class); - private Gson gson; + private StandardCoder standardCoder; private PolicyModelsProvider databaseProvider; @@ -87,11 +86,11 @@ public class PolicyLegacyOperationalPersistenceTest { } /** - * Set up GSON. + * Set up standard coder. */ @Before - public void setupGson() { - gson = new Gson(); + public void setupStandardCoder() { + standardCoder = new StandardCoder(); } @After @@ -122,7 +121,7 @@ public class PolicyLegacyOperationalPersistenceTest { */ public void testJsonStringPolicyPersistence(@NonNull final String policyInputString, final String policyOutputString) throws Exception { - LegacyOperationalPolicy lop = gson.fromJson(policyInputString, LegacyOperationalPolicy.class); + LegacyOperationalPolicy lop = standardCoder.decode(policyInputString, LegacyOperationalPolicy.class); assertNotNull(lop); @@ -132,7 +131,7 @@ public class PolicyLegacyOperationalPersistenceTest { LegacyOperationalPolicy gotLop = databaseProvider.getOperationalPolicy(lop.getPolicyId()); assertEquals(gotLop, lop); - String actualRetrievedJson = gson.toJson(gotLop); + String actualRetrievedJson = standardCoder.encode(gotLop); // All of this dash/underscore stuff is to avoid a checkstyle error around escaping unicode characters assertEquals( |