Search in sources :

Example 1 with RecordedMultipartRequest

use of com.auth0.net.multipart.RecordedMultipartRequest in project auth0-java by auth0.

the class JobsEntityTest method shouldRequestUsersImport.

@Test
public void shouldRequestUsersImport() throws Exception {
    File usersFile = new File(MGMT_JOB_POST_USERS_IMPORTS_INPUT);
    Request<Job> request = api.jobs().importUsers("con_123456789", usersFile, null);
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_JOB_POST_USERS_IMPORTS, 200);
    Job response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/jobs/users-imports"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    String ctHeader = recordedRequest.getHeader("Content-Type");
    assertThat(ctHeader, startsWith("multipart/form-data"));
    String[] ctParts = ctHeader.split("multipart/form-data; boundary=");
    RecordedMultipartRequest recordedMultipartRequest = new RecordedMultipartRequest(recordedRequest);
    assertThat(recordedMultipartRequest.getPartsCount(), is(2));
    assertThat(recordedMultipartRequest.getBoundary(), is(notNullValue()));
    assertThat(recordedMultipartRequest.getBoundary(), is(ctParts[1]));
    // Connection ID
    KeyValuePart formParam = recordedMultipartRequest.getKeyValuePart("connection_id");
    assertThat(formParam, is(notNullValue()));
    assertThat(formParam.getValue(), is("con_123456789"));
    // Users JSON
    FilePart jsonFile = recordedMultipartRequest.getFilePart("users");
    assertThat(jsonFile, is(notNullValue()));
    String utf8Contents = new String(Files.readAllBytes(usersFile.toPath()));
    assertThat(jsonFile.getContentType(), is("text/json"));
    assertThat(jsonFile.getFilename(), is("job_post_users_imports_input.json"));
    assertThat(jsonFile.getValue(), is(utf8Contents));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) Job(com.auth0.json.mgmt.jobs.Job) File(java.io.File) KeyValuePart(com.auth0.net.multipart.KeyValuePart) FilePart(com.auth0.net.multipart.FilePart) Test(org.junit.Test)

Example 2 with RecordedMultipartRequest

use of com.auth0.net.multipart.RecordedMultipartRequest in project auth0-java by auth0.

the class MultipartRequestTest method shouldAddMultipleParts.

@Test
public void shouldAddMultipleParts() throws Exception {
    String boundary = UUID.randomUUID().toString();
    MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(boundary);
    MultipartRequest<TokenHolder> request = new MultipartRequest<>(client, server.getBaseUrl(), "POST", new ObjectMapper(), tokenHolderType, bodyBuilder);
    File fileValue = new File(MULTIPART_SAMPLE);
    request.addPart("keyName", "keyValue");
    request.addPart("jsonFile", fileValue, "text/json");
    server.jsonResponse(AUTH_TOKENS, 200);
    request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    RecordedMultipartRequest recordedMultipartRequest = new RecordedMultipartRequest(recordedRequest);
    assertThat(recordedMultipartRequest.getPartsCount(), is(2));
    assertThat(recordedMultipartRequest.getBoundary(), is(boundary));
    KeyValuePart formParam = recordedMultipartRequest.getKeyValuePart("keyName");
    assertThat(formParam, is(notNullValue()));
    assertThat(formParam.getValue(), is("keyValue"));
    FilePart jsonFile = recordedMultipartRequest.getFilePart("jsonFile");
    assertThat(jsonFile, is(notNullValue()));
    String utf8Contents = new String(Files.readAllBytes(fileValue.toPath()));
    assertThat(jsonFile.getContentType(), is("text/json"));
    assertThat(jsonFile.getFilename(), is("multipart_sample.json"));
    assertThat(jsonFile.getValue(), is(utf8Contents));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TokenHolder(com.auth0.json.auth.TokenHolder) KeyValuePart(com.auth0.net.multipart.KeyValuePart) FilePart(com.auth0.net.multipart.FilePart) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) MultipartBody(okhttp3.MultipartBody) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with RecordedMultipartRequest

use of com.auth0.net.multipart.RecordedMultipartRequest in project auth0-java by auth0.

the class JobsEntityTest method shouldRequestUsersImportWithOptions.

@Test
public void shouldRequestUsersImportWithOptions() throws Exception {
    UsersImportOptions options = new UsersImportOptions();
    options.withExternalId("ext_id123");
    options.withUpsert(true);
    options.withSendCompletionEmail(false);
    File usersFile = new File(MGMT_JOB_POST_USERS_IMPORTS_INPUT);
    Request<Job> request = api.jobs().importUsers("con_123456789", usersFile, options);
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_JOB_POST_USERS_IMPORTS, 200);
    Job response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/jobs/users-imports"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    String ctHeader = recordedRequest.getHeader("Content-Type");
    assertThat(ctHeader, startsWith("multipart/form-data"));
    String[] ctParts = ctHeader.split("multipart/form-data; boundary=");
    RecordedMultipartRequest recordedMultipartRequest = new RecordedMultipartRequest(recordedRequest);
    assertThat(recordedMultipartRequest.getPartsCount(), is(5));
    assertThat(recordedMultipartRequest.getBoundary(), is(notNullValue()));
    assertThat(recordedMultipartRequest.getBoundary(), is(ctParts[1]));
    // Connection ID
    KeyValuePart connectionIdParam = recordedMultipartRequest.getKeyValuePart("connection_id");
    assertThat(connectionIdParam, is(notNullValue()));
    assertThat(connectionIdParam.getValue(), is("con_123456789"));
    // External ID
    KeyValuePart externalIdParam = recordedMultipartRequest.getKeyValuePart("external_id");
    assertThat(externalIdParam, is(notNullValue()));
    assertThat(externalIdParam.getValue(), is("ext_id123"));
    // Upsert
    KeyValuePart upsertParam = recordedMultipartRequest.getKeyValuePart("upsert");
    assertThat(upsertParam, is(notNullValue()));
    assertThat(upsertParam.getValue(), is("true"));
    // Send Completion Email
    KeyValuePart sendCompletionEmailParam = recordedMultipartRequest.getKeyValuePart("send_completion_email");
    assertThat(sendCompletionEmailParam, is(notNullValue()));
    assertThat(sendCompletionEmailParam.getValue(), is("false"));
    // Users JSON
    FilePart jsonFile = recordedMultipartRequest.getFilePart("users");
    assertThat(jsonFile, is(notNullValue()));
    String utf8Contents = new String(Files.readAllBytes(usersFile.toPath()));
    assertThat(jsonFile.getContentType(), is("text/json"));
    assertThat(jsonFile.getFilename(), is("job_post_users_imports_input.json"));
    assertThat(jsonFile.getValue(), is(utf8Contents));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) UsersImportOptions(com.auth0.client.mgmt.filter.UsersImportOptions) Job(com.auth0.json.mgmt.jobs.Job) File(java.io.File) KeyValuePart(com.auth0.net.multipart.KeyValuePart) FilePart(com.auth0.net.multipart.FilePart) Test(org.junit.Test)

Aggregations

FilePart (com.auth0.net.multipart.FilePart)3 KeyValuePart (com.auth0.net.multipart.KeyValuePart)3 RecordedMultipartRequest (com.auth0.net.multipart.RecordedMultipartRequest)3 File (java.io.File)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 Job (com.auth0.json.mgmt.jobs.Job)2 Test (org.junit.Test)2 UsersImportOptions (com.auth0.client.mgmt.filter.UsersImportOptions)1 TokenHolder (com.auth0.json.auth.TokenHolder)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 MultipartBody (okhttp3.MultipartBody)1