@Test
  public void verifyResult() {
    DocumentPackageAttributesExample documentPackageAttributesExample =
        new DocumentPackageAttributesExample(Props.get());
    documentPackageAttributesExample.run();

    DocumentPackage documentPackage = documentPackageAttributesExample.getRetrievedPackage();
    DocumentPackageAttributes documentPackageAttributes = documentPackage.getAttributes();
    Map<String, Object> attributeMap = documentPackageAttributes.getContents();
    assertThat(
        "Attribute key 1 is not setup correctly.", attributeMap.containsKey(ATTRIBUTE_KEY_1));
    assertThat(
        "Attribute key 2 is not setup correctly.", attributeMap.containsKey(ATTRIBUTE_KEY_2));
    assertThat(
        "Attribute key 3 is not setup correctly.", attributeMap.containsKey(ATTRIBUTE_KEY_3));

    assertThat(
        "Attribute 1 is not setup correctly.",
        attributeMap.get(ATTRIBUTE_KEY_1).toString().equals(ATTRIBUTE_1));
    assertThat(
        "Attribute 2 is not setup correctly.",
        attributeMap.get(ATTRIBUTE_KEY_2).toString().equals(ATTRIBUTE_2));
    assertThat(
        "Attribute 3 is not setup correctly.",
        attributeMap.get(ATTRIBUTE_KEY_3).toString().equals(ATTRIBUTE_3));
  }
  public static void getCompletedPackage() {
    EslClient eslClient = new EslClient(API_KEY, API_URL);
    PackageId packageId = new PackageId("jyvSSesuYZBadsRdCLxQAzr3KlsS");
    DocumentPackage retrievedPackage = eslClient.getPackage(packageId);

    System.out.println(retrievedPackage.getName());
  }
  @Test
  public void verifyResult() {
    SignerBoundFieldsExample signerBoundFieldsExample = new SignerBoundFieldsExample(Props.get());
    signerBoundFieldsExample.run();
    DocumentPackage documentPackage = signerBoundFieldsExample.getRetrievedPackage();

    for (Signature signature :
        documentPackage.getDocument(SignerBoundFieldsExample.DOCUMENT_NAME).getSignatures()) {

      for (Field field : signature.getFields()) {
        if ((int) (field.getX() + 0.1) == SignerBoundFieldsExample.SIGNATURE_DATE_POSITION_X
            && (int) (field.getY() + 0.1) == SignerBoundFieldsExample.SIGNATURE_DATE_POSITION_Y) {
          assertThat(field.getPage(), is(equalTo(SignerBoundFieldsExample.SIGNATURE_DATE_PAGE)));
          assertThat(field.getStyle(), is(equalTo(FieldStyle.BOUND_DATE)));
        }
        if ((int) (field.getX() + 0.1) == SignerBoundFieldsExample.SIGNER_COMPANY_POSITION_X
            && (int) (field.getY() + 0.1) == SignerBoundFieldsExample.SIGNER_COMPANY_POSITION_Y) {
          assertThat(field.getPage(), is(equalTo(SignerBoundFieldsExample.SIGNER_COMPANY_PAGE)));
          assertThat(field.getStyle(), is(equalTo(FieldStyle.BOUND_COMPANY)));
        }
        if ((int) (field.getX() + 0.1) == SignerBoundFieldsExample.SIGNER_NAME_POSITION_X
            && (int) (field.getY() + 0.1) == SignerBoundFieldsExample.SIGNER_NAME_POSITION_Y) {
          assertThat(field.getPage(), is(equalTo(SignerBoundFieldsExample.SIGNER_NAME_PAGE)));
          assertThat(field.getStyle(), is(equalTo(FieldStyle.BOUND_NAME)));
        }
        if ((int) (field.getX() + 0.1) == SignerBoundFieldsExample.SIGNER_TITLE_POSITION_X
            && (int) (field.getY() + 0.1) == SignerBoundFieldsExample.SIGNER_TITLE_POSITION_Y) {
          assertThat(field.getPage(), is(equalTo(SignerBoundFieldsExample.SIGNER_TITLE_PAGE)));
          assertThat(field.getStyle(), is(equalTo(FieldStyle.BOUND_TITLE)));
        }
      }
    }
  }
  @Test
  public void verifyResult() {
    CreatePackageFromTemplateWithFieldsExample example =
        new CreatePackageFromTemplateWithFieldsExample();
    example.run();

    DocumentPackage documentPackage = example.getRetrievedPackage();

    for (Signature signature : documentPackage.getDocument(DOCUMENT_NAME).getSignatures()) {
      for (Field field : signature.getFields()) {
        if (field.getId().toString().equals(TEXTFIELD_ID)) {
          assertThat(field.getPage(), equalTo(TEXTFIELD_PAGE));
          assertThat(field.getStyle(), equalTo(UNBOUND_TEXT_FIELD));
        }
        if (field.getId().toString().equals(CHECKBOX_1_ID)) {
          assertThat(field.getPage(), equalTo(CHECKBOX_1_PAGE));
          assertThat(field.getStyle(), equalTo(UNBOUND_CHECK_BOX));
          assertThat(field.getValue(), nullValue());
        }
        if (field.getId().toString().equals(CHECKBOX_2_ID)) {
          assertThat(field.getPage(), equalTo(CHECKBOX_1_PAGE));
          assertThat(field.getStyle(), equalTo(UNBOUND_CHECK_BOX));
          assertThat(field.getValue(), equalTo(CHECKBOX_CHECKED));
        }
        if (field.getId().toString().equals(RADIO_1_ID)) {
          assertThat(field.getPage(), equalTo(RADIO_1_PAGE));
          assertThat(field.getStyle(), equalTo(UNBOUND_RADIO_BUTTON));
          assertThat(field.getFieldValidator().getOptions().get(0), equalTo(RADIO_1_GROUP));
          assertThat(field.getValue(), nullValue());
        }
        if (field.getId().toString().equals(RADIO_2_ID)) {
          assertThat(field.getPage(), equalTo(RADIO_2_PAGE));
          assertThat(field.getStyle(), equalTo(UNBOUND_RADIO_BUTTON));
          assertThat(field.getFieldValidator().getOptions().get(0), equalTo(RADIO_2_GROUP));
          assertThat(field.getValue(), equalTo(RADIO_SELECTED));
        }
        if (field.getId().toString().equals(DROP_LIST_ID)) {
          assertThat(field.getPage(), equalTo(DROP_LIST_PAGE));
          assertThat(field.getStyle(), equalTo(DROP_LIST));
          assertThat(field.getFieldValidator().getOptions().get(0), equalTo(DROP_LIST_OPTION1));
          assertThat(field.getFieldValidator().getOptions().get(1), equalTo(DROP_LIST_OPTION2));
          assertThat(field.getFieldValidator().getOptions().get(2), equalTo(DROP_LIST_OPTION3));
          assertThat(field.getValue(), equalTo(DROP_LIST_OPTION2));
        }
        if (field.getId().toString().equals(TEXT_AREA_ID)) {
          assertThat(field.getPage(), equalTo(TEXT_AREA_PAGE));
          assertThat(field.getStyle(), equalTo(TEXT_AREA));
          assertThat(field.getValue(), equalTo(TEXT_AREA_VALUE));
        }
        if (field.getId().toString().equals(LABEL_ID)) {
          assertThat(field.getPage(), equalTo(LABEL_PAGE));
          assertThat(field.getStyle(), equalTo(LABEL));
          assertThat(field.getValue(), equalTo(LABEL_VALUE));
        }
      }
    }
  }
 private void assertEqualsPackageUpdatedDate(
     Page<DocumentPackage> packages, Date startDate, Date endDate) {
   for (DocumentPackage draftPackage : packages) {
     assertThat(
         draftPackage.getUpdatedDate(),
         is(greaterThanOrEqualTo(new DateTime(startDate).withTimeAtStartOfDay().toDate())));
     assertThat(
         draftPackage.getUpdatedDate(),
         is(lessThan(new DateTime(endDate).plusDays(1).withTimeAtStartOfDay().toDate())));
   }
 }
  /**
   * Get the document's metadata from the package.
   *
   * @param documentPackage The DocumentPackage we want to get document from.
   * @param documentId Id of document to get.
   * @return the document's metadata
   */
  public com.silanis.esl.sdk.Document getDocumentMetadata(
      DocumentPackage documentPackage, String documentId) {
    String path =
        template
            .urlFor(UrlTemplate.DOCUMENT_ID_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .replace("{documentId}", documentId)
            .build();

    try {
      String response = client.get(path);
      Document apilDocument = Serialization.fromJson(response, Document.class);

      // Wipe out the members not related to the metadata
      apilDocument.setApprovals(new ArrayList<Approval>());
      apilDocument.setFields(new ArrayList<Field>());
      apilDocument.setPages(new ArrayList<com.silanis.esl.api.model.Page>());

      return new DocumentConverter(
              apilDocument, new DocumentPackageConverter(documentPackage).toAPIPackage())
          .toSDKDocument();
    } catch (RequestException e) {
      throw new EslServerException("Could not get the document's metadata.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get the document's metadata." + " Exception: " + e.getMessage());
    }
  }
  @Override
  public void execute() {
    DocumentPackage template =
        PackageBuilder.newPackageNamed("Template " + getPackageName())
            .describedAs("first message")
            .withEmailMessage(PACKAGE_EMAIL_MESSAGE)
            .withSigner(
                newSignerWithEmail(email1)
                    .withFirstName(PACKAGE_SIGNER1_FIRST)
                    .withLastName(PACKAGE_SIGNER1_LAST)
                    .withTitle(PACKAGE_SIGNER1_TITLE)
                    .withCompany(PACKAGE_SIGNER1_COMPANY)
                    .withCustomId(PACKAGE_SIGNER1_CUSTOM_ID))
            .withDocument(
                DocumentBuilder.newDocumentWithName(DOCUMENT_NAME)
                    .fromStream(documentInputStream1, DocumentType.PDF)
                    .withId(DOCUMENT_ID)
                    .build())
            .build();

    template.setId(eslClient.getTemplateService().createTemplate(template));

    DocumentPackage newPackage =
        PackageBuilder.newPackageNamed(getPackageName())
            .describedAs(PACKAGE_DESCRIPTION)
            .withEmailMessage(PACKAGE_EMAIL_MESSAGE2)
            .withSigner(
                newSignerWithEmail(email2)
                    .withFirstName(PACKAGE_SIGNER2_FIRST)
                    .withLastName(PACKAGE_SIGNER2_LAST)
                    .withTitle(PACKAGE_SIGNER2_TITLE)
                    .withCompany(PACKAGE_SIGNER2_COMPANY)
                    .withCustomId(PACKAGE_SIGNER2_CUSTOM_ID))
            .withSettings(
                DocumentPackageSettingsBuilder.newDocumentPackageSettings()
                    .withoutInPerson()
                    .withoutDecline()
                    .withoutOptOut()
                    .withWatermark()
                    .build())
            .build();

    packageId =
        eslClient.getTemplateService().createPackageFromTemplate(template.getId(), newPackage);
    retrievedPackage = eslClient.getPackage(packageId);
  }
  @Override
  public void execute() {
    DocumentPackage template =
        PackageBuilder.newPackageNamed("Template " + getPackageName())
            .describedAs("first message")
            .withEmailMessage(PACKAGE_EMAIL_MESSAGE)
            .withSigner(SignerBuilder.newSignerPlaceholder(new Placeholder(PLACEHOLDER_ID)))
            .withDocument(
                DocumentBuilder.newDocumentWithName(DOCUMENT_NAME)
                    .fromStream(documentInputStream1, DocumentType.PDF)
                    .withId(DOCUMENT_ID)
                    .withSignature(
                        SignatureBuilder.signatureFor(new Placeholder(PLACEHOLDER_ID))
                            .onPage(0)
                            .atPosition(100, 100))
                    .build())
            .build();

    template.setId(eslClient.getTemplateService().createTemplate(template));

    DocumentPackage newPackage =
        PackageBuilder.newPackageNamed(getPackageName())
            .describedAs(PACKAGE_DESCRIPTION)
            .withEmailMessage(PACKAGE_EMAIL_MESSAGE2)
            .withSigner(
                newSignerWithEmail(email1)
                    .withFirstName(SIGNER1_FIRST)
                    .withLastName(SIGNER1_LAST)
                    .withTitle(SIGNER1_TITLE)
                    .withCompany(SIGNER1_COMPANY)
                    .withCustomId(PLACEHOLDER_ID)
                    .withAttachmentRequirement(
                        AttachmentRequirementBuilder.newAttachmentRequirementWithName(
                                ATTACHMENT_REQUIREMENT_NAME)
                            .withDescription(ATTACHMENT_REQUIREMENT_DESCRIPTION)
                            .isRequiredAttachment()
                            .build()))
            .withSettings(
                DocumentPackageSettingsBuilder.newDocumentPackageSettings().withInPerson().build())
            .build();

    packageId =
        eslClient.getTemplateService().createPackageFromTemplate(template.getId(), newPackage);
    retrievedPackage = eslClient.getPackage(packageId);
  }
  /**
   * Updates the documents signing order
   *
   * @param documentPackage
   */
  public void orderDocuments(DocumentPackage documentPackage) {
    String path =
        template
            .urlFor(UrlTemplate.DOCUMENT_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .build();

    List<Document> documents = new ArrayList<Document>();
    for (com.silanis.esl.sdk.Document document : documentPackage.getDocuments()) {
      documents.add(new DocumentConverter(document).toAPIDocumentMetadata());
    }
    try {
      String json = Serialization.toJson(documents);

      client.put(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Could not order the documents.", e);
    } catch (Exception e) {
      throw new EslException("Could not order the documents." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Updates the signer order in a package.
   *
   * @param documentPackage The id of the package to update signer order
   */
  public void orderSigners(DocumentPackage documentPackage) {
    String path =
        template
            .urlFor(UrlTemplate.ROLE_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .build();

    List<Role> roles = new ArrayList<Role>();
    for (com.silanis.esl.sdk.Signer signer : documentPackage.getSigners()) {
      roles.add(new SignerConverter(signer).toAPIRole(signer.getId()));
    }

    try {
      String json = Serialization.toJson(roles);
      client.put(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Could not order signers.", e);
    } catch (Exception e) {
      throw new EslException("Could not order signers." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Updates the document's metadata from the package.
   *
   * @param documentPackage
   * @param document
   */
  public void updateDocumentMetadata(
      DocumentPackage documentPackage, com.silanis.esl.sdk.Document document) {
    String path =
        template
            .urlFor(UrlTemplate.DOCUMENT_ID_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .replace("{documentId}", document.getId().toString())
            .build();

    Document internalDoc = new DocumentConverter(document).toAPIDocumentMetadata();

    try {
      String json = Serialization.toJson(internalDoc);

      client.put(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Could not update the document's metadata.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not update the document's metadata." + " Exception: " + e.getMessage());
    }
  }
  @Test
  public void verifyResult() {

    BasicPackageCreationExample basicPackageCreationExample =
        new BasicPackageCreationExample(Props.get());
    basicPackageCreationExample.run();

    DocumentPackage documentPackage = basicPackageCreationExample.getRetrievedPackage();

    // Verify if the package is created correctly.
    assertFalse(
        "Package enableInPerson setting was not set correctly.",
        documentPackage.getSettings().getEnableInPerson());

    assertThat(
        "Package description was not set correctly.",
        documentPackage.getDescription(),
        is("This is a package created using the e-SignLive SDK"));
    assertThat(
        "Package expiry date was not set correctly.",
        documentPackage.getExpiryDate(),
        is(now().plusMonths(1).toDate()));
    assertThat(
        "Package message was not set correctly.",
        documentPackage.getPackageMessage(),
        is("This message should be delivered to all signers"));

    // Verify if the sdk version is set correctly
    assertThat("Package attributes are null", documentPackage.getAttributes(), is(notNullValue()));
    assertThat(
        "Package attributes are empty",
        documentPackage.getAttributes().getContents(),
        is(notNullValue()));
    assertThat(
        "SDK version was not set",
        documentPackage.getAttributes().toMap().containsKey("sdk"),
        is(true));
    assertThat(
        "SDK version was not set to the correct value",
        documentPackage.getAttributes().toMap().get("sdk").toString(),
        is(equalTo("Java v" + VersionUtil.getVersion())));

    // Signer 1
    Signer signer = documentPackage.getSigner(basicPackageCreationExample.email1);

    assertThat("Signer 1 ID was not set correctly.", signer.getId(), is("Client1"));
    assertThat("Signer 1 first name was not set correctly.", signer.getFirstName(), is("John"));
    assertThat("Signer 1 last name was not set correctly.", signer.getLastName(), is("Smith"));
    assertThat("Signer 1 title was not set correctly.", signer.getTitle(), is("Managing Director"));
    assertThat("Signer 1 company was not set correctly.", signer.getCompany(), is("Acme Inc."));

    // Signer 2
    signer = documentPackage.getSigner(basicPackageCreationExample.email2);
    assertThat("Signer 2 first name was not set correctly.", signer.getFirstName(), is("Patty"));
    assertThat("Signer 2 last name was not set correctly.", signer.getLastName(), is("Galant"));

    // Document 1
    Document document = documentPackage.getDocument("First Document pdf");

    Iterator<Signature> signatures = document.getSignatures().iterator();
    Signature signature;
    Field field;

    if (signatures.hasNext()) {
      signature = signatures.next();

      assertThat(
          "Signature's signer Email was not set correctly for First Document.",
          signature.getSignerEmail(),
          is(basicPackageCreationExample.email1));
      assertThat(
          "Signature page was not set correctly for First Document.", signature.getPage(), is(0));

      Iterator<Field> fields = signature.getFields().iterator();
      if (fields.hasNext()) {
        field = fields.next();
        assertThat(
            "Field style for signature was not set correctly in First Document.",
            field.getStyle(),
            is(FieldStyle.UNBOUND_CHECK_BOX));
        assertThat(
            "Field Page number was not set correctly in First Document.", field.getPage(), is(0));
        assertThat(
            "Field value of signature was not set correctly in First Document.",
            field.getValue(),
            is(FieldBuilder.RADIO_SELECTED));
      }
    }

    // Document 2
    document = documentPackage.getDocument("Second Document PDF");
    signatures = document.getSignatures().iterator();

    if (signatures.hasNext()) {
      signature = signatures.next();

      assertThat(
          "Signature's signer Email was not set correctly for Second Document.",
          signature.getSignerEmail(),
          is("*****@*****.**"));
      assertThat(
          "Signature page was not set correctly for Second Document.", signature.getPage(), is(0));

      Iterator<Field> fields = signature.getFields().iterator();
      if (fields.hasNext()) {
        field = fields.next();
        assertThat(
            "First radio button style for signature was not set correctly in Second Document.",
            field.getStyle(),
            is(FieldStyle.UNBOUND_RADIO_BUTTON));
        assertThat(
            "First radio button Page number was not set correctly in Second Document.",
            field.getPage(),
            is(0));
        assertThat(
            "First radio button value of signature was not set correctly in Second Document.",
            field.getValue(),
            is(""));
        assertThat(
            "First radio button group was not set correctly in Second Document.",
            field.getFieldValidator().getOptions().get(0),
            equalTo(basicPackageCreationExample.group1));

        field = fields.next();
        assertThat(
            "Second radio button style for signature was not set correctly in Second Document.",
            field.getStyle(),
            is(FieldStyle.UNBOUND_RADIO_BUTTON));
        assertThat(
            "Second radio button Page number was not set correctly in Second Document.",
            field.getPage(),
            is(0));
        assertThat(
            "Second radio button value of signature was not set correctly in Second Document.",
            field.getValue(),
            is(FieldBuilder.RADIO_SELECTED));
        assertThat(
            "Second radio button group was not set correctly in Second Document.",
            field.getFieldValidator().getOptions().get(0),
            equalTo(basicPackageCreationExample.group1));

        field = fields.next();
        assertThat(
            "Third radio button style for signature was not set correctly in Second Document.",
            field.getStyle(),
            is(FieldStyle.UNBOUND_RADIO_BUTTON));
        assertThat(
            "Third radio button Page number was not set correctly in Second Document.",
            field.getPage(),
            is(0));
        assertThat(
            "Third radio button value of signature was not set correctly in Second Document.",
            field.getValue(),
            is(FieldBuilder.RADIO_SELECTED));
        assertThat(
            "Third radio button group was not set correctly in Second Document.",
            field.getFieldValidator().getOptions().get(0),
            equalTo(basicPackageCreationExample.group2));

        field = fields.next();
        assertThat(
            "Third radio button style for signature was not set correctly in Second Document.",
            field.getStyle(),
            is(FieldStyle.UNBOUND_RADIO_BUTTON));
        assertThat(
            "Third radio button Page number was not set correctly in Second Document.",
            field.getPage(),
            is(0));
        assertThat(
            "Third radio button value of signature was not set correctly in Second Document.",
            field.getValue(),
            is(""));
        assertThat(
            "Third radio button group was not set correctly in Second Document.",
            field.getFieldValidator().getOptions().get(0),
            equalTo(basicPackageCreationExample.group2));
      }
    }
  }