PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/core-server/src/test/java/org/glassfish/jersey/server/modelapi/annotation/IntrospectionModellerTest.java

https://gitlab.com/jaragan/jersey
Java | 235 lines | 152 code | 36 blank | 47 comment | 2 complexity | 20946c1c1d5d3fc933f1ad8b816e7a76 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 2011-2015 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * The contents of this file are subject to the terms of either the GNU
  7. * General Public License Version 2 only ("GPL") or the Common Development
  8. * and Distribution License("CDDL") (collectively, the "License"). You
  9. * may not use this file except in compliance with the License. You can
  10. * obtain a copy of the License at
  11. * http://glassfish.java.net/public/CDDL+GPL_1_1.html
  12. * or packager/legal/LICENSE.txt. See the License for the specific
  13. * language governing permissions and limitations under the License.
  14. *
  15. * When distributing the software, include this License Header Notice in each
  16. * file and include the License file at packager/legal/LICENSE.txt.
  17. *
  18. * GPL Classpath Exception:
  19. * Oracle designates this particular file as subject to the "Classpath"
  20. * exception as provided by Oracle in the GPL Version 2 section of the License
  21. * file that accompanied this code.
  22. *
  23. * Modifications:
  24. * If applicable, add the following below the License Header, with the fields
  25. * enclosed by brackets [] replaced by your own identifying information:
  26. * "Portions Copyright [year] [name of copyright owner]"
  27. *
  28. * Contributor(s):
  29. * If you wish your version of this file to be governed by only the CDDL or
  30. * only the GPL Version 2, indicate your decision by adding "[Contributor]
  31. * elects to include this software in this distribution under the [CDDL or GPL
  32. * Version 2] license." If you don't indicate a single choice of license, a
  33. * recipient has the option to distribute your version of this file under
  34. * either the CDDL, the GPL Version 2 or to extend the choice of license to
  35. * its licensees as provided above. However, if you add GPL Version 2 code
  36. * and therefore, elected the GPL Version 2 license, then the option applies
  37. * only if the new code is made subject to such option by the copyright
  38. * holder.
  39. */
  40. package org.glassfish.jersey.server.modelapi.annotation;
  41. import java.util.Collection;
  42. import java.util.List;
  43. import javax.ws.rs.Consumes;
  44. import javax.ws.rs.GET;
  45. import javax.ws.rs.HeaderParam;
  46. import javax.ws.rs.POST;
  47. import javax.ws.rs.Path;
  48. import javax.ws.rs.PathParam;
  49. import javax.ws.rs.Produces;
  50. import javax.ws.rs.QueryParam;
  51. import javax.ws.rs.core.Context;
  52. import javax.ws.rs.core.UriInfo;
  53. import javax.inject.Inject;
  54. import org.glassfish.jersey.server.model.Parameter;
  55. import org.glassfish.jersey.server.model.Resource;
  56. import org.glassfish.jersey.server.model.ResourceMethod;
  57. import org.hamcrest.Matchers;
  58. import org.junit.Test;
  59. import static org.junit.Assert.assertEquals;
  60. import static org.junit.Assert.assertThat;
  61. import jersey.repackaged.com.google.common.base.Function;
  62. import jersey.repackaged.com.google.common.collect.Collections2;
  63. /**
  64. * @author Jakub Podlesak (jakub.podlesak at oracle.com)
  65. */
  66. public class IntrospectionModellerTest {
  67. static String data;
  68. @Path("/helloworld")
  69. @Produces(" a/b, c/d ")
  70. @Consumes({"e/f,g/h", " i/j"})
  71. public static class HelloWorldResource {
  72. @POST
  73. @Consumes(" a/b, c/d ")
  74. @Produces({"e/f,g/h", " i/j"})
  75. public String postA(final String data) {
  76. return data;
  77. }
  78. @POST
  79. public String postB(final String data) {
  80. return data;
  81. }
  82. }
  83. @Path("/other/{path}")
  84. public static class OtherResource {
  85. private String queryParam;
  86. public String getQueryParam() {
  87. return queryParam;
  88. }
  89. @QueryParam("q")
  90. public void setQueryParam(final String queryParam) {
  91. this.queryParam = queryParam;
  92. }
  93. @PathParam("path")
  94. private String pathParam;
  95. public String getPathParam() {
  96. return pathParam;
  97. }
  98. public void setPathParam(final String pathParam) {
  99. this.pathParam = pathParam;
  100. }
  101. @Context
  102. UriInfo uriInfo;
  103. @Inject
  104. public String annotatedButNotParam;
  105. @Inject
  106. public void setAnnotatedButNotParam(final String annotatedButNotParam) {
  107. this.annotatedButNotParam = annotatedButNotParam;
  108. }
  109. @GET
  110. public String get(@HeaderParam("h") String headerParam) {
  111. return headerParam + data;
  112. }
  113. @POST
  114. public String post(final String data) {
  115. return data;
  116. }
  117. }
  118. public IntrospectionModellerTest() {
  119. }
  120. @Test
  121. /**
  122. * Test of createResource method, of class IntrospectionModeller.
  123. */
  124. public void testCreateResource() {
  125. Class<?> resourceClass;
  126. Resource result;
  127. List<ResourceMethod> resourceMethods;
  128. ResourceMethod resourceMethod;
  129. // HelloWorldResource
  130. resourceClass = HelloWorldResource.class;
  131. result = Resource.builder(resourceClass).build();
  132. resourceMethods = result.getResourceMethods();
  133. assertEquals("Unexpected number of resource methods in the resource model.", 2, resourceMethods.size());
  134. resourceMethod = find(resourceMethods, "postA");
  135. assertEquals("Unexpected number of produced media types in the resource method model",
  136. 3, resourceMethod.getProducedTypes().size());
  137. assertEquals("Unexpected number of consumed media types in the resource method model",
  138. 2, resourceMethod.getConsumedTypes().size());
  139. assertEquals("Unexpected number of handler parameters",
  140. 0, resourceMethod.getInvocable().getHandler().getParameters().size());
  141. resourceMethod = find(resourceMethods, "postB");
  142. assertEquals("Unexpected number of inherited produced media types in the resource method model",
  143. 2, resourceMethod.getProducedTypes().size());
  144. assertEquals("Unexpected number of inherited consumed media types in the resource method model",
  145. 3, resourceMethod.getConsumedTypes().size());
  146. assertEquals("Unexpected number of handler parameters",
  147. 0, resourceMethod.getInvocable().getHandler().getParameters().size());
  148. // OtherResource
  149. resourceClass = OtherResource.class;
  150. result = Resource.builder(resourceClass).build();
  151. resourceMethods = result.getResourceMethods();
  152. assertEquals("Unexpected number of resource methods in the resource model.", 2, resourceMethods.size());
  153. resourceMethod = find(resourceMethods, "get");
  154. assertEquals("Unexpected number of produced media types in the resource method model",
  155. 0, resourceMethod.getProducedTypes().size());
  156. assertEquals("Unexpected number of consumed media types in the resource method model",
  157. 0, resourceMethod.getConsumedTypes().size());
  158. assertEquals("Unexpected number of handler parameters",
  159. 5, resourceMethod.getInvocable().getHandler().getParameters().size());
  160. assertSources(resourceMethod.getInvocable().getHandler().getParameters(),
  161. Parameter.Source.CONTEXT,
  162. Parameter.Source.PATH,
  163. Parameter.Source.QUERY,
  164. Parameter.Source.UNKNOWN, // @Inject on field
  165. Parameter.Source.UNKNOWN); // @Inject on setter
  166. resourceMethod = find(resourceMethods, "post");
  167. assertEquals("Unexpected number of inherited produced media types in the resource method model",
  168. 0, resourceMethod.getProducedTypes().size());
  169. assertEquals("Unexpected number of inherited consumed media types in the resource method model",
  170. 0, resourceMethod.getConsumedTypes().size());
  171. assertEquals("Unexpected number of handler parameters",
  172. 5, resourceMethod.getInvocable().getHandler().getParameters().size());
  173. assertSources(resourceMethod.getInvocable().getHandler().getParameters(),
  174. Parameter.Source.CONTEXT,
  175. Parameter.Source.PATH,
  176. Parameter.Source.QUERY,
  177. Parameter.Source.UNKNOWN, // @Inject on field
  178. Parameter.Source.UNKNOWN); // @Inject on setter
  179. }
  180. private ResourceMethod find(List<ResourceMethod> methods, String javaMethodName) {
  181. for (ResourceMethod method : methods) {
  182. if (method.getInvocable().getHandlingMethod().getName().equals(javaMethodName)) {
  183. return method;
  184. }
  185. }
  186. return null;
  187. }
  188. private void assertSources(Collection<Parameter> parameters, Parameter.Source... sources) {
  189. assertThat("Expected sources not found in the collection",
  190. Collections2.transform(parameters, new Function<Parameter, Parameter.Source>() {
  191. @Override
  192. public Parameter.Source apply(final Parameter parameter) {
  193. return parameter.getSource();
  194. }
  195. }),
  196. Matchers.containsInAnyOrder(sources)
  197. );
  198. }
  199. }