-
Notifications
You must be signed in to change notification settings - Fork 74
Added changes to have multipart sync & async call using actual server #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
neenapj
wants to merge
2
commits into
microprofile:main
Choose a base branch
from
neenapj:403-test-multipart
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
...rc/main/java/org/eclipse/microprofile/rest/client/tck/asynctests/AsyncEntityPartTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * Copyright 2025 Contributors to the Eclipse Foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| * implied. | ||
| * | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.eclipse.microprofile.rest.client.tck.asynctests; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
| import org.jboss.arquillian.container.test.api.Deployment; | ||
| import org.jboss.arquillian.container.test.api.RunAsClient; | ||
| import org.jboss.arquillian.test.api.ArquillianResource; | ||
| import org.jboss.arquillian.testng.Arquillian; | ||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
| import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import jakarta.json.Json; | ||
| import jakarta.json.JsonArray; | ||
| import jakarta.json.JsonArrayBuilder; | ||
| import jakarta.json.JsonObject; | ||
| import jakarta.json.JsonObjectBuilder; | ||
| import jakarta.ws.rs.ApplicationPath; | ||
| import jakarta.ws.rs.BadRequestException; | ||
| import jakarta.ws.rs.Consumes; | ||
| import jakarta.ws.rs.POST; | ||
| import jakarta.ws.rs.Path; | ||
| import jakarta.ws.rs.Produces; | ||
| import jakarta.ws.rs.core.EntityPart; | ||
| import jakarta.ws.rs.core.MediaType; | ||
| import jakarta.ws.rs.core.Response; | ||
|
|
||
| /** | ||
| * @author <a href="mailto:[email protected]">Neena Jacob</a> | ||
| */ | ||
| @RunAsClient | ||
| public class AsyncEntityPartTest extends Arquillian { | ||
|
|
||
| @ArquillianResource | ||
| private URI uri; | ||
|
|
||
| @Deployment | ||
| public static WebArchive createDeployment() { | ||
| return ShrinkWrap.create(WebArchive.class, EntityPart.class.getSimpleName() + ".war") | ||
| .addClasses(FileUploadResource.class, FileUploadApplication.class) | ||
| .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); | ||
| } | ||
|
|
||
| @ApplicationPath("/") | ||
| public static class FileUploadApplication extends jakarta.ws.rs.core.Application { | ||
| } | ||
|
|
||
| @Path("/entitypart") | ||
| public static class FileUploadResource { | ||
|
|
||
| @POST | ||
| @Path("upload") | ||
| @Consumes(MediaType.MULTIPART_FORM_DATA) | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| public Response uploadFile(List<EntityPart> entityParts) throws IOException { | ||
| final JsonArrayBuilder jsonBuilder = Json.createArrayBuilder(); | ||
| for (EntityPart part : entityParts) { | ||
| final JsonObjectBuilder jsonPartBuilder = Json.createObjectBuilder(); | ||
| jsonPartBuilder.add("name", part.getName()); | ||
| if (part.getFileName().isPresent()) { | ||
| jsonPartBuilder.add("fileName", part.getFileName().get()); | ||
| } else { | ||
| throw new BadRequestException("No file name for entity part " + part); | ||
| } | ||
| jsonPartBuilder.add("content", part.getContent(String.class)); | ||
| jsonBuilder.add(jsonPartBuilder); | ||
| } | ||
| return Response.status(201).entity(jsonBuilder.build()).build(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tests that a single file is upload. The response is a simple JSON response with the file information. | ||
| * | ||
| * @throws Exception | ||
| * if a test error occurs | ||
| */ | ||
| @Test | ||
| public void uploadFileAsync() throws Exception { | ||
| try (AsyncFileManagerClient client = createClient()) { | ||
| final byte[] content; | ||
| try (InputStream in = AsyncEntityPartTest.class.getResourceAsStream("/multipart/test-file1.txt")) { | ||
| Assert.assertNotNull(in, "Could not find /multipart/test-file1.txt"); | ||
| content = in.readAllBytes(); | ||
| } | ||
| // Send in an InputStream to ensure it works with an InputStream | ||
| final List<EntityPart> files = List.of(EntityPart.withFileName("test-file1.txt") | ||
| .content(new ByteArrayInputStream(content)) | ||
| .mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE) | ||
| .build()); | ||
|
|
||
| CompletionStage<Response> futureResponse = client.uploadFileAsync(files); | ||
| Response response = futureResponse.toCompletableFuture().get(); | ||
|
|
||
| try { | ||
| Assert.assertEquals(201, response.getStatus()); | ||
| final JsonArray jsonArray = response.readEntity(JsonArray.class); | ||
| Assert.assertNotNull(jsonArray); | ||
| Assert.assertEquals(jsonArray.size(), 1); | ||
| final JsonObject json = jsonArray.getJsonObject(0); | ||
| Assert.assertEquals(json.getString("name"), "test-file1.txt"); | ||
| Assert.assertEquals(json.getString("fileName"), "test-file1.txt"); | ||
| Assert.assertEquals(json.getString("content"), "This is a test file for file 1.\n"); | ||
| } finally { | ||
| response.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private AsyncFileManagerClient createClient() { | ||
| try { | ||
| return RestClientBuilder.newBuilder() | ||
| .baseUri(createCombinedUri(uri, "entitypart")) | ||
| .build(AsyncFileManagerClient.class); | ||
| } catch (URISyntaxException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private static URI createCombinedUri(final URI uri, final String path) throws URISyntaxException { | ||
| if (path == null || path.isEmpty()) { | ||
| return uri; | ||
| } | ||
| String uriString = uri.toString(); | ||
| final StringBuilder builder = new StringBuilder(uriString); | ||
| if (builder.charAt(builder.length() - 1) == '/') { | ||
| if (path.charAt(0) == '/') { | ||
| builder.append(path.substring(1)); | ||
| } else { | ||
| builder.append(path); | ||
| } | ||
| } else if (path.charAt(0) == '/') { | ||
| builder.append(path); | ||
| } else { | ||
| builder.append('/').append(path); | ||
| } | ||
| return new URI(builder.toString()); | ||
| } | ||
|
|
||
| @Consumes(MediaType.MULTIPART_FORM_DATA) | ||
| @Produces(MediaType.APPLICATION_JSON) | ||
| public interface AsyncFileManagerClient extends AutoCloseable { | ||
|
|
||
| @POST | ||
| @Path("upload") | ||
| CompletionStage<Response> uploadFileAsync(List<EntityPart> entityParts); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.