APISonar


io.restassured.http.ContentType.JSON

> io > restassured > http > ContentType > JSON
io APIs restassured APIs http APIs ContentType APIs JSON APIs

Example 1
public void testRestartProcessInstanceWithNullInstructionsSync() {
    doThrow(new BadUserRequestException("instructions is null")).when(builderMock).execute();
    
    HashMap<String, Object> json = new HashMap<String, Object>();
    json.put("processInstanceIds", "processInstanceId");
    
    given()
      .pathParam("id", MockProvider.EXAMPLE_PROCESS_DEFINITION_ID)
      .contentType(ContentType.JSON)
      .body(json)
    .then().expect()
      .statusCode(Status.BAD_REQUEST.getStatusCode())
    .when()
      .post(RESTART_PROCESS_INSTANCE_URL);
  }
Example 2
public void testCreateIncident() {
    when(runtimeServiceMock.createIncident(anyString(), anyString(), anyString(), anyString())).thenReturn(mock(Incident.class));
    Map<String, Object> json = new HashMap<>();
    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");
  }
Example 3
public void testEvaluateDecision_NotValid() {
    String message = "expected message";
    when(decisionEvaluationBuilderMock.evaluate()).thenThrow(new NotValidException(message));

    Map<String, Object> json = new HashMap<String, Object>();
    json.put("variables", Collections.emptyMap());

    given().pathParam("id", MockProvider.EXAMPLE_DECISION_DEFINITION_ID)
      .contentType(POST_JSON_CONTENT_TYPE).body(json)
      .then().expect()
        .statusCode(Status.BAD_REQUEST.getStatusCode()).contentType(ContentType.JSON)
        .body("type", is(InvalidRequestException.class.getSimpleName()))
        .body("message", containsString(message))
    .when().post(EVALUATE_DECISION_URL);
  }
Example 4
@Test public void
    can_upload_json_from_file() throws IOException {
        // Given
        File file = folder.newFile("my.json");
        FileUtils.writeStringToFile(file, "{ \"message\" : \"hello world\"}");

        // When
        given().
                contentType(ContentType.JSON).
                body(file).
        when().
                post("/jsonBody").
        then().
                body(equalTo("hello world"));
    }
Example 5
public void testSetJobPriorityToExtremeValue() {
    Map<String, Object> priorityJson = new HashMap<String, Object>();
    priorityJson.put("priority", Long.MAX_VALUE);

    given()
      .pathParam("id", MockProvider.EXAMPLE_JOB_ID)
      .contentType(ContentType.JSON)
      .body(priorityJson)
    .then().expect()
      .statusCode(Status.NO_CONTENT.getStatusCode())
    .when().put(JOB_RESOURCE_SET_PRIORITY_URL);

    verify(mockManagementService).setJobPriority(MockProvider.EXAMPLE_JOB_ID, Long.MAX_VALUE);
  }
Example 6
public void requestSpecificationAllowsSpecifyingJsonBodyAsInputStreamForPost() {
        InputStream inputStream = getClass().getResourceAsStream("/message.json");

        given().body(inputStream).with().contentType(ContentType.JSON).then().expect().body(equalTo("hello world")).when().post("/jsonBody");
    }
Example 7
public void post_test() {
		Response response = given().
				contentType(ContentType.JSON)
				.accept(ContentType.JSON)
				.body("{\"name\": \"Lisa\",\"salary\": \"2000\"}")
				.when()
				.post(ROOT_URI + "/create");
		System.out.println("POST Response\n" + response.asString());
		// tests
		response.then().body("id", Matchers.any(Integer.class));
		response.then().body("name", Matchers.is("Lisa"));
	}
Example 8
public void testSetJobRetries() {
    Map<String, Object> retriesVariableJson = new HashMap<String, Object>();
    retriesVariableJson.put("retries", MockProvider.EXAMPLE_JOB_RETRIES);

    given().pathParam("id", MockProvider.EXAMPLE_JOB_ID).contentType(ContentType.JSON).body(retriesVariableJson).then().expect()
    .statusCode(Status.NO_CONTENT.getStatusCode())
    .when().put(JOB_RESOURCE_SET_RETRIES_URL);

    verify(mockManagementService).setJobRetries(MockProvider.EXAMPLE_JOB_ID, MockProvider.EXAMPLE_JOB_RETRIES);
  }
Example 9
public void testSetJobDuedateNullCascade() {
    Map<String, Object> duedateVariableJson = new HashMap<String, Object>();
    duedateVariableJson.put("duedate", null);
    duedateVariableJson.put("cascade", true);
    
    given().pathParam("id", MockProvider.EXAMPLE_JOB_ID).contentType(ContentType.JSON).body(duedateVariableJson).then().expect()
    .statusCode(Status.NO_CONTENT.getStatusCode())
    .when().put(JOB_RESOURCE_SET_DUEDATE_URL);
    
    verify(mockManagementService).setJobDuedate(MockProvider.EXAMPLE_JOB_ID, null, true);
  }
Example 10
@Test public void
    adds_default_charset_to_content_type_by_default() {
        final AtomicReference<String> contentType = new AtomicReference<>();

        RestAssuredMockMvc.given().
                standaloneSetup(new GreetingController()).
                contentType(ContentType.JSON).
                interceptor(requestBuilder -> contentType.set(extractContentType(requestBuilder))).
        when().
                get("/greeting?name={name}", "Johan").
        then().
               statusCode(200);

        assertThat(contentType.get()).isEqualTo("application/json;charset=" + RestAssuredMockMvc.config().getEncoderConfig().defaultContentCharset());
    }
Example 11
public void testSetRetriesByProcessWithBadRequestQuery() {
    doThrow(new BadUserRequestException("job ids are empty"))
        .when(mockManagementService).setJobRetriesAsync(eq((List<String>) null), eq((ProcessInstanceQuery) null), anyInt());

    Map<String, Object> messageBodyJson = new HashMap<String, Object>();
    messageBodyJson.put(RETRIES, 5);

    given()
        .contentType(ContentType.JSON).body(messageBodyJson)
        .then().expect()
        .statusCode(Status.BAD_REQUEST.getStatusCode())
        .when().post(SET_JOB_RETRIES_ASYNC_URL);
  }
Example 12
public void testPutSingleLocalVariableWithNoValue() {
    String variableKey = "aVariableKey";

    given().pathParam("id", EXAMPLE_TASK_ID).pathParam("varId", variableKey)
      .contentType(ContentType.JSON).body(EMPTY_JSON_OBJECT)
      .header("accept", MediaType.APPLICATION_JSON)
      .then().expect().statusCode(Status.NO_CONTENT.getStatusCode())
      .when().put(SINGLE_TASK_PUT_SINGLE_VARIABLE_URL);

    verify(taskServiceMock).setVariableLocal(eq(EXAMPLE_TASK_ID), eq(variableKey),
        argThat(EqualsNullValue.matcher()));
  }
Example 13
public void testGetTaskAttachments() {
    Response response = given().pathParam("id", MockProvider.EXAMPLE_TASK_ID)
      .header("accept", MediaType.APPLICATION_JSON)
      .then().expect()
        .statusCode(Status.OK.getStatusCode()).contentType(ContentType.JSON)
        .body("$.size()", equalTo(1))
      .when().get(SINGLE_TASK_ATTACHMENTS_URL);

    verifyTaskAttachments(mockTaskAttachments, response);
    verify(taskServiceMock).getTaskAttachments(MockProvider.EXAMPLE_TASK_ID);
  }
Example 14
public void testGetIdentityLinksByType() {
    Map<String, Object> expectedGroupIdentityLink = toExpectedJsonMap(mockCandidateGroupIdentityLink);
    Map<String, Object> expectedGroupIdentityLink2 = toExpectedJsonMap(mockCandidateGroup2IdentityLink);

    given().pathParam("id", EXAMPLE_TASK_ID).queryParam("type", IdentityLinkType.CANDIDATE)
      .header("accept", MediaType.APPLICATION_JSON)
      .then().expect()
        .statusCode(Status.OK.getStatusCode()).contentType(ContentType.JSON)
        .body("$.size()", equalTo(2))
        .body("$", hasItem(expectedGroupIdentityLink))
        .body("$", hasItem(expectedGroupIdentityLink2))
      .when().get(TASK_IDENTITY_LINKS_URL);

    verify(taskServiceMock).getIdentityLinksForTask(EXAMPLE_TASK_ID);
  }
Example 15
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);
  }
Example 16
public void testMismatchingCorrelation() {
    String messageName = "aMessage";

    doThrow(new MismatchingMessageCorrelationException(messageName, "Expected exception: cannot correlate"))
      .when(messageCorrelationBuilderMock).correlateWithResult();

    Map<String, Object> messageParameters = new HashMap<>();
    messageParameters.put("messageName", messageName);
    given().contentType(POST_JSON_CONTENT_TYPE).body(messageParameters)
      .then().expect().statusCode(Status.BAD_REQUEST.getStatusCode()).contentType(ContentType.JSON)
      .body("type", equalTo(RestException.class.getSimpleName()))
      .body("message", containsString("Expected exception: cannot correlate"))
      .when().post(MESSAGE_URL);
  }
Example 17
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(DECISION_REQUIREMENTS_DEFINITION_QUERY_URL);
  }
Example 18
public void testPutSingleVariableWithUnparseableShort() {
    String variableKey = "aVariableKey";
    String variableValue = "1abc";
    String type = "Short";

    Map<String, Object> variableJson = VariablesBuilder.getVariableValueMap(variableValue, type);

    given().pathParam("id", MockProvider.EXAMPLE_CASE_INSTANCE_ID).pathParam("varId", variableKey)
      .contentType(ContentType.JSON).body(variableJson)
      .then().expect().statusCode(Status.BAD_REQUEST.getStatusCode())
      .body("type", equalTo(InvalidRequestException.class.getSimpleName()))
      .body("message", equalTo("Cannot put case execution variable aVariableKey: "
          + ErrorMessageHelper.getExpectedFailingConversionMessage(variableValue, type, Short.class)))
      .when().put(SINGLE_CASE_INSTANCE_VARIABLE_URL);
  }
Example 19
protected void executeAndVerifyFailingSorting(String sortBy, String sortOrder, Status expectedStatus, String expectedErrorType, String expectedErrorMessage) {
    given().queryParam("sortBy", sortBy).queryParam("sortOrder", sortOrder)
      .then().expect().statusCode(expectedStatus.getStatusCode()).contentType(ContentType.JSON)
      .body("type", equalTo(expectedErrorType))
      .body("message", equalTo(expectedErrorMessage))
      .when().get(DEPLOYMENT_QUERY_URL);
  }
Example 20
public void verifyUser () {
    when(processEngine.getIdentityService()
        .checkPassword(TEST_USERNAME, TEST_PASSWORD))
    .thenReturn(true);

    BasicUserCredentialsDto userCredentialsDto = new BasicUserCredentialsDto();
    userCredentialsDto.setUsername(TEST_USERNAME);
    userCredentialsDto.setUsername(TEST_PASSWORD);
    given()
        .body(userCredentialsDto).contentType(ContentType.JSON).
    expect().statusCode(Status.BAD_REQUEST.getStatusCode()).contentType(ContentType.JSON)
        .body("type", equalTo(InvalidRequestException.class.getSimpleName()))
        .body("message", equalTo("Username and password are required"))
        .when().post(VERIFY_USER_URL);
  }
Example 21
public void testGetReport() {
    given()
    .then().expect()
      .statusCode(Status.OK.getStatusCode())
      .contentType(ContentType.JSON)
    .when().get(HISTORIC_REPORT_URL);

    InOrder inOrder = Mockito.inOrder(historicCaseInstanceReport);
    inOrder.verify(historicCaseInstanceReport).list();
    verifyNoMoreInteractions(historicCaseInstanceReport);
  }
Example 22
public void testSetRetriesByProcessAsyncHistoricQueryBasedWithBadRequestQuery() {
    doThrow(new BadUserRequestException("jobIds is empty"))
      .when(mockManagementService).setJobRetriesAsync(eq(new ArrayList<String>()), eq((ProcessInstanceQuery) null), anyInt());

    SetJobRetriesByProcessDto body = new SetJobRetriesByProcessDto();
    body.setRetries(MockProvider.EXAMPLE_JOB_RETRIES);

    given()
      .contentType(ContentType.JSON).body(body)
    .then().expect()
      .statusCode(Status.BAD_REQUEST.getStatusCode())
    .when().post(SET_JOB_RETRIES_ASYNC_HIST_QUERY_URL);
  }
Example 23
public void testMessageEventTriggeringWithoutVariables() {
    String messageName = "aMessageName";

    given().pathParam("id", MockProvider.EXAMPLE_EXECUTION_ID).pathParam("messageName", messageName)
      .contentType(ContentType.JSON).body(EMPTY_JSON_OBJECT)
      .then().expect().statusCode(Status.NO_CONTENT.getStatusCode())
      .when().post(TRIGGER_MESSAGE_SUBSCRIPTION_URL);

    verify(runtimeServiceMock).messageEventReceived(eq(messageName), eq(MockProvider.EXAMPLE_EXECUTION_ID),
        argThat(new EqualsMap(null)));
  }
Example 24
public void testGroupCreateExistingFails() {
    Group newGroup = MockProvider.createMockGroup();
    when(identityServiceMock.newGroup(MockProvider.EXAMPLE_GROUP_ID)).thenReturn(newGroup);
    doThrow(new ProcessEngineException("")).when(identityServiceMock).saveGroup(newGroup);

    given().body(GroupDto.fromGroup(newGroup)).contentType(ContentType.JSON)
      .then().expect().statusCode(Status.INTERNAL_SERVER_ERROR.getStatusCode()).contentType(ContentType.JSON)
      .body("type", equalTo(ProcessEngineException.class.getSimpleName()))
      .when().post(GROUP_CREATE_URL);

    verify(identityServiceMock).newGroup(MockProvider.EXAMPLE_GROUP_ID);
    verify(identityServiceMock).saveGroup(newGroup);
  }
Example 25
@Test public void
    content_type_is_overwritten_when_defined_in_specification() {
        // Given
        MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().setContentType(ContentType.JSON).build();

        // When
        MockMvcRequestSpecification spec = RestAssuredMockMvc.given().contentType(ContentType.XML).spec(specToMerge);

        // Then
        Assertions.assertThat(implOf(spec).getRequestContentType()).isEqualTo(ContentType.JSON.toString());
    }