component test for add new book added
This commit is contained in:
@@ -2,15 +2,23 @@ package io.wkrzywiec.hexagonal.library.application;
|
|||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
import io.restassured.response.ValidatableResponse;
|
import io.restassured.response.ValidatableResponse;
|
||||||
|
import io.wkrzywiec.hexagonal.library.TestData;
|
||||||
|
import io.wkrzywiec.hexagonal.library.domain.book.model.BookDetailsDTO;
|
||||||
|
import io.wkrzywiec.hexagonal.library.domain.book.model.ExternalBookIdDTO;
|
||||||
|
import io.wkrzywiec.hexagonal.library.infrastructure.repository.BookEntity;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.web.server.LocalServerPort;
|
import org.springframework.boot.web.server.LocalServerPort;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.RestAssured.given;
|
||||||
import static org.hamcrest.Matchers.greaterThan;
|
import static org.hamcrest.Matchers.greaterThan;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
public class AddNewBookTest {
|
public class AddNewBookTest {
|
||||||
@@ -18,6 +26,9 @@ public class AddNewBookTest {
|
|||||||
@LocalServerPort
|
@LocalServerPort
|
||||||
private int port;
|
private int port;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private JdbcTemplate jdbc;
|
||||||
|
|
||||||
private String baseURL;
|
private String baseURL;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
@@ -33,7 +44,7 @@ public class AddNewBookTest {
|
|||||||
ValidatableResponse response = given()
|
ValidatableResponse response = given()
|
||||||
.when()
|
.when()
|
||||||
.param("query", "lean startup")
|
.param("query", "lean startup")
|
||||||
.get( baseURL + "/google/books")
|
.get( baseURL + "/google/books")
|
||||||
.prettyPeek()
|
.prettyPeek()
|
||||||
.then();
|
.then();
|
||||||
|
|
||||||
@@ -41,6 +52,32 @@ public class AddNewBookTest {
|
|||||||
response.statusCode(HttpStatus.OK.value())
|
response.statusCode(HttpStatus.OK.value())
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
.body("items.size()", greaterThan(0));
|
.body("items.size()", greaterThan(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Add new book to a database")
|
||||||
|
public void givenGoogleBooId_whenAddNewBook_thenBookIsSaved(){
|
||||||
|
//given
|
||||||
|
BookDetailsDTO homoDeusBookDetails = TestData.homoDeusBookDetailsDTO();
|
||||||
|
ExternalBookIdDTO googleBookId =
|
||||||
|
ExternalBookIdDTO.builder()
|
||||||
|
.value(homoDeusBookDetails.getBookExternalId())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
//when
|
||||||
|
ValidatableResponse response =
|
||||||
|
given()
|
||||||
|
.contentType("application/json")
|
||||||
|
.body(googleBookId)
|
||||||
|
.when()
|
||||||
|
.post( baseURL + "/books")
|
||||||
|
.prettyPeek()
|
||||||
|
.then();
|
||||||
|
|
||||||
|
//then
|
||||||
|
String homoDeusSql = "select * from book where book_external_id = '" + homoDeusBookDetails.getBookExternalId() + "'";
|
||||||
|
BookEntity savedBook = (BookEntity) jdbc.queryForObject(homoDeusSql, new BeanPropertyRowMapper(BookEntity.class));
|
||||||
|
assertEquals(homoDeusBookDetails.getTitle(), savedBook.getTitle());
|
||||||
|
assertEquals(homoDeusBookDetails.getTitle(), savedBook.getTitle());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ public class BookCommandController {
|
|||||||
private final AddNewBook addNewBook;
|
private final AddNewBook addNewBook;
|
||||||
|
|
||||||
@PostMapping("")
|
@PostMapping("")
|
||||||
ResponseEntity<String> addNewBook(ExternalBookIdDTO externalBookIdDTO){
|
ResponseEntity<String> addNewBook(@RequestBody ExternalBookIdDTO externalBookIdDTO){
|
||||||
addNewBook.handle(externalBookIdDTO);
|
addNewBook.handle(externalBookIdDTO);
|
||||||
return new ResponseEntity<>("New book was added to library", HttpStatus.CREATED);
|
return new ResponseEntity<>("New book was added to library", HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package io.wkrzywiec.hexagonal.library.domain.book.model;
|
package io.wkrzywiec.hexagonal.library.domain.book.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Value;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@Value
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
@Builder
|
@Builder
|
||||||
public class ExternalBookIdDTO {
|
public class ExternalBookIdDTO {
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class GoogleBooksAdapter implements GetBookDetails {
|
|||||||
public BookDetailsDTO handle(String googleBookId) {
|
public BookDetailsDTO handle(String googleBookId) {
|
||||||
|
|
||||||
HttpHeaders requestHeader = new HttpHeaders();
|
HttpHeaders requestHeader = new HttpHeaders();
|
||||||
requestHeader.add("Accept", MediaType.APPLICATION_JSON_VALUE);
|
// requestHeader.add("Accept", MediaType.ALL_VALUE);
|
||||||
HttpEntity<Object> requestEntity = new HttpEntity<>(requestHeader);
|
HttpEntity<Object> requestEntity = new HttpEntity<>(requestHeader);
|
||||||
|
|
||||||
ResponseEntity<String> responseEntity =
|
ResponseEntity<String> responseEntity =
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package io.wkrzywiec.hexagonal.library.infrastructure.repository;
|
package io.wkrzywiec.hexagonal.library.infrastructure.repository;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.CascadeType;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
@@ -13,11 +17,16 @@ import javax.persistence.JoinColumn;
|
|||||||
import javax.persistence.JoinTable;
|
import javax.persistence.JoinTable;
|
||||||
import javax.persistence.ManyToMany;
|
import javax.persistence.ManyToMany;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
@Builder
|
@Builder
|
||||||
@Entity
|
@Entity
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Setter
|
||||||
|
@Getter
|
||||||
@Table(name="book")
|
@Table(name="book")
|
||||||
public class BookEntity {
|
public class BookEntity {
|
||||||
|
|
||||||
@@ -51,12 +60,12 @@ public class BookEntity {
|
|||||||
@Column(name="publishedDate")
|
@Column(name="publishedDate")
|
||||||
private String publishedDate;
|
private String publishedDate;
|
||||||
|
|
||||||
@Column(name="description")
|
@Column(name="description", columnDefinition="TEXT")
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@Column(name="pages")
|
@Column(name="pages")
|
||||||
private int pages;
|
private int pages;
|
||||||
|
|
||||||
@Column(name="imageLink")
|
@Column(name="imageLink", columnDefinition="TEXT")
|
||||||
private String imageLink;
|
private String imageLink;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import com.tngtech.archunit.lang.ArchRule;
|
|||||||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
|
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
|
||||||
|
|
||||||
@AnalyzeClasses(packages = "io.wkrzywiec.hexagonal.library.domain")
|
@AnalyzeClasses(packages = "io.wkrzywiec.hexagonal.library.domain")
|
||||||
public class NoSpringInDomainPackageTest {
|
public class HexagonalArchitectureTest {
|
||||||
|
|
||||||
@ArchTest
|
@ArchTest
|
||||||
public static final ArchRule noSpringDependenciesInDomainPackage =
|
public static final ArchRule noSpringDependenciesInDomainPackage =
|
||||||
Reference in New Issue
Block a user