Skip to content

Commit 5f7fa0f

Browse files
committed
refactor: better name for a single query stub response
1 parent 9009ead commit 5f7fa0f

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<artifactId>qulice-maven-plugin</artifactId>
5050
<configuration>
5151
<excludes>
52-
<exclude>pmd:/src/main/java/ewc/utilities/testableio/core/ConfiguredResponse.java</exclude>
52+
<exclude>pmd:/src/main/java/ewc/utilities/testableio/core/SingleQueryResponses.java</exclude>
5353
</excludes>
5454
</configuration>
5555
</plugin>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ConfigurableResponses {
3939
/**
4040
* The storage for all the configured responses.
4141
*/
42-
private final Map<String, ConfiguredResponse> responses = new HashMap<>();
42+
private final Map<String, SingleQueryResponses> responses = new HashMap<>();
4343

4444
/**
4545
* Returns the next response for the given request.
@@ -65,12 +65,12 @@ public GenericResponse<?> nextResponseFor(final GenericRequest<?> request) {
6565
return response;
6666
}
6767

68-
public void setDefaultResponsesFor(final String query, final ConfiguredResponse response) {
68+
public void setDefaultResponsesFor(final String query, final SingleQueryResponses response) {
6969
this.responses.put(query, response);
7070
}
7171

7272
public void setResponsesFor(
73-
final String client, final String query, final ConfiguredResponse response
73+
final String client, final String query, final SingleQueryResponses response
7474
) {
7575
this.responses.put(ConfigurableResponses.responseKeyFor(client, query), response);
7676
}

src/main/java/ewc/utilities/testableio/core/ConfiguredResponse.java renamed to src/main/java/ewc/utilities/testableio/core/SingleQueryResponses.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
import java.util.stream.Stream;
3131

3232
/**
33-
* This class provides configured responses for all the I/O operations.
33+
* This class provides configured responses for a single I/O opersation stub. The responses could
34+
* be in a form of a single response or an array of responses. The single response will be returned
35+
* forever, while the array of responses will be returned sequentially until it runs out of
36+
* responses. In such case, a {@link NoSuchElementException} will be thrown.
3437
*
3538
* @since 0.1
3639
*/
37-
public class ConfiguredResponse {
40+
public class SingleQueryResponses {
3841
/**
3942
* The iterator of configured responses. This iterator is used to provide
4043
* responses sequentially.
@@ -47,24 +50,43 @@ public class ConfiguredResponse {
4750
*/
4851
private final String description;
4952

50-
public ConfiguredResponse(final String name, final GenericResponse<?> value) {
51-
this(Stream.generate(() -> value).iterator(), name);
53+
/**
54+
* Single response constructor. It means that the same response will be returned forever.
55+
*
56+
* @param stub The short description of this stub instance.
57+
* @param value The response to be returned forever.
58+
*/
59+
public SingleQueryResponses(final String stub, final GenericResponse<?> value) {
60+
this(Stream.generate(() -> value).iterator(), stub);
5261
}
5362

54-
public ConfiguredResponse(final String name, final GenericResponse<?>... values) {
55-
this(Arrays.stream(values).iterator(), name);
63+
/**
64+
* Array of responses constructor. It means that the responses will be returned sequentially
65+
* until the array runs out of responses.
66+
*
67+
* @param stub The short description of this stub instance.
68+
* @param values The array of responses to be returned sequentially.
69+
*/
70+
public SingleQueryResponses(final String stub, final GenericResponse<?>... values) {
71+
this(Arrays.stream(values).iterator(), stub);
5672
}
5773

58-
private ConfiguredResponse(
59-
final Iterator<? extends GenericResponse<?>> values, final String name
74+
/**
75+
* Primary constructor.
76+
*
77+
* @param values The iterator that returns configured responses sequentially.
78+
* @param description The short description of this instance.
79+
*/
80+
private SingleQueryResponses(
81+
final Iterator<? extends GenericResponse<?>> values, final String description
6082
) {
6183
this.values = values;
62-
this.description = name;
84+
this.description = description;
6385
}
6486

6587
/**
6688
* Returns the next response in the iterator. If the iterator has no more
67-
* responses, a NoSuchElementException will be thrown. If the response
89+
* responses, a {@link NoSuchElementException} will be thrown. If the response
6890
* contains a RuntimeException, it will be thrown immediately.
6991
*
7092
* @return The next configured response.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@ void returnsTheResponseThatCorrespondsToTheRequest() {
5757
final ConfigurableResponses target = new ConfigurableResponses();
5858
target.setDefaultResponsesFor(
5959
"getHomePage",
60-
new ConfiguredResponse(
60+
new SingleQueryResponses(
6161
"home",
6262
new GenericResponse<>("Home page", 0, Map.of())
6363
)
6464
);
6565
target.setDefaultResponsesFor(
6666
"getItemRecommendations",
67-
new ConfiguredResponse(
67+
new SingleQueryResponses(
6868
"recommendations",
6969
new GenericResponse<>("Authorization page", 1000, Map.of())
7070
)
7171
);
7272
target.setResponsesFor(
7373
"12345",
7474
"getItemRecommendations",
75-
new ConfiguredResponse(
75+
new SingleQueryResponses(
7676
"recommendations",
7777
new GenericResponse<>("Recommendations page", 0, Map.of())
7878
)

src/test/java/ewc/utilities/testableio/core/ConfiguredResponseTest.java renamed to src/test/java/ewc/utilities/testableio/core/SingleQueryResponsesTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@
3030
import org.junit.jupiter.api.Test;
3131

3232
/**
33-
* Unit-tests for the {@link ConfiguredResponse} class.
33+
* Unit-tests for the {@link SingleQueryResponses} class.
3434
*
3535
* @since 0.1
3636
*/
37-
final class ConfiguredResponseTest {
37+
final class SingleQueryResponsesTest {
3838
@Test
3939
void shouldReturnTheSameResponseForeverIfItIsASingleResponse() {
40-
final ConfiguredResponse response = new ConfiguredResponse(
40+
final SingleQueryResponses response = new SingleQueryResponses(
4141
"single response",
42-
ConfiguredResponseTest.getGenericResponse()
42+
SingleQueryResponsesTest.getGenericResponse()
4343
);
4444
Assertions.assertThat(response.next())
4545
.isEqualTo(response.next())
@@ -49,9 +49,9 @@ void shouldReturnTheSameResponseForeverIfItIsASingleResponse() {
4949

5050
@Test
5151
void shouldReturnJustOneResponseIfItIsAnArrayWithOneElement() {
52-
final ConfiguredResponse response = new ConfiguredResponse(
52+
final SingleQueryResponses response = new SingleQueryResponses(
5353
"array with one element",
54-
new GenericResponse<?>[] {ConfiguredResponseTest.getGenericResponse()}
54+
new GenericResponse<?>[] {SingleQueryResponsesTest.getGenericResponse()}
5555
);
5656
Assertions.assertThat(response.next())
5757
.extracting("contents").isEqualTo("sample response");
@@ -62,7 +62,7 @@ void shouldReturnJustOneResponseIfItIsAnArrayWithOneElement() {
6262

6363
@Test
6464
void shouldThrowAnExceptionThatIsSetAsAResponse() {
65-
final ConfiguredResponse response = new ConfiguredResponse(
65+
final SingleQueryResponses response = new SingleQueryResponses(
6666
"exception response",
6767
new GenericResponse<>(
6868
new ArithmeticException("test exception"),
@@ -77,7 +77,7 @@ void shouldThrowAnExceptionThatIsSetAsAResponse() {
7777

7878
@Test
7979
void shouldReturnAllTheSpecifiedResponsesInOrder() {
80-
final ConfiguredResponse response = new ConfiguredResponse(
80+
final SingleQueryResponses response = new SingleQueryResponses(
8181
"multiple responses",
8282
new GenericResponse<>("response 1", 0, Map.of()),
8383
new GenericResponse<>(new ArithmeticException("test exception"), 0, Map.of())

0 commit comments

Comments
 (0)