Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
notification-service: add more integration tests
Browse files Browse the repository at this point in the history
Add more integration tests to validate that:
1. Auth works as expected in the API
2. NotificationMessage User or Group validation in create API
  • Loading branch information
nirarg committed May 9, 2023
1 parent 32524c5 commit 800a1aa
Showing 1 changed file with 141 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.redhat.parodos.integration.notification;

import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.redhat.parodos.notification.sdk.api.*;
import com.redhat.parodos.notification.sdk.model.NotificationMessageCreateRequestDTO;
Expand All @@ -18,6 +19,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

@Slf4j
public class NotificationRecord {
Expand All @@ -40,70 +42,170 @@ public void setUp() throws IOException {
@Test
public void notificationServiceHappyHappyFlow() {
String testName = "notificationServiceHappyHappyFlow";
logTestStep(testName, "Create the notification messages");
createNotificationMessage("type0", "body0", "subject0", "test");
createNotificationMessage("type1", "body1", "subject1", "test");
createNotificationMessage("type2", "body2", "subject2", "test");
createNotificationMessage("type3", "body3", "subject3", "test2");

validateNotificationRecordCount(testName, 3);
assertDoesNotThrow(() -> {
PageNotificationRecordResponseDTO list = validateNotificationRecordList(testName, 3);
NotificationRecordResponseDTO notificationRecord = list.getContent().get(0);
createNotificationMessage(testName, "type0", "body0", "subject0", List.of("test"));
createNotificationMessage(testName, "type1", "body1", "subject1", List.of("test"));
createNotificationMessage(testName, "type2", "body2", "subject2", List.of("test"));
createNotificationMessage(testName, "type3", "body3", "subject3", List.of("test2"));

countNotificationRecord(testName, 3);
PageNotificationRecordResponseDTO list1 = listNotificationRecord(testName, 3);
NotificationRecordResponseDTO notificationRecord1 = list1.getContent().get(0);
logTestStep(testName, "Update one Notification Record as \"READ\"");
this.recordApiInstance.updateNotificationStatusById(notificationRecord.getId(), "READ");
updateNotificationRecord(testName, notificationRecord1.getId(), "READ");

countNotificationRecord(testName, 2);
PageNotificationRecordResponseDTO list2 = listNotificationRecord(testName, 3);
NotificationRecordResponseDTO notificationRecord2 = list2.getContent().get(1);
deleteNotificationRecord(testName, notificationRecord2.getId());

countNotificationRecord(testName, 1);
listNotificationRecord(testName, 2);
});
}

validateNotificationRecordCount(testName, 2);
assertDoesNotThrow(() -> {
PageNotificationRecordResponseDTO list = validateNotificationRecordList(testName, 3);
NotificationRecordResponseDTO notificationRecord = list.getContent().get(1);
this.recordApiInstance.deleteNotification(notificationRecord.getId());
@Test
public void notificationServiceCreateMissingUsernames() {
String testName = "notificationServiceCreateMissingUsernames";

ApiException e = assertThrows(ApiException.class, () -> {
createNotificationMessage(testName, "type0", "body0", "subject0", null);
});
assertEquals(400, e.getCode());
}

validateNotificationRecordCount(testName, 1);
assertDoesNotThrow(() -> {
PageNotificationRecordResponseDTO list = validateNotificationRecordList(testName, 2);
@Test
public void notificationServiceCreateEmptyUsernames() {
String testName = "notificationServiceCreateMissingUsernames";

ApiException e = assertThrows(ApiException.class, () -> {
createNotificationMessage(testName, "type0", "body0", "subject0", new ArrayList());
});
assertEquals(400, e.getCode());
}

@Test
public void notificationServiceCreateAuthErr() {
String testName = "notificationServiceCreateAuthErr";

ApiClient apiClient = Configuration.getDefaultApiClient();
apiClient.addDefaultHeader(HttpHeaders.AUTHORIZATION, "");
apiClient.setBasePath(BASE_PATH);
this.messageApiInstance = new NotificationMessageApi(apiClient);

ApiException e = assertThrows(ApiException.class, () -> {
createNotificationMessage(testName, "type0", "body0", "subject0", List.of("test"));
});
assertEquals(401, e.getCode());
}

private void validateNotificationRecordCount(String testName, int notificationRecordsExpectedCount) {
logTestStep(testName, "Count Notification Records for the user");
assertDoesNotThrow(() -> {
Integer count = this.recordApiInstance.countUnreadNotifications("UNREAD");
log.info("Found ", count, "notification records for the user");
assertEquals(notificationRecordsExpectedCount, count.intValue());
@Test
public void notificationServiceGetAuthErr() {
String testName = "notificationServiceGetAuthErr";

ApiClient apiClient = Configuration.getDefaultApiClient();
apiClient.addDefaultHeader(HttpHeaders.AUTHORIZATION, "");
apiClient.setBasePath(BASE_PATH);
this.recordApiInstance = new NotificationRecordApi(apiClient);

ApiException e = assertThrows(ApiException.class, () -> {
listNotificationRecord(testName, 0);
});
assertEquals(401, e.getCode());
}

private PageNotificationRecordResponseDTO validateNotificationRecordList(String testName,
int notificationRecordsExpectedCount) throws ApiException {
logTestStep(testName, "List Notification Records for the user");
PageNotificationRecordResponseDTO result = this.recordApiInstance.getNotifications(new Pageable(), null, null);
List<NotificationRecordResponseDTO> content = result.getContent();
log.info(content.toString());
assertEquals(notificationRecordsExpectedCount, content.size());
return result;
@Test
public void notificationServiceCountAuthErr() {
String testName = "notificationServiceCountAuthErr";

ApiClient apiClient = Configuration.getDefaultApiClient();
apiClient.addDefaultHeader(HttpHeaders.AUTHORIZATION, "");
apiClient.setBasePath(BASE_PATH);
this.recordApiInstance = new NotificationRecordApi(apiClient);

ApiException e = assertThrows(ApiException.class, () -> {
countNotificationRecord(testName, 0);
});
assertEquals(401, e.getCode());
}

private void createNotificationMessage(String messageType, String body, String subject, String username) {
@Test
public void notificationServiceDeleteAuthErr() {
String testName = "notificationServiceDeleteAuthErr";

ApiClient apiClient = Configuration.getDefaultApiClient();
apiClient.addDefaultHeader(HttpHeaders.AUTHORIZATION, "");
apiClient.setBasePath(BASE_PATH);
this.recordApiInstance = new NotificationRecordApi(apiClient);

ApiException e = assertThrows(ApiException.class, () -> {
deleteNotificationRecord(testName, UUID.randomUUID());
});
assertEquals(401, e.getCode());
}

@Test
public void notificationServiceUpdateAuthErr() {
String testName = "notificationServiceUpdateAuthErr";

ApiClient apiClient = Configuration.getDefaultApiClient();
apiClient.addDefaultHeader(HttpHeaders.AUTHORIZATION, "");
apiClient.setBasePath(BASE_PATH);
this.recordApiInstance = new NotificationRecordApi(apiClient);

ApiException e = assertThrows(ApiException.class, () -> {
updateNotificationRecord(testName, UUID.randomUUID(), "READ");
});
assertEquals(401, e.getCode());
}

// Helper functions

private void createNotificationMessage(String testName, String messageType, String body, String subject,
List<String> usernames) throws ApiException {

logTestStep(testName, "Create the notification messages");
NotificationMessageCreateRequestDTO notificationMessageCreateRequestDTO = new NotificationMessageCreateRequestDTO();

notificationMessageCreateRequestDTO.messageType(messageType);
notificationMessageCreateRequestDTO.body(body);
notificationMessageCreateRequestDTO.subject(subject);
if (username != null) {
notificationMessageCreateRequestDTO.usernames(Arrays.asList(username));
}
notificationMessageCreateRequestDTO.usernames(usernames);

assertDoesNotThrow(() -> {
this.messageApiInstance.create(notificationMessageCreateRequestDTO);
});
this.messageApiInstance.create(notificationMessageCreateRequestDTO);
}

private void logTestStep(String testName, String stepDescription) {
log.info("############## " + testName + ": " + stepDescription);
}

private void countNotificationRecord(String testName, int notificationRecordsExpectedCount) throws ApiException {
logTestStep(testName, "Count Notification Records for the user");
Integer count = this.recordApiInstance.countUnreadNotifications("UNREAD");
log.info("Found ", count, "notification records for the user");
assertEquals(notificationRecordsExpectedCount, count.intValue());
}

private PageNotificationRecordResponseDTO listNotificationRecord(String testName,
int notificationRecordsExpectedCount) throws ApiException {
logTestStep(testName, "List Notification Records for the user");
PageNotificationRecordResponseDTO result = this.recordApiInstance.getNotifications(new Pageable(), null, null);
List<NotificationRecordResponseDTO> content = result.getContent();
log.info(content.toString());
assertEquals(notificationRecordsExpectedCount, content.size());
return result;
}

private void updateNotificationRecord(String testName, UUID notificationRecordId, String status)
throws ApiException {
logTestStep(testName, "Update Notification Records for the user");
this.recordApiInstance.updateNotificationStatusById(notificationRecordId, status);
}

private void deleteNotificationRecord(String testName, UUID notificationRecordId) throws ApiException {
logTestStep(testName, "Delete Notification Record");
this.recordApiInstance.deleteNotification(notificationRecordId);
}

}

0 comments on commit 800a1aa

Please sign in to comment.