Skip to content

Commit

Permalink
[SELC-5887] refactor for change requests
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo committed Oct 30, 2024
1 parent b02715a commit 45a96ac
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static it.pagopa.selfcare.user.model.TrackEventInput.toTrackEventInput;
import static it.pagopa.selfcare.user.model.constants.EventsMetric.*;
import static it.pagopa.selfcare.user.model.constants.EventsName.EVENT_USER_CDC_NAME;
import static it.pagopa.selfcare.user.model.constants.EventsName.FD_EVENT_USER_CDC_NAME;
import static java.util.Arrays.asList;

@Startup
Expand Down Expand Up @@ -256,18 +257,18 @@ public void consumerToSendUserEventForFD(ChangeStreamDocument<UserInstitution> d
log.info("Sending message to EventHubFdRestClient ... ");
return eventHubFdRestClient.sendMessage(fdUserNotificationToSend)
.onFailure().retry().withBackOff(Duration.ofSeconds(retryMinBackOff), Duration.ofSeconds(retryMaxBackOff)).atMost(maxRetry)
.onItem().invoke(() -> telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(toTrackEventInput(fdUserNotificationToSend)), Map.of(EVENTS_USER_INSTITUTION_PRODUCT_SUCCESS, 1D)))
.onFailure().invoke(() -> telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(toTrackEventInput(fdUserNotificationToSend)), Map.of(EVENTS_USER_INSTITUTION_PRODUCT_FAILURE, 1D)));
.onItem().invoke(() -> telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(toTrackEventInput(fdUserNotificationToSend)), Map.of(FD_EVENTS_USER_INSTITUTION_PRODUCT_SUCCESS, 1D)))
.onFailure().invoke(() -> telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(toTrackEventInput(fdUserNotificationToSend)), Map.of(FD_EVENTS_USER_INSTITUTION_PRODUCT_FAILURE, 1D)));
}
))
.subscribe().with(
result -> {
log.info("SendFdEvents successfully performed from UserInstitution document having id: {}", document.getDocumentKey().toJson());
telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(toTrackEventInputByUserInstitution(userInstitutionChanged)), Map.of(EVENTS_USER_INSTITUTION_SUCCESS, 1D));
telemetryClient.trackEvent(FD_EVENT_USER_CDC_NAME, mapPropsForTrackEvent(toTrackEventInputByUserInstitution(userInstitutionChanged)), Map.of(FD_EVENTS_USER_INSTITUTION_SUCCESS, 1D));
},
failure -> {
log.error("Error during SendFdEvents from UserInstitution document having id: {} , message: {}", document.getDocumentKey().toJson(), failure.getMessage());
telemetryClient.trackEvent(EVENT_USER_CDC_NAME, mapPropsForTrackEvent(toTrackEventInputByUserInstitution(userInstitutionChanged)), Map.of(EVENTS_USER_INSTITUTION_FAILURE, 1D));
telemetryClient.trackEvent(FD_EVENT_USER_CDC_NAME, mapPropsForTrackEvent(toTrackEventInputByUserInstitution(userInstitutionChanged)), Map.of(FD_EVENTS_USER_INSTITUTION_FAILURE, 1D));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package it.pagopa.selfcare.user;

import it.pagopa.selfcare.user.model.FdUserNotificationToSend;
import it.pagopa.selfcare.user.model.NotificationUserType;
import it.pagopa.selfcare.user.model.UserToNotify;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.time.OffsetDateTime;

class FdUserNotificationToSendTest {

@Test
void fdUserNotificationToSend_ACTIVE() {
FdUserNotificationToSend notification = new FdUserNotificationToSend();
notification.setId("123");
notification.setInstitutionId("inst-456");
notification.setProduct("product-789");
notification.setCreatedAt(OffsetDateTime.now().minusDays(1));
notification.setUpdatedAt(OffsetDateTime.now());
notification.setOnboardingTokenId("token-101112");
notification.setType(NotificationUserType.ACTIVE_USER);
UserToNotify user = new UserToNotify();
notification.setUser(user);

assertEquals("123", notification.getId());
assertEquals("inst-456", notification.getInstitutionId());
assertEquals("product-789", notification.getProduct());
assertNotNull(notification.getCreatedAt());
assertNotNull(notification.getUpdatedAt());
assertEquals("token-101112", notification.getOnboardingTokenId());
assertEquals(NotificationUserType.ACTIVE_USER, notification.getType());
assertEquals(user, notification.getUser());
}

@Test
void fdUserNotificationToSend_DELETE() {
FdUserNotificationToSend notification = new FdUserNotificationToSend();
notification.setId("123");
notification.setInstitutionId("inst-456");
notification.setProduct("product-789");
notification.setCreatedAt(OffsetDateTime.now().minusDays(1));
notification.setUpdatedAt(OffsetDateTime.now());
notification.setOnboardingTokenId("token-101112");
notification.setType(NotificationUserType.DELETE_USER);
UserToNotify user = new UserToNotify();
notification.setUser(user);

assertEquals("123", notification.getId());
assertEquals("inst-456", notification.getInstitutionId());
assertEquals("product-789", notification.getProduct());
assertNotNull(notification.getCreatedAt());
assertNotNull(notification.getUpdatedAt());
assertEquals("token-101112", notification.getOnboardingTokenId());
assertEquals(NotificationUserType.DELETE_USER, notification.getType());
assertEquals(user, notification.getUser());
}

@Test
void fdUserNotificationToSend_SUSPEND() {
FdUserNotificationToSend notification = new FdUserNotificationToSend();
notification.setId("123");
notification.setInstitutionId("inst-456");
notification.setProduct("product-789");
notification.setCreatedAt(OffsetDateTime.now().minusDays(1));
notification.setUpdatedAt(OffsetDateTime.now());
notification.setOnboardingTokenId("token-101112");
notification.setType(NotificationUserType.SUSPEND_USER);
UserToNotify user = new UserToNotify();
notification.setUser(user);

assertEquals("123", notification.getId());
assertEquals("inst-456", notification.getInstitutionId());
assertEquals("product-789", notification.getProduct());
assertNotNull(notification.getCreatedAt());
assertNotNull(notification.getUpdatedAt());
assertEquals("token-101112", notification.getOnboardingTokenId());
assertEquals(NotificationUserType.SUSPEND_USER, notification.getType());
assertEquals(user, notification.getUser());
}

@Test
void fdUserNotificationToSend_withNullFields_shouldHandleNullValues() {
FdUserNotificationToSend notification = new FdUserNotificationToSend();

assertNull(notification.getId());
assertNull(notification.getInstitutionId());
assertNull(notification.getProduct());
assertNull(notification.getCreatedAt());
assertNull(notification.getUpdatedAt());
assertNull(notification.getOnboardingTokenId());
assertNull(notification.getType());
assertNull(notification.getUser());
}

@Test
void fdUserNotificationToSend_withPartialData_shouldSetFieldsCorrectly() {
FdUserNotificationToSend notification = new FdUserNotificationToSend();
notification.setId("123");
notification.setProduct("product-789");

assertEquals("123", notification.getId());
assertNull(notification.getInstitutionId());
assertEquals("product-789", notification.getProduct());
assertNull(notification.getCreatedAt());
assertNull(notification.getUpdatedAt());
assertNull(notification.getOnboardingTokenId());
assertNull(notification.getType());
assertNull(notification.getUser());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class EventsMetric {
public static final String EVENTS_USER_INSTITUTION_PRODUCT_FAILURE = "EventsUserInstitutionProduct_failures";
public static final String EVENTS_USER_INSTITUTION_PRODUCT_SUCCESS = "EventsUserInstitutionProduct_success";

public static final String FD_EVENTS_USER_INSTITUTION_FAILURE = "EventsUserInstitution_failures";
public static final String FD_EVENTS_USER_INSTITUTION_SUCCESS = "EventsUserInstitution_success";
public static final String FD_EVENTS_USER_INSTITUTION_PRODUCT_FAILURE = "EventsUserInstitutionProduct_failures";
public static final String FD_EVENTS_USER_INSTITUTION_PRODUCT_SUCCESS = "EventsUserInstitutionProduct_success";

public static final String EVENTS_USER_GROUP_FAILURE = "EventsUserGroup_failures";
public static final String EVENTS_USER_GROUP_SUCCESS = "EventsUserGroup_success";
public static final String EVENTS_USER_GROUP_PRODUCT_FAILURE = "EventsUserGroupProduct_failures";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ public class EventsName {

public static final String EVENT_USER_MS_NAME = "USER_MS";
public static final String EVENT_USER_CDC_NAME = "USER_CDC";

public static final String FD_EVENT_USER_CDC_NAME = "FD_USER_CDC";
public static final String EVENT_USER_GROUP_CDC_NAME = "USER_GROUP_CDC";
}

0 comments on commit 45a96ac

Please sign in to comment.