generated from pagopa/pagopa-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SELC-5034] feature: added logic to discriminate event type in cdc module #288
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ onboarding-cdc.storage.connection-string=${STORAGE_CONNECTION_STRING:UseDevelopm | |
onboarding-cdc.retry.min-backoff=${ONBOARDING-CDC-RETRY-MIN-BACKOFF:1} | ||
onboarding-cdc.retry.max-backoff=${ONBOARDING-CDC-RETRY-MAX-BACKOFF:2} | ||
onboarding-cdc.retry=${ONBOARDING-CDC-RETRY:3} | ||
onboarding-cdc.minutes-threshold-for-update-notification=${ONBOARDING-CDC-MINUTES-THRESHOLD-FOR-UPDATE-NOTIFICATION:5} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
|
||
quarkus.openapi-generator.codegen.spec.onboarding_functions_json.mutiny=true | ||
quarkus.openapi-generator.codegen.spec.onboarding_functions_json.type-mappings.DateTime=java.time.LocalDateTime | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be better to add some comments about this function...what means "isOverUpdateThreshold" ?