From a88c9a232d486865ce007886daff56392ebf2d7b Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Tue, 2 Nov 2021 16:08:46 +0000 Subject: Add support transaction in Statistics Providers Add support transaction in ClElementStatisticsProvider and ParticipantStatisticsProvider Issue-ID: POLICY-3801 Change-Id: Iaf9d55a268627f9d548afdf108476441b19e1413 Signed-off-by: FrancescoFioraEst --- .../provider/ClElementStatisticsProviderTest.java | 50 +++++++++++----------- .../ParticipantStatisticsProviderTest.java | 41 +++++++++--------- 2 files changed, 47 insertions(+), 44 deletions(-) (limited to 'models/src/test/java/org/onap') diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProviderTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProviderTest.java index a9ef9148f..cf4136d3e 100644 --- a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProviderTest.java @@ -23,19 +23,23 @@ package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provide import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.time.Instant; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ClElementStatisticsRepository; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class ClElementStatisticsProviderTest { @@ -43,9 +47,6 @@ class ClElementStatisticsProviderTest { private static final Coder CODER = new StandardCoder(); private static final String CL_ELEMENT_STATS_JSON = "src/test/resources/providers/TestClElementStatistics.json"; - private static AtomicInteger dbNameCounter = new AtomicInteger(); - - private PolicyModelsProviderParameters parameters; private ClElementStatisticsProvider clElementStatisticsProvider; private ClElementStatisticsList inputClElementStats; private String originalJson = ResourceUtils.getResourceAsString(CL_ELEMENT_STATS_JSON); @@ -58,22 +59,23 @@ class ClElementStatisticsProviderTest { @BeforeEach void beforeSetupDao() throws Exception { - parameters = new PolicyModelsProviderParameters(); - parameters.setDatabaseDriver("org.h2.Driver"); - parameters.setName("PolicyProviderParameterGroup"); - parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl"); - parameters.setDatabaseUrl("jdbc:h2:mem:clElementTestDb" + dbNameCounter.getAndIncrement()); - parameters.setDatabaseUser("policy"); - parameters.setDatabasePassword("P01icY"); - parameters.setPersistenceUnit("ToscaConceptTest"); - - clElementStatisticsProvider = new ClElementStatisticsProvider(parameters); inputClElementStats = CODER.decode(originalJson, ClElementStatisticsList.class); - } + var clElementStatisticsRepository = mock(ClElementStatisticsRepository.class); + + var jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate(inputClElementStats.getClElementStatistics(), + JpaClElementStatistics::new, "control loop element statistics"); + + for (var clElementStat : jpaClElementStatisticsList) { + when(clElementStatisticsRepository.findAllById(List.of(clElementStat.getKey()))) + .thenReturn(List.of(clElementStat)); + } + + when(clElementStatisticsRepository.saveAll(anyList())).thenReturn(jpaClElementStatisticsList); + + when(clElementStatisticsRepository.getFiltered(eq(JpaClElementStatistics.class), any())) + .thenReturn(List.of(jpaClElementStatisticsList.get(0))); - @AfterEach - void teardown() { - clElementStatisticsProvider.close(); + clElementStatisticsProvider = new ClElementStatisticsProvider(clElementStatisticsRepository); } @Test @@ -84,10 +86,10 @@ class ClElementStatisticsProviderTest { ClElementStatisticsList createdClElementStats = new ClElementStatisticsList(); createdClElementStats.setClElementStatistics( - clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics())); + clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics())); assertEquals(inputClElementStats.toString().replaceAll("\\s+", ""), - createdClElementStats.toString().replaceAll("\\s+", "")); + createdClElementStats.toString().replaceAll("\\s+", "")); } @Test @@ -104,9 +106,9 @@ class ClElementStatisticsProviderTest { Instant instant = inputClElementStats.getClElementStatistics().get(0).getTimeStamp(); String id = inputClElementStats.getClElementStatistics().get(0).getId().toString(); assertEquals(1, clElementStatisticsProvider - .getClElementStatistics(identifier.getName(), identifier.getVersion(), id, instant).size()); + .getClElementStatistics(identifier.getName(), identifier.getVersion(), id, instant).size()); assertEquals(1, clElementStatisticsProvider - .getFilteredClElementStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size()); + .getFilteredClElementStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size()); } } diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProviderTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProviderTest.java index 8191dd618..c6e1f817d 100644 --- a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProviderTest.java @@ -23,19 +23,23 @@ package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provide import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.time.Instant; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ParticipantStatisticsRepository; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class ParticipantStatisticsProviderTest { @@ -44,32 +48,29 @@ class ParticipantStatisticsProviderTest { private static final Coder CODER = new StandardCoder(); private static final String PARTICIPANT_STATS_JSON = "src/test/resources/providers/TestParticipantStatistics.json"; - private static AtomicInteger dbNameCounter = new AtomicInteger(); - - private PolicyModelsProviderParameters parameters; private ParticipantStatisticsProvider participantStatisticsProvider; private ParticipantStatisticsList inputParticipantStatistics; private String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_STATS_JSON); @BeforeEach void beforeSetupDao() throws Exception { + var participantStatisticsRepository = mock(ParticipantStatisticsRepository.class); + participantStatisticsProvider = new ParticipantStatisticsProvider(participantStatisticsRepository); + inputParticipantStatistics = CODER.decode(originalJson, ParticipantStatisticsList.class); - parameters = new PolicyModelsProviderParameters(); - parameters.setDatabaseDriver("org.h2.Driver"); - parameters.setName("PolicyProviderParameterGroup"); - parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl"); - parameters.setDatabaseUrl("jdbc:h2:mem:participantStatisticsProviderTestDb" + dbNameCounter.getAndIncrement()); - parameters.setDatabaseUser("policy"); - parameters.setDatabasePassword("P01icY"); - parameters.setPersistenceUnit("ToscaConceptTest"); + var jpaParticipantStatisticsList = + ProviderUtils.getJpaAndValidate(inputParticipantStatistics.getStatisticsList(), + JpaParticipantStatistics::new, "Participant Statistics"); - participantStatisticsProvider = new ParticipantStatisticsProvider(parameters); - inputParticipantStatistics = CODER.decode(originalJson, ParticipantStatisticsList.class); - } + for (var participantStat : jpaParticipantStatisticsList) { + when(participantStatisticsRepository.findAllById(List.of(participantStat.getKey()))) + .thenReturn(List.of(participantStat)); + } + + when(participantStatisticsRepository.getFiltered(eq(JpaParticipantStatistics.class), any())) + .thenReturn(List.of(jpaParticipantStatisticsList.get(0))); - @AfterEach - void teardown() { - participantStatisticsProvider.close(); + when(participantStatisticsRepository.saveAll(anyList())).thenReturn(jpaParticipantStatisticsList); } @Test -- cgit 1.2.3-korg