@Test public void whenJacksonException_thenNotInternalServerError_NoErrorsLogged() { mapper= new GeneralExceptionMapper(mockLogger, true); assertThat(mapper.toResponse(new MyLocalException()).getStatus(), is((Status.INTERNAL_SERVER_ERROR.getStatusCode()))); verify(mockLogger, times(0)).error(any(String.class), any(Object[].class)); verify(mockLogger, times(0)).error(any(String.class), any(Throwable.class), any(Object[].class)); }
/** * Get the status of the error response. * * @return Response status code. */ public Response.Status getStatus() { return Response.Status.fromStatusCode(super.getResponse().getStatus()); }
private void setResponseForInvalidUri(final HttpServletResponse response, final Throwable throwable) throws IOException { LOGGER.log(Level.FINER, "Error while processing request.", throwable); final Response.Status badRequest = Response.Status.BAD_REQUEST; if (webComponent.configSetStatusOverSendError) { response.reset(); //noinspection deprecation response.setStatus(badRequest.getStatusCode(), badRequest.getReasonPhrase()); } else { response.sendError(badRequest.getStatusCode(), badRequest.getReasonPhrase()); } }
@Test public void testCreateIncident() { when(runtimeServiceMock.createIncident(anyString(), anyString(), anyString(), anyString())).thenReturn(mock(Incident.class)); Map<String, Object> json = new HashMap<String, Object>(); json.put("incidentType", "incidentType"); json.put("configuration", "configuration"); json.put("message", "message"); given().pathParam("id", MockProvider.EXAMPLE_EXECUTION_ID).contentType(ContentType.JSON).body(json).then().expect().statusCode(Status.OK.getStatusCode()) .when().post(CREATE_INCIDENT_URL); verify(runtimeServiceMock).createIncident("incidentType", MockProvider.EXAMPLE_EXECUTION_ID, "configuration", "message"); }
@Test public void testGetNonExistingProcessInstance() { HistoricProcessInstanceQuery sampleInstanceQuery = mock(HistoricProcessInstanceQuery.class); when(historyServiceMock.createHistoricProcessInstanceQuery()).thenReturn(sampleInstanceQuery); when(sampleInstanceQuery.processInstanceId(anyString())).thenReturn(sampleInstanceQuery); when(sampleInstanceQuery.singleResult()).thenReturn(null); given().pathParam("id", "aNonExistingInstanceId") .then().expect().statusCode(Status.NOT_FOUND.getStatusCode()).contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body("message", equalTo("Historic process instance with id aNonExistingInstanceId does not exist")) .when().get(HISTORIC_SINGLE_PROCESS_INSTANCE_URL); }
@Test public void testEmptySingleResult() { when(filterServiceMock.singleResult(anyString(), any(Query.class))).thenReturn(null); given() .header(ACCEPT_JSON_HEADER) .pathParam("id", EXAMPLE_FILTER_ID) .then().expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .get(EXECUTE_SINGLE_RESULT_FILTER_URL); verify(filterServiceMock).singleResult(eq(EXAMPLE_FILTER_ID), isNull(Query.class)); }
@Test public void testGetVariablesForNonExistingCaseInstance() { when(caseServiceMock.getVariablesTyped(anyString(), eq(true))).thenThrow(new ProcessEngineException("expected exception")); given().pathParam("id", "aNonExistingCaseInstanceId") .then().expect().statusCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()).contentType(ContentType.JSON) .body("type", equalTo(ProcessEngineException.class.getSimpleName())) .body("message", equalTo("expected exception")) .when().get(CASE_INSTANCE_VARIABLES_URL); verify(caseServiceMock).getVariablesTyped("aNonExistingCaseInstanceId", true); }
@Test public void testNonExistingVariable() { String variableKey = "aVariableKey"; when(caseServiceMock.getVariableTyped(eq(MockProvider.EXAMPLE_CASE_INSTANCE_ID), eq(variableKey), eq(true))).thenReturn(null); given().pathParam("id", MockProvider.EXAMPLE_CASE_INSTANCE_ID).pathParam("varId", variableKey) .then().expect().statusCode(Status.NOT_FOUND.getStatusCode()) .body("type", is(InvalidRequestException.class.getSimpleName())) .body("message", is("case execution variable with name " + variableKey + " does not exist")) .when().get(SINGLE_CASE_INSTANCE_VARIABLE_URL); }
@Test public void testCannotDownloadVariableOtherThanFile() { String variableKey = "aVariableKey"; BooleanValue variableValue = Variables.booleanValue(true); when(caseServiceMock.getVariableLocalTyped(eq(EXAMPLE_TASK_ID), eq(variableKey), anyBoolean())).thenReturn(variableValue); given() .pathParam("id", EXAMPLE_TASK_ID) .pathParam("varId", variableKey) .then().expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .when().get(SINGLE_CASE_INSTANCE_BINARY_VARIABLE_URL); }
@Test public void testGetVariableForNonExistingInstance() { String variableKey = "aVariableKey"; when(caseServiceMock.getVariableTyped(eq(MockProvider.EXAMPLE_CASE_INSTANCE_ID), eq(variableKey), eq(true))) .thenThrow(new ProcessEngineException("expected exception")); given().pathParam("id", MockProvider.EXAMPLE_CASE_INSTANCE_ID).pathParam("varId", variableKey) .then().expect().statusCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()) .body("type", is(RestException.class.getSimpleName())) .body("message", is("Cannot get case execution variable " + variableKey + ": expected exception")) .when().get(SINGLE_CASE_INSTANCE_VARIABLE_URL); }
@Test public void whenRuntimeException_thenInternalServerError() { assertThat(mapper.toResponse(mock(RuntimeException.class)).getStatus(), is(Status.INTERNAL_SERVER_ERROR.getStatusCode())); }
@Test public void testGetStartFormDataForNonExistingProcessDefinition() { when(formServiceMock.getStartFormData(anyString())).thenThrow(new ProcessEngineException("expected exception")); given().pathParam("id", "aNonExistingProcessDefinitionId") .then().expect() .statusCode(Status.BAD_REQUEST.getStatusCode()).contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body("message", containsString("Cannot get start form data for process definition")) .when().get(START_FORM_URL); }
@Test public void testGetNonExistingForm() { when(formServiceMock.getTaskFormData(anyString())).thenThrow(new ProcessEngineException("Expected exception: task does not exist.")); given().pathParam("id", NON_EXISTING_ID) .header("accept", MediaType.APPLICATION_JSON) .then().expect().statusCode(Status.BAD_REQUEST.getStatusCode()).contentType(ContentType.JSON) .body("type", equalTo(RestException.class.getSimpleName())) .body("message", equalTo("Cannot get form for task " + NON_EXISTING_ID)) .when().get(TASK_FORM_URL); }
@Test @InSequence(0) public void shouldNotBeAbleToLocateAttachmentRoot() throws Exception { given(). then(). contentType(BASE_MEDIA_TYPE). statusCode(Status.OK.getStatusCode()). root("root"). body("link.find {it.@rel == 'attachment'}.size()", equalTo(0)). when(). get(new URL(base, "api/").toExternalForm()). body(); }
@Override public void perform() { given(). then(). statusCode(Status.OK.getStatusCode()). header("Allow", allOf( not(containsString("POST")), not(containsString("PUT")), not(containsString("DELETE")), not(containsString("PATCH")))). when(). options(testURL.toExternalForm()); } }).inspect(new SetupAuth(null));
@Test @InSequence(4) public void verifyUpdatedUser() throws Exception { assertNotNull("Previous step failed", uri_userInstance); UserType conf = getUpdateUser(); given(). then(). contentType(USER_MEDIA_TYPE). statusCode(Status.OK.getStatusCode()). root("user"). body("name", equalTo(conf.getName())). body("bio", equalTo(conf.getBio())). body("handle", not(equalTo(conf.getHandle()))). // handle is the id, can not be updated when(). get(uri_userInstance); }
@Test public void shouldProvideBookmarkLinkToResource() throws Exception { given(). contentType(TEST_MEDIA_TYPE). then(). contentType(TEST_MEDIA_TYPE). statusCode(Response.Status.OK.getStatusCode()). body("test.link.find {it.@rel == 'bookmark'}.size()", equalTo(1)). body("test.link.@href", equalTo(new URL(baseURL, "api/bookmark/test/200").toExternalForm())). when(). get(baseURL + "api/test/200"); }
@Test public void badChildCollectionId() { given().when().get("/user/1/oops/1").then().statusCode(Status.NOT_FOUND.getStatusCode()); } }
@Override public void perform() { given(). then(). statusCode(Status.UNAUTHORIZED.getStatusCode()). when(). get(whoAmIURL.toExternalForm()); } }).inspect(new SetupAuth(null));
@Test @InSequence(3) public void shouldBeAbleToUpdateUser() throws Exception { assertNotNull("Previous step failed", uri_userInstance); UserType conf = getUpdateUser(); given(). contentType(USER_MEDIA_TYPE). body(conf). then(). statusCode(Status.NO_CONTENT.getStatusCode()). when(). put(uri_userInstance); }