diff options
author | FrancescoFioraEst <francesco.fiora@est.tech> | 2021-11-10 14:11:51 +0000 |
---|---|---|
committer | FrancescoFioraEst <francesco.fiora@est.tech> | 2021-11-19 09:54:38 +0000 |
commit | 0574c7df7944828a7e8a03c1eacf93605c43b154 (patch) | |
tree | 3f33ce0c0784361198ff83f2738a7f84f85a7032 /models/src/test/java | |
parent | b421ff09244107ab6172f72076466c6be8616c5b (diff) |
Add support transaction in ParticipantProvider
Issue-ID: POLICY-3800
Change-Id: I938c2731a30013b5f8e11a23a86d117300c95bff
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'models/src/test/java')
4 files changed, 177 insertions, 71 deletions
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 cf4136d3e..82ce8d789 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 @@ -62,11 +62,13 @@ class ClElementStatisticsProviderTest { inputClElementStats = CODER.decode(originalJson, ClElementStatisticsList.class); var clElementStatisticsRepository = mock(ClElementStatisticsRepository.class); - var jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate(inputClElementStats.getClElementStatistics(), - JpaClElementStatistics::new, "control loop element statistics"); + var jpaClElementStatisticsList = + ProviderUtils.getJpaAndValidateList(inputClElementStats.getClElementStatistics(), + JpaClElementStatistics::new, "control loop element statistics"); for (var clElementStat : jpaClElementStatisticsList) { - when(clElementStatisticsRepository.findAllById(List.of(clElementStat.getKey()))) + when(clElementStatisticsRepository.getById(eq(clElementStat.getKey()))).thenReturn(clElementStat); + when(clElementStatisticsRepository.findAllById(eq(List.of(clElementStat.getKey())))) .thenReturn(List.of(clElementStat)); } diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantProviderTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantProviderTest.java index 681ca96ce..9b48735f6 100644 --- a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantProviderTest.java @@ -23,129 +23,127 @@ 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.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; -import org.junit.jupiter.api.AfterEach; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipant; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ParticipantRepository; 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.base.PfConceptKey; import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter; class ParticipantProviderTest { private static final Coder CODER = new StandardCoder(); - private static final String PARTICIPANT_JSON = - "src/test/resources/providers/TestParticipant.json"; + private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json"; private static final String LIST_IS_NULL = ".*. is marked .*ull but is null"; - private static AtomicInteger dbNameCounter = new AtomicInteger(); - - private PolicyModelsProviderParameters parameters; - private ParticipantProvider participantProvider; private List<Participant> inputParticipants = new ArrayList<>(); - private Participant updateParticipants; + private List<JpaParticipant> jpaParticipantList; private String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON); @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:participantProviderTestDb" + dbNameCounter.getAndIncrement()); - parameters.setDatabaseUser("policy"); - parameters.setDatabasePassword("P01icY"); - parameters.setPersistenceUnit("ToscaConceptTest"); - - participantProvider = new ParticipantProvider(parameters); inputParticipants.add(CODER.decode(originalJson, Participant.class)); - - } - - @AfterEach - void teardown() { - participantProvider.close(); + jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant"); } @Test - void testParticipantCreate() throws Exception { + void testParticipantSave() throws Exception { + var participantRepository = mock(ParticipantRepository.class); + for (var participant : jpaParticipantList) { + when(participantRepository.getById(new PfConceptKey(participant.getName(), participant.getVersion()))) + .thenReturn(participant); + } + var participantProvider = new ParticipantProvider(participantRepository); + assertThatThrownBy(() -> { - participantProvider.createParticipants(null); + participantProvider.saveParticipant(null); }).hasMessageMatching(LIST_IS_NULL); - List<Participant> createdParticipants = new ArrayList<>(); - createdParticipants.addAll(participantProvider - .createParticipants(inputParticipants)); + when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0)); - assertEquals(createdParticipants.get(0), - inputParticipants.get(0)); - } + Participant savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0)); + assertEquals(savedParticipant, inputParticipants.get(0)); + + when(participantRepository.save(any())).thenThrow(IllegalArgumentException.class); + assertThatThrownBy(() -> { + participantProvider.saveParticipant(inputParticipants.get(0)); + }).hasMessageMatching("Error in save Participant"); + } @Test void testGetControlLoops() throws Exception { + var participantRepository = mock(ParticipantRepository.class); + var participantProvider = new ParticipantProvider(participantRepository); - List<Participant> getResponse; - - //Return empty list when no data present in db - getResponse = participantProvider.getParticipants(null, null); + // Return empty list when no data present in db + List<Participant> getResponse = participantProvider.getParticipants(null, null); assertThat(getResponse).isEmpty(); - participantProvider.createParticipants(inputParticipants); String name = inputParticipants.get(0).getName(); String version = inputParticipants.get(0).getVersion(); + when(participantRepository.getFiltered(any(), eq(name), eq(version))) + .thenReturn(List.of(jpaParticipantList.get(0))); assertEquals(1, participantProvider.getParticipants(name, version).size()); - assertThat(participantProvider.getParticipants("invalid_name", - "1.0.1")).isEmpty(); + assertThat(participantProvider.getParticipants("invalid_name", "1.0.1")).isEmpty(); + + assertThat(participantProvider.findParticipant("invalid_name", "1.0.1")).isEmpty(); + + when(participantRepository.findAll()).thenReturn(jpaParticipantList); + assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size()); + + when(participantRepository.findById(any())).thenThrow(IllegalArgumentException.class); + + assertThatThrownBy(() -> { + participantProvider.findParticipant("notValid", "notValid"); + }).hasMessageMatching("Error in find Participant"); assertThatThrownBy(() -> { participantProvider.getFilteredParticipants(null); }).hasMessageMatching("filter is marked .*ull but is null"); + when(participantRepository.getFiltered(eq(JpaParticipant.class), eq(null), eq(null))) + .thenReturn(jpaParticipantList); + final ToscaTypedEntityFilter<Participant> filter = ToscaTypedEntityFilter.<Participant>builder() .type("org.onap.domain.pmsh.PMSHControlLoopDefinition").build(); assertEquals(1, participantProvider.getFilteredParticipants(filter).size()); - } - @Test - void testUpdateParticipant() throws Exception { - assertThatThrownBy(() -> { - participantProvider.updateParticipants(null); - }).hasMessageMatching("participants is marked .*ull but is null"); - - participantProvider.createParticipants(inputParticipants); - updateParticipants = inputParticipants.get(0); - updateParticipants.setParticipantState(ParticipantState.ACTIVE); - List<Participant> participantList = new ArrayList<>(); - participantList.add(updateParticipants); - List<Participant> updateResponse = new ArrayList<>(); - updateResponse = participantProvider.updateParticipants(participantList); - - assertEquals(ParticipantState.ACTIVE, updateResponse.get(0).getParticipantState()); } @Test void testDeleteParticipant() throws Exception { + var participantRepository = mock(ParticipantRepository.class); + var participantProvider = new ParticipantProvider(participantRepository); + assertThatThrownBy(() -> { participantProvider.deleteParticipant("Invalid_name", "1.0.1"); }).hasMessageMatching(".*.failed, participant does not exist"); - Participant deletedParticipant; - List<Participant> participantList = participantProvider.createParticipants(inputParticipants); String name = inputParticipants.get(0).getName(); String version = inputParticipants.get(0).getVersion(); - deletedParticipant = participantProvider.deleteParticipant(name, version); - assertEquals(participantList.get(0), deletedParticipant); + when(participantRepository.findById(any())).thenReturn(Optional.of(jpaParticipantList.get(0))); + + Participant deletedParticipant = participantProvider.deleteParticipant(name, version); + assertEquals(inputParticipants.get(0), deletedParticipant); + when(participantRepository.findById(any())).thenThrow(IllegalArgumentException.class); + assertThatThrownBy(() -> { + participantProvider.deleteParticipant(name, version); + }).hasMessageMatching("Error in delete Participant"); } } 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 c6e1f817d..16f4958a9 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 @@ -59,11 +59,12 @@ class ParticipantStatisticsProviderTest { inputParticipantStatistics = CODER.decode(originalJson, ParticipantStatisticsList.class); var jpaParticipantStatisticsList = - ProviderUtils.getJpaAndValidate(inputParticipantStatistics.getStatisticsList(), + ProviderUtils.getJpaAndValidateList(inputParticipantStatistics.getStatisticsList(), JpaParticipantStatistics::new, "Participant Statistics"); for (var participantStat : jpaParticipantStatisticsList) { - when(participantStatisticsRepository.findAllById(List.of(participantStat.getKey()))) + when(participantStatisticsRepository.getById(eq(participantStat.getKey()))).thenReturn(participantStat); + when(participantStatisticsRepository.findAllById(eq(List.of(participantStat.getKey())))) .thenReturn(List.of(participantStat)); } @@ -89,10 +90,9 @@ class ParticipantStatisticsProviderTest { @Test void testGetControlLoops() throws Exception { - List<ParticipantStatistics> getResponse; - // Return empty list when no data present in db - getResponse = participantStatisticsProvider.getParticipantStatistics(null, null, null); + List<ParticipantStatistics> getResponse = + participantStatisticsProvider.getParticipantStatistics(null, null, null); assertThat(getResponse).isEmpty(); participantStatisticsProvider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList()); diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImplTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImplTest.java new file mode 100644 index 000000000..8b5357457 --- /dev/null +++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImplTest.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ProviderUtils; +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.dao.PfDao; +import org.onap.policy.models.dao.PfFilterParameters; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.provider.impl.ModelsProvider; + +class FilterRepositoryImplTest { + private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json"; + private static final Coder CODER = new StandardCoder(); + private static final AtomicInteger dbNameCounter = new AtomicInteger(); + private static final String originalJson = ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON); + private static List<JpaControlLoop> jpaControlLoops; + private PfDao pfDao; + + @BeforeEach + void beforeSetupDao() throws Exception { + var 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:controlLoopProviderTestDb" + dbNameCounter.getAndDecrement()); + parameters.setDatabaseUser("policy"); + parameters.setDatabasePassword("P01icY"); + parameters.setPersistenceUnit("ToscaConceptTest"); + + pfDao = ModelsProvider.init(parameters); + var inputControlLoops = CODER.decode(originalJson, ControlLoops.class); + jpaControlLoops = ProviderUtils.getJpaAndValidateList(inputControlLoops.getControlLoopList(), + JpaControlLoop::new, "ControlLoops"); + + pfDao.createCollection(jpaControlLoops); + } + + @Test + void testGetPfDao() { + assertThat(new FilterRepositoryImpl().getPfDao()).isNotNull(); + } + + @Test + void testGetFilteredParams() { + var filterRepositoryImpl = new FilterRepositoryImpl() { + @Override + protected PfDao getPfDao() { + return pfDao; + } + }; + var result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, null, null); + assertThat(result).hasSize(2); + + result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, jpaControlLoops.get(0).getName(), null); + assertThat(result).hasSize(1); + } + + @Test + void testGetFiltered() { + var filterRepositoryImpl = new FilterRepositoryImpl() { + @Override + protected PfDao getPfDao() { + return pfDao; + } + }; + + // @formatter:off + PfFilterParameters filterParams = PfFilterParameters + .builder() + .name(jpaControlLoops.get(0).getName()) + .build(); + // @formatter:on + + var result = filterRepositoryImpl.getFiltered(JpaControlLoop.class, filterParams); + assertThat(result).hasSize(1); + } +} |