Search in sources :

Example 6 with JsonBody

use of org.mockserver.model.JsonBody in project strimzi by strimzi.

the class MockCruiseControl method getJsonFromResource.

private static JsonBody getJsonFromResource(String resource) throws URISyntaxException, IOException {
    URI jsonURI = Objects.requireNonNull(MockCruiseControl.class.getClassLoader().getResource(CC_JSON_ROOT + resource)).toURI();
    Optional<String> json = Files.lines(Paths.get(jsonURI), UTF_8).reduce((x, y) -> x + y);
    if (!json.isPresent()) {
        throw new IOException("File " + resource + " from resources was empty");
    }
    return new JsonBody(json.get());
}
Also used : JsonBody(org.mockserver.model.JsonBody) IOException(java.io.IOException) URI(java.net.URI)

Example 7 with JsonBody

use of org.mockserver.model.JsonBody in project strimzi by strimzi.

the class MockCruiseControl method setupCCUserTasksResponseNoGoals.

/**
 * Sets up the User Tasks endpoint. These endpoints expect the query to contain the user-task-id returned in the header of the response from
 * the rebalance endpoints.
 *
 * @param ccServer The ClientAndServer instance that this endpoint will be added too.
 * @param activeCalls The number of calls to the User Tasks endpoint that should return "Active" before "inExecution" is returned as the status.
 * @param inExecutionCalls The number of calls to the User Tasks endpoint that should return "InExecution" before "Completed" is returned as the status.
 * @throws IOException If there are issues connecting to the network port.
 * @throws URISyntaxException If any of the configured end points are invalid.
 */
public static void setupCCUserTasksResponseNoGoals(ClientAndServer ccServer, int activeCalls, int inExecutionCalls) throws IOException, URISyntaxException {
    // User tasks response for the rebalance request with no goals set (non-verbose)
    JsonBody jsonActive = getJsonFromResource("CC-User-task-rebalance-no-goals-Active.json");
    JsonBody jsonInExecution = getJsonFromResource("CC-User-task-rebalance-no-goals-inExecution.json");
    JsonBody jsonCompleted = getJsonFromResource("CC-User-task-rebalance-no-goals-completed.json");
    // The first activeCalls times respond that with a status of "Active"
    ccServer.when(request().withMethod("GET").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.FETCH_COMPLETE.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.USER_TASK_IDS.key, REBALANCE_NO_GOALS_RESPONSE_UTID)).withPath(CruiseControlEndpoints.USER_TASKS.path).withHeader(AUTH_HEADER).withSecure(true), Times.exactly(activeCalls)).respond(response().withBody(jsonActive).withHeaders(header("User-Task-ID", USER_TASK_REBALANCE_NO_GOALS_RESPONSE_UTID)).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    // The next inExecutionCalls times respond that with a status of "InExecution"
    ccServer.when(request().withMethod("GET").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.FETCH_COMPLETE.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.USER_TASK_IDS.key, REBALANCE_NO_GOALS_RESPONSE_UTID)).withPath(CruiseControlEndpoints.USER_TASKS.path).withHeader(AUTH_HEADER).withSecure(true), Times.exactly(inExecutionCalls)).respond(response().withBody(jsonInExecution).withHeaders(header("User-Task-ID", USER_TASK_REBALANCE_NO_GOALS_RESPONSE_UTID)).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    // On the N+1 call, respond with a completed rebalance task.
    ccServer.when(request().withMethod("GET").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.FETCH_COMPLETE.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.USER_TASK_IDS.key, REBALANCE_NO_GOALS_RESPONSE_UTID)).withPath(CruiseControlEndpoints.USER_TASKS.path).withHeader(AUTH_HEADER).withSecure(true), Times.unlimited()).respond(response().withBody(jsonCompleted).withHeaders(header("User-Task-ID", USER_TASK_REBALANCE_NO_GOALS_RESPONSE_UTID)).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    // User tasks response for the rebalance request with no goals set (verbose)
    JsonBody jsonActiveVerbose = getJsonFromResource("CC-User-task-rebalance-no-goals-verbose-Active.json");
    JsonBody jsonInExecutionVerbose = getJsonFromResource("CC-User-task-rebalance-no-goals-verbose-inExecution.json");
    JsonBody jsonCompletedVerbose = getJsonFromResource("CC-User-task-rebalance-no-goals-verbose-completed.json");
    // The first activeCalls times respond that with a status of "Active"
    ccServer.when(request().withMethod("GET").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.FETCH_COMPLETE.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.USER_TASK_IDS.key, REBALANCE_NO_GOALS_VERBOSE_RESPONSE_UTID)).withPath(CruiseControlEndpoints.USER_TASKS.path).withHeader(AUTH_HEADER).withSecure(true), Times.exactly(activeCalls)).respond(response().withBody(jsonActiveVerbose).withHeaders(header("User-Task-ID", USER_TASK_REBALANCE_NO_GOALS_VERBOSE_RESPONSE_UTID)).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    ccServer.when(request().withMethod("GET").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.FETCH_COMPLETE.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.USER_TASK_IDS.key, REBALANCE_NO_GOALS_VERBOSE_RESPONSE_UTID)).withPath(CruiseControlEndpoints.USER_TASKS.path).withHeader(AUTH_HEADER).withSecure(true), Times.exactly(inExecutionCalls)).respond(response().withBody(jsonInExecutionVerbose).withHeaders(header("User-Task-ID", USER_TASK_REBALANCE_NO_GOALS_VERBOSE_RESPONSE_UTID)).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    ccServer.when(request().withMethod("GET").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.FETCH_COMPLETE.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.USER_TASK_IDS.key, REBALANCE_NO_GOALS_VERBOSE_RESPONSE_UTID)).withPath(CruiseControlEndpoints.USER_TASKS.path).withHeader(AUTH_HEADER).withSecure(true), Times.unlimited()).respond(response().withBody(jsonCompletedVerbose).withHeaders(header("User-Task-ID", USER_TASK_REBALANCE_NO_GOALS_VERBOSE_RESPONSE_UTID)).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
}
Also used : JsonBody(org.mockserver.model.JsonBody)

Example 8 with JsonBody

use of org.mockserver.model.JsonBody in project strimzi by strimzi.

the class MockCruiseControl method setupCCStopResponse.

/**
 * Setup response of task being stopped.
 */
public static void setupCCStopResponse(ClientAndServer ccServer) throws IOException, URISyntaxException {
    JsonBody jsonStop = getJsonFromResource("CC-Stop.json");
    ccServer.when(request().withMethod("POST").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true|false")).withPath(CruiseControlEndpoints.STOP.path).withHeader(AUTH_HEADER).withSecure(true)).respond(response().withBody(jsonStop).withHeaders(header("User-Task-ID", "stopped")).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
}
Also used : JsonBody(org.mockserver.model.JsonBody)

Example 9 with JsonBody

use of org.mockserver.model.JsonBody in project strimzi by strimzi.

the class MockCruiseControl method setupCCRebalanceBadGoalsError.

/**
 * Setup responses for various bad goal configurations possible on a rebalance request.
 */
public static void setupCCRebalanceBadGoalsError(ClientAndServer ccServer, CruiseControlEndpoints endpoint) throws IOException, URISyntaxException {
    // Response if the user has set custom goals which do not include all configured hard.goals
    JsonBody jsonError = getJsonFromResource("CC-Rebalance-bad-goals-error.json");
    ccServer.when(request().withMethod("POST").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.DRY_RUN.key, "true|false")).withQueryStringParameter(Parameter.param(CruiseControlParameters.VERBOSE.key, "true|false")).withQueryStringParameter(Parameter.param(CruiseControlParameters.GOALS.key, ".+")).withQueryStringParameter(Parameter.param(CruiseControlParameters.SKIP_HARD_GOAL_CHECK.key, "false")).withPath(endpoint.path).withHeader(AUTH_HEADER).withSecure(true)).respond(response().withBody(jsonError).withHeaders(header("User-Task-ID", REBALANCE_NO_GOALS_RESPONSE_UTID)).withStatusCode(500).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    // 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 = getJsonFromResource("CC-Rebalance-no-goals-verbose.json");
    ccServer.when(request().withMethod("POST").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.DRY_RUN.key, "true|false")).withQueryStringParameter(Parameter.param(CruiseControlParameters.VERBOSE.key, "true|false")).withQueryStringParameter(Parameter.param(CruiseControlParameters.GOALS.key, ".+")).withQueryStringParameter(Parameter.param(CruiseControlParameters.SKIP_HARD_GOAL_CHECK.key, "true")).withPath(endpoint.path).withHeader(AUTH_HEADER).withSecure(true)).respond(response().withBody(jsonSummary).withHeaders(header("User-Task-ID", REBALANCE_NO_GOALS_RESPONSE_UTID)).withStatusCode(200).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
}
Also used : JsonBody(org.mockserver.model.JsonBody)

Example 10 with JsonBody

use of org.mockserver.model.JsonBody in project strimzi by strimzi.

the class MockCruiseControl method setupCCUserTasksCompletedWithError.

/**
 * Setup response of task completed with error.
 */
public static void setupCCUserTasksCompletedWithError(ClientAndServer ccServer) throws IOException, URISyntaxException {
    // This simulates asking for the status of a task that has Complete with error and fetch_completed_task=true
    JsonBody compWithErrorJson = getJsonFromResource("CC-User-task-status-completed-with-error.json");
    ccServer.when(request().withMethod("GET").withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true")).withQueryStringParameter(Parameter.param(CruiseControlParameters.FETCH_COMPLETE.key, "true")).withPath(CruiseControlEndpoints.USER_TASKS.path).withHeader(AUTH_HEADER).withSecure(true)).respond(response().withBody(compWithErrorJson).withStatusCode(200).withHeaders(header("User-Task-ID", USER_TASK_REBALANCE_NO_GOALS_RESPONSE_UTID)).withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
}
Also used : JsonBody(org.mockserver.model.JsonBody)

Aggregations

JsonBody (org.mockserver.model.JsonBody)37 Test (org.junit.Test)12 IOException (java.io.IOException)2 URI (java.net.URI)2 FullTrigger (com.redhat.cloud.policies.app.model.engine.FullTrigger)1 Trigger (com.redhat.cloud.policies.app.model.engine.Trigger)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 RegexBody (org.mockserver.model.RegexBody)1