protected static List<TimestampedAnnotation> convertToTimestampedAnnotationsList( List<JsonDeserializationResult> jdrList ) { if (jdrList == null || jdrList.isEmpty()) { return null; } List<TimestampedAnnotation> annotationList = new ArrayList<>(jdrList.size()); for (JsonDeserializationResult jdr : jdrList) { long timestamp = Long.parseLong(jdr.levelOneKeyValuePairs.get(ANNOTATION_SUBOBJECT_TIMESTAMP_FIELD)); String value = jdr.levelOneKeyValuePairs.get(ANNOTATION_SUBOBJECT_VALUE_FIELD); annotationList.add(new TimestampedAnnotation(timestamp, value)); } return annotationList; }
@Test public void TimestampedAnnotation_constructor_works_as_expected() { // given long timestamp = 42; String value = UUID.randomUUID().toString(); // when TimestampedAnnotation result = new TimestampedAnnotation(timestamp, value); // then assertThat(result.getTimestampEpochMicros()).isEqualTo(timestamp); assertThat(result.getValue()).isEqualTo(value); }
@Test public void TimestampedAnnotation_equals_returns_false_and_hashCode_different_if_other_is_not_a_TimestampedAnnotation() { // given TimestampedAnnotation annotation = new TimestampedAnnotation(42, "foo"); String notAnAnnotation = "notAnAnnotation"; // expect //noinspection EqualsBetweenInconvertibleTypes assertThat(annotation.equals(notAnAnnotation)).isFalse(); assertThat(annotation.hashCode()).isNotEqualTo(notAnAnnotation.hashCode()); }
/** * A convenience static factory method that serves the same purpose as the * {@link TimestampedAnnotation#TimestampedAnnotation(long, String)} constructor. * * @param timestampEpochMicros The timestamp in epoch microseconds (not milliseconds). * @param value The value to associate with the given timestamp. * @return A new {@link TimestampedAnnotation} with the given timestamp and value. */ public static TimestampedAnnotation forEpochMicros(long timestampEpochMicros, String value) { return new TimestampedAnnotation(timestampEpochMicros, value); }
@Test public void TimestampedAnnotation_equals_returns_false_and_hashCode_different_if_timestamp_is_different() { // given TimestampedAnnotation annotation1 = new TimestampedAnnotation(42, "foo"); TimestampedAnnotation annotation2 = new TimestampedAnnotation( annotation1.getTimestampEpochMicros() + 1234, annotation1.getValue() ); // expect assertThat(annotation1.equals(annotation2)).isFalse(); assertThat(annotation1.hashCode()).isNotEqualTo(annotation2.hashCode()); }
@Test public void TimestampedAnnotation_equals_returns_true_and_hashCode_same_if_all_fields_are_equal() { // given TimestampedAnnotation annotation1 = new TimestampedAnnotation(42, "foo"); TimestampedAnnotation annotation2 = new TimestampedAnnotation( annotation1.getTimestampEpochMicros(), annotation1.getValue() ); // expect assertThat(annotation1.equals(annotation2)).isTrue(); assertThat(annotation1.hashCode()).isEqualTo(annotation2.hashCode()); }
@Test public void TimestampedAnnotation_equals_returns_true_and_hashCode_same_if_same_instance() { // given TimestampedAnnotation instance = new TimestampedAnnotation(42, "foo"); // expect //noinspection EqualsWithItself assertThat(instance.equals(instance)).isTrue(); assertThat(instance.hashCode()).isEqualTo(instance.hashCode()); }
@Test public void TimestampedAnnotation_equals_returns_false_and_hashCode_different_if_value_is_different() { // given TimestampedAnnotation annotation1 = new TimestampedAnnotation(42, "foo"); TimestampedAnnotation annotation2 = new TimestampedAnnotation( annotation1.getTimestampEpochMicros(), annotation1.getValue() + "_nope" ); // expect assertThat(annotation1.equals(annotation2)).isFalse(); assertThat(annotation1.hashCode()).isNotEqualTo(annotation2.hashCode()); }