Skip to content

Commit

Permalink
Remove superflous method from ReadWriteUtils (#10608)
Browse files Browse the repository at this point in the history
Signed-off-by: MichaelMorris <michael.morris@est.tech>
  • Loading branch information
MichaelMorrisEst authored Sep 21, 2024
1 parent 2fad621 commit 3649beb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public boolean isRunning() {
*/
public void setupCCStateResponse() {
// Non-verbose response
JsonBody jsonProposalNotReady = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-State-proposal-not-ready.json"));
JsonBody jsonProposalNotReady = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-State-proposal-not-ready.json"));

server
.when(
Expand All @@ -146,7 +146,7 @@ public void setupCCStateResponse() {


// Non-verbose response
JsonBody json = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-State.json"));
JsonBody json = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-State.json"));

server
.when(
Expand All @@ -164,7 +164,7 @@ public void setupCCStateResponse() {
.withDelay(TimeUnit.SECONDS, 0));

// Verbose response
JsonBody jsonVerbose = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-State-verbose.json"));
JsonBody jsonVerbose = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-State-verbose.json"));

server
.when(
Expand All @@ -188,7 +188,7 @@ public void setupCCStateResponse() {
*/
public void setupCCRebalanceNotEnoughDataError(CruiseControlEndpoints endpoint) {
// Rebalance response with no goal that returns an error
JsonBody jsonError = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-Rebalance-NotEnoughValidWindows-error.json"));
JsonBody jsonError = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-Rebalance-NotEnoughValidWindows-error.json"));

server
.when(
Expand All @@ -214,7 +214,7 @@ public void setupCCRebalanceNotEnoughDataError(CruiseControlEndpoints endpoint)
*/
public void setupCCBrokerDoesNotExist(CruiseControlEndpoints endpoint) {
// Add/remove broker response with no goal that returns an error
JsonBody jsonError = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-Broker-not-exist.json"));
JsonBody jsonError = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-Broker-not-exist.json"));

server
.when(
Expand Down Expand Up @@ -248,7 +248,7 @@ public void setupCCRebalanceResponse(int pendingCalls, CruiseControlEndpoints en
*/
public void setupCCRebalanceResponse(int pendingCalls, int responseDelay, CruiseControlEndpoints endpoint) {
// Rebalance in progress response with no goals set - non-verbose
JsonBody pendingJson = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-Rebalance-no-goals-in-progress.json"));
JsonBody pendingJson = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-Rebalance-no-goals-in-progress.json"));
server
.when(
request()
Expand All @@ -269,7 +269,7 @@ public void setupCCRebalanceResponse(int pendingCalls, int responseDelay, Cruise
.withDelay(TimeUnit.SECONDS, responseDelay));

// Rebalance response with no goals set - non-verbose
JsonBody json = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-Rebalance-no-goals.json"));
JsonBody json = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-Rebalance-no-goals.json"));

server
.when(
Expand All @@ -290,7 +290,7 @@ public void setupCCRebalanceResponse(int pendingCalls, int responseDelay, Cruise
.withDelay(TimeUnit.SECONDS, responseDelay));

// Rebalance response with no goals set - verbose
JsonBody jsonVerbose = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-Rebalance-no-goals-verbose.json"));
JsonBody jsonVerbose = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-Rebalance-no-goals-verbose.json"));

server
.when(
Expand All @@ -316,7 +316,8 @@ public void setupCCRebalanceResponse(int pendingCalls, int responseDelay, Cruise
*/
public void setupCCRebalanceBadGoalsError(CruiseControlEndpoints endpoint) {
// Response if the user has set custom goals which do not include all configured hard.goals
JsonBody jsonError = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-Rebalance-bad-goals-error.json"));
JsonBody jsonError = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-Rebalance-bad-goals-error.json"));


server
.when(
Expand All @@ -339,7 +340,7 @@ public void setupCCRebalanceBadGoalsError(CruiseControlEndpoints endpoint) {

// Response if the user has set custom goals which do not include all configured hard.goals
// Note: This uses the no-goals example response but the difference between custom goals and default goals is not tested here
JsonBody jsonSummary = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-Rebalance-no-goals-verbose.json"));
JsonBody jsonSummary = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-Rebalance-no-goals-verbose.json"));

server
.when(
Expand Down Expand Up @@ -372,9 +373,9 @@ public void setupCCRebalanceBadGoalsError(CruiseControlEndpoints endpoint) {
*/
public void setupCCUserTasksResponseNoGoals(int activeCalls, int inExecutionCalls) throws IOException, URISyntaxException {
// User tasks response for the rebalance request with no goals set (non-verbose)
JsonBody jsonActive = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-Active.json"));
JsonBody jsonInExecution = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-inExecution.json"));
JsonBody jsonCompleted = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-completed.json"));
JsonBody jsonActive = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-Active.json"));
JsonBody jsonInExecution = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-inExecution.json"));
JsonBody jsonCompleted = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-completed.json"));

// The first activeCalls times respond that with a status of "Active"
server
Expand Down Expand Up @@ -431,9 +432,9 @@ public void setupCCUserTasksResponseNoGoals(int activeCalls, int inExecutionCall
.withDelay(TimeUnit.SECONDS, 0));

// User tasks response for the rebalance request with no goals set (verbose)
JsonBody jsonActiveVerbose = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-verbose-Active.json"));
JsonBody jsonInExecutionVerbose = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-verbose-inExecution.json"));
JsonBody jsonCompletedVerbose = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-verbose-completed.json"));
JsonBody jsonActiveVerbose = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-verbose-Active.json"));
JsonBody jsonInExecutionVerbose = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-verbose-inExecution.json"));
JsonBody jsonCompletedVerbose = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-User-task-rebalance-no-goals-verbose-completed.json"));

// The first activeCalls times respond that with a status of "Active"
server
Expand Down Expand Up @@ -496,7 +497,7 @@ public void setupCCUserTasksResponseNoGoals(int activeCalls, int inExecutionCall
*/
public void setupCCUserTasksCompletedWithError() throws IOException, URISyntaxException {
// This simulates asking for the status of a task that has Complete with error and fetch_completed_task=true
JsonBody compWithErrorJson = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-User-task-status-completed-with-error.json"));
JsonBody compWithErrorJson = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-User-task-status-completed-with-error.json"));

server
.when(
Expand All @@ -520,7 +521,7 @@ public void setupCCUserTasksCompletedWithError() throws IOException, URISyntaxEx
*/
public void setupUserTasktoEmpty() {
// This simulates asking for the status with empty user task
JsonBody jsonEmptyUserTask = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-User-task-status-empty.json"));
JsonBody jsonEmptyUserTask = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-User-task-status-empty.json"));

server
.when(
Expand All @@ -543,7 +544,7 @@ public void setupUserTasktoEmpty() {
* Setup response of task being stopped.
*/
public void setupCCStopResponse() {
JsonBody jsonStop = new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile(CC_JSON_ROOT + "CC-Stop.json"));
JsonBody jsonStop = new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/" + CC_JSON_ROOT + "CC-Stop.json"));

server
.when(
Expand Down
33 changes: 0 additions & 33 deletions test/src/main/java/io/strimzi/test/ReadWriteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,13 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Class with various utility methods for reading and writing files and objects
Expand Down Expand Up @@ -289,30 +282,4 @@ public static File tempFile(String prefix, String suffix) {
file.deleteOnExit();
return file;
}

/**
* Get JSON content as string from resource file.
*
* TODO: Does the special handling here really matter? Can't we just use redFileFromResources?
*
* @param resourcePath Resource path.
*
* @return JSON content as string.
*/
public static String readSingleLineJsonStringFromResourceFile(String resourcePath) {
try {
URI resourceURI = Objects.requireNonNull(TestUtils.class.getClassLoader().getResource(resourcePath)).toURI();
try (Stream<String> lines = Files.lines(Paths.get(resourceURI), UTF_8)) {
Optional<String> content = lines.reduce((x, y) -> x + y);

if (content.isEmpty()) {
throw new IOException(format("File %s from resources was empty", resourcePath));
}

return content.get();
}
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void expectTopicConfigSuccessResponse(File apiUserFile, File apiPassFile)
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/topic-config-success.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/topic-config-success.json")))
.withHeader(Header.header("User-Task-ID", "8911ca89-351f-888-8d0f-9aade00e098h"))
.withDelay(TimeUnit.SECONDS, 0));

Expand All @@ -108,7 +108,7 @@ public void expectTopicConfigSuccessResponse(File apiUserFile, File apiPassFile)
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/topic-config-success.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/topic-config-success.json")))
.withHeader(Header.header("User-Task-ID", "8911ca89-351f-888-8d0f-9aade00e098h"))
.withDelay(TimeUnit.SECONDS, 0));

Expand All @@ -126,7 +126,7 @@ public void expectTopicConfigSuccessResponse(File apiUserFile, File apiPassFile)
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/topic-config-success.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/topic-config-success.json")))
.withHeader(Header.header("User-Task-ID", "8911ca89-351f-888-8d0f-9aade00e098h"))
.withDelay(TimeUnit.SECONDS, 0));

Expand All @@ -146,7 +146,7 @@ public void expectTopicConfigSuccessResponse(File apiUserFile, File apiPassFile)
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/topic-config-success.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/topic-config-success.json")))
.withHeader(Header.header("User-Task-ID", "8911ca89-351f-888-8d0f-9aade00e098h"))
.withDelay(TimeUnit.SECONDS, 0));
}
Expand All @@ -168,7 +168,7 @@ public void expectTopicConfigErrorResponse(File apiUserFile, File apiPassFile) {
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/topic-config-failure.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/topic-config-failure.json")))
.withHeader(Header.header("User-Task-ID", "8911ca89-351f-888-8d0f-9aade00e098h"))
.withDelay(TimeUnit.SECONDS, 0));
}
Expand Down Expand Up @@ -225,7 +225,7 @@ public void expectUserTasksSuccessResponse(File apiUserFile, File apiPassFile) {
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/user-tasks-success.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/user-tasks-success.json")))
.withDelay(TimeUnit.SECONDS, 0));

// encryption and authentication enabled
Expand All @@ -243,7 +243,7 @@ public void expectUserTasksSuccessResponse(File apiUserFile, File apiPassFile) {
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/user-tasks-success.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/user-tasks-success.json")))
.withDelay(TimeUnit.SECONDS, 0));

// encryption only
Expand All @@ -258,7 +258,7 @@ public void expectUserTasksSuccessResponse(File apiUserFile, File apiPassFile) {
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/user-tasks-success.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/user-tasks-success.json")))
.withDelay(TimeUnit.SECONDS, 0));

// authentication only
Expand All @@ -275,7 +275,7 @@ public void expectUserTasksSuccessResponse(File apiUserFile, File apiPassFile) {
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/user-tasks-success.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/user-tasks-success.json")))
.withDelay(TimeUnit.SECONDS, 0));
}

Expand All @@ -294,7 +294,7 @@ public void expectUserTasksErrorResponse(File apiUserFile, File apiPassFile) {
.respond(
HttpResponse.response()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR_500.code())
.withBody(new JsonBody(ReadWriteUtils.readSingleLineJsonStringFromResourceFile("cruisecontrol/user-tasks-failure.json")))
.withBody(new JsonBody(ReadWriteUtils.readFileFromResources(getClass(), "/cruisecontrol/user-tasks-failure.json")))
.withDelay(TimeUnit.SECONDS, 0));
}

Expand Down

0 comments on commit 3649beb

Please sign in to comment.