@Test public void testAddOneBookWithValidation() { final String id = UUID.randomUUID().toString(); Response r = createWebClient("/rest/custom/bookstore/books").post(new Form().param("id", id)); assertEquals(Status.BAD_REQUEST.getStatusCode(), r.getStatus()); }
@Test public void testInvalidDueDateComperatorAsPost() { String invalidComparator = "bt"; Map<String, Object> conditionJson = new HashMap<String, Object>(); conditionJson.put("operator", invalidComparator); conditionJson.put("value", "2013-05-05T00:00:00"); List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>(); conditions.add(conditionJson); Map<String, Object> json = new HashMap<String, Object>(); json.put("dueDates", conditions); given() .contentType(POST_JSON_CONTENT_TYPE) .body(json) .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body("message", equalTo("Invalid due date comparator specified: " + invalidComparator)) .when() .post(JOBS_RESOURCE_URL); }
@Test public void testLifecycleUnknown() throws Exception { initializeOneAgent(); Request request = Request.Builder.preparePut() .setUri( coordinatorUriBuilder() .appendPath("/v1/slot/lifecycle") .addParameter("binary", "apple:*") .build()) .setBodyGenerator(createStaticBodyGenerator("unknown", UTF_8)) .build(); StatusResponse response = httpClient.execute(request, createStatusResponseHandler()); AgentStatus agentStatus = coordinator.getAgentByAgentId(agentId); SlotStatus apple1Status = agentStatus.getSlotStatus(apple1SotId); SlotStatus apple2Status = agentStatus.getSlotStatus(apple2SlotId); SlotStatus bananaStatus = agentStatus.getSlotStatus(bananaSlotId); assertEquals(response.getStatusCode(), Status.BAD_REQUEST.getStatusCode()); assertEquals(apple1Status.getState(), STOPPED); assertEquals(apple2Status.getState(), STOPPED); assertEquals(bananaStatus.getState(), STOPPED); }
@Test public void testInvalidDueDateAsPost() { Map<String, Object> conditionJson = new HashMap<String, Object>(); conditionJson.put("operator", "lt"); conditionJson.put("value", "invalidValue"); List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>(); conditions.add(conditionJson); Map<String, Object> json = new HashMap<String, Object>(); json.put("dueDates", conditions); given() .contentType(POST_JSON_CONTENT_TYPE) .body(json) .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body( "message", equalTo( "Invalid due date format: Cannot convert value \"invalidValue\" to java type java.util.Date")) .when() .post(JOBS_RESOURCE_URL); }
@Test public void testSortOrderParameterOnly() { given() .queryParam("sortOrder", "asc") .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .when() .get(CASE_DEFINITION_QUERY_URL); }
@Test public void updateWithAnInvalidResourceState() { try { putAt(aResourceURI(), "{\"uri\": \"http://toto.chez-les-papoos.com/invalid/resource\"}"); } catch (WebApplicationException ex) { int recievedStatus = ex.getResponse().getStatus(); int badRequest = Status.BAD_REQUEST.getStatusCode(); assertThat(recievedStatus, is(badRequest)); } }
/* * @todo need a better way to test this; see testGetEmptyBounds */ @Test(expected = UniformInterfaceException.class) @Category(UnitTest.class) public void testGetInvalidBoundsParam() throws Exception { try { execGet(String.valueOf(mapId), null, "-181,-90,180,90"); } catch (UniformInterfaceException e) { Assert.assertEquals(Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus()); Assert.assertTrue(e.getResponse().getEntity(String.class).contains("Invalid coordinate")); throw e; } }
@Test public void updateOfAResourceFromAnInvalidOne() { try { putAt(aResourceURI(), anInvalidResource()); fail("A user shouldn't update a resource with an invalid another one"); } catch (WebApplicationException ex) { int receivedStatus = ex.getResponse().getStatus(); int badRequest = Status.BAD_REQUEST.getStatusCode(); assertThat(receivedStatus, is(badRequest)); } }
@Test(expected = UniformInterfaceException.class) @Category(UnitTest.class) public void testGetReviewScoreMinParamOutOfRange() throws Exception { try { execGet(String.valueOf(mapId), "1.01", null); } catch (UniformInterfaceException e) { Assert.assertEquals(Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus()); Assert.assertTrue( e.getResponse().getEntity(String.class).contains("Invalid input parameter")); throw e; } }
@Test(expected = UniformInterfaceException.class) @Category(UnitTest.class) public void testGetMissingMapIdParam() throws Exception { try { execGet(null, null, null); } catch (UniformInterfaceException e) { Assert.assertEquals(Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus()); Assert.assertTrue( e.getResponse().getEntity(String.class).contains("Invalid input parameter")); throw e; } }
@Test(groups = "slow") public void testBadRequest() throws Exception { final CreditJson input = new CreditJson(null, null, null, null, null, null); final String jsonInput = mapper.writeValueAsString(input); // Try to create the credit final Response response = doPost( JaxrsResource.CREDITS_PATH, jsonInput, DEFAULT_EMPTY_QUERY, DEFAULT_HTTP_TIMEOUT_SEC); assertEquals( response.getStatusCode(), Status.BAD_REQUEST.getStatusCode(), response.getResponseBody()); }
public BadRequest(String message, Throwable cause) { super( cause, Response.status(Status.BAD_REQUEST.getStatusCode()) .entity(message) .type(MediaType.TEXT_PLAIN) .build()); if (cause != null) { LOGGER.warn("Bad request: " + cause.getMessage()); } else { LOGGER.warn("Bad request: " + message); } }
/* post(entity) /wishlists */ @Test public void testPostWithInvalidWishlistOwner() { Wishlist wishlist = new Wishlist(); wishlist.setId(UUID.randomUUID().toString()); wishlist.setOwner("Test"); instanceList.add(wishlist.getId()); final Response response = createWishlist(wishlist); Assert.assertNotNull("Response must not be null", response); Assert.assertEquals( "Should return bad request when wishlist owner does not exist", Status.BAD_REQUEST.getStatusCode(), response.getStatus()); }
@Test public void testSortOrderParameterOnly() { given() .queryParam("sortOrder", "asc") .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body( "message", equalTo("Only a single sorting parameter specified. sortBy and sortOrder required")) .when() .get(JOBS_RESOURCE_URL); }
@Test public void testSortByParameterOnly() { given() .queryParam("sortBy", "processDefinitionId") .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body( "message", containsString( "Only a single sorting parameter specified. sortBy and sortOrder required")) .when() .get(HISTORIC_JOB_LOG_RESOURCE_URL); }
@Test public void testMessagesTimersParameter() { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("messages", MockProvider.EXAMPLE_MESSAGES); parameters.put("timers", MockProvider.EXAMPLE_TIMERS); given() .queryParams(parameters) .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body( "message", equalTo("Parameter timers cannot be used together with parameter messages.")) .when() .get(JOBS_RESOURCE_URL); }
@Test public void testInvalidDueDate() { String variableValue = "invalidValue"; String invalidComparator = "lt"; String queryValue = invalidComparator + "_" + variableValue; given() .queryParam("dueDates", queryValue) .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body("message", equalTo("Invalid due date format: Invalid format: \"invalidValue\"")) .when() .get(JOBS_RESOURCE_URL); }
@Test public void testInvalidDueDateComparator() { String variableValue = "2013-05-05T00:00:00"; String invalidComparator = "bt"; String queryValue = invalidComparator + "_" + variableValue; given() .queryParam("dueDates", queryValue) .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body("message", equalTo("Invalid due date comparator specified: " + invalidComparator)) .when() .get(JOBS_RESOURCE_URL); }
@POST @Path("login") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response realizarLogin(String json) { Response response = null; try { Usuario usuario = new Usuario(); configurarUsuario(usuario, new JSONObject(json)); response = configurarResponse(usuario); } catch (ServiceException e) { response = CallBackUtil.setResponseError(Status.BAD_REQUEST.getStatusCode(), e.getMessage()); } catch (Exception e) { response = CallBackUtil.setResponseError( Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage()); } return response; }
@Test public void testInvalidNumericParameter() { String anInvalidIntegerQueryParam = "aString"; given() .queryParam("version", anInvalidIntegerQueryParam) .then() .expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .contentType(ContentType.JSON) .body("type", equalTo(InvalidRequestException.class.getSimpleName())) .body( "message", equalTo( "Cannot set query parameter 'version' to value 'aString': " + "Cannot convert value aString to java type java.lang.Integer")) .when() .get(CASE_DEFINITION_QUERY_URL); }
/** * This method is supposed to be used as exception mapper from <code>WebApplicationException * </code>, sent in REST response, to <code>AuxiliaryStorageException</code>. * * @param exception Exception to convert from. */ private void handleWebException(WebApplicationException exception) { Response response = exception.getResponse(); if (response == null) { throw new AuxiliaryStorageException("Mapping exception error: response is null"); } int responseStatus = response.getStatus(); if (Status.BAD_REQUEST.getStatusCode() == responseStatus) { throw new IllegalParameterException("Bad request server error"); } else if (Status.NOT_FOUND.getStatusCode() == responseStatus) { throw new ObjectNotFoundException("Object not found in auxiliary storage"); } else if (Status.CONFLICT.getStatusCode() == responseStatus) { throw new ObjectAlreadyExistsException("Object already exists in auxiliary storage"); } else if (Status.INTERNAL_SERVER_ERROR.getStatusCode() == responseStatus) { throw new AuxiliaryStorageException("Internal server error"); } else { throw new AuxiliaryStorageException("Unknown server error"); } }
@Test public void testGETCommandInvalidRequest() throws Exception { ResourceState initialState = new ResourceState("entity", "state", mockActions(), "/path"); // this test incorrectly supplies a resource as a result of the command. InteractionCommand mockCommand = new InteractionCommand() { public Result execute(InteractionContext ctx) { ctx.setResource(null); return Result.INVALID_REQUEST; } }; // create mock command controller NewCommandController mockCommandController = mockCommandController(mockCommand); // RIM with command controller that issues commands that always return SUCCESS HTTPHypermediaRIM rim = new HTTPHypermediaRIM( mockCommandController, new ResourceStateMachine(initialState), createMockMetadata()); Response response = rim.get(mock(HttpHeaders.class), "id", mockEmptyUriInfo()); assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); }