Implemented RelayRestController and MessageForm
parent
a1d0af4e1d
commit
72d70b8a57
|
@ -0,0 +1,13 @@
|
|||
package de.nclazz.service.mailrelay;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class MailRelayApplicationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,9 @@ package de.nclazz.service.mailrelay;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import java.time.Clock;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MailRelayApplication {
|
||||
|
@ -10,4 +13,9 @@ public class MailRelayApplication {
|
|||
SpringApplication.run(MailRelayApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Clock clock() {
|
||||
return Clock.systemDefaultZone();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package de.nclazz.service.mailrelay;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.NonNull;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class MessageForm {
|
||||
|
||||
private String token;
|
||||
|
||||
private String subject;
|
||||
private String content;
|
||||
private String from;
|
||||
|
||||
public Message toMessage(@NonNull LocalDateTime timestamp) {
|
||||
return new Message(
|
||||
this.subject,
|
||||
this.content,
|
||||
this.from,
|
||||
timestamp
|
||||
);
|
||||
}
|
||||
|
||||
public Message toMessage() {
|
||||
return toMessage(LocalDateTime.now());
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package de.nclazz.service.mailrelay;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class MessageRestController {
|
||||
|
||||
private final Relay relay;
|
||||
private final Clock clock;
|
||||
|
||||
@PostMapping
|
||||
public Message forwardMessage(@RequestBody MessageForm form) {
|
||||
String token = form.getToken();
|
||||
Message message = form.toMessage(LocalDateTime.now(this.clock));
|
||||
|
||||
return this.relay.forwardMessage(token, message);
|
||||
}
|
||||
|
||||
}
|
|
@ -5,6 +5,7 @@ import lombok.NonNull;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Data
|
||||
|
@ -17,10 +18,20 @@ public class Relay {
|
|||
this.accounts.add(account);
|
||||
}
|
||||
|
||||
public void forwardMessage(String token, Message message) {
|
||||
this.accounts.stream()
|
||||
.filter(account -> account.getToken().equals(token))
|
||||
.findAny()
|
||||
.ifPresent(account -> account.forwardMessage(message));
|
||||
public Message forwardMessage(String token, Message message) {
|
||||
Optional<RelayAccount> accountOptional = findAccountByToken(token);
|
||||
|
||||
if(accountOptional.isPresent()) {
|
||||
return accountOptional.get().forwardMessage(message);
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
private Optional<RelayAccount> findAccountByToken(String token) {
|
||||
return this.accounts.stream()
|
||||
.filter(acc -> acc.matchesToken(token))
|
||||
.findAny();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,10 +20,13 @@ public class RelayAccount {
|
|||
|
||||
private final List<Message> sentMessages = new ArrayList<>();
|
||||
|
||||
public void forwardMessage(@NonNull Message message) {
|
||||
public Message forwardMessage(@NonNull Message message) {
|
||||
this.sentMessages.add(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean matchesToken(String token) {
|
||||
return this.token.equals(token);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package de.nclazz.service.mailrelay;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
public class FakeClock {
|
||||
|
||||
public static Clock fixed(LocalDateTime localDateTime) {
|
||||
ZoneOffset zoneOffset = ZoneOffset.ofHoursMinutes(0,0);
|
||||
ZoneId zoneId = ZoneId.ofOffset("UTC", zoneOffset);
|
||||
Instant instant = localDateTime.toInstant(zoneOffset);
|
||||
|
||||
return Clock.fixed(instant, zoneId);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package de.nclazz.service.mailrelay;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MessageRestControllerTest {
|
||||
|
||||
@Test
|
||||
void forwardMessageShouldForwardMessageAccordingly() {
|
||||
String token = "my-token";
|
||||
RelayAccount account = new RelayAccount(
|
||||
UUID.randomUUID(),
|
||||
"relay account name",
|
||||
token,
|
||||
List.of("vip@account.com")
|
||||
);
|
||||
|
||||
Relay relay = new Relay();
|
||||
relay.addAccount(account);
|
||||
|
||||
LocalDateTime now = LocalDateTime.of(2020, 12, 24, 12, 54);
|
||||
Clock fakeClock = FakeClock.fixed(now);
|
||||
|
||||
MessageForm form = new MessageForm(token, "Subject", "Message", "sender@company.com");
|
||||
|
||||
MessageRestController controller = new MessageRestController(relay, fakeClock);
|
||||
|
||||
assertThat(controller.forwardMessage(form))
|
||||
.isEqualTo(new Message("Subject", "Message", "sender@company.com", now));
|
||||
}
|
||||
}
|
|
@ -1,13 +1,11 @@
|
|||
package de.nclazz.service.mailrelay;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class RelayTest {
|
||||
|
||||
|
|
Loading…
Reference in New Issue