MultiPart upload testing confusion?

583 views
Skip to first unread message

harry

unread,
Oct 10, 2015, 7:43:40 AM10/10/15
to dropwizard-dev
Can anyone please help with this

All I want to do is perform a file upload test on the following resource method but cannot do it for the life of me. Tried many examples on the web (unfortunately they all seem to be snippets rather than fully working) 


@Path("/file-upload")
public class FileUploadResource
{
    MyConfiguration config;

    public FileUploadResource(MyConfiguration config)
    {
        this.config = config;
    }

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response uploadFile(
                @FormDataParam("fileUpload") InputStream uploadedInputStream,
            @FormDataParam("fileUpload") FormDataContentDisposition fileDetail)
    {
        MyResult result = new MyResult();

        // will eventually do lots more stuff here

        result.setOutcome(SUCCESS);

        return Response.ok(result).build();
    }
...
}

and my test (thought best to leave out rest of form code as so confused with all the examples, no idea where I am with it!) - 

public class FileUploadResourceTest
{
    @ClassRule
    public static final ResourceTestRule resources = ResourceTestRule.builder()
            .addResource(new FileUploadResource(new MyConfiguration()))
            .addResource(new MultiPartFeature())
            .build();

    @Test
    public void testFileUploadKeyGeneration()
    {
        FormDataMultiPart form = new FormDataMultiPart();

    // what do i need to put here to POST test file "x.txt" so I can then check the outCome value etc...?

    // and how do I post it correctly ?
        Response resp = resources.client()
                                                    .target("/file-upload/upload")
                                                    .request()
                                                    .post(????????);

    // assert stuff here
    }
}

Many thanks in advance for any help

harry

unread,
Oct 10, 2015, 1:26:11 PM10/10/15
to dropwizard-dev

Think I'm getting close, abandoned trying to use a file, any ideas what's causing this - 


org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.

at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)

at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)

at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)

at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:502)

at org.glassfish.jersey.test.inmemory.InMemoryConnector.apply(InMemoryConnector.java:239)

at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255)

at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)

at org.glassfish.jersey.internal.Errors.process(Errors.java:315)

at org.glassfish.jersey.internal.Errors.process(Errors.java:297)

at org.glassfish.jersey.internal.Errors.process(Errors.java:228)

at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)

at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)

at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)

at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:349)

at uk.gov.ea.datareturns.resource.FileUploadResourceTest.testFileUploadKeyGeneration(FileUploadResourceTest.java:45)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:497)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at io.dropwizard.testing.junit.ResourceTestRule$1.evaluate(ResourceTestRule.java:199)

at org.junit.rules.RunRules.evaluate(RunRules.java:20)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


public class FileUploadResourceTest

{

    @ClassRule

    public static final ResourceTestRule resources = ResourceTestRule.builder()

        .addResource(new FileUploadResource(new DataExchangeConfiguration()))

        .addResource(new MultiPartFeature()).build();


    @Test

    public void testFileUploadKeyGeneration()

    {

        FormDataMultiPart formDataMultiPart = new FormDataMultiPart();

        String value = "Hello World";

        FormDataContentDisposition dispo = FormDataContentDisposition

            .name("fileName")

            .fileName("test.txt")

            .size(value.getBytes().length)

            .build();


        FormDataBodyPart fdp1 = new FormDataBodyPart(dispo, value);


        formDataMultiPart.bodyPart(fdp1);


        Response resp = resources.client()

            .target("/file-upload/upload")

            .request()

            .post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()), Response.class);

Reply all
Reply to author
Forward
0 new messages