From c9d574283627ad9b5195fe81ebce446994512c6c Mon Sep 17 00:00:00 2001 From: Ruchi Dhamankar Date: Fri, 14 Oct 2022 23:28:32 +0530 Subject: [PATCH] FINERACT-1694-Event-Configuration-Database-Storage --- .../starter/TestDefaultConfiguration.java | 10 +- .../cob/COBBusinessStepServiceImpl.java | 4 + .../BusinessEventNotifierServiceImpl.java | 16 +- .../ExternalEventConfigurationCommand.java | 30 ++ .../data/ExternalEventConfigurationData.java | 30 ++ .../ExternalEventConfigurationItemData.java | 32 ++ ...alEventConfigurationNotFoundException.java | 26 ++ ...mExternalEventConfigurationRepository.java | 26 ++ ...ernalEventConfigurationRepositoryImpl.java | 43 +++ .../ExternalEventConfigurationRepository.java | 25 ++ .../domain/ExternalEventConfiguration.java | 46 +++ ...urationCommandFromApiJsonDeserializer.java | 55 +++ ...EventConfigurationReadPlatformService.java | 26 ++ ...tConfigurationReadPlatformServiceImpl.java | 43 +++ ...ventConfigurationWritePlatformService.java | 27 ++ ...ConfigurationWritePlatformServiceImpl.java | 69 ++++ .../ExternalEventsConfigurationMapper.java | 31 ++ .../support/ExternalEventMapperConfig.java | 27 ++ .../db/changelog/tenant/changelog-tenant.xml | 1 + ...d_external_event_default_configuration.xml | 324 ++++++++++++++++++ ...COBBusinessStepServiceStepDefinitions.java | 4 +- .../BusinessEventNotifierServiceImplTest.java | 30 +- ...lEventConfigurationRepositoryImplTest.java | 71 ++++ ...tConfigurationReadPlatformServiceTest.java | 87 +++++ ...ConfigurationWritePlatformServiceTest.java | 71 ++++ 25 files changed, 1146 insertions(+), 8 deletions(-) create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/command/ExternalEventConfigurationCommand.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/data/ExternalEventConfigurationData.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/data/ExternalEventConfigurationItemData.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/exception/ExternalEventConfigurationNotFoundException.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/CustomExternalEventConfigurationRepository.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/CustomExternalEventConfigurationRepositoryImpl.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/ExternalEventConfigurationRepository.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/domain/ExternalEventConfiguration.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/serialization/ExternalEventConfigurationCommandFromApiJsonDeserializer.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformService.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformServiceImpl.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformService.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformServiceImpl.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventsConfigurationMapper.java create mode 100644 fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/support/ExternalEventMapperConfig.java create mode 100644 fineract-provider/src/main/resources/db/changelog/tenant/parts/0056_add_external_event_default_configuration.xml create mode 100644 fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/CustomExternalEventConfigurationRepositoryImplTest.java create mode 100644 fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformServiceTest.java create mode 100644 fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformServiceTest.java diff --git a/custom/acme/loan/starter/src/test/java/com/acme/fineract/loan/starter/TestDefaultConfiguration.java b/custom/acme/loan/starter/src/test/java/com/acme/fineract/loan/starter/TestDefaultConfiguration.java index 39e463ff3..c60c4e8cb 100644 --- a/custom/acme/loan/starter/src/test/java/com/acme/fineract/loan/starter/TestDefaultConfiguration.java +++ b/custom/acme/loan/starter/src/test/java/com/acme/fineract/loan/starter/TestDefaultConfiguration.java @@ -24,6 +24,7 @@ import org.apache.fineract.cob.COBBusinessStepService; import org.apache.fineract.cob.COBBusinessStepServiceImpl; import org.apache.fineract.cob.domain.BatchBusinessStepRepository; import org.apache.fineract.infrastructure.core.config.FineractProperties; +import org.apache.fineract.infrastructure.event.business.service.BusinessEventNotifierService; import org.apache.fineract.portfolio.loanaccount.domain.LoanAccountDomainService; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -38,10 +39,15 @@ public class TestDefaultConfiguration { return mock(BatchBusinessStepRepository.class); } + @Bean + public BusinessEventNotifierService businessEventNotifierService() { + return mock(BusinessEventNotifierService.class); + } + @Bean public COBBusinessStepService cobBusinessStepService(BatchBusinessStepRepository batchBusinessStepRepository, - ApplicationContext context, ListableBeanFactory beanFactory) { - return new COBBusinessStepServiceImpl(batchBusinessStepRepository, context, beanFactory); + ApplicationContext context, ListableBeanFactory beanFactory, BusinessEventNotifierService businessEventNotifierService) { + return new COBBusinessStepServiceImpl(batchBusinessStepRepository, context, beanFactory, businessEventNotifierService); } @Bean diff --git a/fineract-provider/src/main/java/org/apache/fineract/cob/COBBusinessStepServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/cob/COBBusinessStepServiceImpl.java index 363efda84..33361c4cc 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/cob/COBBusinessStepServiceImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/cob/COBBusinessStepServiceImpl.java @@ -30,6 +30,7 @@ import org.apache.fineract.cob.exceptions.BusinessStepException; import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom; 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.jetbrains.annotations.NotNull; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.context.ApplicationContext; @@ -43,12 +44,14 @@ public class COBBusinessStepServiceImpl implements COBBusinessStepService { private final BatchBusinessStepRepository batchBusinessStepRepository; private final ApplicationContext applicationContext; private final ListableBeanFactory beanFactory; + private final BusinessEventNotifierService businessEventNotifierService; @Override public , S extends AbstractPersistableCustom> S run(TreeMap executionMap, S item) { if (executionMap == null || executionMap.isEmpty()) { throw new BusinessStepException("Execution map is empty! COB Business step execution skipped!"); } + businessEventNotifierService.startExternalEventRecording(); for (String businessStep : executionMap.values()) { try { COBBusinessStep businessStepBean = (COBBusinessStep) applicationContext.getBean(businessStep); @@ -60,6 +63,7 @@ public class COBBusinessStepServiceImpl implements COBBusinessStepService { ThreadLocalContextUtil.setActionContext(ActionContext.COB); } } + businessEventNotifierService.stopExternalEventRecording(); return item; } diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImpl.java index b0c2ac850..10ac52aca 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImpl.java @@ -28,6 +28,7 @@ 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.service.ExternalEventService; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Service; @@ -45,6 +46,7 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe private final ThreadLocal>> recordedEvents = ThreadLocal.withInitial(ArrayList::new); private final ExternalEventService externalEventService; + private final ExternalEventConfigurationRepository eventConfigurationRepository; private final FineractProperties fineractProperties; @Override @@ -88,10 +90,12 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe } if (isExternalEventPostingEnabled()) { // we only want to create external events for operations that were successful, hence the post listener - if (isExternalEventRecordingEnabled()) { - recordedEvents.get().add(businessEvent); - } else { - externalEventService.postEvent(businessEvent); + if (isExternalEventConfiguredForPosting(businessEvent.getType())) { + if (isExternalEventRecordingEnabled()) { + recordedEvents.get().add(businessEvent); + } else { + externalEventService.postEvent(businessEvent); + } } } } @@ -114,6 +118,10 @@ public class BusinessEventNotifierServiceImpl implements BusinessEventNotifierSe return fineractProperties.getEvents().getExternal().isEnabled(); } + private boolean isExternalEventConfiguredForPosting(String eventType) { + return eventConfigurationRepository.findExternalEventConfigurationByTypeWithNotFoundDetection(eventType).isEnabled(); + } + private void throwExceptionIfBulkEvent(BusinessEvent businessEvent) { if (businessEvent instanceof BulkBusinessEvent) { throw new IllegalArgumentException("BulkBusinessEvent cannot be raised directly"); diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/command/ExternalEventConfigurationCommand.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/command/ExternalEventConfigurationCommand.java new file mode 100644 index 000000000..e172b70ec --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/command/ExternalEventConfigurationCommand.java @@ -0,0 +1,30 @@ +/** + * 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.command; + +import java.util.Map; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public class ExternalEventConfigurationCommand { + + private final Map externalEventConfigurations; +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/data/ExternalEventConfigurationData.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/data/ExternalEventConfigurationData.java new file mode 100644 index 000000000..a9612c219 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/data/ExternalEventConfigurationData.java @@ -0,0 +1,30 @@ +/** + * 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.data; + +import java.util.List; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class ExternalEventConfigurationData { + + private List externalEventConfiguration; +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/data/ExternalEventConfigurationItemData.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/data/ExternalEventConfigurationItemData.java new file mode 100644 index 000000000..7b62d9621 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/data/ExternalEventConfigurationItemData.java @@ -0,0 +1,32 @@ +/** + * 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.data; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ExternalEventConfigurationItemData { + + private String type; + private boolean enabled; +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/exception/ExternalEventConfigurationNotFoundException.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/exception/ExternalEventConfigurationNotFoundException.java new file mode 100644 index 000000000..ce56b9e1e --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/exception/ExternalEventConfigurationNotFoundException.java @@ -0,0 +1,26 @@ +/** + * 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.exception; + +public class ExternalEventConfigurationNotFoundException extends RuntimeException { + + public ExternalEventConfigurationNotFoundException(final String externalEventType) { + super("Configuration not found for external event " + externalEventType); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/CustomExternalEventConfigurationRepository.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/CustomExternalEventConfigurationRepository.java new file mode 100644 index 000000000..8978e3a5b --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/CustomExternalEventConfigurationRepository.java @@ -0,0 +1,26 @@ +/** + * 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.repository; + +import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration; + +public interface CustomExternalEventConfigurationRepository { + + ExternalEventConfiguration findExternalEventConfigurationByTypeWithNotFoundDetection(String externalEventType); +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/CustomExternalEventConfigurationRepositoryImpl.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/CustomExternalEventConfigurationRepositoryImpl.java new file mode 100644 index 000000000..b1d0dbf42 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/CustomExternalEventConfigurationRepositoryImpl.java @@ -0,0 +1,43 @@ +/** + * 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.repository; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import lombok.RequiredArgsConstructor; +import org.apache.fineract.infrastructure.event.external.exception.ExternalEventConfigurationNotFoundException; +import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class CustomExternalEventConfigurationRepositoryImpl implements CustomExternalEventConfigurationRepository { + + @PersistenceContext + private final EntityManager entityManager; + + @Override + public ExternalEventConfiguration findExternalEventConfigurationByTypeWithNotFoundDetection(String externalEventType) { + final ExternalEventConfiguration configuration = entityManager.find(ExternalEventConfiguration.class, externalEventType); + if (configuration == null) { + throw new ExternalEventConfigurationNotFoundException(externalEventType); + } + return configuration; + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/ExternalEventConfigurationRepository.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/ExternalEventConfigurationRepository.java new file mode 100644 index 000000000..eb7e91d31 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/ExternalEventConfigurationRepository.java @@ -0,0 +1,25 @@ +/** + * 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.repository; + +import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ExternalEventConfigurationRepository + extends JpaRepository, CustomExternalEventConfigurationRepository {} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/domain/ExternalEventConfiguration.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/domain/ExternalEventConfiguration.java new file mode 100644 index 000000000..350f51458 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/repository/domain/ExternalEventConfiguration.java @@ -0,0 +1,46 @@ +/** + * 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.repository.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Table(name = "m_external_event_configuration") +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class ExternalEventConfiguration { + + @Id + @Column(name = "type", nullable = false) + private String type; + + @Column(name = "enabled", nullable = false) + private boolean enabled = false; + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/serialization/ExternalEventConfigurationCommandFromApiJsonDeserializer.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/serialization/ExternalEventConfigurationCommandFromApiJsonDeserializer.java new file mode 100644 index 000000000..24b2b1317 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/serialization/ExternalEventConfigurationCommandFromApiJsonDeserializer.java @@ -0,0 +1,55 @@ +/** + * 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.serialization; + +import com.google.gson.reflect.TypeToken; +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import lombok.AllArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.apache.fineract.infrastructure.core.exception.InvalidJsonException; +import org.apache.fineract.infrastructure.core.serialization.AbstractFromApiJsonDeserializer; +import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper; +import org.apache.fineract.infrastructure.event.external.command.ExternalEventConfigurationCommand; +import org.springframework.stereotype.Component; + +@Component +@AllArgsConstructor +public class ExternalEventConfigurationCommandFromApiJsonDeserializer + extends AbstractFromApiJsonDeserializer { + + private static final String EXTERNAL_EVENT_CONFIGURATIONS = "externalEventConfigurations"; + private final Set supportedParameters = new HashSet<>(Arrays.asList(EXTERNAL_EVENT_CONFIGURATIONS)); + private final FromJsonHelper fromApiJsonHelper; + + @Override + public ExternalEventConfigurationCommand commandFromApiJson(String json) { + if (StringUtils.isBlank(json)) { + throw new InvalidJsonException(); + } + + final Type typeOfMap = new TypeToken>() {}.getType(); + fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParameters); + + return fromApiJsonHelper.fromJson(json, ExternalEventConfigurationCommand.class); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformService.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformService.java new file mode 100644 index 000000000..82ee6697a --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformService.java @@ -0,0 +1,26 @@ +/** + * 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 org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationData; + +public interface ExternalEventConfigurationReadPlatformService { + + ExternalEventConfigurationData findAllExternalEventConfigurations(); +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformServiceImpl.java new file mode 100644 index 000000000..a7c6eca5e --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformServiceImpl.java @@ -0,0 +1,43 @@ +/** + * 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 java.util.List; +import lombok.RequiredArgsConstructor; +import org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationData; +import org.apache.fineract.infrastructure.event.external.repository.ExternalEventConfigurationRepository; +import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class ExternalEventConfigurationReadPlatformServiceImpl implements ExternalEventConfigurationReadPlatformService { + + private final ExternalEventConfigurationRepository repository; + private final ExternalEventsConfigurationMapper mapper; + + @Override + public ExternalEventConfigurationData findAllExternalEventConfigurations() { + ExternalEventConfigurationData configurationData = new ExternalEventConfigurationData(); + List eventConfigurations = repository.findAll(); + configurationData.setExternalEventConfiguration(mapper.map(eventConfigurations)); + return configurationData; + } + +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformService.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformService.java new file mode 100644 index 000000000..9724cd1ee --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformService.java @@ -0,0 +1,27 @@ +/** + * 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 org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; + +public interface ExternalEventConfigurationWritePlatformService { + + CommandProcessingResult updateConfigurations(JsonCommand command); +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformServiceImpl.java new file mode 100644 index 000000000..77e279303 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformServiceImpl.java @@ -0,0 +1,69 @@ +/** + * 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 java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.AllArgsConstructor; +import org.apache.fineract.infrastructure.core.api.JsonCommand; +import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; +import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder; +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.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@AllArgsConstructor +public class ExternalEventConfigurationWritePlatformServiceImpl implements ExternalEventConfigurationWritePlatformService { + + private final ExternalEventConfigurationRepository repository; + private final ExternalEventConfigurationCommandFromApiJsonDeserializer fromApiJsonDeserializer; + + @Transactional + @Override + public CommandProcessingResult updateConfigurations(final JsonCommand command) { + final ExternalEventConfigurationCommand configurationCommand = fromApiJsonDeserializer.commandFromApiJson(command.json()); + final Map commandConfigurations = configurationCommand.getExternalEventConfigurations(); + final Map changes = new HashMap<>(); + final Map changedConfigurations = new HashMap<>(); + final List modifiedConfigurations = new ArrayList<>(); + for (final String eventType : commandConfigurations.keySet()) { + final ExternalEventConfiguration configuration = repository + .findExternalEventConfigurationByTypeWithNotFoundDetection(eventType); + final boolean canEnable = commandConfigurations.get(eventType).booleanValue(); + configuration.setEnabled(canEnable); + changedConfigurations.put(eventType, canEnable); + modifiedConfigurations.add(configuration); + } + if (!modifiedConfigurations.isEmpty()) { + this.repository.saveAll(modifiedConfigurations); + } + + if (!changedConfigurations.isEmpty()) { + changes.put("externalEventConfigurations", changedConfigurations); + } + + return new CommandProcessingResultBuilder().withCommandId(command.commandId()).with(changes).build(); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventsConfigurationMapper.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventsConfigurationMapper.java new file mode 100644 index 000000000..4214e5182 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventsConfigurationMapper.java @@ -0,0 +1,31 @@ +/** + * 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 java.util.List; +import org.apache.fineract.infrastructure.event.external.data.ExternalEventConfigurationItemData; +import org.apache.fineract.infrastructure.event.external.repository.domain.ExternalEventConfiguration; +import org.apache.fineract.infrastructure.event.external.service.support.ExternalEventMapperConfig; +import org.mapstruct.Mapper; + +@Mapper(config = ExternalEventMapperConfig.class) +public interface ExternalEventsConfigurationMapper { + + List map(List source); +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/support/ExternalEventMapperConfig.java b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/support/ExternalEventMapperConfig.java new file mode 100644 index 000000000..8d1894a19 --- /dev/null +++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/service/support/ExternalEventMapperConfig.java @@ -0,0 +1,27 @@ +/** + * 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.support; + +import org.mapstruct.Builder; +import org.mapstruct.MapperConfig; +import org.mapstruct.MappingConstants; +import org.mapstruct.ReportingPolicy; + +@MapperConfig(componentModel = MappingConstants.ComponentModel.SPRING, unmappedTargetPolicy = ReportingPolicy.ERROR, builder = @Builder(disableBuilder = true)) +public class ExternalEventMapperConfig {} diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml index 1d9f51d22..118402238 100644 --- a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml +++ b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml @@ -75,4 +75,5 @@ + diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/parts/0056_add_external_event_default_configuration.xml b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0056_add_external_event_default_configuration.xml new file mode 100644 index 000000000..4683e0c54 --- /dev/null +++ b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0056_add_external_event_default_configuration.xml @@ -0,0 +1,324 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/fineract-provider/src/test/java/org/apache/fineract/cob/COBBusinessStepServiceStepDefinitions.java b/fineract-provider/src/test/java/org/apache/fineract/cob/COBBusinessStepServiceStepDefinitions.java index a82a8a110..866e13511 100644 --- a/fineract-provider/src/test/java/org/apache/fineract/cob/COBBusinessStepServiceStepDefinitions.java +++ b/fineract-provider/src/test/java/org/apache/fineract/cob/COBBusinessStepServiceStepDefinitions.java @@ -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 executionMap; diff --git a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImplTest.java b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImplTest.java index 3dcd7798d..6cf961ab4 100644 --- a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImplTest.java +++ b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/business/service/BusinessEventNotifierServiceImplTest.java @@ -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 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 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 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(); diff --git a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/CustomExternalEventConfigurationRepositoryImplTest.java b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/CustomExternalEventConfigurationRepositoryImplTest.java new file mode 100644 index 000000000..300521850 --- /dev/null +++ b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/CustomExternalEventConfigurationRepositoryImplTest.java @@ -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")); + } + +} diff --git a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformServiceTest.java b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformServiceTest.java new file mode 100644 index 000000000..4c1d45310 --- /dev/null +++ b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationReadPlatformServiceTest.java @@ -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 configurations = Arrays.asList(new ExternalEventConfiguration("aType", true), + new ExternalEventConfiguration("bType", false)); + List 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 emptyConfiguration = new ArrayList<>(); + when(repository.findAll()).thenReturn(emptyConfiguration); + // when + ExternalEventConfigurationData actualConfiguration = underTest.findAllExternalEventConfigurations(); + // then + assertThat(actualConfiguration.getExternalEventConfiguration(), hasSize(0)); + + } +} diff --git a/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformServiceTest.java b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformServiceTest.java new file mode 100644 index 000000000..5025715b7 --- /dev/null +++ b/fineract-provider/src/test/java/org/apache/fineract/infrastructure/event/external/service/ExternalEventConfigurationWritePlatformServiceTest.java @@ -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 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()); + } + +}