@Test
  public void testCreateAuthorizationHeader() {
    Date now = DateUtil.now();

    String method = "POST";
    String hostname = "api.tuppari.com";
    String path = "/test";
    String query = "a=v1&b=v2";

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Host", "api.tuppari.com");
    headers.put("Content-type", "application/json");
    headers.put("X-Tuppari-Operation", "CreateApplication");

    Map<String, Object> body = new LinkedHashMap<String, Object>();
    body.put("applicationName", "example1");
    String accessKeyId = "accessKeyId";
    String accessSecretKey = "accessSecretKey";

    String authorization =
        SignUtil.createAuthorizationHeader(
            method, hostname, path, query, headers, body, now, accessKeyId, accessSecretKey);
    assertThat(
        authorization,
        is(
            "HMAC-SHA256 Credential=accessKeyId,SignedHeaders=content-type;host;x-tuppari-operation,Signature=050f8711271747d4f63a3caa3ffb420e4cd5a0e9d9dda8ba7e4faad6794c40d0"));
  }
  @Test
  public void testUpsertDateValues() throws Exception {
    long ts = nextTimestamp();
    Date now = new Date(System.currentTimeMillis());
    ensureTableCreated(getUrl(), TestUtil.PTSDB_NAME, null, ts - 2);
    Properties props = new Properties();
    props.setProperty(
        PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 1)); // Execute at timestamp 1
    Connection conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props);
    String dateString = "1999-01-01 02:00:00";
    PreparedStatement upsertStmt =
        conn.prepareStatement(
            "upsert into ptsdb(inst,host,date) values('aaa','bbb',to_date('" + dateString + "'))");
    int rowsInserted = upsertStmt.executeUpdate();
    assertEquals(1, rowsInserted);
    upsertStmt =
        conn.prepareStatement(
            "upsert into ptsdb(inst,host,date) values('ccc','ddd',current_date())");
    rowsInserted = upsertStmt.executeUpdate();
    assertEquals(1, rowsInserted);
    conn.commit();

    props.setProperty(
        PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 2)); // Execute at timestamp 1
    conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props);
    String select = "SELECT date,current_date() FROM ptsdb";
    ResultSet rs = conn.createStatement().executeQuery(select);
    Date then = new Date(System.currentTimeMillis());
    assertTrue(rs.next());
    Date date = DateUtil.parseDate(dateString);
    assertEquals(date, rs.getDate(1));
    assertTrue(rs.next());
    assertTrue(rs.getDate(1).after(now) && rs.getDate(1).before(then));
    assertFalse(rs.next());
  }
  @Test
  public void testCreateSignedRequestConfig() throws ParseException {
    Date now = DateUtil.now();

    String method = "POST";
    URI uri = URI.create("http://api.tuppari.com/test?a=v1&b=v2");
    String operation = "CreateApplication";
    Map<String, Object> body = new LinkedHashMap<String, Object>();
    body.put("applicationName", "example1");
    String accessKeyId = "accessKeyId";
    String accessSecretKey = "accessSecretKey";

    Map<String, Object> config =
        SignUtil.createSignedRequestConfig(
            method, uri, operation, body, accessKeyId, accessSecretKey);

    assertThat((URI) config.get("uri"), is(uri));
    assertThat((String) config.get("body"), is(JSON.encode(body)));

    Map<String, String> headers = (Map<String, String>) config.get("headers");
    assertThat(headers.get("Host"), is("api.tuppari.com"));
    assertThat(headers.get("Content-Type"), is("application/json"));
    assertThat(headers.get("X-Tuppari-Date"), is("Thu, 01 Jan 1970 00:00:00 GMT"));
    assertThat(headers.get("X-Tuppari-Operation"), is("CreateApplication"));
    assertThat(
        headers.get("Authorization"),
        is(
            "HMAC-SHA256 Credential=accessKeyId,SignedHeaders=content-type;host;x-tuppari-date;x-tuppari-operation,Signature=f35767c9fdba4ba5d5bbbf1c622fceed0dbaeb210303bb56b419c6a51bcf1e5d"));
  }
  @Test
  public void testConvertStringToTimestamp() throws Exception {
    final Date today = new Date();
    final Calendar todayCalendar = new GregorianCalendar();
    todayCalendar.setTime(today);
    final String datePart = DateUtil.convertDateToString(today);

    final Timestamp time = (Timestamp) converter.convert(Timestamp.class, datePart + " 01:02:03.4");
    final Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(time.getTime());
    assertEquals(todayCalendar.get(Calendar.YEAR), cal.get(Calendar.YEAR));
    assertEquals(todayCalendar.get(Calendar.MONTH), cal.get(Calendar.MONTH));
    assertEquals(todayCalendar.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH));
  }
  @Test
  public void testConvertStringToDate() throws Exception {
    final Date today = new Date();
    final Calendar todayCalendar = new GregorianCalendar();
    todayCalendar.setTime(today);
    final String datePart = DateUtil.convertDateToString(today);
    // test empty time
    Date date = (Date) converter.convert(Date.class, "");
    assertNull(date);
    date = (Date) converter.convert(Date.class, datePart);

    final Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    assertEquals(todayCalendar.get(Calendar.YEAR), cal.get(Calendar.YEAR));
    assertEquals(todayCalendar.get(Calendar.MONTH), cal.get(Calendar.MONTH));
    assertEquals(todayCalendar.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_MONTH));
  }
  @Test
  public void testCreateSignature_1() throws ParseException {
    String secretKey = "secretKey1";
    String stringToSign =
        "SHA256\n"
            + "19700101T000000Z\n"
            + "152176000cc08c7d9d0558bc3a50368aa38619a695ad20f50bec1344429cb315";

    Date now = DateUtil.now();
    String host = "api.tuppari.com";
    String expectedSignature = "4815ff1681a278e7c852902ea3604f17831a80a78dc0ff82f5142598a034509b";

    // Check with the same secret key, siganature returns same signature
    for (int i = 0; i < 1000; ++i) {
      String result = SignUtil.createSignature(secretKey, stringToSign, now, host);
      assertThat(result, is(expectedSignature));
    }
  }
  @Test
  public void testCreateSignature_2() throws ParseException {
    String secretKey1 = "secretKey1";
    String secretKey2 = "secretKey2";

    String stringToSign =
        "SHA256\n"
            + "19700101T000000Z\n"
            + "152176000cc08c7d9d0558bc3a50368aa38619a695ad20f50bec1344429cb315";

    Date now = DateUtil.now();
    String host = "api.tuppari.com";
    String expectedSignature = "4815ff1681a278e7c852902ea3604f17831a80a78dc0ff82f5142598a034509b";

    String result1 = SignUtil.createSignature(secretKey1, stringToSign, now, host);
    String result2 = SignUtil.createSignature(secretKey2, stringToSign, now, host);

    assertThat(result1, not(equalTo(result2)));
  }
Exemple #8
0
  void checkCalendar(int start, int end) {
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    calendar.clear();

    calendar.set(Calendar.YEAR, start);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DATE, 1);

    Date date = new Date();
    date.setElapsedDays(DateUtil.getDayFromDate(start, 1, 1));
    while (calendar.get(Calendar.YEAR) <= end) {

      String calString = calendar.toString();
      assertThat("Year: " + calString, date.getYear(), is(calendar.get(Calendar.YEAR)));
      assertThat("Month: " + calString, date.getMonth(), is(calendar.get(Calendar.MONTH) + 1));
      assertThat("Date: " + calString, date.getDay(), is(calendar.get(Calendar.DATE)));

      calendar.add(Calendar.DATE, 1);
      date.setElapsedDays(date.getElapsedDays() + 1);
    }
  }
  @Test
  public void testCreateStringToSign() throws ParseException {
    String canonicalRequest =
        "POST\n"
            + "/test\n"
            + "a=v1&b=v2\n"
            + "content-type:application/json\n"
            + "host:api.tuppari.com\n"
            + "x-tuppari-operation:CreateApplication\n"
            + "content-type;host;x-tuppari-operation\n"
            + "8f2d5fe4a93000d3546e578d265fc936806f6ef6dc6f7ee87715e1a5c514c168";

    Date now = DateUtil.now();
    String result = SignUtil.createStringToSign(canonicalRequest, now);
    assertThat(
        result,
        is(
            ""
                + "SHA256\n"
                + "19700101T000000Z\n"
                + "152176000cc08c7d9d0558bc3a50368aa38619a695ad20f50bec1344429cb315"));
  }
 @Test
 public void testConvertDateToString() throws Exception {
   final Calendar cal = new GregorianCalendar(2005, 0, 16);
   final String date = (String) converter.convert(String.class, cal.getTime());
   assertEquals(DateUtil.convertDateToString(cal.getTime()), date);
 }
 @Test
 public void testConvertTimestampToString() throws Exception {
   final Timestamp timestamp = Timestamp.valueOf("2005-03-10 01:02:03.4");
   final String time = (String) converter.convert(String.class, timestamp);
   assertEquals(DateUtil.getDateTime(DateUtil.getDateTimePattern(), timestamp), time);
 }
 @After
 public void after() {
   DateUtil.clearDummyTime();
 }
 @Before
 public void before() {
   DateUtil.setDummyTime("1970-01-01 00:00:00 GMT");
 }