/**
   * スクリプトからExt_MailSenderクラスを呼び出せること. Extensionの MailSenderテスト用。 <br>
   * 本テストを動作させるためには、Ext_MailSenderの jarファイル、プロパティファイルが必要。<br>
   * <br>
   */
  @Test
  public final void スクリプトからExt_MailSenderクラスを呼び出せること() {

    SimpleSmtpServer smtpServer = SimpleSmtpServer.start(1025);

    try {
      String url;
      String testSrc = "callExtensionMailSender.js";
      HttpUriRequest req = null;
      String requestJson =
          "{\"to\":[{\"address\":\"[email protected]\",\"name\":\"テストユーザ1\"},"
              + "{\"address\":\"[email protected]\",\"name\":\"テストユーザ2\"}],"
              + "\"cc\":[{\"address\":\"[email protected]\",\"name\":\"テストユーザ3\"}],"
              + "\"bcc\":[{\"address\":\"[email protected]\",\"name\":\"テストユーザ4\"}],"
              + "\"from\":{\"address\":\"[email protected]\",\"name\":\"テストユーザ5\"},"
              + "\"reply-to\":[{\"address\":\"[email protected]\",\"name\":\"テストユーザ6\"}],"
              + "\"subject\":\"タイトル\",\"text\":\"メール本文の内容\",\"charset\":\"ISO-2022-JP\","
              + "\"envelope-from\":\"[email protected]\",\"headers\":{\"Organization\":\"personium\"}}";

      try {
        if (isServiceTest) {
          // スクリプトの登録 (Davのput)
          putScript(testSrc, "test.js");
          url = requestUrl();
        } else {
          url = requestUrl(testSrc);
        }
        // サービスの実行
        req = new DcRequestBuilder().url(url).method("POST").body(requestJson).token(token).build();
        req.setHeader(KEY_HEADER_BASEURL, baseUrl);
        String version = getVersion();
        if (version != null && !(version.equals(""))) {
          req.setHeader("X-Dc-Version", version);
        }

        HttpResponse objResponse;
        objResponse = httpClient.execute(req);
        DcResponse dcRes = new DcResponse(objResponse);

        assertEquals(HttpStatus.SC_OK, dcRes.getStatusCode());
        assertEquals("Successfully sent a mail.", dcRes.bodyAsString());

        if (!isServiceTest) {
          // ITの場合はローカル以外のSMTPサーバに送信するためチェック不可
          assertEquals(1, smtpServer.getReceivedEmailSize());
        }

      } catch (DaoException e) {
        fail(e.getMessage());
      } catch (ClientProtocolException e) {
        fail(e.getMessage());
      } catch (IOException e) {
        fail(e.getMessage());
      } finally {
        if (isServiceTest) {
          // スクリプトの削除(Davのdel)
          try {
            testSvcCol.del("test.js");
          } catch (DaoException e) {
            fail(e.getMessage());
          }
        }
      }
    } finally {
      smtpServer.stop();
    }
  }
  /**
   * スクリプトからExt_AWSSecurityTokenServiceのSessionToken取得が行えること. Extensionの
   * AWSSecurityTokenServiceテスト用。
   */
  @SuppressWarnings("unchecked")
  @Test
  public final void スクリプトからExt_AWSSecurityTokenServiceのSessionToken取得が行えること() {

    Properties properties = new Properties();
    InputStream configFileStream =
        ClassLoader.getSystemResourceAsStream("extension-test-config.properties");
    try {
      properties.load(configFileStream);
    } catch (IOException e) {
      fail("properties load failuer");
    }

    String url;
    String testSrc = "callExtension_AWSSecurityTokenService_getSessionToken.js";
    HttpUriRequest req = null;
    JSONObject requestBody = new JSONObject();
    requestBody.put("AccessKeyId", properties.getProperty("AccessKeyId"));
    requestBody.put("SecretAccessKey", properties.getProperty("SecretAccessKey"));
    requestBody.put("ProxyHost", properties.getProperty("ProxyHost", null));
    requestBody.put("ProxyPort", Integer.parseInt(properties.getProperty("ProxyPort", "80")));
    requestBody.put("ProxyUser", properties.getProperty("ProxyUser", null));
    requestBody.put("ProxyPassword", properties.getProperty("ProxyPassword", null));
    requestBody.put("durationSeconds", null);

    System.out.println(requestBody.toJSONString());
    try {
      if (isServiceTest) {
        // スクリプトの登録 (Davのput)
        putScript(testSrc, "test.js");
        url = requestUrl();
      } else {
        url = requestUrl(testSrc);
      }
      // サービスの実行(durationSeconds省略)
      req =
          new DcRequestBuilder()
              .url(url)
              .method("POST")
              .body(requestBody.toJSONString())
              .token(token)
              .build();
      req.setHeader(KEY_HEADER_BASEURL, baseUrl);
      String version = getVersion();
      if (version != null && !(version.equals(""))) {
        req.setHeader("X-Dc-Version", version);
      }

      HttpResponse objResponse;
      objResponse = httpClient.execute(req);
      DcResponse dcRes = new DcResponse(objResponse);

      assertEquals(HttpStatus.SC_OK, dcRes.getStatusCode());
      JSONObject credentials = (JSONObject) dcRes.bodyAsJson().get("Credentials");
      System.out.println(credentials.toJSONString());
      assertNotNull(credentials);

      // サービスの実行(durationSeconds指定)
      requestBody.put("durationSeconds", 900);
      req =
          new DcRequestBuilder()
              .url(url)
              .method("POST")
              .body(requestBody.toJSONString())
              .token(token)
              .build();
      req.setHeader(KEY_HEADER_BASEURL, baseUrl);
      version = getVersion();
      if (version != null && !(version.equals(""))) {
        req.setHeader("X-Dc-Version", version);
      }

      objResponse = httpClient.execute(req);
      dcRes = new DcResponse(objResponse);

      assertEquals(HttpStatus.SC_OK, dcRes.getStatusCode());
      credentials = (JSONObject) dcRes.bodyAsJson().get("Credentials");
      System.out.println(credentials.toJSONString());
      assertNotNull(credentials);

    } catch (DaoException e) {
      fail(e.getMessage());
    } catch (ClientProtocolException e) {
      fail(e.getMessage());
    } catch (IOException e) {
      fail(e.getMessage());
    } finally {
      if (isServiceTest) {
        // スクリプトの削除(Davのdel)
        try {
          testSvcCol.del("test.js");
        } catch (DaoException e) {
          fail(e.getMessage());
        }
      }
    }
  }