Skip to content

Commit

Permalink
Fix code review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-krueger committed Mar 13, 2024
1 parent e27d138 commit 0487108
Showing 1 changed file with 49 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.Consumes;
Expand Down Expand Up @@ -136,7 +137,7 @@ public void basicTest() throws Exception {
.request(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(Entity.entity(new GenericEntity<>(multipart) {
}, MediaType.MULTIPART_FORM_DATA))) {
Assertions.assertEquals(Response.Status.OK, response.getStatusInfo());
Assertions.assertEquals(200, response.getStatus());
final List<EntityPart> entityParts = response.readEntity(new GenericType<>() {
});
if (entityParts.size() != 3) {
Expand All @@ -151,8 +152,23 @@ public void basicTest() throws Exception {
Assertions.assertNotNull(part, getMessage(entityParts));
Assertions.assertEquals("test string", part.getContent(String.class));

// The javadoc for EntityPart.getContent(Class<T> type) states:
// "Subsequent invocations will result in an {@code IllegalStateException}.
// Likewise this method will throw an {@code IllegalStateException} if it is
// called after calling {@link #getContent} or {@link #getContent(GenericType)}.
try {
part.getContent(String.class);
Assertions.fail("IllegalStateException is expected when getContent() is " +
"invoked more than once.");
} catch (IllegalStateException e) {
// expected exception
} catch (Throwable t) {
Assertions.fail("Incorrect Throwable received: " + t);
}

part = find(entityParts, "received-file");
Assertions.assertNotNull(part, getMessage(entityParts));
Assertions.assertEquals("test file", part.getFileName().get());
Assertions.assertTrue(part.getContent(String.class).contains("value6"));

part = find(entityParts, "added-input-stream");
Expand Down Expand Up @@ -187,11 +203,11 @@ public void singleFormParamTest() throws Exception {
.mediaType(MediaType.TEXT_PLAIN_TYPE)
.build());
try (
Response response = client.target(createCombinedUri(uri, "test//single-form-param"))
Response response = client.target(createCombinedUri(uri, "test/single-form-param"))
.request(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(Entity.entity(new GenericEntity<>(multipart) {
}, MediaType.MULTIPART_FORM_DATA))) {
Assertions.assertEquals(Response.Status.OK, response.getStatusInfo());
Assertions.assertEquals(200, response.getStatus());
final List<EntityPart> entityParts = response.readEntity(new GenericType<>() {
});
if (entityParts.size() != 3) {
Expand Down Expand Up @@ -240,7 +256,7 @@ public void multiFormParamTest() throws Exception {
.request(MediaType.MULTIPART_FORM_DATA_TYPE)
.post(Entity.entity(new GenericEntity<>(multipart) {
}, MediaType.MULTIPART_FORM_DATA))) {
Assertions.assertEquals(Response.Status.OK, response.getStatusInfo());
Assertions.assertEquals(200, response.getStatus());
final List<EntityPart> entityParts = response.readEntity(new GenericType<>() {
});
if (entityParts.size() != 3) {
Expand All @@ -266,30 +282,36 @@ private static void verifyEntityPart(final List<EntityPart> parts, final String
Assertions.assertEquals(text, part.getContent(String.class));
}

private static String getMessage(final List<EntityPart> parts) throws IOException {
final StringBuilder msg = new StringBuilder();
final Iterator<EntityPart> iter = parts.iterator();
while (iter.hasNext()) {
final EntityPart part = iter.next();
msg.append('[')
.append(part.getName())
.append("={")
.append("headers=")
.append(part.getHeaders())
.append(", mediaType=")
.append(part.getMediaType())
.append(", body=")
.append(toString(part.getContent()))
.append('}');
if (iter.hasNext()) {
msg.append("], ");
} else {
msg.append(']');
private static Supplier<String> getMessage(final List<EntityPart> parts) {
return () -> {
final StringBuilder msg = new StringBuilder();
final Iterator<EntityPart> iter = parts.iterator();
while (iter.hasNext()) {
final EntityPart part = iter.next();
try {
msg.append('[')
.append(part.getName())
.append("={")
.append("headers=")
.append(part.getHeaders())
.append(", mediaType=")
.append(part.getMediaType())
.append(", body=")
.append(toString(part.getContent()))
.append('}');
} catch (IOException e) {
Assertions.fail("Unable to proces Entityparts." + e);
}
if (iter.hasNext()) {
msg.append("], ");
} else {
msg.append(']');
}
}
}
return msg.toString();
return msg.toString();
};
}

private static String toString(final InputStream in) throws IOException {
// try-with-resources fails here due to a bug in the
//noinspection TryFinallyCanBeTryWithResources
Expand Down Expand Up @@ -360,7 +382,7 @@ public Response basicTest(final List<EntityPart> parts) throws IOException {
.mediaType(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.build(),
EntityPart.withName("received-file")
.content(find(parts,"file"))
.content(find(parts,"file").getFileName().get(),find(parts,"file").getContent())
.mediaType(MediaType.APPLICATION_XML)
.build(),
EntityPart.withName("added-input-stream")
Expand Down

0 comments on commit 0487108

Please sign in to comment.