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