View Javadoc

1   package com.atlassian.plugins.rest.module.servlet;
2   
3   import com.atlassian.plugin.servlet.ServletModuleManager;
4   import com.atlassian.plugin.servlet.descriptors.ServletFilterModuleDescriptor;
5   import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
6   import com.atlassian.plugin.servlet.filter.FilterLocation;
7   import com.atlassian.plugin.servlet.util.PathMapper;
8   import com.atlassian.plugins.rest.module.ApiVersion;
9   import com.atlassian.plugins.rest.module.RestServletFilterModuleDescriptor;
10  import org.junit.Before;
11  import org.junit.Test;
12  import static org.mockito.Mockito.*;
13  
14  import javax.servlet.FilterConfig;
15  import javax.servlet.ServletConfig;
16  import java.util.Collections;
17  
18  public class DefaultRestServletModuleManagerTest
19  {
20      private ServletModuleManager servletModuleManagerDelegate;
21      private PathMapper pathMapper;
22  
23      private DefaultRestServletModuleManager servletModuleManager;
24  
25      @Before
26      public void setUp()
27      {
28          servletModuleManagerDelegate = mock(ServletModuleManager.class);
29          pathMapper = mock(PathMapper.class);
30  
31          servletModuleManager = new DefaultRestServletModuleManager(servletModuleManagerDelegate, pathMapper, "/rest");
32      }
33  
34      @Test
35      public void testAddServletModule()
36      {
37          final ServletModuleDescriptor moduleDescriptor = mock(ServletModuleDescriptor.class);
38          servletModuleManager.addServletModule(moduleDescriptor);
39  
40          verify(servletModuleManagerDelegate).addServletModule(moduleDescriptor);
41          verifyNoMoreInteractions(servletModuleManagerDelegate);
42          verifyZeroInteractions(pathMapper);
43      }
44  
45      @Test
46      public void testGetServlet() throws Exception
47      {
48          final String path = "path";
49          final ServletConfig servletConfig = mock(ServletConfig.class);
50          servletModuleManager.getServlet(path, servletConfig);
51  
52          verify(servletModuleManagerDelegate).getServlet(path, servletConfig);
53          verifyNoMoreInteractions(servletModuleManagerDelegate);
54          verifyZeroInteractions(pathMapper);
55      }
56  
57      @Test
58      public void testRemoveServletModule()
59      {
60          final ServletModuleDescriptor moduleDescriptor = mock(ServletModuleDescriptor.class);
61          servletModuleManager.removeServletModule(moduleDescriptor);
62  
63          verify(servletModuleManagerDelegate).removeServletModule(moduleDescriptor);
64          verifyNoMoreInteractions(servletModuleManagerDelegate);
65          verifyZeroInteractions(pathMapper);
66      }
67  
68      @Test
69      public void testGetFilters() throws Exception
70      {
71          final FilterLocation filterLocation = FilterLocation.AFTER_ENCODING;
72          final String pathInfo = "pathInfo";
73          final FilterConfig filterConfig = mock(FilterConfig.class);
74          servletModuleManager.getFilters(filterLocation, pathInfo, filterConfig);
75  
76          verify(servletModuleManagerDelegate).getFilters(filterLocation, pathInfo, filterConfig);
77          verifyNoMoreInteractions(servletModuleManagerDelegate);
78          verifyZeroInteractions(pathMapper);
79      }
80  
81      @Test
82      public void testAddFilterModuleWithNonRestServletFilterModuleDescriptor()
83      {
84          final ServletFilterModuleDescriptor servletFilterModuleDescriptor = mock(ServletFilterModuleDescriptor.class);
85          servletModuleManager.addFilterModule(servletFilterModuleDescriptor);
86  
87          verify(servletModuleManagerDelegate).addFilterModule(servletFilterModuleDescriptor);
88          verifyNoMoreInteractions(servletModuleManagerDelegate);
89          verifyZeroInteractions(pathMapper);
90      }
91  
92      @Test
93      public void testAddFilterModuleWithRestServletFilterModuleDescriptor()
94      {
95          final String key1 = "completekey1";
96          final String basePath = "/basepath";
97          final String path1 = "/basepath/1";
98          final RestServletFilterModuleDescriptor servletFilterModuleDescriptor1 = getMockRestServletModuleDescriptor(key1, basePath, "1");
99          when(servletFilterModuleDescriptor1.getPaths()).thenReturn(Collections.singletonList(path1));
100 
101         // There is no latest yet, so this one should be added
102         servletModuleManager.addFilterModule(servletFilterModuleDescriptor1);
103 
104         verify(pathMapper).put(key1, servletModuleManager.getPathPattern(basePath));
105         verify(servletModuleManagerDelegate).addFilterModule(servletFilterModuleDescriptor1);
106 
107         final String key2 = "completekey2";
108         final RestServletFilterModuleDescriptor servletFilterModuleDescriptor2 = getMockRestServletModuleDescriptor(key2, basePath, "2");
109 
110         // this version is greater than the current latest "completekey1", so should be set as latest
111         servletModuleManager.addFilterModule(servletFilterModuleDescriptor2);
112 
113         // reset the current latest
114         verify(pathMapper).put(key1, null);
115         verify(pathMapper).put(key1, path1);
116         // add the new latest
117         verify(pathMapper).put(key2, servletModuleManager.getPathPattern(basePath));
118         verify(servletModuleManagerDelegate).addFilterModule(servletFilterModuleDescriptor2);
119 
120         final RestServletFilterModuleDescriptor servletFilterModuleDescriptor3 = getMockRestServletModuleDescriptor("completekey3", basePath, "1.2");
121 
122         // this version is lower than latest so should not be set as latest
123         servletModuleManager.addFilterModule(servletFilterModuleDescriptor3);
124 
125         verifyZeroInteractions(pathMapper);
126         verify(servletModuleManagerDelegate).addFilterModule(servletFilterModuleDescriptor3);
127     }
128 
129     @Test
130     public void testRemoveFilterModuleWithRestServletFilterModuleDescriptorWithNoExistingLatest()
131     {
132         final RestServletFilterModuleDescriptor servletFilterModuleDescriptor1 = getMockRestServletModuleDescriptor("completekey1", "/basepath", "1");
133 
134         // this is not the latest so far, as no descriptor has been set as the latest, so nothing special happening
135         servletModuleManager.removeFilterModule(servletFilterModuleDescriptor1);
136 
137         verify(servletModuleManagerDelegate).removeFilterModule(servletFilterModuleDescriptor1);
138         verifyZeroInteractions(pathMapper);
139     }
140 
141     @Test
142     public void testRemoveFilterModuleWithRestServletFilterModuleDescriptorWhenRemovingNonLatest()
143     {
144         final String key1 = "completekey1";
145         final String key2 = "completekey2";
146         final String basePath = "/basepath";
147         final RestServletFilterModuleDescriptor servletFilterModuleDescriptor1 = getMockRestServletModuleDescriptor(key1, basePath, "1");
148         final RestServletFilterModuleDescriptor servletFilterModuleDescriptor2 = getMockRestServletModuleDescriptor(key2, basePath, "2");
149 
150         // adding the descriptor so that it becomes the latest
151         servletModuleManager.addFilterModule(servletFilterModuleDescriptor1);
152         servletModuleManager.addFilterModule(servletFilterModuleDescriptor2);
153 
154         // removing ths one shouldn't do much, this is not the "latest"
155         servletModuleManager.removeFilterModule(servletFilterModuleDescriptor1);
156 
157         verify(servletModuleManagerDelegate).removeFilterModule(servletFilterModuleDescriptor1);
158         verify(pathMapper).put(key1, servletModuleManager.getPathPattern(basePath));
159         verify(pathMapper).put(key1, null);
160         verify(pathMapper).put(key2, servletModuleManager.getPathPattern(basePath));
161         verifyNoMoreInteractions(pathMapper);
162     }
163 
164     @Test
165     public void testRemoveFilterModuleWithRestServletFilterModuleDescriptorWhenRemovingLatest()
166     {
167         final String key1 = "completekey1";
168         final String key2 = "completekey2";
169         final String basePath = "/basepath";
170         final RestServletFilterModuleDescriptor servletFilterModuleDescriptor1 = getMockRestServletModuleDescriptor(key1, basePath, "1");
171         final RestServletFilterModuleDescriptor servletFilterModuleDescriptor2 = getMockRestServletModuleDescriptor(key2, basePath, "2");
172 
173         servletModuleManager.addFilterModule(servletFilterModuleDescriptor2);
174         servletModuleManager.addFilterModule(servletFilterModuleDescriptor1);
175 
176         verify(pathMapper).put(key2, servletModuleManager.getPathPattern(basePath));
177         verifyNoMoreInteractions(pathMapper);
178 
179         servletModuleManager.removeFilterModule(servletFilterModuleDescriptor2);
180 
181         verify(servletModuleManagerDelegate).removeFilterModule(servletFilterModuleDescriptor2);
182         verify(pathMapper).put(key1, servletModuleManager.getPathPattern(basePath));
183         verifyNoMoreInteractions(pathMapper);
184     }
185 
186     @Test
187     public void testRemoveFilterModuleWithNonRestServletFilterModuleDescriptor()
188     {
189         final ServletFilterModuleDescriptor servletFilterModuleDescriptor = mock(ServletFilterModuleDescriptor.class);
190         servletModuleManager.removeFilterModule(servletFilterModuleDescriptor);
191 
192         verify(servletModuleManagerDelegate).removeFilterModule(servletFilterModuleDescriptor);
193         verifyNoMoreInteractions(servletModuleManagerDelegate);
194         verifyZeroInteractions(pathMapper);
195     }
196 
197     private RestServletFilterModuleDescriptor getMockRestServletModuleDescriptor(String key, String path, String version)
198     {
199         final RestServletFilterModuleDescriptor servletFilterModuleDescriptor1 = mock(RestServletFilterModuleDescriptor.class);
200         when(servletFilterModuleDescriptor1.getCompleteKey()).thenReturn(key);
201         when(servletFilterModuleDescriptor1.getBasePath()).thenReturn(path);
202         when(servletFilterModuleDescriptor1.getVersion()).thenReturn(new ApiVersion(version));
203         return servletFilterModuleDescriptor1;
204     }
205 }