Skip to content

Commit

Permalink
SELC-5119 feat: added watch config to prevent azure function invocati…
Browse files Browse the repository at this point in the history
…on on demand
  • Loading branch information
empassaro committed Jun 17, 2024
1 parent 885a5ad commit 6daa0cc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ public class NotificationService {
@RestClient
NotificationsApi notificationsApi;
private final OnboardingMapper onboardingMapper;
private final Boolean watchEnabled;
private final Integer retryMinBackOff;
private final Integer retryMaxBackOff;
private final Integer maxRetry;
private final Integer minutesThresholdForUpdateNotification;

public NotificationService(OnboardingMapper onboardingMapper,
@ConfigProperty(name = "onboarding-cdc.mongodb.watch.enabled") Boolean watchEnabled,
@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.watchEnabled = watchEnabled;
this.retryMinBackOff = retryMinBackOff;
this.retryMaxBackOff = retryMaxBackOff;
this.maxRetry = maxRetry;
this.minutesThresholdForUpdateNotification = minutesThresholdForUpdateNotification;
}

public Uni<OrchestrationResponse> invokeNotificationApi(Onboarding onboarding) {
// If watch is disabled, return an empty response
if(!watchEnabled) {
return Uni.createFrom().item(new OrchestrationResponse());
}

assert onboarding != null;
QueueEvent queueEvent = determineEventType(onboarding);
return notificationsApi.apiNotificationPost(queueEvent.name(), onboardingMapper.toEntity(onboarding))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
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 it.pagopa.selfcare.onboarding.event.profile.NotificationTestProfile;
import jakarta.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.mockito.Mock;
import org.openapi.quarkus.onboarding_functions_json.api.NotificationsApi;
import org.openapi.quarkus.onboarding_functions_json.model.OrchestrationResponse;
Expand Down Expand Up @@ -89,4 +93,27 @@ public void shouldHandleInvokeNotificationApiSuccessForQueueEventUpdateWithStatu

verify(notificationsApi, times(1)).apiNotificationPost(eq(QueueEvent.UPDATE.name()), any());
}

@Nested
@TestProfile(NotificationTestProfile.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class NotificationServiceTestWithDisabledWatcherTest {
@Test
@DisplayName("Should not invoke Notification API when watcher is disabled")
public void shouldNotInvokeNotificationApiWhenWatcheIsDisabled() {
Onboarding onboarding = new Onboarding();
onboarding.setStatus(OnboardingStatus.DELETED);
onboarding.setUpdatedAt(LocalDateTime.now()); // 5 minutes should be the threshold
onboarding.setActivatedAt(LocalDateTime.now());


UniAssertSubscriber<OrchestrationResponse> subscriber = notificationService
.invokeNotificationApi(onboarding)
.subscribe().withSubscriber(UniAssertSubscriber.create());

subscriber.assertCompleted().awaitItem();

verify(notificationsApi, times(0)).apiNotificationPost(any(), any());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.pagopa.selfcare.onboarding.event.profile;

import io.quarkus.test.junit.QuarkusTestProfile;

import java.util.Map;

public class NotificationTestProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Map.of("onboarding-cdc.mongodb.watch.enabled", "false");
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
onboarding-cdc.mongodb.watch.enabled=true

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ app_settings = [
},
{
name = "ONBOARDING-CDC-MONGODB-WATCH-ENABLED"
value = "false"
value = "true"
},
{
name = "ONBOARDING_FUNCTIONS_URL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ app_settings = [
},
{
name = "ONBOARDING-CDC-MONGODB-WATCH-ENABLED"
value = "false"
value = "true"
},
{
name = "ONBOARDING_FUNCTIONS_URL"
Expand Down

0 comments on commit 6daa0cc

Please sign in to comment.