PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/server/validation/validateonexecution/ValidateOnExecutionBasicTest.java

https://gitlab.com/jaragan/jersey
Java | 482 lines | 334 code | 88 blank | 60 comment | 0 complexity | 47c5d6f557e42b9406397208ec0fb117 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 2013-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.tests.e2e.server.validation.validateonexecution;
  41. import javax.ws.rs.GET;
  42. import javax.ws.rs.POST;
  43. import javax.ws.rs.Path;
  44. import javax.ws.rs.client.Entity;
  45. import javax.ws.rs.client.WebTarget;
  46. import javax.ws.rs.core.Application;
  47. import javax.ws.rs.core.Response;
  48. import javax.inject.Singleton;
  49. import javax.validation.Valid;
  50. import javax.validation.constraints.Max;
  51. import javax.validation.constraints.Min;
  52. import javax.validation.constraints.NotNull;
  53. import javax.validation.executable.ExecutableType;
  54. import javax.validation.executable.ValidateOnExecution;
  55. import org.glassfish.jersey.server.ResourceConfig;
  56. import org.glassfish.jersey.server.ServerProperties;
  57. import org.glassfish.jersey.test.TestProperties;
  58. import org.glassfish.jersey.test.util.runner.ConcurrentRunner;
  59. import org.glassfish.jersey.test.util.runner.RunSeparately;
  60. import org.junit.Test;
  61. import org.junit.runner.RunWith;
  62. import static org.hamcrest.CoreMatchers.equalTo;
  63. import static org.junit.Assert.assertThat;
  64. /**
  65. * @author Michal Gajdos
  66. */
  67. @RunWith(ConcurrentRunner.class)
  68. public class ValidateOnExecutionBasicTest extends ValidateOnExecutionAbstractTest {
  69. /**
  70. * On METHOD.
  71. */
  72. @Path("on-method")
  73. public static class ValidateExecutableOnMethodsResource {
  74. @POST
  75. @Path("validateExecutableDefault")
  76. @ValidateOnExecution
  77. @Min(0)
  78. public Integer validateExecutableDefault(@Max(10) final Integer value) {
  79. return value;
  80. }
  81. @POST
  82. @Path("validateExecutableMatch")
  83. @ValidateOnExecution(type = ExecutableType.NON_GETTER_METHODS)
  84. @Min(0)
  85. public Integer validateExecutableMatch(@Max(10) final Integer value) {
  86. return value;
  87. }
  88. @POST
  89. @Path("validateExecutableMiss")
  90. @ValidateOnExecution(type = ExecutableType.CONSTRUCTORS)
  91. @Min(0)
  92. public Integer validateExecutableMiss(@Max(10) final Integer value) {
  93. return value;
  94. }
  95. @POST
  96. @Path("validateExecutableNone")
  97. @ValidateOnExecution(type = ExecutableType.NONE)
  98. @Min(0)
  99. public Integer validateExecutableNone(@Max(10) final Integer value) {
  100. return value;
  101. }
  102. }
  103. /**
  104. * On TYPE.
  105. */
  106. public abstract static class ValidateExecutableOnType {
  107. @POST
  108. @Min(0)
  109. public Integer validateExecutable(@Max(10) final Integer value) {
  110. return value;
  111. }
  112. }
  113. @Path("on-type-default")
  114. @ValidateOnExecution
  115. public static class ValidateExecutableOnTypeDefault extends ValidateExecutableOnType {
  116. }
  117. @Path("on-type-match")
  118. @ValidateOnExecution(type = ExecutableType.NON_GETTER_METHODS)
  119. public static class ValidateExecutableOnTypeMatch extends ValidateExecutableOnType {
  120. }
  121. @Path("on-type-miss")
  122. @ValidateOnExecution(type = ExecutableType.CONSTRUCTORS)
  123. public static class ValidateExecutableOnTypeMiss extends ValidateExecutableOnType {
  124. }
  125. @Path("on-type-none")
  126. @ValidateOnExecution(type = ExecutableType.NONE)
  127. public static class ValidateExecutableOnTypeNone extends ValidateExecutableOnType {
  128. }
  129. /**
  130. * MIXED.
  131. */
  132. @Path("mixed-default")
  133. @ValidateOnExecution(type = ExecutableType.NONE)
  134. public static class ValidateExecutableMixedDefault {
  135. @POST
  136. @Min(0)
  137. @ValidateOnExecution
  138. public Integer validateExecutable(@Max(10) final Integer value) {
  139. return value;
  140. }
  141. }
  142. @Path("mixed-none")
  143. @ValidateOnExecution
  144. public static class ValidateExecutableMixedNone {
  145. @POST
  146. @Min(0)
  147. @ValidateOnExecution(type = ExecutableType.NONE)
  148. public Integer validateExecutable(@Max(10) final Integer value) {
  149. return value;
  150. }
  151. }
  152. /**
  153. * GETTERS.
  154. */
  155. public abstract static class ValidateGetterExecutable {
  156. @GET
  157. @Path("sanity")
  158. public String getSanity() {
  159. return "ok";
  160. }
  161. }
  162. @Path("getter-on-method-default")
  163. public static class ValidateGetterExecutableOnMethodDefault extends ValidateGetterExecutable {
  164. @GET
  165. @ValidateOnExecution
  166. @NotNull
  167. public String getDefault() {
  168. return null;
  169. }
  170. }
  171. @Path("getter-on-method-miss")
  172. public static class ValidateGetterExecutableOnMethodMiss extends ValidateGetterExecutable {
  173. @GET
  174. @ValidateOnExecution(type = ExecutableType.NON_GETTER_METHODS)
  175. @NotNull
  176. public String getMiss() {
  177. return null;
  178. }
  179. }
  180. @Path("getter-on-method-match")
  181. public static class ValidateGetterExecutableOnMethodMatch extends ValidateGetterExecutable {
  182. @GET
  183. @ValidateOnExecution(type = {ExecutableType.NON_GETTER_METHODS, ExecutableType.GETTER_METHODS})
  184. @NotNull
  185. public String getMatch() {
  186. return null;
  187. }
  188. }
  189. @Path("getter-on-type-default")
  190. @ValidateOnExecution
  191. public static class ValidateGetterExecutableOnTypeDefault extends ValidateGetterExecutable {
  192. @GET
  193. @NotNull
  194. public String getDefault() {
  195. return null;
  196. }
  197. }
  198. @Path("getter-on-type-miss")
  199. @ValidateOnExecution(type = ExecutableType.NON_GETTER_METHODS)
  200. public static class ValidateGetterExecutableOnTypeMiss extends ValidateGetterExecutable {
  201. @GET
  202. @NotNull
  203. public String getMiss() {
  204. return null;
  205. }
  206. }
  207. @Path("getter-on-type-match")
  208. @ValidateOnExecution(type = {ExecutableType.NON_GETTER_METHODS, ExecutableType.GETTER_METHODS})
  209. public static class ValidateGetterExecutableOnTypeMatch extends ValidateGetterExecutable {
  210. @GET
  211. @NotNull
  212. public String getMatch() {
  213. return null;
  214. }
  215. }
  216. /**
  217. * BEANS.
  218. */
  219. @Path("getter-on-beans")
  220. public static class ValidateGetterExecutableOnBeans {
  221. @POST
  222. @Valid
  223. public AnotherContactBean post(@Valid final AnotherContactBean bean) {
  224. return bean;
  225. }
  226. @POST
  227. @Path("invalidMail")
  228. @Valid
  229. public AnotherContactBean invalidMail(@Valid final AnotherContactBean bean) {
  230. bean.setEmail("ab");
  231. return bean;
  232. }
  233. @POST
  234. @Path("invalidPhone")
  235. @Valid
  236. public AnotherContactBean invalidPhone(@Valid final AnotherContactBean bean) {
  237. bean.setPhone("12");
  238. return bean;
  239. }
  240. }
  241. @Path("getter-resource-method")
  242. @Singleton
  243. public static class ValidateGetterResourceMethod {
  244. private int count = 1;
  245. @GET
  246. @Max(1)
  247. public int getValue() {
  248. return count++;
  249. }
  250. }
  251. @Path("on-type-getter-null")
  252. @ValidateOnExecution(type = ExecutableType.NON_GETTER_METHODS)
  253. public static class ValidateExecutableResource {
  254. @Path("nogetter")
  255. @GET
  256. @NotNull
  257. public String daNull() {
  258. return null;
  259. }
  260. @Path("getter")
  261. @GET
  262. @NotNull
  263. public String getNull() {
  264. return null;
  265. }
  266. }
  267. @Override
  268. protected Application configure() {
  269. enable(TestProperties.LOG_TRAFFIC);
  270. enable(TestProperties.DUMP_ENTITY);
  271. return new ResourceConfig(ValidateExecutableOnMethodsResource.class,
  272. ValidateExecutableOnTypeDefault.class,
  273. ValidateExecutableOnTypeMatch.class,
  274. ValidateExecutableOnTypeMiss.class,
  275. ValidateExecutableOnTypeNone.class,
  276. ValidateExecutableMixedDefault.class,
  277. ValidateExecutableMixedNone.class,
  278. ValidateGetterExecutableOnMethodDefault.class,
  279. ValidateGetterExecutableOnMethodMiss.class,
  280. ValidateGetterExecutableOnMethodMatch.class,
  281. ValidateGetterExecutableOnTypeDefault.class,
  282. ValidateGetterExecutableOnTypeMiss.class,
  283. ValidateGetterExecutableOnTypeMatch.class,
  284. ValidateGetterExecutableOnBeans.class,
  285. ValidateGetterResourceMethod.class,
  286. ValidateExecutableResource.class)
  287. .property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
  288. }
  289. @Test
  290. public void testOnTypeValidateInputFailValidateExecutableDefault() throws Exception {
  291. _testOnType("default", 15, 400);
  292. }
  293. @Test
  294. @RunSeparately
  295. public void testOnTypeValidateResultFailValidateExecutableDefault() throws Exception {
  296. _testOnType("default", -15, 500);
  297. }
  298. @Test
  299. public void testOnMethodGetterDefault() throws Exception {
  300. final WebTarget target = target("getter-on-method-default");
  301. assertThat(target.request().get().getStatus(), equalTo(400));
  302. assertThat(target.path("sanity").request().get().getStatus(), equalTo(400));
  303. }
  304. @Test
  305. public void testOnMethodGetterMiss() throws Exception {
  306. final WebTarget target = target("getter-on-method-miss");
  307. Response response = target.request().get();
  308. assertThat(response.getStatus(), equalTo(400));
  309. response = target.path("sanity").request().get();
  310. assertThat(response.getStatus(), equalTo(400));
  311. }
  312. @Test
  313. public void testOnMethodGetterMatch() throws Exception {
  314. final WebTarget target = target("getter-on-method-match");
  315. assertThat(target.request().get().getStatus(), equalTo(400));
  316. assertThat(target.path("sanity").request().get().getStatus(), equalTo(400));
  317. }
  318. @Test
  319. public void testOnTypeGetterDefault() throws Exception {
  320. final WebTarget target = target("getter-on-type-default");
  321. assertThat(target.request().get().getStatus(), equalTo(400));
  322. assertThat(target.path("sanity").request().get().getStatus(), equalTo(400));
  323. }
  324. @Test
  325. public void testOnTypeGetterMiss() throws Exception {
  326. final WebTarget target = target("getter-on-type-miss");
  327. Response response = target.request().get();
  328. assertThat(response.getStatus(), equalTo(400));
  329. response = target.path("sanity").request().get();
  330. assertThat(response.getStatus(), equalTo(400));
  331. }
  332. /**
  333. * Validation should fail when getter (also a resource method) is invoked and not when the resource class is validated.
  334. */
  335. @Test
  336. public void testGetterResourceMethod() throws Exception {
  337. final WebTarget target = target("getter-resource-method");
  338. final Response response = target.request().get();
  339. assertThat(response.getStatus(), equalTo(500));
  340. }
  341. @Test
  342. public void testOnTypeGetterNull() throws Exception {
  343. final WebTarget target = target("on-type-getter-null");
  344. Response response = target.path("nogetter").request().get();
  345. assertThat(response.getStatus(), equalTo(400));
  346. response = target.path("getter").request().get();
  347. assertThat(response.getStatus(), equalTo(400));
  348. }
  349. @Test
  350. public void testOnTypeGetterMatch() throws Exception {
  351. final WebTarget target = target("getter-on-type-match");
  352. assertThat(target.request().get().getStatus(), equalTo(400));
  353. assertThat(target.path("sanity").request().get().getStatus(), equalTo(400));
  354. }
  355. @Test
  356. public void testBeansPositive() throws Exception {
  357. final WebTarget target = target("getter-on-beans");
  358. final AnotherContactBean contactBean = new AnotherContactBean("jersey@example.com", null, "Jersey JAX-RS", null);
  359. final Response response = target.request().post(Entity.xml(contactBean));
  360. assertThat(response.getStatus(), equalTo(200));
  361. assertThat(response.readEntity(AnotherContactBean.class), equalTo(contactBean));
  362. }
  363. @Test
  364. public void testBeansValidateGetterInvalidEmail() throws Exception {
  365. final WebTarget target = target("getter-on-beans");
  366. final AnotherContactBean contactBean = new AnotherContactBean("jersey", null, "Jersey JAX-RS", null);
  367. final Response response = target.request().post(Entity.xml(contactBean));
  368. assertThat(response.getStatus(), equalTo(400));
  369. }
  370. @Test
  371. public void testBeansValidateGetterInvalidPhone() throws Exception {
  372. final WebTarget target = target("getter-on-beans");
  373. final AnotherContactBean contactBean = new AnotherContactBean("jersey@example.com", "12", "Jersey JAX-RS", null);
  374. final Response response = target.request().post(Entity.xml(contactBean));
  375. assertThat(response.getStatus(), equalTo(200));
  376. assertThat(response.readEntity(AnotherContactBean.class), equalTo(contactBean));
  377. }
  378. @Test
  379. public void testBeansValidateGetterInvalidReturnMail() throws Exception {
  380. final WebTarget target = target("getter-on-beans").path("invalidMail");
  381. final AnotherContactBean contactBean = new AnotherContactBean("jersey@example.com", null, "Jersey JAX-RS", null);
  382. final Response response = target.request().post(Entity.xml(contactBean));
  383. assertThat(response.getStatus(), equalTo(500));
  384. }
  385. @Test
  386. public void testBeansValidateGetterInvalidReturnPhone() throws Exception {
  387. final WebTarget target = target("getter-on-beans").path("invalidPhone");
  388. final AnotherContactBean contactBean = new AnotherContactBean("jersey@example.com", null, "Jersey JAX-RS", null);
  389. final Response response = target.request().post(Entity.xml(contactBean));
  390. contactBean.setPhone("12");
  391. assertThat(response.getStatus(), equalTo(200));
  392. assertThat(response.readEntity(AnotherContactBean.class), equalTo(contactBean));
  393. }
  394. }