Hexagonal Architecture in Java

This commit is contained in:
yashasvi
2021-09-11 13:18:48 +05:45
parent 6d318be3d9
commit 1add21c1fe
7 changed files with 240 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package com.baeldung.pattern.simplehexagonalexample.controller;
import com.baeldung.pattern.simplehexagonalexample.domain.model.Furniture;
import com.baeldung.pattern.simplehexagonalexample.domain.services.FurnitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/furniture")
public class FurnitureController {
@Autowired
private FurnitureService furnitureService;
@PostMapping
public void manufactureFurniture(@RequestBody Furniture furniture) {
furnitureService.manufactureFurniture(furniture);
}
@GetMapping(path = "/{name}")
public Furniture getFurniture(@PathVariable String name) {
return furnitureService.getFurniture(name);
}
@GetMapping
public List<Furniture> listFurniture() {
return furnitureService.listAllFurniture();
}
}

View File

@@ -0,0 +1,43 @@
package com.baeldung.pattern.simplehexagonalexample.domain.model;
import java.io.Serializable;
public class Furniture implements Serializable {
private String name;
private String color;
private int length;
private int breadth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getBreadth() {
return breadth;
}
public void setBreadth(int breadth) {
this.breadth = breadth;
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.pattern.simplehexagonalexample.domain.services;
import com.baeldung.pattern.simplehexagonalexample.domain.model.Furniture;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface FurnitureService {
Furniture manufactureFurniture(Furniture furniture);
Furniture getFurniture(String name);
List<Furniture> listAllFurniture();
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.pattern.simplehexagonalexample.domain.services;
import com.baeldung.pattern.simplehexagonalexample.domain.model.Furniture;
import com.baeldung.pattern.simplehexagonalexample.persistance.FurnitureRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class FurnitureServiceImpl implements FurnitureService {
private final FurnitureRepository furnitureRepository;
@Autowired
public FurnitureServiceImpl(FurnitureRepository furnitureRepository) {
this.furnitureRepository = furnitureRepository;
}
@Override
public Furniture manufactureFurniture(Furniture furniture) {
return furnitureRepository.manufactureFurniture(furniture);
}
@Override
public Furniture getFurniture(String name) {
return furnitureRepository.getFurniture(name);
}
@Override
public List<Furniture> listAllFurniture() {
return furnitureRepository.listAllFurniture();
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.pattern.simplehexagonalexample.persistance;
import com.baeldung.pattern.simplehexagonalexample.domain.model.Furniture;
import java.util.List;
public interface FurnitureRepository {
Furniture manufactureFurniture(Furniture furniture);
Furniture getFurniture(String name);
List<Furniture> listAllFurniture();
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.pattern.simplehexagonalexample.persistance;
import com.baeldung.pattern.simplehexagonalexample.domain.model.Furniture;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FurnitureRepositoryImpl implements FurnitureRepository {
private final Map<String, Furniture> furnitureStore = new HashMap<>();
@Override
public Furniture manufactureFurniture(Furniture furniture) {
return furnitureStore.put(furniture.getName(), furniture);
}
@Override
public Furniture getFurniture(String name) {
return furnitureStore.get(name);
}
@Override
public List<Furniture> listAllFurniture() {
return null;
}
}

View File

@@ -0,0 +1,71 @@
package com.baeldung.pattern.hexagonal.domain.services;
import com.baeldung.pattern.simplehexagonalexample.domain.model.Furniture;
import com.baeldung.pattern.simplehexagonalexample.domain.services.FurnitureService;
import com.baeldung.pattern.simplehexagonalexample.domain.services.FurnitureServiceImpl;
import com.baeldung.pattern.simplehexagonalexample.persistance.FurnitureRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FurnitureServiceImplUnitTest {
private final List<Furniture> testModelList = new ArrayList<>();
private FurnitureRepository furnitureRepository;
private FurnitureService testService;
private Furniture testModel1;
private Furniture testModel2;
@BeforeEach
void setUp() {
furnitureRepository = mock(FurnitureRepository.class);
testService = new FurnitureServiceImpl(furnitureRepository);
testModel1 = new Furniture();
testModel1.setName("bed");
testModel1.setColor("brown");
testModel1.setLength(50);
testModel1.setBreadth(40);
testModel2 = new Furniture();
testModel2.setName("sofa");
testModel2.setColor("brown");
testModel2.setLength(50);
testModel2.setBreadth(20);
testModelList.add(testModel1);
testModelList.add(testModel2);
}
@Test
void manufactureFurniture() {
when(furnitureRepository.manufactureFurniture(any(Furniture.class))).thenReturn(testModel1);
Furniture testResponse = testService.manufactureFurniture(testModel1);
assertEquals(testModel1, testResponse);
}
@Test
void getFurniture() {
when(furnitureRepository.getFurniture("bed")).thenReturn(testModel1);
Furniture testResponse = testService.getFurniture("bed");
assertEquals(testModel1, testResponse);
}
@Test
void listAllFurniture() {
when(furnitureRepository.listAllFurniture()).thenReturn(Arrays.asList(testModel1, testModel2));
List<Furniture> testResponse = testService.listAllFurniture();
assertEquals(testModelList, testResponse);
}
}