Skip to content

Commit

Permalink
Refactor field selection and helpers
Browse files Browse the repository at this point in the history
- Add FieldSelector.SelectorHelper with static methods to create selectors
- Add SelectorHelperTest
- Rename dns.AbstractOption to Option, make other Option classes abstract
- Add OptionTest class to resource manager and refactor other OptionTest classes
  • Loading branch information
mziccard committed Mar 30, 2016
1 parent 712b255 commit 1710a4c
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.gcloud.FieldSelector;
import com.google.gcloud.FieldSelector.SelectorHelper;
import com.google.gcloud.Page;
import com.google.gcloud.Service;
import com.google.gcloud.bigquery.spi.BigQueryRpc;
Expand Down Expand Up @@ -192,7 +193,7 @@ private DatasetOption(BigQueryRpc.Option option, Object value) {
*/
public static DatasetOption fields(DatasetField... fields) {
return new DatasetOption(BigQueryRpc.Option.FIELDS,
selector(DatasetField.REQUIRED_FIELDS, fields));
SelectorHelper.selector(DatasetField.REQUIRED_FIELDS, fields));
}
}

Expand Down Expand Up @@ -262,7 +263,7 @@ private TableOption(BigQueryRpc.Option option, Object value) {
*/
public static TableOption fields(TableField... fields) {
return new TableOption(BigQueryRpc.Option.FIELDS,
selector(TableField.REQUIRED_FIELDS, fields));
SelectorHelper.selector(TableField.REQUIRED_FIELDS, fields));
}
}

Expand Down Expand Up @@ -359,10 +360,9 @@ public static JobListOption pageToken(String pageToken) {
* listing jobs.
*/
public static JobListOption fields(JobField... fields) {
StringBuilder builder =
selector(new StringBuilder().append("etag,jobs("), JobField.REQUIRED_FIELDS, fields)
.append(",state,errorResult),nextPageToken");
return new JobListOption(BigQueryRpc.Option.FIELDS, builder.toString());
return new JobListOption(BigQueryRpc.Option.FIELDS,
SelectorHelper.selector("jobs", JobField.REQUIRED_FIELDS, fields, "state",
"errorResult"));
}
}

Expand All @@ -386,7 +386,7 @@ private JobOption(BigQueryRpc.Option option, Object value) {
*/
public static JobOption fields(JobField... fields) {
return new JobOption(BigQueryRpc.Option.FIELDS,
Option.selector(JobField.REQUIRED_FIELDS, fields));
SelectorHelper.selector(JobField.REQUIRED_FIELDS, fields));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Sets;
import com.google.gcloud.FieldSelector;
import com.google.gcloud.bigquery.spi.BigQueryRpc;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
* Base class for BigQuery operation option.
*/
class Option implements Serializable {
abstract class Option implements Serializable {

private static final long serialVersionUID = -6647817677804099207L;

Expand Down Expand Up @@ -73,20 +68,4 @@ public String toString() {
.add("value", value)
.toString();
}

static String selector(List<FieldSelector> required, FieldSelector... others) {
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length);
for (FieldSelector field : required) {
fieldStrings.add(field.selector());
}
for (FieldSelector field : others) {
fieldStrings.add(field.selector());
}
return Joiner.on(',').join(fieldStrings);
}

static StringBuilder selector(StringBuilder partialSelector, List<FieldSelector> required,
FieldSelector... others) {
return partialSelector.append(selector(required, others));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -886,12 +886,14 @@ public com.google.api.services.bigquery.model.Job apply(Job job) {
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(jobList.toArray(), Iterables.toArray(page.values(), Job.class));
String selector = (String) capturedOptions.getValue().get(JOB_OPTION_FIELDS.rpcOption());
assertTrue(selector.contains("etag,jobs("));
assertTrue(selector.contains("nextPageToken,jobs("));
assertTrue(selector.contains("configuration"));
assertTrue(selector.contains("jobReference"));
assertTrue(selector.contains("statistics"));
assertTrue(selector.contains("state,errorResult),nextPageToken"));
assertEquals(80, selector.length());
assertTrue(selector.contains("state"));
assertTrue(selector.contains("errorResult"));
assertTrue(selector.contains(")"));
assertEquals(75, selector.length());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,49 @@
package com.google.gcloud.bigquery;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;

import com.google.gcloud.bigquery.spi.BigQueryRpc;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class OptionTest {

private static final BigQueryRpc.Option RPC_OPTION = BigQueryRpc.Option.PAGE_TOKEN;
private static final BigQueryRpc.Option ANOTHER_RPC_OPTION = BigQueryRpc.Option.FIELDS;
private static final String VALUE = "some value";
private static final String OTHER_VALUE = "another value";
private static final Option OPTION = new Option(RPC_OPTION, VALUE) {};
private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {};
private static final Option OPTION_NOT_EQUALS1 = new Option(RPC_OPTION, OTHER_VALUE) {};
private static final Option OPTION_NOT_EQUALS2 = new Option(ANOTHER_RPC_OPTION, VALUE) {};

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testEquals() {
assertEquals(OPTION, OPTION_EQUALS);
assertNotEquals(OPTION, OPTION_NOT_EQUALS1);
assertNotEquals(OPTION, OPTION_NOT_EQUALS2);
}

@Test
public void testOption() {
Option option = new Option(BigQueryRpc.Option.PAGE_TOKEN, "token");
assertEquals(BigQueryRpc.Option.PAGE_TOKEN, option.rpcOption());
assertEquals("token", option.value());
public void testHashCode() {
assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode());
}

@Test(expected = NullPointerException.class)
public void testNullRpcOption() {
new Option(null, "token");
@Test
public void testConstructor() {
assertEquals(RPC_OPTION, OPTION.rpcOption());
assertEquals(VALUE, OPTION.value());
Option option = new Option(RPC_OPTION, null) {};
assertEquals(RPC_OPTION, option.rpcOption());
assertNull(option.value());
thrown.expect(NullPointerException.class);
new Option(null, VALUE) {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@

package com.google.gcloud;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
* Interface for Google Cloud resource's fields. Implementations of this interface can be used to
* select only desired fields when getting or listing Google Cloud resources.
* select only desired fields from a returned Google Cloud resource.
*/
public interface FieldSelector {

Expand All @@ -27,4 +36,61 @@ public interface FieldSelector {
* other field selectors) to specify which resource fields should be returned by an API call.
*/
String selector();

/**
* A helper class used to build composite selectors given a number of fields. This class is not
* supposed to be used directly by users.
*/
class SelectorHelper {

private SelectorHelper() {}

private static final Function<FieldSelector, String> FIELD_TO_STRING_FUNCTION =
new Function<FieldSelector, String>() {
@Override
public String apply(FieldSelector fieldSelector) {
return fieldSelector.selector();
}
};

private static String selector(List<FieldSelector> required, FieldSelector[] others,
String... extraResourceFields) {
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(required.size() + others.length);
fieldStrings.addAll(Lists.transform(required, FIELD_TO_STRING_FUNCTION));
fieldStrings.addAll(Lists.transform(Arrays.asList(others), FIELD_TO_STRING_FUNCTION));
fieldStrings.addAll(Arrays.asList(extraResourceFields));
return Joiner.on(',').join(fieldStrings);
}

/**
* Returns a composite selector given a number of fields. The string selector returned by this
* method can be used for field selection in API calls that return a single resource. This
* method is not supposed to be used directly by users.
*/
public static String selector(List<FieldSelector> required, FieldSelector... others) {
return selector(required, others, new String[]{});
}

/**
* Returns a composite selector given a number of fields and a container name. The string
* selector returned by this method can be used for field selection in API calls that return a
* list of resources. This method is not supposed to be used directly by users.
*/
public static String selector(String containerName, List<FieldSelector> required,
FieldSelector... others) {
return "nextPageToken," + containerName + '(' + selector(required, others) + ')';
}

/**
* Returns a composite selector given a number of fields and a container name. This methods also
* takes an {@code extraResourceFields} parameter to specify some extra fields as strings. The
* string selector returned by this method can be used for field selection in API calls that
* return a list of resources. This method is not supposed to be used directly by users.
*/
public static String selector(String containerName, List<FieldSelector> required,
FieldSelector[] others, String... extraResourceFields) {
return "nextPageToken," + containerName + '('
+ selector(required, others, extraResourceFields) + ')';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.gcloud;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableList;
import com.google.gcloud.FieldSelector.SelectorHelper;

import org.junit.Test;

import java.util.List;

public class SelectorHelperTest {

private static final FieldSelector FIELD1 = new FieldSelector() {
@Override
public String selector() {
return "field1";
}
};
private static final FieldSelector FIELD2 = new FieldSelector() {
@Override
public String selector() {
return "field2";
}
};
private static final FieldSelector FIELD3 = new FieldSelector() {
@Override
public String selector() {
return "field3";
}
};
private static final List<FieldSelector> REQUIRED_FIELDS = ImmutableList.of(FIELD1, FIELD2);
private static final String CONTAINER = "container";

@Test
public void testSelector() {
String selector = SelectorHelper.selector(REQUIRED_FIELDS, FIELD3);
assertTrue(selector.contains("field1"));
assertTrue(selector.contains("field2"));
assertTrue(selector.contains("field3"));
assertEquals(20, selector.length());
}

@Test
public void testListSelector() {
String selector = SelectorHelper.selector(CONTAINER, REQUIRED_FIELDS, FIELD3);
assertTrue(selector.startsWith("nextPageToken,container("));
assertTrue(selector.contains("field1"));
assertTrue(selector.contains("field2"));
assertTrue(selector.contains("field3"));
assertTrue(selector.endsWith(")"));
assertEquals(45, selector.length());
}
}
Loading

0 comments on commit 1710a4c

Please sign in to comment.