Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding unit tests for notification module #216

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Copyright (c) 2024, 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.
*/

-- All the data related to time are stored in unix time stamp and therefore, the data types for the time related data
-- are represented in BIGINT.
-- Since the database systems does not support adding default unix time to the database columns, the default data
-- storing is handled within the database querieS.

-- For event notifications feature run the following queries against the openbank_openbankingdb--

CREATE TABLE IF NOT EXISTS FS_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 BIGINT NOT NULL,
PRIMARY KEY (NOTIFICATION_ID)
)
ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS FS_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 FS_NOTIFICATION(NOTIFICATION_ID)
)
ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS FS_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 FS_NOTIFICATION(NOTIFICATION_ID)
)
ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS FS_NOTIFICATION_SUBSCRIPTION (
SUBSCRIPTION_ID VARCHAR(36) NOT NULL,
CLIENT_ID VARCHAR(255) NOT NULL,
REQUEST CLOB 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 FS_NOTIFICATION_SUBSCRIBED_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 FS_NOTIFICATION_SUBSCRIPTION(SUBSCRIPTION_ID)
)
ENGINE=InnoDB;
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CREATE TABLE FS_NOTIFICATION (
CLIENT_ID varchar(255) NOT NULL,
RESOURCE_ID varchar(255) NOT NULL,
STATUS varchar(10) NOT NULL,
UPDATED_TIMESTAMP DATETIME2(0) DEFAULT GETDATE(),
UPDATED_TIMESTAMP BIGINT NOT NULL,
PRIMARY KEY (NOTIFICATION_ID)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS FS_NOTIFICATION (
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,
UPDATED_TIMESTAMP BIGINT NOT NULL,
PRIMARY KEY (NOTIFICATION_ID)
)
ENGINE=InnoDB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CREATE TABLE FS_NOTIFICATION (
CLIENT_ID varchar2(255) NOT NULL,
RESOURCE_ID varchar2(255) NOT NULL,
STATUS varchar2(10) NOT NULL,
UPDATED_TIMESTAMP TIMESTAMP(0) DEFAULT SYSTIMESTAMP,
UPDATED_TIMESTAMP BIGINT NOT NULL,
PRIMARY KEY (NOTIFICATION_ID)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ CREATE TABLE IF NOT EXISTS FS_NOTIFICATION (
CLIENT_ID varchar(255) NOT NULL,
RESOURCE_ID varchar(255) NOT NULL,
STATUS varchar(10) NOT NULL,
UPDATED_TIMESTAMP TIMESTAMP(0) DEFAULT CURRENT_TIMESTAMP,
UPDATED_TIMESTAMP BIGINT NOT NULL,
PRIMARY KEY (NOTIFICATION_ID)
);

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -69,83 +69,88 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database.wso2</groupId>
<artifactId>h2-database-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!--Todo: Adding frm another PR -->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-surefire-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <suiteXmlFiles>-->
<!-- <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>-->
<!-- </suiteXmlFiles>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- &lt;!&ndash; last version compatible with Maven 2: latest is configured in profile &ndash;&gt;-->
<!-- <groupId>org.jacoco</groupId>-->
<!-- <artifactId>jacoco-maven-plugin</artifactId>-->
<!-- <version>${jacoco.version}</version>-->
<!-- <configuration>-->
<!-- <excludes>-->
<!-- &lt;!&ndash;Excluding them in coverage reports&ndash;&gt;-->
<!-- <exclude>**/*Constants.class</exclude>-->
<!-- <exclude>**/*Component.class</exclude>-->
<!-- <exclude>**/*DataHolder.class</exclude>-->
<!-- <exclude>*SqlStatements.*</exclude>-->
<!-- <exclude>**/exceptions/*</exclude>-->
<!-- <exclude>**/StoreInitializer/*</exclude>-->
<!-- </excludes>-->
<!-- </configuration>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>default-prepare-agent</id>-->
<!-- <goals>-->
<!-- <goal>prepare-agent</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- <execution>-->
<!-- <id>default-prepare-agent-integration</id>-->
<!-- <goals>-->
<!-- <goal>prepare-agent-integration</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- <execution>-->
<!-- <id>default-report</id>-->
<!-- <goals>-->
<!-- <goal>report</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- <execution>-->
<!-- <id>default-report-integration</id>-->
<!-- <goals>-->
<!-- <goal>report-integration</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- <execution>-->
<!-- <id>default-check</id>-->
<!-- <goals>-->
<!-- <goal>check</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <rules>-->
<!-- <rule implementation="org.jacoco.maven.RuleConfiguration">-->
<!-- <element>BUNDLE</element>-->
<!-- <limits>-->
<!-- <limit implementation="org.jacoco.report.check.Limit">-->
<!-- <counter>INSTRUCTION</counter>-->
<!-- <value>COVEREDRATIO</value>-->
<!-- <minimum>0.8</minimum>-->
<!-- </limit>-->
<!-- </limits>-->
<!-- </rule>-->
<!-- </rules>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<!-- last version compatible with Maven 2: latest is configured in profile -->
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<!--Excluding them in coverage reports-->
<exclude>**/*Constants.class</exclude>
<exclude>**/*Component.class</exclude>
<exclude>**/*DataHolder.class</exclude>
<exclude>**/*SqlStatements.class</exclude>
<exclude>**/*Exception.class</exclude>
<exclude>**/EventNotificationStoreInitializer.class</exclude>
<exclude>**/PostgreSqlEventNotificationDAOImpl.class</exclude>
<exclude>**/PostgreSqlEventSubscriptionDAOImpl.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.8</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public class EventCreationService {
public String publishEventNotification(NotificationCreationDTO notificationCreationDTO)
throws FSEventNotificationException {

if (notificationCreationDTO.getClientId() == null) {
log.error("Mandatory field client id is null");
throw new FSEventNotificationException("Mandatory field client id is null");
}

if (notificationCreationDTO.getResourceId() == null) {
log.error("Mandatory field resource id is null");
throw new FSEventNotificationException("Mandatory field resource id is null");
}

if (notificationCreationDTO.getEventPayload() == null) {
log.error("Mandatory field payload is null");
throw new FSEventNotificationException("Mandatory field payload is null");
}

Connection connection = DatabaseUtils.getDBConnection();
Notification notification = getNotification(notificationCreationDTO);
ArrayList<NotificationEvent> eventsList = getEvents(notificationCreationDTO.getEventPayload());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ public class EventSubscriptionService {
public EventSubscription createEventSubscription(EventSubscription eventSubscription)
throws FSEventNotificationException {

if (eventSubscription.getClientId() == null) {
log.error("Mandatory field client id is null");
throw new FSEventNotificationException("Mandatory field client id is null");
}

if (eventSubscription.getRequestData() == null) {
log.error("Mandatory field request data is null");
throw new FSEventNotificationException("Mandatory field request data is null");
}

if (eventSubscription.getStatus() == null) {
log.error("Mandatory field status is null");
throw new FSEventNotificationException("Mandatory field status is null");
}

EventSubscriptionDAO eventSubscriptionDAO = EventNotificationStoreInitializer.getEventSubscriptionDAO();

Connection connection = DatabaseUtils.getDBConnection();
Expand Down Expand Up @@ -85,6 +100,11 @@ public EventSubscription createEventSubscription(EventSubscription eventSubscrip
public EventSubscription getEventSubscriptionBySubscriptionId(String subscriptionId)
throws FSEventNotificationException {

if (subscriptionId == null) {
log.error("Mandatory field subscriptionId is null");
throw new FSEventNotificationException("Mandatory field subscriptionId is null");
}

EventSubscriptionDAO eventSubscriptionDAO = EventNotificationStoreInitializer.getEventSubscriptionDAO();

Connection connection = DatabaseUtils.getDBConnection();
Expand Down Expand Up @@ -118,6 +138,11 @@ public EventSubscription getEventSubscriptionBySubscriptionId(String subscriptio
public List<EventSubscription> getEventSubscriptionsByClientId(String clientId)
throws FSEventNotificationException {

if (clientId == null) {
log.error("Mandatory field clientId is null");
throw new FSEventNotificationException("Mandatory field clientId is null");
}

Connection connection = DatabaseUtils.getDBConnection();
try {
EventSubscriptionDAO eventSubscriptionDAO = EventNotificationStoreInitializer.getEventSubscriptionDAO();
Expand Down Expand Up @@ -149,6 +174,10 @@ public List<EventSubscription> getEventSubscriptionsByClientId(String clientId)
public List<EventSubscription> getEventSubscriptionsByEventType(String eventType)
throws FSEventNotificationException {

if (eventType == null) {
log.error("Mandatory field eventType is null");
throw new FSEventNotificationException("Mandatory field eventType is null");
}

Connection connection = DatabaseUtils.getDBConnection();
try {
Expand Down Expand Up @@ -181,6 +210,26 @@ public List<EventSubscription> getEventSubscriptionsByEventType(String eventType
public Boolean updateEventSubscription(EventSubscription eventSubscription)
throws FSEventNotificationException {

if (eventSubscription.getSubscriptionId() == null) {
log.error("Mandatory field subscription id is null");
throw new FSEventNotificationException("Mandatory field subscription id is null");
}

if (eventSubscription.getClientId() == null) {
log.error("Mandatory field client id is null");
throw new FSEventNotificationException("Mandatory field client id is null");
}

if (eventSubscription.getRequestData() == null) {
log.error("Mandatory field request data is null");
throw new FSEventNotificationException("Mandatory field request data is null");
}

if (eventSubscription.getStatus() == null) {
log.error("Mandatory field status is null");
throw new FSEventNotificationException("Mandatory field status is null");
}

Connection connection = DatabaseUtils.getDBConnection();

EventSubscriptionDAO eventSubscriptionDAO = EventNotificationStoreInitializer.getEventSubscriptionDAO();
Expand Down Expand Up @@ -241,6 +290,11 @@ public Boolean updateEventSubscription(EventSubscription eventSubscription)
*/
public Boolean deleteEventSubscription(String subscriptionId) throws FSEventNotificationException {

if (subscriptionId == null) {
log.error("Mandatory field subscriptionId is null");
throw new FSEventNotificationException("Mandatory field subscriptionId is null");
}

Connection connection = DatabaseUtils.getDBConnection();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public class EventNotificationConstants {
public static final String CALLBACK_URL_PARAM = "callbackUrl";
public static final String VERSION_PARAM = "version";
public static final String EVENT_TYPES_PARAM = "eventTypes";
public static final String EVENT_TYPE_PARAM = "eventType";
public static final String DATA_PARAM = "data";

public static final String DB_ERROR_UPDATING = "Database error while updating notification with ID : " +
"'%s' in the database. ";
Expand Down
Loading
Loading