1
+ package io .github .stavshamir .springwolf .asyncapi .scanners .channels .annotation ;
2
+
3
+
4
+ import org .junit .jupiter .api .Test ;
5
+ import org .springframework .messaging .handler .annotation .Payload ;
6
+
7
+ import java .lang .reflect .Method ;
8
+ import java .util .ArrayList ;
9
+ import java .util .List ;
10
+ import java .util .Optional ;
11
+
12
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
13
+
14
+ public class SpringPayloadAnnotationTypeExtractorTest {
15
+
16
+ @ Test
17
+ public void getPayloadType () throws NoSuchMethodException {
18
+ Method m = TestClass .class .getDeclaredMethod ("consumeWithString" , String .class );
19
+
20
+ Class <?> result = SpringPayloadAnnotationTypeExtractor .getPayloadType (m );
21
+
22
+ assertEquals (String .class , result );
23
+ }
24
+
25
+ @ Test
26
+ public void getPayloadTypeWithPayloadAnnotation () throws NoSuchMethodException {
27
+ Method m = TestClass .class .getDeclaredMethod ("consumeWithStringAndPayloadAnnotation" , String .class , Integer .class );
28
+
29
+ Class <?> result = SpringPayloadAnnotationTypeExtractor .getPayloadType (m );
30
+
31
+ assertEquals (String .class , result );
32
+ }
33
+
34
+ @ Test
35
+ public void getPayloadTypeWithListOfStrings () throws NoSuchMethodException {
36
+ Method m = TestClass .class .getDeclaredMethod ("consumeWithListOfStrings" , List .class );
37
+
38
+ Class <?> result = SpringPayloadAnnotationTypeExtractor .getPayloadType (m );
39
+
40
+ assertEquals (String .class , result );
41
+ }
42
+
43
+ @ Test
44
+ public void getPayloadTypeWithListOfInterfaces () throws NoSuchMethodException {
45
+ Method m = TestClass .class .getDeclaredMethod ("consumeWithListOfGenericClasses" , List .class );
46
+
47
+ Class <?> result = SpringPayloadAnnotationTypeExtractor .getPayloadType (m );
48
+
49
+ // Unable to resolve optional<String>, fallback to root type list
50
+ assertEquals (List .class , result );
51
+ }
52
+
53
+ @ Test
54
+ public void getPayloadTypeWithInterface () throws NoSuchMethodException {
55
+ Method m = TestClass .class .getDeclaredMethod ("consumeWithGenericClass" , Optional .class );
56
+
57
+ Class <?> result = SpringPayloadAnnotationTypeExtractor .getPayloadType (m );
58
+
59
+ assertEquals (Optional .class , result );
60
+ }
61
+
62
+ @ Test
63
+ public void getPayloadTypeWithListOfStringExtends () throws NoSuchMethodException {
64
+ Method m = TestClass .class .getDeclaredMethod ("consumeWithListOfStringExtends" , List .class );
65
+
66
+ Class <?> result = SpringPayloadAnnotationTypeExtractor .getPayloadType (m );
67
+
68
+ // Unable to resolve optional<String>, fallback to root type list
69
+ assertEquals (List .class , result );
70
+ }
71
+
72
+ @ Test
73
+ public void getPayloadTypeWithCustomType () throws NoSuchMethodException {
74
+ Method m = TestClass .class .getDeclaredMethod ("consumeWithCustomType" , TestClass .MyType .class );
75
+
76
+ Class <?> result = SpringPayloadAnnotationTypeExtractor .getPayloadType (m );
77
+
78
+ assertEquals (TestClass .MyType .class , result );
79
+ }
80
+
81
+ public static class TestClass {
82
+ public void consumeWithStringAndPayloadAnnotation (@ Payload String value , Integer value2 ) {}
83
+ public void consumeWithString (String value ) {}
84
+ public void consumeWithGenericClass (Optional <String > value ) {}
85
+ public void consumeWithListOfStrings (List <String > value ) {}
86
+ public void consumeWithListOfGenericClasses (List <Optional <String >> value ) {}
87
+ public void consumeWithListOfStringExtends (List <? extends String > value ) {}
88
+ public void consumeWithCustomType (MyType value ) {}
89
+
90
+ public static class MyType extends ArrayList <String > { }
91
+ }
92
+ }
0 commit comments