committing code changes with a simple Junit test. corrected the project
structure as well.
This commit is contained in:
parent
da32163216
commit
f92573f298
|
@ -1,47 +1,100 @@
|
|||
<?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>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.6.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>HexagonalLibrary</artifactId>
|
||||
<artifactId>hexagonalPattern</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>HexagonalLibrary</name>
|
||||
<description>HexagonalLibrary</description>
|
||||
|
||||
<name>hexagonalPattern</name>
|
||||
<!-- FIXME change it to the project's website -->
|
||||
<!-- <url>http://www.example.com</url> -->
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.1.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>2.26.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>2.22.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.HexagonalLibrary</mainClass>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven
|
||||
defaults (may be moved to parent pom) -->
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>2.5.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
</plugin>
|
||||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>3.7.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String, Book> books = new HashMap<String, Book>();
|
||||
|
||||
@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<Book> getAllBooks() {
|
||||
// Fetch all books from db
|
||||
return books.values().stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Book> listAllBooks() {
|
||||
// TODO Auto-generated method stub
|
||||
return 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; }
|
||||
*/
|
||||
|
||||
}
|
|
@ -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<Book> getAllBooks() {
|
||||
// TODO Auto-generated method
|
||||
return 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<Book> getAllBooks();
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<String, Book> books = new HashMap<String, Book>();
|
||||
|
||||
@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<Book> getAllBooks() {
|
||||
// Fetch all books from db
|
||||
return books.values().stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Book> listAllBooks() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -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; }
|
||||
*/
|
||||
|
||||
}
|
|
@ -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<Book> getAllBooks() {
|
||||
return bookRepo.getAllBooks();
|
||||
}
|
||||
}
|
|
@ -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<Book> getAllBooks();
|
||||
public List<Book> getAllBooks();
|
||||
}
|
|
@ -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<Book> getAllBooks();
|
||||
|
||||
}
|
|
@ -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<Book> listAllBooks();
|
||||
@GetMapping
|
||||
public List<Book> listAllBooks();
|
||||
|
||||
}
|
|
@ -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<Book> returnBookList = new ArrayList<Book>();
|
||||
returnBookList = libService.getAllBooks();
|
||||
assertNotNull(returnBookList);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue