AddRestRequestToMockServiceAction extends AbstractSoapUIAction<RestRequest> { @Override public void perform(RestRequest restRequest, Object param) { String title = getName(); RestMockService mockService = null; WsdlProject project = restRequest.getOperation().getInterface().getProject(); while (mockService == null) { if (project.getRestMockServiceCount() > 0) { String option = promptForMockServiceSelection(title, project); boolean userCancelled = option == null; if (userCancelled) { return; } mockService = project.getRestMockServiceByName(option); } if (mockService == null) { mockService = createNewMockService(title, project); UISupport.showDesktopPanel(mockService); maybeStart(mockService); } } addRequestToMockService(restRequest, mockService); restRequest.getOperation().getService().addEndpoint(mockService.getLocalEndpoint()); Analytics.trackAction(ADD_REST_REQUEST_TO_REST_MOCK_SERVICE); } AddRestRequestToMockServiceAction(); @Override void perform(RestRequest restRequest, Object param); static final String SOAPUI_ACTION_ID; }
@Test public void shouldSaveRestMockWithSetNameToProject() { action.perform(restRequest, notUsed); List<RestMockService> serviceList = project.getRestMockServiceList(); assertThat(serviceList.size(), is(1)); RestMockService service = project.getRestMockServiceByName(mockServiceName); assertThat(service.getName(), is(mockServiceName)); }
@Test public void shouldSetAGoodNameOnTheRestMockAction() { action.perform(restRequest, notUsed); RestMockService service = project.getRestMockServiceByName(mockServiceName); assertThat(service.getMockOperationByName(requestPath), is(NOT_NULL)); }
@Test public void shouldFireProjectChangedEvent() { ProjectListenerAdapter listener = mock(ProjectListenerAdapter.class); project.addProjectListener(listener); action.perform(restRequest, notUsed); verify(listener, times(1)).mockServiceAdded(any(RestMockService.class)); }
@Test public void shouldAddASecondResponseToAnOperationForTheSamePath() throws SoapUIException { action.perform(restRequest, notUsed); action.perform(restRequest, notUsed); int mockResponseCount = getFirstMockOperation().getMockResponseCount(); assertThat(mockResponseCount, is(2)); }
@Test public void shouldCreateNewOperationForDifferentPath() throws SoapUIException { action.perform(restRequest, notUsed); restRequest.setPath("someotherpath"); action.perform(restRequest, notUsed); int mockResponseCount = getFirstMockOperation().getMockResponseCount(); assertThat(mockResponseCount, is(1)); assertThat(getFirstRestMockService().getMockOperationCount(), is(2)); }
@Test public void shouldCreateNewOperationForDifferentVerb() { action.perform(restRequest, notUsed); int mockOperationCount = getFirstRestMockService().getMockOperationCount(); assertThat(mockOperationCount, is(1)); restRequest.setMethod(RestRequestInterface.HttpMethod.TRACE); action.perform(restRequest, notUsed); mockOperationCount = getFirstRestMockService().getMockOperationCount(); assertThat(mockOperationCount, is(2)); }
@Test public void shouldSaveHeadersOnMockResponse() { action.perform(restRequest, notUsed); StringToStringsMap responseHeaders = getActualResponseHeaders(); assertThat(responseHeaders.get(ONE_HEADER).get(0), is("oneValue")); assertThat(responseHeaders.get(ANOTHER_HEADER).get(0), is("anotherValue")); }
@Test public void shouldNotSaveSomeHeaders() { String[] headersNotToSave = new String[]{HEADER_STATUS, HEADER_CONTENT_TYPE, HEADER_CONTENT_LENGTH}; action.perform(restRequest, notUsed); StringToStringsMap responseHeaders = getActualResponseHeaders(); for (String header : headersNotToSave) { assertThat(responseHeaders.get(header), is(NULL)); } }
@Test public void shouldAddEmptyResponses() { restRequest.setResponse(null, null); action.perform(restRequest, notUsed); assertThat(getFirstMockOperation().getMockResponseCount(), is(1)); }
@Test public void shouldExpandPathParameters() throws SoapUIException { RestService restService = (RestService) project.addNewInterface("a rest resource", RestServiceFactory.REST_TYPE); RestResource restResource = restService.addNewResource("resource", "http: RestMethod restMethod = restResource.addNewMethod("get"); RestRequest anotherRestRequest = createRestRequest(restMethod, "/template/{id}/path"); anotherRestRequest.setPropertyValue("id", "42"); action.perform(anotherRestRequest, notUsed); assertThat(getFirstMockOperation().getResourcePath(), is("/template/42/path")); }
@Test public void shouldExpandMultiplePathParameters() throws SoapUIException { RestService restService = (RestService) project.addNewInterface("a rest resource", RestServiceFactory.REST_TYPE); RestResource restResource = restService.addNewResource("resource", "http: RestMethod restMethod = restResource.addNewMethod("get"); RestRequest anotherRestRequest = createRestRequest(restMethod, "/template/{id}/path/{version}"); anotherRestRequest.setPropertyValue("id", "42"); anotherRestRequest.setPropertyValue("version", "3.1"); action.perform(anotherRestRequest, notUsed); assertThat(getFirstMockOperation().getResourcePath(), is("/template/42/path/3.1")); assertThat(getFirstMockOperation().getName(), is("/template/42/path/3.1")); }
@Test public void shouldAddEndPointToRestService() throws SoapUIException { int endPointCount = restRequest.getOperation().getService().getEndpoints().length; int expectedEndPointCount = endPointCount + 1; action.perform(restRequest, notUsed); assertThat(restRequest.getOperation().getService().getEndpoints().length, is(expectedEndPointCount)); }