FINERACT-1694-Event-Configuration-Database-Storage

This commit is contained in:
Ruchi Dhamankar
2022-10-17 15:15:52 +02:00
committed by Arnold Gálovics
parent 55b1baeacb
commit c9d5742836
25 changed files with 1146 additions and 8 deletions
@@ -37,6 +37,7 @@ import org.apache.fineract.cob.loan.LoanCOBBusinessStep;
import org.apache.fineract.infrastructure.core.domain.AbstractAuditableCustom;
import org.apache.fineract.infrastructure.core.domain.ActionContext;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService;
import org.apache.fineract.mix.data.MixTaxonomyData;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.ListableBeanFactory;
@@ -47,8 +48,9 @@ public class COBBusinessStepServiceStepDefinitions implements En {
private ApplicationContext applicationContext = mock(ApplicationContext.class);
private ListableBeanFactory beanFactory = mock(ListableBeanFactory.class);
private BatchBusinessStepRepository batchBusinessStepRepository = mock(BatchBusinessStepRepository.class);
private BusinessEventNotifierService businessEventNotifierService = mock(BusinessEventNotifierService.class);
private final COBBusinessStepService businessStepService = new COBBusinessStepServiceImpl(batchBusinessStepRepository,
applicationContext, beanFactory);
applicationContext, beanFactory, businessEventNotifierService);
private COBBusinessStep cobBusinessStep = mock(COBBusinessStep.class);
private COBBusinessStep notRegistereCobBusinessStep = mock(COBBusinessStep.class);
private TreeMap<Long, String> executionMap;
@@ -23,17 +23,21 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.event.business.BusinessEventListener;
import org.apache.fineract.infrastructure.event.business.domain.BulkBusinessEvent;
import org.apache.fineract.infrastructure.event.business.domain.BusinessEvent;
import org.apache.fineract.infrastructure.event.external.repository.ExternalEventConfigurationRepository;
import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration;
import org.apache.fineract.infrastructure.event.external.service.ExternalEventService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
@@ -45,6 +49,10 @@ class BusinessEventNotifierServiceImplTest {
@Mock
private ExternalEventService externalEventService;
@Mock
private ExternalEventConfigurationRepository externalEventConfigurationRepository;
@Mock
private FineractProperties fineractProperties;
@@ -74,6 +82,9 @@ class BusinessEventNotifierServiceImplTest {
MockBusinessEvent event = new MockBusinessEvent();
BusinessEventListener<MockBusinessEvent> postListener = mockListener();
underTest.addPostBusinessEventListener(MockBusinessEvent.class, postListener);
when(externalEventConfigurationRepository.findExternalEventConfigurationByTypeWithNotFoundDetection(Mockito.any()))
.thenReturn(new ExternalEventConfiguration("aType", true));
// when
underTest.notifyPostBusinessEvent(event);
// then
@@ -85,7 +96,8 @@ class BusinessEventNotifierServiceImplTest {
public void testNotifyPostBusinessEventShouldNotifyPostListenersAndPostAnBulkExternalEventWhenRecordingEnabled() {
// given
configureExternalEventsProperties(true);
when(externalEventConfigurationRepository.findExternalEventConfigurationByTypeWithNotFoundDetection(Mockito.any()))
.thenReturn(new ExternalEventConfiguration("aType", true));
MockBusinessEvent event = new MockBusinessEvent();
BusinessEventListener<MockBusinessEvent> postListener = mockListener();
underTest.addPostBusinessEventListener(MockBusinessEvent.class, postListener);
@@ -133,6 +145,22 @@ class BusinessEventNotifierServiceImplTest {
verifyNoInteractions(externalEventService);
}
@Test
public void testNotifyPostBusinessEventShouldNotifyPostListenersAndShouldNotPostAnExternalEventIfNotConfiguredForPosting() {
// given
configureExternalEventsProperties(true);
when(externalEventConfigurationRepository.findExternalEventConfigurationByTypeWithNotFoundDetection(Mockito.any()))
.thenReturn(new ExternalEventConfiguration("aType", false));
MockBusinessEvent event = new MockBusinessEvent();
BusinessEventListener<MockBusinessEvent> postListener = mockListener();
underTest.addPostBusinessEventListener(MockBusinessEvent.class, postListener);
// when
underTest.notifyPostBusinessEvent(event);
// then
verify(postListener).onBusinessEvent(event);
verifyNoInteractions(externalEventService);
}
private void configureExternalEventsProperties(boolean isExternalEventsEnabled) {
FineractProperties.FineractEventsProperties eventsProperties = new FineractProperties.FineractEventsProperties();
FineractProperties.FineractExternalEventsProperties externalProperties = new FineractProperties.FineractExternalEventsProperties();
@@ -0,0 +1,71 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.fineract.infrastructure.event.external.service;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
import javax.persistence.EntityManager;
import org.apache.fineract.infrastructure.event.external.exception.ExternalEventConfigurationNotFoundException;
import org.apache.fineract.infrastructure.event.external.repository.CustomExternalEventConfigurationRepositoryImpl;
import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class CustomExternalEventConfigurationRepositoryImplTest {
@Mock
private EntityManager entityManager;
private CustomExternalEventConfigurationRepositoryImpl underTest;
@BeforeEach
public void setUp() {
underTest = new CustomExternalEventConfigurationRepositoryImpl(entityManager);
}
@Test
public void givenConfigurationExistsThenReturnConfiguration() {
// given
ExternalEventConfiguration configuration = new ExternalEventConfiguration("aType", true);
when(entityManager.find(Mockito.any(), Mockito.anyString())).thenReturn(configuration);
// when
ExternalEventConfiguration actualConfiguration = underTest.findExternalEventConfigurationByTypeWithNotFoundDetection("aType");
// then
assertThat(actualConfiguration.getType(), equalTo(configuration.getType()));
assertThat(actualConfiguration.isEnabled(), equalTo(configuration.isEnabled()));
}
@Test
public void givenConfigurationDoesNotExistsThenThrowExternalEventConfigurationNotFoundException() {
// given
when(entityManager.find(Mockito.any(), Mockito.anyString())).thenReturn(null);
// then
assertThrows(ExternalEventConfigurationNotFoundException.class,
() -> underTest.findExternalEventConfigurationByTypeWithNotFoundDetection("aType"));
}
}
@@ -0,0 +1,87 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.fineract.infrastructure.event.external.service;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.any;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationData;
import org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationItemData;
import org.apache.fineract.infrastructure.event.external.repository.ExternalEventConfigurationRepository;
import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class ExternalEventConfigurationReadPlatformServiceTest {
@Mock
private ExternalEventConfigurationRepository repository;
@Mock
private ExternalEventsConfigurationMapper mapper;
private ExternalEventConfigurationReadPlatformServiceImpl underTest;
@BeforeEach
public void setUp() {
underTest = new ExternalEventConfigurationReadPlatformServiceImpl(repository, mapper);
}
@Test
public void givenConfigurationsThenReturnConfigurationData() {
// given
List<ExternalEventConfiguration> configurations = Arrays.asList(new ExternalEventConfiguration("aType", true),
new ExternalEventConfiguration("bType", false));
List<ExternalEventConfigurationItemData> configurationDataItems = Arrays
.asList(new ExternalEventConfigurationItemData("aType", true), new ExternalEventConfigurationItemData("bType", false));
when(repository.findAll()).thenReturn(configurations);
when(mapper.map(Mockito.anyList())).thenReturn(configurationDataItems);
// when
ExternalEventConfigurationData actualConfiguration = underTest.findAllExternalEventConfigurations();
// then
assertThat(actualConfiguration.getExternalEventConfiguration(), hasSize(2));
assertThat(actualConfiguration.getExternalEventConfiguration().get(0), any(ExternalEventConfigurationItemData.class));
assertThat(actualConfiguration.getExternalEventConfiguration().get(0).getType(), equalTo("aType"));
assertThat(actualConfiguration.getExternalEventConfiguration().get(0).isEnabled(), equalTo(true));
}
@Test
public void givenNoConfigurationsThenReturnEmptyConfigurationData() {
// given
List<ExternalEventConfiguration> emptyConfiguration = new ArrayList<>();
when(repository.findAll()).thenReturn(emptyConfiguration);
// when
ExternalEventConfigurationData actualConfiguration = underTest.findAllExternalEventConfigurations();
// then
assertThat(actualConfiguration.getExternalEventConfiguration(), hasSize(0));
}
}
@@ -0,0 +1,71 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.fineract.infrastructure.event.external.service;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.event.external.command.ExternalEventConfigurationCommand;
import org.apache.fineract.infrastructure.event.external.repository.ExternalEventConfigurationRepository;
import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration;
import org.apache.fineract.infrastructure.event.external.serialization.ExternalEventConfigurationCommandFromApiJsonDeserializer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class ExternalEventConfigurationWritePlatformServiceTest {
@Mock
private ExternalEventConfigurationRepository repository;
@Mock
private ExternalEventConfigurationCommandFromApiJsonDeserializer fromApiJsonDeserializer;
private ExternalEventConfigurationWritePlatformServiceImpl underTest;
@BeforeEach
public void setUp() {
underTest = new ExternalEventConfigurationWritePlatformServiceImpl(repository, fromApiJsonDeserializer);
}
@Test
public void givenExternalEventConfigurationsWithChangeWhenUpdateConfigurationThenConfigurationIsUpdated() {
// given
final JsonCommand jsonCommand = Mockito.mock(JsonCommand.class);
Map<String, Boolean> mapOfConfigurationsForUpdate = new HashMap<>();
mapOfConfigurationsForUpdate.put("aType", Boolean.TRUE);
ExternalEventConfigurationCommand command = new ExternalEventConfigurationCommand(mapOfConfigurationsForUpdate);
when(jsonCommand.json()).thenReturn("");
when(fromApiJsonDeserializer.commandFromApiJson(Mockito.anyString())).thenReturn(command);
when(repository.findExternalEventConfigurationByTypeWithNotFoundDetection(Mockito.anyString()))
.thenReturn(new ExternalEventConfiguration("aType", false));
// when
underTest.updateConfigurations(jsonCommand);
// then
verify(repository, times(1)).saveAll(Mockito.anyCollection());
}
}