Clean architecture initial commit
This commit is contained in:
parent
17d2870daa
commit
3ef0ac4257
|
@ -0,0 +1,87 @@
|
|||
<?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>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<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.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</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.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-engine</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</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,2 @@
|
|||
server.port=8080
|
||||
server.error.include-message=always
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import com.baeldung.pattern.cleanarchitecture.usercreation.UserResponseFormatter;
|
||||
import com.baeldung.pattern.cleanarchitecture.usercreation.UserResponseModel;
|
||||
|
||||
class UserResponseFormatterTests {
|
||||
|
||||
UserResponseFormatter userResponseFormatter = new UserResponseFormatter();
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.pattern.cleanarchitecture.usercreation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class UserUnitTest {
|
||||
|
||||
@Test
|
||||
void given123Password_whenPasswordIsNotValid_thenIsFalse() {
|
||||
User user = new CommonUser("Baeldung", "123");
|
||||
|
||||
assertThat(user.passwordIsValid()).isFalse();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue