View Javadoc

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