View Javadoc

1   package it.com.atlassian.rest.validation;
2   
3   import javax.ws.rs.core.MediaType;
4   
5   import com.atlassian.plugins.rest.common.validation.ValidationErrors;
6   import com.atlassian.plugins.rest.validation.Person;
7   import com.atlassian.plugins.rest.validation.PersonResponse;
8   import com.atlassian.rest.jersey.client.WebResourceFactory;
9   
10  import com.sun.jersey.api.client.UniformInterfaceException;
11  import com.sun.jersey.api.client.WebResource;
12  
13  import org.junit.Before;
14  import org.junit.Test;
15  
16  import static junit.framework.Assert.assertTrue;
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.fail;
20  
21  public class ValidatedResourceTest {
22      private WebResource webResource;
23  
24      @Before
25      public void setUp() {
26          webResource = WebResourceFactory.authenticated();
27      }
28  
29      @Test
30      public void testValidationPassed() {
31          Person goodPerson = new Person();
32          goodPerson.setName("Jim");
33  
34          PersonResponse response = webResource.path("validatedResource.xml").entity(goodPerson, MediaType.APPLICATION_XML).post(PersonResponse.class);
35          assertTrue(response.getName().equals("Jim"));
36      }
37  
38      @Test
39      public void testValidationPassedJson() {
40          Person goodPerson = new Person();
41          goodPerson.setName("Jim");
42  
43          PersonResponse response = webResource.path("validatedResource.json").entity(goodPerson, MediaType.APPLICATION_JSON).post(PersonResponse.class);
44          assertTrue(response.getName().equals("Jim"));
45      }
46  
47      @Test
48      public void testValidationFailed() {
49          Person badPerson = new Person();
50          badPerson.setName("a");
51  
52          try {
53              webResource.path("validatedResource.xml").entity(badPerson, MediaType.APPLICATION_XML).post(ValidationErrors.class);
54              fail();
55          } catch (UniformInterfaceException ex) {
56              ValidationErrors errors = ex.getResponse().getEntity(ValidationErrors.class);
57              assertNotNull(errors);
58              assertEquals(1, errors.getErrors().size());
59              assertEquals("The name must be between 2 and 10 characters", errors.getErrors().get(0).getMessage());
60          }
61      }
62  
63      @Test
64      public void testValidationFailedJson() {
65          Person badPerson = new Person();
66          badPerson.setName("a");
67  
68          try {
69              webResource.path("validatedResource.json").entity(badPerson, MediaType.APPLICATION_JSON).post(ValidationErrors.class);
70              fail();
71          } catch (UniformInterfaceException ex) {
72              ValidationErrors errors = ex.getResponse().getEntity(ValidationErrors.class);
73              assertNotNull(errors);
74              assertEquals(1, errors.getErrors().size());
75              assertEquals("The name must be between 2 and 10 characters", errors.getErrors().get(0).getMessage());
76          }
77      }
78  }