Skip to content
Permalink
Browse files
Added test case for response entity with annotations (#895)
  • Loading branch information
reta committed Jan 28, 2022
1 parent b176e8e commit bc36018b48a2e877432a90bf066deda81156183a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
@@ -22,6 +22,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -30,10 +31,14 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
@@ -54,9 +59,12 @@
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;
import javax.ws.rs.ext.WriterInterceptor;
@@ -102,6 +110,7 @@ protected Server createServer(Bus bus) throws Exception {
providers.add(new ServerTestFeature());
providers.add(new JacksonJaxbJsonProvider());
providers.add(new IOExceptionMapper());
providers.add(new GregorianCalendarMessageBodyWriter());
sf.setApplication(new Application());
sf.setProviders(providers);
sf.setResourceProvider(BookStore.class,
@@ -538,4 +547,27 @@ public Response toResponse(IOException ex) {
}

}

@Provider
private class GregorianCalendarMessageBodyWriter implements MessageBodyWriter<GregorianCalendar> {
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return GregorianCalendar.class.equals(type);
}

@Override
public void writeTo(GregorianCalendar t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
// Write in the following format: yyyy-MM-dd{ann1,ann2,...}
final String str = new SimpleDateFormat("yyyy-MM-dd").format(t.getTime())
+ "{"
+ Arrays
.stream(annotations)
.map(a -> a.annotationType().getName())
.collect(Collectors.joining(","))
+ "}";
entityStream.write(str.getBytes());
}
}
}
@@ -82,6 +82,7 @@
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@@ -1815,6 +1816,19 @@ public Response echoEntity(String entity) {
.build();
}

@GET
@Path("/annotated/{bookId}/")
@Produces(MediaType.TEXT_PLAIN)
public Response getGenericBookDate(@PathParam("bookId") String id) {
@Provider
@Consumes
class AnnotatedClass {
}

return Response.ok().entity(new GregorianCalendar(2020, 00, 01),
AnnotatedClass.class.getAnnotations()).build();
}

public final String init() {
books.clear();
cds.clear();
@@ -961,7 +961,19 @@ public void testQueryParamSpecialCharactersEncoded() throws Exception {
}
}


@Test
public void testGetBookDateAnnotated() throws Exception {
final String response = ClientBuilder
.newClient()
.target("http://localhost:" + PORT + "/bookstore/annotated/123")
.request(MediaType.TEXT_PLAIN)
.get()
.readEntity(String.class);

assertThat(response, equalTo("2020-01-01{javax.ws.rs.GET,javax.ws.rs.Path,javax.ws.rs.Produces,"
+ "javax.ws.rs.ext.Provider,javax.ws.rs.Consumes}"));
}

private static class ReplaceBodyFilter implements ClientRequestFilter {

@Override

0 comments on commit bc36018

Please sign in to comment.