Skip to content

Commit 35e2067

Browse files
committed
test: basic end-to-end tests
Refs: #2
1 parent c4cbd1b commit 35e2067

File tree

4 files changed

+129
-48
lines changed

4 files changed

+129
-48
lines changed

src/main/java/ewc/utilities/testableio/core/Stub.java

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,28 @@
2525
package ewc.utilities.testableio.core;
2626

2727
import java.util.Objects;
28+
import lombok.NonNull;
2829

2930
/**
3031
* A stub for a query.
3132
*
32-
* @param query The ID of the query to be stubbed.
33-
* @param name The ID of the response.
34-
* @param response The response to be returned by the stub.
3533
* @since 0.2
3634
*/
37-
public record Stub(QueryId query, ResponseId name, GenericResponse response) {
35+
public final class Stub {
36+
private final QueryId query;
37+
private final ResponseId name;
38+
private final GenericResponse response;
39+
40+
/**
41+
* @param query The ID of the query to be stubbed.
42+
* @param name The ID of the response.
43+
* @param response The response to be returned by the stub.
44+
*/
45+
Stub(QueryId query, ResponseId name, GenericResponse response) {
46+
this.query = query;
47+
this.name = name;
48+
this.response = response;
49+
}
3850

3951
@SuppressWarnings("PMD.ProhibitPublicStaticMethod")
4052
public static QueryStubBuilder forQueryId(final String query) {
@@ -57,6 +69,27 @@ public int hashCode() {
5769
return Objects.hash(this.query, this.name);
5870
}
5971

72+
public QueryId query() {
73+
return query;
74+
}
75+
76+
public ResponseId name() {
77+
return name;
78+
}
79+
80+
public GenericResponse response() {
81+
return response;
82+
}
83+
84+
@Override
85+
public String toString() {
86+
return "Stub[" +
87+
"query=" + query + ", " +
88+
"name=" + name + ", " +
89+
"response=" + response + ']';
90+
}
91+
92+
6093
/**
6194
* Interface for building a stub for a specific query ID.
6295
*
@@ -72,9 +105,7 @@ public interface QueryStubBuilder {
72105
* @since 0.2
73106
*/
74107
public interface StubBuilder {
75-
StubBuilder withResponseId(String name);
76-
77-
Stub build();
108+
Stub withResponseId(String name);
78109

79110
}
80111

@@ -117,28 +148,14 @@ private static class ConcreteStubBuilder implements StubBuilder {
117148
*/
118149
private final GenericResponse response;
119150

120-
/**
121-
* The name of the response.
122-
*/
123-
private String name;
124-
125151
ConcreteStubBuilder(final String query, final GenericResponse response) {
126152
this.query = query;
127153
this.response = response;
128154
}
129155

130156
@Override
131-
public StubBuilder withResponseId(final String identifier) {
132-
this.name = identifier;
133-
return this;
134-
}
135-
136-
@Override
137-
public Stub build() {
138-
if (this.name == null) {
139-
this.name = this.query;
140-
}
141-
return new Stub(new QueryId(this.query), new ResponseId(this.name), this.response);
157+
public Stub withResponseId(@NonNull final String name) {
158+
return new Stub(new QueryId(this.query), new ResponseId(name), this.response);
142159
}
143160
}
144161
}

src/test/java/ewc/utilities/testableio/core/Mocks.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,21 @@ static Stub emptyStub() {
7272
return Stub
7373
.forQueryId("test_request")
7474
.withContents(Mocks.emptyResponse())
75-
.withResponseId("empty_response")
76-
.build();
75+
.withResponseId("empty_response");
7776
}
7877

7978
static Stub errorStub() {
8079
return Stub
8180
.forQueryId("test_request")
8281
.withContents(Mocks.errorResponse())
83-
.withResponseId("error_response")
84-
.build();
82+
.withResponseId("error_response");
8583
}
8684

8785
static Stub defaultStub() {
8886
return Stub
8987
.forQueryId("test_request")
9088
.withContents(Mocks.defaultResponse())
91-
.withResponseId("default_response")
92-
.build();
89+
.withResponseId("default_response");
9390
}
9491

9592
}

src/test/java/ewc/utilities/testableio/core/StubBuilderTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void shouldBuildACommonStub() {
4848
final Stub common = Stub
4949
.forQueryId(StubBuilderTest.QUERY.query())
5050
.withContents(Mocks.defaultResponse())
51-
.build();
51+
.withResponseId("recommendationIds");
5252
Assertions.assertThat(common)
5353
.isEqualTo(
5454
new Stub(
@@ -64,8 +64,7 @@ void shouldBuildWithSpecificName() {
6464
final Stub specific = Stub
6565
.forQueryId(StubBuilderTest.QUERY.query())
6666
.withContents(Mocks.defaultResponse())
67-
.withResponseId("myStub")
68-
.build();
67+
.withResponseId("myStub");
6968
Assertions.assertThat(specific)
7069
.isEqualTo(
7170
new Stub(

src/test/java/ewc/utilities/testableio/external/EndToEndTest.java

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,103 @@ final class EndToEndTest {
4343
public static final ClientId ANY_CLIENT = new ClientId("any client");
4444
public static final ClientId VIP_CLIENT = new ClientId("VIP client");
4545
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");
4750
private GenericIoStub target;
4851

4952
@BeforeEach
5053
void setUp() {
5154
this.target = new GenericIoStub();
5255
Stream.of(
5356
Stub.forQueryId("home")
54-
.withContents(new GenericResponse("html for the home page"))
55-
.build(),
57+
.withContents(DEFAULT_HOME_PAGE)
58+
.withResponseId("home"),
5659
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"),
5962
Stub.forQueryId("number")
60-
.withContents(new GenericResponse(1000L))
61-
.build()
63+
.withContents(DEFAULT_NUMBER_PAGE)
64+
.withResponseId("number")
6265
).forEach(stub -> this.target.addCommonStub(stub));
6366
}
6467

6568
@Test
6669
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+
}
76144
}
77145
}

0 commit comments

Comments
 (0)