-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(sandside): #190 add unit test for notification service
refs #190
- Loading branch information
Showing
4 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
.../java/fr/ght1pc9kc/baywatch/notify/infra/adapters/NotificationPersistenceAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
.../src/test/java/fr/ght1pc9kc/baywatch/notify/infra/samples/NotificationsRecordSamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |