@@ -43,35 +43,103 @@ final class EndToEndTest {
43
43
public static final ClientId ANY_CLIENT = new ClientId ("any client" );
44
44
public static final ClientId VIP_CLIENT = new ClientId ("VIP client" );
45
45
public static final ClientId NEW_CLIENT = new ClientId ("new client" );
46
-
46
+ public static final GenericResponse DEFAULT_HOME_PAGE = new GenericResponse ("html for the home page" );
47
+ public static final GenericResponse DEFAULT_NUMBER_PAGE = new GenericResponse (1000L );
48
+ public static final IllegalStateException NO_SUCH_PAGE = new IllegalStateException ("no such page" );
49
+ public static final GenericResponse VIP_HOME_PAGE = new GenericResponse ("VIP home page" );
47
50
private GenericIoStub target ;
48
51
49
52
@ BeforeEach
50
53
void setUp () {
51
54
this .target = new GenericIoStub ();
52
55
Stream .of (
53
56
Stub .forQueryId ("home" )
54
- .withContents (new GenericResponse ( "html for the home page" ) )
55
- .build ( ),
57
+ .withContents (DEFAULT_HOME_PAGE )
58
+ .withResponseId ( "home" ),
56
59
Stub .forQueryId ("non-existing" )
57
- .withContents (new GenericResponse (new IllegalStateException ( "no such page" ) ))
58
- .build ( ),
60
+ .withContents (new GenericResponse (NO_SUCH_PAGE ))
61
+ .withResponseId ( "non-existing" ),
59
62
Stub .forQueryId ("number" )
60
- .withContents (new GenericResponse ( 1000L ) )
61
- .build ( )
63
+ .withContents (DEFAULT_NUMBER_PAGE )
64
+ .withResponseId ( "number" )
62
65
).forEach (stub -> this .target .addCommonStub (stub ));
63
66
}
64
67
65
68
@ Test
66
69
void shouldHaveAPredefinedSetOfDefaultResponses () {
67
- Assertions .assertThat (this .target .nextResponseFor (ANY_CLIENT , HOME_PAGE ))
68
- .isEqualTo (new GenericResponse ("html for the home page" ));
69
- Assertions .assertThat (this .target .nextResponseFor (VIP_CLIENT , HOME_PAGE ))
70
- .isEqualTo (new GenericResponse ("html for the home page" ));
71
- Assertions .assertThatThrownBy (() -> this .target .nextResponseFor (ANY_CLIENT , MISSING_PAGE ))
72
- .isInstanceOf (IllegalStateException .class )
73
- .hasMessage ("no such page" );
74
- Assertions .assertThat (this .target .nextResponseFor (ANY_CLIENT , NUMBER_PAGE ))
75
- .isEqualTo (new GenericResponse (1000L ));
70
+ when (VIP_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
71
+ when (NEW_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
72
+ when (ANY_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
73
+ when (ANY_CLIENT ).requests (NUMBER_PAGE ).then (DEFAULT_NUMBER_PAGE );
74
+ when (NEW_CLIENT ).requests (NUMBER_PAGE ).then (DEFAULT_NUMBER_PAGE );
75
+ when (VIP_CLIENT ).requests (NUMBER_PAGE ).then (DEFAULT_NUMBER_PAGE );
76
+ when (ANY_CLIENT ).requests (MISSING_PAGE ).thenThrows (NO_SUCH_PAGE );
77
+ when (NEW_CLIENT ).requests (MISSING_PAGE ).thenThrows (NO_SUCH_PAGE );
78
+ when (VIP_CLIENT ).requests (MISSING_PAGE ).thenThrows (NO_SUCH_PAGE );
79
+ }
80
+
81
+ @ Test
82
+ void shouldSetSpecificStubForAClient () {
83
+ final Stub newHome = Stub .forQueryId ("home" ).withContents (VIP_HOME_PAGE ).withResponseId ("home" );
84
+ this .target .addClientStub (newHome , VIP_CLIENT );
85
+ when (VIP_CLIENT ).requests (HOME_PAGE ).then (VIP_HOME_PAGE );
86
+ when (NEW_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
87
+ when (ANY_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
88
+ }
89
+
90
+ @ Test
91
+ void shouldChooseActiveStubFromStoredStubs () {
92
+ final Stub newHome = Stub .forQueryId ("home" ).withContents (DEFAULT_NUMBER_PAGE ).withResponseId ("number" );
93
+ this .target .addClientStub (newHome , VIP_CLIENT );
94
+ when (VIP_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_NUMBER_PAGE );
95
+ when (NEW_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
96
+ when (ANY_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
97
+
98
+ this .target .setActiveResponse (VIP_CLIENT , HOME_PAGE , new ResponseId ("home" ));
99
+ when (VIP_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
100
+ when (NEW_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
101
+ when (ANY_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
102
+
103
+ this .target .setActiveResponse (VIP_CLIENT , HOME_PAGE , new ResponseId ("number" ));
104
+ when (VIP_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_NUMBER_PAGE );
105
+ when (NEW_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
106
+ when (ANY_CLIENT ).requests (HOME_PAGE ).then (DEFAULT_HOME_PAGE );
107
+ }
108
+
109
+
110
+ private When when (ClientId client ) {
111
+ return new When (client );
112
+ }
113
+
114
+ private class When {
115
+ private final ClientId client ;
116
+
117
+ public When (ClientId client ) {
118
+ this .client = client ;
119
+ }
120
+
121
+ public Then requests (QueryId query ) {
122
+ return new Then (client , query );
123
+ }
124
+ }
125
+
126
+ private class Then {
127
+ private final ClientId client ;
128
+ private final QueryId query ;
129
+
130
+ public Then (ClientId client , QueryId query ) {
131
+ this .client = client ;
132
+ this .query = query ;
133
+ }
134
+
135
+ public void then (GenericResponse response ) {
136
+ Assertions .assertThat (target .nextResponseFor (this .client , this .query )).isEqualTo (response );
137
+ }
138
+
139
+ public void thenThrows (RuntimeException exception ) {
140
+ Assertions .assertThatThrownBy (() -> target .nextResponseFor (this .client , this .query ))
141
+ .isInstanceOf (exception .getClass ())
142
+ .hasMessage (exception .getMessage ());
143
+ }
76
144
}
77
145
}
0 commit comments