From c38df2f7b92ff5a734e558e65f4d6ff29fb9981c Mon Sep 17 00:00:00 2001 From: Ashirwada Date: Fri, 1 Sep 2023 11:04:15 +0530 Subject: [PATCH 1/4] Fixing issue in event subscription error handling --- .../constants/EventNotificationConstants.java | 7 ++ .../dto/EventNotificationErrorDTO.java | 53 ++++++++ ...efaultEventSubscriptionServiceHandler.java | 114 ++++++++++-------- .../response/EventSubscriptionResponse.java | 12 +- .../util/EventNotificationServiceUtil.java | 20 ++- ...tEventSubscriptionServiceHandlerTests.java | 24 ++-- .../endpoint/util/EventNotificationUtils.java | 2 +- .../endpoint/util/EventSubscriptionUtils.java | 34 ++---- 8 files changed, 170 insertions(+), 96 deletions(-) create mode 100644 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 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..9edebcc6 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 @@ -32,6 +32,12 @@ public class EventNotificationConstants { public static final String OPEN = "OPEN"; //Response Status + public static final int NOT_FOUND_CODE = 404; + public static final int OK_CODE = 200; + public static final int CREATED_CODE = 201; + public static final int BAD_REQUEST_CODE = 400; + public static final int NO_CONTENT_CODE = 204; + public static final int INTERNAL_SERVER_ERROR_CODE = 500; public static final String NOT_FOUND = "NOTFOUND"; public static final String OK = "OK"; public static final String CREATED = "CREATED"; @@ -53,6 +59,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"; 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/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 new file mode 100644 index 00000000..37703521 --- /dev/null +++ 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 @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.wso2.openbanking.accelerator.event.notifications.service.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Error model for Event Notifications. + */ +public class EventNotificationErrorDTO { + + private String errorDescription; + private String error; + + @JsonProperty("error_description") + public String getError_description() { + + return errorDescription; + } + + public void setErrorDescription(String errorDescription) { + + this.errorDescription = errorDescription; + } + + @JsonProperty("error") + public String getError() { + + return error; + } + + public void setError(String error) { + + this.error = error; + } + +} 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..d23546e0 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 @@ -47,8 +47,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 +64,16 @@ public EventSubscriptionResponse createEventSubscription(EventSubscriptionDTO ev try { EventSubscription createEventSubscriptionResponse = eventSubscriptionService. createEventSubscription(eventSubscription); - eventSubscriptionResponse.setStatus(EventNotificationConstants.CREATED); + eventSubscriptionResponse.setStatus(EventNotificationConstants.CREATED_CODE); 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(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Error occurred while creating event subscription")); 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,21 @@ public EventSubscriptionResponse getEventSubscription(String clientId, String su try { EventSubscription eventSubscription = eventSubscriptionService. getEventSubscriptionBySubscriptionId(subscriptionId); - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK); + eventSubscriptionResponse.setStatus(EventNotificationConstants.OK_CODE); 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(EventNotificationConstants.BAD_REQUEST_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Event subscription not found")); } else { - eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR); - eventSubscriptionResponse.setErrorResponse("Error occurred while retrieving event subscription"); + eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Error occurred while retrieving event subscription")); } return eventSubscriptionResponse; } @@ -115,8 +122,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 +141,15 @@ public EventSubscriptionResponse getAllEventSubscriptions(String clientId) { for (EventSubscription eventSubscription : eventSubscriptionList) { eventSubscriptionResponseList.add(mapSubscriptionModelToResponseJson(eventSubscription)); } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK); + eventSubscriptionResponse.setStatus(EventNotificationConstants.OK_CODE); 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(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Error occurred while retrieving event subscription.")); return eventSubscriptionResponse; } } @@ -148,9 +157,9 @@ 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 +177,15 @@ public EventSubscriptionResponse getEventSubscriptionsByEventType(String clientI for (EventSubscription eventSubscription : eventSubscriptionList) { eventSubscriptionResponseList.add(mapSubscriptionModelToResponseJson(eventSubscription)); } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK); + eventSubscriptionResponse.setStatus(EventNotificationConstants.OK_CODE); 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(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Error occurred while retrieving event subscription.")); return eventSubscriptionResponse; } } @@ -182,8 +193,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 +211,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(EventNotificationConstants.BAD_REQUEST_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Event subscription not found.")); return eventSubscriptionResponse; } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK); + eventSubscriptionResponse.setStatus(EventNotificationConstants.OK_CODE); EventSubscription eventSubscriptionUpdateResponse = eventSubscriptionService. getEventSubscriptionBySubscriptionId(eventSubscriptionUpdateRequestDto.getSubscriptionId()); eventSubscriptionResponse. @@ -212,8 +225,10 @@ 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(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Error occurred while updating event subscription.")); return eventSubscriptionResponse; } } @@ -221,9 +236,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 +251,20 @@ 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(EventNotificationConstants.BAD_REQUEST_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Event subscription not found")); return eventSubscriptionResponse; } - eventSubscriptionResponse.setStatus(EventNotificationConstants.NO_CONTENT); + eventSubscriptionResponse.setStatus(EventNotificationConstants.NO_CONTENT_CODE); 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(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "Error occurred while deleting event subscription")); return eventSubscriptionResponse; } } @@ -253,7 +272,7 @@ public EventSubscriptionResponse deleteEventSubscription(String clientId, String /** * This method is used to validate the client ID. * - * @param clientId + * @param clientId Client ID * @return EventSubscriptionResponse if the client ID is invalid, if the client ID is valid, null will be returned. */ private EventSubscriptionResponse validateClientId(String clientId) { @@ -262,9 +281,10 @@ 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(EventNotificationConstants.BAD_REQUEST_CODE); + eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, + "A client was not found for the client id in the database.")); return eventSubscriptionResponse; } return null; @@ -274,8 +294,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 +326,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..3427d7ad 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,15 @@ public static String getCallbackURL(String clientID) { public static DefaultEventCreationServiceHandler getDefaultEventCreationServiceHandler() { return new DefaultEventCreationServiceHandler(); } + + /** + * Method to map Event subscription Service error to API response. + * @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..528f8db6 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 @@ -73,7 +73,7 @@ public void testCreateEventSubscription() throws Exception { EventSubscriptionResponse eventSubscriptionCreationResponse = defaultEventSubscriptionServiceHandler .createEventSubscription(EventNotificationTestUtils.getSampleEventSubscriptionDTO()); - Assert.assertEquals(eventSubscriptionCreationResponse.getStatus(), EventNotificationConstants.CREATED); + Assert.assertEquals(eventSubscriptionCreationResponse.getStatus(), EventNotificationConstants.CREATED_CODE); } @Test @@ -92,7 +92,7 @@ public void testCreateEventSubscriptionServiceError() throws Exception { .createEventSubscription(EventNotificationTestUtils.getSampleEventSubscriptionDTO()); Assert.assertEquals(eventSubscriptionCreationResponse.getStatus(), - EventNotificationConstants.INTERNAL_SERVER_ERROR); + EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE); } @Test @@ -110,7 +110,7 @@ public void testGetEventSubscription() throws Exception { .getEventSubscription(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_SUBSCRIPTION_ID_1); - Assert.assertEquals(EventNotificationConstants.OK, eventSubscriptionRetrieveResponse.getStatus()); + Assert.assertEquals(EventNotificationConstants.OK_CODE, eventSubscriptionRetrieveResponse.getStatus()); } @Test @@ -129,7 +129,7 @@ public void testGetEventSubscriptionServiceError() throws Exception { .getEventSubscription(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_SUBSCRIPTION_ID_1); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, + Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE, eventSubscriptionRetrieveResponse.getStatus()); } @@ -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(EventNotificationConstants.OK_CODE, eventSubscriptionRetrieveResponse.getStatus()); } @Test @@ -165,7 +165,7 @@ public void testGetAllEventSubscriptionsServiceError() throws Exception { EventSubscriptionResponse eventSubscriptionRetrieveResponse = defaultEventSubscriptionServiceHandler .getAllEventSubscriptions(EventNotificationTestConstants.SAMPLE_CLIENT_ID); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, + Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE, eventSubscriptionRetrieveResponse.getStatus()); } @@ -184,7 +184,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(EventNotificationConstants.OK_CODE, eventSubscriptionRetrieveResponse.getStatus()); } @Test @@ -203,7 +203,7 @@ public void testGetEventSubscriptionsByEventTypeServiceError() throws Exception .getEventSubscriptionsByEventType(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_NOTIFICATION_EVENT_TYPE_1); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, + Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE, eventSubscriptionRetrieveResponse.getStatus()); } @@ -223,7 +223,7 @@ public void testUpdateEventSubscription() throws Exception { EventSubscriptionResponse eventSubscriptionUpdateResponse = defaultEventSubscriptionServiceHandler .updateEventSubscription(EventNotificationTestUtils.getSampleEventSubscriptionUpdateDTO()); - Assert.assertEquals(EventNotificationConstants.OK, eventSubscriptionUpdateResponse.getStatus()); + Assert.assertEquals(EventNotificationConstants.OK_CODE, eventSubscriptionUpdateResponse.getStatus()); } @Test @@ -243,7 +243,7 @@ public void testUpdateEventSubscriptionServiceError() throws Exception { EventSubscriptionResponse eventSubscriptionUpdateResponse = defaultEventSubscriptionServiceHandler .updateEventSubscription(EventNotificationTestUtils.getSampleEventSubscriptionUpdateDTO()); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, + Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE, eventSubscriptionUpdateResponse.getStatus()); } @@ -262,7 +262,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(EventNotificationConstants.NO_CONTENT_CODE, eventSubscriptionDeletionResponse.getStatus()); } @Test @@ -281,7 +281,7 @@ public void testDeleteEventSubscriptionServiceError() throws Exception { .deleteEventSubscription(EventNotificationTestConstants.SAMPLE_CLIENT_ID, EventNotificationTestConstants.SAMPLE_SUBSCRIPTION_ID_1); - Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR, + Assert.assertEquals(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE, eventSubscriptionDeletionResponse.getStatus()); } 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..427723c8 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,8 +21,6 @@ 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 net.minidev.json.JSONObject; @@ -74,34 +72,16 @@ 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(); - } - } else if (status.equals(EventNotificationConstants.BAD_REQUEST)) { - return Response.status(Response.Status.BAD_REQUEST) - .entity(EventNotificationUtils.getErrorDTO( - EventNotificationEndPointConstants.INVALID_REQUEST, - eventSubscriptionResponse.getErrorResponse())) + return Response.status(status) + .entity(eventSubscriptionResponse.getResponseBody()) + .build(); + } 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(); } } From a31eef6176e5003bd359e183aae45a90a5874f2b Mon Sep 17 00:00:00 2001 From: Ashirwada Date: Fri, 1 Sep 2023 14:30:45 +0530 Subject: [PATCH 2/4] Fixing issue in event subscription error handling --- .../constants/EventNotificationConstants.java | 8 +- .../dto/EventNotificationErrorDTO.java | 2 +- ...efaultEventSubscriptionServiceHandler.java | 99 +++++++++---------- .../util/EventNotificationServiceUtil.java | 3 + ...tEventSubscriptionServiceHandlerTests.java | 30 +++--- .../dto/EventNotificationErrorDTO.java | 53 ---------- .../endpoint/util/EventSubscriptionUtils.java | 16 ++- 7 files changed, 79 insertions(+), 132 deletions(-) delete mode 100644 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 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 9edebcc6..35d75845 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 @@ -32,12 +32,6 @@ public class EventNotificationConstants { public static final String OPEN = "OPEN"; //Response Status - public static final int NOT_FOUND_CODE = 404; - public static final int OK_CODE = 200; - public static final int CREATED_CODE = 201; - public static final int BAD_REQUEST_CODE = 400; - public static final int NO_CONTENT_CODE = 204; - public static final int INTERNAL_SERVER_ERROR_CODE = 500; public static final String NOT_FOUND = "NOTFOUND"; public static final String OK = "OK"; public static final String CREATED = "CREATED"; @@ -119,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 vent subscription " + + "request"; } 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/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 index 37703521..1121b211 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/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 @@ -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 d23546e0..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 Event Subscription DTO - * @return EventSubscriptionResponse Event Subscription Response + * @param eventSubscriptionRequestDto Event Subscription DTO + * @return EventSubscriptionResponse Event Subscription Response */ public EventSubscriptionResponse createEventSubscription(EventSubscriptionDTO eventSubscriptionRequestDto) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -64,16 +65,15 @@ public EventSubscriptionResponse createEventSubscription(EventSubscriptionDTO ev try { EventSubscription createEventSubscriptionResponse = eventSubscriptionService. createEventSubscription(eventSubscription); - eventSubscriptionResponse.setStatus(EventNotificationConstants.CREATED_CODE); + 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_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( - EventNotificationConstants.INVALID_REQUEST, - "Error occurred while creating event subscription")); + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } @@ -82,10 +82,10 @@ public EventSubscriptionResponse createEventSubscription(EventSubscriptionDTO ev /** * This method is used to retrieve a single event subscription. * - * @param clientId Client ID of the subscription created - * @param subscriptionId Subscription ID of the subscription created + * @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 + * details for the given subscription ID */ public EventSubscriptionResponse getEventSubscription(String clientId, String subscriptionId) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -99,21 +99,19 @@ public EventSubscriptionResponse getEventSubscription(String clientId, String su try { EventSubscription eventSubscription = eventSubscriptionService. getEventSubscriptionBySubscriptionId(subscriptionId); - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK_CODE); + 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_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( - EventNotificationConstants.INVALID_REQUEST, - "Event subscription not found")); + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); } else { - eventSubscriptionResponse.setStatus(EventNotificationConstants.INTERNAL_SERVER_ERROR_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( - EventNotificationConstants.INVALID_REQUEST, - "Error occurred while retrieving event subscription")); + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); } return eventSubscriptionResponse; } @@ -122,8 +120,8 @@ public EventSubscriptionResponse getEventSubscription(String clientId, String su /** * This method is used to retrieve all event subscriptions of a client. * - * @param clientId Client ID - * @return EventSubscriptionResponse Event Subscription Response containing all the subscriptions + * @param clientId Client ID + * @return EventSubscriptionResponse Event Subscription Response containing all the subscriptions */ public EventSubscriptionResponse getAllEventSubscriptions(String clientId) { EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse(); @@ -141,15 +139,14 @@ public EventSubscriptionResponse getAllEventSubscriptions(String clientId) { for (EventSubscription eventSubscription : eventSubscriptionList) { eventSubscriptionResponseList.add(mapSubscriptionModelToResponseJson(eventSubscription)); } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK_CODE); + 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_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( - EventNotificationConstants.INVALID_REQUEST, - "Error occurred while retrieving event subscription.")); + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } } @@ -157,9 +154,10 @@ public EventSubscriptionResponse getAllEventSubscriptions(String clientId) { /** * This method is used to retrieve all event subscriptions by event type. * - * @param clientId Client ID - * @param eventType Event Type to retrieve subscriptions - * @return EventSubscriptionResponse Event Subscription Response containing subscriptions per specified event type + * @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(); @@ -177,15 +175,14 @@ public EventSubscriptionResponse getEventSubscriptionsByEventType(String clientI for (EventSubscription eventSubscription : eventSubscriptionList) { eventSubscriptionResponseList.add(mapSubscriptionModelToResponseJson(eventSubscription)); } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK_CODE); + 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_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( - EventNotificationConstants.INVALID_REQUEST, - "Error occurred while retrieving event subscription.")); + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } } @@ -193,8 +190,8 @@ public EventSubscriptionResponse getEventSubscriptionsByEventType(String clientI /** * This method is used to update an event subscription. * - * @param eventSubscriptionUpdateRequestDto Event Subscription Update Request DTO - * @return EventSubscriptionResponse Event Subscription Response containing the updated subscription + * @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(); @@ -211,13 +208,13 @@ public EventSubscriptionResponse updateEventSubscription(EventSubscriptionDTO ev try { Boolean isUpdated = eventSubscriptionService.updateEventSubscription(eventSubscription); if (!isUpdated) { - eventSubscriptionResponse.setStatus(EventNotificationConstants.BAD_REQUEST_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( EventNotificationConstants.INVALID_REQUEST, "Event subscription not found.")); return eventSubscriptionResponse; } - eventSubscriptionResponse.setStatus(EventNotificationConstants.OK_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.OK_200); EventSubscription eventSubscriptionUpdateResponse = eventSubscriptionService. getEventSubscriptionBySubscriptionId(eventSubscriptionUpdateRequestDto.getSubscriptionId()); eventSubscriptionResponse. @@ -225,10 +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_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( - EventNotificationConstants.INVALID_REQUEST, - "Error occurred while updating event subscription.")); + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } } @@ -236,9 +232,9 @@ public EventSubscriptionResponse updateEventSubscription(EventSubscriptionDTO ev /** * This method is used to delete an event subscription. * - * @param clientId Client ID - * @param subscriptionId Subscription ID to be deleted - * @return EventSubscriptionResponse Event Subscription Response containing the deleted subscription + * @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(); @@ -251,20 +247,19 @@ public EventSubscriptionResponse deleteEventSubscription(String clientId, String try { Boolean isDeleted = eventSubscriptionService.deleteEventSubscription(subscriptionId); if (!isDeleted) { - eventSubscriptionResponse.setStatus(EventNotificationConstants.BAD_REQUEST_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( EventNotificationConstants.INVALID_REQUEST, "Event subscription not found")); return eventSubscriptionResponse; } - eventSubscriptionResponse.setStatus(EventNotificationConstants.NO_CONTENT_CODE); + 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_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( - EventNotificationConstants.INVALID_REQUEST, - "Error occurred while deleting event subscription")); + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } } @@ -272,8 +267,9 @@ public EventSubscriptionResponse deleteEventSubscription(String clientId, String /** * This method is used to validate the client ID. * - * @param clientId Client ID - * @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 { @@ -281,10 +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_CODE); + eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400); eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( - EventNotificationConstants.INVALID_REQUEST, - "A client was not found for the client id in the database.")); + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventSubscriptionResponse; } return null; @@ -294,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 Event Subscription DTO - * @return EventSubscription Event Subscription Model mapped + * @param eventSubscriptionDTO Event Subscription DTO + * @return EventSubscription Event Subscription Model mapped */ private EventSubscription mapEventSubscriptionDtoToModel(EventSubscriptionDTO eventSubscriptionDTO) { EventSubscription eventSubscription = new EventSubscription(); @@ -326,7 +321,7 @@ private EventSubscription mapEventSubscriptionDtoToModel(EventSubscriptionDTO ev /** * This method is used to create the response JSON object from the event subscription model. * - * @param eventSubscription Event Subscription Model + * @param eventSubscription Event Subscription Model * @return JSONObject containing mapped subscription */ public JSONObject mapSubscriptionModelToResponseJson(EventSubscription eventSubscription) { 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 3427d7ad..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 @@ -157,6 +157,9 @@ public static DefaultEventCreationServiceHandler getDefaultEventCreationServiceH /** * 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) { 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 528f8db6..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_CODE); + 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_CODE); + 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_CODE, 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_CODE, - 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_CODE, 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_CODE, - 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_CODE, 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_CODE, - 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_CODE, 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_CODE, - 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_CODE, 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_CODE, - 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/dto/EventNotificationErrorDTO.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/dto/EventNotificationErrorDTO.java deleted file mode 100644 index 9d92ca83..00000000 --- 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 +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package com.wso2.openbanking.accelerator.event.notifications.endpoint.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; - -/** - * Error model for Event Notifications. - */ -public class EventNotificationErrorDTO { - - private String errorDescription; - private String error; - - @JsonProperty("error_description") - public String getError_description() { - - return errorDescription; - } - - public void setErrorDescription(String errorDescription) { - - this.errorDescription = errorDescription; - } - - @JsonProperty("error") - public String getError() { - - return error; - } - - public void setError(String error) { - - this.error = error; - } - -} 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 427723c8..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,12 +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.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,9 +77,16 @@ public static JSONObject getJSONObjectPayload(HttpServletRequest request) throws public static Response mapEventSubscriptionServiceResponse(EventSubscriptionResponse eventSubscriptionResponse) { int status = eventSubscriptionResponse.getStatus(); if (eventSubscriptionResponse.getErrorResponse() == null) { - return Response.status(status) - .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 { return Response.status(status) .entity(eventSubscriptionResponse.getErrorResponse()) From 49ca0e0197b91db77a0ba5b7c998514931e3f6cc Mon Sep 17 00:00:00 2001 From: Ashirwada Date: Mon, 4 Sep 2023 09:11:25 +0530 Subject: [PATCH 3/4] Fixing issue in event subscription error handling --- .../service/constants/EventNotificationConstants.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 35d75845..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 @@ -113,6 +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 vent subscription " + - "request"; + public static final String ERROR_HANDLING_EVENT_SUBSCRIPTION = "Error occurred while handling the event " + + "subscription request"; } From 8d075b3463d270afe7ec0bd15354b3092f93944f Mon Sep 17 00:00:00 2001 From: Ashirwada Date: Thu, 21 Sep 2023 10:22:19 +0530 Subject: [PATCH 4/4] Fixing issue in event polling error handling --- .../DefaultEventPollingServiceHandler.java | 9 ++++--- .../response/EventPollingResponse.java | 6 ++--- .../endpoint/util/EventNotificationUtils.java | 26 +++++++------------ 3 files changed, 17 insertions(+), 24 deletions(-) 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/DefaultEventPollingServiceHandler.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/DefaultEventPollingServiceHandler.java index 26943de1..063a2dca 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/DefaultEventPollingServiceHandler.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/DefaultEventPollingServiceHandler.java @@ -65,9 +65,9 @@ public EventPollingResponse pollEvents(JSONObject eventPollingRequest) { } catch (OBEventNotificationException e) { log.error("Invalid client ID", e); eventPollingResponse.setStatus(EventNotificationConstants.BAD_REQUEST); - eventPollingResponse.setErrorResponse(String.format("A client was not found" + - " for the client id : '%s' in the database.. ", - eventPollingDTO.getClientId())); + eventPollingResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, String.format("A client was not found" + + " for the client id : '%s' in the database.. ", eventPollingDTO.getClientId()))); return eventPollingResponse; } @@ -80,7 +80,8 @@ public EventPollingResponse pollEvents(JSONObject eventPollingRequest) { } catch (OBEventNotificationException e) { log.error("OB Event Notification error" , e); eventPollingResponse.setStatus(EventNotificationConstants.BAD_REQUEST); - eventPollingResponse.setErrorResponse(e.getMessage()); + eventPollingResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO( + EventNotificationConstants.INVALID_REQUEST, e.getMessage())); return eventPollingResponse; } 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/EventPollingResponse.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/EventPollingResponse.java index 14580d04..af8b7ee0 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/EventPollingResponse.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/EventPollingResponse.java @@ -27,7 +27,7 @@ public class EventPollingResponse { private String status; private JSONObject responseBody; - private String errorResponse; + private Object errorResponse; public String getStatus() { return status; @@ -45,11 +45,11 @@ public void setResponseBody(JSONObject 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/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 95d8194f..ab744948 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 @@ -97,25 +97,17 @@ public static Response mapEventCreationServiceResponse(EventCreationResponse eve public static Response mapEventPollingServiceResponse(EventPollingResponse eventPollingResponse) { if (EventNotificationConstants.OK.equals(eventPollingResponse.getStatus())) { - return Response.status(Response.Status.OK).entity(eventPollingResponse.getResponseBody()).build(); - - } else if (EventNotificationConstants.NOT_FOUND.equals(eventPollingResponse.getStatus())) { - - return Response.status(Response.Status.NOT_FOUND).entity(EventNotificationUtils.getErrorDTO( - EventNotificationEndPointConstants.NOTIFICATIONS_NOT_FOUND, - EventNotificationEndPointConstants.NOT_FOUND_RESPONSE)).build(); - - } else if (EventNotificationConstants.BAD_REQUEST.equals(eventPollingResponse.getStatus())) { - - return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( - EventNotificationEndPointConstants.INVALID_REQUEST, - eventPollingResponse.getErrorResponse())).build(); + } else { + if (eventPollingResponse.getErrorResponse() instanceof String) { + return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( + EventNotificationEndPointConstants.INVALID_REQUEST, + eventPollingResponse.getErrorResponse().toString())).build(); + } else { + return Response.status(Response.Status.BAD_REQUEST).entity(eventPollingResponse.getErrorResponse()) + .build(); + } } - - return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( - EventNotificationEndPointConstants.INVALID_REQUEST, - EventNotificationEndPointConstants.POLLING_ERROR_RESPONSE)).build(); } /**