Skip to content

Commit

Permalink
Add SerializationTest for logging and minor fixes (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard authored Jul 19, 2016
1 parent 23bd46a commit b1860bf
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package com.google.cloud;

import com.google.cloud.MonitoredResourceDescriptor.LabelDescriptor;
import com.google.cloud.MonitoredResourceDescriptor.LabelDescriptor.ValueType;
import com.google.cloud.ServiceAccountSigner.SigningException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -39,6 +42,15 @@ public class SerializationTest extends BaseSerializationTest {
private static final SomeIamPolicy SOME_IAM_POLICY = new SomeIamPolicy.Builder().build();
private static final WaitForOption CHECKING_PERIOD =
WaitForOption.checkEvery(42, TimeUnit.SECONDS);
private static final LabelDescriptor LABEL_DESCRIPTOR =
new LabelDescriptor("project_id", ValueType.STRING, "The project id");
private static final MonitoredResourceDescriptor MONITORED_RESOURCE_DESCRIPTOR =
MonitoredResourceDescriptor.builder("global")
.labels(ImmutableList.of(LABEL_DESCRIPTOR))
.build();
private static final MonitoredResource MONITORED_RESOURCE = MonitoredResource.builder("global")
.labels(ImmutableMap.of("project_id", "project"))
.build();
private static final String JSON_KEY = "{\n"
+ " \"private_key_id\": \"somekeyid\",\n"
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
Expand Down Expand Up @@ -91,7 +103,8 @@ public Builder toBuilder() {
@Override
protected Serializable[] serializableObjects() {
return new Serializable[]{BASE_SERVICE_EXCEPTION, EXCEPTION_HANDLER, IDENTITY, PAGE,
RETRY_PARAMS, SOME_IAM_POLICY, SIGNING_EXCEPTION, CHECKING_PERIOD};
RETRY_PARAMS, SOME_IAM_POLICY, SIGNING_EXCEPTION, CHECKING_PERIOD, LABEL_DESCRIPTOR,
MONITORED_RESOURCE_DESCRIPTOR, MONITORED_RESOURCE};
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions gcloud-java-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
<artifactId>auto-value</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class HttpRequest implements Serializable {
/**
* The HTTP request method.
*/
enum RequestMethod {
public enum RequestMethod {
GET,
HEAD,
PUT,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.logging;

import static com.google.cloud.logging.Logging.EntryListOption;
import static com.google.cloud.logging.Logging.SortingField;
import static com.google.cloud.logging.Logging.SortingOrder;
import static com.google.cloud.logging.Logging.WriteOption;
import static org.junit.Assert.assertEquals;

import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.Logging.ListOption;
import com.google.common.collect.ImmutableMap;

import org.junit.Test;

import java.util.Map;

public class LoggingTest {

private static final int PAGE_SIZE = 42;
private static final String PAGE_TOKEN = "page token";
private static final String FILTER = "filter";
private static final Map<String, String> LABELS = ImmutableMap.of("key", "value");
private static final String LOG_NAME = "logName";
private static final MonitoredResource RESOURCE =
MonitoredResource.of("global", ImmutableMap.of("project_id", "p"));

@Test
public void testListOption() {
// page token
ListOption listOption = ListOption.pageToken(PAGE_TOKEN);
assertEquals(PAGE_TOKEN, listOption.value());
assertEquals(ListOption.OptionType.PAGE_TOKEN, listOption.optionType());
// page size
listOption = ListOption.pageSize(PAGE_SIZE);
assertEquals(PAGE_SIZE, listOption.value());
assertEquals(ListOption.OptionType.PAGE_SIZE, listOption.optionType());
}

@Test
public void testEntryListOption() {
EntryListOption listOption = EntryListOption.pageToken(PAGE_TOKEN);
assertEquals(PAGE_TOKEN, listOption.value());
assertEquals(EntryListOption.OptionType.PAGE_TOKEN, listOption.optionType());
// page size
listOption = EntryListOption.pageSize(PAGE_SIZE);
assertEquals(PAGE_SIZE, listOption.value());
assertEquals(EntryListOption.OptionType.PAGE_SIZE, listOption.optionType());
// filter
listOption = EntryListOption.filter(FILTER);
assertEquals(FILTER, listOption.value());
assertEquals(EntryListOption.OptionType.FILTER, listOption.optionType());
// sort order
listOption = EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.ASCENDING);
assertEquals("timestamp asc", listOption.value());
assertEquals(EntryListOption.OptionType.ORDER_BY, listOption.optionType());
listOption = EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.DESCENDING);
assertEquals("timestamp desc", listOption.value());
assertEquals(EntryListOption.OptionType.ORDER_BY, listOption.optionType());
}

@Test
public void testWriteOption() {
WriteOption writeOption = WriteOption.labels(LABELS);
assertEquals(LABELS, writeOption.value());
assertEquals(WriteOption.OptionType.LABELS, writeOption.optionType());
writeOption = WriteOption.logName(LOG_NAME);
assertEquals(LOG_NAME, writeOption.value());
assertEquals(WriteOption.OptionType.LOG_NAME, writeOption.optionType());
writeOption = WriteOption.resource(RESOURCE);
assertEquals(RESOURCE, writeOption.value());
assertEquals(WriteOption.OptionType.RESOURCE, writeOption.optionType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,30 @@ public class MetricTest {
.build();
private final Logging serviceMockReturnsOptions = createStrictMock(Logging.class);
private final LoggingOptions mockOptions = createMock(LoggingOptions.class);
private Logging pubsub;
private Logging logging;
private Metric expectedMetric;
private Metric metric;

private void initializeExpectedMetric(int optionsCalls) {
expect(serviceMockReturnsOptions.options()).andReturn(mockOptions).times(optionsCalls);
replay(serviceMockReturnsOptions);
pubsub = createStrictMock(Logging.class);
logging = createStrictMock(Logging.class);
expectedMetric = new Metric(serviceMockReturnsOptions, new Metric.BuilderImpl(METRIC_INFO));
}

private void initializeMetric() {
metric = new Metric(pubsub, new Metric.BuilderImpl(METRIC_INFO));
metric = new Metric(logging, new Metric.BuilderImpl(METRIC_INFO));
}

@After
public void tearDown() throws Exception {
verify(pubsub, serviceMockReturnsOptions);
verify(logging, serviceMockReturnsOptions);
}

@Test
public void testBuilder() {
initializeExpectedMetric(2);
replay(pubsub);
replay(logging);
MetricInfo builtMetric = expectedMetric.toBuilder()
.name(NEW_NAME)
.filter(NEW_FILTER)
Expand All @@ -83,7 +83,7 @@ public void testBuilder() {
@Test
public void testToBuilder() {
initializeExpectedMetric(2);
replay(pubsub);
replay(logging);
compareMetric(expectedMetric, expectedMetric.toBuilder().build());
}

Expand All @@ -93,9 +93,9 @@ public void testReload() {
MetricInfo updatedInfo = METRIC_INFO.toBuilder().filter(NEW_FILTER).build();
Metric expectedMetric =
new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo));
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.getMetric(NAME)).andReturn(expectedMetric);
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.getMetric(NAME)).andReturn(expectedMetric);
replay(logging);
initializeMetric();
Metric updatedMetric = metric.reload();
compareMetric(expectedMetric, updatedMetric);
Expand All @@ -104,9 +104,9 @@ public void testReload() {
@Test
public void testReloadNull() {
initializeExpectedMetric(1);
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.getMetric(NAME)).andReturn(null);
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.getMetric(NAME)).andReturn(null);
replay(logging);
initializeMetric();
assertNull(metric.reload());
}
Expand All @@ -116,10 +116,10 @@ public void testReloadAsync() throws ExecutionException, InterruptedException {
initializeExpectedMetric(2);
MetricInfo updatedInfo = METRIC_INFO.toBuilder().filter(NEW_FILTER).build();
Metric expectedMetric = new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo));
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.getMetricAsync(NAME))
expect(logging.options()).andReturn(mockOptions);
expect(logging.getMetricAsync(NAME))
.andReturn(Futures.immediateFuture(expectedMetric));
replay(pubsub);
replay(logging);
initializeMetric();
Metric updatedMetric = metric.reloadAsync().get();
compareMetric(expectedMetric, updatedMetric);
Expand All @@ -128,9 +128,9 @@ public void testReloadAsync() throws ExecutionException, InterruptedException {
@Test
public void testReloadAsyncNull() throws ExecutionException, InterruptedException {
initializeExpectedMetric(1);
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.getMetricAsync(NAME)).andReturn(Futures.<Metric>immediateFuture(null));
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.getMetricAsync(NAME)).andReturn(Futures.<Metric>immediateFuture(null));
replay(logging);
initializeMetric();
assertNull(metric.reloadAsync().get());
}
Expand All @@ -140,9 +140,9 @@ public void testUpdate() {
initializeExpectedMetric(2);
MetricInfo updatedInfo = METRIC_INFO.toBuilder().filter(NEW_FILTER).build();
Metric expectedMetric = new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo));
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.update(updatedInfo)).andReturn(expectedMetric);
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.update(updatedInfo)).andReturn(expectedMetric);
replay(logging);
initializeMetric();
Metric updatedMetric = metric.update(updatedInfo);
compareMetric(expectedMetric, updatedMetric);
Expand All @@ -153,9 +153,9 @@ public void testUpdateAsync() throws ExecutionException, InterruptedException {
initializeExpectedMetric(2);
MetricInfo updatedInfo = METRIC_INFO.toBuilder().filter(NEW_FILTER).build();
Metric expectedMetric = new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo));
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.updateAsync(updatedInfo)).andReturn(Futures.immediateFuture(expectedMetric));
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.updateAsync(updatedInfo)).andReturn(Futures.immediateFuture(expectedMetric));
replay(logging);
initializeMetric();
Metric updatedMetric = metric.updateAsync(updatedInfo).get();
compareMetric(expectedMetric, updatedMetric);
Expand All @@ -164,39 +164,39 @@ public void testUpdateAsync() throws ExecutionException, InterruptedException {
@Test
public void testDeleteTrue() {
initializeExpectedMetric(1);
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.deleteMetric(NAME)).andReturn(true);
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.deleteMetric(NAME)).andReturn(true);
replay(logging);
initializeMetric();
assertTrue(metric.delete());
}

@Test
public void testDeleteFalse() {
initializeExpectedMetric(1);
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.deleteMetric(NAME)).andReturn(false);
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.deleteMetric(NAME)).andReturn(false);
replay(logging);
initializeMetric();
assertFalse(metric.delete());
}

@Test
public void testDeleteAsyncTrue() throws ExecutionException, InterruptedException {
initializeExpectedMetric(1);
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.deleteMetricAsync(NAME)).andReturn(Futures.immediateFuture(true));
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.deleteMetricAsync(NAME)).andReturn(Futures.immediateFuture(true));
replay(logging);
initializeMetric();
assertTrue(metric.deleteAsync().get());
}

@Test
public void testDeleteAsyncFalse() throws ExecutionException, InterruptedException {
initializeExpectedMetric(1);
expect(pubsub.options()).andReturn(mockOptions);
expect(pubsub.deleteMetricAsync(NAME)).andReturn(Futures.immediateFuture(false));
replay(pubsub);
expect(logging.options()).andReturn(mockOptions);
expect(logging.deleteMetricAsync(NAME)).andReturn(Futures.immediateFuture(false));
replay(logging);
initializeMetric();
assertFalse(metric.deleteAsync().get());
}
Expand Down
Loading

0 comments on commit b1860bf

Please sign in to comment.