View Javadoc

1   package it.com.atlassian.plugins.rest.sample.entities;
2   
3   import com.atlassian.plugins.rest.sample.entities.UriBuilder;
4   import com.sun.jersey.api.client.Client;
5   import com.sun.jersey.api.client.ClientResponse;
6   import org.hamcrest.Matchers;
7   import org.junit.Assert;
8   import org.junit.Test;
9   
10  import javax.ws.rs.core.MediaType;
11  import java.net.URI;
12  
13  import static org.hamcrest.MatcherAssert.assertThat;
14  import static org.hamcrest.Matchers.is;
15  
16  /**
17   * Testing a {@link com.atlassian.plugins.rest.sample.entities.EntityResource} that has no versions.
18   */
19  public class NoVersionTest {
20      @Test
21      public void testResourceWithNoVersion() {
22          // URI does not contain a version number:
23          // the "/entity-versionless" path in atlassian-plugin.xml specifies version="none",
24          // which causes the RestModuleDescriptor to generate a url-pattern using a path generated by
25          // a RestApiContext created with a parameter ApiVersion("none"), which does not put a version in the path
26          final URI baseUri = UriBuilder.create().path("rest/entity-versionless/fruit/jackfruit").build();
27          final String returned = Client.create().resource(baseUri)
28                  .accept(MediaType.APPLICATION_XML)
29                  .get(String.class);
30  
31          Assert.assertThat(returned, Matchers.containsString("<jackFruit jaxb-description=\"fresh at"));
32      }
33  
34      @Test
35      public void testLatestVersionOfResourceWithNoVersion() {
36          // *everything* is the latest version, so this still works
37          final URI baseUri = UriBuilder.create().path("rest/entity-versionless/latest/fruit/jackfruit").build();
38          final String returned = Client.create().resource(baseUri)
39                  .accept(MediaType.APPLICATION_XML)
40                  .get(String.class);
41  
42          Assert.assertThat(returned, Matchers.containsString("<jackFruit jaxb-description=\"fresh at"));
43      }
44  
45      @Test
46      public void testSpecificVersionOfResourceWithNoVersion() {
47          // there are no named versions so this cannot possibly work
48          final URI baseUri = UriBuilder.create().path("rest/entity-versionless/1/fruit/jackfruit").build();
49          ClientResponse response = Client.create().resource(baseUri).get(ClientResponse.class);
50          assertThat(response.getClientResponseStatus(), is(ClientResponse.Status.NOT_FOUND));
51      }
52  }