return savedBook by database

This commit is contained in:
wkrzywiec
2020-05-21 16:24:58 +02:00
parent 8bea3936b7
commit f613a67517
9 changed files with 38 additions and 12 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -67,6 +67,10 @@ public class Book {
this.imageLink = imageLink;
}
public Long getIdAsLong(){
return id;
}
private Book() {
}
}

View File

@@ -0,0 +1,4 @@
package io.wkrzywiec.hexagonal.library.inventory.model;
public class NewBookWasAddedEvent {
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}