org.mockserver.model.JsonBody Java Examples

The following examples show how to use org.mockserver.model.JsonBody. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LowLatencyMontageClientTest.java    From render with GNU General Public License v2.0 6 votes vote down vote up
private void addAcqNextTileResponse(final AcquisitionTileList acquisitionTileList) {

        final JsonBody responseBody = json(acquisitionTileList.toJson());

        mockServer
                .when(
                        HttpRequest.request()
                                .withMethod("POST")
                                .withPath(getBaseAcquisitionPath() + "/next-tile"),
                        Times.once()
                )
                .respond(
                        HttpResponse.response()
                                .withStatusCode(HttpStatus.SC_OK)
                                .withHeader("Content-Type", responseBody.getContentType())
                                .withBody(responseBody)
        );
    }
 
Example #2
Source File: LowLatencyMontageClientTest.java    From render with GNU General Public License v2.0 6 votes vote down vote up
private void addRenderStackMetaDataResponse() {
    final String requestPath = getRenderStackRequestPath();
    final StackMetaData stackMetaData = new StackMetaData(acquireStackId, null);
    stackMetaData.setState(StackMetaData.StackState.LOADING);
    final JsonBody responseBody = json(stackMetaData.toJson());
    mockServer
            .when(
                    HttpRequest.request()
                            .withMethod("GET")
                            .withPath(requestPath),
                    Times.once()
            )
            .respond(
                    HttpResponse.response()
                            .withStatusCode(HttpStatus.SC_OK)
                            .withHeader("Content-Type", responseBody.getContentType())
                            .withBody(responseBody)
            );
}
 
Example #3
Source File: MockCruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
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))
                .respond(
                        response()
                                .withBody(jsonStop)
                                .withHeaders(header("User-Task-ID", "stopped"))
                                .withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));

    }
 
Example #4
Source File: MockCruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
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))
                .respond(
                        response()
                                .withBody(compWithErrorJson)
                                .withStatusCode(200)
                                .withHeaders(header("User-Task-ID", USER_TASK_REBALANCE_NO_GOALS_RESPONSE_UTID))
                                .withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    }
 
Example #5
Source File: MockCruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
public static void setupCCRebalanceNotEnoughDataError(ClientAndServer ccServer) throws IOException, URISyntaxException {

        // Rebalance response with no goal that returns an error
        JsonBody jsonError = getJsonFromResource("CC-Rebalance-NotEnoughValidWindows-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"))
                                .withPath(CruiseControlEndpoints.REBALANCE.path))

                .respond(
                        response()
                                .withStatusCode(500)
                                .withBody(jsonError)
                                .withHeaders(header(CruiseControlApi.CC_REST_API_USER_ID_HEADER, REBALANCE_NOT_ENOUGH_VALID_WINDOWS_ERROR_RESPONSE_UTID))
                                .withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    }
 
Example #6
Source File: IntegrationTestBase.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
void verifyEthNodeReceived(
    final Map<String, String> headers, final String proxyBodyRequest, final String path) {
  clientAndServer.verify(
      request()
          .withPath(path)
          .withBody(JsonBody.json(proxyBodyRequest))
          .withHeaders(convertHeadersToMockServerHeaders(headers)));
}
 
Example #7
Source File: PendingChecksFilterTests.java    From gerrit-code-review-plugin with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  UsernamePasswordCredentialsImpl c =
      new UsernamePasswordCredentialsImpl(
          CredentialsScope.GLOBAL, "cid", "cid", "USERNAME", "PASSWORD");
  CredentialsProvider.lookupStores(j.jenkins)
      .iterator()
      .next()
      .addCredentials(Domain.global(), c);

  g.getClient()
      .when(
          HttpRequest.request("/a/plugins/checks/checks.pending/")
              .withQueryStringParameters(query)
              .withMethod("GET"))
      .respond(
          HttpResponse.response()
              .withStatusCode(200)
              .withBody(JsonBody.json(pendingChecksInfos)));

  GerritSCMSource source =
      new GerritSCMSource(
          String.format(
              "https://%s:%s/a/test",
              g.getClient().remoteAddress().getHostName(),
              g.getClient().remoteAddress().getPort()));
  source.setInsecureHttps(true);
  source.setCredentialsId("cid");
  request = context.newRequest(source, new StreamTaskListener());
}
 
Example #8
Source File: GerritReviewStepTest.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void gerritReviewStepInvokeTest() throws Exception {
  int changeId = 4321;
  int revision = 1;
  String label = "Verfied";
  int score = -1;
  String message = "Does not work";
  String branch = String.format("%02d/%d/%d", changeId % 100, changeId, revision);

  UsernamePasswordCredentialsImpl c =
      new UsernamePasswordCredentialsImpl(
          CredentialsScope.GLOBAL, "cid", "cid", "USERNAME", "PASSWORD");
  CredentialsProvider.lookupStores(j.jenkins)
      .iterator()
      .next()
      .addCredentials(Domain.global(), c);
  WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
  p.setDefinition(
      new CpsFlowDefinition(
          String.format(
              ""
                  + "node {\n"
                  + "  withEnv([\n"
                  + "    'GERRIT_API_URL=https://%s:%s/a/project',\n"
                  + "    'GERRIT_API_INSECURE_HTTPS=true',\n"
                  + "    'GERRIT_CREDENTIALS_ID=cid',\n"
                  + "    'BRANCH_NAME=%s',\n"
                  + "  ]) {\n"
                  + "    gerritReview labels: ['%s': %s], message: '%s'\n"
                  + "  }\n"
                  + "}",
              g.getClient().remoteAddress().getHostString(),
              g.getClient().remoteAddress().getPort(),
              branch,
              label,
              score,
              message),
          true));

  ReviewInput reviewInput = new ReviewInputForObjectMapper().label(label, score).message(message);
  reviewInput.drafts = ReviewInput.DraftHandling.PUBLISH;
  reviewInput.tag = "autogenerated:jenkins";
  reviewInput.notify = NotifyHandling.OWNER;
  g.getClient()
      .when(
          HttpRequest.request(
                  String.format(
                      "/a/project/a/changes/%s/revisions/%s/review", changeId, revision))
              .withMethod("POST")
              .withBody(JsonBody.json(reviewInput)))
      .respond(
          HttpResponse.response()
              .withStatusCode(200)
              .withBody(JsonBody.json(Collections.emptyMap())));

  WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
  String log = JenkinsRule.getLog(run);

  g.getClient()
      .verify(
          HttpRequest.request(
              String.format("/a/project/a/changes/%s/revisions/%s/review", changeId, revision)),
          VerificationTimes.once());
}
 
Example #9
Source File: GerritReviewStepTest.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void gerritReviewStepInvokeLabelsTest() throws Exception {
  int changeId = 4321;
  int revision = 1;
  String label1 = "Verfied";
  int score1 = -1;
  String label2 = "CI-Review";
  int score2 = -1;
  String message = "Does not work";
  String branch = String.format("%02d/%d/%d", changeId % 100, changeId, revision);

  UsernamePasswordCredentialsImpl c =
      new UsernamePasswordCredentialsImpl(
          CredentialsScope.GLOBAL, "cid", "cid", "USERNAME", "PASSWORD");
  CredentialsProvider.lookupStores(j.jenkins)
      .iterator()
      .next()
      .addCredentials(Domain.global(), c);
  WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
  p.setDefinition(
      new CpsFlowDefinition(
          String.format(
              ""
                  + "node {\n"
                  + "  withEnv([\n"
                  + "    'GERRIT_API_URL=https://%s:%s/a/project',\n"
                  + "    'GERRIT_API_INSECURE_HTTPS=true',\n"
                  + "    'GERRIT_CREDENTIALS_ID=cid',\n"
                  + "    'BRANCH_NAME=%s',\n"
                  + "  ]) {\n"
                  + "    gerritReview labels: ['%s': %s, '%s': %s], message: '%s'\n"
                  + "  }\n"
                  + "}",
              g.getClient().remoteAddress().getHostString(),
              g.getClient().remoteAddress().getPort(),
              branch,
              label1,
              score1,
              label2,
              score2,
              message),
          true));

  ReviewInput reviewInput =
      new ReviewInputForObjectMapper()
          .label(label1, score1)
          .label(label2, score2)
          .message(message);
  reviewInput.drafts = ReviewInput.DraftHandling.PUBLISH;
  reviewInput.tag = "autogenerated:jenkins";
  reviewInput.notify = NotifyHandling.OWNER;
  g.getClient()
      .when(
          HttpRequest.request(
                  String.format(
                      "/a/project/a/changes/%s/revisions/%s/review", changeId, revision))
              .withMethod("POST")
              .withBody(JsonBody.json(reviewInput)))
      .respond(
          HttpResponse.response()
              .withStatusCode(200)
              .withBody(JsonBody.json(Collections.emptyMap())));

  WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
  String log = JenkinsRule.getLog(run);

  g.getClient()
      .verify(
          HttpRequest.request(
              String.format("/a/project/a/changes/%s/revisions/%s/review", changeId, revision)),
          VerificationTimes.once());
}
 
Example #10
Source File: MockCruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public static void setupCCStateResponse(ClientAndServer ccServer) throws IOException, URISyntaxException {

        // Non-verbose response
        JsonBody jsonProposalNotReady = getJsonFromResource("CC-State-proposal-not-ready.json");

        ccServer
                .when(
                        request()
                                .withMethod("GET")
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true|false"))
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.VERBOSE.key, "true|false"))
                                .withPath(CruiseControlEndpoints.STATE.path)
                                .withHeaders(header(CruiseControlApi.CC_REST_API_USER_ID_HEADER, STATE_PROPOSAL_NOT_READY)))
                .respond(
                        response()
                                .withBody(jsonProposalNotReady)
                                .withHeaders(header("User-Task-ID", STATE_PROPOSAL_NOT_READY_RESPONSE))
                                .withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));


        // Non-verbose response
        JsonBody json = getJsonFromResource("CC-State.json");

        ccServer
                .when(
                        request()
                                .withMethod("GET")
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true"))
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.VERBOSE.key, "false"))
                                .withPath(CruiseControlEndpoints.STATE.path))
                .respond(
                        response()
                                .withBody(json)
                                .withHeaders(header("User-Task-ID", "cruise-control-state"))
                                .withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));

        // Verbose response
        JsonBody jsonVerbose = getJsonFromResource("CC-State-verbose.json");

        ccServer
                .when(
                        request()
                                .withMethod("GET")
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.JSON.key, "true"))
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.VERBOSE.key, "true"))
                                .withPath(CruiseControlEndpoints.STATE.path))
                .respond(
                        response()
                                .withBody(jsonVerbose)
                                .withHeaders(header("User-Task-ID", "cruise-control-state-verbose"))
                                .withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));

    }
 
Example #11
Source File: GerritCheckStepTest.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void gerritCheckStepTestWithUrlSet() throws Exception {
  int changeId = 4321;
  int revision = 1;
  String checkerUuid = "checker";
  String checkStatus = "SUCCESSFUL";
  String url = "https://example.com/test";
  String branch = String.format("%02d/%d/%d", changeId % 100, changeId, revision);
  UsernamePasswordCredentialsImpl c =
      new UsernamePasswordCredentialsImpl(
          CredentialsScope.GLOBAL, "cid", "cid", "USERNAME", "PASSWORD");
  CredentialsProvider.lookupStores(j.jenkins)
      .iterator()
      .next()
      .addCredentials(Domain.global(), c);
  WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
  p.setDefinition(
      new CpsFlowDefinition(
          String.format(
              ""
                  + "node {\n"
                  + "  withEnv([\n"
                  + "    'GERRIT_API_URL=https://%s:%s/a/project',\n"
                  + "    'GERRIT_API_INSECURE_HTTPS=true',\n"
                  + "    'GERRIT_CREDENTIALS_ID=cid',\n"
                  + "    'BRANCH_NAME=%s',\n"
                  + "  ]) {\n"
                  + "    gerritCheck checks: [%s: '%s'], url: '%s'\n"
                  + "  }\n"
                  + "}",
              g.getClient().remoteAddress().getHostString(),
              g.getClient().remoteAddress().getPort(),
              branch,
              checkerUuid,
              checkStatus,
              url),
          true));

  String expectedUrl = String.format("/a/changes/%s/revisions/%s/checks/", changeId, revision);

  CheckInput checkInput = new CheckInputForObjectMapper();
  checkInput.checkerUuid = checkerUuid;
  checkInput.state = CheckState.valueOf(checkStatus);
  checkInput.url = url;
  g.getClient()
      .when(
          HttpRequest.request(expectedUrl).withMethod("POST").withBody(JsonBody.json(checkInput)))
      .respond(
          HttpResponse.response()
              .withStatusCode(200)
              .withBody(JsonBody.json(Collections.emptyMap())));

  WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
  String log = JenkinsRule.getLog(run);

  g.getClient().verify(HttpRequest.request(expectedUrl), VerificationTimes.once());
}
 
Example #12
Source File: MockCruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public static void setupCCRebalanceResponse(ClientAndServer ccServer, int pendingCalls, int responseDelay) throws IOException, URISyntaxException {

        // Rebalance in progress response with no goals set - non-verbose
        JsonBody pendingJson = getJsonFromResource("CC-Rebalance-no-goals-in-progress.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, "false"))
                                .withPath(CruiseControlEndpoints.REBALANCE.path),
                        Times.exactly(pendingCalls))
                .respond(
                        response()
                                .withBody(pendingJson)
                                .withHeaders(header("User-Task-ID", REBALANCE_NO_GOALS_RESPONSE_UTID))
                                .withStatusCode(202)
                                .withDelay(TimeUnit.SECONDS, responseDelay));

        // Rebalance response with no goals set - non-verbose
        JsonBody json = getJsonFromResource("CC-Rebalance-no-goals.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, "false"))
                                .withPath(CruiseControlEndpoints.REBALANCE.path),
                        Times.unlimited())
                .respond(
                        response()
                                .withBody(json)
                                .withHeaders(header("User-Task-ID", REBALANCE_NO_GOALS_RESPONSE_UTID))
                                .withDelay(TimeUnit.SECONDS, responseDelay));

        // Rebalance response with no goals set - verbose
        JsonBody jsonVerbose = 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"))
                                .withPath(CruiseControlEndpoints.REBALANCE.path))
                .respond(
                        response()
                                .withBody(jsonVerbose)
                                .withHeaders(header("User-Task-ID", REBALANCE_NO_GOALS_VERBOSE_RESPONSE_UTID))
                                .withDelay(TimeUnit.SECONDS, responseDelay));
    }
 
Example #13
Source File: MockCruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
/**
 * 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),
                    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),
                    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),
                    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 jsonInExecutionVerbose = getJsonFromResource("CC-User-task-rebalance-no-goals-verbose-inExecution.json");
    JsonBody jsonCompletedVerbose = getJsonFromResource("CC-User-task-rebalance-no-goals-verbose-completed.json");

    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),
                    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),
                    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));
}
 
Example #14
Source File: GerritCheckStepTest.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void gerritCheckStepInvokeTest() throws Exception {
  int changeId = 4321;
  int revision = 1;
  String checkerUuid = "checker";
  String checkStatus = "SUCCESSFUL";
  String message = "Does work";
  String branch = String.format("%02d/%d/%d", changeId % 100, changeId, revision);

  UsernamePasswordCredentialsImpl c =
      new UsernamePasswordCredentialsImpl(
          CredentialsScope.GLOBAL, "cid", "cid", "USERNAME", "PASSWORD");
  CredentialsProvider.lookupStores(j.jenkins)
      .iterator()
      .next()
      .addCredentials(Domain.global(), c);
  WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "q");
  p.setDefinition(
      new CpsFlowDefinition(
          String.format(
              ""
                  + "node {\n"
                  + "  withEnv([\n"
                  + "    'GERRIT_API_URL=https://%s:%s/a/project',\n"
                  + "    'GERRIT_API_INSECURE_HTTPS=true',\n"
                  + "    'GERRIT_CREDENTIALS_ID=cid',\n"
                  + "    'BRANCH_NAME=%s',\n"
                  + "  ]) {\n"
                  + "    gerritCheck checks: [%s: '%s'], message: '%s'\n"
                  + "  }\n"
                  + "}",
              g.getClient().remoteAddress().getHostString(),
              g.getClient().remoteAddress().getPort(),
              branch,
              checkerUuid,
              checkStatus,
              message),
          true));

  String expectedUrl = String.format("/a/changes/%s/revisions/%s/checks/", changeId, revision);

  CheckInput checkInput = new CheckInputForObjectMapper();
  checkInput.checkerUuid = checkerUuid;
  checkInput.state = CheckState.valueOf(checkStatus);
  checkInput.message = message;
  checkInput.url = j.getURL().toString() + p.getUrl() + "1/console";
  g.getClient()
      .when(
          HttpRequest.request(expectedUrl).withMethod("POST").withBody(JsonBody.json(checkInput)))
      .respond(
          HttpResponse.response()
              .withStatusCode(200)
              .withBody(JsonBody.json(Collections.emptyMap())));

  WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
  String log = JenkinsRule.getLog(run);

  g.getClient().verify(HttpRequest.request(expectedUrl), VerificationTimes.once());
}
 
Example #15
Source File: GerritCommentStepTest.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void gerritCommentStepInvokeTest() throws Exception {
  int changeId = 4321;
  int revision = 1;
  String path = "/path/to/file";
  int line = 1;
  String message = "Invalid spacing";
  String branch = String.format("%02d/%d/%d", changeId % 100, changeId, revision);

  UsernamePasswordCredentialsImpl c =
      new UsernamePasswordCredentialsImpl(
          CredentialsScope.GLOBAL, "cid", "cid", "USERNAME", "PASSWORD");
  CredentialsProvider.lookupStores(j.jenkins)
      .iterator()
      .next()
      .addCredentials(Domain.global(), c);
  WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
  p.setDefinition(
      new CpsFlowDefinition(
          String.format(
              ""
                  + "node {\n"
                  + "  withEnv([\n"
                  + "    'GERRIT_API_URL=https://%s:%s/a/project',\n"
                  + "    'GERRIT_API_INSECURE_HTTPS=true',\n"
                  + "    'GERRIT_CREDENTIALS_ID=cid',\n"
                  + "    'BRANCH_NAME=%s',\n"
                  + "  ]) {\n"
                  + "    gerritComment path: '%s', line: %s, message: '%s'\n"
                  + "  }\n"
                  + "}",
              g.getClient().remoteAddress().getHostString(),
              g.getClient().remoteAddress().getPort(),
              branch,
              path,
              line,
              message),
          true));

  DraftInput draftInput = new DraftInput();
  draftInput.path = path;
  draftInput.line = line;
  draftInput.message = message;
  g.getClient()
      .when(
          HttpRequest.request(
                  String.format(
                      "/a/project/a/changes/%s/revisions/%s/drafts", changeId, revision))
              .withMethod("PUT")
              .withBody(JsonBody.json(draftInput)))
      .respond(
          HttpResponse.response()
              .withStatusCode(200)
              .withBody(JsonBody.json(Collections.emptyMap())));

  WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
  String log = JenkinsRule.getLog(run);

  g.getClient()
      .verify(
          HttpRequest.request(
              String.format("/a/project/a/changes/%s/revisions/%s/drafts", changeId, revision)),
          VerificationTimes.once());
}
 
Example #16
Source File: IntegrationTestBase.java    From ethsigner with Apache License 2.0 4 votes vote down vote up
void verifyEthNodeReceived(final String proxyBodyRequest) {
  clientAndServer.verify(
      request()
          .withBody(JsonBody.json(proxyBodyRequest))
          .withHeaders(convertHeadersToMockServerHeaders(emptyMap())));
}
 
Example #17
Source File: MockCruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 3 votes vote down vote up
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()) {
            return new JsonBody(json.get());
        } else {
            throw new IOException("File " + resource + " from resources was empty");
        }

    }
 
Example #18
Source File: MockCruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 3 votes vote down vote up
public static void setupCCRebalanceBadGoalsError(ClientAndServer ccServer) 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, "false"))
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.GOALS.key, ".+"))
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.SKIP_HARD_GOAL_CHECK.key, "false"))
                                .withPath(CruiseControlEndpoints.REBALANCE.path))
                .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.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, "false"))
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.GOALS.key, ".+"))
                                .withQueryStringParameter(Parameter.param(CruiseControlParameters.SKIP_HARD_GOAL_CHECK.key, "true"))
                                .withPath(CruiseControlEndpoints.REBALANCE.path))
                .respond(
                        response()
                                .withBody(jsonSummary)
                                .withHeaders(header("User-Task-ID", REBALANCE_NO_GOALS_RESPONSE_UTID))
                                .withStatusCode(200)
                                .withDelay(TimeUnit.SECONDS, RESPONSE_DELAY_SEC));
    }