View Javadoc

1   package com.atlassian.plugins.rest.common.filter;
2   
3   import com.sun.jersey.core.util.MultivaluedMapImpl;
4   import com.sun.jersey.spi.container.ContainerRequest;
5   import static org.junit.Assert.*;
6   import org.junit.Before;
7   import org.junit.Test;
8   import org.mockito.ArgumentMatcher;
9   import org.mockito.Matchers;
10  import org.mockito.Mock;
11  import static org.mockito.Mockito.*;
12  import org.mockito.MockitoAnnotations;
13  
14  import javax.ws.rs.core.MediaType;
15  import javax.ws.rs.core.MultivaluedMap;
16  import java.net.URI;
17  import java.util.Collections;
18  import java.util.List;
19  import java.util.Map;
20  
21  
22  public class ExtensionJerseyFilterTest
23  {
24      private static final String ACCEPT_HEADER = "Accept";
25  
26      @Mock
27      private ContainerRequest containerRequest;
28  
29      private ExtensionJerseyFilter jerseyFilter;
30  
31      @Before
32      public void setUp()
33      {
34          MockitoAnnotations.initMocks(this);
35          jerseyFilter = new ExtensionJerseyFilter(Collections.singletonList("/excluded.*"));
36      }
37  
38      @Test
39      public void testDefaultConstructor() throws Exception
40      {
41          jerseyFilter = new ExtensionJerseyFilter();
42          final MultivaluedMap<String, String> parameters = new MultivaluedMapImpl();
43          parameters.add("param1", "value1");
44          parameters.add("param2", "value2.1");
45          parameters.add("param2", "value2.2");
46          testFilter(parameters);
47      }
48  
49      @Test
50      public void testFilterWithoutQueryParameters() throws Exception
51      {
52          testFilter(new MultivaluedMapImpl());
53      }
54  
55      @Test
56      public void testFilterWithQueryParameters() throws Exception
57      {
58          final MultivaluedMap<String, String> parameters = new MultivaluedMapImpl();
59          parameters.add("param1", "value1");
60          parameters.add("param2", "value2.1");
61          parameters.add("param2", "value2.2");
62          testFilter(parameters);
63      }
64  
65      @Test
66      public void testDoNotFilterWhenExtensionNotMatched() throws Exception
67      {
68          testDoNotFilter("resource.myextension");
69      }
70  
71      @Test
72      public void testDoNotFilterWhenNoExtension() throws Exception
73      {
74          testDoNotFilter("resource");
75      }
76  
77      @Test
78      public void testDoNotFilterWhenExcluded() throws Exception
79      {
80          testDoNotFilter("excluded/resource.json");
81      }
82  
83      private void testFilter(MultivaluedMap<String, String> queryParameters) throws Exception
84      {
85          final MultivaluedMapImpl headers = new MultivaluedMapImpl();
86          final URI baseUri = new URI("http://localhost:8080/rest");
87  
88          when(containerRequest.getAbsolutePath()).thenReturn(new URI("http://localhost:8080/rest/application.json"));
89          when(containerRequest.getBaseUri()).thenReturn(baseUri);
90          when(containerRequest.getRequestHeaders()).thenReturn(headers);
91          when(containerRequest.getQueryParameters()).thenReturn(queryParameters);
92  
93          jerseyFilter.filter(containerRequest);
94  
95          final List<String> acceptHeader = headers.get(ACCEPT_HEADER);
96          assertEquals(1, acceptHeader.size());
97          assertEquals(MediaType.APPLICATION_JSON, acceptHeader.get(0));
98  
99          verify(containerRequest).setUris(eq(baseUri), argThat(new UriMatcher(new URI("http://localhost:8080/rest/application"), queryParameters)));
100     }
101 
102     private void testDoNotFilter(String resourceName) throws Exception
103     {
104         final String baseUri = "http://localhost:8080/rest/";
105 
106         when(containerRequest.getBaseUri()).thenReturn(new URI(baseUri));
107         when(containerRequest.getAbsolutePath()).thenReturn(new URI(baseUri + resourceName));
108 
109         jerseyFilter.filter(containerRequest);
110 
111         verify(containerRequest, never()).getRequestHeaders();
112         verify(containerRequest, never()).setUris(Matchers.<URI>anyObject(), Matchers.<URI>anyObject());
113     }
114 
115     private static class UriMatcher extends ArgumentMatcher<URI>
116     {
117         private final URI requestUri;
118         private final MultivaluedMap<String, String> parameters;
119 
120         UriMatcher(URI requestUri, MultivaluedMap<String, String> parameters)
121         {
122             this.requestUri = requestUri;
123             this.parameters = parameters;
124         }
125 
126         public boolean matches(Object actual)
127         {
128             final URI uri = (URI) actual;
129 
130             if (!actual.toString().startsWith(requestUri.toString()))
131             {
132                 return false;
133             }
134 
135             // check query parameters
136             final String queryParametersString = '&' + uri.getQuery() + '&';
137             for (Map.Entry<String, List<String>> queryParameter : parameters.entrySet())
138             {
139                 for (String value : queryParameter.getValue())
140                 {
141                     if (!queryParametersString.contains('&' + queryParameter.getKey() + '=' + value + '&'))
142                     {
143                         return false;
144                     }
145                 }
146             }
147             return true;
148         }
149     }
150 }