org.mockserver.model.Parameter Java Examples
The following examples show how to use
org.mockserver.model.Parameter.
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: MockCruiseControl.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
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 #2
Source File: MockCruiseControl.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
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 #3
Source File: MockCruiseControl.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
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: PullRequestServiceExTests.java From HubTurbo with GNU Lesser General Public License v3.0 | 5 votes |
/** * Tests that getReviewComments method correctly receives 1 page of review comments * returned from a MockServer that emulates GitHub API service. */ @Test public void testGetReviewComments() throws IOException { MockServerClient mockServer = ClientAndServer.startClientAndServer(8888); String sampleComments = TestUtils.readFileFromResource(this, "tests/ReviewCommentsSample.json"); mockServer.when( request() .withPath(TestUtils.API_PREFIX + "/repos/hubturbo/hubturbo/pulls/1125/comments") .withQueryStringParameters( new Parameter("per_page", "100"), new Parameter("page", "1") ) ).respond(response().withBody(sampleComments)); GitHubClient client = new GitHubClient("localhost", 8888, "http"); PullRequestServiceEx service = new PullRequestServiceEx(client); Type listOfComments = new TypeToken<List<ReviewComment>>() { }.getType(); List<ReviewComment> expectedComments = new Gson().fromJson(sampleComments, listOfComments); List<ReviewComment> actualComments = service.getReviewComments( RepositoryId.createFromId("hubturbo/hubturbo"), 1125); assertEquals(expectedComments.size(), actualComments.size()); Comparator<ReviewComment> comparator = (a, b) -> (int) (a.getId() - b.getId()); Collections.sort(expectedComments, comparator); Collections.sort(actualComments, comparator); for (int i = 0; i < expectedComments.size(); i++) { assertEquals(expectedComments.get(i).getId(), actualComments.get(i).getId()); } mockServer.stop(); }
Example #5
Source File: MockServerImpl.java From Insights with Apache License 2.0 | 4 votes |
/** * This function will read all json files from getAllMockRequestsFromJson method * and create expectations from the given Paths and Response in the individual * tool's json. */ public void startMockServerWithExpectations() { // Starting mock server on port 1080. MockServerClient mockServer = startClientAndServer(1080); // Reading mock requests from multiple JSON files. List<MockRequest> mockRequests = getAllMockRequestsFromJson(); String response = null; Iterator<MockRequest> mockIterator = mockRequests.iterator(); while (mockIterator.hasNext()) { MockRequest request = mockIterator.next(); List<Parameter> parameterList = new ArrayList<>(); if(request.isResponseJson()) { response = request.getResponse().get(0).toString(); } else { response = request.getResponse().toString(); } // checking if the request has any parameters. if(request.getParameters() != null) { for (Map.Entry<String,String> paramEntry : request.getParameters().entrySet()) { Parameter param = new Parameter(paramEntry.getKey(), paramEntry.getValue()); parameterList.add(param); } } // Creating mock Expectation mockServer .when( request() .withPath(request.getPath()) .withQueryStringParameters(parameterList) ) .respond( response() .withStatusCode(200) .withBody(response) ); log.debug("Expectation created for the path {} with query string parmeters {}",request.getPath(),parameterList); } log.info("Mock Server Started"); }
Example #6
Source File: MockCruiseControl.java From strimzi-kafka-operator with Apache License 2.0 | 4 votes |
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 #7
Source File: MockCruiseControl.java From strimzi-kafka-operator with Apache License 2.0 | 4 votes |
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 #8
Source File: MockCruiseControl.java From strimzi-kafka-operator with Apache License 2.0 | 4 votes |
/** * 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 #9
Source File: MockCruiseControl.java From strimzi-kafka-operator with Apache License 2.0 | 3 votes |
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)); }