Skip to content

Commit

Permalink
Merge "JERESY-2787: Incorrect WADL generated when entity parameter is…
Browse files Browse the repository at this point in the history
… annotated with unknown annotation"
  • Loading branch information
shamoh authored and Gerrit Code Review committed Sep 8, 2015
2 parents 923b797 + 42d674a commit 2f76133
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private void processRequestParameters(final org.glassfish.jersey.server.model.Re
final Collection<Parameter> requestParameters,
final Request wadlRequest) {
for (Parameter parameter : requestParameters) {
if (parameter.getSource() == Parameter.Source.ENTITY) {
if (parameter.getSource() == Parameter.Source.ENTITY || parameter.getSource() == Parameter.Source.UNKNOWN) {
for (MediaType mediaType : resourceMethod.getConsumedTypes()) {
setRepresentationForMediaType(parentResource, resourceMethod, mediaType, wadlRequest);
}
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1702,5 +1702,6 @@
<validation.impl.version>5.1.3.Final</validation.impl.version>
<weld.version>2.2.14.Final</weld.version>
<xerces.version>2.11.0</xerces.version>
<xmlunit.version>1.6</xmlunit.version>
</properties>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
Expand All @@ -62,6 +68,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
Expand All @@ -77,6 +84,7 @@
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
Expand Down Expand Up @@ -105,6 +113,12 @@
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.tests.e2e.entity.JaxbBean;

import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.ElementQualifier;
import org.custommonkey.xmlunit.SimpleNamespaceContext;
import org.custommonkey.xmlunit.XMLAssert;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -121,8 +135,12 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableMap;

import com.sun.research.ws.wadl.Method;
import com.sun.research.ws.wadl.Param;
import com.sun.research.ws.wadl.Request;
import com.sun.research.ws.wadl.Resource;
import com.sun.research.ws.wadl.Resources;

/**
Expand All @@ -145,7 +163,9 @@
WadlResourceTest.Wadl7Test.class,
WadlResourceTest.Wadl8Test.class,
WadlResourceTest.Wadl9Test.class,
WadlResourceTest.Wadl10Test.class})
WadlResourceTest.Wadl10Test.class,
WadlResourceTest.Wadl11Test.class,
})
public class WadlResourceTest {

/**
Expand Down Expand Up @@ -173,6 +193,13 @@ static Document extractWadlAsDocument(final Response response) throws ParserConf
return d;
}

private static String nodeAsString(final Object resourceNode) throws TransformerException {
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource((Node) resourceNode), new StreamResult(writer));
return writer.toString();
}

public static class Wadl7Test extends JerseyTest {

@Override
Expand Down Expand Up @@ -1160,4 +1187,123 @@ public void testWadl() throws Exception {
}

} // class Wadl10Test

/**
* Tests whether unknown annotation affects a WADL correctness.
*/
public static class Wadl11Test extends JerseyTest {

@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface UnknownAnnotation {
}

@Path("annotated")
@Consumes("application/json")
@UnknownAnnotation
public static class Annotated {

@Path("subresource")
@POST
@Produces("application/json")
@UnknownAnnotation
public Entity myMethod1(@UnknownAnnotation final Entity entity) {
return entity;
}

@Path("subresource")
@POST
@Produces("application/xml")
public Entity myMethod2(@UnknownAnnotation final Entity entity) {
return entity;
}

}

@Path("not-annotated")
@Consumes("application/json")
public static class Plain {

@Path("subresource")
@POST
@Produces("application/json")
public Entity myMethod1(final Entity entity) {
return entity;
}

@Path("subresource")
@POST
@Produces("application/xml")
public Entity myMethod2(final Entity entity) {
return entity;
}

}

@Override
protected Application configure() {
return new ResourceConfig(Annotated.class, Plain.class);
}

@Test
public void testWadlIsComplete() throws Exception {
final Response response = target("/application.wadl").request().get();

assertThat(response.getStatus(), is(200));
assertThat(response.hasEntity(), is(true));

final com.sun.research.ws.wadl.Application application =
response.readEntity(com.sun.research.ws.wadl.Application.class);

// "annotated/subresource"
final Resource resource =
(Resource) application.getResources().get(0).getResource().get(0).getMethodOrResource().get(0);

assertThatMethodContainsRR(resource, "myMethod1", 0);
assertThatMethodContainsRR(resource, "myMethod2", 1);

}

private static void assertThatMethodContainsRR(final Resource resource, final String methodName, final int methodIndex) {
final Method method = (Method) resource.getMethodOrResource().get(methodIndex);

assertThat(method.getId(), equalTo(methodName));

final Request request = method.getRequest();
final List<com.sun.research.ws.wadl.Response> response = method.getResponse();

assertThat(request, notNullValue());
assertThat(response.isEmpty(), is(false));
}

@Test
public void testWadlIsSameForAnnotatedAndNot() throws Exception {

final Response response = target("/application.wadl").request().get();

final Document document = extractWadlAsDocument(response);

final XPath xp = XPathFactory.newInstance().newXPath();
final SimpleNamespaceResolver nsContext = new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02");
xp.setNamespaceContext(nsContext);

final Diff diff = XMLUnit.compareXML(
nodeAsString(
xp.evaluate("//wadl:resource[@path='annotated']/wadl:resource", document,
XPathConstants.NODE)),
nodeAsString(
xp.evaluate("//wadl:resource[@path='not-annotated']/wadl:resource", document,
XPathConstants.NODE))
);
XMLUnit.setXpathNamespaceContext(
new SimpleNamespaceContext(ImmutableMap.of("wadl", "http://wadl.dev.java.net/2009/02")));
final ElementQualifier elementQualifier = new RecursiveElementNameAndTextQualifier();
diff.overrideElementQualifier(elementQualifier);
XMLAssert.assertXMLEqual(diff, true);

}

} // class Wadl11Test

}

0 comments on commit 2f76133

Please sign in to comment.