diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/NotificationEventServiceDefault.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/NotificationEventServiceDefault.java index 4a9d6a010..bb92759cc 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/NotificationEventServiceDefault.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/NotificationEventServiceDefault.java @@ -87,34 +87,29 @@ public void send(ExecutionContext context, Onboarding onboarding, QueueEvent que return; } - try { - context.getLogger().info(() -> String.format("Getting product info for onboarding with ID %s and productId %s", onboarding.getId(), onboarding.getProductId())); - Product product = productService.getProduct(onboarding.getProductId()); - if (product.getConsumers() == null || product.getConsumers().isEmpty()) { - context.getLogger().warning(() -> String.format("Node consumers is null or empty for product with ID %s", onboarding.getProductId())); - return; - } + context.getLogger().info(() -> String.format("Getting product info for onboarding with ID %s and productId %s", onboarding.getId(), onboarding.getProductId())); + Product product = productService.getProduct(onboarding.getProductId()); + if (product.getConsumers() == null || product.getConsumers().isEmpty()) { + context.getLogger().warning(() -> String.format("Node consumers is null or empty for product with ID %s", onboarding.getProductId())); + return; + } - if(Objects.isNull(queueEvent)) { - queueEvent = queueEventExaminer.determineEventType(onboarding); - } + if(Objects.isNull(queueEvent)) { + queueEvent = queueEventExaminer.determineEventType(onboarding); + } - context.getLogger().info(() -> String.format("Retrieving institution having ID %s", onboarding.getInstitution().getId())); - InstitutionResponse institution = institutionApi.retrieveInstitutionByIdUsingGET(onboarding.getInstitution().getId()); + context.getLogger().info(() -> String.format("Retrieving institution having ID %s", onboarding.getInstitution().getId())); + InstitutionResponse institution = institutionApi.retrieveInstitutionByIdUsingGET(onboarding.getInstitution().getId()); - Token token = tokenRepository.findByOnboardingId(onboarding.getId()).orElse(null); - NotificationsResources notificationsResources = new NotificationsResources(onboarding, institution, token, queueEvent); - for (String consumer : product.getConsumers()) { - NotificationConfig.Consumer consumerConfig = notificationConfig.consumers().get(consumer.toLowerCase()); - prepareAndSendNotification(context, product, consumerConfig, notificationsResources, notificationEventTraceId); - } - } catch (Exception e) { - context.getLogger().severe(String.format("Error sending notification for onboarding with ID %s %s", onboarding.getId(), Arrays.toString(e.getStackTrace()))); - telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, onboardingEventFailureMap(onboarding, e, notificationEventTraceId), Map.of(EVENT_ONBOARDING_INSTTITUTION_FN_FAILURE, 1D)); + Token token = tokenRepository.findByOnboardingId(onboarding.getId()).orElse(null); + NotificationsResources notificationsResources = new NotificationsResources(onboarding, institution, token, queueEvent); + for (String consumer : product.getConsumers()) { + NotificationConfig.Consumer consumerConfig = notificationConfig.consumers().get(consumer.toLowerCase()); + prepareAndSendNotification(context, product, consumerConfig, notificationsResources, notificationEventTraceId); } } - private void prepareAndSendNotification(ExecutionContext context, Product product, NotificationConfig.Consumer consumer, NotificationsResources notificationsResources, String notificationEventTraceId) throws JsonProcessingException { + private void prepareAndSendNotification(ExecutionContext context, Product product, NotificationConfig.Consumer consumer, NotificationsResources notificationsResources, String notificationEventTraceId) { NotificationBuilder notificationBuilder = notificationBuilderFactory.create(consumer); if (notificationBuilder.shouldSendNotification(notificationsResources.getOnboarding(), notificationsResources.getInstitution())) { NotificationToSend notificationToSend = notificationBuilder.buildNotificationToSend(notificationsResources.getOnboarding(), notificationsResources.getToken(), notificationsResources.getInstitution(), notificationsResources.getQueueEvent()); @@ -125,19 +120,22 @@ private void prepareAndSendNotification(ExecutionContext context, Product produc } } - private void sendNotification(ExecutionContext context, String topic, NotificationToSend notificationToSend, String notificationEventTraceId) throws JsonProcessingException { - String message = mapper.writeValueAsString(notificationToSend); - context.getLogger().info(() -> String.format("Sending notification on topic: %s with message: %s", topic, message)); - + private void sendNotification(ExecutionContext context, String topic, NotificationToSend notificationToSend, String notificationEventTraceId) { + String message = null; try { - eventHubRestClient.sendMessage(topic, message); - telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, notificationEventMap(notificationToSend, topic, notificationEventTraceId), Map.of(EVENT_ONBOARDING_INSTTITUTION_FN_SUCCESS, 1D)); - } catch (Exception e) { - throw new NotificationException(e.getMessage()); + message = mapper.writeValueAsString(notificationToSend); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } finally { + String finalMessage = message; + context.getLogger().info(() -> String.format("Sending notification on topic: %s with message: %s", topic, finalMessage)); } + + eventHubRestClient.sendMessage(topic, message); + telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, notificationEventMap(notificationToSend, topic, notificationEventTraceId), Map.of(EVENT_ONBOARDING_INSTTITUTION_FN_SUCCESS, 1D)); } - private void sendTestEnvProductsNotification(ExecutionContext context, Product product, String topic, NotificationToSend notificationToSend, String notificationEventTraceId) throws JsonProcessingException { + private void sendTestEnvProductsNotification(ExecutionContext context, Product product, String topic, NotificationToSend notificationToSend, String notificationEventTraceId) { context.getLogger().info(() -> String.format("Starting sendTestEnvProductsNotification with testEnv %s", product.getTestEnvProductIds())); if (product.getTestEnvProductIds() != null) { for (String testEnvProductId : product.getTestEnvProductIds()) { diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/BaseNotificationBuilder.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/BaseNotificationBuilder.java index f5eb720eb..454d5fdab 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/BaseNotificationBuilder.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/BaseNotificationBuilder.java @@ -153,18 +153,13 @@ private PaymentServiceProvider toSetPaymentServiceProvider(PaymentServiceProvide @Override public void retrieveAndSetGeographicData(InstitutionToNotify institution) { - try { - InstitutionResource institutionProxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institution.getTaxCode(), null, null); - institution.setIstatCode(institutionProxyInfo.getIstatCode()); - institution.setCategory(institutionProxyInfo.getCategory()); - GeographicTaxonomyResource geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(institutionProxyInfo.getIstatCode()); - institution.setCounty(geographicTaxonomies.getProvinceAbbreviation()); - institution.setCountry(geographicTaxonomies.getCountryAbbreviation()); - institution.setCity(geographicTaxonomies.getDesc().replace(DESCRIPTION_TO_REPLACE_REGEX, "")); - } catch (Exception e) { - log.warn("Error while searching institution {} on IPA, {} ", institution.getTaxCode(), e.getMessage()); - institution.setIstatCode(null); - } + InstitutionResource institutionProxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institution.getTaxCode(), null, null); + institution.setIstatCode(institutionProxyInfo.getIstatCode()); + institution.setCategory(institutionProxyInfo.getCategory()); + GeographicTaxonomyResource geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(institutionProxyInfo.getIstatCode()); + institution.setCounty(geographicTaxonomies.getProvinceAbbreviation()); + institution.setCountry(geographicTaxonomies.getCountryAbbreviation()); + institution.setCity(geographicTaxonomies.getDesc().replace(DESCRIPTION_TO_REPLACE_REGEX, "")); } @Override diff --git a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/SapNotificationBuilder.java b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/SapNotificationBuilder.java index ce908ea97..e73d36383 100644 --- a/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/SapNotificationBuilder.java +++ b/apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/utils/SapNotificationBuilder.java @@ -80,40 +80,35 @@ public InstitutionToNotify retrieveInstitution(InstitutionResponse institution) } @Override public void retrieveAndSetGeographicData(InstitutionToNotify institutionToNotify) { - try { - GeographicTaxonomyResource geographicTaxonomies; - if (institutionToNotify.getSubUnitType() != null) { - switch (Objects.requireNonNull(institutionToNotify.getSubUnitType())) { - case "UO" -> { - UOResource organizationUnit = proxyRegistryUoApi.findByUnicodeUsingGET1(institutionToNotify.getSubUnitCode(), null); - institutionToNotify.setIstatCode(organizationUnit.getCodiceComuneISTAT()); - geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(organizationUnit.getCodiceComuneISTAT()); - } - case "AOO" -> { - AOOResource homogeneousOrganizationalArea = proxyRegistryAooApi.findByUnicodeUsingGET(institutionToNotify.getSubUnitCode(), null); - institutionToNotify.setIstatCode(homogeneousOrganizationalArea.getCodiceComuneISTAT()); - geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(homogeneousOrganizationalArea.getCodiceComuneISTAT()); - } - default -> { - InstitutionResource proxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institutionToNotify.getTaxCode(), null, null); - institutionToNotify.setIstatCode(proxyInfo.getIstatCode()); - geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(proxyInfo.getIstatCode()); - } + GeographicTaxonomyResource geographicTaxonomies; + if (institutionToNotify.getSubUnitType() != null) { + switch (Objects.requireNonNull(institutionToNotify.getSubUnitType())) { + case "UO" -> { + UOResource organizationUnit = proxyRegistryUoApi.findByUnicodeUsingGET1(institutionToNotify.getSubUnitCode(), null); + institutionToNotify.setIstatCode(organizationUnit.getCodiceComuneISTAT()); + geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(organizationUnit.getCodiceComuneISTAT()); + } + case "AOO" -> { + AOOResource homogeneousOrganizationalArea = proxyRegistryAooApi.findByUnicodeUsingGET(institutionToNotify.getSubUnitCode(), null); + institutionToNotify.setIstatCode(homogeneousOrganizationalArea.getCodiceComuneISTAT()); + geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(homogeneousOrganizationalArea.getCodiceComuneISTAT()); + } + default -> { + InstitutionResource proxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institutionToNotify.getTaxCode(), null, null); + institutionToNotify.setIstatCode(proxyInfo.getIstatCode()); + geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(proxyInfo.getIstatCode()); } - } else { - InstitutionResource proxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institutionToNotify.getTaxCode(), null, null); - institutionToNotify.setIstatCode(proxyInfo.getIstatCode()); - geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(proxyInfo.getIstatCode()); } + } else { + InstitutionResource proxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institutionToNotify.getTaxCode(), null, null); + institutionToNotify.setIstatCode(proxyInfo.getIstatCode()); + geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(proxyInfo.getIstatCode()); + } - if (geographicTaxonomies != null) { - institutionToNotify.setCounty(geographicTaxonomies.getProvinceAbbreviation()); - institutionToNotify.setCountry(geographicTaxonomies.getCountryAbbreviation()); - institutionToNotify.setCity(geographicTaxonomies.getDesc().replace(DESCRIPTION_TO_REPLACE_REGEX, "")); - } - } catch (Exception e) { - log.warn("Error while searching institution {} on IPA, {} ", institutionToNotify.getDescription(), e.getMessage()); - institutionToNotify.setIstatCode(null); + if (geographicTaxonomies != null) { + institutionToNotify.setCounty(geographicTaxonomies.getProvinceAbbreviation()); + institutionToNotify.setCountry(geographicTaxonomies.getCountryAbbreviation()); + institutionToNotify.setCity(geographicTaxonomies.getDesc().replace(DESCRIPTION_TO_REPLACE_REGEX, "")); } } @Override diff --git a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/NotificationEventServiceDefaultTest.java b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/NotificationEventServiceDefaultTest.java index 4e1dd424f..817a474c5 100644 --- a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/NotificationEventServiceDefaultTest.java +++ b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/service/NotificationEventServiceDefaultTest.java @@ -160,7 +160,7 @@ void sendMessageWithError() { .when(eventHubRestClient).sendMessage(anyString(), anyString()); ExecutionContext context = mock(ExecutionContext.class); doReturn(Logger.getGlobal()).when(context).getLogger(); - assertDoesNotThrow(() -> messageServiceDefault.send(context, onboarding, QueueEvent.ADD)); + assertThrows(NotificationException.class, () -> messageServiceDefault.send(context, onboarding, QueueEvent.ADD)); verify(eventHubRestClient, times(1)) .sendMessage(anyString(), anyString()); } diff --git a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/utils/SapNotificationBuilderTest.java b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/utils/SapNotificationBuilderTest.java index 2c499aa82..70bbba934 100644 --- a/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/utils/SapNotificationBuilderTest.java +++ b/apps/onboarding-functions/src/test/java/it/pagopa/selfcare/onboarding/utils/SapNotificationBuilderTest.java @@ -263,21 +263,22 @@ void toNotificationToSendForEcWhenPartyRegistryThrowsException() { when(registryProxyInstitutionsApi.findInstitutionUsingGET(any(), any(), any())) .thenThrow(new RuntimeException("Error")); - NotificationToSend notification = sapNotificationBuilder.buildNotificationToSend(onboarding, token, institution, QueueEvent.ADD); - - assertNotNull(notification); - assertNull(notification.getClosedAt()); - assertEquals("ACTIVE", notification.getState()); - assertNotEquals(onboarding.getId(), notification.getId()); - assertEquals(onboarding.getActivatedAt(), notification.getCreatedAt().toLocalDateTime()); - assertEquals(onboarding.getActivatedAt(), notification.getUpdatedAt().toLocalDateTime()); - assertNull(notification.getBilling().getTaxCodeInvoicing()); - assertNull(notification.getBilling().isPublicServices()); - assertNull(notification.getNotificationType()); - assertEquals(NotificationType.ADD_INSTITUTE, notification.getType()); - assertNull(notification.getBilling().getTaxCodeInvoicing()); - assertEquals("taxCodeInvoicing", notification.getInstitution().getTaxCode()); - assertNull(notification.getInstitution().getIstatCode()); +// NotificationToSend notification = sapNotificationBuilder.buildNotificationToSend(onboarding, token, institution, QueueEvent.ADD); + assertThrows(RuntimeException.class, () -> sapNotificationBuilder.buildNotificationToSend(onboarding, token, institution, QueueEvent.ADD)); + +// assertNotNull(notification); +// assertNull(notification.getClosedAt()); +// assertEquals("ACTIVE", notification.getState()); +// assertNotEquals(onboarding.getId(), notification.getId()); +// assertEquals(onboarding.getActivatedAt(), notification.getCreatedAt().toLocalDateTime()); +// assertEquals(onboarding.getActivatedAt(), notification.getUpdatedAt().toLocalDateTime()); +// assertNull(notification.getBilling().getTaxCodeInvoicing()); +// assertNull(notification.getBilling().isPublicServices()); +// assertNull(notification.getNotificationType()); +// assertEquals(NotificationType.ADD_INSTITUTE, notification.getType()); +// assertNull(notification.getBilling().getTaxCodeInvoicing()); +// assertEquals("taxCodeInvoicing", notification.getInstitution().getTaxCode()); +// assertNull(notification.getInstitution().getIstatCode()); } @Test