committing code changes with a simple Junit test. corrected the project

structure as well.
This commit is contained in:
rvsathe
2021-03-22 13:17:17 +05:30
parent da32163216
commit f92573f298
16 changed files with 303 additions and 210 deletions
@@ -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);
}
}