diff --git a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/constants/EventNotificationConstants.java b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/constants/EventNotificationConstants.java index 3fcbd192..12124024 100644 --- a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/constants/EventNotificationConstants.java +++ b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/constants/EventNotificationConstants.java @@ -53,6 +53,7 @@ public class EventNotificationConstants { public static final String REQUEST = "REQUEST"; //Error Constants + public static final String INVALID_REQUEST = "invalid_request"; public static final String EVENT_NOTIFICATION_CREATION_ERROR = "Error occurred while saving event " + "notifications in the database"; public static final String MISSING_REQ_PAYLOAD = "No request payload found"; @@ -112,4 +113,6 @@ public class EventNotificationConstants { "subscription in the database. "; public static final String EVENT_SUBSCRIPTION_NOT_FOUND = "Event subscription not found."; public static final String EVENT_SUBSCRIPTIONS_NOT_FOUND = "Event subscriptions not found for the given client id."; + public static final String ERROR_HANDLING_EVENT_SUBSCRIPTION = "Error occurred while handling the event " + + "subscription request"; } diff --git a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/dto/EventNotificationErrorDTO.java b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/dto/EventNotificationErrorDTO.java similarity index 91% rename from open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/dto/EventNotificationErrorDTO.java rename to open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/dto/EventNotificationErrorDTO.java index 9d92ca83..1121b211 100644 --- a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/dto/EventNotificationErrorDTO.java +++ b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/dto/EventNotificationErrorDTO.java @@ -16,7 +16,7 @@ * under the License. */ -package com.wso2.openbanking.accelerator.event.notifications.endpoint.dto; +package com.wso2.openbanking.accelerator.event.notifications.service.dto; import com.fasterxml.jackson.annotation.JsonProperty; @@ -29,7 +29,7 @@ public class EventNotificationErrorDTO { private String error; @JsonProperty("error_description") - public String getError_description() { + public String getErrorDescription() { return errorDescription; } diff --git a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/handler/DefaultEventSubscriptionServiceHandler.java b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/handler/DefaultEventSubscriptionServiceHandler.java index bafc1f5a..163f5dbb 100644 --- a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/handler/DefaultEventSubscriptionServiceHandler.java +++ b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/handler/DefaultEventSubscriptionServiceHandler.java @@ -28,6 +28,7 @@ import net.minidev.json.JSONObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.eclipse.jetty.http.HttpStatus; import java.util.ArrayList; import java.util.List; @@ -47,8 +48,8 @@ public void setEventSubscriptionService(EventSubscriptionService eventSubscripti /** * This method is used to create event subscriptions. * - * @param eventSubscriptionRequestDto - * @return EventSubscriptionResponse + * @param eventSubscriptionRequestDto Event Subscription DTO + * @return EventSubscriptionResponse Event Subscription Response */ public EventSubscriptionResponse createEventSubscription(EventSubscriptionDTO eventSubscriptionRequestDto) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -64,14 +65,15 @@ public EventSubscriptionResponse createEventSubscription(EventSubscriptionDTO ev try { EventSubscription createEventSubscriptionResponse = eventSubscriptionService. createEventSubscription(eventSubscription); - eventSubscriptionResponse.setStatus(EventNotificationConstants.CREATED); + eventSubscriptionResponse.setStatus(HttpStatus.CREATED_201); eventSubscriptionResponse. setResponseBody(mapSubscriptionModelToResponseJson(createEventSubscriptionResponse)); return eventSubscriptionResponse; } catch (OBEventNotificationException e) { log.error("Error occurred while creating event subscription", e); - eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR); - eventSubscriptionResponse.setErrorResponse("Error occurred while creating event subscription"); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } @@ -80,9 +82,10 @@ public EventSubscriptionResponse createEventSubscription(EventSubscriptionDTO ev /** * This method is used to retrieve a single event subscription. * - * @param clientId - * @param subscriptionId - * @return EventSubscriptionResponse + * @param clientId Client ID of the subscription created + * @param subscriptionId Subscription ID of the subscription created + * @return EventSubscriptionResponse Event Subscription Response containing subscription + * details for the given subscription ID */ public EventSubscriptionResponse getEventSubscription(String clientId, String subscriptionId) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -96,17 +99,19 @@ public EventSubscriptionResponse getEventSubscription(String clientId, String su try { EventSubscription eventSubscription = eventSubscriptionService. getEventSubscriptionBySubscriptionId(subscriptionId); - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK); + eventSubscriptionResponse.setStatus(HttpStatus.OK_200); eventSubscriptionResponse.setResponseBody(mapSubscriptionModelToResponseJson(eventSubscription)); return eventSubscriptionResponse; } catch (OBEventNotificationException e) { log.error("Error occurred while retrieving event subscription", e); if (e.getMessage().equals(EventNotificationConstants.EVENT_SUBSCRIPTION_NOT_FOUND)) { - eventSubscriptionResponse.setStatus(EventNotificationConstants.BAD_REQUEST); - eventSubscriptionResponse.setErrorResponse("Event subscription not found"); + eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); } else { - eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR); - eventSubscriptionResponse.setErrorResponse("Error occurred while retrieving event subscription"); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); } return eventSubscriptionResponse; } @@ -115,8 +120,8 @@ public EventSubscriptionResponse getEventSubscription(String clientId, String su /** * This method is used to retrieve all event subscriptions of a client. * - * @param clientId - * @return EventSubscriptionResponse + * @param clientId Client ID + * @return EventSubscriptionResponse Event Subscription Response containing all the subscriptions */ public EventSubscriptionResponse getAllEventSubscriptions(String clientId) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -134,13 +139,14 @@ public EventSubscriptionResponse getAllEventSubscriptions(String clientId) { for (EventSubscription eventSubscription : eventSubscriptionList) { eventSubscriptionResponseList.add(mapSubscriptionModelToResponseJson(eventSubscription)); } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK); + eventSubscriptionResponse.setStatus(HttpStatus.OK_200); eventSubscriptionResponse.setResponseBody(eventSubscriptionResponseList); return eventSubscriptionResponse; } catch (OBEventNotificationException e) { log.error("Error occurred while retrieving event subscriptions", e); - eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR); - eventSubscriptionResponse.setErrorResponse("Error occurred while retrieving event subscription."); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } } @@ -148,9 +154,10 @@ public EventSubscriptionResponse getAllEventSubscriptions(String clientId) { /** * This method is used to retrieve all event subscriptions by event type. * - * @param clientId - * @param eventType - * @return + * @param clientId Client ID + * @param eventType Event Type to retrieve subscriptions + * @return EventSubscriptionResponse Event Subscription Response containing subscriptions per specified + * event type */ public EventSubscriptionResponse getEventSubscriptionsByEventType(String clientId, String eventType) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -168,13 +175,14 @@ public EventSubscriptionResponse getEventSubscriptionsByEventType(String clientI for (EventSubscription eventSubscription : eventSubscriptionList) { eventSubscriptionResponseList.add(mapSubscriptionModelToResponseJson(eventSubscription)); } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK); + eventSubscriptionResponse.setStatus(HttpStatus.OK_200); eventSubscriptionResponse.setResponseBody(eventSubscriptionResponseList); return eventSubscriptionResponse; } catch (OBEventNotificationException e) { log.error("Error occurred while retrieving event subscriptions", e); - eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR); - eventSubscriptionResponse.setErrorResponse("Error occurred while retrieving event subscription."); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } } @@ -182,8 +190,8 @@ public EventSubscriptionResponse getEventSubscriptionsByEventType(String clientI /** * This method is used to update an event subscription. * - * @param eventSubscriptionUpdateRequestDto - * @return EventSubscriptionResponse + * @param eventSubscriptionUpdateRequestDto Event Subscription Update Request DTO + * @return EventSubscriptionResponse Event Subscription Response containing the updated subscription */ public EventSubscriptionResponse updateEventSubscription(EventSubscriptionDTO eventSubscriptionUpdateRequestDto) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -200,11 +208,13 @@ public EventSubscriptionResponse updateEventSubscription(EventSubscriptionDTO ev try { Boolean isUpdated = eventSubscriptionService.updateEventSubscription(eventSubscription); if (!isUpdated) { - eventSubscriptionResponse.setStatus(EventNotificationConstants.BAD_REQUEST); - eventSubscriptionResponse.setErrorResponse("Event subscription not found."); + eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Event subscription not found.")); return eventSubscriptionResponse; } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK); + eventSubscriptionResponse.setStatus(HttpStatus.OK_200); EventSubscription eventSubscriptionUpdateResponse = eventSubscriptionService. getEventSubscriptionBySubscriptionId(eventSubscriptionUpdateRequestDto.getSubscriptionId()); eventSubscriptionResponse. @@ -212,8 +222,9 @@ public EventSubscriptionResponse updateEventSubscription(EventSubscriptionDTO ev return eventSubscriptionResponse; } catch (OBEventNotificationException e) { log.error("Error occurred while updating event subscription", e); - eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR); - eventSubscriptionResponse.setErrorResponse("Error occurred while updating event subscription."); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } } @@ -221,9 +232,9 @@ public EventSubscriptionResponse updateEventSubscription(EventSubscriptionDTO ev /** * This method is used to delete an event subscription. * - * @param clientId - * @param subscriptionId - * @return EventSubscriptionResponse + * @param clientId Client ID + * @param subscriptionId Subscription ID to be deleted + * @return EventSubscriptionResponse Event Subscription Response containing the deleted subscription */ public EventSubscriptionResponse deleteEventSubscription(String clientId, String subscriptionId) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -236,16 +247,19 @@ public EventSubscriptionResponse deleteEventSubscription(String clientId, String try { Boolean isDeleted = eventSubscriptionService.deleteEventSubscription(subscriptionId); if (!isDeleted) { - eventSubscriptionResponse.setStatus(EventNotificationConstants.BAD_REQUEST); - eventSubscriptionResponse.setErrorResponse("Event subscription not found"); + eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Event subscription not found")); return eventSubscriptionResponse; } - eventSubscriptionResponse.setStatus(EventNotificationConstants.NO_CONTENT); + eventSubscriptionResponse.setStatus(HttpStatus.NO_CONTENT_204); return eventSubscriptionResponse; } catch (OBEventNotificationException e) { log.error("Error occurred while deleting event subscription", e); - eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR); - eventSubscriptionResponse.setErrorResponse("Error occurred while deleting event subscription"); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } } @@ -253,8 +267,9 @@ public EventSubscriptionResponse deleteEventSubscription(String clientId, String /** * This method is used to validate the client ID. * - * @param clientId - * @return EventSubscriptionResponse if the client ID is invalid, if the client ID is valid, null will be returned. + * @param clientId Client ID + * @return EventSubscriptionResponse Return EventSubscriptionResponse if the client ID is + * invalid, if the client ID is valid, null will be returned. */ private EventSubscriptionResponse validateClientId(String clientId) { try { @@ -262,9 +277,9 @@ private EventSubscriptionResponse validateClientId(String clientId) { } catch (OBEventNotificationException e) { log.error("Invalid client ID", e); EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); - eventSubscriptionResponse.setStatus(EventNotificationConstants.BAD_REQUEST); - eventSubscriptionResponse.setErrorResponse(String.format("A client was not found" + - " for the client id : '%s' in the database.. ", clientId)); + eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } return null; @@ -274,8 +289,8 @@ private EventSubscriptionResponse validateClientId(String clientId) { * This method will map the event subscription DTO to event subscription model * to be passed to the dao layer. * - * @param eventSubscriptionDTO - * @return EventSubscription + * @param eventSubscriptionDTO Event Subscription DTO + * @return EventSubscription Event Subscription Model mapped */ private EventSubscription mapEventSubscriptionDtoToModel(EventSubscriptionDTO eventSubscriptionDTO) { EventSubscription eventSubscription = new EventSubscription(); @@ -306,8 +321,8 @@ private EventSubscription mapEventSubscriptionDtoToModel(EventSubscriptionDTO ev /** * This method is used to create the response JSON object from the event subscription model. * - * @param eventSubscription - * @return JSONObject + * @param eventSubscription Event Subscription Model + * @return JSONObject containing mapped subscription */ public JSONObject mapSubscriptionModelToResponseJson(EventSubscription eventSubscription) { JSONObject responsePayload = new JSONObject(); diff --git a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/response/EventSubscriptionResponse.java b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/response/EventSubscriptionResponse.java index 380b6ec1..327ff5f0 100644 --- a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/response/EventSubscriptionResponse.java +++ b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/response/EventSubscriptionResponse.java @@ -23,15 +23,15 @@ */ public class EventSubscriptionResponse { - private String status; + private int status; private Object responseBody; - private String errorResponse; + private Object errorResponse; - public String getStatus() { + public int getStatus() { return status; } - public void setStatus(String status) { + public void setStatus(int status) { this.status = status; } @@ -43,11 +43,11 @@ public void setResponseBody(Object responseBody) { this.responseBody = responseBody; } - public String getErrorResponse() { + public Object getErrorResponse() { return errorResponse; } - public void setErrorResponse(String errorResponse) { + public void setErrorResponse(Object errorResponse) { this.errorResponse = errorResponse; } diff --git a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/util/EventNotificationServiceUtil.java b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/util/EventNotificationServiceUtil.java index 1c5314d4..4e0fd1eb 100644 --- a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/util/EventNotificationServiceUtil.java +++ b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/main/java/com/wso2/openbanking/accelerator/event/notifications/service/util/EventNotificationServiceUtil.java @@ -24,6 +24,7 @@ import com.wso2.openbanking.accelerator.common.util.OpenBankingUtils; import com.wso2.openbanking.accelerator.consent.mgt.service.impl.ConsentCoreServiceImpl; import com.wso2.openbanking.accelerator.event.notifications.service.constants.EventNotificationConstants; +import com.wso2.openbanking.accelerator.event.notifications.service.dto.EventNotificationErrorDTO; import com.wso2.openbanking.accelerator.event.notifications.service.exceptions.OBEventNotificationException; import com.wso2.openbanking.accelerator.event.notifications.service.handler.DefaultEventCreationServiceHandler; import com.wso2.openbanking.accelerator.event.notifications.service.realtime.service.RealtimeEventNotificationRequestGenerator; @@ -77,6 +78,8 @@ public static RealtimeEventNotificationRequestGenerator getRealtimeEventNotifica /** * Method to modify event notification payload with custom eventValues. + * + * @param jsonNode Json Node to convert * @return String eventNotificationPayload */ public static String getCustomNotificationPayload(JsonNode jsonNode) { @@ -87,8 +90,8 @@ public static String getCustomNotificationPayload(JsonNode jsonNode) { /** * Method to get event JSON from eventInformation payload string. - * @param eventInformation - * @return + * @param eventInformation String event Information + * @return JSONObject converted event json * @throws ParseException */ public static JSONObject getEventJSONFromString(String eventInformation) throws ParseException { @@ -99,7 +102,7 @@ public static JSONObject getEventJSONFromString(String eventInformation) throws /** * Validate if the client ID is existing. - * @param clientId + * @param clientId client ID of the TPP * @throws OBEventNotificationException */ @Generated(message = "Excluded since this needs OAuth2Util service provider") @@ -151,4 +154,18 @@ public static String getCallbackURL(String clientID) { public static DefaultEventCreationServiceHandler getDefaultEventCreationServiceHandler() { return new DefaultEventCreationServiceHandler(); } + + /** + * Method to map Event subscription Service error to API response. + * + * @param error Error code + * @param errorDescription Error description + * @return EventNotificationErrorDTO + */ + public static EventNotificationErrorDTO getErrorDTO(String error, String errorDescription) { + EventNotificationErrorDTO eventNotificationErrorDTO = new EventNotificationErrorDTO(); + eventNotificationErrorDTO.setError(error); + eventNotificationErrorDTO.setErrorDescription(errorDescription); + return eventNotificationErrorDTO; + } } diff --git a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/test/java/com/wso2/openbanking/accelerator/event/notifications/service/handler/DefaultEventSubscriptionServiceHandlerTests.java b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/test/java/com/wso2/openbanking/accelerator/event/notifications/service/handler/DefaultEventSubscriptionServiceHandlerTests.java index 6e18de13..f8cd9f2f 100644 --- a/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/test/java/com/wso2/openbanking/accelerator/event/notifications/service/handler/DefaultEventSubscriptionServiceHandlerTests.java +++ b/open-banking-accelerator/components/event-notifications/com.wso2.openbanking.accelerator.event.notifications.service/src/test/java/com/wso2/openbanking/accelerator/event/notifications/service/handler/DefaultEventSubscriptionServiceHandlerTests.java @@ -26,6 +26,7 @@ import com.wso2.openbanking.accelerator.event.notifications.service.util.EventNotificationServiceUtil; import com.wso2.openbanking.accelerator.event.notifications.service.utils.EventNotificationTestUtils; import net.minidev.json.JSONObject; +import org.eclipse.jetty.http.HttpStatus; import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -73,7 +74,7 @@ public void testCreateEventSubscription() throws Exception { EventSubscriptionResponse eventSubscriptionCreationResponse = defaultEventSubscriptionServiceHandler .createEventSubscription(EventNotificationTestUtils.getSampleEventSubscriptionDTO()); - Assert.assertEquals(eventSubscriptionCreationResponse.getStatus(), EventNotificationConstants.CREATED); + Assert.assertEquals(eventSubscriptionCreationResponse.getStatus(), HttpStatus.CREATED_201); } @Test @@ -92,7 +93,7 @@ public void testCreateEventSubscriptionServiceError() throws Exception { .createEventSubscription(EventNotificationTestUtils.getSampleEventSubscriptionDTO()); Assert.assertEquals(eventSubscriptionCreationResponse.getStatus(), - EventNotificationConstants.INTERNAL_SERVER_ERROR); + HttpStatus.INTERNAL_SERVER_ERROR_500); } @Test @@ -110,7 +111,7 @@ public void testGetEventSubscription() throws Exception { .getEventSubscription(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_SUBSCRIPTION_ID_1); - Assert.assertEquals(EventNotificationConstants.OK, eventSubscriptionRetrieveResponse.getStatus()); + Assert.assertEquals(eventSubscriptionRetrieveResponse.getStatus(), HttpStatus.OK_200); } @Test @@ -129,8 +130,7 @@ public void testGetEventSubscriptionServiceError() throws Exception { .getEventSubscription(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_SUBSCRIPTION_ID_1); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, - eventSubscriptionRetrieveResponse.getStatus()); + Assert.assertEquals(eventSubscriptionRetrieveResponse.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR_500); } @Test @@ -147,7 +147,7 @@ public void testGetAllEventSubscriptions() throws Exception { EventSubscriptionResponse eventSubscriptionRetrieveResponse = defaultEventSubscriptionServiceHandler .getAllEventSubscriptions(EventNotificationTestConstants.SAMPLE_CLIENT_ID); - Assert.assertEquals(EventNotificationConstants.OK, eventSubscriptionRetrieveResponse.getStatus()); + Assert.assertEquals(eventSubscriptionRetrieveResponse.getStatus(), HttpStatus.OK_200); } @Test @@ -165,8 +165,7 @@ public void testGetAllEventSubscriptionsServiceError() throws Exception { EventSubscriptionResponse eventSubscriptionRetrieveResponse = defaultEventSubscriptionServiceHandler .getAllEventSubscriptions(EventNotificationTestConstants.SAMPLE_CLIENT_ID); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, - eventSubscriptionRetrieveResponse.getStatus()); + Assert.assertEquals(eventSubscriptionRetrieveResponse.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR_500); } @Test @@ -184,7 +183,7 @@ public void testGetEventSubscriptionsByEventType() throws Exception { .getEventSubscriptionsByEventType(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_NOTIFICATION_EVENT_TYPE_1); - Assert.assertEquals(EventNotificationConstants.OK, eventSubscriptionRetrieveResponse.getStatus()); + Assert.assertEquals(eventSubscriptionRetrieveResponse.getStatus(), HttpStatus.OK_200); } @Test @@ -203,8 +202,7 @@ public void testGetEventSubscriptionsByEventTypeServiceError() throws Exception .getEventSubscriptionsByEventType(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_NOTIFICATION_EVENT_TYPE_1); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, - eventSubscriptionRetrieveResponse.getStatus()); + Assert.assertEquals(eventSubscriptionRetrieveResponse.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR_500); } @Test @@ -223,7 +221,7 @@ public void testUpdateEventSubscription() throws Exception { EventSubscriptionResponse eventSubscriptionUpdateResponse = defaultEventSubscriptionServiceHandler .updateEventSubscription(EventNotificationTestUtils.getSampleEventSubscriptionUpdateDTO()); - Assert.assertEquals(EventNotificationConstants.OK, eventSubscriptionUpdateResponse.getStatus()); + Assert.assertEquals(eventSubscriptionUpdateResponse.getStatus(), HttpStatus.OK_200); } @Test @@ -243,8 +241,7 @@ public void testUpdateEventSubscriptionServiceError() throws Exception { EventSubscriptionResponse eventSubscriptionUpdateResponse = defaultEventSubscriptionServiceHandler .updateEventSubscription(EventNotificationTestUtils.getSampleEventSubscriptionUpdateDTO()); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, - eventSubscriptionUpdateResponse.getStatus()); + Assert.assertEquals(eventSubscriptionUpdateResponse.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR_500); } @Test @@ -262,7 +259,7 @@ public void testDeleteEventSubscription() throws Exception { .deleteEventSubscription(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_SUBSCRIPTION_ID_1); - Assert.assertEquals(EventNotificationConstants.NO_CONTENT, eventSubscriptionDeletionResponse.getStatus()); + Assert.assertEquals(eventSubscriptionDeletionResponse.getStatus(), HttpStatus.NO_CONTENT_204); } @Test @@ -281,8 +278,7 @@ public void testDeleteEventSubscriptionServiceError() throws Exception { .deleteEventSubscription(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_SUBSCRIPTION_ID_1); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, - eventSubscriptionDeletionResponse.getStatus()); + Assert.assertEquals(eventSubscriptionDeletionResponse.getStatus(), HttpStatus.INTERNAL_SERVER_ERROR_500); } @Test diff --git a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/util/EventNotificationUtils.java b/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/util/EventNotificationUtils.java index 4382ace6..95d8194f 100644 --- a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/util/EventNotificationUtils.java +++ b/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/util/EventNotificationUtils.java @@ -22,8 +22,8 @@ import com.wso2.openbanking.accelerator.common.constant.OpenBankingConstants; import com.wso2.openbanking.accelerator.common.util.OpenBankingUtils; import com.wso2.openbanking.accelerator.event.notifications.endpoint.constants.EventNotificationEndPointConstants; -import com.wso2.openbanking.accelerator.event.notifications.endpoint.dto.EventNotificationErrorDTO; import com.wso2.openbanking.accelerator.event.notifications.service.constants.EventNotificationConstants; +import com.wso2.openbanking.accelerator.event.notifications.service.dto.EventNotificationErrorDTO; import com.wso2.openbanking.accelerator.event.notifications.service.handler.EventCreationServiceHandler; import com.wso2.openbanking.accelerator.event.notifications.service.handler.EventPollingServiceHandler; import com.wso2.openbanking.accelerator.event.notifications.service.response.EventCreationResponse; diff --git a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/util/EventSubscriptionUtils.java b/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/util/EventSubscriptionUtils.java index 32a80e93..f5cd07f5 100644 --- a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/util/EventSubscriptionUtils.java +++ b/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/util/EventSubscriptionUtils.java @@ -21,14 +21,15 @@ import com.wso2.openbanking.accelerator.common.config.OpenBankingConfigParser; import com.wso2.openbanking.accelerator.common.constant.OpenBankingConstants; import com.wso2.openbanking.accelerator.common.util.OpenBankingUtils; -import com.wso2.openbanking.accelerator.event.notifications.endpoint.constants.EventNotificationEndPointConstants; import com.wso2.openbanking.accelerator.event.notifications.service.constants.EventNotificationConstants; import com.wso2.openbanking.accelerator.event.notifications.service.handler.EventSubscriptionServiceHandler; import com.wso2.openbanking.accelerator.event.notifications.service.response.EventSubscriptionResponse; +import com.wso2.openbanking.accelerator.event.notifications.service.util.EventNotificationServiceUtil; import net.minidev.json.JSONObject; import net.minidev.json.parser.JSONParser; import net.minidev.json.parser.ParseException; import org.apache.commons.io.IOUtils; +import org.eclipse.jetty.http.HttpStatus; import java.io.IOException; @@ -74,34 +75,23 @@ public static JSONObject getJSONObjectPayload(HttpServletRequest request) throws * @return Response */ public static Response mapEventSubscriptionServiceResponse(EventSubscriptionResponse eventSubscriptionResponse) { - String status = eventSubscriptionResponse.getStatus(); + int status = eventSubscriptionResponse.getStatus(); if (eventSubscriptionResponse.getErrorResponse() == null) { - switch (status) { - case EventNotificationConstants.CREATED: - return Response.status(Response.Status.CREATED) - .entity(eventSubscriptionResponse.getResponseBody()) - .build(); - case EventNotificationConstants.OK: - return Response.status(Response.Status.OK) - .entity(eventSubscriptionResponse.getResponseBody()) - .build(); - case EventNotificationConstants.NO_CONTENT: - return Response.status(Response.Status.NO_CONTENT) - .entity(eventSubscriptionResponse.getResponseBody()) - .build(); + if (eventSubscriptionResponse.getResponseBody() != null) { + return Response.status(status) + .entity(eventSubscriptionResponse.getResponseBody()) + .build(); + } else { + return Response.status(HttpStatus.INTERNAL_SERVER_ERROR_500) + .entity(EventNotificationServiceUtil.getErrorDTO(EventNotificationConstants.INVALID_REQUEST, + EventNotificationConstants.ERROR_HANDLING_EVENT_SUBSCRIPTION)) + .build(); } - } else if (status.equals(EventNotificationConstants.BAD_REQUEST)) { - return Response.status(Response.Status.BAD_REQUEST) - .entity(EventNotificationUtils.getErrorDTO( - EventNotificationEndPointConstants.INVALID_REQUEST, - eventSubscriptionResponse.getErrorResponse())) + } else { + return Response.status(status) + .entity(eventSubscriptionResponse.getErrorResponse()) .build(); } - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(EventNotificationUtils.getErrorDTO( - "Internal Server Error", - eventSubscriptionResponse.getErrorResponse())) - .build(); } }