[JAVA-14174] Renamed paterns to paterns-module (#12718)
* [JAVA-14174] Renamed paterns to paterns-module * [JAVA-14174] naming fixes Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
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 {
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
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);
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
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);
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
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> {
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user