Skip to content

Commit 2b234c2

Browse files
committed
test : Unit test for SubjectResolver
1 parent c21afa0 commit 2b234c2

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.wwan13.wintersecurity.resolve;
18+
19+
import io.wwan13.wintersecurity.jwt.PayloadAnalysis;
20+
import io.wwan13.wintersecurity.jwt.PayloadAnalyst;
21+
import io.wwan13.wintersecurity.jwt.TokenClaims;
22+
import io.wwan13.wintersecurity.jwt.payload.annotation.Roles;
23+
import io.wwan13.wintersecurity.jwt.payload.annotation.Subject;
24+
import io.wwan13.wintersecurity.jwt.payload.support.DefaultPayloadAnalyst;
25+
26+
import java.util.Map;
27+
import java.util.Set;
28+
29+
public class ResolveTestContainer {
30+
31+
public static TargetAnnotations targetAnnotations = new TargetAnnotations(
32+
Set.of(RequestUserSubject.class, RequestUserId.class),
33+
Set.of(RequestUserRoles.class)
34+
);
35+
36+
public static PayloadAnalysis payloadAnalysis;
37+
38+
static {
39+
PayloadAnalyst payloadAnalyst = new DefaultPayloadAnalyst();
40+
payloadAnalysis = payloadAnalyst.analyze(ResolveTestPayload.class);
41+
}
42+
43+
public static TokenClaims defaultTestClaims = new TokenClaims(
44+
Map.of(
45+
"sub", "1",
46+
"roles", "ROLE_USER"
47+
)
48+
);
49+
50+
public static class ResolveTestPayload {
51+
@Subject
52+
Long subject;
53+
@Roles
54+
Set<String> roles;
55+
}
56+
57+
public static class ResolveTestPayloadWithStringRole {
58+
@Subject
59+
Long subject;
60+
@Roles
61+
String role;
62+
}
63+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.wwan13.wintersecurity.resolve;
18+
19+
import io.wwan13.wintersecurity.UnitTest;
20+
import io.wwan13.wintersecurity.resolve.stub.StubMethodParameter;
21+
import io.wwan13.wintersecurity.resolve.stub.StubModerAndViesContainer;
22+
import io.wwan13.wintersecurity.resolve.stub.StubNativeWebRequest;
23+
import io.wwan13.wintersecurity.resolve.stub.StubWebDataBinderFactory;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.util.Set;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
class SubjectResolverTest extends UnitTest {
31+
32+
static SubjectResolver subjectResolver = new SubjectResolver(
33+
ResolveTestContainer.targetAnnotations,
34+
ResolveTestContainer.payloadAnalysis
35+
);
36+
37+
@Test
38+
void should_ReturnTrue_when_AnnotationDeclaredWithValidParameterType() {
39+
// given
40+
final StubMethodParameter methodParameter = new StubMethodParameter();
41+
42+
methodParameter.declaredAnnotationsWillBe(RequestUserId.class);
43+
methodParameter.parameterTypeWillBe(Long.class);
44+
45+
// when
46+
boolean result = subjectResolver.supportsParameter(methodParameter);
47+
48+
// then
49+
assertThat(result).isTrue();
50+
}
51+
52+
@Test
53+
void should_ReturnFalse_when_InValidParameterType() {
54+
// given
55+
final StubMethodParameter methodParameter = new StubMethodParameter();
56+
57+
methodParameter.declaredAnnotationsWillBe(RequestUserId.class);
58+
methodParameter.parameterTypeWillBe(String.class);
59+
60+
// when
61+
boolean result = subjectResolver.supportsParameter(methodParameter);
62+
63+
// then
64+
assertThat(result).isFalse();
65+
}
66+
67+
@Test
68+
void should_ReturnFalse_when_AnnotationNotDeclared() {
69+
// given
70+
final StubMethodParameter methodParameter = new StubMethodParameter();
71+
72+
methodParameter.declaredAnnotationsWillBe(Set.of());
73+
methodParameter.parameterTypeWillBe(Long.class);
74+
75+
// when
76+
boolean result = subjectResolver.supportsParameter(methodParameter);
77+
78+
// then
79+
assertThat(result).isFalse();
80+
}
81+
82+
@Test
83+
void should_ReturnFalse_when_InValidParameterTypeIsDataType() {
84+
// given
85+
final StubMethodParameter methodParameter = new StubMethodParameter();
86+
87+
methodParameter.declaredAnnotationsWillBe(RequestUserId.class);
88+
methodParameter.parameterTypeWillBe(String.class);
89+
90+
// when
91+
boolean result = subjectResolver.supportsParameter(methodParameter);
92+
93+
// then
94+
assertThat(result).isFalse();
95+
}
96+
97+
@Test
98+
void should_ResolveSubject() {
99+
// given
100+
final StubMethodParameter methodParameter = new StubMethodParameter();
101+
final StubModerAndViesContainer modelAndViewContainer = new StubModerAndViesContainer();
102+
final StubNativeWebRequest nativeWebRequest = new StubNativeWebRequest();
103+
final StubWebDataBinderFactory webDataBinderFactory = new StubWebDataBinderFactory();
104+
105+
nativeWebRequest.requestAttributesWillBe(ResolveTestContainer.defaultTestClaims);
106+
methodParameter.parameterTypeWillBe(Long.class);
107+
108+
// when
109+
Object value = subjectResolver.resolveArgument(
110+
methodParameter,
111+
modelAndViewContainer,
112+
nativeWebRequest,
113+
webDataBinderFactory
114+
);
115+
116+
// then
117+
assertThat(value.getClass()).isAssignableFrom(Long.class);
118+
assertThat((Long) value).isEqualTo(1L);
119+
}
120+
}

0 commit comments

Comments
 (0)