0

I've got a webservice (running in jBoss 7.4) that's using MTOM to receive files.

The client (another application. SoapUI for testing) sends the file and we receive it.

What's the best approach to create a test that does a request with a file attached to it and after that do a check if the attachment was really received (compare the binary data).

How should I do this?

GregD
  • 1,884
  • 2
  • 28
  • 55

1 Answers1

0

I wrote a similar test case few days back for my application. Yes, it compares actual contents. Below is the source code. This may prove helpful to you.

/**
 * Compares the contents of SOAP attachment and contents of actual file used for creating the attachment
 * Useful for XML/HTML/Plain text attachments
 * @throws SOAPException
 * @throws IOException
 * @throws IllegalArgumentException
 * @throws ClassNotFoundException
 */
@Test
public void testSetAndGetContentForTextualAttachment() throws SOAPException, IOException,
        IllegalArgumentException, ClassNotFoundException {

    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
    SOAPMTOMMessageImpl soapImpl = new SOAPMTOMMessageImpl(soapMessage);
    SOAPPart part = soapMessage.getSOAPPart();

    SOAPEnvelope envelope = part.getEnvelope();
    SOAPBody body = envelope.getBody();
    SOAPBodyElement element = body.addBodyElement(envelope.createName(
            "test", "test", "http://namespace.com/"));

    // Create an Attachment from a file
    File attachmentFile = new File ("C:\\temp\\temp.txt");

    // get the expected contents from a file
    StringBuffer expectedContent = new StringBuffer();
    String line = null;
    BufferedReader br = new BufferedReader(new FileReader(attachmentFile));
    while((line = br.readLine()) != null){
        expectedContent = expectedContent.append(line);
    }

    // create attachment
    // Uses my application's custom classes, but you can use normal SAAJ classes for doing the same.    

    Attachment fileAttachment = soapImpl.createAttachmentFromFile(attachmentFile, "text/plain");
    // content id will be used for downloading the attachment
    fileAttachment.setContentID(attachmentFile.getName()+".restore");

    // create MTOM type soap object from this attachment
    QName fileSoapAttachmentQname = new QName("http://namespace.com/", "AttachFileAsSOAPAttachmentMTOM", "AttachmentElement");
    soapImpl.setXopQname(fileSoapAttachmentQname);
    soapImpl.addAttachmentAsMTOM(fileAttachment, element);

    // Extract the attachment and cross check the contents
    StringBuffer actualContent = new StringBuffer();
    List<Attachment> attachments = soapImpl.getAllAttachments();
    for(int i=0; i<attachments.size(); i++){
        AttachmentPart attachmentPart = ((AttachmentImpl) attachments.get(i)).getAttachmentPart();
        BufferedInputStream bis = new BufferedInputStream (attachmentPart.getDataHandler().getInputStream());

        byte[] data = new byte[1024];
        int numOfBytesRead = 0;
        while(bis.available() > 0){
            numOfBytesRead = bis.read(data);
            String tmp = new String(data,0,numOfBytesRead);
            actualContent = actualContent.append(tmp);
        }
        bis.close();
    }
    try {
        Assert.assertEquals(true,
                (expectedContent.toString()).equals(actualContent.toString()));
    } catch (Throwable e) {
        collector.addError(e);
    }

}

To make similar validation on server side, you can use Content-length header value for comparison. Or you can add an extra attribute to determine the expected attachment size or kind of check-sum.

_Thanks, Bhushan