diff --git a/hexagonal-example/pom.xml b/hexagonal-example/pom.xml index 47bd8e7489..eaa05332a8 100644 --- a/hexagonal-example/pom.xml +++ b/hexagonal-example/pom.xml @@ -1,47 +1,100 @@ + 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.1.6.RELEASE - - + com.baeldung - HexagonalLibrary + hexagonalPattern 0.0.1-SNAPSHOT - HexagonalLibrary - HexagonalLibrary + + hexagonalPattern + + 1.8 + UTF-8 + 1.8 + 1.8 - org.springframework.boot - spring-boot-starter-web + org.junit.jupiter + junit-jupiter-engine + 5.1.0 + test - org.springframework.boot - spring-boot-starter-test + spring-boot-starter-parent + 2.2.1.RELEASE + pom + + + org.springframework.boot + spring-boot-starter-web + 2.2.1.RELEASE + + + org.mockito + mockito-core + 2.26.0 + + + org.mockito + mockito-core + 2.22.0 test - - - org.springframework.boot - spring-boot-maven-plugin - - com.baeldung.HexagonalLibrary - - - + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + - - \ No newline at end of file + diff --git a/hexagonal-example/src/com/baeldung/HexagonalLibrary.java b/hexagonal-example/src/com/baeldung/HexagonalLibrary.java deleted file mode 100644 index b730fa1244..0000000000 --- a/hexagonal-example/src/com/baeldung/HexagonalLibrary.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class HexagonalLibrary { - - public static void main(String[] args) { - SpringApplication.run(HexagonalLibrary.class, args); - } - -} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRepoImpl.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRepoImpl.java deleted file mode 100644 index 1b678a4e80..0000000000 --- a/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRepoImpl.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.hexagonalPattern.adapter; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import org.springframework.stereotype.Repository; - -import com.baeldung.hexagonalPattern.core.domain.Book; -import com.baeldung.hexagonalPattern.ports.LibraryRepo; - -@Repository -public class LibraryRepoImpl implements LibraryRepo { - // This class is the actual implementation of the out bound port/adapter. - - private HashMap books = new HashMap(); - - @Override - public void insertBook(Book book) { - // Mock Database call here. - books.put("mock", new Book("mock", "mock", "mock")); - } - - @Override - public Book searchBook(String name) { - // TODO Auto-generated method stub - Book b = new Book(); - // Some code for retrieval of book from db - return b; - } - - @Override - public List getAllBooks() { - // Fetch all books from db - return books.values().stream().collect(Collectors.toList()); - } - -} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRestController.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRestController.java deleted file mode 100644 index 176b8471b5..0000000000 --- a/hexagonal-example/src/com/baeldung/hexagonalPattern/adapter/LibraryRestController.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.hexagonalPattern.adapter; - -import java.util.List; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.hexagonalPattern.core.domain.Book; -import com.baeldung.hexagonalPattern.web.LibraryRestUI; - -@RestController -public class LibraryRestController implements LibraryRestUI { - - @Override - @RequestMapping("/library") - public void insertBook(Book book) { - // TODO Auto-generated method stub - - } - - @Override - @GetMapping("/searchBook") - public Book searchBook(@PathVariable String name) { - // TODO Auto-generated method stub - return null; - } - - @Override - @GetMapping("/listBooks") - public List listAllBooks() { - // TODO Auto-generated method stub - return null; - } - -} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/core/domain/Book.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/core/domain/Book.java deleted file mode 100644 index 38e761ba50..0000000000 --- a/hexagonal-example/src/com/baeldung/hexagonalPattern/core/domain/Book.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.hexagonalPattern.core.domain; - -public class Book { - - private String name; - private String author_list; - private String isbn13; - - /* constructors and getter and setters */ - public Book(String string, String string2, String string3) { - // TODO Auto-generated constructor stub - } - - public Book() { - // TODO Auto-generated constructor stub - } - - /* - * public String getName() { return name; } - * - * public void setName(String name) { this.name = name; } - * - * public String getAuthor_list() { return author_list; } - * - * public void setAuthor_list(String author_list) { this.author_list = - * author_list; } - * - * public String getIsbn13() { return isbn13; } - * - * public void setIsbn13(String isbn13) { this.isbn13 = isbn13; } - */ - -} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java deleted file mode 100644 index 1bde7c8d0e..0000000000 --- a/hexagonal-example/src/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.hexagonalPattern.core.impl; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import com.baeldung.hexagonalPattern.core.domain.Book; -import com.baeldung.hexagonalPattern.ports.LibraryRepo; -import com.baeldung.hexagonalPattern.ports.LibraryService; - -@Service -public class LibraryServiceImpl implements LibraryService { - - // This is the class which actually implements the methods from the ports. - // The ports are just for exposing the methods to the outside. - - @Autowired - private LibraryRepo bookRepo; - - @Override - public void insertBook(Book book) { - // TODO some implementation to insert record in the db or similar - bookRepo.insertBook(book); - } - - @Override - public Book lendBook(String name) { - // TODO Auto-generated method stub - return bookRepo.searchBook(name); - } - - @Override - public List getAllBooks() { - // TODO Auto-generated method - return null; - } - -} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryService.java b/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryService.java deleted file mode 100644 index 7391b4795d..0000000000 --- a/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryService.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.hexagonalPattern.ports; - -import java.util.List; -import com.baeldung.hexagonalPattern.core.domain.Book; - -public interface LibraryService { -//This is the in bound port.exposes the application to the world. - - public void insertBook(Book book); - - public Book lendBook(String name); - - List getAllBooks(); - -} diff --git a/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/HexagonalLibraryApplication.java b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/HexagonalLibraryApplication.java new file mode 100644 index 0000000000..62f35111db --- /dev/null +++ b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/HexagonalLibraryApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.hexagonalPattern; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HexagonalLibraryApplication { + public static void main(String[] args) { + SpringApplication.run(HexagonalLibraryApplication.class, args); + } +} diff --git a/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/adapters/LibraryRepoImpl.java b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/adapters/LibraryRepoImpl.java new file mode 100644 index 0000000000..c8fe584419 --- /dev/null +++ b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/adapters/LibraryRepoImpl.java @@ -0,0 +1,40 @@ +package com.baeldung.hexagonalPattern.adapters; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.springframework.stereotype.Repository; + +import com.baeldung.hexagonalPattern.core.domain.Book; +import com.baeldung.hexagonalPattern.ports.LibraryRepo; + +@Repository +public class LibraryRepoImpl implements LibraryRepo { + // This class is the actual implementation of the out bound port/adapter. + + private HashMap books = new HashMap(); + + @Override + public int insertBook(Book book) { + // Mock Database call here. + books.put("mock", new Book("mock", "mock", "mock")); + return 1; + } + + @Override + public Book searchBook(String name) { + // TODO Auto-generated method stub + Book b = new Book(); + // Some code for retrieval of book from db + return b; + } + + @Override + public List getAllBooks() { + // Fetch all books from db + return books.values().stream().collect(Collectors.toList()); + } + +} diff --git a/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/adapters/LibraryRestController.java b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/adapters/LibraryRestController.java new file mode 100644 index 0000000000..92c4b0a8a7 --- /dev/null +++ b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/adapters/LibraryRestController.java @@ -0,0 +1,38 @@ +package com.baeldung.hexagonalPattern.adapters; + +import java.util.List; + +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.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.hexagonalPattern.core.domain.Book; +import com.baeldung.hexagonalPattern.web.LibraryRestUI; + +@RestController +@RequestMapping("/library") +public class LibraryRestController implements LibraryRestUI { + + @Override + @PostMapping("/insertBook") + public int insertBook(Book book) { + return 0; + } + + @Override + @GetMapping("/searchBook") + public Book searchBook(@PathVariable String name) { + // TODO Auto-generated method stub + return null; + } + + @Override + @GetMapping("/listBooks") + public List listAllBooks() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/core/domain/Book.java b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/core/domain/Book.java new file mode 100644 index 0000000000..9551d0f09d --- /dev/null +++ b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/core/domain/Book.java @@ -0,0 +1,33 @@ +package com.baeldung.hexagonalPattern.core.domain; + +public class Book { + + private String name; + private String author_list; + private String isbn13; + + /* constructors and getter and setters */ + public Book(String string, String string2, String string3) { + // TODO Auto-generated constructor stub + } + + public Book() { + // TODO Auto-generated constructor stub + } + + /* + * public String getName() { return name; } + * + * public void setName(String name) { this.name = name; } + * + * public String getAuthor_list() { return author_list; } + * + * public void setAuthor_list(String author_list) { this.author_list = + * author_list; } + * + * public String getIsbn13() { return isbn13; } + * + * public void setIsbn13(String isbn13) { this.isbn13 = isbn13; } + */ + +} diff --git a/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java new file mode 100644 index 0000000000..de64e46f36 --- /dev/null +++ b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/core/impl/LibraryServiceImpl.java @@ -0,0 +1,33 @@ +package com.baeldung.hexagonalPattern.core.impl; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.hexagonalPattern.adapters.LibraryRepoImpl; +import com.baeldung.hexagonalPattern.core.domain.Book; +import com.baeldung.hexagonalPattern.ports.LibraryService; + +@Service +public class LibraryServiceImpl implements LibraryService { + + @Autowired + private LibraryRepoImpl bookRepo = new LibraryRepoImpl(); + + @Override + public int insertBook(Book book) { + bookRepo.insertBook(book); + return 1; + } + + @Override + public Book searchBook(String name) { + return bookRepo.searchBook(name); + } + + @Override + public List getAllBooks() { + return bookRepo.getAllBooks(); + } +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryRepo.java b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/ports/LibraryRepo.java similarity index 55% rename from hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryRepo.java rename to hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/ports/LibraryRepo.java index 83858403ef..99de3c2e93 100644 --- a/hexagonal-example/src/com/baeldung/hexagonalPattern/ports/LibraryRepo.java +++ b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/ports/LibraryRepo.java @@ -4,11 +4,10 @@ import java.util.List; import com.baeldung.hexagonalPattern.core.domain.Book; public interface LibraryRepo { - // Outbound Port. - public void insertBook(Book book); + public int insertBook(Book book); - public Book searchBook(String name); + public Book searchBook(String name); - public List getAllBooks(); + public List getAllBooks(); } diff --git a/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/ports/LibraryService.java b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/ports/LibraryService.java new file mode 100644 index 0000000000..947ea402dc --- /dev/null +++ b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/ports/LibraryService.java @@ -0,0 +1,15 @@ +package com.baeldung.hexagonalPattern.ports; + +import java.util.List; + +import com.baeldung.hexagonalPattern.core.domain.Book; + +public interface LibraryService { + + public int insertBook(Book book); + + public Book searchBook(String name); + + public List getAllBooks(); + +} diff --git a/hexagonal-example/src/com/baeldung/hexagonalPattern/web/LibraryRestUI.java b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/web/LibraryRestUI.java similarity index 63% rename from hexagonal-example/src/com/baeldung/hexagonalPattern/web/LibraryRestUI.java rename to hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/web/LibraryRestUI.java index 16d7beac87..e212fca33d 100644 --- a/hexagonal-example/src/com/baeldung/hexagonalPattern/web/LibraryRestUI.java +++ b/hexagonal-example/src/main/java/com/baeldung/hexagonalPattern/web/LibraryRestUI.java @@ -10,15 +10,15 @@ import org.springframework.web.bind.annotation.RequestBody; import com.baeldung.hexagonalPattern.core.domain.Book; public interface LibraryRestUI { - // This is the in bound Adapter + // This is the in bound Adapter - @PostMapping - void insertBook(@RequestBody Book book); + @PostMapping + public int insertBook(@RequestBody Book book); - @GetMapping("/{name}") - public Book searchBook(@PathVariable String name); + @GetMapping + public Book searchBook(@PathVariable String name); - @GetMapping - public List listAllBooks(); + @GetMapping + public List listAllBooks(); } diff --git a/hexagonal-example/src/test/java/com/baeldung/hexagonalPattern/HexagonalLibraryApplicationTest.java b/hexagonal-example/src/test/java/com/baeldung/hexagonalPattern/HexagonalLibraryApplicationTest.java new file mode 100644 index 0000000000..bee384eb7d --- /dev/null +++ b/hexagonal-example/src/test/java/com/baeldung/hexagonalPattern/HexagonalLibraryApplicationTest.java @@ -0,0 +1,46 @@ +package com.baeldung.hexagonalPattern; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +import com.baeldung.hexagonalPattern.core.domain.Book; +import com.baeldung.hexagonalPattern.core.impl.LibraryServiceImpl; + +@RunWith(MockitoJUnitRunner.class) +public class HexagonalLibraryApplicationTest { + + @Mock + private LibraryServiceImpl libService = org.mockito.Mockito.mock(LibraryServiceImpl.class);; + Book bk = new Book(); + Book book; + + @Test + public void testInsertBook() { + Mockito.when(libService.insertBook(bk)).thenReturn(1); + int returnVal = libService.insertBook(bk); + assertEquals(1, returnVal); + } + + @Test + public void testSearchBook() { + Book returnBook = new Book(); + Mockito.lenient().when(libService.searchBook("Pride and Prejudice")).thenReturn(returnBook); + assertNotNull(returnBook); + } + + @Test + public void testGetAllBooks() { + List returnBookList = new ArrayList(); + returnBookList = libService.getAllBooks(); + assertNotNull(returnBookList); + } +}