add new package

This commit is contained in:
2024-04-30 11:54:43 -04:00
parent 6b33c0d65d
commit 99259d231b
291 changed files with 7240 additions and 0 deletions
@@ -0,0 +1,4 @@
### Relevant Articles:
- [Clean Architecture with Spring Boot](https://www.baeldung.com/spring-boot-clean-architecture)
- [Anemic vs. Rich Domain Objects](https://www.baeldung.com/java-anemic-vs-rich-domain-objects)
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>clean-architecture</artifactId>
<version>1.0</version>
<name>clean-architecture</name>
<description>Project for clean architecture in java</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,40 @@
package com.baeldung.pattern.cleanarchitecture;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
@SpringBootApplication
public class CleanArchitectureApplication {
public static void main(String[] args) {
SpringApplication.run(CleanArchitectureApplication.class);
}
@Bean
BeanFactoryPostProcessor beanFactoryPostProcessor(ApplicationContext beanRegistry) {
return beanFactory -> {
genericApplicationContext((BeanDefinitionRegistry) ((AnnotationConfigServletWebServerApplicationContext) beanRegistry).getBeanFactory());
};
}
void genericApplicationContext(BeanDefinitionRegistry beanRegistry) {
ClassPathBeanDefinitionScanner beanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanRegistry);
beanDefinitionScanner.addIncludeFilter(removeModelAndEntitiesFilter());
beanDefinitionScanner.scan("com.baeldung.pattern.cleanarchitecture");
}
static TypeFilter removeModelAndEntitiesFilter() {
return (MetadataReader mr, MetadataReaderFactory mrf) -> !mr.getClassMetadata()
.getClassName()
.endsWith("Model");
}
}
@@ -0,0 +1,30 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
class CommonUser implements User {
String name;
String password;
CommonUser(String name, String password) {
this.name = name;
this.password = password;
}
CommonUser() {
}
@Override
public boolean passwordIsValid() {
return password != null && password.length() > 5;
}
@Override
public String getName() {
return name;
}
@Override
public String getPassword() {
return password;
}
}
@@ -0,0 +1,8 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
class CommonUserFactory implements UserFactory {
@Override
public User create(String name, String password) {
return new CommonUser(name, password);
}
}
@@ -0,0 +1,21 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
class JpaUser implements UserRegisterDsGateway {
final JpaUserRepository repository;
JpaUser(JpaUserRepository repository) {
this.repository = repository;
}
@Override
public boolean existsByName(String name) {
return repository.existsById(name);
}
@Override
public void save(UserDsRequestModel requestModel) {
UserDataMapper accountDataMapper = new UserDataMapper(requestModel.getName(), requestModel.getPassword(), requestModel.getCreationTime());
repository.save(accountDataMapper);
}
}
@@ -0,0 +1,8 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
interface JpaUserRepository extends JpaRepository<UserDataMapper, String> {
}
@@ -0,0 +1,9 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
interface User {
boolean passwordIsValid();
String getName();
String getPassword();
}
@@ -0,0 +1,53 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
import java.time.LocalDateTime;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user")
class UserDataMapper {
@Id
String name;
String password;
LocalDateTime creationTime;
public UserDataMapper() {
}
public UserDataMapper(String name, String password, LocalDateTime creationTime) {
super();
this.name = name;
this.password = password;
this.creationTime = creationTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public LocalDateTime getCreationTime() {
return creationTime;
}
public void setCreationTime(LocalDateTime creationTime) {
this.creationTime = creationTime;
}
}
@@ -0,0 +1,41 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
import java.time.LocalDateTime;
class UserDsRequestModel {
String name;
String password;
LocalDateTime creationTime;
public UserDsRequestModel(String name, String password, LocalDateTime creationTime) {
this.name = name;
this.password = password;
this.creationTime = creationTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public LocalDateTime getCreationTime() {
return creationTime;
}
public void setCreationTime(LocalDateTime creationTime) {
this.creationTime = creationTime;
}
}
@@ -0,0 +1,5 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
interface UserFactory {
User create(String name, String password);
}
@@ -0,0 +1,5 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
public interface UserInputBoundary {
UserResponseModel create(UserRequestModel requestModel);
}
@@ -0,0 +1,7 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
interface UserPresenter {
UserResponseModel prepareSuccessView(UserResponseModel user);
UserResponseModel prepareFailView(String error);
}
@@ -0,0 +1,20 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
class UserRegisterController {
final UserInputBoundary userInput;
UserRegisterController(UserInputBoundary accountGateway) {
this.userInput = accountGateway;
}
@PostMapping("/user")
UserResponseModel create(@RequestBody UserRequestModel requestModel) {
return userInput.create(requestModel);
}
}
@@ -0,0 +1,7 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
interface UserRegisterDsGateway {
boolean existsByName(String identifier);
void save(UserDsRequestModel requestModel);
}
@@ -0,0 +1,35 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
import java.time.LocalDateTime;
class UserRegisterInteractor implements UserInputBoundary {
final UserRegisterDsGateway userDsGateway;
final UserPresenter userPresenter;
final UserFactory userFactory;
UserRegisterInteractor(UserRegisterDsGateway userRegisterDfGateway, UserPresenter userPresenter,
UserFactory userFactory) {
this.userDsGateway = userRegisterDfGateway;
this.userPresenter = userPresenter;
this.userFactory = userFactory;
}
@Override
public UserResponseModel create(UserRequestModel requestModel) {
if (userDsGateway.existsByName(requestModel.getName())) {
return userPresenter.prepareFailView("User already exists.");
}
User user = userFactory.create(requestModel.getName(), requestModel.getPassword());
if (!user.passwordIsValid()) {
return userPresenter.prepareFailView("User password must have more than 5 characters.");
}
LocalDateTime now = LocalDateTime.now();
UserDsRequestModel userDsModel = new UserDsRequestModel(user.getName(), user.getPassword(), now);
userDsGateway.save(userDsModel);
UserResponseModel accountResponseModel = new UserResponseModel(user.getName(), now.toString());
return userPresenter.prepareSuccessView(accountResponseModel);
}
}
@@ -0,0 +1,33 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
class UserRequestModel {
String name;
String password;
public UserRequestModel() {
super();
}
UserRequestModel(String name, String password) {
super();
this.name = name;
this.password = password;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
String getPassword() {
return password;
}
void setPassword(String password) {
this.password = password;
}
}
@@ -0,0 +1,22 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
class UserResponseFormatter implements UserPresenter {
@Override
public UserResponseModel prepareSuccessView(UserResponseModel response) {
LocalDateTime responseTime = LocalDateTime.parse(response.getCreationTime());
response.setCreationTime(responseTime.format(DateTimeFormatter.ofPattern("hh:mm:ss")));
return response;
}
@Override
public UserResponseModel prepareFailView(String error) {
throw new ResponseStatusException(HttpStatus.CONFLICT, error);
}
}
@@ -0,0 +1,29 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
public class UserResponseModel {
String login;
String creationTime;
public UserResponseModel(String login, String creationTime) {
this.login = login;
this.creationTime = creationTime;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getCreationTime() {
return creationTime;
}
public void setCreationTime(String creationTime) {
this.creationTime = creationTime;
}
}
@@ -0,0 +1,35 @@
package com.baeldung.pattern.richdomainmodel;
public class Player {
private int points ;
final String name;
public Player(String name) {
this(name, 0);
}
private Player(String name, int points) {
this.name = name;
this.points = 0;
}
public void gainPoint() {
points++;
}
public boolean hasScoreBiggerThan(Score score) {
return this.points > score.points();
}
public int pointsDifference(Player other) {
return points - other.points;
}
public String name() {
return name;
}
public String score() {
return Score.from(points).label();
}
}
@@ -0,0 +1,29 @@
package com.baeldung.pattern.richdomainmodel;
import java.util.Arrays;
public enum Score {
LOVE(0, "Love"), FIFTEEN(1, "Fifteen"), THIRTY(2, "Thirty"), FORTY(3, "Forty");
private final int points;
private final String label;
Score(int points, String label) {
this.points = points;
this.label = label;
}
public static Score from(int value) {
return Arrays.stream(values())
.filter(v -> v.points == value)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("no such element: " + value));
}
public int points() {
return points;
}
public String label() {
return label;
}
}
@@ -0,0 +1,71 @@
package com.baeldung.pattern.richdomainmodel;
public class TennisGame {
private final Player server;
private final Player receiver;
public TennisGame(String server, String receiver) {
this.server = new Player(server);
this.receiver = new Player(receiver);
}
public void wonPoint(String playerName) {
if(server.name().equals(playerName)) {
server.gainPoint();
} else {
receiver.gainPoint();
}
}
public String getScore() {
if (gameContinues()) {
return getGameScore();
}
return "Win for " + leadingPlayer().name();
}
private String getGameScore() {
if (isScoreEqual()) {
return getEqualScore();
}
if (isAdvantage()) {
return "Advantage " + leadingPlayer().name();
}
return getSimpleScore();
}
private boolean isScoreEqual() {
return server.pointsDifference(receiver) == 0;
}
private boolean isAdvantage() {
return leadingPlayer().hasScoreBiggerThan(Score.FORTY)
&& Math.abs(server.pointsDifference(receiver)) == 1;
}
private boolean isGameFinished() {
return leadingPlayer().hasScoreBiggerThan(Score.FORTY)
&& Math.abs(server.pointsDifference(receiver)) >= 2;
}
private Player leadingPlayer() {
if (server.pointsDifference(receiver) > 0) {
return server;
}
return receiver;
}
private boolean gameContinues() {
return !isGameFinished();
}
private String getSimpleScore() {
return String.format("%s-%s", server.score(), receiver.score());
}
private String getEqualScore() {
if (server.hasScoreBiggerThan(Score.THIRTY)) {
return "Deuce";
}
return String.format("%s-All", server.score());
}
}
@@ -0,0 +1,2 @@
server.port=8080
server.error.include-message=always
@@ -0,0 +1,50 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import org.mockito.ArgumentCaptor;
import org.junit.jupiter.api.Test;
import org.springframework.web.server.ResponseStatusException;
class UserResponseFormatterUnitTest {
UserResponseFormatter userResponseFormatter = new UserResponseFormatter();
UserRegisterDsGateway userDsGateway = mock(UserRegisterDsGateway.class);
UserPresenter userPresenter = mock(UserPresenter.class);
UserFactory userFactory = mock(UserFactory.class);
UserInputBoundary userInputBoundary = new UserRegisterInteractor(userDsGateway, userPresenter, userFactory);
ArgumentCaptor<String> userRequestModelArgumentCaptor = ArgumentCaptor.forClass(String.class);
@Test
void givenDateAnd3HourTime_whenPrepareSuccessView_thenReturnOnly3HourTime() {
UserResponseModel modelResponse = new UserResponseModel("baeldung", "2020-12-20T03:00:00.000");
UserResponseModel formattedResponse = userResponseFormatter.prepareSuccessView(modelResponse);
assertThat(formattedResponse.getCreationTime()).isEqualTo("03:00:00");
}
@Test
void whenPrepareFailView_thenThrowHttpConflictException() {
assertThatThrownBy(() -> userResponseFormatter.prepareFailView("Invalid password")).isInstanceOf(ResponseStatusException.class);
}
@Test
void whenCreateUser_thenSuccess() {
UserRequestModel userRequestModel = new UserRequestModel("baeldung", "123456");
when(userFactory.create(anyString(), anyString())).thenReturn(new CommonUser("baeldung", "123456"));
userInputBoundary.create(userRequestModel);
verify(userDsGateway).existsByName(userRequestModelArgumentCaptor.capture());
String name = userRequestModel.getName();
assertEquals("baeldung", name);
}
}
@@ -0,0 +1,36 @@
package com.baeldung.pattern.cleanarchitecture.usercreation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.any;
import org.junit.jupiter.api.Test;
class UserUnitTest {
UserRegisterDsGateway userDsGateway = mock(UserRegisterDsGateway.class);
UserPresenter userPresenter = mock(UserPresenter.class);
UserFactory userFactory = mock(UserFactory.class);
UserInputBoundary interactor = new UserRegisterInteractor(userDsGateway, userPresenter, userFactory);
@Test
void given123Password_whenPasswordIsNotValid_thenIsFalse() {
User user = new CommonUser("Baeldung", "123");
assertThat(user.passwordIsValid()).isFalse();
}
@Test
void givenBaeldungUserAnd123456Password_whenCreate_thenSaveItAndPrepareSuccessView() {
User user = new CommonUser("baeldung", "123456");
UserRequestModel userRequestModel = new UserRequestModel(user.getName(), user.getPassword());
when(userFactory.create(anyString(), anyString())).thenReturn(new CommonUser(user.getName(), user.getPassword()));
interactor.create(userRequestModel);
verify(userDsGateway, times(1)).save(any(UserDsRequestModel.class));
verify(userPresenter, times(1)).prepareSuccessView(any(UserResponseModel.class));
}
}
@@ -0,0 +1,33 @@
package com.baeldung.pattern.richdomainmodel;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class RichDomainModelUnitTest {
@Test
public void givenATennisGame_whenReceiverWinsThreePoints_thenScoreIsFortyLove() {
TennisGame game = new TennisGame("server", "receiver");
game.wonPoint("server");
game.wonPoint("server");
game.wonPoint("server");
assertThat(game.getScore())
.isEqualTo("Forty-Love");
}
@Test
public void givenATennisGame_whenEachPlayerWonTwoPoints_thenScoreIsThirtyAll() {
TennisGame game = new TennisGame("server", "receiver");
game.wonPoint("server");
game.wonPoint("server");
game.wonPoint("receiver");
game.wonPoint("receiver");
assertThat(game.getScore())
.isEqualTo("Thirty-All");
}
}