1
1
package org .springdoc .core .model ;
2
2
3
- import java .lang .reflect .Method ;
4
- import java .util .Locale ;
5
-
6
3
import org .junit .jupiter .api .Test ;
4
+ import org .mockito .MockedStatic ;
5
+ import org .mockito .Mockito ;
7
6
import org .springdoc .core .models .MethodAttributes ;
7
+ import org .springframework .core .annotation .AnnotatedElementUtils ;
8
+ import org .springframework .web .bind .annotation .RequestMapping ;
9
+
10
+ import java .lang .reflect .Method ;
11
+ import java .util .Locale ;
8
12
9
13
import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
14
+ import static org .mockito .BDDMockito .given ;
10
15
11
16
public class MethodAttributesTest {
12
17
13
- @ Test
14
- public void testMergeArrays () throws Exception {
15
- MethodAttributes methodAttributes = new MethodAttributes ("application/json" , "application/xml" , Locale .ENGLISH );
16
-
17
- String [] array1 = { "application/json" , "application/xml" };
18
- String [] array2 = { "application/xml" , "application/yaml" };
19
-
20
- String [] expected = { "application/json" , "application/xml" , "application/yaml" };
21
-
22
- Method mergeArraysMethod = MethodAttributes .class .getDeclaredMethod ("mergeArrays" , String [].class , String [].class );
23
- mergeArraysMethod .setAccessible (true );
24
- String [] result = (String []) mergeArraysMethod .invoke (methodAttributes , (Object ) array1 , (Object ) array2 );
25
-
26
- assertArrayEquals (expected , result );
27
- }
28
-
29
- @ Test
30
- public void testMergeArraysWithNullArray1 () throws Exception {
31
- MethodAttributes methodAttributes = new MethodAttributes ("application/json" , "application/xml" , Locale .ENGLISH );
32
-
33
- String [] array1 = null ;
34
- String [] array2 = { "application/xml" , "application/yaml" };
35
-
36
- String [] expected = { "application/xml" , "application/yaml" };
37
-
38
- Method mergeArraysMethod = MethodAttributes .class .getDeclaredMethod ("mergeArrays" , String [].class , String [].class );
39
- mergeArraysMethod .setAccessible (true );
40
- String [] result = (String []) mergeArraysMethod .invoke (methodAttributes , (Object ) array1 , (Object ) array2 );
41
-
42
- assertArrayEquals (expected , result );
43
- }
44
-
45
- @ Test
46
- public void testDefaultProducesMediaType () {
47
- MethodAttributes methodAttributes = new MethodAttributes ("application/json" , "application/xml" , Locale .ENGLISH );
48
-
49
- Method method = this .getClass ().getDeclaredMethods ()[0 ];
50
- methodAttributes .calculateConsumesProduces (method );
18
+ private static final String APPLICATION_JSON = "application/json" ;
19
+ private static final String APPLICATION_XML = "application/xml" ;
20
+ private static final String APPLICATION_YAML = "application/yaml" ;
51
21
52
- String [] expectedProduces = { "application/xml" };
53
- String [] resultProduces = methodAttributes .getMethodProduces ();
22
+ @ Test
23
+ void testMergeArrays () throws Exception {
24
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
54
25
55
- assertArrayEquals ( expectedProduces , resultProduces ) ;
56
- }
26
+ String [] array1 = { APPLICATION_JSON , APPLICATION_XML } ;
27
+ String [] array2 = { APPLICATION_XML , APPLICATION_YAML };
57
28
58
- @ Test
59
- public void testDefaultConsumesMediaType () {
60
- MethodAttributes methodAttributes = new MethodAttributes ("application/json" , "application/xml" , Locale .ENGLISH );
29
+ String [] expected = {APPLICATION_JSON , APPLICATION_XML , APPLICATION_YAML };
61
30
62
- Method method = this .getClass ().getDeclaredMethods ()[0 ];
63
- methodAttributes .calculateConsumesProduces (method );
31
+ Method mergeArraysMethod = MethodAttributes .class .getDeclaredMethod ("mergeArrays" , String [].class , String [].class );
32
+ mergeArraysMethod .setAccessible (true );
33
+ String [] result = (String []) mergeArraysMethod .invoke (methodAttributes , (Object ) array1 , (Object ) array2 );
34
+
35
+ assertArrayEquals (expected , result );
36
+ }
37
+
38
+ @ Test
39
+ void testMergeArraysWithNullArray1 () throws Exception {
40
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
64
41
65
- String [] expectedConsumes = { "application/json" };
66
- String [] resultConsumes = methodAttributes .getMethodConsumes ();
42
+ String [] array1 = null ;
43
+ String [] array2 = {APPLICATION_XML , APPLICATION_YAML };
44
+
45
+ String [] expected = {APPLICATION_XML , APPLICATION_YAML };
67
46
68
- assertArrayEquals (expectedConsumes , resultConsumes );
69
- }
47
+ Method mergeArraysMethod = MethodAttributes .class .getDeclaredMethod ("mergeArrays" , String [].class , String [].class );
48
+ mergeArraysMethod .setAccessible (true );
49
+ String [] result = (String []) mergeArraysMethod .invoke (methodAttributes , (Object ) array1 , (Object ) array2 );
50
+
51
+ assertArrayEquals (expected , result );
52
+ }
53
+
54
+ @ Test
55
+ void testDefaultProducesMediaType () {
56
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
57
+
58
+ Method method = this .getClass ().getDeclaredMethods ()[0 ];
59
+ methodAttributes .calculateConsumesProduces (method );
60
+
61
+ String [] expectedProduces = {APPLICATION_XML };
62
+ String [] resultProduces = methodAttributes .getMethodProduces ();
63
+
64
+ assertArrayEquals (expectedProduces , resultProduces );
65
+ }
66
+
67
+ @ Test
68
+ void testDefaultConsumesMediaType () {
69
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
70
+
71
+ Method method = this .getClass ().getDeclaredMethods ()[0 ];
72
+ methodAttributes .calculateConsumesProduces (method );
73
+
74
+ String [] expectedConsumes = {APPLICATION_JSON };
75
+ String [] resultConsumes = methodAttributes .getMethodConsumes ();
76
+
77
+ assertArrayEquals (expectedConsumes , resultConsumes );
78
+ }
79
+
80
+ @ Test
81
+ void methodConsumesOverridesClassConsumes () {
82
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
83
+ RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations (
84
+ new String []{APPLICATION_JSON , APPLICATION_XML },
85
+ new String []{APPLICATION_JSON , APPLICATION_XML }
86
+ );
87
+ Method method = this .getClass ().getDeclaredMethods ()[0 ];
88
+ try (MockedStatic <AnnotatedElementUtils > annotatedElementUtils = Mockito .mockStatic (AnnotatedElementUtils .class )) {
89
+ annotatedElementUtils .when (() -> AnnotatedElementUtils .findMergedAnnotation (method , RequestMapping .class ))
90
+ .thenReturn (requestMapping );
91
+
92
+ methodAttributes .setClassConsumes (new String []{APPLICATION_YAML });
93
+ methodAttributes .calculateConsumesProduces (method );
94
+
95
+ String [] expectedConsumes = {APPLICATION_JSON , APPLICATION_XML };
96
+ String [] resultConsumes = methodAttributes .getMethodConsumes ();
97
+
98
+ assertArrayEquals (expectedConsumes , resultConsumes );
99
+ }
100
+ }
101
+
102
+ @ Test
103
+ void methodProducesOverridesClassProduces () {
104
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
105
+ RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations (
106
+ new String []{APPLICATION_JSON , APPLICATION_XML },
107
+ new String []{APPLICATION_JSON , APPLICATION_XML }
108
+ );
109
+ Method method = this .getClass ().getDeclaredMethods ()[0 ];
110
+ try (MockedStatic <AnnotatedElementUtils > annotatedElementUtils = Mockito .mockStatic (AnnotatedElementUtils .class )) {
111
+ annotatedElementUtils .when (() -> AnnotatedElementUtils .findMergedAnnotation (method , RequestMapping .class ))
112
+ .thenReturn (requestMapping );
113
+
114
+ methodAttributes .setClassProduces (new String []{APPLICATION_YAML });
115
+ methodAttributes .calculateConsumesProduces (method );
116
+
117
+ String [] expectedProduces = {APPLICATION_JSON , APPLICATION_XML };
118
+ String [] resultProduces = methodAttributes .getMethodProduces ();
119
+
120
+ assertArrayEquals (expectedProduces , resultProduces );
121
+ }
122
+ }
123
+
124
+ @ Test
125
+ void methodConsumesIsSetToClassConsumesIfNoMethodConsumesIsDefined () {
126
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
127
+ RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations (
128
+ new String []{APPLICATION_JSON , APPLICATION_XML },
129
+ new String []{}
130
+ );
131
+ Method method = this .getClass ().getDeclaredMethods ()[0 ];
132
+ try (MockedStatic <AnnotatedElementUtils > annotatedElementUtils = Mockito .mockStatic (AnnotatedElementUtils .class )) {
133
+ annotatedElementUtils .when (() -> AnnotatedElementUtils .findMergedAnnotation (method , RequestMapping .class ))
134
+ .thenReturn (requestMapping );
135
+
136
+ String [] classConsumes = new String []{APPLICATION_YAML };
137
+ methodAttributes .setClassConsumes (classConsumes );
138
+ methodAttributes .calculateConsumesProduces (method );
139
+
140
+ String [] resultConsumes = methodAttributes .getMethodConsumes ();
141
+
142
+ assertArrayEquals (classConsumes , resultConsumes );
143
+ }
144
+ }
145
+
146
+ @ Test
147
+ void methodProducesIsSetToClassProducesIfNoMethodProducesIsDefined () {
148
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
149
+ RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations (
150
+ new String []{},
151
+ new String []{APPLICATION_JSON , APPLICATION_XML }
152
+ );
153
+ Method method = this .getClass ().getDeclaredMethods ()[0 ];
154
+ try (MockedStatic <AnnotatedElementUtils > annotatedElementUtils = Mockito .mockStatic (AnnotatedElementUtils .class )) {
155
+ annotatedElementUtils .when (() -> AnnotatedElementUtils .findMergedAnnotation (method , RequestMapping .class ))
156
+ .thenReturn (requestMapping );
157
+
158
+ String [] classProduces = new String []{APPLICATION_YAML };
159
+ methodAttributes .setClassProduces (classProduces );
160
+ methodAttributes .calculateConsumesProduces (method );
161
+
162
+ String [] resultProduces = methodAttributes .getMethodProduces ();
163
+
164
+ assertArrayEquals (classProduces , resultProduces );
165
+ }
166
+ }
167
+
168
+ @ Test
169
+ void methodConsumesIsSetToClassConsumesIfNoMethodConsumesIsDefinedAndClassConsumesNotSet () {
170
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
171
+ String [] classConsumes = new String []{APPLICATION_YAML };
172
+ RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations (
173
+ new String []{APPLICATION_JSON , APPLICATION_XML },
174
+ new String []{}
175
+ );
176
+ RequestMapping classMapping = givenAnnotationHasMediaTypeAnnotations (
177
+ new String []{},
178
+ classConsumes
179
+ );
180
+ Method method = this .getClass ().getDeclaredMethods ()[0 ];
181
+ try (MockedStatic <AnnotatedElementUtils > annotatedElementUtils = Mockito .mockStatic (AnnotatedElementUtils .class )) {
182
+ annotatedElementUtils .when (() -> AnnotatedElementUtils .findMergedAnnotation (method , RequestMapping .class ))
183
+ .thenReturn (requestMapping );
184
+ annotatedElementUtils .when (() -> AnnotatedElementUtils .findMergedAnnotation (method .getDeclaringClass (), RequestMapping .class ))
185
+ .thenReturn (classMapping );
186
+
187
+ methodAttributes .calculateConsumesProduces (method );
188
+
189
+ String [] resultConsumes = methodAttributes .getMethodConsumes ();
190
+
191
+ assertArrayEquals (classConsumes , resultConsumes );
192
+ }
193
+ }
194
+
195
+ @ Test
196
+ void methodProducesIsSetToClassProducesIfNoMethodProducesIsDefinedAndClassProducesNotSet () {
197
+ MethodAttributes methodAttributes = new MethodAttributes (APPLICATION_JSON , APPLICATION_XML , Locale .ENGLISH );
198
+ String [] classProduces = new String []{APPLICATION_YAML };
199
+ RequestMapping requestMapping = givenAnnotationHasMediaTypeAnnotations (
200
+ new String []{},
201
+ new String []{APPLICATION_JSON , APPLICATION_XML }
202
+ );
203
+ RequestMapping classMapping = givenAnnotationHasMediaTypeAnnotations (
204
+ classProduces ,
205
+ new String []{}
206
+ );
207
+ Method method = this .getClass ().getDeclaredMethods ()[0 ];
208
+ try (MockedStatic <AnnotatedElementUtils > annotatedElementUtils = Mockito .mockStatic (AnnotatedElementUtils .class )) {
209
+ annotatedElementUtils .when (() -> AnnotatedElementUtils .findMergedAnnotation (method , RequestMapping .class ))
210
+ .thenReturn (requestMapping );
211
+ annotatedElementUtils .when (() -> AnnotatedElementUtils .findMergedAnnotation (method .getDeclaringClass (), RequestMapping .class ))
212
+ .thenReturn (classMapping );
213
+
214
+ methodAttributes .calculateConsumesProduces (method );
215
+
216
+ String [] resultProduces = methodAttributes .getMethodProduces ();
217
+
218
+ assertArrayEquals (classProduces , resultProduces );
219
+ }
220
+ }
221
+
222
+ private RequestMapping givenAnnotationHasMediaTypeAnnotations (String [] produces , String [] consumes ) {
223
+ RequestMapping requestMapping = Mockito .mock (RequestMapping .class );
224
+ given (requestMapping .produces ()).willReturn (produces );
225
+ given (requestMapping .consumes ()).willReturn (consumes );
226
+ return requestMapping ;
227
+ }
70
228
}
0 commit comments