View Javadoc

1   package com.atlassian.plugins.rest.common.interceptor.impl;
2   
3   import com.atlassian.plugins.rest.common.interceptor.MethodInvocation;
4   import com.atlassian.plugins.rest.common.interceptor.ResourceInterceptor;
5   import com.sun.jersey.api.model.AbstractResourceMethod;
6   import com.sun.jersey.api.model.Parameter;
7   import org.junit.AfterClass;
8   import org.junit.BeforeClass;
9   import org.junit.Test;
10  
11  import javax.ws.rs.FormParam;
12  import javax.ws.rs.QueryParam;
13  import java.lang.annotation.Annotation;
14  import java.lang.reflect.Method;
15  import java.util.ArrayList;
16  import java.util.Arrays;
17  import java.util.List;
18  import java.util.TreeSet;
19  
20  import static com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper.invokeMethodWithInterceptors;
21  import static java.util.Collections.emptyList;
22  import static java.util.Collections.emptySet;
23  import static java.util.Collections.singletonList;
24  import static org.junit.Assert.assertArrayEquals;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  /**
29   * Tests the compatibility shim for JRADEV-11989 / REST-206 / JERSEY-291.
30   * To be removed after JIRA 6.0 ships.
31   */
32  public class Jersey291ShimTest
33  {
34      private static final String SYSTEM_PROPERTY = "com.atlassian.plugins.rest.shim.JERSEY-291";
35  
36      private static String originalSystemProperty;
37      private static final Object[] ARRAY_WITH_NULL = { null };
38  
39      @BeforeClass
40      public static void saveOriginalSystemProperty()
41      {
42          originalSystemProperty = System.getProperty(SYSTEM_PROPERTY);
43      }
44  
45      @AfterClass
46      public static void restoreOriginalSystemProperty()
47      {
48          initSystemProperty(originalSystemProperty);
49      }
50  
51      private static void initSystemProperty(final String value)
52      {
53          if (value != null)
54          {
55              System.setProperty(SYSTEM_PROPERTY, value);
56          }
57          else
58          {
59              System.getProperties().remove(SYSTEM_PROPERTY);
60          }
61          DispatchProviderHelper.JERSEY_291_SHIM.set(null);
62      }
63  
64      private AbstractResourceMethod abstractResourceMethod = mock(AbstractResourceMethod.class);
65  
66      //================================================================
67  
68  
69      @Test
70      public void testShimOffByDefault1() throws Exception
71      {
72          initSystemProperty(null);
73          invokeAndCheckParameters("queryParam", emptyList());
74      }
75  
76      @Test
77      public void testShimOffByDefault2() throws Exception
78      {
79          initSystemProperty("hello");
80          invokeAndCheckParameters("queryParam", emptyList());
81      }
82  
83      @Test
84      public void testEmptyListMapsToNull() throws Exception
85      {
86          initSystemProperty("true");
87          invokeAndCheckParameters(new Validator(ARRAY_WITH_NULL), "queryParam", emptyList());
88      }
89  
90      @Test
91      public void testEmptySetMapsToNull() throws Exception
92      {
93          initSystemProperty("true");
94          invokeAndCheckParameters(new Validator(ARRAY_WITH_NULL), "queryParam", emptySet());
95      }
96  
97      @Test
98      public void testEmptyTreeSetMapsToNull() throws Exception
99      {
100         initSystemProperty("true");
101         invokeAndCheckParameters(new Validator(ARRAY_WITH_NULL), "queryParam", new TreeSet<String>());
102     }
103 
104     @Test
105     public void testFormParamUnchanged() throws Exception
106     {
107         initSystemProperty("true");
108         invokeAndCheckParameters("formParam", emptyList());
109     }
110 
111     @Test
112     public void testUnannotatedUnchanged() throws Exception
113     {
114         initSystemProperty("true");
115         invokeAndCheckParameters("unannotated", emptyList());
116     }
117 
118     @Test
119     public void testNonEmptyCollectionUnchanged() throws Exception
120     {
121         initSystemProperty("true");
122         invokeAndCheckParameters("queryParam", singletonList("hello"));
123     }
124 
125     @Test
126     public void testNonCollectionUnchanged() throws Exception
127     {
128         initSystemProperty("true");
129         invokeAndCheckParameters("queryParam", "");
130     }
131 
132 
133     //================================================================
134 
135     private void invokeAndCheckParameters(String annotationDonatingMethod, Object... inputParameters) throws Exception
136     {
137         invokeAndCheckParameters(new Validator(inputParameters), annotationDonatingMethod, inputParameters);
138 
139     }
140     private void invokeAndCheckParameters(ResourceInterceptor validator, String annotationDonatingMethod, Object... inputParameters) throws Exception
141     {
142         when(abstractResourceMethod.getParameters()).thenReturn(parameters(annotationDonatingMethod));
143         invokeMethodWithInterceptors(Arrays.asList(validator), abstractResourceMethod, null, null, inputParameters, null);
144     }
145 
146     private List<Parameter> parameters(String exampleMethod) throws Exception
147     {
148         final Method m = ContainerClass.class.getMethod(exampleMethod, String.class);
149         final Annotation[][] allParameterAnnotations = m.getParameterAnnotations();
150         final List<Parameter> parameters = new ArrayList<Parameter>(allParameterAnnotations.length);
151         for (Annotation[] thisParameterAnnotations : allParameterAnnotations)
152         {
153             parameters.add(new Parameter(thisParameterAnnotations, null, null, null, null, null));
154         }
155         return parameters;
156     }
157 
158     // Annotation supply
159     @SuppressWarnings("unused")
160     public static class ContainerClass
161     {
162         public void unannotated(String x) {}
163         public void queryParam(@QueryParam("foo") String x) {}
164         public void formParam(@FormParam("baz") String x) {}
165     }
166 
167     static class Validator implements ResourceInterceptor
168     {
169         private final Object[] expected;
170 
171         private Validator(final Object... expected)
172         {
173             this.expected = expected.clone();
174         }
175 
176         public void intercept(final MethodInvocation invocation)
177         {
178             final Object[] got = invocation.getParameters();
179             assertArrayEquals(expected, got);
180         }
181     }
182 }
183