/**
   * Test
   *
   * @throws Exception Any exception
   */
  @Test
  public void createHTTPRequestTest() throws Exception {
    File file = File.createTempFile("text_junit", ".txt");
    file.deleteOnExit();
    IOHelper.writeTextFile("TEST_DATA", file);
    FaxJob faxJob = new FaxJobImpl();
    faxJob.setFilePath(file.getPath());
    faxJob.setTargetName("TEST_TARGET_NAME");
    HTTPRequest output =
        this.converter.createHTTPRequest(
            this.faxClientSpi,
            FaxActionType.SUBMIT_FAX_JOB,
            FaxJob2HTTPRequestConverterConfigurationConstants.SUBMIT_FAX_JOB_TEMPLATE_PROPERTY_KEY,
            faxJob);

    file.delete();

    Assert.assertNotNull(output);
    Assert.assertEquals("TEST_TARGET_NAME", output.getResource());
    Assert.assertEquals(
        "param1=value1&param2=value2&target.name=TEST_TARGET_NAME", output.getParametersText());
    Assert.assertEquals(
        "submit\ntarget.address=\ntarget.name=TEST_TARGET_NAME\nsender.name=\nsender.fax.number=\nsender.email=\nCONTENT:\nTEST_DATA",
        output.getContentAsString());
  }
 /**
  * Test
  *
  * @throws Exception Any exception
  */
 @Test
 public void formatHTTPURLParametersTest() throws Exception {
   FaxJob faxJob = new FaxJobImpl();
   faxJob.setTargetName("TEST_TARGET_NAME");
   String output = this.converter.formatHTTPURLParameters(this.faxClientSpi, faxJob);
   Assert.assertNotNull(output);
   Assert.assertEquals("param1=value1&param2=value2&target.name=TEST_TARGET_NAME", output);
 }
 /**
  * Test
  *
  * @throws Exception Any exception
  */
 @Test
 public void formatHTTPResourceTest() throws Exception {
   FaxJob faxJob = new FaxJobImpl();
   faxJob.setTargetName("TEST_TARGET_NAME");
   String output =
       this.converter.formatHTTPResource(this.faxClientSpi, FaxActionType.SUBMIT_FAX_JOB, faxJob);
   Assert.assertNotNull(output);
   Assert.assertEquals("TEST_TARGET_NAME", output);
 }
  /**
   * Test
   *
   * @throws Exception Any exception
   */
  @Test
  public void formatTemplateTest() throws Exception {
    File file = File.createTempFile("text_junit", ".txt");
    file.deleteOnExit();
    IOHelper.writeTextFile("TEST_DATA", file);
    String template =
        "1:${file}\n2:${target.address}\n3:${target.name}\n4:${sender.name}\n5:${sender.fax.number}\n6:${sender.email}\n";
    FaxJob faxJob = new FaxJobImpl();
    faxJob.setFilePath(file.getPath());
    faxJob.setTargetAddress("TEST_TARGET_ADDRESS");
    faxJob.setTargetName("TEST_TARGET_NAME");
    faxJob.setSenderName("TEST_SENDER_NAME");
    faxJob.setSenderFaxNumber("TEST_SENDER_FAX_NUMBER");
    faxJob.setSenderEmail("TEST_SENDER_EMAIL");
    String output = this.converter.formatTemplate(template, faxJob);

    file.delete();

    Assert.assertNotNull(output);
    Assert.assertNotSame(String.valueOf(-1), String.valueOf(output.indexOf("1:TEST_DATA\n")));
    Assert.assertNotSame(
        String.valueOf(-1), String.valueOf(output.indexOf("2:TEST_TARGET_ADDRESS\n")));
    Assert.assertNotSame(
        String.valueOf(-1), String.valueOf(output.indexOf("3:TEST_TARGET_NAME\n")));
    Assert.assertNotSame(
        String.valueOf(-1), String.valueOf(output.indexOf("4:TEST_SENDER_NAME\n")));
    Assert.assertNotSame(
        String.valueOf(-1), String.valueOf(output.indexOf("5:TEST_SENDER_FAX_NUMBER\n")));
    Assert.assertNotSame(
        String.valueOf(-1), String.valueOf(output.indexOf("6:TEST_SENDER_EMAIL\n")));
  }
  /**
   * This function will create the message used to invoke the fax job action.<br>
   * If this method returns null, the SPI will throw an UnsupportedOperationException.
   *
   * @param faxJob The fax job object containing the needed information
   * @param mailResourcesHolder The mail resources holder
   * @return The message to send (if null, the SPI will throw an UnsupportedOperationException)
   */
  @Override
  protected Message createSubmitFaxJobMessage(
      FaxJob faxJob, MailResourcesHolder mailResourcesHolder) {
    // get logger
    Logger logger = this.getLogger();

    // get target address
    String targetAddress = faxJob.getTargetAddress();

    // format TO value
    String toAddress = MessageFormat.format(this.mailAddressTemplate, new Object[] {targetAddress});
    logger.logDebug(new Object[] {"Formatted TO address: ", toAddress}, null);

    // format subject value
    String subject = MessageFormat.format(this.mailSubjectTemplate, new Object[] {targetAddress});
    logger.logDebug(new Object[] {"Formatted subject: ", subject}, null);

    // get session
    Session session = mailResourcesHolder.getSession();

    // create message
    Message message = new MimeMessage(session);

    // set from
    String from = faxJob.getSenderEmail();
    if ((from != null) && (from.length() > 0)) {
      try {
        message.setFrom(new InternetAddress(from));
      } catch (Exception exception) {
        throw new FaxException("Error while setting from address: " + from, exception);
      }
    }

    try {
      // add message recipient
      message.addRecipient(RecipientType.TO, new InternetAddress(toAddress));
    } catch (Exception exception) {
      throw new FaxException("Error while setting TO address: " + toAddress, exception);
    }

    try {
      // set message subject
      message.setSubject(subject);
    } catch (Exception exception) {
      throw new FaxException("Error while setting subject: " + subject, exception);
    }

    // create multi part
    Multipart multipart = new MimeMultipart();

    // init body part
    BodyPart messageFileAttachmentBodyPart = new MimeBodyPart();

    // init data source
    File file = faxJob.getFile();
    DataSource source = new FileDataSource(file);

    try {
      // set data
      messageFileAttachmentBodyPart.setDataHandler(new DataHandler(source));
      messageFileAttachmentBodyPart.setFileName(file.getName());

      // add to body
      multipart.addBodyPart(messageFileAttachmentBodyPart);

      // set content
      message.setContent(multipart);
    } catch (Exception exception) {
      throw new FaxException("Error while setting attachment.", exception);
    }

    return message;
  }