-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12902 from machi1990/fix/12898
Fix RedisClientName and MongoClientName can't be applied to parameters
- Loading branch information
Showing
12 changed files
with
343 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...ongodb-client/src/main/java/io/quarkus/it/mongodb/BookResourceWithParameterInjection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.quarkus.it.mongodb; | ||
|
||
import static com.mongodb.client.model.Filters.eq; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
import com.mongodb.client.FindIterable; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
|
||
import io.quarkus.mongodb.MongoClientName; | ||
|
||
@Path("/books-with-parameter-injection") | ||
public class BookResourceWithParameterInjection { | ||
private MongoClient client; | ||
private MongoCollection<Book> collection; | ||
|
||
@Inject | ||
public BookResourceWithParameterInjection(@MongoClientName("parameter-injection") MongoClient client) { | ||
this.client = client; | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
MongoDatabase database = client.getDatabase("books-with-parameter-injection"); | ||
collection = database.getCollection("my-collection", Book.class); | ||
} | ||
|
||
@GET | ||
public List<Book> getBooks() { | ||
FindIterable<Book> iterable = collection.find(); | ||
List<Book> books = new ArrayList<>(); | ||
for (Book doc : iterable) { | ||
books.add(doc); | ||
} | ||
return books; | ||
} | ||
|
||
@POST | ||
public Response addBook(Book book) { | ||
collection.insertOne(book); | ||
return Response.accepted().build(); | ||
} | ||
|
||
@GET | ||
@Path("/{author}") | ||
public List<Book> getBooksByAuthor(@PathParam("author") String author) { | ||
FindIterable<Book> iterable = collection.find(eq("author", author)); | ||
List<Book> books = new ArrayList<>(); | ||
for (Book doc : iterable) { | ||
String title = doc.getTitle(); | ||
books.add(new Book().setTitle(title).setAuthor(author)); | ||
} | ||
return books; | ||
} | ||
|
||
} |
4 changes: 3 additions & 1 deletion
4
integration-tests/mongodb-client/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
quarkus.mongodb.connection-string=mongodb://localhost:27018 | ||
quarkus.mongodb.write-concern.journal=false | ||
quarkus.mongodb.write-concern.journal=false | ||
quarkus.mongodb.parameter-injection.connection-string=mongodb://localhost:27018 | ||
quarkus.mongodb.parameter-injection.write-concern.journal=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...db-client/src/test/java/io/quarkus/it/mongodb/BookResourceWithParameterInjectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.it.mongodb; | ||
|
||
import static org.hamcrest.Matchers.*; | ||
|
||
import javax.json.bind.Jsonb; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(MongoTestResource.class) | ||
public class BookResourceWithParameterInjectionTest { | ||
|
||
private static Jsonb jsonb; | ||
|
||
@BeforeAll | ||
public static void giveMeAMapper() { | ||
jsonb = Utils.initialiseJsonb(); | ||
} | ||
|
||
@AfterAll | ||
public static void releaseMapper() throws Exception { | ||
jsonb.close(); | ||
} | ||
|
||
@Test | ||
public void testInjectedClient() { | ||
Utils.callTheEndpoint("/books-with-parameter-injection"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...lient/src/test/java/io/quarkus/it/mongodb/NativeBookResourceWithParameterInjectionIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.quarkus.it.mongodb; | ||
|
||
import io.quarkus.test.junit.NativeImageTest; | ||
|
||
@NativeImageTest | ||
class NativeBookResourceWithParameterInjectionIT extends BookResourceWithParameterInjectionTest { | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
integration-tests/mongodb-client/src/test/java/io/quarkus/it/mongodb/Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package io.quarkus.it.mongodb; | ||
|
||
import static io.restassured.RestAssured.get; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.json.bind.Jsonb; | ||
import javax.json.bind.JsonbBuilder; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.common.mapper.TypeRef; | ||
import io.restassured.config.ObjectMapperConfig; | ||
import io.restassured.mapper.ObjectMapper; | ||
import io.restassured.mapper.ObjectMapperDeserializationContext; | ||
import io.restassured.mapper.ObjectMapperSerializationContext; | ||
import io.restassured.response.Response; | ||
|
||
class Utils { | ||
static Jsonb initialiseJsonb() { | ||
Jsonb jsonb = JsonbBuilder.create(); | ||
ObjectMapper mapper = new ObjectMapper() { | ||
@Override | ||
public Object deserialize(ObjectMapperDeserializationContext context) { | ||
return jsonb.fromJson(context.getDataToDeserialize().asString(), context.getType()); | ||
} | ||
|
||
@Override | ||
public Object serialize(ObjectMapperSerializationContext context) { | ||
return jsonb.toJson(context.getObjectToSerialize()); | ||
} | ||
}; | ||
RestAssured.config().objectMapperConfig(ObjectMapperConfig.objectMapperConfig().defaultObjectMapper(mapper)); | ||
|
||
return jsonb; | ||
} | ||
|
||
static void callTheEndpoint(String endpoint) { | ||
List<Book> list = get(endpoint).as(new TypeRef<List<Book>>() { | ||
}); | ||
Assertions.assertEquals(0, list.size()); | ||
|
||
Book book1 = new Book().setAuthor("Victor Hugo").setTitle("Les Misérables") | ||
.setCategories(Arrays.asList("long", "very long")) | ||
.setDetails(new BookDetail().setRating(3).setSummary("A very long book")); | ||
Response response = RestAssured | ||
.given() | ||
.header("Content-Type", "application/json") | ||
.body(book1) | ||
.post(endpoint) | ||
.andReturn(); | ||
Assertions.assertEquals(202, response.statusCode()); | ||
|
||
Book book2 = new Book().setAuthor("Victor Hugo").setTitle("Notre-Dame de Paris") | ||
.setCategories(Arrays.asList("long", "quasimodo")) | ||
.setDetails(new BookDetail().setRating(4).setSummary("quasimodo and esmeralda")); | ||
response = RestAssured | ||
.given() | ||
.header("Content-Type", "application/json") | ||
.body(book2) | ||
.post(endpoint) | ||
.andReturn(); | ||
Assertions.assertEquals(202, response.statusCode()); | ||
|
||
list = get(endpoint).as(new TypeRef<List<Book>>() { | ||
}); | ||
Assertions.assertEquals(2, list.size()); | ||
|
||
Book book3 = new Book().setAuthor("Charles Baudelaire").setTitle("Les fleurs du mal") | ||
.setCategories(Collections.singletonList("poem")) | ||
.setDetails(new BookDetail().setRating(2).setSummary("Les Fleurs du mal is a volume of poetry.")); | ||
response = RestAssured | ||
.given() | ||
.header("Content-Type", "application/json") | ||
.body(book3) | ||
.post(endpoint) | ||
.andReturn(); | ||
Assertions.assertEquals(202, response.statusCode()); | ||
|
||
list = get(endpoint).as(new TypeRef<List<Book>>() { | ||
}); | ||
Assertions.assertEquals(3, list.size()); | ||
|
||
list = get(endpoint + "/Victor Hugo").as(new TypeRef<List<Book>>() { | ||
}); | ||
Assertions.assertEquals(2, list.size()); | ||
} | ||
|
||
} |
Oops, something went wrong.