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
30
31
32 public class Jersey291ShimTest {
33 private static final String SYSTEM_PROPERTY = "com.atlassian.plugins.rest.shim.JERSEY-291";
34
35 private static String originalSystemProperty;
36 private static final Object[] ARRAY_WITH_NULL = {null};
37
38 @BeforeClass
39 public static void saveOriginalSystemProperty() {
40 originalSystemProperty = System.getProperty(SYSTEM_PROPERTY);
41 }
42
43 @AfterClass
44 public static void restoreOriginalSystemProperty() {
45 initSystemProperty(originalSystemProperty);
46 }
47
48 private static void initSystemProperty(final String value) {
49 if (value != null) {
50 System.setProperty(SYSTEM_PROPERTY, value);
51 } else {
52 System.getProperties().remove(SYSTEM_PROPERTY);
53 }
54 DispatchProviderHelper.JERSEY_291_SHIM.set(null);
55 }
56
57 private AbstractResourceMethod abstractResourceMethod = mock(AbstractResourceMethod.class);
58
59
60
61
62 @Test
63 public void testShimOffByDefault1() throws Exception {
64 initSystemProperty(null);
65 invokeAndCheckParameters("queryParam", emptyList());
66 }
67
68 @Test
69 public void testShimOffByDefault2() throws Exception {
70 initSystemProperty("hello");
71 invokeAndCheckParameters("queryParam", emptyList());
72 }
73
74 @Test
75 public void testEmptyListMapsToNull() throws Exception {
76 initSystemProperty("true");
77 invokeAndCheckParameters(new Validator(ARRAY_WITH_NULL), "queryParam", emptyList());
78 }
79
80 @Test
81 public void testEmptySetMapsToNull() throws Exception {
82 initSystemProperty("true");
83 invokeAndCheckParameters(new Validator(ARRAY_WITH_NULL), "queryParam", emptySet());
84 }
85
86 @Test
87 public void testEmptyTreeSetMapsToNull() throws Exception {
88 initSystemProperty("true");
89 invokeAndCheckParameters(new Validator(ARRAY_WITH_NULL), "queryParam", new TreeSet<String>());
90 }
91
92 @Test
93 public void testFormParamUnchanged() throws Exception {
94 initSystemProperty("true");
95 invokeAndCheckParameters("formParam", emptyList());
96 }
97
98 @Test
99 public void testUnannotatedUnchanged() throws Exception {
100 initSystemProperty("true");
101 invokeAndCheckParameters("unannotated", emptyList());
102 }
103
104 @Test
105 public void testNonEmptyCollectionUnchanged() throws Exception {
106 initSystemProperty("true");
107 invokeAndCheckParameters("queryParam", singletonList("hello"));
108 }
109
110 @Test
111 public void testNonCollectionUnchanged() throws Exception {
112 initSystemProperty("true");
113 invokeAndCheckParameters("queryParam", "");
114 }
115
116
117
118
119 private void invokeAndCheckParameters(String annotationDonatingMethod, Object... inputParameters) throws Exception {
120 invokeAndCheckParameters(new Validator(inputParameters), annotationDonatingMethod, inputParameters);
121
122 }
123
124 private void invokeAndCheckParameters(ResourceInterceptor validator, String annotationDonatingMethod, Object... inputParameters) throws Exception {
125 when(abstractResourceMethod.getParameters()).thenReturn(parameters(annotationDonatingMethod));
126 invokeMethodWithInterceptors(Arrays.asList(validator), abstractResourceMethod, null, null, inputParameters, null);
127 }
128
129 private List<Parameter> parameters(String exampleMethod) throws Exception {
130 final Method m = ContainerClass.class.getMethod(exampleMethod, String.class);
131 final Annotation[][] allParameterAnnotations = m.getParameterAnnotations();
132 final List<Parameter> parameters = new ArrayList<Parameter>(allParameterAnnotations.length);
133 for (Annotation[] thisParameterAnnotations : allParameterAnnotations) {
134 parameters.add(new Parameter(thisParameterAnnotations, null, null, null, null, null));
135 }
136 return parameters;
137 }
138
139
140 @SuppressWarnings("unused")
141 public static class ContainerClass {
142 public void unannotated(String x) {
143 }
144
145 public void queryParam(@QueryParam("foo") String x) {
146 }
147
148 public void formParam(@FormParam("baz") String x) {
149 }
150 }
151
152 static class Validator implements ResourceInterceptor {
153 private final Object[] expected;
154
155 private Validator(final Object... expected) {
156 this.expected = expected.clone();
157 }
158
159 public void intercept(final MethodInvocation invocation) {
160 final Object[] got = invocation.getParameters();
161 assertArrayEquals(expected, got);
162 }
163 }
164 }
165