Merge pull request #13204 from eugenp/BAEL-71323

BAEL-71323 re-org modules
This commit is contained in:
Loredana Crusoveanu 2022-12-25 17:14:15 +02:00 committed by GitHub
commit e4a775f1e4
59 changed files with 2 additions and 539 deletions

View File

@ -1,6 +0,0 @@
.classpath
.project
.settings/
target/
bin/

View File

@ -1,5 +0,0 @@
## Java Bean Validation Examples
This module contains articles about Bean Validation.
### Relevant Articles:

View File

@ -1,34 +0,0 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>javaxval2</artifactId>
<version>0.1-SNAPSHOT</version>
<name>javaxval2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind.version}</version>
</dependency>
</dependencies>
<properties>
<spring.boot.version>2.7.5</spring.boot.version>
<jackson.databind.version>2.14.0</jackson.databind.version>
</properties>
</project>

View File

@ -1,20 +0,0 @@
package com.baeldung.javaxval.afterdeserialization;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
public class BeanDeserializerModifierWithValidation extends BeanDeserializerModifier {
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
if (deserializer instanceof BeanDeserializer) {
return new BeanDeserializerWithValidation((BeanDeserializer) deserializer);
}
return deserializer;
}
}

View File

@ -1,40 +0,0 @@
package com.baeldung.javaxval.afterdeserialization;
import java.io.IOException;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerBase;
public class BeanDeserializerWithValidation extends BeanDeserializer {
private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
private static final Validator validator = factory.getValidator();
protected BeanDeserializerWithValidation(BeanDeserializerBase src) {
super(src);
}
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Object instance = super.deserialize(p, ctxt);
validate(instance);
return instance;
}
public <T> void validate(T t) {
Set<ConstraintViolation<T>> violations = validator.validate(t);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
}
}

View File

@ -1,14 +0,0 @@
package com.baeldung.javaxval.afterdeserialization;
import javax.validation.constraints.Size;
public class Student {
@Size(min = 5, max = 10, message = "Student's name must be between 5 and 10 characters")
private String name;
public String getName() {
return name;
}
}

View File

@ -1,24 +0,0 @@
package com.baeldung.javaxval.afterdeserialization;
import java.io.IOException;
import java.io.InputStream;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
public class StudentDeserializerWithValidation {
public static Student readStudent(InputStream inputStream) throws IOException {
ObjectMapper mapper = getObjectMapperWithValidation();
return mapper.readValue(inputStream, Student.class);
}
private static ObjectMapper getObjectMapperWithValidation() {
SimpleModule validationModule = new SimpleModule();
validationModule.setDeserializerModifier(new BeanDeserializerModifierWithValidation());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(validationModule);
return mapper;
}
}

View File

@ -1,51 +0,0 @@
package com.baeldung.javaxval;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.InputStream;
import javax.validation.ConstraintViolationException;
import org.junit.jupiter.api.Test;
import com.baeldung.javaxval.afterdeserialization.Student;
import com.baeldung.javaxval.afterdeserialization.StudentDeserializerWithValidation;
public class StudentDeserializerWithValidationUnitTest {
private final String EXPECTED_ERROR_MESSAGE = "name: Student's name must be between 5 and 10 characters";
private final String EXPECTED_STUDENT_NAME = "Daniel";
private final String NAME_TOO_LONG_STUDENT_FILE = "nameTooLongStudent.json";
private final String NAME_TOO_SHORT_STUDENT_FILE = "nameTooShortStudent.json";
private final String SUBDIRECTORY = "afterdeserialization/";
private final String VALID_STUDENT_FILE = "validStudent.json";
@Test
void givenValidStudent_WhenReadStudent_ThenReturnStudent() throws IOException {
InputStream inputStream = getInputStream(VALID_STUDENT_FILE);
Student result = StudentDeserializerWithValidation.readStudent(inputStream);
assertEquals(EXPECTED_STUDENT_NAME, result.getName());
}
@Test
void givenStudentWithTooShortName_WhenReadStudent_ThenThrows() {
InputStream inputStream = getInputStream(NAME_TOO_SHORT_STUDENT_FILE);
ConstraintViolationException constraintViolationException = assertThrows(ConstraintViolationException.class, () -> StudentDeserializerWithValidation.readStudent(inputStream));
assertEquals(EXPECTED_ERROR_MESSAGE, constraintViolationException.getMessage());
}
@Test
void givenStudentWithTooLongName_WhenReadStudent_ThenThrows() {
InputStream inputStream = getInputStream(NAME_TOO_LONG_STUDENT_FILE);
ConstraintViolationException constraintViolationException = assertThrows(ConstraintViolationException.class, () -> StudentDeserializerWithValidation.readStudent(inputStream));
assertEquals(EXPECTED_ERROR_MESSAGE, constraintViolationException.getMessage());
}
private InputStream getInputStream(String fileName) {
return getClass().getClassLoader()
.getResourceAsStream(SUBDIRECTORY + fileName);
}
}

View File

@ -1,3 +0,0 @@
{
"name": "Constantine"
}

View File

@ -1,3 +0,0 @@
{
"name": "Daniel"
}

View File

@ -2,4 +2,3 @@
This module contains articles about design patterns.
- [Coupling in Java](https://www.baeldung.com/java-coupling-classes-tight-loose)

View File

@ -0,0 +1,2 @@
- [Coupling in Java](https://www.baeldung.com/java-coupling-classes-tight-loose)

View File

@ -12,10 +12,6 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>wire-tap</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>

View File

@ -1,25 +0,0 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>wire-tap</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<artifactId>enterprise-patterns</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,50 +0,0 @@
<?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>
<groupId>com.baeldung</groupId>
<artifactId>hexagonal-architecture</artifactId>
<version>1.0</version>
<name>hexagonal-architecture</name>
<description>Project for hexagonal 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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,13 +0,0 @@
package com.baeldung.pattern.hexagonal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HexArchApplicationDemo {
public static void main(String[] args) {
SpringApplication.run(HexArchApplicationDemo.class, args);
}
}

View File

@ -1,15 +0,0 @@
package com.baeldung.pattern.hexagonal.config;
import com.baeldung.pattern.hexagonal.domain.services.EmployeeService;
import com.baeldung.pattern.hexagonal.domain.services.EmployeeServiceImpl;
import com.baeldung.pattern.hexagonal.persistence.EmployeeRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public EmployeeService getEmployeeService(EmployeeRepository employeeRepository) {
return new EmployeeServiceImpl(employeeRepository);
}
}

View File

@ -1,10 +0,0 @@
package com.baeldung.pattern.hexagonal.config;
import com.baeldung.pattern.hexagonal.persistence.MongoRepoEx;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
@Configuration
@EnableMongoRepositories(basePackageClasses = MongoRepoEx.class)
public class MongoConfig {
}

View File

@ -1,25 +0,0 @@
package com.baeldung.pattern.hexagonal.controller;
import com.baeldung.pattern.hexagonal.domain.model.Employee;
import com.baeldung.pattern.hexagonal.domain.services.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/employees")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Employee addEmployee(@RequestBody Employee employee) {
return employeeService.addEmployee(employee);
}
@GetMapping(path = "/{employeeId}")
public Employee getEmployee(@PathVariable("employeeId") String employeeId) {
return employeeService.getEmployee(employeeId);
}
}

View File

@ -1,51 +0,0 @@
package com.baeldung.pattern.hexagonal.domain.model;
import org.springframework.data.annotation.Id;
import java.util.Objects;
public class Employee {
@Id
private String empId;
private String empName;
private String empJobTitle;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpJobTitle() {
return empJobTitle;
}
public void setEmpJobTitle(String empJobTitle) {
this.empJobTitle = empJobTitle;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Employee employee = (Employee) o;
return empId.equals(employee.empId);
}
@Override
public int hashCode() {
return Objects.hash(empId);
}
}

View File

@ -1,10 +0,0 @@
package com.baeldung.pattern.hexagonal.domain.services;
import com.baeldung.pattern.hexagonal.domain.model.Employee;
public interface EmployeeService {
Employee addEmployee(Employee employee);
Employee getEmployee(String employeeId);
}

View File

@ -1,34 +0,0 @@
package com.baeldung.pattern.hexagonal.domain.services;
import com.baeldung.pattern.hexagonal.domain.model.Employee;
import com.baeldung.pattern.hexagonal.persistence.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeRepository employeeRepository;
@Autowired
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@Override
public Employee addEmployee(Employee employee) {
return employeeRepository.add(employee);
}
@Override
public Employee getEmployee(String employeeId) {
Optional<Employee> employee = employeeRepository.findById(employeeId);
if (employee.isPresent()) {
return employee.get();
} else {
// throw
}
return null;
}
}

View File

@ -1,15 +0,0 @@
package com.baeldung.pattern.hexagonal.persistence;
import com.baeldung.pattern.hexagonal.domain.model.Employee;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface EmployeeRepository {
Employee add(Employee employee);
Optional<Employee> findById(String id);
}

View File

@ -1,24 +0,0 @@
package com.baeldung.pattern.hexagonal.persistence;
import com.baeldung.pattern.hexagonal.domain.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public class MongoDBRepository implements EmployeeRepository {
@Autowired
MongoRepoEx mongoRepository;
@Override
public Employee add(Employee employee) {
return mongoRepository.insert(employee);
}
@Override
public Optional<Employee> findById(String id) {
return mongoRepository.findById(id);
}
}

View File

@ -1,9 +0,0 @@
package com.baeldung.pattern.hexagonal.persistence;
import com.baeldung.pattern.hexagonal.domain.model.Employee;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MongoRepoEx extends MongoRepository<Employee, String> {
}

View File

@ -1,46 +0,0 @@
package com.baeldung.pattern.hexagonal.domain.services;
import com.baeldung.pattern.hexagonal.domain.model.Employee;
import com.baeldung.pattern.hexagonal.persistence.EmployeeRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class EmployeeServiceImplUnitTest {
private EmployeeRepository employeeRepository;
private EmployeeService testService;
private Employee testModel;
@BeforeEach
void setUp() {
employeeRepository = mock(EmployeeRepository.class);
testService = new EmployeeServiceImpl(employeeRepository);
testModel = new Employee();
testModel.setEmpId("2000");
testModel.setEmpName("Test user 1");
testModel.setEmpJobTitle("Software engineer");
}
@Test
void addEmployee() {
when(employeeRepository.add(any(Employee.class))).thenReturn(testModel);
Employee testResponse = testService.addEmployee(testModel);
assertEquals(testModel, testResponse);
}
@Test
void getEmployee() {
when(employeeRepository.findById("2000")).thenReturn(Optional.of(testModel));
Employee testResponse = testService.getEmployee("2000");
assertEquals(testModel, testResponse);
}
}

View File

@ -26,7 +26,6 @@
<module>dip</module>
<module>cqrs-es</module>
<module>front-controller</module>
<module>hexagonal-architecture</module>
<module>intercepting-filter</module>
<module>solid</module>
<module>clean-architecture</module>

View File

@ -411,7 +411,6 @@
<module>javax-sound</module>
<module>javaxval</module>
<module>javaxval-2</module>
<module>javaxval2</module>
<module>javax-validation-advanced</module>
<module>jaxb</module>
<module>jersey</module>
@ -797,7 +796,6 @@
<module>javax-sound</module>
<module>javaxval</module>
<module>javaxval-2</module>
<module>javaxval2</module>
<module>javax-validation-advanced</module>
<module>jaxb</module>
<module>jersey</module>