diff --git a/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/ResponseWithNotification.java b/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/NotificationDataResponse.java similarity index 63% rename from nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/ResponseWithNotification.java rename to nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/NotificationDataResponse.java index 51ec64a3a..e5dcc4c1c 100644 --- a/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/ResponseWithNotification.java +++ b/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/NotificationDataResponse.java @@ -1,7 +1,6 @@ package net.croz.nrich.notification.api.response; import lombok.Getter; -import lombok.RequiredArgsConstructor; import net.croz.nrich.notification.api.model.Notification; /** @@ -9,18 +8,16 @@ * * @param type of response data */ -@RequiredArgsConstructor @Getter -public class ResponseWithNotification { +public class NotificationDataResponse extends NotificationResponse { /** * Response data */ private final T data; - /** - * Notification - */ - private final Notification notification; - + public NotificationDataResponse(Notification notification, T data) { + super(notification); + this.data = data; + } } diff --git a/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/NotificationResponse.java b/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/NotificationResponse.java new file mode 100644 index 000000000..d78a4068a --- /dev/null +++ b/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/response/NotificationResponse.java @@ -0,0 +1,19 @@ +package net.croz.nrich.notification.api.response; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import net.croz.nrich.notification.api.model.Notification; + +/** + * Response that holds notification. + */ +@RequiredArgsConstructor +@Getter +public class NotificationResponse { + + /** + * Notification + */ + private final Notification notification; + +} diff --git a/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/service/BaseNotificationResponseService.java b/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/service/BaseNotificationResponseService.java new file mode 100644 index 000000000..58a842b26 --- /dev/null +++ b/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/service/BaseNotificationResponseService.java @@ -0,0 +1,84 @@ +package net.croz.nrich.notification.api.service; + +import net.croz.nrich.notification.api.model.AdditionalNotificationData; +import net.croz.nrich.notification.api.response.NotificationDataResponse; +import org.springframework.validation.Errors; + +import javax.validation.ConstraintViolationException; + + +/** + * Helper service for creation of response with notification. + * + * @param Type of response with notification i.e. {@link NotificationDataResponse} + */ +public interface BaseNotificationResponseService { + + /** + * Returns response with {@link net.croz.nrich.notification.api.model.ValidationFailureNotification} instance. + * + * @param errors Springs {@link Errors} that will be used to resolve validation notification messages. + * @param validationFailedOwningType class on which validation errors were found + * @param additionalNotificationData additional notification data to add to notification + * @return response with validation failure notification + */ + T responseWithValidationFailureNotification(Errors errors, Class validationFailedOwningType, AdditionalNotificationData additionalNotificationData); + + /** + * Returns response with {@link net.croz.nrich.notification.api.model.ValidationFailureNotification} instance. + * + * @param exception validation exception that will be used to resolve validation notification messages. + * @param additionalNotificationData additional notification data to add to notification + * @return response with validation failure notification + */ + T responseWithValidationFailureNotification(ConstraintViolationException exception, AdditionalNotificationData additionalNotificationData); + + /** + * Returns response with {@link net.croz.nrich.notification.api.model.Notification} instance. + * + * @param throwable exception for which to resolve notification + * @param additionalNotificationData additional notification data to add to notification + * @param exceptionMessageArgumentList optional exception argument list, will be used when resolving exception message + * @return response with notification + */ + T responseWithExceptionNotification(Throwable throwable, AdditionalNotificationData additionalNotificationData, Object... exceptionMessageArgumentList); + + /** + * Returns response with {@link net.croz.nrich.notification.api.model.Notification} instance. + * + * @param actionName name of the action for which to resolve notification + * @param additionalNotificationData additional notification data to add to notification + * @return response with notification + */ + T responseWithNotification(String actionName, AdditionalNotificationData additionalNotificationData); + + /** + * Returns response with {@link net.croz.nrich.notification.api.model.Notification} instance. + * + * @param additionalNotificationData additional notification data to add to notification + * @return response with notification + */ + T responseWithNotificationActionResolvedFromRequest(AdditionalNotificationData additionalNotificationData); + + NotificationResolverService notificationResolverService(); + + default T responseWithValidationFailureNotification(Errors errors, Class validationFailedOwningType) { + return responseWithValidationFailureNotification(errors, validationFailedOwningType, AdditionalNotificationData.empty()); + } + + default T responseWithValidationFailureNotification(ConstraintViolationException exception) { + return responseWithValidationFailureNotification(exception, AdditionalNotificationData.empty()); + } + + default T responseWithExceptionNotification(Throwable throwable, Object... additionalMessageArgumentList) { + return responseWithExceptionNotification(throwable, AdditionalNotificationData.empty(), additionalMessageArgumentList); + } + + default T responseWithNotificationActionResolvedFromRequest() { + return responseWithNotificationActionResolvedFromRequest(AdditionalNotificationData.empty()); + } + + default T responseWithNotification(String actionName) { + return responseWithNotification(actionName, AdditionalNotificationData.empty()); + } +} diff --git a/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/service/NotificationResponseService.java b/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/service/NotificationResponseService.java index 2808f7f39..9a888762b 100644 --- a/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/service/NotificationResponseService.java +++ b/nrich-notification-api/src/main/java/net/croz/nrich/notification/api/service/NotificationResponseService.java @@ -1,47 +1,10 @@ package net.croz.nrich.notification.api.service; import net.croz.nrich.notification.api.model.AdditionalNotificationData; -import org.springframework.validation.Errors; +import net.croz.nrich.notification.api.response.NotificationDataResponse; +import net.croz.nrich.notification.api.response.NotificationResponse; -import javax.validation.ConstraintViolationException; - -// TODO maybe reduce number of methods, this seems a bit complicated? - -/** - * Helper service that helps creation response with notification. - * - * @param Type of response with notification i.e. {@link net.croz.nrich.notification.api.response.ResponseWithNotification} - */ -public interface NotificationResponseService { - - /** - * Returns response with {@link net.croz.nrich.notification.api.model.ValidationFailureNotification} instance. - * - * @param errors Springs {@link Errors} that will be used to resolve validation notification messages. - * @param validationFailedOwningType class on which validation errors were found - * @param additionalNotificationData additional notification data to add to notification - * @return response with validation failure notification - */ - T responseWithValidationFailureNotification(Errors errors, Class validationFailedOwningType, AdditionalNotificationData additionalNotificationData); - - /** - * Returns response with {@link net.croz.nrich.notification.api.model.ValidationFailureNotification} instance. - * - * @param exception validation exception that will be used to resolve validation notification messages. - * @param additionalNotificationData additional notification data to add to notification - * @return response with validation failure notification - */ - T responseWithValidationFailureNotification(ConstraintViolationException exception, AdditionalNotificationData additionalNotificationData); - - /** - * Returns response with {@link net.croz.nrich.notification.api.model.Notification} instance. - * - * @param throwable exception for which to resolve notification - * @param additionalNotificationData additional notification data to add to notification - * @param exceptionMessageArgumentList optional exception argument list, will be used when resolving exception message - * @return response with notification - */ - T responseWithExceptionNotification(Throwable throwable, AdditionalNotificationData additionalNotificationData, Object... exceptionMessageArgumentList); +public interface NotificationResponseService extends BaseNotificationResponseService { /** * Returns response with {@link net.croz.nrich.notification.api.model.Notification} instance. @@ -51,7 +14,7 @@ public interface NotificationResponseService { * @param type of data * @return response with notification */ - T responseWithNotificationActionResolvedFromRequest(D data, AdditionalNotificationData additionalNotificationData); + NotificationDataResponse responseWithNotificationActionResolvedFromRequest(D data, AdditionalNotificationData additionalNotificationData); /** * Returns response with {@link net.croz.nrich.notification.api.model.Notification} instance. @@ -62,35 +25,13 @@ public interface NotificationResponseService { * @param type of data * @return response with notification */ - T responseWithNotification(D data, String actionName, AdditionalNotificationData additionalNotificationData); - - NotificationResolverService notificationResolverService(); - - default T responseWithValidationFailureNotification(Errors errors, Class validationFailedOwningType) { - return responseWithValidationFailureNotification(errors, validationFailedOwningType, AdditionalNotificationData.empty()); - } - - default T responseWithValidationFailureNotification(ConstraintViolationException exception) { - return responseWithValidationFailureNotification(exception, AdditionalNotificationData.empty()); - } - - default T responseWithExceptionNotification(Throwable throwable, Object... additionalMessageArgumentList) { - return responseWithExceptionNotification(throwable, AdditionalNotificationData.empty(), additionalMessageArgumentList); - } + NotificationDataResponse responseWithNotification(D data, String actionName, AdditionalNotificationData additionalNotificationData); - default T responseWithNotificationActionResolvedFromRequest(D data) { + default NotificationDataResponse responseWithNotificationActionResolvedFromRequest(D data) { return responseWithNotificationActionResolvedFromRequest(data, AdditionalNotificationData.empty()); } - default T responseWithNotification(D data, String actionName) { + default NotificationDataResponse responseWithNotification(D data, String actionName) { return responseWithNotification(data, actionName, AdditionalNotificationData.empty()); } - - default T responseWithNotificationActionResolvedFromRequest() { - return responseWithNotificationActionResolvedFromRequest(null, AdditionalNotificationData.empty()); - } - - default T responseWithNotification(String actionName) { - return responseWithNotification(null, actionName, AdditionalNotificationData.empty()); - } } diff --git a/nrich-notification-spring-boot-starter/src/main/java/net/croz/nrich/notification/starter/configuration/NrichNotificationAutoConfiguration.java b/nrich-notification-spring-boot-starter/src/main/java/net/croz/nrich/notification/starter/configuration/NrichNotificationAutoConfiguration.java index 993338bb2..a69f26a8b 100644 --- a/nrich-notification-spring-boot-starter/src/main/java/net/croz/nrich/notification/starter/configuration/NrichNotificationAutoConfiguration.java +++ b/nrich-notification-spring-boot-starter/src/main/java/net/croz/nrich/notification/starter/configuration/NrichNotificationAutoConfiguration.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; import net.croz.nrich.notification.api.service.NotificationMessageResolverService; import net.croz.nrich.notification.api.service.NotificationResolverService; +import net.croz.nrich.notification.api.service.BaseNotificationResponseService; import net.croz.nrich.notification.api.service.NotificationResponseService; import net.croz.nrich.notification.service.ConstraintConversionService; import net.croz.nrich.notification.service.DefaultConstraintConversionService; @@ -42,9 +43,9 @@ public NotificationResolverService notificationResolverService(NotificationMessa } @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) - @ConditionalOnMissingBean(NotificationResponseService.class) + @ConditionalOnMissingBean(BaseNotificationResponseService.class) @Bean - public WebMvcNotificationResponseService notificationResponseService(NotificationResolverService notificationResolverService) { + public NotificationResponseService notificationResponseService(NotificationResolverService notificationResolverService) { return new WebMvcNotificationResponseService(notificationResolverService); } diff --git a/nrich-notification-spring-boot-starter/src/test/java/net/croz/nrich/notification/starter/NrichNotificationAutoConfigurationTest.java b/nrich-notification-spring-boot-starter/src/test/java/net/croz/nrich/notification/starter/NrichNotificationAutoConfigurationTest.java index fe126ca13..f641afeec 100644 --- a/nrich-notification-spring-boot-starter/src/test/java/net/croz/nrich/notification/starter/NrichNotificationAutoConfigurationTest.java +++ b/nrich-notification-spring-boot-starter/src/test/java/net/croz/nrich/notification/starter/NrichNotificationAutoConfigurationTest.java @@ -1,7 +1,7 @@ package net.croz.nrich.notification.starter; import net.croz.nrich.notification.api.service.NotificationResolverService; -import net.croz.nrich.notification.api.service.NotificationResponseService; +import net.croz.nrich.notification.api.service.BaseNotificationResponseService; import net.croz.nrich.notification.service.ConstraintConversionService; import net.croz.nrich.notification.starter.configuration.NrichNotificationAutoConfiguration; import org.junit.jupiter.api.Test; @@ -27,7 +27,7 @@ void shouldConfigureDefaultConfiguration() { contextRunner.run(context -> { assertThat(context).hasSingleBean(ConstraintConversionService.class); assertThat(context).hasSingleBean(NotificationResolverService.class); - assertThat(context).doesNotHaveBean(NotificationResponseService.class); + assertThat(context).doesNotHaveBean(BaseNotificationResponseService.class); assertThat(context).hasSingleBean(NrichNotificationAutoConfiguration.NotificationMessageSourceRegistrar.class); }); } @@ -44,7 +44,7 @@ void shouldNotRegisterMessagesWhenExplicitlyDisalbed() { void shouldIncludeNotificationResponseServiceWhenRunningInWebEnvironment() { // expect webContextRunner.run(context -> - assertThat(context).hasSingleBean(NotificationResponseService.class) + assertThat(context).hasSingleBean(BaseNotificationResponseService.class) ); } diff --git a/nrich-notification/README.md b/nrich-notification/README.md index 75c917f12..89c1eb9d5 100644 --- a/nrich-notification/README.md +++ b/nrich-notification/README.md @@ -30,7 +30,7 @@ To be able to use this library following configuration is required: } @Bean - public NotificationResponseService notificationResponseService(NotificationResolverService notificationResolverService) { + public NotificationResponseService notificationResponseService(NotificationResolverService notificationResolverService) { return new WebMvcNotificationResponseService(notificationResolverService); } @@ -62,7 +62,7 @@ advice would look something like this: @RequiredArgsConstructor public class NotificationErrorHandlingRestControllerAdvice { - private NotificationResponseService notificationResponseService; + private NotificationResponseService notificationResponseService; @ExceptionHandler(Exception.class) public ResponseEntity handleException(Exception exception, HttpServletRequest request) { @@ -93,7 +93,7 @@ Users can also use `NotificationResponseService` to return notifications with re @RequiredArgsConstructor public class NotificationTestController { - private NotificationResponseService> notificationResponseService; + private NotificationResponseService notificationResponseService; private ExampleService exampleService; diff --git a/nrich-notification/src/main/java/net/croz/nrich/notification/service/WebMvcNotificationResponseService.java b/nrich-notification/src/main/java/net/croz/nrich/notification/service/WebMvcNotificationResponseService.java index 37616c660..7ba97c5b8 100644 --- a/nrich-notification/src/main/java/net/croz/nrich/notification/service/WebMvcNotificationResponseService.java +++ b/nrich-notification/src/main/java/net/croz/nrich/notification/service/WebMvcNotificationResponseService.java @@ -3,7 +3,8 @@ import lombok.RequiredArgsConstructor; import net.croz.nrich.notification.api.model.AdditionalNotificationData; import net.croz.nrich.notification.api.model.Notification; -import net.croz.nrich.notification.api.response.ResponseWithNotification; +import net.croz.nrich.notification.api.response.NotificationDataResponse; +import net.croz.nrich.notification.api.response.NotificationResponse; import net.croz.nrich.notification.api.service.NotificationResolverService; import net.croz.nrich.notification.api.service.NotificationResponseService; import net.croz.nrich.notification.constant.NotificationConstants; @@ -17,43 +18,57 @@ import java.util.Objects; @RequiredArgsConstructor -public class WebMvcNotificationResponseService implements NotificationResponseService> { +public class WebMvcNotificationResponseService implements NotificationResponseService { private final NotificationResolverService notificationResolverService; @Override - public ResponseWithNotification responseWithValidationFailureNotification(Errors errors, Class validationFailedOwningType, AdditionalNotificationData additionalNotificationData) { + public NotificationResponse responseWithValidationFailureNotification(Errors errors, Class validationFailedOwningType, AdditionalNotificationData additionalNotificationData) { Notification notification = notificationResolverService.createNotificationForValidationFailure(errors, validationFailedOwningType, additionalNotificationData); - return new ResponseWithNotification<>(null, notification); + return new NotificationResponse(notification); } @Override - public ResponseWithNotification responseWithValidationFailureNotification(ConstraintViolationException exception, AdditionalNotificationData additionalNotificationData) { + public NotificationResponse responseWithValidationFailureNotification(ConstraintViolationException exception, AdditionalNotificationData additionalNotificationData) { Notification notification = notificationResolverService.createNotificationForValidationFailure(exception, additionalNotificationData); - return new ResponseWithNotification<>(null, notification); + return new NotificationResponse(notification); } @Override - public ResponseWithNotification responseWithExceptionNotification(Throwable throwable, AdditionalNotificationData additionalNotificationData, Object... exceptionMessageArgumentList) { + public NotificationResponse responseWithExceptionNotification(Throwable throwable, AdditionalNotificationData additionalNotificationData, Object... exceptionMessageArgumentList) { Notification notification = notificationResolverService.createNotificationForException(throwable, additionalNotificationData, exceptionMessageArgumentList); - return new ResponseWithNotification<>(null, notification); + return new NotificationResponse(notification); } @Override - public ResponseWithNotification responseWithNotificationActionResolvedFromRequest(D data, AdditionalNotificationData additionalNotificationData) { + public NotificationResponse responseWithNotificationActionResolvedFromRequest(AdditionalNotificationData additionalNotificationData) { + String actionName = extractActionNameFromCurrentRequest(); + + return responseWithNotification(actionName, additionalNotificationData); + } + + @Override + public NotificationResponse responseWithNotification(String actionName, AdditionalNotificationData additionalNotificationData) { + Notification notification = notificationResolverService.createNotificationForAction(actionName, additionalNotificationData); + + return new NotificationResponse(notification); + } + + @Override + public NotificationDataResponse responseWithNotificationActionResolvedFromRequest(D data, AdditionalNotificationData additionalNotificationData) { String actionName = extractActionNameFromCurrentRequest(); return responseWithNotification(data, actionName, additionalNotificationData); } @Override - public ResponseWithNotification responseWithNotification(D data, String actionName, AdditionalNotificationData additionalNotificationData) { + public NotificationDataResponse responseWithNotification(D data, String actionName, AdditionalNotificationData additionalNotificationData) { Notification notification = notificationResolverService.createNotificationForAction(actionName, additionalNotificationData); - return new ResponseWithNotification<>(data, notification); + return new NotificationDataResponse<>(notification, data); } @Override diff --git a/nrich-notification/src/test/java/net/croz/nrich/notification/service/WebMvcNotificationResponseServiceTest.java b/nrich-notification/src/test/java/net/croz/nrich/notification/service/WebMvcNotificationResponseServiceTest.java index d88173d08..54adae5d6 100644 --- a/nrich-notification/src/test/java/net/croz/nrich/notification/service/WebMvcNotificationResponseServiceTest.java +++ b/nrich-notification/src/test/java/net/croz/nrich/notification/service/WebMvcNotificationResponseServiceTest.java @@ -4,7 +4,8 @@ import net.croz.nrich.notification.api.model.Notification; import net.croz.nrich.notification.api.model.NotificationSeverity; import net.croz.nrich.notification.api.model.ValidationFailureNotification; -import net.croz.nrich.notification.api.response.ResponseWithNotification; +import net.croz.nrich.notification.api.response.NotificationDataResponse; +import net.croz.nrich.notification.api.response.NotificationResponse; import net.croz.nrich.notification.api.service.NotificationResolverService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -58,37 +59,37 @@ void shouldReturnNotificationResolverService() { @Test void shouldCreateResponseForValidationFailure() { // when - ResponseWithNotification responseWithNotification = notificationResponseService.responseWithValidationFailureNotification(mock(Errors.class), Object.class); + NotificationResponse notificationResponse = notificationResponseService.responseWithValidationFailureNotification(mock(Errors.class), Object.class); // then - assertThat(responseWithNotification).isNotNull(); - assertThat(responseWithNotification.getNotification()).isNotNull(); - assertThat(responseWithNotification.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); - assertThat(responseWithNotification.getNotification().getContent()).isEqualTo("Validation failed 1"); + assertThat(notificationResponse).isNotNull(); + assertThat(notificationResponse.getNotification()).isNotNull(); + assertThat(notificationResponse.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); + assertThat(notificationResponse.getNotification().getContent()).isEqualTo("Validation failed 1"); } @Test void shouldCreateResponseForValidationFailureForConstraintViolation() { // when - ResponseWithNotification responseWithNotification = notificationResponseService.responseWithValidationFailureNotification(mock(ConstraintViolationException.class)); + NotificationResponse notificationResponse = notificationResponseService.responseWithValidationFailureNotification(mock(ConstraintViolationException.class)); // then - assertThat(responseWithNotification).isNotNull(); - assertThat(responseWithNotification.getNotification()).isNotNull(); - assertThat(responseWithNotification.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); - assertThat(responseWithNotification.getNotification().getContent()).isEqualTo("Validation failed 2"); + assertThat(notificationResponse).isNotNull(); + assertThat(notificationResponse.getNotification()).isNotNull(); + assertThat(notificationResponse.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); + assertThat(notificationResponse.getNotification().getContent()).isEqualTo("Validation failed 2"); } @Test void shouldCreateResponseForException() { // when - ResponseWithNotification responseWithNotification = notificationResponseService.responseWithExceptionNotification(new Exception()); + NotificationResponse notificationResponse = notificationResponseService.responseWithExceptionNotification(new Exception()); // then - assertThat(responseWithNotification).isNotNull(); - assertThat(responseWithNotification.getNotification()).isNotNull(); - assertThat(responseWithNotification.getNotification().getSeverity()).isEqualTo(NotificationSeverity.ERROR); - assertThat(responseWithNotification.getNotification().getContent()).isEqualTo("Exception"); + assertThat(notificationResponse).isNotNull(); + assertThat(notificationResponse.getNotification()).isNotNull(); + assertThat(notificationResponse.getNotification().getSeverity()).isEqualTo(NotificationSeverity.ERROR); + assertThat(notificationResponse.getNotification().getContent()).isEqualTo("Exception"); } @Test @@ -99,14 +100,14 @@ void shouldCreateNotificationFromCurrentRequest() { RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest)); // when - ResponseWithNotification responseWithNotification = notificationResponseService.responseWithNotificationActionResolvedFromRequest(data); + NotificationDataResponse notificationDataResponse = notificationResponseService.responseWithNotificationActionResolvedFromRequest(data); // then - assertThat(responseWithNotification).isNotNull(); - assertThat(responseWithNotification.getData()).isEqualTo(data); - assertThat(responseWithNotification.getNotification()).isNotNull(); - assertThat(responseWithNotification.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); - assertThat(responseWithNotification.getNotification().getContent()).isEqualTo("Action success"); + assertThat(notificationDataResponse).isNotNull(); + assertThat(notificationDataResponse.getData()).isEqualTo(data); + assertThat(notificationDataResponse.getNotification()).isNotNull(); + assertThat(notificationDataResponse.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); + assertThat(notificationDataResponse.getNotification().getContent()).isEqualTo("Action success"); } @Test @@ -115,14 +116,14 @@ void shouldCreateNotificationFromActionName() { String data = "data"; // when - ResponseWithNotification responseWithNotification = notificationResponseService.responseWithNotification(data, "actionPrefix.actionSuffix"); + NotificationDataResponse notificationDataResponse = notificationResponseService.responseWithNotification(data, "actionPrefix.actionSuffix"); // then - assertThat(responseWithNotification).isNotNull(); - assertThat(responseWithNotification.getData()).isEqualTo(data); - assertThat(responseWithNotification.getNotification()).isNotNull(); - assertThat(responseWithNotification.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); - assertThat(responseWithNotification.getNotification().getContent()).isEqualTo("Action success"); + assertThat(notificationDataResponse).isNotNull(); + assertThat(notificationDataResponse.getData()).isEqualTo(data); + assertThat(notificationDataResponse.getNotification()).isNotNull(); + assertThat(notificationDataResponse.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); + assertThat(notificationDataResponse.getNotification().getContent()).isEqualTo("Action success"); } @Test @@ -132,24 +133,24 @@ void shouldCreateNotificationFromCurrentRequestWithoutData() { RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockHttpServletRequest)); // when - ResponseWithNotification responseWithNotification = notificationResponseService.responseWithNotificationActionResolvedFromRequest(); + NotificationResponse notificationDataResponse = notificationResponseService.responseWithNotificationActionResolvedFromRequest(); // then - assertThat(responseWithNotification).isNotNull(); - assertThat(responseWithNotification.getNotification()).isNotNull(); - assertThat(responseWithNotification.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); - assertThat(responseWithNotification.getNotification().getContent()).isEqualTo("Action success"); + assertThat(notificationDataResponse).isNotNull(); + assertThat(notificationDataResponse.getNotification()).isNotNull(); + assertThat(notificationDataResponse.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); + assertThat(notificationDataResponse.getNotification().getContent()).isEqualTo("Action success"); } @Test void shouldCreateNotificationFromActionNameWithoutData() { // when - ResponseWithNotification responseWithNotification = notificationResponseService.responseWithNotification("actionPrefix.actionSuffix"); + NotificationResponse notificationDataResponse = notificationResponseService.responseWithNotification("actionPrefix.actionSuffix"); // then - assertThat(responseWithNotification).isNotNull(); - assertThat(responseWithNotification.getNotification()).isNotNull(); - assertThat(responseWithNotification.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); - assertThat(responseWithNotification.getNotification().getContent()).isEqualTo("Action success"); + assertThat(notificationDataResponse).isNotNull(); + assertThat(notificationDataResponse.getNotification()).isNotNull(); + assertThat(notificationDataResponse.getNotification().getSeverity()).isEqualTo(NotificationSeverity.INFO); + assertThat(notificationDataResponse.getNotification().getContent()).isEqualTo("Action success"); } } diff --git a/nrich-webmvc-spring-boot-starter/src/main/java/net/croz/nrich/webmvc/starter/configuration/NrichWebMvcAutoConfiguration.java b/nrich-webmvc-spring-boot-starter/src/main/java/net/croz/nrich/webmvc/starter/configuration/NrichWebMvcAutoConfiguration.java index dfe403bd4..233f93af9 100644 --- a/nrich-webmvc-spring-boot-starter/src/main/java/net/croz/nrich/webmvc/starter/configuration/NrichWebMvcAutoConfiguration.java +++ b/nrich-webmvc-spring-boot-starter/src/main/java/net/croz/nrich/webmvc/starter/configuration/NrichWebMvcAutoConfiguration.java @@ -1,7 +1,7 @@ package net.croz.nrich.webmvc.starter.configuration; import net.croz.nrich.logging.api.service.LoggingService; -import net.croz.nrich.notification.api.service.NotificationResponseService; +import net.croz.nrich.notification.api.service.BaseNotificationResponseService; import net.croz.nrich.springboot.condition.ConditionalOnPropertyNotEmpty; import net.croz.nrich.webmvc.advice.ControllerEditorRegistrationAdvice; import net.croz.nrich.webmvc.advice.NotificationErrorHandlingRestControllerAdvice; @@ -22,7 +22,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -@ConditionalOnBean({ NotificationResponseService.class, LoggingService.class }) +@ConditionalOnBean({ BaseNotificationResponseService.class, LoggingService.class }) @EnableConfigurationProperties(NrichWebMvcProperties.class) @Configuration(proxyBeanMethods = false) public class NrichWebMvcAutoConfiguration { @@ -54,7 +54,7 @@ public ExceptionHttpStatusResolverService exceptionHttpStatusResolverService(Mes @ConditionalOnProperty(name = "nrich.webmvc.controller-advice-enabled", havingValue = "true", matchIfMissing = true) @Bean - public NotificationErrorHandlingRestControllerAdvice notificationRestControllerAdvice(NrichWebMvcProperties webMvcProperties, NotificationResponseService notificationResponseService, + public NotificationErrorHandlingRestControllerAdvice notificationRestControllerAdvice(NrichWebMvcProperties webMvcProperties, BaseNotificationResponseService notificationResponseService, LoggingService loggingService, ExceptionHttpStatusResolverService exceptionHttpStatusResolverService, @Autowired(required = false) ExceptionAuxiliaryDataResolverService exceptionAuxiliaryDataResolverService) { return new NotificationErrorHandlingRestControllerAdvice( diff --git a/nrich-webmvc-spring-boot-starter/src/test/java/net/croz/nrich/webmvc/starter/configuration/stub/NotificationResponseTestService.java b/nrich-webmvc-spring-boot-starter/src/test/java/net/croz/nrich/webmvc/starter/configuration/stub/NotificationResponseTestService.java index cfe90edfa..80224c666 100644 --- a/nrich-webmvc-spring-boot-starter/src/test/java/net/croz/nrich/webmvc/starter/configuration/stub/NotificationResponseTestService.java +++ b/nrich-webmvc-spring-boot-starter/src/test/java/net/croz/nrich/webmvc/starter/configuration/stub/NotificationResponseTestService.java @@ -2,12 +2,12 @@ import net.croz.nrich.notification.api.model.AdditionalNotificationData; import net.croz.nrich.notification.api.service.NotificationResolverService; -import net.croz.nrich.notification.api.service.NotificationResponseService; +import net.croz.nrich.notification.api.service.BaseNotificationResponseService; import org.springframework.validation.Errors; import javax.validation.ConstraintViolationException; -public class NotificationResponseTestService implements NotificationResponseService { +public class NotificationResponseTestService implements BaseNotificationResponseService { @Override public Object responseWithValidationFailureNotification(Errors errors, Class validationFailedOwningType, AdditionalNotificationData additionalNotificationData) { @@ -25,12 +25,12 @@ public Object responseWithExceptionNotification(Throwable throwable, AdditionalN } @Override - public Object responseWithNotificationActionResolvedFromRequest(D data, AdditionalNotificationData additionalNotificationData) { + public Object responseWithNotification(String actionName, AdditionalNotificationData additionalNotificationData) { return null; } @Override - public Object responseWithNotification(D data, String actionName, AdditionalNotificationData additionalNotificationData) { + public Object responseWithNotificationActionResolvedFromRequest(AdditionalNotificationData additionalNotificationData) { return null; } diff --git a/nrich-webmvc/README.md b/nrich-webmvc/README.md index dcbf0a264..c24b19fd9 100644 --- a/nrich-webmvc/README.md +++ b/nrich-webmvc/README.md @@ -37,7 +37,7 @@ nrich-webmvc depends on nrich-logging and nrich-notification libraries but users } @Bean - public NotificationErrorHandlingRestControllerAdvice notificationErrorHandlingRestControllerAdvice(NotificationResponseService notificationResponseService, LoggingService loggingService, ExceptionAuxiliaryDataResolverService exceptionAuxiliaryDataResolverService, ExceptionHttpStatusResolverService exceptionHttpStatusResolverService) { + public NotificationErrorHandlingRestControllerAdvice notificationErrorHandlingRestControllerAdvice(BaseNotificationResponseService notificationResponseService, LoggingService loggingService, ExceptionAuxiliaryDataResolverService exceptionAuxiliaryDataResolverService, ExceptionHttpStatusResolverService exceptionHttpStatusResolverService) { return new NotificationErrorHandlingRestControllerAdvice(Collections.singletonList(ExecutionException.class.getName()), Collections.singletonList("uuid"), notificationResponseService, loggingService, exceptionAuxiliaryDataResolverService, exceptionHttpStatusResolverService); } diff --git a/nrich-webmvc/src/main/java/net/croz/nrich/webmvc/advice/NotificationErrorHandlingRestControllerAdvice.java b/nrich-webmvc/src/main/java/net/croz/nrich/webmvc/advice/NotificationErrorHandlingRestControllerAdvice.java index b0bae5edb..f04ffb0db 100644 --- a/nrich-webmvc/src/main/java/net/croz/nrich/webmvc/advice/NotificationErrorHandlingRestControllerAdvice.java +++ b/nrich-webmvc/src/main/java/net/croz/nrich/webmvc/advice/NotificationErrorHandlingRestControllerAdvice.java @@ -4,7 +4,7 @@ import net.croz.nrich.core.api.exception.ExceptionWithArguments; import net.croz.nrich.logging.api.service.LoggingService; import net.croz.nrich.notification.api.model.AdditionalNotificationData; -import net.croz.nrich.notification.api.service.NotificationResponseService; +import net.croz.nrich.notification.api.service.BaseNotificationResponseService; import net.croz.nrich.webmvc.api.service.ExceptionAuxiliaryDataResolverService; import net.croz.nrich.webmvc.api.service.ExceptionHttpStatusResolverService; import org.springframework.http.HttpStatus; @@ -30,7 +30,7 @@ public class NotificationErrorHandlingRestControllerAdvice { private final List exceptionAuxiliaryDataToIncludeInNotification; - private final NotificationResponseService notificationResponseService; + private final BaseNotificationResponseService notificationResponseService; private final LoggingService loggingService; diff --git a/nrich-webmvc/src/test/java/net/croz/nrich/webmvc/WebmvcTestConfiguration.java b/nrich-webmvc/src/test/java/net/croz/nrich/webmvc/WebmvcTestConfiguration.java index 9e8b1490f..be85647a4 100644 --- a/nrich-webmvc/src/test/java/net/croz/nrich/webmvc/WebmvcTestConfiguration.java +++ b/nrich-webmvc/src/test/java/net/croz/nrich/webmvc/WebmvcTestConfiguration.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import net.croz.nrich.logging.api.service.LoggingService; import net.croz.nrich.logging.service.Slf4jLoggingService; +import net.croz.nrich.notification.api.service.BaseNotificationResponseService; import net.croz.nrich.notification.api.service.NotificationMessageResolverService; import net.croz.nrich.notification.api.service.NotificationResolverService; import net.croz.nrich.notification.api.service.NotificationResponseService; @@ -81,7 +82,7 @@ public NotificationResolverService notificationResolverService(NotificationMessa } @Bean - public NotificationResponseService notificationResponseService(NotificationResolverService notificationResolverService) { + public NotificationResponseService notificationResponseService(NotificationResolverService notificationResolverService) { return new WebMvcNotificationResponseService(notificationResolverService); } @@ -111,7 +112,7 @@ public ExceptionHttpStatusResolverService exceptionHttpStatusResolverService(Mes } @Bean - public NotificationErrorHandlingRestControllerAdvice notificationErrorHandlingRestControllerAdvice(NotificationResponseService notificationResponseService, LoggingService loggingService, + public NotificationErrorHandlingRestControllerAdvice notificationErrorHandlingRestControllerAdvice(BaseNotificationResponseService notificationResponseService, LoggingService loggingService, ExceptionAuxiliaryDataResolverService exceptionAuxiliaryDataResolverService, ExceptionHttpStatusResolverService exceptionHttpStatusResolverService) { return new NotificationErrorHandlingRestControllerAdvice(