kkaravitis@gmail.com hexagonal architecture in java

This commit is contained in:
kkaravitis 2020-03-08 19:58:47 +02:00
parent f2d45b8269
commit aab469525e
12 changed files with 323 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<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>hexagonal-architecture-example</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>patterns</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.baeldung.hexagonal.architecture.example.infrastructure.CocktailsMenuApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,46 @@
package com.baeldung.hexagonal.architecture.example.adapters.inbound;
import java.util.List;
import java.util.Scanner;
import com.baeldung.hexagonal.architecture.example.application.domain.Cocktail;
import com.baeldung.hexagonal.architecture.example.application.ports.in.AddCocktailPort;
import com.baeldung.hexagonal.architecture.example.application.ports.in.AddCocktailPort.AddCocktailCommand;
import com.baeldung.hexagonal.architecture.example.application.ports.in.GetCocktailsPort;
public class CommandLineAdapter {
private AddCocktailPort addCocktailPort;
private GetCocktailsPort listCocktailsPort;
private Scanner scanner;
public CommandLineAdapter(Scanner scanner, AddCocktailPort addCocktailPort, GetCocktailsPort listCocktailsPort) {
this.addCocktailPort = addCocktailPort;
this.listCocktailsPort = listCocktailsPort;
this.scanner = scanner;
}
public void addCocktail() {
try {
System.out.println("Enter the name of the cocktail");
scanner.nextLine();
String name = scanner.nextLine();
System.out.println("Enter the price of the cocktail");
double price = scanner.nextDouble();
scanner.nextLine();
AddCocktailCommand command = new AddCocktailCommand(name, price);
addCocktailPort.addCocktail(command);
} catch (Exception exception) {
System.out.println("Error during adding a new Cocktail");
}
}
public void listCocktails() {
List<Cocktail> cocktailList = listCocktailsPort.getCocktails();
System.out.println("Cocktails menu: \n");
cocktailList.stream()
.forEach(cocktail -> {
System.out.println(cocktail.getName() + "\n" +
"price: " + cocktail.getPrice() + " $ " + "\n");
});
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.hexagonal.architecture.example.adapters.outbound;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baeldung.hexagonal.architecture.example.application.domain.Cocktail;
import com.baeldung.hexagonal.architecture.example.application.ports.out.FetchCocktailsPort;
import com.baeldung.hexagonal.architecture.example.application.ports.out.SaveCocktailPort;
public class CocktailRepository implements FetchCocktailsPort, SaveCocktailPort {
private Map<String, Cocktail> store = new HashMap<>();
public List<Cocktail> fetchCocktails() {
return new ArrayList<>(store.values());
}
public void saveCocktail(Cocktail cocktail) {
store.put(cocktail.getName(), cocktail);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.hexagonal.architecture.example.application.domain;
public class Cocktail {
private String name;
private double price;
public Cocktail(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.hexagonal.architecture.example.application.ports.in;
public interface AddCocktailPort {
void addCocktail(AddCocktailCommand command);
class AddCocktailCommand {
private String name;
private double price;
public AddCocktailCommand(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.hexagonal.architecture.example.application.ports.in;
import java.util.List;
import com.baeldung.hexagonal.architecture.example.application.domain.Cocktail;
public interface GetCocktailsPort {
List<Cocktail> getCocktails();
}

View File

@ -0,0 +1,19 @@
package com.baeldung.hexagonal.architecture.example.application.service;
import com.baeldung.hexagonal.architecture.example.application.domain.Cocktail;
import com.baeldung.hexagonal.architecture.example.application.ports.in.AddCocktailPort;
import com.baeldung.hexagonal.architecture.example.application.ports.out.SaveCocktailPort;
public class AddCocktailService implements AddCocktailPort {
private SaveCocktailPort saveCocktailPort;
public AddCocktailService(SaveCocktailPort saveCocktailPort) {
this.saveCocktailPort = saveCocktailPort;
}
@Override
public void addCocktail(AddCocktailCommand command) {
Cocktail cocktail = new Cocktail(command.getName(), command.getPrice());
saveCocktailPort.saveCocktail(cocktail);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.hexagonal.architecture.example.application.service;
import java.util.List;
import com.baeldung.hexagonal.architecture.example.application.domain.Cocktail;
import com.baeldung.hexagonal.architecture.example.application.ports.in.GetCocktailsPort;
import com.baeldung.hexagonal.architecture.example.application.ports.out.FetchCocktailsPort;
public class GetCocktailsService implements GetCocktailsPort {
private FetchCocktailsPort fetchCocktailsPort;
public GetCocktailsService(FetchCocktailsPort fetchCocktailsPort) {
this.fetchCocktailsPort = fetchCocktailsPort;
}
@Override
public List<Cocktail> getCocktails() {
return fetchCocktailsPort.fetchCocktails();
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.hexagonal.architecture.example.infrastructure;
import java.util.Scanner;
import com.baeldung.hexagonal.architecture.example.adapters.inbound.CommandLineAdapter;
import com.baeldung.hexagonal.architecture.example.adapters.outbound.CocktailRepository;
import com.baeldung.hexagonal.architecture.example.application.service.AddCocktailService;
import com.baeldung.hexagonal.architecture.example.application.service.GetCocktailsService;
public class CocktailsMenuApp {
private static final String ADD = "add";
private static final String LIST = "list";
private static final String EXIT = "exit";
private static final String HELP_TEXT = "Enter command \n" +
ADD + " - add a new Cocktail to the menu \n" +
LIST + " - list cocktails \n" +
EXIT + " - exit \n";
private static Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
CocktailRepository cocktailRepository = new CocktailRepository();
AddCocktailService addCocktailService = new AddCocktailService(cocktailRepository);
GetCocktailsService getCocktailsMenuService = new GetCocktailsService(cocktailRepository);
CommandLineAdapter commandLineAdapter = new CommandLineAdapter(SCANNER, addCocktailService, getCocktailsMenuService);
runWith(commandLineAdapter);
}
public static void runWith(CommandLineAdapter adapter) {
String command = null;
while (!EXIT.equalsIgnoreCase(command)) {
System.out.println(HELP_TEXT);
command = SCANNER.next();
switch (command.toLowerCase()) {
case ADD:
adapter.addCocktail();
break;
case LIST:
adapter.listCocktails();
break;
}
}
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.hexagonal.architecture.example.application;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.baeldung.hexagonal.architecture.example.application.domain.Cocktail;
import com.baeldung.hexagonal.architecture.example.application.ports.in.AddCocktailPort.AddCocktailCommand;
import com.baeldung.hexagonal.architecture.example.application.ports.out.SaveCocktailPort;
import com.baeldung.hexagonal.architecture.example.application.service.AddCocktailService;
class AddCocktailUnitTest {
SaveCocktailPort saveCocktailPort;
AddCocktailService addCocktailService;
@BeforeEach
void setup() {
saveCocktailPort = mock(SaveCocktailPort.class);
addCocktailService = new AddCocktailService(saveCocktailPort);
}
@Test
void whenAddingACocktail_thenTheCocktailIsSaved() {
AddCocktailCommand command = mock(AddCocktailCommand.class);
when(command.getName()).thenReturn("mockito");
when(command.getPrice()).thenReturn(9.50);
ArgumentCaptor<Cocktail> captor = ArgumentCaptor.forClass(Cocktail.class);
addCocktailService.addCocktail(command);
Mockito.verify(saveCocktailPort).saveCocktail(captor.capture());
Cocktail cocktailThatSaved = captor.getValue();
assertEquals(command.getName(), cocktailThatSaved.getName());
assertEquals(command.getPrice(), cocktailThatSaved.getPrice());
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.hexagonal.architecture.example.application;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.baeldung.hexagonal.architecture.example.application.domain.Cocktail;
import com.baeldung.hexagonal.architecture.example.application.ports.out.FetchCocktailsPort;
import com.baeldung.hexagonal.architecture.example.application.service.GetCocktailsService;
class GetCocktailsUnitTest {
FetchCocktailsPort fetchCocktailsPort;
GetCocktailsService getCocktailsService;
@BeforeEach
void setup() {
fetchCocktailsPort = mock(FetchCocktailsPort.class);
getCocktailsService = new GetCocktailsService(fetchCocktailsPort);
}
@Test
void whenGettingCocktails_thenCocktailsFetchedFromPortAndReturned() {
Cocktail cocktail = mock(Cocktail.class);
when(cocktail.getName()).thenReturn("zombie");
when(cocktail.getPrice()).thenReturn(12.00);
when(fetchCocktailsPort.fetchCocktails()).thenReturn(List.of(cocktail));
List<Cocktail> cocktails = getCocktailsService.getCocktails();
assertEquals(1, cocktails.size());
assertEquals(cocktail.getName(), cocktails.get(0).getName());
assertEquals(cocktail.getPrice(), cocktails.get(0).getPrice());
}
}

View File

@ -24,6 +24,7 @@
<module>front-controller</module> <module>front-controller</module>
<module>intercepting-filter</module> <module>intercepting-filter</module>
<module>solid</module> <module>solid</module>
<module>hexagonal-architecture-example</module>
</modules> </modules>
<dependencyManagement> <dependencyManagement>