From f69b6681486e4d1c5859f649a3293488c1859712 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 7 Jan 2020 10:24:12 +0000 Subject: Support persistence of data types Data types must be persisted to the database in order for them to be retrieved in policy type queries and later for policy validation. Issue-ID: POLICY-2315 Change-Id: Iacb88501a597aeee8f6d8bcc3d9604c13dc09090 Signed-off-by: liamfallon --- .../impl/DatabasePolicyModelsProviderImpl.java | 19 ++- .../impl/DummyPolicyModelsProviderImpl.java | 4 +- .../provider/PolicyModelsProviderFactoryTest.java | 22 +-- .../impl/DatabasePolicyModelsProviderTest.java | 150 ++++++++++---------- .../impl/DummyPolicyModelsProviderTest.java | 12 +- .../provider/AuthorativeToscaProvider.java | 8 +- .../tosca/simple/provider/SimpleToscaProvider.java | 124 +++++++++++++++- .../onap/policy/models/tosca/utils/ToscaUtils.java | 29 +++- .../AuthorativeToscaProviderPolicyTest.java | 81 +++++------ .../AuthorativeToscaProviderPolicyTypeTest.java | 70 +++++---- .../provider/LegacyProvider4LegacyGuardTest.java | 84 +++++------ .../LegacyProvider4LegacyOperationalTest.java | 55 ++++---- .../simple/provider/SimpleToscaProviderTest.java | 156 ++++++++++++++++----- .../policy/models/tosca/utils/ToscaUtilsTest.java | 68 ++++++++- .../src/test/resources/META-INF/persistence.xml | 9 +- 15 files changed, 573 insertions(+), 318 deletions(-) diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java index 1ac24585f..d154910bb 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java @@ -1,6 +1,6 @@ /*- * ============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"); @@ -26,8 +26,11 @@ import java.util.Date; import java.util.List; import java.util.Map; import java.util.Properties; + import javax.ws.rs.core.Response; + import lombok.NonNull; + import org.eclipse.persistence.config.PersistenceUnitProperties; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; @@ -65,7 +68,7 @@ import org.slf4j.LoggerFactory; */ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { - private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class); + private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderImpl.class); private final PolicyModelsProviderParameters parameters; @@ -116,7 +119,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { } catch (Exception exc) { String errorMessage = "could not create Data Access Object (DAO) using url \"" + parameters.getDatabaseUrl() + "\" and persistence unit \"" + parameters.getPersistenceUnit() + "\""; - LOGGER.warn(errorMessage, exc); this.close(); throw new PfModelException(Response.Status.NOT_ACCEPTABLE, errorMessage, exc); @@ -207,7 +209,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter); } - @Override public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { @@ -265,15 +266,15 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { } @Override - public Map createGuardPolicy( - @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + public Map + createGuardPolicy(@NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { assertInitialized(); return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy); } @Override - public Map updateGuardPolicy( - @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { + public Map + updateGuardPolicy(@NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { assertInitialized(); return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy); } @@ -291,7 +292,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { return new PdpProvider().getPdpGroups(pfDao, name); } - @Override public List getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException { assertInitialized(); @@ -357,7 +357,6 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { return new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsList); } - @Override public List deletePdpStatistics(@NonNull final String name, final Date timestamp) throws PfModelException { diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java index 2c88bcacd..c0a6e2c2a 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,7 +26,9 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; + import javax.ws.rs.core.Response; + import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; 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 index 5e87f808d..bdbf5b353 100644 --- 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 @@ -1,6 +1,6 @@ /*- * ============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"); @@ -24,6 +24,7 @@ package org.onap.policy.models.provider; import static org.assertj.core.api.Assertions.assertThatThrownBy; import lombok.ToString; + import org.junit.Test; /** @@ -38,38 +39,37 @@ public class PolicyModelsProviderFactoryTest { public void testFactory() { PolicyModelsProviderFactory factory = new PolicyModelsProviderFactory(); + // @formatter:off assertThatThrownBy(() -> { factory.createPolicyModelsProvider(null); - }).hasMessage("parameters is marked @NonNull but is null"); + }) .hasMessageMatching("^parameters is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); pars.setImplementation(null); factory.createPolicyModelsProvider(pars); - }).hasMessage("could not find implementation of the \"PolicyModelsProvider\" interface \"null\""); + }) .hasMessage("could not find implementation of the \"PolicyModelsProvider\" interface \"null\""); assertThatThrownBy(() -> { PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); pars.setImplementation("com.acmecorp.RoadRunner"); factory.createPolicyModelsProvider(pars); - }) - .hasMessage("could not find implementation of the \"PolicyModelsProvider\" " - + "interface \"com.acmecorp.RoadRunner\""); + }) .hasMessage("could not find implementation of the \"PolicyModelsProvider\" " + + "interface \"com.acmecorp.RoadRunner\""); assertThatThrownBy(() -> { PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); pars.setImplementation("java.lang.String"); factory.createPolicyModelsProvider(pars); - }) - .hasMessage( + }) .hasMessage( "the class \"java.lang.String\" is not an implementation of the \"PolicyModelsProvider\" interface"); assertThatThrownBy(() -> { PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); pars.setImplementation("org.onap.policy.models.provider.impl.DummyBadProviderImpl"); factory.createPolicyModelsProvider(pars); - }) - .hasMessage("could not create an instance of PolicyModelsProvider " - + "\"org.onap.policy.models.provider.impl.DummyBadProviderImpl\""); + }) .hasMessage("could not create an instance of PolicyModelsProvider " + + "\"org.onap.policy.models.provider.impl.DummyBadProviderImpl\""); + // @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 index f8a3490cc..02481ca12 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,6 @@ /*- * ============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"); @@ -61,21 +61,17 @@ import org.slf4j.LoggerFactory; public class DatabasePolicyModelsProviderTest { private static final String NAME = "name"; - private static final String TEMPLATE_IS_NULL = "serviceTemplate is marked @NonNull but is null"; + 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 @NonNull but is null"; + private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$"; - private static final String PDP_TYPE_IS_NULL = "pdpType is marked @NonNull but is null"; + private static final String SUBGROUP_IS_NULL = "^pdpSubGroup is marked .*on.*ull but is null$"; - private static final String SUBGROUP_IS_NULL = "pdpSubGroup is marked @NonNull but is null"; + private static final String GROUP_IS_NULL = "^pdpGroupName is marked .*on.*ull but is null$"; - private static final String GROUP_IS_NULL = "pdpGroupName is marked @NonNull but is null"; + private static final String NAME_IS_NULL = "^name is marked .*on.*ull but is null$"; - private static final String NAME_IS_NULL = "name is marked @NonNull but is null"; - - private static final String FILTER_IS_NULL = "filter is marked @NonNull but is null"; - - private static final String INSTANCE = "Instance"; + private static final String FILTER_IS_NULL = "^filter is marked .*on.*ull but is null$"; private static final String POLICY_ID = "policy_id"; @@ -106,7 +102,7 @@ public class DatabasePolicyModelsProviderTest { public void testInitAndClose() throws Exception { assertThatThrownBy(() -> { new DatabasePolicyModelsProviderImpl(null); - }).hasMessage("parameters is marked @NonNull but is null"); + }).hasMessageMatching("^parameters is marked .*on.*ull but is null$"); PolicyModelsProvider databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); @@ -143,71 +139,71 @@ public class DatabasePolicyModelsProviderTest { public void testProviderMethodsNull() throws Exception { try (PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { assertThatThrownBy(() -> { databaseProvider.getFilteredPolicyTypes(null); - }).hasMessage(FILTER_IS_NULL); + }).hasMessageMatching(FILTER_IS_NULL); assertThatThrownBy(() -> { databaseProvider.getFilteredPolicyTypeList(null); - }).hasMessage(FILTER_IS_NULL); + }).hasMessageMatching(FILTER_IS_NULL); assertThatThrownBy(() -> { databaseProvider.createPolicyTypes(null); - }).hasMessage(TEMPLATE_IS_NULL); + }).hasMessageMatching(TEMPLATE_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePolicyTypes(null); - }).hasMessage(TEMPLATE_IS_NULL); + }).hasMessageMatching(TEMPLATE_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deletePolicyType(null, null); - }).hasMessage(NAME_IS_NULL); + }).hasMessageMatching(NAME_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deletePolicyType("aaa", null); - }).hasMessage("version is marked @NonNull but is null"); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.deletePolicyType(null, "aaa"); - }).hasMessage(NAME_IS_NULL); + }).hasMessageMatching(NAME_IS_NULL); assertThatThrownBy(() -> { databaseProvider.getFilteredPolicies(null); - }).hasMessage(FILTER_IS_NULL); + }).hasMessageMatching(FILTER_IS_NULL); assertThatThrownBy(() -> { databaseProvider.getFilteredPolicyList(null); - }).hasMessage(FILTER_IS_NULL); + }).hasMessageMatching(FILTER_IS_NULL); assertThatThrownBy(() -> { databaseProvider.createPolicies(null); - }).hasMessage(TEMPLATE_IS_NULL); + }).hasMessageMatching(TEMPLATE_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePolicies(null); - }).hasMessage(TEMPLATE_IS_NULL); + }).hasMessageMatching(TEMPLATE_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deletePolicy(null, null); - }).hasMessage(NAME_IS_NULL); + }).hasMessageMatching(NAME_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deletePolicy(null, "aaa"); - }).hasMessage(NAME_IS_NULL); + }).hasMessageMatching(NAME_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deletePolicy("aaa", null); - }).hasMessage("version is marked @NonNull but is null"); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.getOperationalPolicy(null, null); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { databaseProvider.getOperationalPolicy(null, ""); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { databaseProvider.getOperationalPolicy("", null); @@ -215,31 +211,31 @@ public class DatabasePolicyModelsProviderTest { assertThatThrownBy(() -> { databaseProvider.createOperationalPolicy(null); - }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.updateOperationalPolicy(null); - }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.deleteOperationalPolicy(null, null); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deleteOperationalPolicy(null, ""); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deleteOperationalPolicy("", null); - }).hasMessage("policyVersion is marked @NonNull but is null"); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.getGuardPolicy(null, null); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { databaseProvider.getGuardPolicy(null, ""); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { databaseProvider.getGuardPolicy("", null); @@ -247,47 +243,47 @@ public class DatabasePolicyModelsProviderTest { assertThatThrownBy(() -> { databaseProvider.createGuardPolicy(null); - }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.updateGuardPolicy(null); - }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + }).hasMessageMatching("^legacyGuardPolicy is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.deleteGuardPolicy(null, null); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deleteGuardPolicy(null, ""); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deleteGuardPolicy("", null); - }).hasMessage("policyVersion is marked @NonNull but is null"); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.getFilteredPdpGroups(null); - }).hasMessage(FILTER_IS_NULL); + }).hasMessageMatching(FILTER_IS_NULL); assertThatThrownBy(() -> { databaseProvider.createPdpGroups(null); - }).hasMessage("pdpGroups is marked @NonNull but is null"); + }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.updatePdpGroups(null); - }).hasMessage("pdpGroups is marked @NonNull but is null"); + }).hasMessageMatching("^pdpGroups is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.updatePdpSubGroup(null, null); - }).hasMessage(GROUP_IS_NULL); + }).hasMessageMatching(GROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdpSubGroup(null, new PdpSubGroup()); - }).hasMessage(GROUP_IS_NULL); + }).hasMessageMatching(GROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdpSubGroup(NAME, null); - }).hasMessage(SUBGROUP_IS_NULL); + }).hasMessageMatching(SUBGROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdpSubGroup(NAME, new PdpSubGroup()); @@ -295,31 +291,31 @@ public class DatabasePolicyModelsProviderTest { assertThatThrownBy(() -> { databaseProvider.updatePdp(null, null, null); - }).hasMessage(GROUP_IS_NULL); + }).hasMessageMatching(GROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdp(null, null, new Pdp()); - }).hasMessage(GROUP_IS_NULL); + }).hasMessageMatching(GROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdp(null, "sub", null); - }).hasMessage(GROUP_IS_NULL); + }).hasMessageMatching(GROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdp(null, "sub", new Pdp()); - }).hasMessage(GROUP_IS_NULL); + }).hasMessageMatching(GROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdp(NAME, null, null); - }).hasMessage(SUBGROUP_IS_NULL); + }).hasMessageMatching(SUBGROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdp(NAME, null, new Pdp()); - }).hasMessage(SUBGROUP_IS_NULL); + }).hasMessageMatching(SUBGROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updatePdp(NAME, "sub", null); - }).hasMessage("pdp is marked @NonNull but is null"); + }).hasMessageMatching("^pdp is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.updatePdp(NAME, "sub", new Pdp()); @@ -327,23 +323,23 @@ public class DatabasePolicyModelsProviderTest { assertThatThrownBy(() -> { databaseProvider.deletePdpGroup(null); - }).hasMessage(NAME_IS_NULL); + }).hasMessageMatching(NAME_IS_NULL); assertThatThrownBy(() -> { databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP); - }).hasMessage(GROUP_IS_NULL); + }).hasMessageMatching(GROUP_IS_NULL); assertThatThrownBy(() -> { databaseProvider.createPdpStatistics(null); - }).hasMessage("pdpStatisticsList is marked @NonNull but is null"); + }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.updatePdpStatistics(null); - }).hasMessage("pdpStatisticsList is marked @NonNull but is null"); + }).hasMessageMatching("^pdpStatisticsList is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { databaseProvider.deletePdpStatistics(null, TIMESTAMP); - }).hasMessage(NAME_IS_NULL); + }).hasMessageMatching(NAME_IS_NULL); } } @@ -411,11 +407,11 @@ public class DatabasePolicyModelsProviderTest { assertThatThrownBy(() -> { databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()); - }).hasMessage(NAME_IS_NULL); + }).hasMessageMatching(NAME_IS_NULL); assertThatThrownBy(() -> { databaseProvider.updateOperationalPolicy(new LegacyOperationalPolicy()); - }).hasMessage(NAME_IS_NULL); + }).hasMessageMatching(NAME_IS_NULL); assertThatThrownBy(() -> { databaseProvider.deleteOperationalPolicy(POLICY_ID, "55"); @@ -483,15 +479,15 @@ public class DatabasePolicyModelsProviderTest { pdpSubGroup.setDesiredInstanceCount(234); databaseProvider.updatePdpSubGroup(GROUP, pdpSubGroup); - assertEquals(234, databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups() - .get(0).getDesiredInstanceCount()); + 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()); + 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()); + assertEquals("Howdy", databaseProvider.getPdpGroups(GROUP).get(0).getPdpSubgroups().get(0).getPdpInstances() + .get(0).getMessage()); assertThatThrownBy(() -> { databaseProvider.deletePdpGroup(NAME); @@ -499,23 +495,23 @@ public class DatabasePolicyModelsProviderTest { assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName()); - assertEquals(0, databaseProvider.getPdpStatistics(null,null).size()); + assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); - databaseProvider.getFilteredPdpStatistics(null, GROUP,null, null, null); - databaseProvider.getFilteredPdpStatistics(null, GROUP,null, new Date(), null); - databaseProvider.getFilteredPdpStatistics(null, GROUP,null, null, new Date()); - databaseProvider.getFilteredPdpStatistics(null, GROUP,null, new Date(), new Date()); + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, null); + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), null); + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, null, new Date()); + databaseProvider.getFilteredPdpStatistics(null, GROUP, null, new Date(), new Date()); - databaseProvider.getFilteredPdpStatistics(NAME, GROUP,null, null, null); - databaseProvider.getFilteredPdpStatistics(NAME, GROUP,null, new Date(), new Date()); + databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, null, null); + databaseProvider.getFilteredPdpStatistics(NAME, GROUP, null, new Date(), new Date()); - databaseProvider.getFilteredPdpStatistics(NAME, GROUP,"type", null, null); - databaseProvider.getFilteredPdpStatistics(NAME, GROUP,"type", new Date(), new Date()); + databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", null, null); + databaseProvider.getFilteredPdpStatistics(NAME, GROUP, "type", new Date(), new Date()); databaseProvider.createPdpStatistics(statisticsArrayList); databaseProvider.updatePdpStatistics(statisticsArrayList); - databaseProvider.deletePdpStatistics("pdp1",null); + databaseProvider.deletePdpStatistics("pdp1", null); } catch (Exception exc) { LOGGER.warn("test should not throw an exception", exc); fail("test should not throw an exception"); diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java index 737e0fc28..452bbd42a 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 @@ -1,6 +1,6 @@ /*- * ============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"); @@ -28,10 +28,10 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; + import org.junit.Test; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; -import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; @@ -60,7 +60,7 @@ public class DummyPolicyModelsProviderTest { parameters.setPersistenceUnit("dummy"); try (PolicyModelsProvider dummyProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { dummyProvider.init(); @@ -79,7 +79,7 @@ public class DummyPolicyModelsProviderTest { parameters.setPersistenceUnit("dummy"); try (PolicyModelsProvider dummyProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { + new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) { dummyProvider.init(); assertNotNull(dummyProvider.getPolicyTypes("name", VERSION)); @@ -124,12 +124,12 @@ public class DummyPolicyModelsProviderTest { @Test public void testDummyResponse() { try (DummyPolicyModelsProviderSubImpl resp = - new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters())) { + new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters())) { assertThatThrownBy(resp::getBadDummyResponse1).hasMessage("error serializing object"); } try (DummyPolicyModelsProviderSubImpl resp = - new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters())) { + new DummyPolicyModelsProviderSubImpl(new PolicyModelsProviderParameters())) { assertThatThrownBy(resp::getBadDummyResponse2).hasMessage("error serializing object"); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java index 8c6e492f7..7999f620b 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -1,6 +1,6 @@ /*- * ============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"); @@ -28,6 +28,7 @@ import java.util.Map; import java.util.TreeMap; import lombok.NonNull; + import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.dao.PfDao; @@ -64,8 +65,9 @@ public class AuthorativeToscaProvider { LOGGER.debug("->getPolicyTypes: name={}, version={}", name, version); - ToscaServiceTemplate serviceTemplate = - new SimpleToscaProvider().getPolicyTypes(dao, name, version).toAuthorative(); + JpaToscaServiceTemplate jpaServiceTemplate = new SimpleToscaProvider().getPolicyTypes(dao, name, version); + + ToscaServiceTemplate serviceTemplate = jpaServiceTemplate.toAuthorative(); LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); return serviceTemplate; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java index 30ab89491..cce6fd9ee 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,8 @@ import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; @@ -54,6 +56,116 @@ import org.slf4j.LoggerFactory; public class SimpleToscaProvider { private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class); + /** + * Get data types. + * + * @param dao the DAO to use to access the database + * @param name the name of the data type to get, set to null to get all policy types + * @param version the version of the data type to get, set to null to get all versions + * @return the data types found + * @throws PfModelException on errors getting data types + */ + public JpaToscaServiceTemplate getDataTypes(@NonNull final PfDao dao, final String name, final String version) + throws PfModelException { + LOGGER.debug("->getDataTypes: name={}, version={}", name, version); + + // Create the structure of the TOSCA service template to contain the data type + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + + // Add the data type to the TOSCA service template + List jpaDataTypeList = dao.getFiltered(JpaToscaDataType.class, name, version); + serviceTemplate.getDataTypes().getConceptMap().putAll(asConceptMap(jpaDataTypeList)); + + LOGGER.debug("<-getDataTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); + return serviceTemplate; + } + + /** + * Create data types. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definition of the data types to be created + * @return the TOSCA service template containing the created data types + * @throws PfModelException on errors creating data types + */ + public JpaToscaServiceTemplate createDataTypes(@NonNull final PfDao dao, + @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException { + LOGGER.debug("->createDataTypes: serviceTempalate={}", serviceTemplate); + + ToscaUtils.assertDataTypesExist(serviceTemplate); + + for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) { + dao.create(dataType); + } + + // Return the created Data types + JpaToscaDataTypes returnDataTypes = new JpaToscaDataTypes(); + + for (PfConceptKey dataTypeKey : serviceTemplate.getDataTypes().getConceptMap().keySet()) { + returnDataTypes.getConceptMap().put(dataTypeKey, dao.get(JpaToscaDataType.class, dataTypeKey)); + } + + JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate(); + returnServiceTemplate.setDataTypes(returnDataTypes); + + LOGGER.debug("<-createDataTypes: returnServiceTempalate={}", returnServiceTemplate); + return returnServiceTemplate; + } + + /** + * Update Data types. + * + * @param dao the DAO to use to access the database + * @param serviceTemplate the service template containing the definition of the data types to be modified + * @return the TOSCA service template containing the modified data types + * @throws PfModelException on errors updating Data types + */ + public JpaToscaServiceTemplate updateDataTypes(@NonNull final PfDao dao, + @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException { + LOGGER.debug("->updateDataTypes: serviceTempalate={}", serviceTemplate); + + ToscaUtils.assertDataTypesExist(serviceTemplate); + + for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) { + dao.update(dataType); + } + + // Return the created data types + JpaToscaDataTypes returnDataTypes = new JpaToscaDataTypes(); + + for (PfConceptKey dataTypeKey : serviceTemplate.getDataTypes().getConceptMap().keySet()) { + returnDataTypes.getConceptMap().put(dataTypeKey, dao.get(JpaToscaDataType.class, dataTypeKey)); + } + + JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate(); + returnServiceTemplate.setDataTypes(returnDataTypes); + + LOGGER.debug("<-updateDataTypes: returnServiceTempalate={}", returnServiceTemplate); + return returnServiceTemplate; + } + + /** + * Delete Data types. + * + * @param dao the DAO to use to access the database + * @param dataTypeKey the data type key for the Data types to be deleted, if the version of the key is null, all + * versions of the data type are deleted. + * @return the TOSCA service template containing the data types that were deleted + * @throws PfModelException on errors deleting data types + */ + public JpaToscaServiceTemplate deleteDataType(@NonNull final PfDao dao, @NonNull final PfConceptKey dataTypeKey) + throws PfModelException { + LOGGER.debug("->deleteDataType: key={}", dataTypeKey); + + JpaToscaServiceTemplate serviceTemplate = getDataTypes(dao, dataTypeKey.getName(), dataTypeKey.getVersion()); + + dao.delete(JpaToscaDataType.class, dataTypeKey); + + LOGGER.debug("<-deleteDataType: key={}, serviceTempalate={}", dataTypeKey, serviceTemplate); + return serviceTemplate; + } + /** * Get policy types. * @@ -75,7 +187,7 @@ public class SimpleToscaProvider { List jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version); serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); - LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate=", name, version, serviceTemplate); + LOGGER.debug("<-getPolicyTypes: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); return serviceTemplate; } @@ -112,7 +224,7 @@ public class SimpleToscaProvider { } /** - * Create policy types. + * Update policy types. * * @param dao the DAO to use to access the database * @param serviceTemplate the service template containing the definition of the policy types to be modified @@ -161,7 +273,7 @@ public class SimpleToscaProvider { dao.delete(JpaToscaPolicyType.class, policyTypeKey); - LOGGER.debug("<-deletePolicyType: key={}, serviceTempalate=", policyTypeKey, serviceTemplate); + LOGGER.debug("<-deletePolicyType: key={}, serviceTempalate={}", policyTypeKey, serviceTemplate); return serviceTemplate; } @@ -187,7 +299,7 @@ public class SimpleToscaProvider { List jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version); serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); - LOGGER.debug("<-getPolicies: name={}, version={}, serviceTemplate=", name, version, serviceTemplate); + LOGGER.debug("<-getPolicies: name={}, version={}, serviceTemplate={}", name, version, serviceTemplate); return serviceTemplate; } @@ -273,7 +385,7 @@ public class SimpleToscaProvider { dao.delete(JpaToscaPolicy.class, policyKey); - LOGGER.debug("<-deletePolicy: key={}, serviceTempalate=", policyKey, serviceTemplate); + LOGGER.debug("<-deletePolicy: key={}, serviceTempalate={}", policyKey, serviceTemplate); return serviceTemplate; } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java index 1b509e2a8..23a428b65 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,28 @@ public final class ToscaUtils { } /** - * Check if policy types have been specified is initialized. + * Check if data types have been specified correctly. + * + * @param serviceTemplate the service template containing data types to be checked + */ + public static void assertDataTypesExist(final JpaToscaServiceTemplate serviceTemplate) { + if (serviceTemplate.getDataTypes() == null) { + String errorMessage = "no data types specified on service template"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + if (serviceTemplate.getDataTypes().getConceptMap().isEmpty()) { + String errorMessage = "list of data types specified on service template is empty"; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + } + + /** + * Check if policy types have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked */ public static void assertPolicyTypesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getPolicyTypes() == null) { @@ -60,7 +81,9 @@ public final class ToscaUtils { } /** - * Check if policy types have been specified is initialized. + * Check if policies have been specified correctly. + * + * @param serviceTemplate the service template containing policy types to be checked */ public static void assertPoliciesExist(final JpaToscaServiceTemplate serviceTemplate) { if (serviceTemplate.getTopologyTemplate() == null) { diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java index 8b9003be0..36c66546a 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java @@ -1,6 +1,6 @@ /*- * ============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"); @@ -60,7 +60,7 @@ public class AuthorativeToscaProviderPolicyTest { private static final String VCPE_JSON = "policies/vCPE.policy.monitoring.input.tosca.json"; private static final String POLICY_AND_VERSION = "onap.restart.tca:1.0.0"; private static final String POLICY1 = "onap.restart.tca"; - private static final String DAO_IS_NULL = "dao is marked @NonNull but is null"; + private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; private static final String VERSION_100 = "1.0.0"; private PfDao pfDao; private StandardCoder standardCoder; @@ -108,17 +108,16 @@ public class AuthorativeToscaProviderPolicyTest { public void testPoliciesGet() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().getPolicies(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getPolicyList(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); createPolicyTypes(); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = @@ -141,8 +140,7 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); - List gotPolicyList = - new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100); + List gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); @@ -166,33 +164,32 @@ public class AuthorativeToscaProviderPolicyTest { public void testPoliciesGetFiltered() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicies(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicies(pfDao, null); - }).hasMessage("filter is marked @NonNull but is null"); + }).hasMessageMatching("^filter is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyList(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null); - }).hasMessage("filter is marked @NonNull but is null"); + }).hasMessageMatching("^filter is marked .*on.*ull but is null$"); createPolicyTypes(); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = @@ -229,8 +226,7 @@ public class AuthorativeToscaProviderPolicyTest { assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); - List gotPolicyList = - new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100); + List gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, POLICY1, VERSION_100); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); @@ -254,21 +250,20 @@ public class AuthorativeToscaProviderPolicyTest { public void testPolicyCreate() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(null, new ToscaServiceTemplate()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(pfDao, null); - }).hasMessage("serviceTemplate is marked @NonNull but is null"); + }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); createPolicyTypes(); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = @@ -288,25 +283,24 @@ public class AuthorativeToscaProviderPolicyTest { public void testPolicyUpdate() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicies(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicies(null, new ToscaServiceTemplate()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicies(pfDao, null); - }).hasMessage("serviceTemplate is marked @NonNull but is null"); + }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); createPolicyTypes(); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = @@ -334,37 +328,36 @@ public class AuthorativeToscaProviderPolicyTest { public void testPoliciesDelete() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(null, null, VERSION); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(null, "name", null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(null, "name", VERSION); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(pfDao, null, null); - }).hasMessage("name is marked @NonNull but is null"); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(pfDao, null, VERSION); - }).hasMessage("name is marked @NonNull but is null"); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); - }).hasMessage("version is marked @NonNull but is null"); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); createPolicyTypes(); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_JSON), ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = @@ -399,7 +392,7 @@ public class AuthorativeToscaProviderPolicyTest { assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); - }).hasMessage("version is marked @NonNull but is null"); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java index 20b8e5d1e..c41f5e1f4 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java @@ -1,6 +1,6 @@ /*- * ============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"); @@ -27,9 +27,11 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.gson.GsonBuilder; + import java.util.LinkedHashMap; import java.util.List; import java.util.Properties; + import org.apache.commons.lang3.ObjectUtils; import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; @@ -59,13 +61,12 @@ public class AuthorativeToscaProviderPolicyTypeTest { private static final String POLICY_AFFINITY_VERSION0 = "onap.policies.NoVersion:0.0.0"; private static final String POLICY_AFFINITY = "onap.policies.NoVersion"; private static final String MISSING_POLICY_TYPES = "no policy types specified on service template"; - private static final String DAO_IS_NULL = "dao is marked @NonNull but is null"; + private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; private static final String VERSION_000 = "0.0.0"; private static String yamlAsJsonString; private PfDao pfDao; private StandardCoder standardCoder; - /** * Read the policy type definition. * @@ -73,8 +74,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { */ @BeforeClass public static void readPolicyDefinition() { - String yamlString = - ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml"); + String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml"); Object yamlObject = new Yaml().load(yamlString); yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); @@ -100,7 +100,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver"); jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb"); - daoParameters.setJdbcProperties(jdbcProperties ); + daoParameters.setJdbcProperties(jdbcProperties); pfDao = new PfDaoFactory().createPfDao(daoParameters); pfDao.init(daoParameters); @@ -123,11 +123,11 @@ public class AuthorativeToscaProviderPolicyTypeTest { public void testPolicyTypesGet() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().getPolicyTypes(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getPolicyList(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); @@ -149,13 +149,12 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - List gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, - POLICY_AFFINITY, VERSION_000); + List gotPolicyTypeList = + new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_AFFINITY, VERSION_000); assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); - gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, - POLICY_AFFINITY, null); + gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_AFFINITY, null); assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); @@ -168,32 +167,31 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); } - @Test public void testPolicyTypesGetFiltered() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null); - }).hasMessage("filter is marked @NonNull but is null"); + }).hasMessageMatching("^filter is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null); - }).hasMessage("filter is marked @NonNull but is null"); + }).hasMessageMatching("^filter is marked .*on.*ull but is null$"); ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); @@ -229,8 +227,8 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); - List gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, - POLICY_AFFINITY, VERSION_000); + List gotPolicyTypeList = + new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_AFFINITY, VERSION_000); assertEquals(1, gotPolicyTypeList.size()); assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); @@ -259,15 +257,15 @@ public class AuthorativeToscaProviderPolicyTypeTest { public void testPolicyTypesCreate() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicyTypes(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicyTypes(pfDao, null); - }).hasMessage("serviceTemplate is marked @NonNull but is null"); + }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate(); assertThatThrownBy(() -> { @@ -297,19 +295,19 @@ public class AuthorativeToscaProviderPolicyTypeTest { public void testPolicyTypesUpdate() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicyTypes(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicyTypes(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null); - }).hasMessage("serviceTemplate is marked @NonNull but is null"); + }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$"); ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); @@ -336,31 +334,31 @@ public class AuthorativeToscaProviderPolicyTypeTest { public void testPolicyTypesDelete() throws Exception { assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicyType(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicyType(null, "name", null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null); - }).hasMessage("name is marked @NonNull but is null"); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION); - }).hasMessage("name is marked @NonNull but is null"); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null); - }).hasMessage("version is marked @NonNull but is null"); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); @@ -394,7 +392,7 @@ public class AuthorativeToscaProviderPolicyTypeTest { assertThatThrownBy(() -> { new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null); - }).hasMessage("version is marked @NonNull but is null"); + }).hasMessageMatching("^version is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java index cc510b74f..047ef4891 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java @@ -1,6 +1,6 @@ /*- * ============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"); @@ -53,15 +53,14 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class LegacyProvider4LegacyGuardTest { - private static final String POLICY_ID_IS_NULL = "policyId is marked @NonNull but is null"; + private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$"; private static final String VDNS_OUTPUT_JSON = "policies/vDNS.policy.guard.frequency.output.json"; private static final String VDNS_INPUT_JSON = "policies/vDNS.policy.guard.frequency.input.json"; - private static final String LEGACY_POLICY_IS_NULL = "legacyGuardPolicy is marked @NonNull but is null"; - private static final String DAO_IS_NULL = "dao is marked @NonNull but is null"; + private static final String LEGACY_POLICY_IS_NULL = "^legacyGuardPolicy is marked .*on.*ull but is null$"; + private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; private PfDao pfDao; private StandardCoder standardCoder; - /** * Set up the DAO towards the database. * @@ -105,15 +104,15 @@ public class LegacyProvider4LegacyGuardTest { public void testPoliciesGet() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().getGuardPolicy(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().getGuardPolicy(null, null, ""); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().getGuardPolicy(pfDao, null, null); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist", null); @@ -121,9 +120,8 @@ public class LegacyProvider4LegacyGuardTest { createPolicyTypes(); - LegacyGuardPolicyInput originalGip = standardCoder.decode( - ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), - LegacyGuardPolicyInput.class); + LegacyGuardPolicyInput originalGip = + standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); assertNotNull(originalGip); @@ -140,8 +138,7 @@ public class LegacyProvider4LegacyGuardTest { assertEquals(originalGip.getContent(), gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - String expectedJsonOutput = - ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); + String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); String actualJsonOutput = standardCoder.encode(gotGopm); assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); @@ -165,21 +162,20 @@ public class LegacyProvider4LegacyGuardTest { public void testPolicyCreate() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().createGuardPolicy(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().createGuardPolicy(pfDao, null); - }).hasMessage(LEGACY_POLICY_IS_NULL); + }).hasMessageMatching(LEGACY_POLICY_IS_NULL); createPolicyTypes(); - LegacyGuardPolicyInput originalGip = standardCoder.decode( - ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), - LegacyGuardPolicyInput.class); + LegacyGuardPolicyInput originalGip = + standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); assertNotNull(originalGip); @@ -196,8 +192,7 @@ public class LegacyProvider4LegacyGuardTest { assertEquals(originalGip.getContent(), gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - String expectedJsonOutput = - ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); + String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); String actualJsonOutput = standardCoder.encode(gotGopm); assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); @@ -207,21 +202,20 @@ public class LegacyProvider4LegacyGuardTest { public void testPolicyCreateBad() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().createGuardPolicy(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().createGuardPolicy(pfDao, null); - }).hasMessage(LEGACY_POLICY_IS_NULL); + }).hasMessageMatching(LEGACY_POLICY_IS_NULL); createPolicyTypes(); - LegacyGuardPolicyInput originalGip = standardCoder.decode( - ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), - LegacyGuardPolicyInput.class); + LegacyGuardPolicyInput originalGip = + standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); assertNotNull(originalGip); @@ -236,15 +230,15 @@ public class LegacyProvider4LegacyGuardTest { public void testPolicyUpdate() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().updateGuardPolicy(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().updateGuardPolicy(pfDao, null); - }).hasMessage(LEGACY_POLICY_IS_NULL); + }).hasMessageMatching(LEGACY_POLICY_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput()); @@ -252,9 +246,8 @@ public class LegacyProvider4LegacyGuardTest { createPolicyTypes(); - LegacyGuardPolicyInput originalGip = standardCoder.decode( - ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), - LegacyGuardPolicyInput.class); + LegacyGuardPolicyInput originalGip = + standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); assertNotNull(originalGip); @@ -285,36 +278,35 @@ public class LegacyProvider4LegacyGuardTest { gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe()); } - @Test public void testPoliciesDelete() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(null, null, ""); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(null, "", null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(null, "", ""); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(pfDao, null, null); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(pfDao, null, ""); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(pfDao, "", null); - }).hasMessage("policyVersion is marked @NonNull but is null"); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(pfDao, "IDontExist", "0"); @@ -322,9 +314,8 @@ public class LegacyProvider4LegacyGuardTest { createPolicyTypes(); - LegacyGuardPolicyInput originalGip = standardCoder.decode( - ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), - LegacyGuardPolicyInput.class); + LegacyGuardPolicyInput originalGip = + standardCoder.decode(ResourceUtils.getResourceAsString(VDNS_INPUT_JSON), LegacyGuardPolicyInput.class); assertNotNull(originalGip); @@ -340,15 +331,14 @@ public class LegacyProvider4LegacyGuardTest { assertEquals(originalGip.getContent(), gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); - String expectedJsonOutput = - ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); + String expectedJsonOutput = ResourceUtils.getResourceAsString(VDNS_OUTPUT_JSON); String actualJsonOutput = standardCoder.encode(gotGopm); assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); assertThatThrownBy(() -> { new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), null); - }).hasMessage("policyVersion is marked @NonNull but is null"); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); Map deletedGopm = new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId(), "1"); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java index 4dd998ee9..ec03122a6 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java @@ -1,6 +1,6 @@ /*- * ============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"); @@ -50,10 +50,10 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class LegacyProvider4LegacyOperationalTest { - private static final String POLICY_ID_IS_NULL = "policyId is marked @NonNull but is null"; + private static final String POLICY_ID_IS_NULL = "^policyId is marked .*on.*ull but is null$"; private static final String VCPE_OUTPUT_JSON = "policies/vCPE.policy.operational.output.json"; private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.operational.input.json"; - private static final String DAO_IS_NULL = "dao is marked @NonNull but is null"; + private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; private PfDao pfDao; private StandardCoder standardCoder; @@ -100,15 +100,15 @@ public class LegacyProvider4LegacyOperationalTest { public void testPoliciesGet() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(null, "", null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, null, null); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist", null); @@ -117,8 +117,7 @@ public class LegacyProvider4LegacyOperationalTest { createPolicyTypes(); LegacyOperationalPolicy originalLop = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), - LegacyOperationalPolicy.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -146,21 +145,20 @@ public class LegacyProvider4LegacyOperationalTest { public void testPolicyCreate() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(pfDao, null); - }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); createPolicyTypes(); LegacyOperationalPolicy originalLop = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), - LegacyOperationalPolicy.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -183,25 +181,24 @@ public class LegacyProvider4LegacyOperationalTest { public void testPolicyUpdate() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(pfDao, null); - }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); + }).hasMessageMatching("^legacyOperationalPolicy is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy()); - }).hasMessage("name is marked @NonNull but is null"); + }).hasMessageMatching("^name is marked .*on.*ull but is null$"); createPolicyTypes(); LegacyOperationalPolicy originalLop = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), - LegacyOperationalPolicy.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -226,33 +223,32 @@ public class LegacyProvider4LegacyOperationalTest { public void testPoliciesDelete() throws Exception { assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(null, null, ""); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(null, "", null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(null, "", ""); - - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, null, null); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, null, ""); - }).hasMessage(POLICY_ID_IS_NULL); + }).hasMessageMatching(POLICY_ID_IS_NULL); assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, "", null); - }).hasMessage("policyVersion is marked @NonNull but is null"); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "0"); @@ -261,8 +257,7 @@ public class LegacyProvider4LegacyOperationalTest { createPolicyTypes(); LegacyOperationalPolicy originalLop = - standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), - LegacyOperationalPolicy.class); + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -281,7 +276,7 @@ public class LegacyProvider4LegacyOperationalTest { assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), null); - }).hasMessage("policyVersion is marked @NonNull but is null"); + }).hasMessageMatching("^policyVersion is marked .*on.*ull but is null$"); LegacyOperationalPolicy deletedLop = new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), "1"); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java index a3e3ba518..db6dc52b6 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java @@ -1,6 +1,6 @@ /*- * ============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"); @@ -27,6 +27,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Properties; + import org.eclipse.persistence.config.PersistenceUnitProperties; import org.junit.After; import org.junit.Before; @@ -42,6 +43,8 @@ import org.onap.policy.models.dao.PfDaoFactory; import org.onap.policy.models.dao.impl.DefaultPfDao; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvider; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; @@ -53,9 +56,9 @@ import org.yaml.snakeyaml.Yaml; * @author Liam Fallon (liam.fallon@est.tech) */ public class SimpleToscaProviderTest { - private static final String TEMPLATE_IS_NULL = "serviceTemplate is marked @NonNull but is null"; + private static final String TEMPLATE_IS_NULL = "^serviceTemplate is marked .*on.*ull but is null$"; private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.monitoring.input.tosca.json"; - private static final String DAO_IS_NULL = "dao is marked @NonNull but is null"; + private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$"; private PfDao pfDao; private StandardCoder standardCoder; @@ -98,11 +101,51 @@ public class SimpleToscaProviderTest { pfDao.close(); } + @Test + public void testCreateUpdateGetDeleteDataType() throws PfModelException { + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + + PfConceptKey dataType0Key = new PfConceptKey("DataType0", "0.0.1"); + JpaToscaDataType dataType0 = new JpaToscaDataType(); + dataType0.setKey(dataType0Key); + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + serviceTemplate.getDataTypes().getConceptMap().put(dataType0Key, dataType0); + + JpaToscaServiceTemplate createdServiceTemplate = + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + + assertEquals(dataType0, createdServiceTemplate.getDataTypes().get(dataType0Key)); + assertEquals(null, createdServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + + dataType0.setDescription("Updated Description"); + + JpaToscaServiceTemplate updatedServiceTemplate = + new SimpleToscaProvider().updateDataTypes(pfDao, serviceTemplate); + + assertEquals(dataType0, updatedServiceTemplate.getDataTypes().get(dataType0Key)); + assertEquals("Updated Description", updatedServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + + JpaToscaServiceTemplate gotServiceTemplate = + new SimpleToscaProvider().getDataTypes(pfDao, dataType0Key.getName(), dataType0Key.getVersion()); + + assertEquals(dataType0, gotServiceTemplate.getDataTypes().get(dataType0Key)); + assertEquals("Updated Description", gotServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + + JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key); + + assertEquals(dataType0, deletedServiceTemplate.getDataTypes().get(dataType0Key)); + assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + + JpaToscaServiceTemplate doesNotExistServiceTemplate = + new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key); + + assertEquals(null, doesNotExistServiceTemplate.getDataTypes().get(dataType0Key)); + } + @Test public void testPoliciesGet() throws Exception { - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); @@ -127,9 +170,8 @@ public class SimpleToscaProviderTest { @Test public void testPolicyCreate() throws Exception { - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); @@ -145,9 +187,8 @@ public class SimpleToscaProviderTest { @Test public void testPolicyUpdate() throws Exception { - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); @@ -163,9 +204,8 @@ public class SimpleToscaProviderTest { @Test public void testPoliciesDelete() throws Exception { - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = + standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON), ToscaServiceTemplate.class); createPolicyTypes(); @@ -195,98 +235,138 @@ public class SimpleToscaProviderTest { JpaToscaServiceTemplate testServiceTemplate = new JpaToscaServiceTemplate(); assertThatThrownBy(() -> new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate)) - .hasMessage("topology template not specified on service template"); + .hasMessage("topology template not specified on service template"); testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); assertThatThrownBy(() -> new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate)) - .hasMessage("no policies specified on topology template of service template"); + .hasMessage("no policies specified on topology template of service template"); testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); assertThatThrownBy(() -> new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate)) - .hasMessage("list of policies specified on topology template of service template is empty"); + .hasMessage("list of policies specified on topology template of service template is empty"); } @Test public void testNonNulls() { + assertThatThrownBy(() -> { + new SimpleToscaProvider().getDataTypes(null, null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createDataTypes(null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createDataTypes(null, new JpaToscaServiceTemplate()); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createDataTypes(pfDao, null); + }).hasMessageMatching(TEMPLATE_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updateDataTypes(null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updateDataTypes(null, new JpaToscaServiceTemplate()); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updateDataTypes(pfDao, null); + }).hasMessageMatching(TEMPLATE_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deleteDataType(null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deleteDataType(null, new PfConceptKey()); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deleteDataType(pfDao, null); + }).hasMessageMatching("^dataTypeKey is marked .*on.*ull but is null$"); + assertThatThrownBy(() -> { new SimpleToscaProvider().getPolicyTypes(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().createPolicyTypes(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().createPolicyTypes(null, new JpaToscaServiceTemplate()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().createPolicyTypes(pfDao, null); - }).hasMessage(TEMPLATE_IS_NULL); + }).hasMessageMatching(TEMPLATE_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().updatePolicyTypes(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().updatePolicyTypes(null, new JpaToscaServiceTemplate()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().updatePolicyTypes(pfDao, null); - }).hasMessage(TEMPLATE_IS_NULL); + }).hasMessageMatching(TEMPLATE_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().deletePolicyType(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().deletePolicyType(null, new PfConceptKey()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().deletePolicyType(pfDao, null); - }).hasMessage("policyTypeKey is marked @NonNull but is null"); + }).hasMessageMatching("^policyTypeKey is marked .*on.*ull but is null$"); assertThatThrownBy(() -> { new SimpleToscaProvider().getPolicies(null, null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().createPolicies(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().createPolicies(pfDao, null); - }).hasMessage(TEMPLATE_IS_NULL); + }).hasMessageMatching(TEMPLATE_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().updatePolicies(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().updatePolicies(pfDao, null); - }).hasMessage(TEMPLATE_IS_NULL); + }).hasMessageMatching(TEMPLATE_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().deletePolicy(null, null); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey()); - }).hasMessage(DAO_IS_NULL); + }).hasMessageMatching(DAO_IS_NULL); assertThatThrownBy(() -> { new SimpleToscaProvider().deletePolicy(pfDao, null); - }).hasMessage("policyKey is marked @NonNull but is null"); + }).hasMessageMatching("^policyKey is marked .*on.*ull but is null$"); } private void createPolicyTypes() throws CoderException, PfModelException { diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java index 40a2fd29a..b7c814a5e 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,16 @@ package org.onap.policy.models.tosca.utils; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; /** * Import the {@link ToscaUtils} class. @@ -34,12 +39,71 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; public class ToscaUtilsTest { @Test - public void test() { + public void testAssertDataTypes() { JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(); + + assertThatThrownBy(() -> { + ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate); + }).hasMessage("no data types specified on service template"); + + jpaToscaServiceTemplate.setDataTypes(new JpaToscaDataTypes()); + + assertThatThrownBy(() -> { + ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate); + }).hasMessage("list of data types specified on service template is empty"); + + jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null); + + assertThatCode(() -> { + ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate); + }).doesNotThrowAnyException(); + } + + @Test + public void testAssertPolicyTypes() { + JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(); + + assertThatThrownBy(() -> { + ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate); + }).hasMessage("no policy types specified on service template"); + jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); assertThatThrownBy(() -> { ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate); }).hasMessage("list of policy types specified on service template is empty"); + + jpaToscaServiceTemplate.getPolicyTypes().getConceptMap().put(new PfConceptKey(), null); + + assertThatCode(() -> { + ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate); + }).doesNotThrowAnyException(); + } + + @Test + public void testAssertPolicies() { + JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(); + + assertThatThrownBy(() -> { + ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); + }).hasMessage("topology template not specified on service template"); + + jpaToscaServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + + assertThatThrownBy(() -> { + ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); + }).hasMessage("no policies specified on topology template of service template"); + + jpaToscaServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + + assertThatThrownBy(() -> { + ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); + }).hasMessage("list of policies specified on topology template of service template is empty"); + + jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null); + + assertThatCode(() -> { + ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); + }).doesNotThrowAnyException(); } } diff --git a/models-tosca/src/test/resources/META-INF/persistence.xml b/models-tosca/src/test/resources/META-INF/persistence.xml index 936e5a11c..1cca1ee62 100644 --- a/models-tosca/src/test/resources/META-INF/persistence.xml +++ b/models-tosca/src/test/resources/META-INF/persistence.xml @@ -1,20 +1,20 @@ @@ -28,6 +28,7 @@ org.onap.policy.models.base.PfConceptKey org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType + org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType -- cgit 1.2.3-korg