Skip to content

Commit

Permalink
test(sandside): #190 add unit test for notification service
Browse files Browse the repository at this point in the history
refs #190
  • Loading branch information
Marthym committed Dec 10, 2023
1 parent 8862198 commit e99e34c
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ public Flux<ServerEvent> consume(String userId) {
int count = (int) n;
var rs = cursor.fetchNext(count);
rs.forEach(sink::next);
dsl.deleteFrom(NOTIFICATIONS).where(NOTIFICATIONS.NOTI_ID.eq(rs.field(NOTIFICATIONS.NOTI_ID)))
.execute();
if (rs.size() < count) {
sink.complete();
}
}).onDispose(cursor::close);
}).limitRate(Integer.MAX_VALUE - 1).subscribeOn(databaseScheduler)
.flatMap(r -> Mono.fromCallable(() ->
dsl.deleteFrom(NOTIFICATIONS)
.where(NOTIFICATIONS.NOTI_ID.eq(r.getNotiId()))
.execute())
.then(Mono.just(r)))
.map(this::buildServerEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import fr.ght1pc9kc.baywatch.notify.api.model.EventType;
import fr.ght1pc9kc.baywatch.notify.api.model.ServerEvent;
import fr.ght1pc9kc.baywatch.notify.domain.ports.NotificationPersistencePort;
import fr.ght1pc9kc.baywatch.security.api.AuthenticationFacade;
import fr.ght1pc9kc.baywatch.tests.samples.UserSamples;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -32,7 +35,9 @@ void setUp() {
Mono.just(UserSamples.LUKE),
Mono.just(UserSamples.OBIWAN),
Mono.just(UserSamples.LUKE));
tested = new NotifyServiceImpl(authFacadeMock);
NotificationPersistencePort notificationPersistenceMock = mock(NotificationPersistencePort.class);
when(notificationPersistenceMock.consume(anyString())).thenReturn(Flux.empty());
tested = new NotifyServiceImpl(authFacadeMock, notificationPersistenceMock);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.ght1pc9kc.baywatch.notify.infra.adapters;

import com.fasterxml.jackson.databind.ObjectMapper;
import fr.ght1pc9kc.baywatch.dsl.tables.Notifications;
import fr.ght1pc9kc.baywatch.notify.api.model.EventType;
import fr.ght1pc9kc.baywatch.notify.api.model.ServerEvent;
import fr.ght1pc9kc.baywatch.notify.domain.ports.NotificationPersistencePort;
import fr.ght1pc9kc.baywatch.notify.infra.samples.NotificationsRecordSamples;
import fr.ght1pc9kc.baywatch.tests.samples.UserSamples;
import fr.ght1pc9kc.testy.core.extensions.ChainedExtension;
import fr.ght1pc9kc.testy.jooq.WithDslContext;
import fr.ght1pc9kc.testy.jooq.WithInMemoryDatasource;
import fr.ght1pc9kc.testy.jooq.WithSampleDataLoaded;
import org.jooq.DSLContext;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;

import static org.assertj.core.api.Assertions.assertThat;

class NotificationPersistenceAdapterTest {
private static final WithInMemoryDatasource wDs = WithInMemoryDatasource.builder().build();
private static final WithDslContext wDslContext = WithDslContext.builder()
.setDatasourceExtension(wDs).build();
private static final WithSampleDataLoaded wSamples = WithSampleDataLoaded.builder(wDslContext)
.createTablesIfNotExists()
.addDataset(NotificationsRecordSamples.SAMPLE)
.build();

@RegisterExtension
@SuppressWarnings("unused")
static ChainedExtension chain = ChainedExtension.outer(wDs)
.append(wDslContext)
.append(wSamples)
.register();

private NotificationPersistencePort tested;

@BeforeEach
void setUp(DSLContext dsl) {
tested = new NotificationPersistenceAdapter(Schedulers.immediate(), dsl, new ObjectMapper());
}

@Test
void should_consume_notifications(DSLContext dsl) {
assertThat(dsl.fetchCount(Notifications.NOTIFICATIONS)).isNotZero();

Flux<ServerEvent> actuals = tested.consume(UserSamples.OBIWAN.id);

StepVerifier.create(actuals)
.assertNext(actual -> Assertions.assertAll(
() -> assertThat(actual.id()).isEqualTo("EV01HHA6PFESHHFK4YHT1T2HKHSR"),
() -> assertThat(actual.type()).isEqualTo(EventType.USER_NOTIFICATION)
)).verifyComplete();

assertThat(dsl.fetchCount(Notifications.NOTIFICATIONS)).isOne();
}

@Test
void should_consume_dummy_notifications(DSLContext dsl) {
assertThat(dsl.fetchCount(Notifications.NOTIFICATIONS)).isNotZero();

Flux<ServerEvent> actuals = tested.consume(UserSamples.MWINDU.id);

StepVerifier.create(actuals)
.assertNext(actual -> Assertions.assertAll(
() -> assertThat(actual.id()).isEqualTo("EV01HHA6PFESHHFK4YHT1T2HKHSQ"),
() -> assertThat(actual.type()).isEqualTo(EventType.USER_NOTIFICATION)
)).verifyComplete();

assertThat(dsl.fetchCount(Notifications.NOTIFICATIONS)).isOne();
}

@Test
void should_not_consume_notifications(DSLContext dsl) {
assertThat(dsl.fetchCount(Notifications.NOTIFICATIONS)).isNotZero();

Flux<ServerEvent> actuals = tested.consume(UserSamples.DSIDIOUS.id);

StepVerifier.create(actuals).verifyComplete();

assertThat(dsl.fetchCount(Notifications.NOTIFICATIONS)).isNotZero();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package fr.ght1pc9kc.baywatch.notify.infra.samples;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.ght1pc9kc.baywatch.dsl.tables.records.NotificationsRecord;
import fr.ght1pc9kc.baywatch.notify.api.model.EventType;
import fr.ght1pc9kc.baywatch.notify.api.model.Severity;
import fr.ght1pc9kc.baywatch.notify.api.model.UserNotification;
import fr.ght1pc9kc.baywatch.tests.samples.FeedSamples;
import fr.ght1pc9kc.baywatch.tests.samples.UserSamples;
import fr.ght1pc9kc.testy.jooq.model.RelationalDataSet;

import java.time.LocalDateTime;
import java.util.List;

import static fr.ght1pc9kc.baywatch.dsl.tables.Notifications.NOTIFICATIONS;

public class NotificationsRecordSamples implements RelationalDataSet<NotificationsRecord> {
public static final NotificationsRecordSamples SAMPLE = new NotificationsRecordSamples();

public static final UserNotification USER_NOTIFICATION = UserNotification.builder()
.code(UserNotification.CODE_NEWS_ADD)
.severity(Severity.info)
.title("The new light saber was arrived")
.message("Hello your new light saber was arrived, you can go to get it !")
.actions("VSC")
.target(FeedSamples.JEDI.id)
.build();

public static final NotificationsRecord DUMMY_NOTIFICATION_RECORD = NOTIFICATIONS.newRecord()
.setNotiId("EV01HHA6PFESHHFK4YHT1T2HKHSQ")
.setNotiUserId(UserSamples.MWINDU.id)
.setNotiEventType(EventType.USER_NOTIFICATION.name())
.setNotiData("String")
.setNotiCreatedAt(LocalDateTime.now());
public static final NotificationsRecord USER_NOTIFICATIONS_RECORD;

static {
ObjectMapper json = new ObjectMapper();
try {
USER_NOTIFICATIONS_RECORD = NOTIFICATIONS.newRecord()
.setNotiId("EV01HHA6PFESHHFK4YHT1T2HKHSR")
.setNotiUserId(UserSamples.OBIWAN.id)
.setNotiEventType(EventType.USER_NOTIFICATION.name())
.setNotiData(json.writeValueAsString(USER_NOTIFICATION))
.setNotiCreatedAt(LocalDateTime.now());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@Override
public List<NotificationsRecord> records() {
return List.of(USER_NOTIFICATIONS_RECORD, DUMMY_NOTIFICATION_RECORD);
}
}

0 comments on commit e99e34c

Please sign in to comment.