return savedBook by database
This commit is contained in:
@@ -19,6 +19,7 @@ public class InventoryFacade implements AddNewBook{
|
||||
@Override
|
||||
public void handle(AddNewBookCommand addNewBookCommand){
|
||||
Book book = getBookDetails.handle(addNewBookCommand.getGoogleBookId());
|
||||
database.save(book);
|
||||
Book savedBook = database.save(book);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ class InventoryDatabaseAdapter implements InventoryDatabase {
|
||||
private final BookRepository repository;
|
||||
|
||||
@Override
|
||||
public void save(Book book) {
|
||||
repository.save(book);
|
||||
public Book save(Book book) {
|
||||
return repository.save(book);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,6 +67,10 @@ public class Book {
|
||||
this.imageLink = imageLink;
|
||||
}
|
||||
|
||||
public Long getIdAsLong(){
|
||||
return id;
|
||||
}
|
||||
|
||||
private Book() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.model;
|
||||
|
||||
public class NewBookWasAddedEvent {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.wkrzywiec.hexagonal.library.inventory.ports.outgoing;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.NewBookWasAddedEvent;
|
||||
|
||||
public interface EventPublisher {
|
||||
void publishNewBookWasAddedEvent(NewBookWasAddedEvent event);
|
||||
}
|
||||
@@ -3,5 +3,5 @@ package io.wkrzywiec.hexagonal.library.inventory.ports.outgoing;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Book;
|
||||
|
||||
public interface InventoryDatabase {
|
||||
void save(Book book);
|
||||
Book save(Book book);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.wkrzywiec.hexagonal.library.inventory;
|
||||
|
||||
import io.wkrzywiec.hexagonal.library.inventory.model.Book;
|
||||
import io.wkrzywiec.hexagonal.library.inventory.ports.outgoing.InventoryDatabase;
|
||||
import org.apache.commons.lang3.reflect.FieldUtils;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -11,8 +12,15 @@ public class InMemoryInventoryDatabase implements InventoryDatabase {
|
||||
ConcurrentHashMap<Long, Book> books = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void save(Book book) {
|
||||
public Book save(Book book) {
|
||||
Long id = books.size() + 1L;
|
||||
|
||||
try {
|
||||
FieldUtils.writeField(book, "id", id, true);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
books.put(id, book);
|
||||
return book;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
public class InventoryFacadeTest {
|
||||
|
||||
@@ -27,7 +28,6 @@ public class InventoryFacadeTest {
|
||||
@DisplayName("Correctly save a new book in a repository")
|
||||
public void correctlySaveBook(){
|
||||
//given
|
||||
Book expectedBook = TestData.homoDeusBook();
|
||||
AddNewBookCommand externalBookId = AddNewBookCommand
|
||||
.builder()
|
||||
.googleBookId(TestData.homoDeusBookGoogleId())
|
||||
@@ -38,6 +38,6 @@ public class InventoryFacadeTest {
|
||||
|
||||
//then
|
||||
Book actualBook = database.books.get(1L);
|
||||
assertEquals(expectedBook, actualBook);
|
||||
assertNotNull(actualBook);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@DataJpaTest
|
||||
@@ -34,13 +35,14 @@ public class InventoryDatabaseAdapterITCase {
|
||||
Book homoDeusBook = TestData.homoDeusBook();
|
||||
|
||||
//when
|
||||
database.save(homoDeusBook);
|
||||
Book savedBook = database.save(homoDeusBook);
|
||||
|
||||
//then
|
||||
Long savedBookId = jdbcTemplate.queryForObject(
|
||||
"SELECT id FROM book",
|
||||
Long.class);
|
||||
"SELECT id FROM book WHERE id = ?",
|
||||
Long.class,
|
||||
savedBook.getIdAsLong());
|
||||
|
||||
assertTrue(savedBookId > 0);
|
||||
assertEquals(savedBook.getIdAsLong(), savedBookId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user