forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve dynamic resolution of MessageBodyWriter providers
Fixes: quarkusio#22119
- Loading branch information
Showing
2 changed files
with
175 additions
and
4 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
159 changes: 159 additions & 0 deletions
159
...est/java/org/jboss/resteasy/reactive/server/vertx/test/matching/MultipleProducesTest.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,159 @@ | ||
package org.jboss.resteasy.reactive.server.vertx.test.matching; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.function.Supplier; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.ext.MessageBodyWriter; | ||
import javax.ws.rs.ext.Provider; | ||
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
public class MultipleProducesTest { | ||
|
||
@RegisterExtension | ||
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest() | ||
.setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(Entity.class, FirstResource.class, SecondResource.class, ExcelMessageBodyWriter.class, | ||
TextPlainMessageBodyWriter.class, ApplicationJsonMessageBodyWriter.class); | ||
} | ||
}); | ||
|
||
@Test | ||
public void firstResourceShouldReturnJson() { | ||
doTest("first", "application/json", "application/json"); | ||
} | ||
|
||
@Test | ||
public void firstResourceShouldReturnMsExcel() { | ||
doTest("first", "application/vnd.ms-excel", "foo"); | ||
} | ||
|
||
@Test | ||
public void firstResourceShouldReturnTextPlain() { | ||
doTest("first", "text/plain", "text/plain"); | ||
} | ||
|
||
@Test | ||
public void secondResourceShouldReturnJson() { | ||
doTest("second", "application/json", "application/json"); | ||
} | ||
|
||
@Test | ||
public void secondResourceShouldReturnMsExcel() { | ||
doTest("second", "application/vnd.ms-excel", "bar"); | ||
} | ||
|
||
@Test | ||
public void secondResourceShouldReturnTextPlain() { | ||
doTest("second", "text/plain", "text/plain"); | ||
} | ||
|
||
private void doTest(String path, String acceptType, String expectBody) { | ||
given() | ||
.accept(acceptType) | ||
.when().get(path) | ||
.then() | ||
.statusCode(200) | ||
.contentType(acceptType) | ||
.body(is(expectBody)); | ||
} | ||
|
||
public static class Entity { | ||
|
||
public final String data; | ||
|
||
public Entity(String data) { | ||
this.data = data; | ||
} | ||
} | ||
|
||
@Path("first") | ||
public static class FirstResource { | ||
|
||
@GET | ||
@Produces({ "application/vnd.ms-excel", MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN }) | ||
public Response get() { | ||
return Response.ok().entity(new Entity("foo")).build(); | ||
} | ||
} | ||
|
||
@Path("second") | ||
public static class SecondResource { | ||
|
||
@GET | ||
@Produces({ MediaType.APPLICATION_JSON, "application/vnd.ms-excel", MediaType.TEXT_PLAIN }) | ||
public Response get() { | ||
return Response.ok().entity(new Entity("bar")).build(); | ||
} | ||
} | ||
|
||
@Provider | ||
public static class ExcelMessageBodyWriter implements MessageBodyWriter<Entity> { | ||
|
||
@Override | ||
public boolean isWriteable(Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType) { | ||
return clazz.equals(Entity.class) && | ||
mediaType.getType().equals("application") && | ||
mediaType.getSubtype().equals("vnd.ms-excel"); | ||
} | ||
|
||
@Override | ||
public void writeTo(Entity entity, Class<?> aClass, Type type, Annotation[] annotations, | ||
MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, | ||
OutputStream outputStream) throws IOException, WebApplicationException { | ||
|
||
outputStream.write(entity.data.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
} | ||
|
||
@Provider | ||
public static class TextPlainMessageBodyWriter implements MessageBodyWriter<Entity> { | ||
|
||
@Override | ||
public boolean isWriteable(Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType) { | ||
return clazz.equals(Entity.class) && mediaType.isCompatible(MediaType.TEXT_PLAIN_TYPE); | ||
} | ||
|
||
@Override | ||
public void writeTo(Entity myResponseEntity, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, | ||
MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) | ||
throws IOException, WebApplicationException { | ||
outputStream.write("text/plain".getBytes()); | ||
} | ||
} | ||
|
||
@Provider | ||
public static class ApplicationJsonMessageBodyWriter implements MessageBodyWriter<Entity> { | ||
|
||
@Override | ||
public boolean isWriteable(Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType) { | ||
return clazz.equals(Entity.class) && mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE); | ||
} | ||
|
||
@Override | ||
public void writeTo(Entity myResponseEntity, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, | ||
MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) | ||
throws IOException, WebApplicationException { | ||
outputStream.write("application/json".getBytes()); | ||
} | ||
} | ||
} |