Skip to content

Commit

Permalink
Fixing issue in event subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashi1993 committed Oct 12, 2023
1 parent 195f5bc commit 19d7949
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE TABLE OB_NOTIFICATION (
NOTIFICATION_ID varchar(36) NOT NULL,
CLIENT_ID varchar(255) NOT NULL,
RESOURCE_ID varchar(255) NOT NULL,
STATUS varchar(10) NOT NULL,
UPDATED_TIMESTAMP DATETIME2(0) DEFAULT GETDATE(),
PRIMARY KEY (NOTIFICATION_ID)
);

CREATE TABLE OB_NOTIFICATION_EVENT (
EVENT_ID int NOT NULL IDENTITY,
NOTIFICATION_ID varchar(36) NOT NULL,
EVENT_TYPE varchar(200) NOT NULL,
EVENT_INFO varchar(1000) NOT NULL,
PRIMARY KEY (EVENT_ID),
CONSTRAINT FK_NotificationEvent FOREIGN KEY (NOTIFICATION_ID) REFERENCES OB_NOTIFICATION(NOTIFICATION_ID)
);

CREATE TABLE OB_NOTIFICATION_ERROR (
NOTIFICATION_ID varchar(36) NOT NULL,
ERROR_CODE varchar(255) NOT NULL,
DESCRIPTION varchar(255) NOT NULL,
PRIMARY KEY (NOTIFICATION_ID),
CONSTRAINT FK_NotificationError FOREIGN KEY (NOTIFICATION_ID) REFERENCES OB_NOTIFICATION(NOTIFICATION_ID)
);

CREATE TABLE OB_NOTIFICATION_SUBSCRIPTION (
SUBSCRIPTION_ID varchar(36) NOT NULL,
CLIENT_ID varchar(255) NOT NULL,
REQUEST JSON NOT NULL,
CALLBACK_URL varchar(255),
TIMESTAMP BIGINT NOT NULL,
SPEC_VERSION varchar(255),
STATUS varchar(255) NOT NULL,
PRIMARY KEY (SUBSCRIPTION_ID)
);

CREATE TABLE OB_NOTIFICATION_SUBSCRIPTION_EVENTS (
SUBSCRIPTION_ID varchar(36) NOT NULL,
EVENT_TYPE varchar(255) NOT NULL,
PRIMARY KEY (SUBSCRIPTION_ID, EVENT_TYPE),
CONSTRAINT FK_NotificationSubEvents FOREIGN KEY (SUBSCRIPTION_ID) REFERENCES OB_NOTIFICATION_SUBSCRIPTION(SUBSCRIPTION_ID)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- For event notifications feature run the following queries against the openbank_openbankingdb--

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION (
NOTIFICATION_ID varchar(36) NOT NULL,
CLIENT_ID varchar(255) NOT NULL,
RESOURCE_ID varchar(255) NOT NULL,
STATUS varchar(10) NOT NULL,
UPDATED_TIMESTAMP TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (NOTIFICATION_ID)
)
ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION_EVENT (
EVENT_ID int(11) NOT NULL AUTO_INCREMENT,
NOTIFICATION_ID varchar(36) NOT NULL,
EVENT_TYPE varchar(200) NOT NULL,
EVENT_INFO varchar(1000) NOT NULL,
PRIMARY KEY (EVENT_ID),
CONSTRAINT FK_NotificationEvent FOREIGN KEY (NOTIFICATION_ID) REFERENCES OB_NOTIFICATION(NOTIFICATION_ID)
)
ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION_ERROR (
NOTIFICATION_ID varchar(36) NOT NULL,
ERROR_CODE varchar(255) NOT NULL,
DESCRIPTION varchar(255) NOT NULL,
PRIMARY KEY (NOTIFICATION_ID),
CONSTRAINT FK_NotificationError FOREIGN KEY (NOTIFICATION_ID) REFERENCES OB_NOTIFICATION(NOTIFICATION_ID)
)
ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION_SUBSCRIPTION (
SUBSCRIPTION_ID varchar(36) NOT NULL,
CLIENT_ID varchar(255) NOT NULL,
REQUEST JSON NOT NULL,
CALLBACK_URL varchar(255),
TIMESTAMP BIGINT NOT NULL,
SPEC_VERSION varchar(255),
STATUS varchar(255) NOT NULL,
PRIMARY KEY (SUBSCRIPTION_ID)
)
ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION_SUBSCRIPTION_EVENTS (
SUBSCRIPTION_ID varchar(36) NOT NULL,
EVENT_TYPE varchar(255) NOT NULL,
PRIMARY KEY (SUBSCRIPTION_ID, EVENT_TYPE),
CONSTRAINT FK_NotificationSubEvents FOREIGN KEY (SUBSCRIPTION_ID) REFERENCES OB_NOTIFICATION_SUBSCRIPTION(SUBSCRIPTION_ID)
)
ENGINE=InnoDB;
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
CREATE TABLE OB_NOTIFICATION (
NOTIFICATION_ID varchar2(36) NOT NULL,
CLIENT_ID varchar2(255) NOT NULL,
RESOURCE_ID varchar2(255) NOT NULL,
STATUS varchar2(10) NOT NULL,
UPDATED_TIMESTAMP TIMESTAMP(0) DEFAULT SYSTIMESTAMP,
PRIMARY KEY (NOTIFICATION_ID)
);

CREATE TABLE OB_NOTIFICATION_EVENT (
EVENT_ID number(10) NOT NULL,
NOTIFICATION_ID varchar2(36) NOT NULL,
EVENT_TYPE varchar2(200) NOT NULL,
EVENT_INFO varchar2(1000) NOT NULL,
PRIMARY KEY (EVENT_ID),
CONSTRAINT FK_NotificationEvent FOREIGN KEY (NOTIFICATION_ID) REFERENCES OB_NOTIFICATION(NOTIFICATION_ID)
);

-- Generate ID using sequence and trigger
CREATE SEQUENCE OB_NOTIFICATION_EVENT_seq START WITH 1 INCREMENT BY 1;

CREATE OR REPLACE TRIGGER OB_NOTIFICATION_EVENT_seq_tr
BEFORE INSERT ON OB_NOTIFICATION_EVENT FOR EACH ROW
WHEN (NEW.EVENT_ID IS NULL)
BEGIN
SELECT OB_NOTIFICATION_EVENT_seq.NEXTVAL INTO :NEW.EVENT_ID FROM DUAL;
END;


CREATE TABLE OB_NOTIFICATION_ERROR (
NOTIFICATION_ID varchar2(36) NOT NULL,
ERROR_CODE varchar2(255) NOT NULL,
DESCRIPTION varchar2(255) NOT NULL,
PRIMARY KEY (NOTIFICATION_ID),
CONSTRAINT FK_NotificationError FOREIGN KEY (NOTIFICATION_ID) REFERENCES OB_NOTIFICATION(NOTIFICATION_ID)
)

CREATE TABLE OB_NOTIFICATION_SUBSCRIPTION (
SUBSCRIPTION_ID varchar(36) NOT NULL,
CLIENT_ID varchar(255) NOT NULL,
REQUEST JSON NOT NULL,
CALLBACK_URL varchar(255),
TIMESTAMP BIGINT NOT NULL,
SPEC_VERSION varchar(255),
STATUS varchar(255) NOT NULL,
PRIMARY KEY (SUBSCRIPTION_ID)
);

CREATE TABLE OB_NOTIFICATION_SUBSCRIPTION_EVENTS (
SUBSCRIPTION_ID varchar(36) NOT NULL,
EVENT_TYPE varchar(255) NOT NULL,
PRIMARY KEY (SUBSCRIPTION_ID, EVENT_TYPE),
CONSTRAINT FK_NotificationSubEvents FOREIGN KEY (SUBSCRIPTION_ID) REFERENCES OB_NOTIFICATION_SUBSCRIPTION(SUBSCRIPTION_ID)
);


Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- For event notifications feature run the following queries against the openbank_openbankingdb--

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION (
NOTIFICATION_ID varchar(36) NOT NULL,
CLIENT_ID varchar(255) NOT NULL,
RESOURCE_ID varchar(255) NOT NULL,
STATUS varchar(10) NOT NULL,
UPDATED_TIMESTAMP TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (NOTIFICATION_ID)
);

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION_EVENT (
EVENT_ID SERIAL PRIMARY KEY,
NOTIFICATION_ID varchar(36) NOT NULL,
EVENT_TYPE varchar(200) NOT NULL,
EVENT_INFO varchar(1000) NOT NULL,
CONSTRAINT FK_NotificationEvent FOREIGN KEY (NOTIFICATION_ID) REFERENCES OB_NOTIFICATION(NOTIFICATION_ID)
);

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION_ERROR (
NOTIFICATION_ID varchar(36) NOT NULL,
ERROR_CODE varchar(255) NOT NULL,
DESCRIPTION varchar(255) NOT NULL,
PRIMARY KEY (NOTIFICATION_ID),
CONSTRAINT FK_NotificationError FOREIGN KEY (NOTIFICATION_ID) REFERENCES OB_NOTIFICATION(NOTIFICATION_ID)
);

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION_SUBSCRIPTION (
SUBSCRIPTION_ID varchar(36) NOT NULL,
CLIENT_ID varchar(255) NOT NULL,
REQUEST JSON NOT NULL,
CALLBACK_URL varchar(255),
TIMESTAMP BIGINT NOT NULL,
SPEC_VERSION varchar(255),
STATUS varchar(255) NOT NULL,
PRIMARY KEY (SUBSCRIPTION_ID)
);

CREATE TABLE IF NOT EXISTS OB_NOTIFICATION_SUBSCRIPTION_EVENTS (
SUBSCRIPTION_ID varchar(36) NOT NULL,
EVENT_TYPE varchar(255) NOT NULL,
PRIMARY KEY (SUBSCRIPTION_ID, EVENT_TYPE),
CONSTRAINT FK_NotificationSubEvents FOREIGN KEY (SUBSCRIPTION_ID) REFERENCES OB_NOTIFICATION_SUBSCRIPTION(SUBSCRIPTION_ID)
);
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +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 org.springframework.http.HttpStatus;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -65,13 +65,13 @@ public EventSubscriptionResponse createEventSubscription(EventSubscriptionDTO ev
try {
EventSubscription createEventSubscriptionResponse = eventSubscriptionService.
createEventSubscription(eventSubscription);
eventSubscriptionResponse.setStatus(HttpStatus.CREATED_201);
eventSubscriptionResponse.setStatus(HttpStatus.CREATED.value());
eventSubscriptionResponse.
setResponseBody(mapSubscriptionModelToResponseJson(createEventSubscriptionResponse));
return eventSubscriptionResponse;
} catch (OBEventNotificationException e) {
log.error("Error occurred while creating event subscription", e);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST, e.getMessage()));
return eventSubscriptionResponse;
Expand Down Expand Up @@ -99,17 +99,17 @@ public EventSubscriptionResponse getEventSubscription(String clientId, String su
try {
EventSubscription eventSubscription = eventSubscriptionService.
getEventSubscriptionBySubscriptionId(subscriptionId);
eventSubscriptionResponse.setStatus(HttpStatus.OK_200);
eventSubscriptionResponse.setStatus(HttpStatus.OK.value());
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(HttpStatus.BAD_REQUEST_400);
eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST, e.getMessage()));
} else {
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST, e.getMessage()));
}
Expand Down Expand Up @@ -139,12 +139,12 @@ public EventSubscriptionResponse getAllEventSubscriptions(String clientId) {
for (EventSubscription eventSubscription : eventSubscriptionList) {
eventSubscriptionResponseList.add(mapSubscriptionModelToResponseJson(eventSubscription));
}
eventSubscriptionResponse.setStatus(HttpStatus.OK_200);
eventSubscriptionResponse.setStatus(HttpStatus.OK.value());
eventSubscriptionResponse.setResponseBody(eventSubscriptionResponseList);
return eventSubscriptionResponse;
} catch (OBEventNotificationException e) {
log.error("Error occurred while retrieving event subscriptions", e);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST, e.getMessage()));
return eventSubscriptionResponse;
Expand Down Expand Up @@ -175,12 +175,12 @@ public EventSubscriptionResponse getEventSubscriptionsByEventType(String clientI
for (EventSubscription eventSubscription : eventSubscriptionList) {
eventSubscriptionResponseList.add(mapSubscriptionModelToResponseJson(eventSubscription));
}
eventSubscriptionResponse.setStatus(HttpStatus.OK_200);
eventSubscriptionResponse.setStatus(HttpStatus.OK.value());
eventSubscriptionResponse.setResponseBody(eventSubscriptionResponseList);
return eventSubscriptionResponse;
} catch (OBEventNotificationException e) {
log.error("Error occurred while retrieving event subscriptions", e);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST, e.getMessage()));
return eventSubscriptionResponse;
Expand Down Expand Up @@ -208,21 +208,21 @@ public EventSubscriptionResponse updateEventSubscription(EventSubscriptionDTO ev
try {
Boolean isUpdated = eventSubscriptionService.updateEventSubscription(eventSubscription);
if (!isUpdated) {
eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400);
eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST,
"Event subscription not found."));
return eventSubscriptionResponse;
}
eventSubscriptionResponse.setStatus(HttpStatus.OK_200);
eventSubscriptionResponse.setStatus(HttpStatus.OK.value());
EventSubscription eventSubscriptionUpdateResponse = eventSubscriptionService.
getEventSubscriptionBySubscriptionId(eventSubscriptionUpdateRequestDto.getSubscriptionId());
eventSubscriptionResponse.
setResponseBody(mapSubscriptionModelToResponseJson(eventSubscriptionUpdateResponse));
return eventSubscriptionResponse;
} catch (OBEventNotificationException e) {
log.error("Error occurred while updating event subscription", e);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST, e.getMessage()));
return eventSubscriptionResponse;
Expand All @@ -247,17 +247,17 @@ public EventSubscriptionResponse deleteEventSubscription(String clientId, String
try {
Boolean isDeleted = eventSubscriptionService.deleteEventSubscription(subscriptionId);
if (!isDeleted) {
eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400);
eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST,
"Event subscription not found"));
return eventSubscriptionResponse;
}
eventSubscriptionResponse.setStatus(HttpStatus.NO_CONTENT_204);
eventSubscriptionResponse.setStatus(HttpStatus.NO_CONTENT.value());
return eventSubscriptionResponse;
} catch (OBEventNotificationException e) {
log.error("Error occurred while deleting event subscription", e);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
eventSubscriptionResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST, e.getMessage()));
return eventSubscriptionResponse;
Expand All @@ -277,7 +277,7 @@ private EventSubscriptionResponse validateClientId(String clientId) {
} catch (OBEventNotificationException e) {
log.error("Invalid client ID", e);
EventSubscriptionResponse eventSubscriptionResponse = new EventSubscriptionResponse();
eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST_400);
eventSubscriptionResponse.setStatus(HttpStatus.BAD_REQUEST.value());
eventSubscriptionResponse.setErrorResponse(EventNotificationServiceUtil.getErrorDTO(
EventNotificationConstants.INVALID_REQUEST, e.getMessage()));
return eventSubscriptionResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ public static Response mapEventPollingServiceResponse(EventPollingResponse event

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(eventPollingResponse.getResponseBody()).build();
} else {
if (eventPollingResponse.getErrorResponse() instanceof String) {
return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO(
EventNotificationEndPointConstants.INVALID_REQUEST,
eventPollingResponse.getErrorResponse().toString())).build();
return Response.status(getErrorResponseStatus(eventPollingResponse.getStatus()))
.entity(EventNotificationUtils.getErrorDTO(EventNotificationEndPointConstants.INVALID_REQUEST,
eventPollingResponse.getErrorResponse().toString())).build();
} else {
return Response.status(Response.Status.BAD_REQUEST).entity(eventPollingResponse.getErrorResponse())
return Response.status(getErrorResponseStatus(eventPollingResponse.getStatus()))
.entity(eventPollingResponse.getErrorResponse())
.build();
}
}
Expand All @@ -120,4 +123,20 @@ public static EventNotificationErrorDTO getErrorDTO(String error, String errorDe
eventNotificationErrorDTO.setErrorDescription(errorDescription);
return eventNotificationErrorDTO;
}

/**
* Get mapped Response.Status for the given status value.
* @param status status value
* @return Mapped Response.Status
*/
private static Response.Status getErrorResponseStatus(String status) {

if (EventNotificationConstants.NOT_FOUND.equals(status)) {
return Response.Status.NOT_FOUND;
} else if (EventNotificationConstants.BAD_REQUEST.equals(status)) {
return Response.Status.BAD_REQUEST;
} else {
return Response.Status.INTERNAL_SERVER_ERROR;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public static JSONObject getJSONObjectPayload(HttpServletRequest request) throws
*/
public static Response mapEventSubscriptionServiceResponse(EventSubscriptionResponse eventSubscriptionResponse) {
int status = eventSubscriptionResponse.getStatus();
if (eventSubscriptionResponse.getErrorResponse() == null) {
if (HttpStatus.NO_CONTENT_204 == status) {
return Response.status(status)
.build();
} else if (eventSubscriptionResponse.getErrorResponse() == null) {
if (eventSubscriptionResponse.getResponseBody() != null) {
return Response.status(status)
.entity(eventSubscriptionResponse.getResponseBody())
Expand All @@ -92,6 +95,5 @@ public static Response mapEventSubscriptionServiceResponse(EventSubscriptionResp
.entity(eventSubscriptionResponse.getErrorResponse())
.build();
}

}
}

0 comments on commit 19d7949

Please sign in to comment.