0

I'm running a set of integration tests of a jax-rs service using embedded openejb. One of these require to receive a binary file. See the method below:

@POST
@Path("signOff")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void signOffDeliveries(
    @Encoded @FormParam("signature") File signature) {

}

Now, this method works fine running it on Websphere, but running a test towards the embedded openejb fails. The signature gets a file name that contains the whole image binary data(a lot of scrambled signs).

Now reading this file gives a FileNotFoundException, as expected. My question is then, is this a bug in the embedded openejb, or am I setting up my tests the wrong way? Heres the test code btw:

@RunWith(ApplicationComposer.class)
public class DeliveryServiceIT {

    private HttpClient httpClient;
    private DeliveryServiceClient client;

    @Configuration
    public Properties config() throws Exception {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");
        return properties;
    }

    @MockInjector
    public Class<?> mockitoInjector() {
        return MockitoInjector.class;
    }

    @Module
    public EjbModule createModule() throws Exception {
        EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new StatelessBean(DeliveryService.class));
        EjbModule module = new EjbModule(ejbJar);
        return module;
    }

    @Before
    public void setup() {
        //this is where I create the http client that makes the actual http request
    }

    @Test
    public void signOffDeliveries_givenSignatureImageAndDeliveries_expectsValidRequest() throws FileNotFoundException, IOException {
        File f = new File("/signature.png");

        client.signOffDeliveries(file);
        //the client will take the file, get the bytes, create 
        //multipart/form-data request and send the request to the 
        //service method posted above
    }

Since this is working on my websphere, I think its either a problem with the openejb version I'm running my integration tests in(4.5.0) or there is a problem with how the test is setup.

yegor256
  • 102,010
  • 123
  • 446
  • 597
Runar Halse
  • 3,528
  • 10
  • 39
  • 59

1 Answers1

0

You expect your clients to send file full server path over HTTP? This is exactly what is happening. Form param signature encoded in HTTP POST body is used as a single argument to construct an object of class File. Of couse the file is not found.

Use String instead and build server-specific file path inside your JAX-RS resource.

yegor256
  • 102,010
  • 123
  • 446
  • 597
  • The clients sends a multipart request with a content disposition as part of the payload, giving the param name and file name: `Content-Disposition: form-data; name="paramName"; filename="fileName"`. Also, the transfer encoding is specified `Content-Transfer-Encoding: binary` The payload then follows. Somehow, openejb interprets this request as the payload is part of the filename? – Runar Halse Jan 31 '13 at 07:40