mardi 8 septembre 2015

Uploading file to test as FormDataParam

So I have problem with testing my application. I am trying to do REST/HTTP test. Here is my code:

@Path("/ftpaction")
public class JerseyFileUpload {
    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response postMsg(@HeaderParam("FTP-Host") String Host, @HeaderParam("FTP-Port") String Port,
            @HeaderParam("FTP-User") String User, @HeaderParam("FTP-Password") String Password,
            @HeaderParam("FTP-Path") String Path, @FormDataParam("file") InputStream inputStream) {
        try {
            InformationHandler informationHandler = new InformationHandler(Path, Host, Port, User, Password);
            CountriesStructure worker = new CountriesStructure();
            worker.prepareCountriesStructure(inputStream, true, informationHandler);
        } catch (UsernameOrPasswordException e) {
            return Response.status(401).entity("Status 401.").build();
        } catch (SocketException e) {
            return Response.status(404).entity("Status 404.").build();
        } catch (IOException e) {
            return Response.status(400).entity("Status 400.").build();
        } catch (JAXBException e) {
            return Response.status(500).entity("Status 500.").build();
        } catch (Exception e) {
            return Response.status(500).entity("Status 500.").build();
        }
        return Response.status(200).entity("Success!").build();
    }
}

And my test:

@RunWith(HttpJUnitRunner.class)
public class TestMain extends TestCase {
    @Rule
    public Destination destination = new Destination(this, "http://localhost:8080");

    @Context
    private Response response;

    @HttpTest(method = Method.POST, path = "/JerseyWebApp/ftpaction/upload", content = "{}", file = "/CountriesList.txt", type = MediaType.MULTIPART_FORM_DATA, headers = {
            @Header(name = "FTP-Host", value = "localhost"), @Header(name = "FTP-Port", value = "21"),
            @Header(name = "FTP-User", value = "ftptest"), @Header(name = "FTP-Password", value = "test"),
            @Header(name = "FTP-Path", value = "/test123"), @Header(name = "Accept-Encoding", value = "multipart/form-data")})
    public void checkPost() {
        System.out.println(response.getBody());
    }
}

I have problem with reading file by test. I don't know what I have to do, because I am using file as "FormDataParam". Somebody have any idea how can I upload file to test as FormDataParam? Because like now it doesn't see my file and just return "Status 400".

Aucun commentaire:

Enregistrer un commentaire