diff options
author | Jim Hahn <jrh3@att.com> | 2021-04-09 09:49:48 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-04-12 08:59:17 -0400 |
commit | 0b7b94b17340406b34817d6088daa97d965d5c2c (patch) | |
tree | faaea701389cf01ab17afa3bbbfea950a863aeae | |
parent | f616fc7065ffadf046b1abf0fb479d706fec9464 (diff) |
Close DB in junits
Modified some of the junits to close the DB.
In addition, one of the junits was apparently dependent on a previous
test method adding records to the DB, thus the test methods were
actually order dependent, even though no annotations were used to
enforce the order. Modified that method to insert the data, itself, so
the test methods are no longer order dependent.
Issue-ID: POLICY-3171
Change-Id: I778c12796340f2a665b4ba00588c823c925c078c
Signed-off-by: Jim Hahn <jrh3@att.com>
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 |