Skip to content

Commit 76f8359

Browse files
committed
feat: ability to peek at current responses
Closes: #4
1 parent 35e2067 commit 76f8359

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public class GenericIoStub {
5252
private final Set<Stub> stored = new HashSet<>();
5353

5454
public GenericResponse nextResponseFor(final ClientId client, final QueryId query) {
55-
return this.stubs
56-
.getOrDefault(client, this.stubs.get(GenericIoStub.COMMON_CLIENT))
57-
.nextResponseFor(query);
55+
return this.stubsForClient(client).nextResponseFor(query);
5856
}
5957

6058
/**
@@ -109,14 +107,27 @@ public void setActiveResponse(
109107
});
110108
}
111109

110+
public Map<QueryId, GenericResponse> getCurrentCommonStubs() {
111+
return this.stubs.get(GenericIoStub.COMMON_CLIENT).currentActive();
112+
}
113+
114+
public Map<QueryId, GenericResponse> getCurrentClientStubs(final ClientId client) {
115+
final Map<QueryId, GenericResponse> result = new HashMap<>(
116+
this.stubs.get(GenericIoStub.COMMON_CLIENT).currentActive()
117+
);
118+
result.putAll(this.stubsForClient(client).currentActive());
119+
return result;
120+
}
121+
112122
/**
113123
* Sets the active response for a specific client.
114124
*
115125
* @param client The client ID for which the response is to be set.
116126
* @param stub The response to be set as active.
117127
*/
118128
private void setActiveResponse(final ClientId client, final Stub stub) {
119-
this.getStubsFor(client).setSingleResponseFor(stub.query(), stub.response());
129+
this.stubs.putIfAbsent(client, new SingleClientStubs());
130+
this.stubs.get(client).setSingleResponseFor(stub.query(), stub.response());
120131
}
121132

122133
/**
@@ -127,11 +138,10 @@ private void setActiveResponse(final ClientId client, final Stub stub) {
127138
*/
128139
private void addStub(final Stub stub, final ClientId client) {
129140
this.stored.add(stub);
130-
this.getStubsFor(client).setSingleResponseFor(stub.query(), stub.response());
141+
this.setActiveResponse(client, stub);
131142
}
132143

133-
private SingleClientStubs getStubsFor(final ClientId client) {
134-
this.stubs.putIfAbsent(client, new SingleClientStubs());
135-
return this.stubs.get(client);
144+
private SingleClientStubs stubsForClient(ClientId client) {
145+
return this.stubs.getOrDefault(client, this.stubs.get(GenericIoStub.COMMON_CLIENT));
136146
}
137147
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ public GenericResponse nextResponseFor(QueryId query) {
5959
}
6060
return response;
6161
}
62+
63+
public Map<QueryId, GenericResponse> currentActive() {
64+
final Map<QueryId, GenericResponse> active = new HashMap<>(this.stubs.size());
65+
this.stubs.keySet().forEach(query -> {
66+
active.put(query, this.stubs.get(query).peek());
67+
});
68+
return active;
69+
}
6270
}

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.NoSuchElementException;
2828
import java.util.concurrent.atomic.AtomicInteger;
29+
import java.util.function.Supplier;
2930

3031
/**
3132
* This class provides configured responses for a single I/O operation stub. The responses could
@@ -96,6 +97,24 @@ private SingleQueryResponses(
9697
* @return The next configured response.
9798
*/
9899
public GenericResponse next() {
100+
final GenericResponse value = responseUsing(this.index::getAndIncrement);
101+
if (value.contents() instanceof RuntimeException runtimeException) {
102+
throw runtimeException;
103+
}
104+
return value;
105+
}
106+
107+
/**
108+
* Returns the next configured response without "removing" it from responses queue.
109+
* If the queue is already exhausted, a {@link NoSuchElementException} will be thrown.
110+
*
111+
* @return The next configured response.
112+
*/
113+
public GenericResponse peek() {
114+
return responseUsing(this.index::currentValue);
115+
}
116+
117+
private GenericResponse responseUsing(Supplier<Integer> counter) {
99118
if (this.values == null) {
100119
throw new IllegalStateException("No response to send");
101120
}
@@ -104,11 +123,7 @@ public GenericResponse next() {
104123
String.format("No more configured responses for %s", this.description)
105124
);
106125
}
107-
final GenericResponse value = this.values[this.index.getAndIncrement()];
108-
if (value.contents() instanceof RuntimeException runtimeException) {
109-
throw runtimeException;
110-
}
111-
return value;
126+
return this.values[counter.get()];
112127
}
113128

114129
/**

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

+26-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package ewc.utilities.testableio.external;
2626

2727
import ewc.utilities.testableio.core.*;
28+
import java.util.Map;
2829
import java.util.stream.Stream;
2930
import org.assertj.core.api.Assertions;
3031
import org.junit.jupiter.api.BeforeEach;
@@ -43,10 +44,11 @@ final class EndToEndTest {
4344
public static final ClientId ANY_CLIENT = new ClientId("any client");
4445
public static final ClientId VIP_CLIENT = new ClientId("VIP client");
4546
public static final ClientId NEW_CLIENT = new ClientId("new client");
47+
public static final IllegalStateException NO_SUCH_PAGE_EXCEPTION = new IllegalStateException("no such page");
4648
public static final GenericResponse DEFAULT_HOME_PAGE = new GenericResponse("html for the home page");
4749
public static final GenericResponse DEFAULT_NUMBER_PAGE = new GenericResponse(1000L);
48-
public static final IllegalStateException NO_SUCH_PAGE = new IllegalStateException("no such page");
4950
public static final GenericResponse VIP_HOME_PAGE = new GenericResponse("VIP home page");
51+
public static final GenericResponse NO_SUCH_PAGE = new GenericResponse(NO_SUCH_PAGE_EXCEPTION);
5052
private GenericIoStub target;
5153

5254
@BeforeEach
@@ -57,7 +59,7 @@ void setUp() {
5759
.withContents(DEFAULT_HOME_PAGE)
5860
.withResponseId("home"),
5961
Stub.forQueryId("non-existing")
60-
.withContents(new GenericResponse(NO_SUCH_PAGE))
62+
.withContents(NO_SUCH_PAGE)
6163
.withResponseId("non-existing"),
6264
Stub.forQueryId("number")
6365
.withContents(DEFAULT_NUMBER_PAGE)
@@ -73,9 +75,9 @@ void shouldHaveAPredefinedSetOfDefaultResponses() {
7375
when(ANY_CLIENT).requests(NUMBER_PAGE).then(DEFAULT_NUMBER_PAGE);
7476
when(NEW_CLIENT).requests(NUMBER_PAGE).then(DEFAULT_NUMBER_PAGE);
7577
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);
78+
when(ANY_CLIENT).requests(MISSING_PAGE).thenThrows(NO_SUCH_PAGE_EXCEPTION);
79+
when(NEW_CLIENT).requests(MISSING_PAGE).thenThrows(NO_SUCH_PAGE_EXCEPTION);
80+
when(VIP_CLIENT).requests(MISSING_PAGE).thenThrows(NO_SUCH_PAGE_EXCEPTION);
7981
}
8082

8183
@Test
@@ -106,6 +108,25 @@ void shouldChooseActiveStubFromStoredStubs() {
106108
when(ANY_CLIENT).requests(HOME_PAGE).then(DEFAULT_HOME_PAGE);
107109
}
108110

111+
@Test
112+
void shouldReturnCorrectCommonState() {
113+
final Map<QueryId, GenericResponse> actual = this.target.getCurrentCommonStubs();
114+
Assertions.assertThat(actual)
115+
.containsEntry(HOME_PAGE, DEFAULT_HOME_PAGE)
116+
.containsEntry(NUMBER_PAGE, DEFAULT_NUMBER_PAGE)
117+
.containsEntry(MISSING_PAGE, NO_SUCH_PAGE);
118+
}
119+
120+
@Test
121+
void shouldCombineCommonAndClientCurrentState() {
122+
final Stub newHome = Stub.forQueryId("home").withContents(VIP_HOME_PAGE).withResponseId("home");
123+
this.target.addClientStub(newHome, VIP_CLIENT);
124+
final Map<QueryId, GenericResponse> actual = this.target.getCurrentClientStubs(VIP_CLIENT);
125+
Assertions.assertThat(actual)
126+
.containsEntry(HOME_PAGE, VIP_HOME_PAGE)
127+
.containsEntry(NUMBER_PAGE, DEFAULT_NUMBER_PAGE)
128+
.containsEntry(MISSING_PAGE, NO_SUCH_PAGE);
129+
}
109130

110131
private When when(ClientId client) {
111132
return new When(client);

0 commit comments

Comments
 (0)