com.google.cloud.Timestamp Java Examples

The following examples show how to use com.google.cloud.Timestamp. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleCloudUtils.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an Entity Builder for the given key and properties. Converts the objects to the proper
 * datastore values
 */
static Entity.Builder createEntityBuilder(Key key, Map<String, Object> data) throws IOException {
  Entity.Builder builder = Entity.newBuilder(key);

  for (Map.Entry<String, Object> entry : data.entrySet()) {
    if (entry.getValue() instanceof String) {
      builder.set(entry.getKey(), (String) entry.getValue()); // StringValue
    } else if (entry.getValue() instanceof Integer) {
      builder.set(entry.getKey(), (Integer) entry.getValue()); // LongValue
    } else if (entry.getValue() instanceof Double) {
      builder.set(entry.getKey(), (Double) entry.getValue()); // DoubleValue
    } else if (entry.getValue() instanceof Boolean) {
      builder.set(entry.getKey(), (Boolean) entry.getValue()); // BooleanValue
    } else if (entry.getValue() instanceof Timestamp) {
      builder.set(entry.getKey(), (Timestamp) entry.getValue()); // TimestampValue
    } else {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try (ObjectOutputStream out = new ObjectOutputStream(bos)) {
        out.writeObject(entry.getValue());
      }
      builder.set(entry.getKey(), Blob.copyFrom(bos.toByteArray())); // BlobValue
    }
  }
  return builder;
}
 
Example #2
Source File: SpannerSample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
static void updateBackup(DatabaseAdminClient dbAdminClient, BackupId backupId) {
  // Get current backup metadata.
  Backup backup = dbAdminClient.newBackupBuilder(backupId).build().reload();
  // Add 30 days to the expire time.
  // Expire time must be within 366 days of the create time of the backup.
  Timestamp expireTime =
      Timestamp.ofTimeMicroseconds(
          TimeUnit.SECONDS.toMicros(backup.getExpireTime().getSeconds())
              + TimeUnit.NANOSECONDS.toMicros(backup.getExpireTime().getNanos())
              + TimeUnit.DAYS.toMicros(30L));
  System.out.println(String.format(
      "Updating expire time of backup [%s] to %s...",
      backupId.toString(),
      LocalDateTime.ofEpochSecond(
          expireTime.getSeconds(),
          expireTime.getNanos(),
          OffsetDateTime.now().getOffset()).toString()));

  // Update expire time.
  backup = backup.toBuilder().setExpireTime(expireTime).build();
  backup.updateExpireTime();
  System.out.println("Updated backup [" + backupId + "]");
}
 
Example #3
Source File: TFRecordIOIT.java    From beam with Apache License 2.0 6 votes vote down vote up
private void collectAndPublishMetrics(
    final PipelineResult writeResults, final PipelineResult readResults) {
  final String uuid = UUID.randomUUID().toString();
  final String timestamp = Timestamp.now().toString();
  final Set<NamedTestResult> results = new HashSet<>();

  results.add(
      NamedTestResult.create(uuid, timestamp, RUN_TIME, getRunTime(writeResults, readResults)));
  results.addAll(
      MetricsReader.ofResults(writeResults, TFRECORD_NAMESPACE)
          .readAll(getWriteMetricSuppliers(uuid, timestamp)));
  results.addAll(
      MetricsReader.ofResults(readResults, TFRECORD_NAMESPACE)
          .readAll(getReadMetricSuppliers(uuid, timestamp)));

  IOITMetrics.publish(bigQueryDataset, bigQueryTable, results);
  IOITMetrics.publishToInflux(uuid, timestamp, results, settings);
}
 
Example #4
Source File: DateMapper.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  try {
    Timestamp ts = ((TimestampValue) input).get();
    long millis = TimeUnit.SECONDS.toMillis(ts.getSeconds())
        + TimeUnit.NANOSECONDS.toMillis(ts.getNanos());
    return new Date(millis);
  } catch (ClassCastException exp) {
    String pattern = "Expecting %s, but found %s";
    throw new MappingException(
        String.format(pattern, TimestampValue.class.getName(), input.getClass().getName()), exp);
  }
}
 
Example #5
Source File: CloudSpannerTransaction.java    From spanner-jdbc with MIT License 6 votes vote down vote up
public Timestamp commit() throws SQLException {
  Timestamp res = null;
  try {
    if (connection.isBatchReadOnly()) {
      if (batchReadOnlyTransaction != null) {
        batchReadOnlyTransaction.close();
      }
    } else if (connection.isReadOnly()) {
      if (readOnlyTransaction != null) {
        readOnlyTransaction.close();
      }
    } else {
      if (transactionThread != null) {
        res = transactionThread.commit();
      }
    }
  } finally {
    transactionThread = null;
    readOnlyTransaction = null;
    batchReadOnlyTransaction = null;
  }
  return res;
}
 
Example #6
Source File: AvroRecordConverter.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private Optional<Timestamp> readTimestamp(
    GenericRecord record, Schema.Type avroType, LogicalType logicalType, String fieldName) {
  switch (avroType) {
    case LONG:
      if (LogicalTypes.timestampMillis().equals(logicalType)) {
        return Optional.ofNullable((Long) record.get(fieldName))
            .map(x -> Timestamp.ofTimeMicroseconds(1000L * x));
      }
      if (LogicalTypes.timestampMicros().equals(logicalType)) {
        return Optional.ofNullable((Long) record.get(fieldName))
            .map(Timestamp::ofTimeMicroseconds);
      }
      // Default to micro-seconds.
      return Optional.ofNullable((Long) record.get(fieldName)).map(Timestamp::ofTimeMicroseconds);
    case STRING:
      return Optional.ofNullable((Utf8) record.get(fieldName))
          .map(Utf8::toString)
          .map(Timestamp::parseTimestamp);
    default:
      throw new IllegalArgumentException("Cannot interpret " + avroType + " as TIMESTAMP");
  }
}
 
Example #7
Source File: ConverterAwareMappingSpannerEntityReaderTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void readUnconvertableValueTest() {
	this.expectedEx.expect(ConversionFailedException.class);
	this.expectedEx.expectMessage("Failed to convert from type [java.lang.String] to type " +
			"[java.lang.Double] for value 'UNCONVERTABLE VALUE'; nested exception is " +
			"java.lang.NumberFormatException: For input string: \"UNCONVERTABLEVALUE\"");
	Struct struct = Struct.newBuilder().set("id").to(Value.string("key1")).set("id2")
			.to(Value.string("key2")).set("id3").to(Value.string("key3")).set("id4")
			.to(Value.string("key4")).set("intField2").to(Value.int64(333L))
			.set("custom_col").to(Value.string("WHITE")).set("booleanField")
			.to(Value.bool(true)).set("longField").to(Value.int64(3L))
			.set("doubleField").to(Value.string("UNCONVERTABLE VALUE"))
			.set("doubleArray")
			.to(Value.float64Array(new double[] { 3.33, 3.33, 3.33 }))
			.set("dateField").to(Value.date(Date.fromYearMonthDay(2018, 11, 22)))
			.set("timestampField")
			.to(Value.timestamp(Timestamp.ofTimeMicroseconds(333))).set("bytes")
			.to(Value.bytes(ByteArray.copyFrom("string1"))).build();

	this.spannerEntityReader.read(TestEntity.class, struct);
}
 
Example #8
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void readNullTest() {
	byte[] bytes = { 1, 2, 3 };
	Entity entity = getEntityBuilder()
			.set("durationField", "PT24H")
			.set("stringField", new NullValue())
			.set("boolField", true)
			.set("doubleField", 3.1415D)
			.set("longField", 123L)
			.set("latLngField", LatLng.of(10, 20))
			.set("timestampField", Timestamp.ofTimeSecondsAndNanos(30, 40))
			.set("blobField", Blob.copyFrom(bytes))
			.set("intField", 99)
			.set("enumField", "BLACK")
			.build();
	TestDatastoreItem item = ENTITY_CONVERTER.read(TestDatastoreItem.class, entity);

	assertThat(item.getStringField()).as("validate null field").isNull();
}
 
Example #9
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void writeNullTest() {
	byte[] bytes = { 1, 2, 3 };
	TestDatastoreItem item = new TestDatastoreItem();
	item.setStringField(null);
	item.setBoolField(true);
	item.setDoubleField(3.1415D);
	item.setLongField(123L);
	item.setLatLngField(LatLng.of(10, 20));
	item.setTimestampField(Timestamp.ofTimeSecondsAndNanos(30, 40));
	item.setBlobField(Blob.copyFrom(bytes));

	Entity.Builder builder = getEntityBuilder();
	ENTITY_CONVERTER.write(item, builder);

	Entity entity = builder.build();

	assertThat(entity.getValue("stringField").equals(new NullValue()))
			.as("validate null field").isTrue();
}
 
Example #10
Source File: CommitTimestampIntegrationTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommitTimestamp() {

	final CommitTimestamps entity = new CommitTimestamps();
	final String id = UUID.randomUUID().toString();
	entity.id = id;

	doWithFields(CommitTimestamps.class,
			f -> setField(f, entity, CommitTimestamp.of(f.getType())),
			ff -> !ff.isSynthetic() && Objects.isNull(ff.getAnnotation(PrimaryKey.class)));

	final Timestamp committedAt = databaseClient.write(mutationFactory.insert(entity));

	final CommitTimestamps fetched = spannerOperations.read(CommitTimestamps.class, Key.of(id));
	doWithFields(CommitTimestamps.class,
			f -> assertThat(getField(f, fetched))
					.describedAs("Test of the field %s has tailed", f)
					.isEqualTo(getConverter(f).convert(committedAt)),
			ff -> !ff.isSynthetic() && isNull(ff.getAnnotation(PrimaryKey.class)));
}
 
Example #11
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends DataModel> void update(UUID jobId, String key, T model) {
  Transaction transaction = datastore.newTransaction();
  Key entityKey = getDataKey(jobId, key);

  try {
    Entity previousEntity = transaction.get(entityKey);
    if (previousEntity == null) {
      throw new IOException("Could not find record for data key: " + entityKey.getName());
    }

    String serialized = objectMapper.writeValueAsString(model);
    Entity entity =
        Entity.newBuilder(entityKey)
            .set(CREATED_FIELD, Timestamp.now())
            .set(model.getClass().getName(), serialized)
            .build();

    transaction.put(entity);
    transaction.commit();
  } catch (IOException t) {
    transaction.rollback();
    throw new RuntimeException("Failed atomic update of key: " + key, t);
  }
}
 
Example #12
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void readOnlyTransactionTest() {

	ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
	when(this.databaseClient.readOnlyTransaction(
			eq(TimestampBound.ofMinReadTimestamp(Timestamp.ofTimeMicroseconds(333)))))
					.thenReturn(readOnlyTransaction);

	String finalResult = this.spannerTemplate
			.performReadOnlyTransaction((spannerOperations) -> {
				List<TestEntity> items = spannerOperations.readAll(TestEntity.class);
				TestEntity item = spannerOperations.read(TestEntity.class,
						Key.of("key"));
				return "all done";
			}, new SpannerReadOptions()
					.setTimestampBound(TimestampBound.ofMinReadTimestamp(Timestamp.ofTimeMicroseconds(333L))));

	assertThat(finalResult).isEqualTo("all done");
	verify(readOnlyTransaction, times(2)).read(eq("custom_test_table"), any(), any());
}
 
Example #13
Source File: SpannerTemplateTests.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
@Test
public void readOnlyTransactionPartitionedDmlTest() {

	this.expectedException.expectMessage("A read-only transaction template cannot execute partitioned" +
			" DML.");

	ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
	when(this.databaseClient.readOnlyTransaction(
			eq(TimestampBound.ofReadTimestamp(Timestamp.ofTimeMicroseconds(333)))))
					.thenReturn(readOnlyTransaction);

	this.spannerTemplate
			.performReadOnlyTransaction((spannerOperations) -> {
				spannerOperations.executePartitionedDmlStatement(Statement.of("fail"));
				return null;
			}, new SpannerReadOptions()
					.setTimestamp(Timestamp.ofTimeMicroseconds(333)));
}
 
Example #14
Source File: Queue.java    From spanner-event-exporter with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a QueueMessage to the spanner queue table
 *
 * <p>Example of sending data to a queue.
 *
 * <pre>{@code
 * MyProto myProto = MyProto.newBuilder().setMessage("My-Message").build();
 *
 * try {
 *   Queue.send(dbClient, queueName, "myKey", ByteArray.copyFrom(myProto.toByteArray()));
 * } catch (SpannerException e) {
 *   log.error("Could not write message to Queue", e);
 * }
 * }</pre>
 *
 * @param dbClient the Spanner database client
 * @param queueName the name of the queue to be polled
 * @param key the name used to partition the passed value for storage and lookup
 * @param value the message payload
 * @return Timestamp the timestamp that the message was written
 */
public static Timestamp send(DatabaseClient dbClient, String queueName, String key, byte[] value)
    throws SpannerException {
  Preconditions.checkNotNull(dbClient);
  Preconditions.checkNotNull(queueName);
  Preconditions.checkNotNull(key);
  Preconditions.checkNotNull(value);

  final List<Mutation> mutations = new ArrayList<>();
  mutations.add(
      Mutation.newInsertBuilder(queueName)
          .set("MessageId")
          .to(UUID.randomUUID().toString())
          .set("Key")
          .to(key)
          .set("Payload")
          .to(ByteArray.copyFrom(value))
          .set("Ack")
          .to(false)
          .set("Timestamp")
          .to(Value.COMMIT_TIMESTAMP)
          .build());

  return dbClient.write(mutations);
}
 
Example #15
Source File: ConverterAwareMappingSpannerEntityProcessorTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void timestampIterableCorrespondingType() {
	ConverterAwareMappingSpannerEntityProcessor converter = new ConverterAwareMappingSpannerEntityProcessor(
			new SpannerMappingContext());

	Class<?> spannerJavaType = converter.getCorrespondingSpannerJavaType(java.sql.Timestamp.class, true);

	assertThat(spannerJavaType).isEqualTo(Timestamp.class);
}
 
Example #16
Source File: ConverterAwareMappingSpannerEntityProcessorTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void canConvertDefaultTypesCustomConverters() {
	ConverterAwareMappingSpannerEntityProcessor converter = new ConverterAwareMappingSpannerEntityProcessor(
			new SpannerMappingContext(), Arrays.asList(JAVA_TO_SPANNER),
			Arrays.asList(SPANNER_TO_JAVA));

	verifyCanConvert(converter, java.util.Date.class, Timestamp.class);
	verifyCanConvert(converter, LocalDate.class, Date.class);
	verifyCanConvert(converter, Instant.class, Timestamp.class);
	verifyCanConvert(converter, JavaType.class, SpannerType.class);
}
 
Example #17
Source File: TaskList.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a task entity to the Datastore.
 *
 * @param description The task description
 * @return The {@link Key} of the entity
 * @throws DatastoreException if the ID allocation or put fails
 */
Key addTask(String description) {
  Key key = datastore.allocateId(keyFactory.newKey());
  Entity task = Entity.newBuilder(key)
      .set("description", StringValue.newBuilder(description).setExcludeFromIndexes(true).build())
      .set("created", Timestamp.now())
      .set("done", false)
      .build();
  datastore.put(task);
  return key;
}
 
Example #18
Source File: ConverterAwareMappingSpannerEntityProcessorTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void canConvertDefaultTypesNoCustomConverters() {
	ConverterAwareMappingSpannerEntityProcessor converter = new ConverterAwareMappingSpannerEntityProcessor(
			new SpannerMappingContext());

	verifyCanConvert(converter, java.util.Date.class, Timestamp.class);
	verifyCanConvert(converter, Instant.class, Timestamp.class);
	verifyCanConvert(converter, LocalDate.class, Date.class);
}
 
Example #19
Source File: OffsetDateTimeMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToDatastore_Now() {
  OffsetDateTimeMapper mapper = new OffsetDateTimeMapper();
  OffsetDateTime now = OffsetDateTime.now();
  Timestamp ts = ((TimestampValue) mapper.toDatastore(now).build()).get();
  assertEquals(now.toEpochSecond(), ts.getSeconds());
  assertEquals(now.getNano(), ts.getNanos());
}
 
Example #20
Source File: CreateEntity.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) {
  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
  Key key = keyFactory.newKey("keyName");
  Entity entity =
      Entity.newBuilder(key)
          .set("name", "John Doe")
          .set("age", 30)
          .set("access_time", Timestamp.now())
          .build();
  datastore.put(entity);
}
 
Example #21
Source File: ConceptsTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testProperties() {
  // [START datastore_properties]
  Entity task = Entity.newBuilder(taskKey)
      .set("category", "Personal")
      .set("created", Timestamp.now())
      .set("done", false)
      .set("priority", 4)
      .set("percent_complete", 10.0)
      .set("description",
        StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build())
      .build();
  // [END datastore_properties]
  assertValidEntity(task);
}
 
Example #22
Source File: TransactionThreadTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Override
public <T> T run(TransactionCallable<T> callable) {
  T res = null;
  try {
    for (int i = 0; i < runs; i++) {
      mock.clearMutations();
      res = callable.run(mock.createTransactionContextMock());
    }
    commitTimestamp = Timestamp.now();
  } catch (Exception e) {
    throw new RuntimeException(e.getMessage(), e);
  }
  return res;
}
 
Example #23
Source File: UpdateEntity.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) {
  Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
  KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
  Key key = keyFactory.newKey("keyName");
  Entity entity = datastore.get(key);
  if (entity != null) {
    System.out.println("Updating access_time for " + entity.getString("name"));
    entity = Entity.newBuilder(entity).set("access_time", Timestamp.now()).build();
    datastore.update(entity);
  }
}
 
Example #24
Source File: MonitoringServlet.java    From tomcat-runtime with Apache License 2.0 5 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  JsonNode body = objectMapper.readTree(req.getReader());
  String name = body.path("name").asText();
  long token = body.path("token").asLong();

  logger.info("Creating Time series with name " + name + " and token " + token);

  MetricServiceClient serviceClient = MetricServiceClient.create();

  TimeSeries timeSeries =
      TimeSeries.newBuilder()
          .addPoints(Point.newBuilder()
              .setValue(TypedValue.newBuilder().setInt64Value(token))
              .setInterval(TimeInterval.newBuilder()
                .setEndTime(Timestamp.now().toProto())))
          .setMetric(Metric.newBuilder().setType(name))
          .build();

  serviceClient.createTimeSeries(
      ProjectName.create(ServiceOptions.getDefaultProjectId()),
      Collections.singletonList(timeSeries));

  resp.setContentType("text/plain");
  resp.getWriter().println("OK");
}
 
Example #25
Source File: EntityPropertyValueProviderTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void getPropertyValue() {
	byte[] bytes = { 1, 2, 3 };
	Entity entity = Entity.newBuilder(this.datastore.newKeyFactory().setKind("aKind").newKey("1"))
			.set("stringField", "string value")
			.set("boolField", true)
			.set("doubleField", 3.1415D)
			.set("longField", 123L)
			.set("latLngField", LatLng.of(10, 20))
			.set("timestampField", Timestamp.ofTimeSecondsAndNanos(30, 40))
			.set("blobField", Blob.copyFrom(bytes))
			.build();

	EntityPropertyValueProvider provider = new EntityPropertyValueProvider(entity, this.twoStepsConversion);

	assertThat((String) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("stringField")))
			.as("validate string field").isEqualTo("string value");
	assertThat((Boolean) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("boolField")))
			.as("validate boolean field").isTrue();
	assertThat((Double) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("doubleField")))
			.as("validate double field").isEqualTo(3.1415D);
	assertThat((Long) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("longField")))
			.as("validate long field").isEqualTo(123L);
	assertThat((LatLng) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("latLngField")))
			.as("validate latLng field").isEqualTo(LatLng.of(10, 20));
	assertThat((Timestamp) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("timestampField")))
			.as("validate timestamp field")
			.isEqualTo(Timestamp.ofTimeSecondsAndNanos(30, 40));
	assertThat((Blob) provider.getPropertyValue(this.persistentEntity.getPersistentProperty("blobField")))
			.as("validate blob field").isEqualTo(Blob.copyFrom(bytes));
}
 
Example #26
Source File: DefaultDatastoreEntityConverterTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void readTest() {
	byte[] bytes = { 1, 2, 3 };
	Key otherKey = Key.newBuilder("testproject", "test_kind", "test_name").build();
	Entity entity = getEntityBuilder()
			.set("durationField", "PT24H")
			.set("stringField", "string value")
			.set("boolField", true)
			.set("doubleField", 3.1415D)
			.set("longField", 123L)
			.set("latLngField", LatLng.of(10, 20))
			.set("timestampField", Timestamp.ofTimeSecondsAndNanos(30, 40))
			.set("blobField", Blob.copyFrom(bytes))
			.set("intField", 99)
			.set("enumField", "WHITE")
			.set("keyField", otherKey)
			.build();
	TestDatastoreItem item = ENTITY_CONVERTER.read(TestDatastoreItem.class, entity);

	assertThat(item.getDurationField()).as("validate duration field").isEqualTo(Duration.ofDays(1));
	assertThat(item.getStringField()).as("validate string field").isEqualTo("string value");
	assertThat(item.getBoolField()).as("validate boolean field").isTrue();
	assertThat(item.getDoubleField()).as("validate double field").isEqualTo(3.1415D);
	assertThat(item.getLongField()).as("validate long field").isEqualTo(123L);
	assertThat(item.getLatLngField()).as("validate latLng field")
			.isEqualTo(LatLng.of(10, 20));
	assertThat(item.getTimestampField()).as("validate timestamp field")
			.isEqualTo(Timestamp.ofTimeSecondsAndNanos(30, 40));
	assertThat(item.getBlobField()).as("validate blob field").isEqualTo(Blob.copyFrom(bytes));
	assertThat(item.getIntField()).as("validate int field").isEqualTo(99);
	assertThat(item.getEnumField()).as("validate enum field").isEqualTo(TestDatastoreItem.Color.WHITE);
	assertThat(item.getKeyField()).as("validate key field").isEqualTo(otherKey);
}
 
Example #27
Source File: CloudSpannerResultSetTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Test
public void testGetTimestampLabelCalendar() throws SQLException {
  Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
  Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
  expected.clear();
  expected.set(2017, 8, 10, 8, 15, 59);

  assertNotNull(subject.getTimestamp(TIMESTAMP_COL_NOT_NULL, cal));
  assertEquals(new java.sql.Timestamp(expected.getTimeInMillis()),
      subject.getTimestamp(TIMESTAMP_COL_NOT_NULL, cal));
  assertEquals(false, subject.wasNull());
  assertNull(subject.getTimestamp(TIMESTAMP_COL_NULL, cal));
  assertTrue(subject.wasNull());
}
 
Example #28
Source File: CloudSpannerResultSetTest.java    From spanner-jdbc with MIT License 5 votes vote down vote up
@Test
public void testGetTimestampIndexCalendar() throws SQLException {
  Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
  Calendar expected = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
  expected.clear();
  expected.set(2017, 8, 11, 8, 15, 59);

  assertNotNull(subject.getTimestamp(TIMESTAMP_COLINDEX_NOTNULL, cal));
  assertEquals(new java.sql.Timestamp(expected.getTimeInMillis()),
      subject.getTimestamp(TIMESTAMP_COLINDEX_NOTNULL, cal));
  assertEquals(false, subject.wasNull());
  assertNull(subject.getTimestamp(TIMESTAMP_COLINDEX_NULL, cal));
  assertTrue(subject.wasNull());
}
 
Example #29
Source File: OffsetDateTimeMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_Now() {
  Calendar now = Calendar.getInstance();
  TimestampValue v = TimestampValue.newBuilder(Timestamp.of(now.getTime())).build();
  OffsetDateTimeMapper mapper = new OffsetDateTimeMapper();
  OffsetDateTime output = (OffsetDateTime) mapper.toModel(v);
  assertTrue(now.getTimeInMillis() == output.toInstant().toEpochMilli());
}
 
Example #30
Source File: OffsetDateTimeMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  OffsetDateTime offsetDateTime = (OffsetDateTime) input;
  long seconds = offsetDateTime.toEpochSecond();
  int nanos = offsetDateTime.getNano();
  long microseconds = TimeUnit.SECONDS.toMicros(seconds) + TimeUnit.NANOSECONDS.toMicros(nanos);
  return TimestampValue.newBuilder(Timestamp.ofTimeMicroseconds(microseconds));
}