Skip to content

Commit 714e8d1

Browse files
committed
WW-5352 Add full unit test coverage
1 parent bf3f407 commit 714e8d1

File tree

1 file changed

+218
-0
lines changed

1 file changed

+218
-0
lines changed
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package org.apache.struts2.interceptor.parameter;
2+
3+
import com.opensymphony.xwork2.security.AcceptedPatternsChecker;
4+
import com.opensymphony.xwork2.security.NotExcludedAcceptedPatternsChecker;
5+
import org.apache.struts2.dispatcher.HttpParameters;
6+
import org.apache.struts2.dispatcher.Parameter;
7+
import org.apache.struts2.ognl.ThreadAllowlist;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import java.util.HashMap;
13+
import java.util.HashSet;
14+
import java.util.Map;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.mockito.ArgumentMatchers.anyString;
18+
import static org.mockito.Mockito.mock;
19+
import static org.mockito.Mockito.when;
20+
21+
public class StrutsParameterAnnotationTest {
22+
23+
private ParametersInterceptor parametersInterceptor;
24+
25+
private ThreadAllowlist threadAllowlist;
26+
27+
@Before
28+
public void setUp() throws Exception {
29+
parametersInterceptor = new ParametersInterceptor();
30+
parametersInterceptor.setRequireAnnotations(Boolean.TRUE.toString());
31+
32+
threadAllowlist = new ThreadAllowlist();
33+
parametersInterceptor.setThreadAllowlist(threadAllowlist);
34+
35+
NotExcludedAcceptedPatternsChecker checker = mock(NotExcludedAcceptedPatternsChecker.class);
36+
when(checker.isAccepted(anyString())).thenReturn(AcceptedPatternsChecker.IsAccepted.yes(""));
37+
when(checker.isExcluded(anyString())).thenReturn(NotExcludedAcceptedPatternsChecker.IsExcluded.no(new HashSet<>()));
38+
parametersInterceptor.setAcceptedPatterns(checker);
39+
parametersInterceptor.setExcludedPatterns(checker);
40+
}
41+
42+
@After
43+
public void tearDown() throws Exception {
44+
threadAllowlist.clearAllowlist();
45+
}
46+
47+
private void testParameter(Object action, String paramName, boolean shouldContain) {
48+
Map<String, String[]> requestParamMap = new HashMap<>();
49+
requestParamMap.put(paramName, new String[]{"value"});
50+
HttpParameters httpParameters = HttpParameters.create(requestParamMap).build();
51+
52+
Map<String, Parameter> acceptedParameters = parametersInterceptor.toAcceptableParameters(httpParameters, action);
53+
54+
if (shouldContain) {
55+
assertThat(acceptedParameters).containsOnlyKeys(paramName);
56+
} else {
57+
assertThat(acceptedParameters).isEmpty();
58+
assertThat(threadAllowlist.getAllowlist()).isEmpty();
59+
}
60+
}
61+
62+
@Test
63+
public void privateStrAnnotated() {
64+
testParameter(new FieldAction(), "privateStr", false);
65+
}
66+
67+
@Test
68+
public void publicStrAnnotated() {
69+
testParameter(new FieldAction(), "publicStr", true);
70+
assertThat(threadAllowlist.getAllowlist()).isEmpty();
71+
}
72+
73+
@Test
74+
public void publicStrNotAnnotated() {
75+
testParameter(new FieldAction(), "publicStrNotAnnotated", false);
76+
}
77+
78+
@Test
79+
public void privatePojoAnnotated() {
80+
testParameter(new FieldAction(), "privatePojo.key", false);
81+
}
82+
83+
@Test
84+
public void publicPojoDepthZero() {
85+
testParameter(new FieldAction(), "publicPojoDepthZero.key", false);
86+
}
87+
88+
@Test
89+
public void publicPojoDepthOne() {
90+
testParameter(new FieldAction(), "publicPojoDepthOne.key", true);
91+
assertThat(threadAllowlist.getAllowlist()).containsExactly(Pojo.class);
92+
}
93+
94+
@Test
95+
public void publicNestedPojoDepthOne() {
96+
testParameter(new FieldAction(), "publicPojoDepthOne.key.key", false);
97+
}
98+
99+
@Test
100+
public void publicPojoDepthTwo() {
101+
testParameter(new FieldAction(), "publicPojoDepthTwo.key", true);
102+
assertThat(threadAllowlist.getAllowlist()).containsExactly(Pojo.class);
103+
}
104+
105+
@Test
106+
public void publicNestedPojoDepthTwo() {
107+
testParameter(new FieldAction(), "publicPojoDepthTwo.key.key", true);
108+
assertThat(threadAllowlist.getAllowlist()).containsExactly(Pojo.class);
109+
}
110+
111+
@Test
112+
public void privateStrAnnotatedMethod() {
113+
testParameter(new MethodAction(), "privateStr", false);
114+
}
115+
116+
@Test
117+
public void publicStrAnnotatedMethod() {
118+
testParameter(new MethodAction(), "publicStr", true);
119+
assertThat(threadAllowlist.getAllowlist()).isEmpty();
120+
}
121+
122+
@Test
123+
public void publicStrNotAnnotatedMethod() {
124+
testParameter(new MethodAction(), "publicStrNotAnnotated", false);
125+
}
126+
127+
@Test
128+
public void privatePojoAnnotatedMethod() {
129+
testParameter(new MethodAction(), "privatePojo.key", false);
130+
}
131+
132+
@Test
133+
public void publicPojoDepthZeroMethod() {
134+
testParameter(new MethodAction(), "publicPojoDepthZero.key", false);
135+
}
136+
137+
@Test
138+
public void publicPojoDepthOneMethod() {
139+
testParameter(new MethodAction(), "publicPojoDepthOne.key", true);
140+
assertThat(threadAllowlist.getAllowlist()).containsExactly(Pojo.class);
141+
}
142+
143+
@Test
144+
public void publicNestedPojoDepthOneMethod() {
145+
testParameter(new MethodAction(), "publicPojoDepthOne.key.key", false);
146+
}
147+
148+
@Test
149+
public void publicPojoDepthTwoMethod() {
150+
testParameter(new MethodAction(), "publicPojoDepthTwo.key", true);
151+
assertThat(threadAllowlist.getAllowlist()).containsExactly(Pojo.class);
152+
}
153+
154+
@Test
155+
public void publicNestedPojoDepthTwoMethod() {
156+
testParameter(new MethodAction(), "publicPojoDepthTwo.key.key", true);
157+
assertThat(threadAllowlist.getAllowlist()).containsExactly(Pojo.class);
158+
}
159+
160+
class FieldAction {
161+
@StrutsParameter
162+
private String privateStr;
163+
164+
@StrutsParameter
165+
public String publicStr;
166+
167+
public String publicStrNotAnnotated;
168+
169+
@StrutsParameter(depth = 1)
170+
private Pojo privatePojo;
171+
172+
@StrutsParameter
173+
public Pojo publicPojoDepthZero;
174+
175+
@StrutsParameter(depth = 1)
176+
public Pojo publicPojoDepthOne ;
177+
178+
@StrutsParameter(depth = 2)
179+
public Pojo publicPojoDepthTwo;
180+
}
181+
182+
class MethodAction {
183+
184+
@StrutsParameter
185+
private void setPrivateStr(String str) {
186+
}
187+
188+
@StrutsParameter
189+
public void setPublicStr(String str) {
190+
}
191+
192+
public void setPublicStrNotAnnotated(String str) {
193+
}
194+
195+
@StrutsParameter(depth = 1)
196+
private Pojo getPrivatePojo() {
197+
return null;
198+
}
199+
200+
@StrutsParameter
201+
public Pojo getPublicPojoDepthZero() {
202+
return null;
203+
}
204+
205+
@StrutsParameter(depth = 1)
206+
public Pojo getPublicPojoDepthOne() {
207+
return null;
208+
}
209+
210+
@StrutsParameter(depth = 2)
211+
public Pojo getPublicPojoDepthTwo() {
212+
return null;
213+
}
214+
}
215+
216+
class Pojo {
217+
}
218+
}

0 commit comments

Comments
 (0)