|
| 1 | +package org.javaee7.jaxrs.fileupload; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileOutputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.MalformedURLException; |
| 9 | +import java.net.URI; |
| 10 | +import java.net.URL; |
| 11 | + |
| 12 | +import javax.ws.rs.client.Client; |
| 13 | +import javax.ws.rs.client.ClientBuilder; |
| 14 | +import javax.ws.rs.client.Entity; |
| 15 | +import javax.ws.rs.client.WebTarget; |
| 16 | +import javax.ws.rs.core.MediaType; |
| 17 | +import javax.ws.rs.core.Response; |
| 18 | +import javax.ws.rs.core.Response.Status; |
| 19 | + |
| 20 | +import org.assertj.core.api.Condition; |
| 21 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 22 | +import org.jboss.arquillian.junit.Arquillian; |
| 23 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 24 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 25 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Arun Gupta |
| 33 | + * @author Xavier Coulon |
| 34 | + */ |
| 35 | +@RunWith(Arquillian.class) |
| 36 | +public class MyResourceTest { |
| 37 | + |
| 38 | + @Deployment(testable = false) |
| 39 | + public static WebArchive createDeployment() { |
| 40 | + return ShrinkWrap.create(WebArchive.class).addClasses(MyApplication.class, MyResource.class); |
| 41 | + } |
| 42 | + |
| 43 | + private static WebTarget target; |
| 44 | + |
| 45 | + private static File tempFile; |
| 46 | + @ArquillianResource |
| 47 | + private URL base; |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public static void generateSampleFile() throws IOException { |
| 51 | + tempFile = File.createTempFile("javaee7samples", ".png"); |
| 52 | + // fill the file with 1KB of content |
| 53 | + try (FileOutputStream outputStream = new FileOutputStream(tempFile)) { |
| 54 | + for (int i = 0; i < 1000; i++) { |
| 55 | + outputStream.write(0); |
| 56 | + } |
| 57 | + } |
| 58 | + assertThat(tempFile).canRead().has(new Condition<File>() { |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean matches(File tempFile) { |
| 62 | + return tempFile.length() == 1000; |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setUpClass() throws MalformedURLException { |
| 69 | + Client client = ClientBuilder.newClient(); |
| 70 | + target = client.target(URI.create(new URL(base, "webresources/endpoint").toExternalForm())); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void shouldPostOctetStreamContentAsInputStream() { |
| 75 | + // when |
| 76 | + Long uploadedFileSize = target.path("/upload").request() |
| 77 | + .post(Entity.entity(tempFile, MediaType.APPLICATION_OCTET_STREAM), Long.class); |
| 78 | + // then |
| 79 | + assertThat(uploadedFileSize).isEqualTo(1000); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void shouldNotPostImagePngContentAsInputStream() { |
| 84 | + // when |
| 85 | + final Response response = target.path("/upload").request().post(Entity.entity(tempFile, "image/png")); |
| 86 | + // then |
| 87 | + assertThat(response.getStatus()).isEqualTo(Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void shouldPostOctetStreamContentAsFile() { |
| 92 | + // when |
| 93 | + Long uploadedFileSize = target.path("/upload2").request() |
| 94 | + .post(Entity.entity(tempFile, MediaType.APPLICATION_OCTET_STREAM), Long.class); |
| 95 | + // then |
| 96 | + assertThat(uploadedFileSize).isEqualTo(1000); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void shouldPostImagePngContentAsFile() { |
| 101 | + // when |
| 102 | + Long uploadedFileSize = target.path("/upload2").request() |
| 103 | + .post(Entity.entity(tempFile, "image/png"), Long.class); |
| 104 | + // then |
| 105 | + assertThat(uploadedFileSize).isEqualTo(1000); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments