1
+ package software .amazon .smithy .typescript .codegen .integration ;
2
+
3
+ import java .util .List ;
4
+ import org .junit .jupiter .api .Test ;
5
+ import org .junit .jupiter .api .extension .ExtendWith ;
6
+ import org .mockito .Mock ;
7
+ import org .mockito .junit .jupiter .MockitoExtension ;
8
+ import software .amazon .smithy .codegen .core .CodegenException ;
9
+ import software .amazon .smithy .model .Model ;
10
+ import software .amazon .smithy .model .shapes .MemberShape ;
11
+ import software .amazon .smithy .model .shapes .ShapeId ;
12
+ import software .amazon .smithy .model .shapes .StructureShape ;
13
+ import software .amazon .smithy .model .shapes .UnionShape ;
14
+ import software .amazon .smithy .model .traits .HttpPayloadTrait ;
15
+ import software .amazon .smithy .model .traits .StreamingTrait ;
16
+
17
+ import static org .junit .jupiter .api .Assertions .*;
18
+ import static org .mockito .Mockito .when ;
19
+
20
+ @ ExtendWith (MockitoExtension .class )
21
+ class EventStreamGeneratorTest {
22
+ @ Test
23
+ void getEventStreamMember (
24
+ @ Mock ProtocolGenerator .GenerationContext context ,
25
+ @ Mock Model model ,
26
+ @ Mock StructureShape struct ,
27
+ @ Mock MemberShape eventStreamMember1 ,
28
+ @ Mock ShapeId streamingMember1ShapeId ,
29
+ @ Mock UnionShape streamingTarget1
30
+ ) {
31
+ when (struct .members ()).thenReturn (List .of (eventStreamMember1 ));
32
+ when (eventStreamMember1 .getTarget ()).thenReturn (streamingMember1ShapeId );
33
+ when (context .getModel ()).thenReturn (model );
34
+ when (model .expectShape (streamingMember1ShapeId )).thenReturn (streamingTarget1 );
35
+
36
+ when (streamingTarget1 .hasTrait (StreamingTrait .class )).thenReturn (true );
37
+ when (streamingTarget1 .isUnionShape ()).thenReturn (true );
38
+ when (eventStreamMember1 .hasTrait (StreamingTrait .class )).thenReturn (false );
39
+ when (eventStreamMember1 .hasTrait (HttpPayloadTrait .class )).thenReturn (true );
40
+
41
+ MemberShape eventStreamMember = EventStreamGenerator .getEventStreamMember (
42
+ context ,
43
+ struct
44
+ );
45
+
46
+ assertEquals (eventStreamMember1 , eventStreamMember );
47
+ }
48
+
49
+ @ Test
50
+ void getEventStreamMemberTooFew (
51
+ @ Mock ProtocolGenerator .GenerationContext context ,
52
+ @ Mock StructureShape struct
53
+ ) {
54
+ when (struct .members ()).thenReturn (List .of ());
55
+ when (struct .getId ()).thenReturn (ShapeId .from ("namespace#Shape" ));
56
+
57
+ try {
58
+ MemberShape eventStreamMember = EventStreamGenerator .getEventStreamMember (
59
+ context ,
60
+ struct
61
+ );
62
+ } catch (CodegenException e ) {
63
+ assertEquals (
64
+ "No event stream member found in " + struct .getId ().toString (),
65
+ e .getMessage ()
66
+ );
67
+ }
68
+ }
69
+
70
+ @ Test
71
+ void getEventStreamMemberTooMany (
72
+ @ Mock ProtocolGenerator .GenerationContext context ,
73
+ @ Mock Model model ,
74
+ @ Mock StructureShape struct ,
75
+ @ Mock MemberShape eventStreamMember1 ,
76
+ @ Mock ShapeId streamingMember1ShapeId ,
77
+ @ Mock UnionShape streamingTarget1 ,
78
+ @ Mock MemberShape eventStreamMember2 ,
79
+ @ Mock ShapeId streamingMember2ShapeId ,
80
+ @ Mock UnionShape streamingTarget2
81
+ ) {
82
+ when (struct .members ()).thenReturn (List .of (
83
+ eventStreamMember1 ,
84
+ eventStreamMember2
85
+ ));
86
+ when (context .getModel ()).thenReturn (model );
87
+ when (struct .getId ()).thenReturn (ShapeId .from ("namespace#Shape" ));
88
+
89
+ when (eventStreamMember1 .getTarget ()).thenReturn (streamingMember1ShapeId );
90
+ when (model .expectShape (streamingMember1ShapeId )).thenReturn (streamingTarget1 );
91
+ when (streamingTarget1 .hasTrait (StreamingTrait .class )).thenReturn (true );
92
+ when (streamingTarget1 .isUnionShape ()).thenReturn (true );
93
+ when (eventStreamMember1 .hasTrait (StreamingTrait .class )).thenReturn (false );
94
+ when (eventStreamMember1 .hasTrait (HttpPayloadTrait .class )).thenReturn (true );
95
+
96
+ when (eventStreamMember2 .getTarget ()).thenReturn (streamingMember2ShapeId );
97
+ when (model .expectShape (streamingMember2ShapeId )).thenReturn (streamingTarget2 );
98
+ when (streamingTarget2 .hasTrait (StreamingTrait .class )).thenReturn (true );
99
+ when (streamingTarget2 .isUnionShape ()).thenReturn (true );
100
+ when (eventStreamMember2 .hasTrait (StreamingTrait .class )).thenReturn (false );
101
+ when (eventStreamMember2 .hasTrait (HttpPayloadTrait .class )).thenReturn (true );
102
+
103
+ try {
104
+ MemberShape eventStreamMember = EventStreamGenerator .getEventStreamMember (
105
+ context ,
106
+ struct
107
+ );
108
+ } catch (CodegenException e ) {
109
+ assertEquals (
110
+ "More than one event stream member in " + struct .getId ().toString (),
111
+ e .getMessage ()
112
+ );
113
+ }
114
+ }
115
+ }
0 commit comments