Search in sources :

Example 1 with JsonPayload

use of com.google.cloud.logging.Payload.JsonPayload in project google-cloud-java by GoogleCloudPlatform.

the class PayloadTest method testOf.

@Test
public void testOf() {
    assertEquals(Type.STRING, STRING_PAYLOAD.getType());
    assertEquals(STRING_DATA, STRING_PAYLOAD.getData());
    assertEquals(Type.JSON, JSON_PAYLOAD.getType());
    assertEquals(STRUCT_DATA, JSON_PAYLOAD.getData());
    assertEquals(JSON_DATA, JSON_PAYLOAD.getDataAsMap());
    assertEquals(Type.PROTO, PROTO_PAYLOAD.getType());
    assertEquals(PROTO_DATA, PROTO_PAYLOAD.getData());
    JsonPayload jsonPayload = JsonPayload.of(STRUCT_DATA);
    assertEquals(Type.JSON, jsonPayload.getType());
    assertEquals(STRUCT_DATA, jsonPayload.getData());
    assertEquals(JSON_DATA, jsonPayload.getDataAsMap());
}
Also used : JsonPayload(com.google.cloud.logging.Payload.JsonPayload) Test(org.junit.Test)

Example 2 with JsonPayload

use of com.google.cloud.logging.Payload.JsonPayload in project google-cloud-java by GoogleCloudPlatform.

the class BaseSystemTest method testWriteAndListLogEntries.

@Test
public void testWriteAndListLogEntries() throws InterruptedException {
    String logName = formatForTest("test-write-log-entries-log");
    String filter = "logName = projects/" + logging().getOptions().getProjectId() + "/logs/" + logName;
    StringPayload firstPayload = StringPayload.of("stringPayload");
    LogEntry firstEntry = LogEntry.newBuilder(firstPayload).addLabel("key1", "value1").setLogName(logName).setHttpRequest(HttpRequest.newBuilder().setStatus(500).build()).setResource(MonitoredResource.newBuilder("global").build()).build();
    JsonPayload secondPayload = JsonPayload.of(ImmutableMap.<String, Object>of("jsonKey", "jsonValue"));
    LogEntry secondEntry = LogEntry.newBuilder(secondPayload).addLabel("key2", "value2").setLogName(logName).setOperation(Operation.of("operationId", "operationProducer")).setResource(MonitoredResource.newBuilder("cloudsql_database").build()).build();
    logging().write(ImmutableList.of(firstEntry));
    logging().write(ImmutableList.of(secondEntry));
    EntryListOption[] options = { EntryListOption.filter(filter), EntryListOption.pageSize(1) };
    Page<LogEntry> page = logging().listLogEntries(options);
    while (Iterators.size(page.iterateAll().iterator()) < 2) {
        Thread.sleep(500);
        page = logging().listLogEntries(options);
    }
    Iterator<LogEntry> iterator = page.iterateAll().iterator();
    assertTrue(iterator.hasNext());
    LogEntry entry = iterator.next();
    assertEquals(firstPayload, entry.getPayload());
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("key1", "value1"), entry.getLabels());
    assertEquals("global", entry.getResource().getType());
    assertEquals(HttpRequest.newBuilder().setStatus(500).build(), entry.getHttpRequest());
    assertEquals(Severity.DEFAULT, entry.getSeverity());
    assertNull(entry.getOperation());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertTrue(iterator.hasNext());
    entry = iterator.next();
    assertEquals(secondPayload, entry.getPayload());
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("key2", "value2"), entry.getLabels());
    assertEquals("cloudsql_database", entry.getResource().getType());
    assertEquals(Operation.of("operationId", "operationProducer"), entry.getOperation());
    assertEquals(Severity.DEFAULT, entry.getSeverity());
    assertNull(entry.getHttpRequest());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    options = new EntryListOption[] { EntryListOption.filter(filter), EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.DESCENDING) };
    page = logging().listLogEntries(options);
    while (Iterators.size(page.iterateAll().iterator()) < 2) {
        Thread.sleep(500);
        page = logging().listLogEntries(options);
    }
    iterator = page.iterateAll().iterator();
    Long lastTimestamp = iterator.next().getTimestamp();
    while (iterator.hasNext()) {
        assertTrue(iterator.next().getTimestamp() <= lastTimestamp);
    }
    assertTrue(logging().deleteLog(logName));
}
Also used : StringPayload(com.google.cloud.logging.Payload.StringPayload) JsonPayload(com.google.cloud.logging.Payload.JsonPayload) EntryListOption(com.google.cloud.logging.Logging.EntryListOption) Test(org.junit.Test)

Aggregations

JsonPayload (com.google.cloud.logging.Payload.JsonPayload)2 Test (org.junit.Test)2 EntryListOption (com.google.cloud.logging.Logging.EntryListOption)1 StringPayload (com.google.cloud.logging.Payload.StringPayload)1