-
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.
Merge branch 'feature/190-improve-notification-subscription' into dev…
…elop
- Loading branch information
Showing
11 changed files
with
288 additions
and
11 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
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
11 changes: 11 additions & 0 deletions
11
.../src/main/java/fr/ght1pc9kc/baywatch/notify/domain/ports/NotificationPersistencePort.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,11 @@ | ||
package fr.ght1pc9kc.baywatch.notify.domain.ports; | ||
|
||
import fr.ght1pc9kc.baywatch.notify.api.model.ServerEvent; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface NotificationPersistencePort { | ||
Mono<ServerEvent> persist(ServerEvent event); | ||
|
||
Flux<ServerEvent> consume(String userId); | ||
} |
76 changes: 76 additions & 0 deletions
76
...main/java/fr/ght1pc9kc/baywatch/notify/infra/adapters/NotificationPersistenceAdapter.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,76 @@ | ||
package fr.ght1pc9kc.baywatch.notify.infra.adapters; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.type.MapLikeType; | ||
import fr.ght1pc9kc.baywatch.common.infra.DatabaseQualifier; | ||
import fr.ght1pc9kc.baywatch.dsl.tables.records.NotificationsRecord; | ||
import fr.ght1pc9kc.baywatch.notify.api.model.BasicEvent; | ||
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 lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jooq.DSLContext; | ||
import org.springframework.stereotype.Repository; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Scheduler; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static fr.ght1pc9kc.baywatch.dsl.tables.Notifications.NOTIFICATIONS; | ||
|
||
@Slf4j | ||
@Repository | ||
@RequiredArgsConstructor | ||
public class NotificationPersistenceAdapter implements NotificationPersistencePort { | ||
private final @DatabaseQualifier Scheduler databaseScheduler; | ||
private final DSLContext dsl; | ||
private final ObjectMapper jsonMapper; | ||
|
||
@Override | ||
public Mono<ServerEvent> persist(ServerEvent event) { | ||
return null; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings({"BlockingMethodInNonBlockingContext", "resource"}) | ||
public Flux<ServerEvent> consume(String userId) { | ||
var select = dsl.selectFrom(NOTIFICATIONS).where(NOTIFICATIONS.NOTI_USER_ID.eq(userId)); | ||
|
||
return Flux.<NotificationsRecord>create(sink -> { | ||
var cursor = select.fetchLazy(); | ||
sink.onRequest(n -> { | ||
int count = (int) n; | ||
var rs = cursor.fetchNext(count); | ||
rs.forEach(sink::next); | ||
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); | ||
} | ||
|
||
private ServerEvent buildServerEvent(NotificationsRecord noti) { | ||
MapLikeType dataType = jsonMapper.getTypeFactory().constructMapLikeType( | ||
Map.class, String.class, Object.class); | ||
Map<String, String> message = Optional.ofNullable(noti.getNotiData()).map(data -> { | ||
try { | ||
return jsonMapper.readValue(noti.getNotiData(), dataType); | ||
} catch (Exception e) { | ||
return Map.of("message", data); | ||
} | ||
}).orElse(Map.of()); | ||
return new BasicEvent<>( | ||
noti.getNotiId(), | ||
EventType.valueOf(noti.getNotiEventType()), | ||
message); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
sandside/src/main/resources/db/migration/V2_1_202312091459__add_notifications.sql
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,11 @@ | ||
create table NOTIFICATIONS | ||
( | ||
NOTI_ID VARCHAR(64) NOT NULL PRIMARY KEY, | ||
NOTI_USER_ID VARCHAR(64) NOT NULL, | ||
NOTI_EVENT_TYPE VARCHAR(20) NOT NULL, | ||
NOTI_DATA TEXT, | ||
NOTI_CREATED_AT DATETIME, | ||
|
||
constraint FK_NOTI_USER_ID | ||
foreign key (NOTI_USER_ID) references USERS (USER_ID) | ||
); |
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(); | ||
} | ||
} |
Oops, something went wrong.