Skip to content

Commit

Permalink
Merge pull request #121 from Sgitario/enable_test_for_13307
Browse files Browse the repository at this point in the history
Updates after adding basic exception handling in REST services
  • Loading branch information
Sgitario authored Mar 9, 2021
2 parents 068504e + 49b9bf2 commit 14dde25
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Book(String title, String description, int publicationYear) {
this.publicationYear = publicationYear;
}

@NotBlank(message = "Title may not be blank")
@NotBlank(message = "Title cannot be blank")
@ProtoField(number = 1)
public String getTitle() {
return title;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.qe.books;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.http.HttpStatus;
Expand Down Expand Up @@ -33,4 +34,14 @@ public void testBookResource() {

assertEquals(BOOK, actual);
}

@Test
public void testBookResourceShouldValidateBook() {
given()
.contentType(ContentType.JSON)
.body(new Book())
.when().post("/book/add")
.then().statusCode(HttpStatus.SC_BAD_REQUEST)
.body(containsString("Title cannot be blank"));
}
}
4 changes: 0 additions & 4 deletions 011-quarkus-panache-rest-flyway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ Topics covered here:
- https://quarkus.io/guides/flyway

In the future, I would like to cover different database at the same time.

## Disabled tests

`PostgreSqlApplicationResourceTest#shouldReturnBadRequestIfApplicationNameIsNull` - see https://github.com/quarkusio/quarkus/issues/13307
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@Filter(name = "useLikeByName", condition = "name like '%' || :name || '%'")
@FilterDef(name = "useServiceByName", parameters = { @ParamDef(name = "name", type = "string") })
public class ApplicationEntity extends PanacheEntity {
@NotEmpty
@NotEmpty(message = "name can't be null")
@Column(unique = true, nullable = false)
public String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.quarkus.qe.containers.PostgreSqlDatabaseTestResource;
Expand Down Expand Up @@ -68,16 +67,13 @@ public void shouldDeleteApplication() {
thenApplicationsCountIs(0);
}

/**
* This test is disabled because the conflict exception raised by hibernate validator is wrapping it up by the rollback
* exception which ends up in a HTTP 500 Internal Server Error instead of a HTTP 409 Conflict.
*/
@Disabled("Caused by https://github.com/quarkusio/quarkus/issues/13307.")
@Test
public void shouldReturnBadRequestIfApplicationNameIsNull() {
applicationPath().body(new ApplicationEntity()).post()
.then()
.statusCode(HttpStatus.SC_CONFLICT);
.statusCode(HttpStatus.SC_BAD_REQUEST);
// TODO: Body assertion is not working caused by https://github.com/quarkusio/quarkus/issues/15492
// .body(containsString("name can't be null"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package org.acme.spring.data.rest;

import com.sun.xml.bind.v2.model.core.ID;
import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;

import java.util.List;
import java.util.Optional;

@RepositoryRestResource(exported=false, path = "books", collectionResourceRel = "books")
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {

@Override
@RestResource(exported = true)
Page<Book> findAll(Pageable pageable);

@RestResource(exported = true)
List<Book> findByOrderByNameDesc();

@Override
@RestResource(path = "id")
Optional<Book> findById(ID id);
Optional<Book> findById(Long id);

@Override
@RestResource()
Book save(Book book);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.acme.spring.data.rest;

import com.sun.xml.bind.v2.model.core.ID;
import java.util.Optional;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RestResource;

import java.util.Optional;

public interface LibraryRepository extends CrudRepository<Library, Long> {
@Override
@RestResource(path = "id")
Optional<Library> findById(ID id);
Optional<Library> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package org.acme.spring.data.rest;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;

import io.restassured.response.Response;
import org.acme.spring.data.rest.containers.PostgreSqlDatabaseTestResource;
import org.hibernate.annotations.SQLUpdate;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.core.IsNot.not;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.core.IsNot.not;
import org.acme.spring.data.rest.containers.PostgreSqlDatabaseTestResource;
import org.apache.http.HttpStatus;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;

@QuarkusTest
@QuarkusTestResource(PostgreSqlDatabaseTestResource.class)
Expand Down Expand Up @@ -118,10 +118,11 @@ void testRepositoryValidator() throws InterruptedException{
.body("{\"name\": \"Q\", \"author\": \"Li\"}")
.when().post("/books")
.then()
.statusCode(500)
.body(
containsString("length must be between 2 and 50"),
containsString("propertyPath=name")
);
.statusCode(HttpStatus.SC_BAD_REQUEST);
// TODO: Body assertion is not working caused by https://github.com/quarkusio/quarkus/issues/15492
// .body(
// containsString("length must be between 2 and 50"),
// containsString("propertyPath=name")
//);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import org.acme.spring.data.rest.containers.PostgreSqlDatabaseTestResource;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
Expand Down Expand Up @@ -79,9 +80,10 @@ void testRepositoryValidator() throws InterruptedException{
.body("{\"name\": \"\"}")
.when().post("/library")
.then()
.statusCode(500)
.body(
containsString("Name may not be blank")
);
.statusCode(HttpStatus.SC_BAD_REQUEST);
// TODO: Body assertion is not working caused by https://github.com/quarkusio/quarkus/issues/15492
//.body(
// containsString("Name may not be blank")
//);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.acme.spring.data.rest;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
Expand Down

0 comments on commit 14dde25

Please sign in to comment.