Search in sources :

Example 1 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project check-ins by objectcomputing.

the class EmployeeHoursControllerTest method testCreateEmployeeHoursWithNoTeamMemberId.

@Test
void testCreateEmployeeHoursWithNoTeamMemberId() {
    File file = new File("src/test/java/com/objectcomputing/checkins/services/employee_hours/test.csv");
    MultipartBody multipartBody = MultipartBody.builder().addPart("file", "test.csv", new MediaType("text/csv"), file).build();
    MemberProfile user = createAnUnrelatedUser();
    createAndAssignAdminRole(user);
    final HttpRequest<MultipartBody> request = HttpRequest.POST("/upload", multipartBody).basicAuth(user.getWorkEmail(), ADMIN_ROLE).contentType(MediaType.MULTIPART_FORM_DATA);
    HttpClientResponseException responseException = assertThrows(HttpClientResponseException.class, () -> client.toBlocking().exchange(request, Map.class));
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseException.getStatus());
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) MediaType(io.micronaut.http.MediaType) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project check-ins by objectcomputing.

the class EmployeeHoursControllerTest method testCreateEmployeeHours.

@Test
void testCreateEmployeeHours() {
    File file = new File("src/test/java/com/objectcomputing/checkins/services/employee_hours/test.csv");
    MultipartBody multipartBody = MultipartBody.builder().addPart("file", "test.csv", new MediaType("text/csv"), file).build();
    MemberProfile memberProfile = createADefaultMemberProfile();
    createADefaultMemberProfileForPdl(memberProfile);
    MemberProfile user = createAnUnrelatedUser();
    createAndAssignAdminRole(user);
    final HttpRequest<MultipartBody> request = HttpRequest.POST("/upload", multipartBody).basicAuth(user.getWorkEmail(), ADMIN_ROLE).contentType(MediaType.MULTIPART_FORM_DATA);
    final HttpResponse<EmployeeHoursResponseDTO> response = client.toBlocking().exchange(request, EmployeeHoursResponseDTO.class);
    EmployeeHoursResponseDTO employeeHoursResponseDTO = response.body();
    assertEquals(employeeHoursResponseDTO, response.body());
    assertEquals(HttpStatus.OK, response.getStatus());
}
Also used : MultipartBody(io.micronaut.http.client.multipart.MultipartBody) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) MediaType(io.micronaut.http.MediaType) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class UploadControllerSpec method testCompletedFileUploadNoNameWithBytes.

@Test
public void testCompletedFileUploadNoNameWithBytes() {
    MultipartBody body = MultipartBody.builder().addPart("file", "", MediaType.APPLICATION_JSON_TYPE, "{\"title\":\"Foo\"}".getBytes()).build();
    Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/upload/completed", body).contentType(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
    HttpClientResponseException ex = Assertions.assertThrows(HttpClientResponseException.class, () -> flowable.blockFirst());
    Map<String, Object> embedded = (Map<String, Object>) ex.getResponse().getBody(Map.class).get().get("_embedded");
    Object message = ((Map<String, Object>) ((List) embedded.get("errors")).get(0)).get("message");
    assertEquals("Required argument [CompletedFileUpload file] not specified", message);
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Map(java.util.Map) Test(org.junit.Test)

Example 4 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class UploadControllerSpec method testCompletedFileUpload.

@Test
public void testCompletedFileUpload() {
    MultipartBody body = MultipartBody.builder().addPart("file", "file.json", MediaType.APPLICATION_JSON_TYPE, "{\"title\":\"Foo\"}".getBytes()).build();
    Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/upload/completed", body).contentType(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
    HttpResponse<String> response = flowable.blockFirst();
    assertEquals(HttpStatus.OK.getCode(), response.code());
    assertEquals("Uploaded", response.getBody().get());
}
Also used : MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.Test)

Example 5 with MultipartBody

use of io.micronaut.http.client.multipart.MultipartBody in project micronaut-core by micronaut-projects.

the class UploadControllerSpec method testFileBytesUpload.

@Test
public void testFileBytesUpload() {
    MultipartBody body = MultipartBody.builder().addPart("file", "file.json", MediaType.TEXT_PLAIN_TYPE, "some data".getBytes()).addPart("fileName", "bar").build();
    Flux<HttpResponse<String>> flowable = Flux.from(client.exchange(HttpRequest.POST("/upload/bytes", body).contentType(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_PLAIN_TYPE), String.class));
    HttpResponse<String> response = flowable.blockFirst();
    assertEquals(HttpStatus.OK.getCode(), response.code());
    assertEquals("Uploaded", response.getBody().get());
}
Also used : MultipartBody(io.micronaut.http.client.multipart.MultipartBody) HttpResponse(io.micronaut.http.HttpResponse) Test(org.junit.Test)

Aggregations

MultipartBody (io.micronaut.http.client.multipart.MultipartBody)15 HttpResponse (io.micronaut.http.HttpResponse)10 Test (org.junit.Test)10 MediaType (io.micronaut.http.MediaType)5 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)5 File (java.io.File)5 Map (java.util.Map)3 Test (org.junit.jupiter.api.Test)3 MemberProfile (com.objectcomputing.checkins.services.memberprofile.MemberProfile)2 EmployeeHours (com.objectcomputing.checkins.services.employee_hours.EmployeeHours)1 EmployeeHoursServices (com.objectcomputing.checkins.services.employee_hours.EmployeeHoursServices)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 MultipartDataFactory (io.micronaut.http.client.multipart.MultipartDataFactory)1 NettyClientSslBuilder (io.micronaut.http.client.netty.ssl.NettyClientSslBuilder)1 MultipartException (io.micronaut.http.multipart.MultipartException)1 NettyHttpRequestBuilder (io.micronaut.http.netty.NettyHttpRequestBuilder)1 NettyHttpResponseBuilder (io.micronaut.http.netty.NettyHttpResponseBuilder)1 UriBuilder (io.micronaut.http.uri.UriBuilder)1 ByteBuf (io.netty.buffer.ByteBuf)1