Last active
February 27, 2021 11:57
-
-
Save psamsotha/218c6bbeb6164bac7cbc to your computer and use it in GitHub Desktop.
Jersey Multipart MP3 test
This file contains 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
import java.io.File; | |
import java.io.InputStream; | |
import java.util.logging.Logger; | |
import javax.ws.rs.Consumes; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.client.Entity; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import org.glassfish.jersey.client.ClientConfig; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.media.multipart.FormDataContentDisposition; | |
import org.glassfish.jersey.media.multipart.FormDataMultiPart; | |
import org.glassfish.jersey.media.multipart.FormDataParam; | |
import org.glassfish.jersey.media.multipart.MultiPart; | |
import org.glassfish.jersey.media.multipart.MultiPartFeature; | |
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import static org.junit.Assert.assertEquals; | |
import org.junit.Test; | |
/* | |
* Stack Overflow question: http://stackoverflow.com/q/33405074/2587435 | |
* | |
* Run this like any other JUnit test. Replace the `FILE` with your file. | |
* | |
* You will need to following Maven dependences. | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* <version>2.19</version> | |
* <scope>test</scope> | |
* </dependency> | |
* <dependency> | |
* <groupId>org.glassfish.jersey.media</groupId> | |
* <artifactId>jersey-media-multipart</artifactId> | |
* <version>2.19</version> | |
* </dependency> | |
* | |
* @author Paul Samsotha | |
*/ | |
public class Mp3Test extends JerseyTest { | |
private static final String FILE = "The Show Goes On.mp3"; | |
private static final String QUEUE = "qMultipart"; | |
private static final String EXPECTED = FILE + ":" + QUEUE; | |
@Path("multipart") | |
public static class MultiPartResource { | |
@POST | |
@Consumes(MediaType.MULTIPART_FORM_DATA) | |
public String post(@FormDataParam("file") InputStream file, | |
@FormDataParam("file") FormDataContentDisposition fdcd, | |
@FormDataParam("queue") String queue) throws Exception { | |
file.close(); | |
return fdcd.getFileName() + ":" + queue; | |
} | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig(MultiPartResource.class) | |
.register(MultiPartFeature.class) | |
.register(new LoggingFilter(Logger.getAnonymousLogger(), true)); | |
} | |
@Override | |
public void configureClient(ClientConfig config) { | |
config.register(MultiPartFeature.class); | |
} | |
@Test | |
public void test() { | |
FileDataBodyPart filePart = new FileDataBodyPart("file", new File(FILE)); | |
filePart.setContentDisposition( | |
FormDataContentDisposition.name("file").fileName(FILE).build()); | |
filePart.setMediaType(MediaType.valueOf("audio/mpeg")); | |
MultiPart multiPart = new FormDataMultiPart() | |
.field("queue", QUEUE, MediaType.TEXT_PLAIN_TYPE) | |
.bodyPart(filePart); | |
Response response = target("multipart").request() | |
.post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE)); | |
assertEquals(200, response.getStatus()); | |
assertEquals(EXPECTED, response.readEntity(String.class)); | |
response.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the nice example.
I have one question:
My resource class MultiPartResource does not have a no argument constructor but has one argument constructor taking Configuration class, what changes I have to make ?
I will have to mock this configuration class and pass the mock to the resource class
I get the error "Could not find a suitable constructor..." and I guess it is because of the line 'return new
ResourceConfig(MultiPartResource.class)'.
Also, if I am saving the uploaded file to disk using a private method in my resource class, how do I test this ? I tried to use the PowerMock
but it seems since I am extending the JerseyTest there is some issue.
Thanks for your help.