Ejemplo n.º 1
0
  protected static Colony fromJSON(JSONObject json) throws JSONException {
    Colony colony = new Colony(json.getString("id"));
    colony.setX(json.getDouble("x"));
    colony.setY(json.getDouble("y"));

    colony.setAttribute("census.visited", json.optBoolean("visited", false));
    colony.setAttribute("census.active", json.optBoolean("active", false));

    final Object updatedObject = json.opt("modified");
    if (updatedObject == null
        || JSONObject.NULL.equals(updatedObject)
        || !(updatedObject instanceof String)) {
      // Set to now
      colony.setUpdateTime(DateTime.now());
    } else {
      try {
        final DateTime updatedTime =
            ISODateTimeFormat.dateTimeParser().parseDateTime((String) updatedObject);
        colony.setUpdateTime(updatedTime);
      } catch (IllegalArgumentException e) {
        // Could not parse time
        // Set to now
        colony.setUpdateTime(DateTime.now());
      }
    }
    return colony;
  }
Ejemplo n.º 2
0
  @Override
  public TextFeature toText() {
    if (value == null) {
      return new TextFeature(null);
    }

    return new TextFeature(
        ISODateTimeFormat.dateTimeParser().withOffsetParsed().print(new DateTime(value)));
  }
/**
 * ISO 8601 date format decoder.
 *
 * <p>Uses hardcoded UTC timezone and english locale.
 */
public class ISO8601JsonKafkaFieldDecoder extends JsonKafkaFieldDecoder {
  @VisibleForTesting static final String NAME = "iso8601";

  /** TODO: configurable time zones and locales */
  private static final DateTimeFormatter FORMATTER =
      ISODateTimeFormat.dateTimeParser().withLocale(Locale.ENGLISH).withZoneUTC();

  @Override
  public Set<Class<?>> getJavaTypes() {
    return ImmutableSet.<Class<?>>of(long.class, Slice.class);
  }

  @Override
  public String getFieldDecoderName() {
    return NAME;
  }

  @Override
  public KafkaFieldValueProvider decode(JsonNode value, KafkaColumnHandle columnHandle) {
    checkNotNull(columnHandle, "columnHandle is null");
    checkNotNull(value, "value is null");

    return new ISO8601JsonKafkaValueProvider(value, columnHandle);
  }

  public static class ISO8601JsonKafkaValueProvider extends JsonKafkaValueProvider {
    public ISO8601JsonKafkaValueProvider(JsonNode value, KafkaColumnHandle columnHandle) {
      super(value, columnHandle);
    }

    @Override
    public boolean getBoolean() {
      throw new PrestoException(
          KAFKA_CONVERSION_NOT_SUPPORTED, "conversion to boolean not supported");
    }

    @Override
    public double getDouble() {
      throw new PrestoException(
          KAFKA_CONVERSION_NOT_SUPPORTED, "conversion to double not supported");
    }

    @Override
    public long getLong() {
      if (isNull()) {
        return 0L;
      }

      if (value.canConvertToLong()) {
        return value.asLong();
      }

      String textValue = value.isValueNode() ? value.asText() : value.toString();
      return FORMATTER.parseMillis(textValue);
    }
  }
}
Ejemplo n.º 4
0
 @Override
 public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
   JsonToken t = jp.getCurrentToken();
   if (t == JsonToken.VALUE_STRING) {
     String str = jp.getText().trim();
     return ISODateTimeFormat.dateTimeParser().parseDateTime(str).toLocalDate();
   }
   if (t == JsonToken.VALUE_NUMBER_INT) {
     return new LocalDate(jp.getLongValue());
   }
   throw ctxt.mappingException(handledType());
 }
Ejemplo n.º 5
0
  @Test
  public void testMarshalAndUnmarshalWithDateTimeGetter() {
    DateTime dateTime =
        ISODateTimeFormat.dateTimeParser()
            .withOffsetParsed()
            .parseDateTime("2013-07-10T15:37:58.340+02:00");
    ClassWithDateTimeGetter myObject = new ClassWithDateTimeGetter(dateTime);

    String marshaled = serializer.marshal(myObject);
    assertThat(marshaled, is("{\"dateTime\":\"2013-07-10T15:37:58.340+02:00\"}"));

    ClassWithDateTimeGetter unmarshaled =
        serializer.unmarshal(ClassWithDateTimeGetter.class, marshaled);
    assertThat(unmarshaled, equalTo(myObject));
  }
Ejemplo n.º 6
0
  public static final class JodaDateTimeSerializer
      implements JsonDeserializer<DateTime>, JsonSerializer<DateTime> {

    private static final DateTimeFormatter formatter =
        ISODateTimeFormat.dateTimeParser().withZoneUTC();
    private static final DateTimeFormatter printer = ISODateTimeFormat.dateTime();

    @Override
    public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      return formatter.parseDateTime(json.getAsString());
    }

    @Override
    public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
      return new JsonPrimitive(printer.print(src));
    }
  }
/** Those tests mustn't change anything on server side, as jira is restored only once */
@SuppressWarnings("ConstantConditions") // To ignore "May produce NPE" warnings
@RestoreOnce("jira-dump-with-comment-and-worklog-from-removed-user.xml")
public class AsynchronousIssueRestClientReadOnlyTest extends AbstractAsynchronousRestClientTest {

  private static final String REMOVED_USER_NAME = "removed_user";
  private static final String ISSUE_KEY_WITH_REMOVED_USER_DATA = "ANONEDIT-1";

  // no timezone here, as JIRA does not store timezone information in its dump file
  private final DateTime dateTime =
      ISODateTimeFormat.dateTimeParser().parseDateTime("2010-08-04T17:46:45.454");

  @Test
  public void testGetWatchers() throws Exception {
    final Issue issue = client.getIssueClient().getIssue("TST-1").claim();
    final Watchers watchers =
        client.getIssueClient().getWatchers(issue.getWatchers().getSelf()).claim();
    assertEquals(1, watchers.getNumWatchers());
    assertFalse(watchers.isWatching());
    assertThat(watchers.getUsers(), containsInAnyOrder(USER1));
  }

  @Test
  public void testGetWatcherForAnonymouslyAccessibleIssue() {
    setAnonymousMode();
    final Issue issue = client.getIssueClient().getIssue("ANNON-1").claim();
    final Watchers watchers =
        client.getIssueClient().getWatchers(issue.getWatchers().getSelf()).claim();
    assertEquals(1, watchers.getNumWatchers());
    assertFalse(watchers.isWatching());
    assertTrue("JRADEV-3594 bug!!!", Iterables.isEmpty(watchers.getUsers()));
    // to save time
    assertEquals(new TimeTracking(2700, 2400, null), issue.getTimeTracking());
  }

  @Test
  public void testGetIssueWithAnonymouslyCreatedAttachment() {
    setAnonymousMode();
    final Issue issue = client.getIssueClient().getIssue("ANONEDIT-1").claim();
    final Iterator<Attachment> attachmentIterator = issue.getAttachments().iterator();
    assertTrue(attachmentIterator.hasNext());
    assertNull(attachmentIterator.next().getAuthor());
  }

  @Test
  public void testGetIssueWithAnonymouslyCreatedWorklogEntry() {
    setAnonymousMode();
    final Issue issue = client.getIssueClient().getIssue("ANONEDIT-2").claim();
    final Iterator<Worklog> worklogIterator = issue.getWorklogs().iterator();
    assertTrue(worklogIterator.hasNext());
    assertNull(worklogIterator.next().getAuthor());
  }

  // URIs are broken in 5.0 - https://jdog.atlassian.com/browse/JRADEV-7691
  private void assertEqualsNoUri(BasicUser expected, BasicUser actual) {
    assertEquals(expected.getName(), actual.getName());
    assertEquals(expected.getDisplayName(), actual.getDisplayName());
  }

  @Test
  public void testGetIssue() throws Exception {
    final Issue issue = client.getIssueClient().getIssue("TST-1").claim();
    assertEquals("TST-1", issue.getKey());
    assertEquals(Long.valueOf(10000), issue.getId());
    assertTrue(issue.getSelf().toString().startsWith(jiraUri.toString()));
    assertEqualsNoUri(IntegrationTestUtil.USER_ADMIN, issue.getReporter());
    assertEqualsNoUri(IntegrationTestUtil.USER_ADMIN, issue.getAssignee());

    assertThat(issue.getLabels(), containsInAnyOrder("a", "bcds"));

    assertEquals(3, Iterables.size(issue.getComments()));
    final Iterable<String> expectedExpandos = getExpectedExpands();
    assertThat(
        ImmutableList.copyOf(issue.getExpandos()),
        containsInAnyOrder(toArray(expectedExpandos, String.class)));
    assertEquals(new TimeTracking(null, 0, 190), issue.getTimeTracking());
    assertTrue(Iterables.size(issue.getFields()) > 0);

    assertEquals(
        IntegrationTestUtil.START_PROGRESS_TRANSITION_ID, Iterables.size(issue.getAttachments()));
    final Iterable<Attachment> items = issue.getAttachments();
    assertNotNull(items);
    Attachment attachment1 =
        new Attachment(
            IntegrationTestUtil.concat(
                IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER
                    ? UriBuilder.fromUri(jiraUri).path("/rest/api/2/").build()
                    : jiraRestRootUri,
                "/attachment/10040"),
            "dla Paw\u0142a.txt",
            IntegrationTestUtil.USER_ADMIN,
            dateTime,
            643,
            "text/plain",
            IntegrationTestUtil.concat(jiraUri, "/secure/attachment/10040/dla+Paw%C5%82a.txt"),
            null);

    assertEquals(attachment1, items.iterator().next());

    // test for changelog
    assertNull(issue.getChangelog());

    final Issue issueWithChangelogAndOperations =
        client.getIssueClient().getIssue("TST-2", EnumSet.of(CHANGELOG, OPERATIONS)).claim();
    final Iterable<ChangelogGroup> changelog = issueWithChangelogAndOperations.getChangelog();
    if (isJira5xOrNewer()) {
      assertNotNull(changelog);
      final ChangelogGroup chg1 = Iterables.get(changelog, 18);
      assertEquals("admin", chg1.getAuthor().getName());
      assertEquals("Administrator", chg1.getAuthor().getDisplayName());
      assertEquals(
          new DateTime(2010, 8, 17, 16, 40, 34, 924).toInstant(), chg1.getCreated().toInstant());

      assertEquals(
          Collections.singletonList(
              new ChangelogItem(FieldType.JIRA, "status", "1", "Open", "3", "In Progress")),
          chg1.getItems());

      final ChangelogGroup chg2 = Iterables.get(changelog, 20);
      assertEquals("admin", chg2.getAuthor().getName());
      assertEquals("Administrator", chg2.getAuthor().getDisplayName());
      assertEquals(
          new DateTime(2010, 8, 24, 16, 10, 23, 468).toInstant(), chg2.getCreated().toInstant());

      final List<ChangelogItem> expected =
          ImmutableList.of(
              new ChangelogItem(FieldType.JIRA, "timeoriginalestimate", null, null, "0", "0"),
              new ChangelogItem(FieldType.CUSTOM, "My Radio buttons", null, null, null, "Another"),
              new ChangelogItem(FieldType.CUSTOM, "project3", null, null, "10000", "Test Project"),
              new ChangelogItem(FieldType.CUSTOM, "My Number Field New", null, null, null, "1.45"));
      assertEquals(expected, chg2.getItems());
    }
    final Operations operations = issueWithChangelogAndOperations.getOperations();
    if (isJira5xOrNewer()) {
      assertThat(operations, notNullValue());
      assertThat(
          operations.getOperationById("log-work"),
          allOf(instanceOf(OperationLink.class), hasProperty("id", is("log-work"))));
    }
  }

  private Iterable<String> getExpectedExpands() {
    final ImmutableList<String> expandForJira5 =
        ImmutableList.of(
            "renderedFields",
            "names",
            "schema",
            "transitions",
            "operations",
            "editmeta",
            "changelog");
    if (isJira6_4_OrNewer()) {
      return ImmutableList.<String>builder()
          .addAll(expandForJira5)
          .add("versionedRepresentations")
          .build();
    }
    return expandForJira5;
  }

  @Test
  public void testGetIssueWithNonTrivialComments() {
    final Issue issue = client.getIssueClient().getIssue("TST-2").claim();
    final Iterable<Comment> comments = issue.getComments();
    assertEquals(3, Iterables.size(comments));
    final Comment c1 = Iterables.get(comments, 0);
    assertEquals(Visibility.role("Administrators"), c1.getVisibility());

    final Comment c3 = Iterables.get(comments, 2);
    assertEquals(Visibility.group("jira-users"), c3.getVisibility());
  }

  @Test
  public void testGetVoter() {
    final Issue issue = client.getIssueClient().getIssue("TST-1").claim();
    final Votes votes = client.getIssueClient().getVotes(issue.getVotes().getSelf()).claim();
    assertFalse(votes.hasVoted());
    assertThat(votes.getUsers(), containsInAnyOrder(USER1));
  }

  @Test
  public void testGetVotersWithoutViewIssuePermission() {
    final Issue issue = client.getIssueClient().getIssue("RST-1").claim();
    setUser2();
    final String optionalDot = isJira5xOrNewer() ? "." : "";
    assertErrorCode(
        Response.Status.FORBIDDEN,
        "You do not have the permission to see the specified issue" + optionalDot,
        new Runnable() {
          @Override
          public void run() {
            client.getIssueClient().getVotes(issue.getVotes().getSelf()).claim();
          }
        });
  }

  @Test
  public void testGetVotersWithoutViewVotersPermission() {
    setUser2();
    assertNumVotesAndNoVotersDetails("TST-1", 1);
  }

  @Test
  public void testGetVotersAnonymously() {
    setAnonymousMode();
    assertNumVotesAndNoVotersDetails("ANNON-1", 0);
  }

  private void assertNumVotesAndNoVotersDetails(final String issueKey, final int numVotes) {
    final Issue issue = client.getIssueClient().getIssue(issueKey).claim();
    assertEquals(numVotes, issue.getVotes().getVotes());
    assertFalse(issue.getVotes().hasVoted());
    final Votes votes = client.getIssueClient().getVotes(issue.getVotes().getSelf()).claim();
    assertFalse(votes.hasVoted());
    assertEquals(numVotes, votes.getVotes());
    assertTrue(Iterables.isEmpty(votes.getUsers()));
  }

  @Test
  public void testGetTransitions() throws Exception {
    final Issue issue = client.getIssueClient().getIssue("TST-1").claim();
    final Iterable<Transition> transitions = client.getIssueClient().getTransitions(issue).claim();
    assertEquals(4, Iterables.size(transitions));
    assertTrue(
        Iterables.contains(
            transitions,
            new Transition(
                "Start Progress",
                IntegrationTestUtil.START_PROGRESS_TRANSITION_ID,
                Collections.<Transition.Field>emptyList())));
  }

  @JiraBuildNumberDependent(BN_JIRA_5)
  @Test
  public void testGetCreateIssueMetadata() throws URISyntaxException {
    final Iterable<CimProject> metadataProjects =
        client
            .getIssueClient()
            .getCreateIssueMetadata(
                new GetCreateIssueMetadataOptionsBuilder().withExpandedIssueTypesFields().build())
            .claim();

    assertEquals(4, Iterables.size(metadataProjects));

    final CimProject project =
        Iterables.find(
            metadataProjects,
            new Predicate<CimProject>() {
              @Override
              public boolean apply(CimProject input) {
                return "ANONEDIT".equals(input.getKey());
              }
            });

    assertEquals(project.getName(), "Anonymous Editable Project");

    for (CimIssueType issueType : project.getIssueTypes()) {
      assertFalse(
          String.format("Issue type ('%s') fields are not empty!", issueType.getName()),
          issueType.getFields().isEmpty());
    }
  }

  @JiraBuildNumberDependent(BN_JIRA_5)
  @Test
  public void testGetCreateIssueMetadataWithFieldsNotExpanded() throws URISyntaxException {
    final Iterable<CimProject> metadataProjects =
        client.getIssueClient().getCreateIssueMetadata(null).claim();

    assertEquals(4, Iterables.size(metadataProjects));

    final CimProject project =
        Iterables.find(
            metadataProjects,
            new Predicate<CimProject>() {
              @Override
              public boolean apply(CimProject input) {
                return "ANONEDIT".equals(input.getKey());
              }
            });

    assertEquals(project.getName(), "Anonymous Editable Project");
    assertEquals(5, Iterables.size(project.getIssueTypes()));

    for (CimIssueType issueType : project.getIssueTypes()) {
      assertTrue(issueType.getFields().isEmpty());
    }
  }

  @JiraBuildNumberDependent(BN_JIRA_5)
  @Test
  public void testGetCreateIssueMetadataWithProjectKeyFilter() throws URISyntaxException {
    final Iterable<CimProject> metadataProjects =
        client
            .getIssueClient()
            .getCreateIssueMetadata(
                new GetCreateIssueMetadataOptionsBuilder()
                    .withProjectKeys("ANONEDIT", "TST")
                    .withExpandedIssueTypesFields()
                    .build())
            .claim();

    assertEquals(2, Iterables.size(metadataProjects));

    final CimProject project =
        Iterables.find(
            metadataProjects,
            new Predicate<CimProject>() {
              @Override
              public boolean apply(CimProject input) {
                return "TST".equals(input.getKey());
              }
            });

    assertEquals(project.getName(), "Test Project");
    assertEquals(5, Iterables.size(project.getIssueTypes()));

    for (CimIssueType issueType : project.getIssueTypes()) {
      assertFalse(issueType.getFields().isEmpty());
    }
  }

  @Test
  public void testFetchingIssueWithWorklogWhenAuthorIsDeleted() {
    final Issue issue = client.getIssueClient().getIssue(ISSUE_KEY_WITH_REMOVED_USER_DATA).claim();
    final Worklog worklog = issue.getWorklogs().iterator().next();
    assertNotNull(worklog);
    final BasicUser author = worklog.getAuthor();
    assertNotNull(author);
    assertThat(author.getName(), equalTo(REMOVED_USER_NAME));
    assertTrue("expected incomplete self uri", author.isSelfUriIncomplete());
  }

  @Test
  public void testFetchingIssueWithCommentWhenAuthorIsDeleted() {
    final Issue issue = client.getIssueClient().getIssue(ISSUE_KEY_WITH_REMOVED_USER_DATA).claim();
    final Comment comment = issue.getComments().iterator().next();
    assertNotNull(comment);
    final BasicUser author = comment.getAuthor();
    assertNotNull(author);
    assertEquals(getUserUri(REMOVED_USER_NAME), author.getSelf());
  }
}
Ejemplo n.º 8
0
 public Timestamp(String iso8601) {
   this.time =
       ISODateTimeFormat.dateTimeParser().parseDateTime(iso8601).toDateTime(DateTimeZone.UTC);
 }
Ejemplo n.º 9
0
 public static DateTime stringToDateTime(String datetime) {
   DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser();
   return parser.parseDateTime(datetime);
 }
Ejemplo n.º 10
0
 public DateTime getTimestamp() {
   return ISODateTimeFormat.dateTimeParser().parseDateTime(publishedAt);
 }
/**
 * MessageDecoder class that will convert the payload into a JSON object, look for a the
 * camus.message.timestamp.field, convert that timestamp to a unix epoch long using
 * camus.message.timestamp.format, and then set the CamusWrapper's timestamp property to the
 * record's timestamp. If the JSON does not have a timestamp or if the timestamp could not be parsed
 * properly, then System.currentTimeMillis() will be used.
 *
 * <p>camus.message.timestamp.format will be used with SimpleDateFormat. If your
 * camus.message.timestamp.field is stored in JSON as a unix epoch timestamp, you should set
 * camus.message.timestamp.format to 'unix_seconds' (if your timestamp units are seconds) or
 * 'unix_milliseconds' (if your timestamp units are milliseconds).
 *
 * <p>This MessageDecoder returns a CamusWrapper that works with Strings payloads, since JSON data
 * is always a String.
 */
public class JsonStringMessageDecoder extends MessageDecoder<byte[], String> {
  private static final org.apache.log4j.Logger log =
      Logger.getLogger(JsonStringMessageDecoder.class);

  // Property for format of timestamp in JSON timestamp field.
  public static final String CAMUS_MESSAGE_TIMESTAMP_FORMAT = "camus.message.timestamp.format";
  public static final String DEFAULT_TIMESTAMP_FORMAT = "[dd/MMM/yyyy:HH:mm:ss Z]";

  // Property for the JSON field name of the timestamp.
  public static final String CAMUS_MESSAGE_TIMESTAMP_FIELD = "camus.message.timestamp.field";
  public static final String DEFAULT_TIMESTAMP_FIELD = "timestamp";

  JsonParser jsonParser = new JsonParser();
  DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser();

  private String timestampFormat;
  private String timestampField;

  @Override
  public void init(Properties props, String topicName) {
    this.props = props;
    this.topicName = topicName;

    timestampFormat = props.getProperty(CAMUS_MESSAGE_TIMESTAMP_FORMAT, DEFAULT_TIMESTAMP_FORMAT);
    timestampField = props.getProperty(CAMUS_MESSAGE_TIMESTAMP_FIELD, DEFAULT_TIMESTAMP_FIELD);
  }

  @Override
  public CamusWrapper<String> decode(byte[] payload) {
    long timestamp = 0;
    String payloadString;
    JsonObject jsonObject;

    try {
      payloadString = new String(payload, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      log.error("Unable to load UTF-8 encoding, falling back to system default", e);
      payloadString = new String(payload);
    }

    // Parse the payload into a JsonObject.
    try {
      jsonObject = jsonParser.parse(payloadString.trim()).getAsJsonObject();
    } catch (RuntimeException e) {
      log.error("Caught exception while parsing JSON string '" + payloadString + "'.");
      throw new RuntimeException(e);
    }

    // Attempt to read and parse the timestamp element into a long.
    if (jsonObject.has(timestampField)) {
      // If timestampFormat is 'unix_seconds',
      // then the timestamp only needs converted to milliseconds.
      // Also support 'unix' for backwards compatibility.
      if (timestampFormat.equals("unix_seconds") || timestampFormat.equals("unix")) {
        timestamp = jsonObject.get(timestampField).getAsLong();
        // This timestamp is in seconds, convert it to milliseconds.
        timestamp = timestamp * 1000L;
      }
      // Else if this timestamp is already in milliseconds,
      // just save it as is.
      else if (timestampFormat.equals("unix_milliseconds")) {
        timestamp = jsonObject.get(timestampField).getAsLong();
      }
      // Else if timestampFormat is 'ISO-8601', parse that
      else if (timestampFormat.equals("ISO-8601")) {
        String timestampString = jsonObject.get(timestampField).getAsString();
        try {
          timestamp = new DateTime(timestampString).getMillis();
        } catch (IllegalArgumentException e) {
          log.error(
              "Could not parse timestamp '"
                  + timestampString
                  + "' as ISO-8601 while decoding JSON message.");
        }
      }
      // Otherwise parse the timestamp as a string in timestampFormat.
      else {
        String timestampString = jsonObject.get(timestampField).getAsString();
        try {
          timestamp = dateTimeParser.parseDateTime(timestampString).getMillis();
        } catch (IllegalArgumentException e) {
          try {
            timestamp = new SimpleDateFormat(timestampFormat).parse(timestampString).getTime();
          } catch (ParseException pe) {
            log.error(
                "Could not parse timestamp '" + timestampString + "' while decoding JSON message.");
          }
        } catch (Exception ee) {
          log.error(
              "Could not parse timestamp '" + timestampString + "' while decoding JSON message.");
        }
      }
    }

    // If timestamp wasn't set in the above block,
    // then set it to current time.
    if (timestamp == 0) {
      log.warn(
          "Couldn't find or parse timestamp field '"
              + timestampField
              + "' in JSON message, defaulting to current time.");
      timestamp = System.currentTimeMillis();
    }

    return new CamusWrapper<String>(payloadString, timestamp);
  }
}