RestMockAction extends AbstractMockOperation<RESTMockActionConfig, RestMockResponse> implements HasHelpUrl { public RestMockResult dispatchRequest(RestMockRequest request) throws DispatchException { if (getMockResponseCount() == 0) { throw new DispatchException("Missing MockResponse(s) in MockOperation [" + getName() + "]"); } try { RestMockResult result = new RestMockResult(request); MockResponse mockResponse = getDispatcher().selectMockResponse(request, result); result.setMockResponse(mockResponse); result.setMockOperation(this); if (mockResponse == null) { mockResponse = getMockResponseByName(this.getDefaultResponse()); } if (mockResponse == null) { throw new DispatchException("Failed to find MockResponse"); } result.setMockResponse(mockResponse); mockResponse.execute(request, result); return result; } catch (Exception e) { throw new DispatchException(e); } } RestMockAction(RestMockService mockService, RESTMockActionConfig config);  RestMockAction(RestMockService mockService, RESTMockActionConfig config, RestRequest request); static String getIconName(RESTMockActionConfig methodConfig); static String getDefaultIcon(); @Override RestMockService getMockService(); @Override void removeResponseFromConfig(int index); @Override Operation getOperation(); @Override String getScriptHelpUrl(); @Override void propertyChange(PropertyChangeEvent evt); @Override List<? extends ModelItem> getChildren(); RestMockResponse addNewMockResponse(String name); RestMockResult dispatchRequest(RestMockRequest request); String getResourcePath(); void setMethod(RestRequestInterface.HttpMethod method); RestRequestInterface.HttpMethod getMethod(); void setResourcePath(String path); void setResource(RestResource resource); @Override void setExampleScript(); String getHelpUrl();  }
@Test public void testDispatchRequestReturnsHttpStatus() throws Exception { mockResponse.setResponseHttpStatus(HttpStatus.SC_BAD_REQUEST); RestMockResult mockResult = mockAction.dispatchRequest(restMockRequest); verify(mockResult.getMockRequest().getHttpResponse()).setStatus(HttpStatus.SC_BAD_REQUEST); assertThat(mockResult.getMockResponse().getResponseHttpStatus(), is(HttpStatus.SC_BAD_REQUEST)); }
@Test public void testDispatchRequestReturnsResponseContent() throws Exception { String responseContent = "response content"; mockResponse.setResponseContent(responseContent); RestMockResult mockResult = mockAction.dispatchRequest(restMockRequest); assertThat(mockResult.getMockResponse().getResponseContent(), is(responseContent)); }
@Test public void testDispatchRequestReturnsHttpHeader() throws Exception { StringToStringsMap responseHeaders = mockResponse.getResponseHeaders(); String headerKey = "awesomekey"; String headerValue = "awesomevalue"; responseHeaders.add(headerKey, headerValue); mockResponse.setResponseHeaders(responseHeaders); RestMockResult mockResult = mockAction.dispatchRequest(restMockRequest); verify(mockResult.getMockRequest().getHttpResponse()).addHeader(headerKey, headerValue); assertThat(mockResult.getResponseHeaders().get(headerKey, ""), is(headerValue)); assertThat(mockResult.getMockResponse().getResponseHeaders().get(headerKey, ""), is(headerValue)); }
@Test public void testScriptIsExecuted() throws Exception { String mockServiceName = "RenamedFromScript"; mockResponse.setName("MockResponse"); mockResponse.setScript("mockResponse.setName('" + mockServiceName + "')"); RestMockResult mockResult = mockAction.dispatchRequest(restMockRequest); assertThat(mockResult.getMockResponse().getName(), is(mockServiceName)); }
@Test public void testResponsesAreDispatchedSequentiallyForSingleResponse() throws Exception { RestMockResult mockResult; mockResult = mockAction.dispatchRequest(restMockRequest); assertThat(mockResult.getMockResponse().getName(), is("response 1")); mockResult = mockAction.dispatchRequest(restMockRequest); assertThat(mockResult.getMockResponse().getName(), is("response 1")); }