PageRenderTime 8755ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/bean-validation-webapp/src/test/java/org/glassfish/jersey/examples/beanvalidation/webapp/ContactCardTest.java

https://gitlab.com/jaragan/jersey
Java | 305 lines | 199 code | 59 blank | 47 comment | 3 complexity | a9f9e339407a79484b73699554f9266e MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright (c) 2012-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.examples.beanvalidation.webapp;
  41. import java.net.URI;
  42. import java.util.HashSet;
  43. import java.util.List;
  44. import java.util.Set;
  45. import javax.ws.rs.client.Entity;
  46. import javax.ws.rs.client.WebTarget;
  47. import javax.ws.rs.core.Application;
  48. import javax.ws.rs.core.GenericType;
  49. import javax.ws.rs.core.MediaType;
  50. import javax.ws.rs.core.Response;
  51. import javax.ws.rs.core.UriBuilder;
  52. import org.glassfish.jersey.client.ClientConfig;
  53. import org.glassfish.jersey.examples.beanvalidation.webapp.domain.ContactCard;
  54. import org.glassfish.jersey.moxy.json.MoxyJsonConfig;
  55. import org.glassfish.jersey.moxy.json.MoxyJsonFeature;
  56. import org.glassfish.jersey.server.ServerProperties;
  57. import org.glassfish.jersey.server.validation.ValidationError;
  58. import org.glassfish.jersey.test.JerseyTest;
  59. import org.glassfish.jersey.test.TestProperties;
  60. import org.glassfish.jersey.test.external.ExternalTestContainerFactory;
  61. import org.eclipse.persistence.jaxb.BeanValidationMode;
  62. import org.eclipse.persistence.jaxb.MarshallerProperties;
  63. import org.junit.Test;
  64. import static org.junit.Assert.assertEquals;
  65. import static org.junit.Assert.assertNotNull;
  66. import static org.junit.Assert.assertTrue;
  67. /**
  68. * @author Michal Gajdos
  69. */
  70. public class ContactCardTest extends JerseyTest {
  71. private static final ContactCard CARD_1;
  72. private static final ContactCard CARD_2;
  73. static {
  74. CARD_1 = new ContactCard();
  75. CARD_1.setFullName("Jersey Foo");
  76. CARD_1.setPhone("1337");
  77. CARD_2 = new ContactCard();
  78. CARD_2.setFullName("Jersey Bar");
  79. CARD_2.setEmail("jersey@bar.com");
  80. }
  81. @Override
  82. protected Application configure() {
  83. enable(TestProperties.LOG_TRAFFIC);
  84. enable(TestProperties.DUMP_ENTITY);
  85. return new MyApplication().property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
  86. }
  87. @Override
  88. protected void configureClient(final ClientConfig config) {
  89. super.configureClient(config);
  90. config.register(MoxyJsonFeature.class);
  91. // Turn off BV otherwise the entities on client would be validated as well.
  92. config.register(new MoxyJsonConfig()
  93. .property(MarshallerProperties.BEAN_VALIDATION_MODE, BeanValidationMode.NONE)
  94. .resolver());
  95. }
  96. @Override
  97. protected URI getBaseUri() {
  98. final UriBuilder baseUriBuilder = UriBuilder.fromUri(super.getBaseUri()).path("bean-validation-webapp");
  99. final boolean externalFactoryInUse = getTestContainerFactory() instanceof ExternalTestContainerFactory;
  100. return externalFactoryInUse ? baseUriBuilder.path("api").build() : baseUriBuilder.build();
  101. }
  102. @Test
  103. public void testAddContact() throws Exception {
  104. final WebTarget target = target()
  105. .path("contact");
  106. final Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
  107. .post(Entity.entity(CARD_1, MediaType.APPLICATION_JSON_TYPE));
  108. final ContactCard contactCard = response.readEntity(ContactCard.class);
  109. assertEquals(200, response.getStatus());
  110. assertNotNull(contactCard.getId());
  111. final Response invalidResponse = target.request(MediaType.APPLICATION_JSON_TYPE)
  112. .post(Entity.entity(CARD_1, MediaType.APPLICATION_JSON_TYPE));
  113. assertEquals(500, invalidResponse.getStatus());
  114. assertTrue(getValidationMessageTemplates(invalidResponse).contains("{contact.already.exist}"));
  115. assertEquals(200, target.path("" + contactCard.getId()).request(MediaType.APPLICATION_JSON_TYPE).delete().getStatus());
  116. }
  117. @Test
  118. public void testContactDoesNotExist() throws Exception {
  119. final WebTarget target = target()
  120. .path("contact");
  121. // GET
  122. Response response = target.path("1").request(MediaType.APPLICATION_JSON_TYPE).get();
  123. assertEquals(500, response.getStatus());
  124. Set<String> violationsMessageTemplates = getValidationMessageTemplates(response);
  125. assertEquals(1, violationsMessageTemplates.size());
  126. assertTrue(violationsMessageTemplates.contains("{contact.does.not.exist}"));
  127. // DELETE
  128. response = target.path("1").request(MediaType.APPLICATION_JSON_TYPE).delete();
  129. assertEquals(500, response.getStatus());
  130. violationsMessageTemplates = getValidationMessageTemplates(response);
  131. assertEquals(1, violationsMessageTemplates.size());
  132. assertTrue(violationsMessageTemplates.contains("{contact.does.not.exist}"));
  133. }
  134. @Test
  135. public void testContactWrongId() throws Exception {
  136. final WebTarget target = target()
  137. .path("contact");
  138. // GET
  139. Response response = target.path("-1").request(MediaType.APPLICATION_JSON_TYPE).get();
  140. assertEquals(400, response.getStatus());
  141. Set<String> violationsMessageTemplates = getValidationMessageTemplates(response);
  142. assertEquals(1, violationsMessageTemplates.size());
  143. assertTrue(violationsMessageTemplates.contains("{contact.wrong.id}"));
  144. // DELETE
  145. response = target.path("-2").request(MediaType.APPLICATION_JSON_TYPE).delete();
  146. assertEquals(400, response.getStatus());
  147. violationsMessageTemplates = getValidationMessageTemplates(response);
  148. assertEquals(1, violationsMessageTemplates.size());
  149. assertTrue(violationsMessageTemplates.contains("{contact.wrong.id}"));
  150. }
  151. private List<ValidationError> getValidationErrorList(final Response response) {
  152. return response.readEntity(new GenericType<List<ValidationError>>() {});
  153. }
  154. private Set<String> getValidationMessageTemplates(final Response response) {
  155. return getValidationMessageTemplates(getValidationErrorList(response));
  156. }
  157. private Set<String> getValidationMessageTemplates(final List<ValidationError> errors) {
  158. final Set<String> templates = new HashSet<>();
  159. for (final ValidationError error : errors) {
  160. templates.add(error.getMessageTemplate());
  161. }
  162. return templates;
  163. }
  164. @Test
  165. public void testAddInvalidContact() throws Exception {
  166. final ContactCard entity = new ContactCard();
  167. entity.setPhone("Crrrn");
  168. final Response response = target()
  169. .path("contact")
  170. .request(MediaType.APPLICATION_JSON_TYPE)
  171. .post(Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE));
  172. assertEquals(400, response.getStatus());
  173. final List<ValidationError> validationErrorList = getValidationErrorList(response);
  174. for (final ValidationError validationError : validationErrorList) {
  175. assertTrue(validationError.getPath().contains("ContactCardResource.addContact.contact."));
  176. }
  177. final Set<String> messageTemplates = getValidationMessageTemplates(validationErrorList);
  178. assertEquals(2, messageTemplates.size());
  179. assertTrue(messageTemplates.contains("{contact.wrong.name}"));
  180. assertTrue(messageTemplates.contains("{contact.wrong.phone}"));
  181. }
  182. @Test
  183. public void testSearchByUnknown() throws Exception {
  184. final Response response = target()
  185. .path("contact")
  186. .path("search/unknown")
  187. .queryParam("q", "er")
  188. .request(MediaType.APPLICATION_JSON_TYPE)
  189. .get();
  190. assertEquals(400, response.getStatus());
  191. final Set<String> messageTemplates = getValidationMessageTemplates(response);
  192. assertEquals(1, messageTemplates.size());
  193. assertTrue(
  194. messageTemplates.contains("{org.glassfish.jersey.examples.beanvalidation.webapp.constraint.SearchType.message}"));
  195. }
  196. @Test
  197. public void testSearchByEmailEmpty() throws Exception {
  198. final Response response = target()
  199. .path("contact")
  200. .path("search/email")
  201. .queryParam("q", "er")
  202. .request(MediaType.APPLICATION_JSON_TYPE)
  203. .get();
  204. assertEquals(200, response.getStatus());
  205. final List<ContactCard> result = response.readEntity(new GenericType<List<ContactCard>>() {});
  206. assertEquals(0, result.size());
  207. }
  208. @Test
  209. public void testSearchByPhoneInvalid() throws Exception {
  210. final Response response = target()
  211. .path("contact")
  212. .path("search/phone")
  213. .queryParam("q", (String) null)
  214. .request(MediaType.APPLICATION_JSON_TYPE)
  215. .get();
  216. assertEquals(400, response.getStatus());
  217. final Set<String> messageTemplates = getValidationMessageTemplates(response);
  218. assertEquals(1, messageTemplates.size());
  219. assertTrue(messageTemplates.contains("{search.string.empty}"));
  220. }
  221. @Test
  222. public void testSearchByName() throws Exception {
  223. final WebTarget target = target().path("contact");
  224. target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(CARD_1, MediaType.APPLICATION_JSON_TYPE));
  225. target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(CARD_2, MediaType.APPLICATION_JSON_TYPE));
  226. Response response = target.path("search/name")
  227. .queryParam("q", "er")
  228. .request(MediaType.APPLICATION_JSON_TYPE)
  229. .get();
  230. List<ContactCard> contactCards = response.readEntity(new GenericType<List<ContactCard>>() {});
  231. assertEquals(200, response.getStatus());
  232. assertEquals(2, contactCards.size());
  233. for (final ContactCard contactCard : contactCards) {
  234. assertTrue(contactCard.getFullName().contains("er"));
  235. }
  236. response = target.path("search/name")
  237. .queryParam("q", "Foo")
  238. .request(MediaType.APPLICATION_JSON_TYPE)
  239. .get();
  240. contactCards = response.readEntity(new GenericType<List<ContactCard>>() {});
  241. assertEquals(200, response.getStatus());
  242. assertEquals(1, contactCards.size());
  243. assertTrue(contactCards.get(0).getFullName().contains("Foo"));
  244. assertEquals(200, target.request(MediaType.APPLICATION_JSON_TYPE).delete().getStatus());
  245. }
  246. }