Skip to content

Commit c4cbd1b

Browse files
committed
refactor: got rid of GenericRequest
Refs: #2
1 parent b7b07ff commit c4cbd1b

File tree

8 files changed

+81
-223
lines changed

8 files changed

+81
-223
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,9 @@ public class GenericIoStub {
5151
*/
5252
private final Set<Stub> stored = new HashSet<>();
5353

54-
/**
55-
* Returns the next response for the given query.
56-
*
57-
* @param query The query for which to get the next response.
58-
* @return The next response object.
59-
*/
60-
public GenericResponse nextResponseFor(final GenericRequest query) {
54+
public GenericResponse nextResponseFor(final ClientId client, final QueryId query) {
6155
return this.stubs
62-
.getOrDefault(query.clientId(), this.stubs.get(GenericIoStub.COMMON_CLIENT))
56+
.getOrDefault(client, this.stubs.get(GenericIoStub.COMMON_CLIENT))
6357
.nextResponseFor(query);
6458
}
6559

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

Lines changed: 0 additions & 96 deletions
This file was deleted.

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,23 @@ class SingleClientStubs {
4040
*/
4141
private final Map<QueryId, SingleQueryResponses> stubs = new HashMap<>();
4242

43-
/**
44-
* Returns the next response for the given query.
45-
*
46-
* @param query The query for which to get the next response.
47-
* @return The next response object.
48-
*/
43+
public void setSingleResponseFor(final QueryId query, final GenericResponse response) {
44+
this.stubs.put(query, new SingleQueryResponses(query.query(), response));
45+
}
46+
47+
public void setMultipleResponsesFor(final String query, final GenericResponse... responses) {
48+
this.stubs.put(new QueryId(query), new SingleQueryResponses(query, responses));
49+
}
50+
4951
@SneakyThrows
50-
public GenericResponse nextResponseFor(final GenericRequest query) {
51-
final QueryId key = query.queryId();
52-
if (!this.stubs.containsKey(key)) {
52+
public GenericResponse nextResponseFor(QueryId query) {
53+
if (!this.stubs.containsKey(query)) {
5354
throw new NoSuchElementException("No responses configured");
5455
}
55-
final GenericResponse response = this.stubs.get(key).next();
56+
final GenericResponse response = this.stubs.get(query).next();
5657
if (response.delay() > 0) {
5758
Thread.sleep(response.delay());
5859
}
5960
return response;
6061
}
61-
62-
public void setSingleResponseFor(final QueryId query, final GenericResponse response) {
63-
this.stubs.put(query, new SingleQueryResponses(query.query(), response));
64-
}
65-
66-
public void setMultipleResponsesFor(final String query, final GenericResponse... responses) {
67-
this.stubs.put(new QueryId(query), new SingleQueryResponses(query, responses));
68-
}
6962
}

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,40 @@
3434
* @since 0.1
3535
*/
3636
final class GenericIoStubTest {
37-
/**
38-
* The client id for the test.
39-
*/
40-
public static final ClientId SPEC_CLIENT = Mocks.testRequestFromVipClient().clientId();
37+
public static final ClientId ANY_CLIENT = new ClientId("any client");
38+
39+
public static final ClientId VIP_CLIENT = new ClientId("12345");
40+
41+
public static final ClientId NEW_CLIENT = new ClientId("new client");
42+
43+
public static final QueryId TEST_REQUEST = new QueryId("test_request");
4144

4245
@Test
4346
void createDefaultStub() {
4447
final GenericIoStub target = new GenericIoStub();
4548
target.addCommonStub(Mocks.defaultStub());
46-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromAnyClient()))
49+
Assertions.assertThat(target.nextResponseFor(ANY_CLIENT, TEST_REQUEST))
4750
.isEqualTo(Mocks.defaultResponse());
48-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromVipClient()))
51+
Assertions.assertThat(target.nextResponseFor(VIP_CLIENT, TEST_REQUEST))
4952
.isEqualTo(Mocks.defaultResponse());
5053
}
5154

5255
@Test
5356
void createInfiniteStubForSpecificClient() {
5457
final GenericIoStub target = new GenericIoStub();
5558
target.addCommonStub(Mocks.defaultStub());
56-
target.addClientStub(Mocks.emptyStub(), GenericIoStubTest.SPEC_CLIENT);
57-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromAnyClient()))
59+
target.addClientStub(Mocks.emptyStub(), GenericIoStubTest.VIP_CLIENT);
60+
Assertions.assertThat(target.nextResponseFor(ANY_CLIENT, TEST_REQUEST))
5861
.isEqualTo(Mocks.defaultResponse());
59-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromVipClient()))
62+
Assertions.assertThat(target.nextResponseFor(VIP_CLIENT, TEST_REQUEST))
6063
.isEqualTo(Mocks.emptyResponse());
6164
}
6265

6366
@Test
6467
void shouldAddResponseToStoredResponses() {
6568
final GenericIoStub target = new GenericIoStub();
6669
target.addCommonStub(Mocks.defaultStub());
67-
target.addClientStub(Mocks.emptyStub(), GenericIoStubTest.SPEC_CLIENT);
70+
target.addClientStub(Mocks.emptyStub(), GenericIoStubTest.VIP_CLIENT);
6871
final Set<Stub> stubs = target.getResponsesFor("test_request");
6972
Assertions.assertThat(stubs.stream().map(Stub::name).toList())
7073
.isNotEmpty()
@@ -77,9 +80,9 @@ void shouldAddResponseToStoredResponses() {
7780
@Test
7881
void shouldAddTheExceptionAsTheResponse() {
7982
final GenericIoStub target = new GenericIoStub();
80-
target.addClientStub(Mocks.errorStub(), GenericIoStubTest.SPEC_CLIENT);
83+
target.addClientStub(Mocks.errorStub(), GenericIoStubTest.VIP_CLIENT);
8184
Assertions
82-
.assertThatThrownBy(() -> target.nextResponseFor(Mocks.testRequestFromVipClient()))
85+
.assertThatThrownBy(() -> target.nextResponseFor(VIP_CLIENT, TEST_REQUEST))
8386
.isInstanceOf(RuntimeException.class)
8487
.hasMessage("test error");
8588
}
@@ -88,31 +91,31 @@ void shouldAddTheExceptionAsTheResponse() {
8891
void shouldActivateOneOfStoredResponses() {
8992
final GenericIoStub target = new GenericIoStub();
9093
target.addCommonStub(Mocks.defaultStub());
91-
target.addClientStub(Mocks.emptyStub(), GenericIoStubTest.SPEC_CLIENT);
92-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromVipClient()))
94+
target.addClientStub(Mocks.emptyStub(), GenericIoStubTest.VIP_CLIENT);
95+
Assertions.assertThat(target.nextResponseFor(VIP_CLIENT, TEST_REQUEST))
9396
.isEqualTo(Mocks.emptyResponse());
9497
target.setActiveResponse(
95-
GenericIoStubTest.SPEC_CLIENT,
98+
GenericIoStubTest.VIP_CLIENT,
9699
Mocks.defaultStub().query(),
97100
Mocks.defaultStub().name()
98101
);
99-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromVipClient()))
102+
Assertions.assertThat(target.nextResponseFor(VIP_CLIENT, TEST_REQUEST))
100103
.isEqualTo(Mocks.defaultResponse());
101104
}
102105

103106
@Test
104107
void shouldActivateStoredResponseForNewClient() {
105108
final GenericIoStub target = new GenericIoStub();
106109
target.addCommonStub(Mocks.defaultStub());
107-
target.addClientStub(Mocks.emptyStub(), GenericIoStubTest.SPEC_CLIENT);
108-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromNewClient()))
110+
target.addClientStub(Mocks.emptyStub(), GenericIoStubTest.VIP_CLIENT);
111+
Assertions.assertThat(target.nextResponseFor(NEW_CLIENT, TEST_REQUEST))
109112
.isEqualTo(Mocks.defaultResponse());
110113
target.setActiveResponse(
111-
Mocks.testRequestFromNewClient().clientId(),
114+
NEW_CLIENT,
112115
Mocks.emptyStub().query(),
113116
Mocks.emptyStub().name()
114117
);
115-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromNewClient()))
118+
Assertions.assertThat(target.nextResponseFor(NEW_CLIENT, TEST_REQUEST))
116119
.isEqualTo(Mocks.emptyResponse());
117120
}
118121
}

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

Lines changed: 0 additions & 49 deletions
This file was deleted.

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
package ewc.utilities.testableio.core;
2626

27-
import java.util.Map;
28-
2927
/**
3028
* The utility class providing several predefined test objects.
3129
*
@@ -36,18 +34,6 @@ private Mocks() {
3634
// Prevent instantiation of the utility class
3735
}
3836

39-
static GenericRequest testRequestFromAnyClient() {
40-
return Mocks.testFromClient("any client");
41-
}
42-
43-
static GenericRequest testRequestFromVipClient() {
44-
return Mocks.testFromClient("12345");
45-
}
46-
47-
static GenericRequest testRequestFromNewClient() {
48-
return Mocks.testFromClient("new client");
49-
}
50-
5137
/**
5238
* A test response object that can be used for testing purposes. It contains an empty map of
5339
* metadata, a delay of 0 milliseconds and a simple string as the contents.
@@ -106,17 +92,4 @@ static Stub defaultStub() {
10692
.build();
10793
}
10894

109-
/**
110-
* The test utility method to create a request object with "test_request" as query ID.
111-
*
112-
* @param client String representing the client ID.
113-
* @return A GenericRequest object for the specified client ID.
114-
*/
115-
private static GenericRequest testFromClient(final String client) {
116-
return GenericRequest.builder()
117-
.client(r -> client)
118-
.query(r -> "test_request")
119-
.parameters(Map.of())
120-
.build();
121-
}
12295
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.Map;
2828
import java.util.NoSuchElementException;
29+
import javax.management.Query;
2930
import org.assertj.core.api.Assertions;
3031
import org.junit.jupiter.api.Test;
3132

@@ -36,6 +37,9 @@
3637
* @since 0.1
3738
*/
3839
final class SingleClientStubsTest {
40+
41+
public static final QueryId TEST_REQUEST = new QueryId("test_request");
42+
3943
@Test
4044
void couldBeInstantiated() {
4145
final SingleClientStubs target = new SingleClientStubs();
@@ -45,9 +49,8 @@ void couldBeInstantiated() {
4549
@Test
4650
void throwsIfNoResponsesConfigured() {
4751
final SingleClientStubs target = new SingleClientStubs();
48-
final GenericRequest request = Mocks.testRequestFromAnyClient();
4952
Assertions.assertThatExceptionOfType(NoSuchElementException.class)
50-
.isThrownBy(() -> target.nextResponseFor(request))
53+
.isThrownBy(() -> target.nextResponseFor(TEST_REQUEST))
5154
.withMessage("No responses configured");
5255
}
5356

@@ -60,11 +63,11 @@ void returnsTheResponseThatCorrespondsToTheRequest() {
6063
target.setSingleResponseFor(
6164
new QueryId("test_request"), new GenericResponse("Recommendations page", 1000, Map.of())
6265
);
63-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromAnyClient()))
66+
Assertions.assertThat(target.nextResponseFor(TEST_REQUEST))
6467
.isNotNull()
6568
.extracting("contents").isEqualTo("Recommendations page");
6669
final long now = System.currentTimeMillis();
67-
Assertions.assertThat(target.nextResponseFor(Mocks.testRequestFromVipClient()))
70+
Assertions.assertThat(target.nextResponseFor(TEST_REQUEST))
6871
.isNotNull()
6972
.extracting("contents").isEqualTo("Recommendations page");
7073
Assertions.assertThat(System.currentTimeMillis() - now)

0 commit comments

Comments
 (0)