X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-provider%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fprovider%2Fimpl%2FDatabasePolicyModelsProviderTest.java;h=aa6802a5a63f8226f2fe3d1e034b2de9e49d31e5;hb=88bcb550c2efd5e43ad3d256fe075a6bf7e90538;hp=e4ecb9d1d91e1900c0ebd3177346f9e4ec7c2677;hpb=cee84a9344f35356f86384b0d48127f5a83e3776;p=policy%2Fmodels.git 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..aa6802a5a 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 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. @@ -20,25 +21,35 @@ package org.onap.policy.models.provider.impl; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.Base64; +import java.util.Date; +import java.util.List; 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.base.PfModelException; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroupFilter; +import org.onap.policy.models.pdp.concepts.PdpStatistics; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; -import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Test the database models provider implementation. @@ -46,7 +57,29 @@ import org.slf4j.LoggerFactory; * @author Liam Fallon (liam.fallon@est.tech) */ public class DatabasePolicyModelsProviderTest { - private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class); + private static final String NAME = "name"; + + private static final String TEMPLATE_IS_NULL = "^serviceTemplate is marked .*on.*ull but is null$"; + + private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$"; + + private static final String SUBGROUP_IS_NULL = "^pdpSubGroup is marked .*on.*ull but is null$"; + + private static final String GROUP_IS_NULL = "^pdpGroupName is marked .*on.*ull but is null$"; + + private static final String NAME_IS_NULL = "^name is marked .*on.*ull but is null$"; + + private static final String FILTER_IS_NULL = "^filter is marked .*on.*ull but is null$"; + + private static final String POLICY_ID = "policy_id"; + + private static final String GROUP = "group"; + + private static final String VERSION_100 = "1.0.0"; + + private static final Date TIMESTAMP = new Date(); + + private static final String ORDER = "DESC"; PolicyModelsProviderParameters parameters; @@ -56,198 +89,255 @@ public class DatabasePolicyModelsProviderTest { @Before public void setupParameters() { parameters = new PolicyModelsProviderParameters(); + parameters.setDatabaseDriver("org.h2.Driver"); parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); parameters.setDatabaseUser("policy"); parameters.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes())); parameters.setPersistenceUnit("ToscaConceptTest"); - } @Test public void testInitAndClose() throws Exception { + assertThatThrownBy(() -> { + new DatabasePolicyModelsProviderImpl(null); + }).hasMessageMatching("^parameters is marked .*on.*ull but is null$"); + 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"); - } + databaseProvider.close(); + databaseProvider.init(); + + databaseProvider.close(); + + parameters.setDatabaseUrl("jdbc:h2:mem:testdb"); 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()); - } + + assertThatThrownBy(databaseProvider::init).hasMessageContaining("could not create Data Access Object (DAO)"); + parameters.setPersistenceUnit("ToscaConceptTest"); - try { + databaseProvider.init(); + databaseProvider.close(); + + assertThatThrownBy(() -> { 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()); - } + }).hasMessage("provider is already initialized"); + + databaseProvider.close(); + + databaseProvider.close(); } @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 { + assertThatThrownBy(() -> { + databaseProvider.getFilteredPolicyTypes(null); + }).hasMessageMatching(FILTER_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.getFilteredPolicyTypeList(null); + }).hasMessageMatching(FILTER_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 { + }).hasMessageMatching(TEMPLATE_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 { - 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 { + }).hasMessageMatching(TEMPLATE_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deletePolicyType(null, null); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deletePolicyType("aaa", null); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.deletePolicyType(null, "aaa"); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.getFilteredPolicies(null); + }).hasMessageMatching(FILTER_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.getFilteredPolicyList(null); + }).hasMessageMatching(FILTER_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 { + }).hasMessageMatching(TEMPLATE_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 { - 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 { + }).hasMessageMatching(TEMPLATE_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deletePolicy(null, null); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deletePolicy(null, "aaa"); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deletePolicy("aaa", null); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy(null, null); + }).hasMessageMatching(POLICY_ID_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy(null, ""); + }).hasMessageMatching(POLICY_ID_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy("", null); + }).hasMessage("no policy found for policy: :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 { + }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull 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 { - 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 { + }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy(null, null); + }).hasMessageMatching(POLICY_ID_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy(null, ""); + }).hasMessageMatching(POLICY_ID_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy("", null); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy(null, null); + }).hasMessageMatching(POLICY_ID_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy(null, ""); + }).hasMessageMatching(POLICY_ID_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy("", null); + }).hasMessage("no policy found for policy: :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 { + }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull 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 { - 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 { + }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy(null, null); + }).hasMessageMatching(POLICY_ID_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy(null, ""); + }).hasMessageMatching(POLICY_ID_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deleteGuardPolicy("", null); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.getFilteredPdpGroups(null); + }).hasMessageMatching(FILTER_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 { + }).hasMessageMatching("^pdpGroups is marked .*on.*ull 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 { - databaseProvider.deletePdpGroups(null); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("pdpGroupFilter is marked @NonNull but is null", npe.getMessage()); - } + }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(null, null); + }).hasMessageMatching(GROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(null, new PdpSubGroup()); + }).hasMessageMatching(GROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(NAME, null); + }).hasMessageMatching(SUBGROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup()); + }).hasMessage("parameter \"localName\" is null"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, null, null); + }).hasMessageMatching(GROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, null, new Pdp()); + }).hasMessageMatching(GROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, "sub", null); + }).hasMessageMatching(GROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(null, "sub", new Pdp()); + }).hasMessageMatching(GROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(NAME, null, null); + }).hasMessageMatching(SUBGROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(NAME, null, new Pdp()); + }).hasMessageMatching(SUBGROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(NAME, "sub", null); + }).hasMessageMatching("^pdp is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdp(NAME, "sub", new Pdp()); + }).hasMessage("parameter \"localName\" is null"); + + assertThatThrownBy(() -> { + databaseProvider.deletePdpGroup(null); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP, ORDER, 0); + }).hasMessageMatching(GROUP_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.createPdpStatistics(null); + }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.updatePdpStatistics(null); + }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { + databaseProvider.deletePdpStatistics(null, TIMESTAMP); + }).hasMessageMatching(NAME_IS_NULL); databaseProvider.close(); } @@ -256,104 +346,194 @@ public class DatabasePolicyModelsProviderTest { 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()); - } + + databaseProvider.close(); + + assertThatThrownBy(() -> { + databaseProvider.getPolicyTypes(NAME, "version"); + }).hasMessage("policy models provider is not initilaized"); } @Test - public void testProviderMethods() { - try (PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { - databaseProvider.init(); + public void testProviderMethods() throws PfModelException { + PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + + assertThatThrownBy(() -> databaseProvider.getPolicyTypes(NAME, VERSION_100)) + .hasMessage("service template not found in database"); + + assertTrue(databaseProvider.getPolicyTypeList(NAME, VERSION_100).isEmpty()); + + assertThatThrownBy(() -> databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build())) + .hasMessage("service template not found in database"); + + assertTrue(databaseProvider.getFilteredPolicyTypeList(ToscaPolicyTypeFilter.builder().build()).isEmpty()); + + assertThatThrownBy(() -> { + databaseProvider.createPolicyTypes(new ToscaServiceTemplate()); + }).hasMessage("no policy types specified on service template"); + + assertThatThrownBy(() -> { + databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()); + }).hasMessage("no policy types specified on service template"); + + assertThatThrownBy(() -> databaseProvider.deletePolicyType(NAME, VERSION_100)) + .hasMessage("service template not found in database"); + + assertThatThrownBy(() -> databaseProvider.getPolicies(NAME, VERSION_100)) + .hasMessage("service template not found in database"); + + assertTrue(databaseProvider.getPolicyList(NAME, VERSION_100).isEmpty()); + + assertThatThrownBy(() -> databaseProvider.getFilteredPolicies(ToscaPolicyFilter.builder().build())) + .hasMessage("service template not found in database"); - try { - 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 { - databaseProvider.createPolicyTypes(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("no policy types specified on service template", npe.getMessage()); - } - try { - databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("no policy types specified on service template", npe.getMessage()); - } - try { - databaseProvider.deletePolicyTypes(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy type not found: NULL:0.0.0", npe.getMessage()); - } - - try { - databaseProvider.getPolicies(new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception npe) { - assertEquals("policy not found: NULL:0.0.0", npe.getMessage()); - } - try { - databaseProvider.createPolicies(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("topology template not specified on service template", npe.getMessage()); - } - try { - databaseProvider.updatePolicies(new ToscaServiceTemplate()); - } catch (Exception npe) { - assertEquals("topology template not specified on service template", npe.getMessage()); - } - try { - 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")); - - assertNotNull(databaseProvider.getPdpGroups("filter")); - assertNotNull(databaseProvider.createPdpGroups(new PdpGroups())); - assertNotNull(databaseProvider.updatePdpGroups(new PdpGroups())); - assertNotNull(databaseProvider.deletePdpGroups("filter")); - - } catch (Exception exc) { - LOGGER.warn("test should not throw an exception", exc); - fail("test should not throw an exception"); - } + assertTrue(databaseProvider.getFilteredPolicyList(ToscaPolicyFilter.builder().build()).isEmpty()); + + assertThatThrownBy(() -> { + databaseProvider.createPolicies(new ToscaServiceTemplate()); + }).hasMessage("topology template not specified on service template"); + + assertThatThrownBy(() -> { + databaseProvider.updatePolicies(new ToscaServiceTemplate()); + }).hasMessage("topology template not specified on service template"); + + assertThatThrownBy(() -> databaseProvider.deletePolicy("Policy", "0.0.0").getToscaTopologyTemplate()) + .hasMessage("service template not found in database"); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy(POLICY_ID, null); + }).hasMessage("no policy found for policy: policy_id:null"); + + assertThatThrownBy(() -> { + databaseProvider.getOperationalPolicy(POLICY_ID, "10"); + }).hasMessage("no policy found for policy: policy_id:10"); + + assertThatThrownBy(() -> { + databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()); + }).hasMessageMatching(NAME_IS_NULL); + + assertThatThrownBy(() -> { + databaseProvider.deleteOperationalPolicy(POLICY_ID, "55"); + }).hasMessage("no policy found for policy: policy_id:55"); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy(POLICY_ID, null); + }).hasMessage("no policy found for policy: policy_id:null"); + + assertThatThrownBy(() -> { + databaseProvider.getGuardPolicy(POLICY_ID, "6"); + }).hasMessage("no policy found for policy: policy_id:6"); + + 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, "33"); + }).hasMessage("no policy found for policy: policy_id:33"); + + assertEquals(0, databaseProvider.getPdpGroups(NAME).size()); + assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); + + assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); + assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName(GROUP); + pdpGroup.setVersion("1.2.3"); + pdpGroup.setPdpGroupState(PdpState.ACTIVE); + pdpGroup.setPdpSubgroups(new ArrayList<>()); + List groupList = new ArrayList<>(); + groupList.add(pdpGroup); + + PdpSubGroup pdpSubGroup = new PdpSubGroup(); + pdpSubGroup.setPdpType("type"); + pdpSubGroup.setDesiredInstanceCount(123); + pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier("type", "7.8.9")); + pdpGroup.getPdpSubgroups().add(pdpSubGroup); + + Pdp pdp = new Pdp(); + pdp.setInstanceId("type-0"); + pdp.setMessage("Hello"); + pdp.setPdpState(PdpState.ACTIVE); + pdp.setHealthy(PdpHealthStatus.UNKNOWN); + pdpSubGroup.setPdpInstances(new ArrayList<>()); + pdpSubGroup.getPdpInstances().add(pdp); + + PdpStatistics pdpStatistics = new PdpStatistics(); + pdpStatistics.setPdpInstanceId(NAME); + pdpStatistics.setTimeStamp(new Date()); + pdpStatistics.setPdpGroupName(GROUP); + pdpStatistics.setPdpSubGroupName("type"); + ArrayList statisticsArrayList = new ArrayList<>(); + statisticsArrayList.add(pdpStatistics); + + assertEquals(123, + databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount()); + assertEquals(1, databaseProvider.getPdpGroups(GROUP).size()); + + pdpSubGroup.setDesiredInstanceCount(234); + databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup); + assertEquals(234, + databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount()); + + assertEquals("Hello", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances() + .get(0).getMessage()); + pdp.setMessage("Howdy"); + databaseProvider.updatePdp(GROUP, "type", pdp); + assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances() + .get(0).getMessage()); + + assertThatThrownBy(() -> { + databaseProvider.deletePdpGroup(NAME); + }).hasMessage("delete of PDP group \"name:0.0.0\" failed, PDP group does not exist"); + + assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName()); + + assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); + assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size()); + assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size()); + + assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null, ORDER, 0).get(0) + .getPdpInstanceId()); + assertEquals(0, + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null, ORDER, 0).size()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date(), ORDER, 0) + .get(0).getPdpInstanceId()); + assertEquals(0, + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date(), ORDER, 0).size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null, ORDER, 0).get(0) + .getPdpInstanceId()); + assertEquals(0, + databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date(), ORDER, 0).size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 0).get(0) + .getPdpInstanceId()); + assertEquals(0, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 0) + .size()); + + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 1).get(0) + .getPdpInstanceId()); + assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null, ORDER, 5).get(0) + .getPdpInstanceId()); + assertEquals(0, databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date(), ORDER, 5) + .size()); + + assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId()); + assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); + + databaseProvider.close(); } }