generated from pagopa/pagopa-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SELC-5034] feature: added logic to discriminate event type in cdc mo…
…dule (#288) Co-authored-by: andrea-putzu <[email protected]>
- Loading branch information
1 parent
bb7d9c1
commit 41ff0d7
Showing
10 changed files
with
198 additions
and
29 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...onboarding-cdc/src/main/java/it/pagopa/selfcare/onboarding/event/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package it.pagopa.selfcare.onboarding.event; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
import it.pagopa.selfcare.onboarding.event.entity.Onboarding; | ||
import it.pagopa.selfcare.onboarding.event.entity.util.QueueEvent; | ||
import it.pagopa.selfcare.onboarding.event.mapper.OnboardingMapper; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.openapi.quarkus.onboarding_functions_json.api.NotificationsApi; | ||
import org.openapi.quarkus.onboarding_functions_json.model.OrchestrationResponse; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.util.Objects; | ||
|
||
@ApplicationScoped | ||
public class NotificationService { | ||
@Inject | ||
@RestClient | ||
NotificationsApi notificationsApi; | ||
private final OnboardingMapper onboardingMapper; | ||
private final Integer retryMinBackOff; | ||
private final Integer retryMaxBackOff; | ||
private final Integer maxRetry; | ||
private final Integer minutesThresholdForUpdateNotification; | ||
|
||
public NotificationService(OnboardingMapper onboardingMapper, | ||
@ConfigProperty(name = "onboarding-cdc.retry.min-backoff") Integer retryMinBackOff, | ||
@ConfigProperty(name = "onboarding-cdc.retry.max-backoff") Integer retryMaxBackOff, | ||
@ConfigProperty(name = "onboarding-cdc.retry") Integer maxRetry, | ||
@ConfigProperty(name = "onboarding-cdc.minutes-threshold-for-update-notification") Integer minutesThresholdForUpdateNotification) { | ||
this.onboardingMapper = onboardingMapper; | ||
this.retryMinBackOff = retryMinBackOff; | ||
this.retryMaxBackOff = retryMaxBackOff; | ||
this.maxRetry = maxRetry; | ||
this.minutesThresholdForUpdateNotification = minutesThresholdForUpdateNotification; | ||
} | ||
|
||
public Uni<OrchestrationResponse> invokeNotificationApi(Onboarding onboarding) { | ||
assert onboarding != null; | ||
QueueEvent queueEvent = determineEventType(onboarding); | ||
return notificationsApi.apiNotificationPost(queueEvent.name(), onboardingMapper.toEntity(onboarding)) | ||
.onFailure().retry().withBackOff(Duration.ofSeconds(retryMinBackOff), Duration.ofHours(retryMaxBackOff)).atMost(maxRetry); | ||
} | ||
|
||
private QueueEvent determineEventType(Onboarding onboarding) { | ||
return switch (onboarding.getStatus()) { | ||
case COMPLETED -> (isOverUpdateThreshold(onboarding.getUpdatedAt(), onboarding.getActivatedAt())) ? QueueEvent.UPDATE : QueueEvent.ADD; | ||
case DELETED -> QueueEvent.UPDATE; | ||
default -> throw new IllegalArgumentException("Onboarding status not supported"); | ||
}; | ||
} | ||
|
||
private boolean isOverUpdateThreshold(LocalDateTime updatedAt, LocalDateTime activatedAt) { | ||
return Objects.nonNull(updatedAt) | ||
&& Objects.nonNull(activatedAt) | ||
&& updatedAt.isAfter(activatedAt.plusMinutes(minutesThresholdForUpdateNotification)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...oarding-cdc/src/main/java/it/pagopa/selfcare/onboarding/event/entity/util/QueueEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package it.pagopa.selfcare.onboarding.event.entity.util; | ||
|
||
public enum QueueEvent { | ||
ADD, | ||
UPDATE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...arding-cdc/src/test/java/it/pagopa/selfcare/onboarding/event/NotificationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package it.pagopa.selfcare.onboarding.event; | ||
|
||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; | ||
import it.pagopa.selfcare.onboarding.common.OnboardingStatus; | ||
import it.pagopa.selfcare.onboarding.event.entity.Onboarding; | ||
import it.pagopa.selfcare.onboarding.event.entity.util.QueueEvent; | ||
import it.pagopa.selfcare.onboarding.event.mapper.OnboardingMapper; | ||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.openapi.quarkus.onboarding_functions_json.api.NotificationsApi; | ||
import org.openapi.quarkus.onboarding_functions_json.model.OrchestrationResponse; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
@QuarkusTest | ||
public class NotificationServiceTest { | ||
@Mock | ||
private OnboardingMapper onboardingMapper; | ||
@InjectMock | ||
@RestClient | ||
private NotificationsApi notificationsApi; | ||
@Inject | ||
private NotificationService notificationService; | ||
|
||
@Test | ||
@DisplayName("Should handle Invoke Notification API Success passing event ADD") | ||
public void shouldHandleInvokeNotificationApiSuccessForQueueEventAdd() { | ||
Onboarding onboarding = new Onboarding(); | ||
onboarding.setStatus(OnboardingStatus.COMPLETED); | ||
onboarding.setUpdatedAt(LocalDateTime.now()); | ||
onboarding.setActivatedAt(LocalDateTime.now()); | ||
|
||
when(notificationsApi.apiNotificationPost(any(), any())) | ||
.thenReturn(Uni.createFrom().item(new OrchestrationResponse())); | ||
|
||
UniAssertSubscriber<OrchestrationResponse> subscriber = notificationService | ||
.invokeNotificationApi(onboarding) | ||
.subscribe().withSubscriber(UniAssertSubscriber.create()); | ||
|
||
subscriber.assertCompleted().awaitItem(); | ||
|
||
verify(notificationsApi, times(1)).apiNotificationPost(eq(QueueEvent.ADD.name()), any()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Should handle Invoke Notification API Success passing event UPDATE") | ||
public void shouldHandleInvokeNotificationApiSuccessForQueueEventUpdate() { | ||
Onboarding onboarding = new Onboarding(); | ||
onboarding.setStatus(OnboardingStatus.COMPLETED); | ||
onboarding.setUpdatedAt(LocalDateTime.now().plusMinutes(10)); // 5 minutes should be the threshold | ||
onboarding.setActivatedAt(LocalDateTime.now()); | ||
|
||
when(notificationsApi.apiNotificationPost(any(), any())) | ||
.thenReturn(Uni.createFrom().item(new OrchestrationResponse())); | ||
|
||
UniAssertSubscriber<OrchestrationResponse> subscriber = notificationService | ||
.invokeNotificationApi(onboarding) | ||
.subscribe().withSubscriber(UniAssertSubscriber.create()); | ||
|
||
subscriber.assertCompleted().awaitItem(); | ||
|
||
verify(notificationsApi, times(1)).apiNotificationPost(eq(QueueEvent.UPDATE.name()), any()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Should handle Invoke Notification API Success passing event UPDATE with status DELETED") | ||
public void shouldHandleInvokeNotificationApiSuccessForQueueEventUpdateWithStatusDeleted() { | ||
Onboarding onboarding = new Onboarding(); | ||
onboarding.setStatus(OnboardingStatus.DELETED); | ||
onboarding.setUpdatedAt(LocalDateTime.now()); // 5 minutes should be the threshold | ||
onboarding.setActivatedAt(LocalDateTime.now()); | ||
|
||
when(notificationsApi.apiNotificationPost(any(), any())) | ||
.thenReturn(Uni.createFrom().item(new OrchestrationResponse())); | ||
|
||
UniAssertSubscriber<OrchestrationResponse> subscriber = notificationService | ||
.invokeNotificationApi(onboarding) | ||
.subscribe().withSubscriber(UniAssertSubscriber.create()); | ||
|
||
subscriber.assertCompleted().awaitItem(); | ||
|
||
verify(notificationsApi, times(1)).apiNotificationPost(eq(QueueEvent.UPDATE.name()), any()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters