diff options
4 files changed, 70 insertions, 42 deletions
diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java index b40abaedf..bc022be0a 100644 --- a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java +++ b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java @@ -37,6 +37,7 @@ import java.util.Set; import java.util.TreeSet; import java.util.UUID; import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.junit.After; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; @@ -63,6 +64,17 @@ public class EntityTest { private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1613494293).plusSeconds(90); private PfDao pfDao; + /** + * Closes the DAO. + */ + @After + public void tearDown() { + if (pfDao != null) { + pfDao.close(); + pfDao = null; + } + } + @Test public void testEntityTestSanity() throws PfModelException { final DaoParameters daoParameters = new DaoParameters(); @@ -92,8 +104,6 @@ public class EntityTest { assertThatThrownBy(() -> pfDao.create(new PfConceptKey())) .hasMessage("Policy Framework DAO has not been initialized"); - - pfDao.close(); } @Test @@ -119,8 +129,6 @@ public class EntityTest { testVersionOps(); testgetFilteredOps(); - - pfDao.close(); } @Test @@ -164,9 +172,8 @@ public class EntityTest { pfDao.getConcept(PfConceptKey.class, nullKey); pfDao.getConcept(null, nullRefKey); pfDao.getConcept(PfReferenceKey.class, nullRefKey); - pfDao.size(null); - assertThatCode(() -> pfDao.close()).doesNotThrowAnyException(); + assertThatCode(() -> pfDao.size(null)).doesNotThrowAnyException(); } private void testAllOps() { 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 3c85e31a9..a1102cb51 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 @@ -32,6 +32,7 @@ import static org.junit.Assert.assertTrue; import java.time.Instant; import java.util.ArrayList; import java.util.List; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.onap.policy.models.base.PfModelException; @@ -79,7 +80,9 @@ public class DatabasePolicyModelsProviderTest { private static final String ORDER = "DESC"; - PolicyModelsProviderParameters parameters; + private PolicyModelsProviderParameters parameters; + + private PolicyModelsProvider databaseProvider; /** * Initialize parameters. @@ -94,14 +97,23 @@ public class DatabasePolicyModelsProviderTest { parameters.setPersistenceUnit("ToscaConceptTest"); } + /** + * Closes the DB. + */ + @After + public void tearDown() throws PfModelException { + if (databaseProvider != null) { + databaseProvider.close(); + } + } + @Test public void testInitAndClose() throws Exception { assertThatThrownBy(() -> { new DatabasePolicyModelsProviderImpl(null); }).hasMessageMatching("^parameters is marked .*on.*ull but is null$"); - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); parameters.setDatabaseUrl("jdbc://www.acmecorp.nonexist"); @@ -134,8 +146,7 @@ public class DatabasePolicyModelsProviderTest { @Test public void testProviderMethodsNull() throws Exception { - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); assertThatThrownBy(() -> { databaseProvider.getFilteredPolicyTypes(null); @@ -197,8 +208,7 @@ public class DatabasePolicyModelsProviderTest { @Test public void testProviderMethodsNullGroup() throws Exception { - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); assertThatThrownBy(() -> { databaseProvider.getFilteredPdpGroups(null); @@ -285,8 +295,7 @@ public class DatabasePolicyModelsProviderTest { @Test public void testProviderMethodsNotInit() throws Exception { - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); databaseProvider.close(); @@ -297,8 +306,7 @@ public class DatabasePolicyModelsProviderTest { @Test public void testProviderMethods() throws PfModelException { - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); assertThatThrownBy(() -> databaseProvider.getPolicyTypes(NAME, VERSION_100)) .hasMessage("service template not found in database"); @@ -378,16 +386,7 @@ public class DatabasePolicyModelsProviderTest { pdpSubGroup.setPdpInstances(new ArrayList<>()); pdpSubGroup.getPdpInstances().add(pdp); - PdpStatistics pdpStatistics = new PdpStatistics(); - pdpStatistics.setPdpInstanceId(NAME); - pdpStatistics.setTimeStamp(TIMESTAMP); - pdpStatistics.setPdpGroupName(GROUP); - pdpStatistics.setPdpSubGroupName("type"); - ArrayList<PdpStatistics> statisticsArrayList = new ArrayList<>(); - statisticsArrayList.add(pdpStatistics); - - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getDesiredInstanceCount()); @@ -411,16 +410,17 @@ public class DatabasePolicyModelsProviderTest { assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName()); - assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); - assertEquals(1, databaseProvider.createPdpStatistics(statisticsArrayList).size()); - assertEquals(1, databaseProvider.updatePdpStatistics(statisticsArrayList).size()); - } + List<PdpStatistics> statisticsArrayList = makePdpStatisticsList(); + assertThat(databaseProvider.getPdpStatistics(null, null)).hasSize(0); + assertThat(databaseProvider.createPdpStatistics(statisticsArrayList)).hasSize(1); + assertThat(databaseProvider.updatePdpStatistics(statisticsArrayList)).hasSize(1); + } @Test public void testProviderMethodsStatistics() throws PfModelException { - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider.createPdpStatistics(makePdpStatisticsList()); assertEquals(NAME, databaseProvider.getPdpStatistics(null, null).get(0).getPdpInstanceId()); assertEquals(NAME, databaseProvider.getFilteredPdpStatistics(null, GROUP, null, @@ -491,8 +491,7 @@ public class DatabasePolicyModelsProviderTest { List<PdpGroup> pdpGroups = new ArrayList<>(); pdpGroups.add(pdpGroup); - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); databaseProvider.createPdpGroups(pdpGroups); @@ -526,8 +525,7 @@ public class DatabasePolicyModelsProviderTest { List<PdpGroup> pdpGroups = new ArrayList<>(); pdpGroups.add(pdpGroup); - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); databaseProvider.createPdpGroups(pdpGroups); @@ -539,4 +537,14 @@ public class DatabasePolicyModelsProviderTest { databaseProvider.close(); } + + private List<PdpStatistics> makePdpStatisticsList() { + PdpStatistics pdpStatistics = new PdpStatistics(); + pdpStatistics.setPdpInstanceId(NAME); + pdpStatistics.setTimeStamp(TIMESTAMP); + pdpStatistics.setPdpGroupName(GROUP); + pdpStatistics.setPdpSubGroupName("type"); + List<PdpStatistics> statisticsArrayList = List.of(pdpStatistics); + return statisticsArrayList; + } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyStatisticsPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyStatisticsPersistenceTest.java index b01349865..abc08846a 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyStatisticsPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyStatisticsPersistenceTest.java @@ -41,9 +41,9 @@ public class PolicyStatisticsPersistenceTest { public void testPdpStatiscticsPersistence() throws PfModelException { // Try the test on three providers for (int i = 0; i < 3; i++) { - PolicyModelsProvider databaseProvider = setupProvider(); - testPdpStatiscticsPersistenceOneProvider(databaseProvider); - databaseProvider.close(); + try (PolicyModelsProvider databaseProvider = setupProvider()) { + testPdpStatiscticsPersistenceOneProvider(databaseProvider); + } } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/revisionhierarchy/HierarchyFetchTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/revisionhierarchy/HierarchyFetchTest.java index bb0eb1688..c55325e57 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/revisionhierarchy/HierarchyFetchTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/revisionhierarchy/HierarchyFetchTest.java @@ -28,10 +28,12 @@ import static org.junit.Assert.assertEquals; import java.util.Iterator; import java.util.List; import java.util.Map; +import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.utils.coder.YamlJsonTranslator; import org.onap.policy.common.utils.resources.TextFileUtils; +import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; @@ -43,6 +45,8 @@ public class HierarchyFetchTest { private static PolicyModelsProviderParameters parameters; + private PolicyModelsProvider databaseProvider; + @BeforeClass public static void beforeSetupParameters() { parameters = new PolicyModelsProviderParameters(); @@ -53,10 +57,19 @@ public class HierarchyFetchTest { parameters.setPersistenceUnit("ToscaConceptTest"); } + /** + * Closes the DB. + */ + @After + public void tearDown() throws PfModelException { + if (databaseProvider != null) { + databaseProvider.close(); + } + } + @Test public void testMultipleVersions() throws Exception { - PolicyModelsProvider databaseProvider = - new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); ToscaServiceTemplate serviceTemplate = new YamlJsonTranslator().fromYaml( TextFileUtils |