Refactored RelayAccount -> Account

pull/21/head
Niclas Thobaben 2022-02-14 19:02:52 +01:00
parent 6015b06a87
commit 8f6fcf2578
18 changed files with 117 additions and 117 deletions

View File

@ -1,9 +1,9 @@
package de.nclazz.service.mailrelay;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.nclazz.service.mailrelay.adapter.web.RelayAccountForm;
import de.nclazz.service.mailrelay.adapter.web.AccountForm;
import de.nclazz.service.mailrelay.domain.Account;
import de.nclazz.service.mailrelay.domain.Relay;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@ -49,7 +49,7 @@ public class AccountCrudTest {
@Test
void newAccountCanBeCreatedAndAccessedAfterwards() throws Exception {
RelayAccountForm form = new RelayAccountForm("my-company", List.of("vip@my-company.com"));
AccountForm form = new AccountForm("my-company", List.of("vip@my-company.com"));
mockMvc.perform(post("/accounts").contentType(APPLICATION_JSON).content(mapper.writeValueAsString(form)))
.andExpect(status().isCreated())
.andExpect(content().contentType(APPLICATION_JSON))
@ -60,7 +60,7 @@ public class AccountCrudTest {
@Test
void existingAccountCanBeRetrieved() throws Exception {
RelayAccount account = RelayAccount.of("retrievable-account", List.of("info@account.com"));
Account account = Account.of("retrievable-account", List.of("info@account.com"));
account = relay.saveAccount(account);
mockMvc.perform(get("/accounts/{guid}", account.getGuid()))
@ -72,10 +72,10 @@ public class AccountCrudTest {
@Test
void existingAccountCanBeUpdated() throws Exception {
RelayAccount account = RelayAccount.of("updated-account", List.of("info@account.com"));
Account account = Account.of("updated-account", List.of("info@account.com"));
account = relay.saveAccount(account);
RelayAccountForm form = new RelayAccountForm("updated-account-renamed", List.of("vip@account.com"));
AccountForm form = new AccountForm("updated-account-renamed", List.of("vip@account.com"));
mockMvc.perform(put("/accounts/{guid}", account.getGuid()).contentType(APPLICATION_JSON).content(mapper.writeValueAsString(form)))
.andExpect(status().is2xxSuccessful())

View File

@ -1,7 +1,7 @@
package de.nclazz.service.mailrelay;
import de.nclazz.service.mailrelay.domain.Account;
import de.nclazz.service.mailrelay.domain.Relay;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@ -46,7 +46,7 @@ public class ForwardMessageFormTest {
@PostConstruct
public void init() {
RelayAccount account = new RelayAccount(UUID.randomUUID(), "company-account", TOKEN, List.of("vip@company.com"));
Account account = new Account(UUID.randomUUID(), "company-account", TOKEN, List.of("vip@company.com"));
relay.saveAccount(account);
}
}

View File

@ -0,0 +1,13 @@
package de.nclazz.service.mailrelay.adapter.jpa;
import de.nclazz.service.mailrelay.domain.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
import java.util.UUID;
public interface JpaAccountRepository extends JpaRepository<Account, UUID> {
Optional<Account> findByToken(String token);
}

View File

@ -1,7 +1,7 @@
package de.nclazz.service.mailrelay.adapter.jpa;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import de.nclazz.service.mailrelay.domain.RelayAccountRepository;
import de.nclazz.service.mailrelay.domain.Account;
import de.nclazz.service.mailrelay.domain.AccountRepository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
@ -12,27 +12,27 @@ import java.util.UUID;
@Repository
@RequiredArgsConstructor
public class JpaRelayAccountRepositoryAdapter implements RelayAccountRepository {
public class JpaAccountRepositoryAdapter implements AccountRepository {
private final JpaRelayAccountRepository jpaRepository;
private final JpaAccountRepository jpaRepository;
@Override
public List<RelayAccount> findAll() {
public List<Account> findAll() {
return this.jpaRepository.findAll();
}
@Override
public Optional<RelayAccount> findByGuid(@NonNull UUID guid) {
public Optional<Account> findByGuid(@NonNull UUID guid) {
return this.jpaRepository.findById(guid);
}
@Override
public Optional<RelayAccount> findByToken(@NonNull String token) {
public Optional<Account> findByToken(@NonNull String token) {
return this.jpaRepository.findByToken(token);
}
@Override
public RelayAccount save(@NonNull RelayAccount account) {
public Account save(@NonNull Account account) {
return this.jpaRepository.save(account);
}

View File

@ -1,13 +0,0 @@
package de.nclazz.service.mailrelay.adapter.jpa;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
import java.util.UUID;
public interface JpaRelayAccountRepository extends JpaRepository<RelayAccount, UUID> {
Optional<RelayAccount> findByToken(String token);
}

View File

@ -1,6 +1,6 @@
package de.nclazz.service.mailrelay.adapter.web;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import de.nclazz.service.mailrelay.domain.Account;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -10,12 +10,12 @@ import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RelayAccountForm {
public class AccountForm {
private String name;
private List<String> receivers;
public RelayAccount toAccount() {
return RelayAccount.of(this.name, this.receivers);
public Account toAccount() {
return Account.of(this.name, this.receivers);
}
}

View File

@ -1,7 +1,7 @@
package de.nclazz.service.mailrelay.adapter.web;
import de.nclazz.service.mailrelay.domain.Account;
import de.nclazz.service.mailrelay.domain.Relay;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -21,37 +21,37 @@ import java.util.UUID;
@RestController
@RequestMapping("accounts")
@RequiredArgsConstructor
public class RelayAccountRestController {
public class AccountRestController {
private final Relay relay;
@PostMapping
public ResponseEntity<RelayAccount> addAccount(@RequestBody RelayAccountForm form) {
RelayAccount account = form.toAccount();
public ResponseEntity<Account> addAccount(@RequestBody AccountForm form) {
Account account = form.toAccount();
account = this.relay.saveAccount(account);
return ResponseEntity.status(HttpStatus.CREATED).body(account);
}
@GetMapping
public List<RelayAccount> getAllAccounts() {
public List<Account> getAllAccounts() {
return this.relay.accounts();
}
@GetMapping("{guid}")
public RelayAccount findByGuid(@PathVariable("guid")UUID guid) {
public Account findByGuid(@PathVariable("guid")UUID guid) {
return this.relay.findAccount(guid)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
@DeleteMapping("{guid}")
public RelayAccount deleteByGuid(@PathVariable("guid")UUID guid) {
public Account deleteByGuid(@PathVariable("guid")UUID guid) {
return this.relay.deleteAccount(guid)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
@PutMapping("{guid}")
public RelayAccount updateByGuid(@PathVariable("guid")UUID guid, @RequestBody RelayAccountForm form) {
RelayAccount account = this.relay.findAccount(guid)
public Account updateByGuid(@PathVariable("guid")UUID guid, @RequestBody AccountForm form) {
Account account = this.relay.findAccount(guid)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
account.setName(form.getName());
account.setReceivers(form.getReceivers());

View File

@ -29,7 +29,7 @@ import java.util.UUID;
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class RelayAccount {
public class Account {
@Id
@Column(name = "guid", columnDefinition = "char(36)")
@ -53,8 +53,8 @@ public class RelayAccount {
this.sentMessages.add(message);
}
public static RelayAccount of(@NonNull String name, @NonNull List<String> receivers) {
RelayAccount account = new RelayAccount();
public static Account of(@NonNull String name, @NonNull List<String> receivers) {
Account account = new Account();
account.setName(name);
account.setReceivers(receivers);
return account;

View File

@ -0,0 +1,20 @@
package de.nclazz.service.mailrelay.domain;
import lombok.NonNull;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public interface AccountRepository {
List<Account> findAll();
Optional<Account> findByGuid(@NonNull UUID guid);
Optional<Account> findByToken(@NonNull String token);
Account save(@NonNull Account account);
void deleteByGuid(@NonNull UUID guid);
}

View File

@ -15,12 +15,12 @@ import java.util.UUID;
@Service
public class Relay {
private final RelayAccountRepository accountRepository;
private final AccountRepository accountRepository;
private final MessageRepository messageRepository;
public Message forwardMessage(@NonNull String token, @NonNull Message message) {
log.info("Forward message '{}' to <{}>", message.getSubject(), token);
Optional<RelayAccount> accountOptional = this.accountRepository.findByToken(token);
Optional<Account> accountOptional = this.accountRepository.findByToken(token);
if(accountOptional.isPresent()) {
message = this.messageRepository.save(message);
@ -30,20 +30,20 @@ public class Relay {
return message;
}
public RelayAccount saveAccount(@NonNull RelayAccount account) {
public Account saveAccount(@NonNull Account account) {
return this.accountRepository.save(account);
}
public List<RelayAccount> accounts() {
public List<Account> accounts() {
return this.accountRepository.findAll();
}
public Optional<RelayAccount> findAccount(@NonNull UUID guid) {
public Optional<Account> findAccount(@NonNull UUID guid) {
return this.accountRepository.findByGuid(guid);
}
public Optional<RelayAccount> deleteAccount(@NotNull UUID guid) {
Optional<RelayAccount> accountOptional = findAccount(guid);
public Optional<Account> deleteAccount(@NotNull UUID guid) {
Optional<Account> accountOptional = findAccount(guid);
if(accountOptional.isEmpty()) {
return Optional.empty();
}

View File

@ -1,20 +0,0 @@
package de.nclazz.service.mailrelay.domain;
import lombok.NonNull;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public interface RelayAccountRepository {
List<RelayAccount> findAll();
Optional<RelayAccount> findByGuid(@NonNull UUID guid);
Optional<RelayAccount> findByToken(@NonNull String token);
RelayAccount save(@NonNull RelayAccount account);
void deleteByGuid(@NonNull UUID guid);
}

View File

@ -1,9 +1,9 @@
package de.nclazz.service.mailrelay.adapter.web;
import de.nclazz.service.mailrelay.domain.Account;
import de.nclazz.service.mailrelay.domain.FakeAccountRepository;
import de.nclazz.service.mailrelay.domain.FakeMessageRepository;
import de.nclazz.service.mailrelay.domain.FakeRelayAccountRepository;
import de.nclazz.service.mailrelay.domain.Relay;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import org.junit.jupiter.api.Test;
import org.springframework.web.server.ResponseStatusException;
@ -13,15 +13,15 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@SuppressWarnings("ConstantConditions")
class RelayAccountRestControllerTest {
class AccountRestControllerTest {
@Test
void addAccountCreatesNewAccountAndReturnsIs() {
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
RelayAccountRestController controller = new RelayAccountRestController(relay);
RelayAccountForm form = new RelayAccountForm(
AccountRestController controller = new AccountRestController(relay);
AccountForm form = new AccountForm(
"my-account",
List.of("vip@my-company.com", "info@my-company.com")
);
@ -33,15 +33,15 @@ class RelayAccountRestControllerTest {
@Test
void addedAccountCanBeAccessedByItsGuid() {
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
RelayAccountRestController controller = new RelayAccountRestController(relay);
RelayAccountForm form = new RelayAccountForm(
AccountRestController controller = new AccountRestController(relay);
AccountForm form = new AccountForm(
"my-account",
List.of("vip@my-company.com", "info@my-company.com")
);
RelayAccount account = controller.addAccount(form).getBody();
Account account = controller.addAccount(form).getBody();
assertThat(controller.findByGuid(account.getGuid()))
.isNotNull()
@ -51,15 +51,15 @@ class RelayAccountRestControllerTest {
@Test
void addedAccountCanBeDeletedAndIsReturned() {
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
RelayAccountRestController controller = new RelayAccountRestController(relay);
RelayAccountForm form = new RelayAccountForm(
AccountRestController controller = new AccountRestController(relay);
AccountForm form = new AccountForm(
"my-account",
List.of("vip@my-company.com", "info@my-company.com")
);
RelayAccount account = controller.addAccount(form).getBody();
Account account = controller.addAccount(form).getBody();
assertThat(controller.deleteByGuid(account.getGuid()))
.isNotNull()
@ -69,15 +69,15 @@ class RelayAccountRestControllerTest {
@Test
void addedAccountCanBeDeletedAndIsNotAccessibleAfterwards() {
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
RelayAccountRestController controller = new RelayAccountRestController(relay);
RelayAccountForm form = new RelayAccountForm(
AccountRestController controller = new AccountRestController(relay);
AccountForm form = new AccountForm(
"my-account",
List.of("vip@my-company.com", "info@my-company.com")
);
RelayAccount account = controller.addAccount(form).getBody();
Account account = controller.addAccount(form).getBody();
controller.deleteByGuid(account.getGuid());
assertThatThrownBy(() -> controller.findByGuid(account.getGuid()))
@ -86,18 +86,18 @@ class RelayAccountRestControllerTest {
@Test
void accountCanBeUpdated() {
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
RelayAccountRestController controller = new RelayAccountRestController(relay);
AccountRestController controller = new AccountRestController(relay);
RelayAccount account = controller.addAccount(
new RelayAccountForm(
Account account = controller.addAccount(
new AccountForm(
"my-account",
List.of("vip@my-company.com")
)
).getBody();
RelayAccountForm form = new RelayAccountForm("my-renamed-account", List.of("vip@my-company.com", "other@my-company.com"));
AccountForm form = new AccountForm("my-renamed-account", List.of("vip@my-company.com", "other@my-company.com"));
assertThat(controller.updateByGuid(account.getGuid(), form))
.extracting("name", "receivers")

View File

@ -1,10 +1,10 @@
package de.nclazz.service.mailrelay.adapter.web;
import de.nclazz.service.mailrelay.FakeClock;
import de.nclazz.service.mailrelay.domain.Account;
import de.nclazz.service.mailrelay.domain.FakeAccountRepository;
import de.nclazz.service.mailrelay.domain.FakeMessageRepository;
import de.nclazz.service.mailrelay.domain.FakeRelayAccountRepository;
import de.nclazz.service.mailrelay.domain.Relay;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import org.junit.jupiter.api.Test;
import java.time.Clock;
@ -19,14 +19,14 @@ class MessageFormControllerTest {
@Test
void forwardMessageShouldForwardMessageAccordingly() {
String token = "my-token";
RelayAccount account = new RelayAccount(
Account account = new Account(
UUID.randomUUID(),
"relay account name",
token,
List.of("vip@account.com")
);
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
relay.saveAccount(account);
LocalDateTime now = LocalDateTime.of(2020, 12, 24, 12, 54);

View File

@ -1,10 +1,10 @@
package de.nclazz.service.mailrelay.adapter.web;
import de.nclazz.service.mailrelay.FakeClock;
import de.nclazz.service.mailrelay.domain.Account;
import de.nclazz.service.mailrelay.domain.FakeAccountRepository;
import de.nclazz.service.mailrelay.domain.FakeMessageRepository;
import de.nclazz.service.mailrelay.domain.FakeRelayAccountRepository;
import de.nclazz.service.mailrelay.domain.Relay;
import de.nclazz.service.mailrelay.domain.RelayAccount;
import org.junit.jupiter.api.Test;
import java.time.Clock;
@ -19,14 +19,14 @@ class MessageRestControllerTest {
@Test
void forwardMessageShouldForwardMessageAccordingly() {
String token = "my-token";
RelayAccount account = new RelayAccount(
Account account = new Account(
UUID.randomUUID(),
"relay account name",
token,
List.of("vip@account.com")
);
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
relay.saveAccount(account);
LocalDateTime now = LocalDateTime.of(2020, 12, 24, 12, 54);

View File

@ -8,11 +8,11 @@ import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
class RelayAccountTest {
class AccountTest {
@Test
void forwardMessageAddsMessageToSentMessages() {
RelayAccount account = new RelayAccount(
Account account = new Account(
UUID.randomUUID(),
"company-invoices",
"abcdefg",

View File

@ -10,31 +10,31 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
public class FakeRelayAccountRepository implements RelayAccountRepository {
public class FakeAccountRepository implements AccountRepository {
private Set<RelayAccount> accounts = new HashSet<>();
private Set<Account> accounts = new HashSet<>();
@Override
public List<RelayAccount> findAll() {
public List<Account> findAll() {
return new ArrayList<>(this.accounts);
}
@Override
public Optional<RelayAccount> findByGuid(@NonNull UUID guid) {
public Optional<Account> findByGuid(@NonNull UUID guid) {
return this.accounts.stream()
.filter(account -> account.getGuid().equals(guid))
.findAny();
}
@Override
public Optional<RelayAccount> findByToken(@NonNull String token) {
public Optional<Account> findByToken(@NonNull String token) {
return this.accounts.stream()
.filter(account -> account.getToken().equals(token))
.findAny();
}
@Override
public RelayAccount save(@NonNull RelayAccount account) {
public Account save(@NonNull Account account) {
UUID guid = (account.getGuid() != null ? account.getGuid() : UUID.randomUUID());
account.setGuid(guid);

View File

@ -13,14 +13,14 @@ class ForwardMessageTest {
@Test
void relayForwardsMessageToCorrectAccountByToken() {
String token = "my-token";
RelayAccount account = new RelayAccount(
Account account = new Account(
UUID.randomUUID(),
"company-invoices",
token,
List.of("info@company.com")
);
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
relay.saveAccount(account);
LocalDateTime now = LocalDateTime.now();
@ -40,14 +40,14 @@ class ForwardMessageTest {
@Test
void relayDoesNotForwardMessageForWrongToken() {
String token = "my-token";
RelayAccount account = new RelayAccount(
Account account = new Account(
UUID.randomUUID(),
"company-invoices",
token,
List.of("info@company.com")
);
Relay relay = new Relay(new FakeRelayAccountRepository(), new FakeMessageRepository());
Relay relay = new Relay(new FakeAccountRepository(), new FakeMessageRepository());
relay.saveAccount(account);
LocalDateTime now = LocalDateTime.now();

View File

@ -11,14 +11,14 @@ class RelayTest {
@Test
void addAccountAddsAccountToRelay() {
RelayAccount account = new RelayAccount(
Account account = new Account(
UUID.randomUUID(),
"relay account name",
"may-token",
List.of("vip@account.com")
);
FakeRelayAccountRepository repository = new FakeRelayAccountRepository();
FakeAccountRepository repository = new FakeAccountRepository();
Relay relay = new Relay(repository, new FakeMessageRepository());
relay.saveAccount(account);