Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Gorzala committed Mar 28, 2024
1 parent c5d9082 commit 10f49f0
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
public class ListenerAll {
public class StateRelatedEventListener {

private static final Logger log = LoggerFactory.getLogger(ListenerAll.class);
private static final Logger log = LoggerFactory.getLogger(StateRelatedEventListener.class);

private final ApplicationEventPublisher applicationEventPublisher;

Expand All @@ -26,8 +27,7 @@ public class ListenerAll {
@KafkaListener(topics = {
"message-posted",
"chat-created",
"message-read",
"profile-updated"
"message-read"
})
void listener(CloudEvent cloudEvent) throws IOException {
log.info("Got this event....");
Expand Down Expand Up @@ -62,7 +62,9 @@ private void messagePostedEvent(CloudEvent cloudEvent) throws IOException {
);
messagePostedEvent.setMessageId(messagePostedEventDto.messageId);
messagePostedEvent.setAuthorId(messagePostedEventDto.authorId);
messagePostedEvent.setRecipients(messagePostedEventDto.participantIds);
messagePostedEvent.setRecipients(
messagePostedEventDto.participantIds.stream().filter(p -> !p.equals(messagePostedEventDto.authorId)).collect(Collectors.toList())
);

applicationEventPublisher.publishEvent(messagePostedEvent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.dancier.kikeriki.adapter.out.infomail;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class InfoMailCheckJob {
private static final Logger log = LoggerFactory.getLogger(InfoMailCheckJob.class);

@Scheduled(fixedRate = 5000L)
public void recheck() {
log.info("rechecking");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.dancier.kikeriki.adapter.out.infomail;

import net.dancier.kikeriki.application.port.ScheduleInfomailCheck;

import java.time.LocalDateTime;

public class InfomailServiceAdapter implements ScheduleInfomailCheck {
@Override
public void schedule(LocalDateTime when, String dancerId) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.dancier.kikeriki.application;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import net.dancier.kikeriki.application.domain.model.state.State;
import net.dancier.kikeriki.application.port.StatePort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
public class CheckAndSendService {
private final Logger log = LoggerFactory.getLogger(CheckAndSendService.class);

private final StatePort statePort;

@Transactional
public void checkAndSend(String dancerId) {
log.info("CheckAndSend for {}", dancerId);
State state = statePort.get(dancerId);
if (state.isCandidateForSendingMail(LocalDateTime.now())) {
sendMail(dancerId);
statePort.save(state.toDto(), dancerId);
}
}
public void sendMail(String dancerId) {
log.info("Sending InfoMail...");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ public class StateRelatedApplicationEventListener {

@EventListener
public void handle(MessagePostedEvent messagePostedEvent) {
log.info("Handling MessagePostedEvent: " + messagePostedEvent);
log.info("With this content: {}", messagePostedEvent);
for (String recipientId: messagePostedEvent.getRecipients().stream().filter(r -> !r.equals(messagePostedEvent.getAuthorId())).collect(Collectors.toList())) {
log.info("Loading for: " + recipientId);
for (String recipientId: messagePostedEvent.getRecipients()) {
State state = statePort.get(recipientId);
log.info("Loaded: " + state);
state.addUnreadChatMessage(UnreadChatMessage.of(messagePostedEvent.getMessageId(), messagePostedEvent.getCreatedAt()));
statePort.save(state.toDto(),recipientId);
// schedule check in 30 minutes
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public void setLastMailMessage(@NonNull InfoMail infoMail) {
this.optLastTimeCustomerWasInformed = Optional.of(infoMail);
}

public Optional<LocalDateTime> getLastTimeOfInfomail() {
public Optional<LocalDateTime> getLastTimeOfInfoMail() {
return optLastTimeCustomerWasInformed.map(InfoMail::getCreatedAt);
}

public Boolean allowedToSendAnotherInfomail(LocalDate now) {
if (optLastTimeCustomerWasInformed.isEmpty()) {
return true;
} else {
return getLastTimeOfInfomail().get().toLocalDate().isBefore(now);
return getLastTimeOfInfoMail().get().toLocalDate().isBefore(now);
}
}

Expand All @@ -48,11 +48,11 @@ public Integer unreadMessagesCount() {
}

public Boolean hastUnreadMessagesSinceLastInfomail() {
if (getLastTimeOfInfomail().isPresent()) {
if (getLastTimeOfInfoMail().isPresent()) {
return this
.unreadChatMessages
.stream()
.anyMatch(unreadChatMessage -> unreadChatMessage.getCreatedAt().isAfter(getLastTimeOfInfomail().get()));
.anyMatch(unreadChatMessage -> unreadChatMessage.getCreatedAt().isAfter(getLastTimeOfInfoMail().get()));
} else {
return (unreadChatMessages.size() > 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.dancier.kikeriki.application.port;

import java.time.LocalDateTime;

public interface ScheduleInfomailCheck {
void schedule(LocalDateTime when, String dancerId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;


class SchedulingAndSendingMailWorks extends NeededInfrastructureBaseTestClass {
@Autowired
MailOutboxJpaRepository mailOutboxJpaRepository;

@Autowired
ApplicationEventPublisher applicationEventPublisher;
Expand All @@ -32,8 +29,7 @@ class SchedulingAndSendingMailWorks extends NeededInfrastructureBaseTestClass {

@Test
public void scheduleAndSendTest() {
List<MailOutboxJpaEntity> all = mailOutboxJpaRepository.findAll();
assertThat(all).isEmpty();
verify(javaMailSender, never()).send(any(SimpleMailMessage.class));

applicationEventPublisher.publishEvent(getEmailSendingRequestedCommandStub());

Expand Down
12 changes: 3 additions & 9 deletions src/test/java/net/dancier/kikeriki/StateIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.dancier.kikeriki.application.port.StatePort;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;

import java.time.LocalDateTime;
Expand All @@ -16,13 +15,6 @@ public class StateIntegrationTest extends NeededInfrastructureBaseTestClass {
@Autowired
ApplicationEventPublisher applicationEventPublisher;

@Value("${spring.datasource.url}")
private String datasource;

@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;


@Autowired
StatePort statePort;

Expand All @@ -36,8 +28,10 @@ public void newUnreadMessage() {
State resultingState = statePort.get(ApplicationEventStubbing.RECIPIENT_ID);

assertThat(resultingState.unreadMessagesCount()).isEqualTo(1);
assertThat(resultingState.getLastTimeOfInfomail()).isEmpty();
assertThat(resultingState.getLastTimeOfInfoMail()).isEmpty();
assertThat(resultingState.isCandidateForSendingMail(LocalDateTime.now())).isTrue();


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public void testTimeOfLastMailMessage() {
State state = new State();

// no message has been sent so the last MessageSentTimeStamp must by Optional.empty
assertThat(state.getLastTimeOfInfomail()).isEmpty();
assertThat(state.getLastTimeOfInfoMail()).isEmpty();

LocalDateTime now = LocalDateTime.now();
InfoMail infoMail = InfoMail.of(now);
state.setLastMailMessage(infoMail);

assertThat(state.getLastTimeOfInfomail()).isNotEmpty();
assertThat(state.getLastTimeOfInfomail().get()).isEqualTo(now);
assertThat(state.getLastTimeOfInfoMail()).isNotEmpty();
assertThat(state.getLastTimeOfInfoMail().get()).isEqualTo(now);

assertThatThrownBy(() -> {
state.setLastMailMessage(null);
Expand Down

0 comments on commit 10f49f0

Please sign in to comment.