Skip to content

Commit

Permalink
Merge 2c01eca into 889776f
Browse files Browse the repository at this point in the history
  • Loading branch information
ssisk committed Jul 13, 2017
2 parents 889776f + 2c01eca commit 720f093
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 286 deletions.
10 changes: 10 additions & 0 deletions sdks/java/io/common/pom.xml
Expand Up @@ -38,5 +38,15 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,114 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.beam.sdk.io.common;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.beam.sdk.transforms.DoFn;

/**
* Used to pass values around within test pipelines.
*/
@AutoValue
public abstract class TestRow implements Serializable, Comparable<TestRow> {
/**
* Manually create a test row.
*/
public static TestRow create(Integer id, String name) {
return new AutoValue_TestRow(id, name);
}

public abstract Integer id();
public abstract String name();

public int compareTo(TestRow other) {
return id().compareTo(other.id());
}

/**
* Creates a {@link org.apache.beam.sdk.io.common.TestRow} from the seed value.
*/
public static TestRow fromSeed(Integer seed) {
return create(seed, getNameForSeed(seed));
}

/**
* Returns the name field value produced from the given seed.
*/
public static String getNameForSeed(Integer seed) {
return "Testval" + seed;
}

/**
* Returns a range of {@link org.apache.beam.sdk.io.common.TestRow}s for seed values between
* rangeStart (inclusive) and rangeEnd (exclusive).
*/
public static Iterable<TestRow> getExpectedValues(int rangeStart, int rangeEnd) {
List<TestRow> ret = new ArrayList<TestRow>(rangeEnd - rangeStart + 1);
for (int i = rangeStart; i < rangeEnd; i++) {
ret.add(fromSeed(i));
}
return ret;
}

/**
* Uses the input Long values as seeds to produce {@link org.apache.beam.sdk.io.common.TestRow}s.
*/
public static class DeterministicallyConstructTestRowFn extends DoFn<Long, TestRow> {
@ProcessElement
public void processElement(ProcessContext c) {
c.output(fromSeed(c.element().intValue()));
}
}

/**
* Outputs just the name stored in the {@link org.apache.beam.sdk.io.common.TestRow}.
*/
public static class SelectNameFn extends DoFn<TestRow, String> {
@ProcessElement
public void processElement(ProcessContext c) {
c.output(c.element().name());
}
}

/**
* Precalculated hashes - you can calculate an entry by running HashingFn on
* the name() for the rows generated from seeds in [0, n).
*/
private static final Map<Integer, String> EXPECTED_HASHES = ImmutableMap.of(
1000, "7d94d63a41164be058a9680002914358"
);

/**
* Returns the hash value that {@link org.apache.beam.sdk.io.common.HashingFn} will return when it
* is run on {@link org.apache.beam.sdk.io.common.TestRow}s produced by
* getExpectedValues(0, rowCount).
*/
public static String getExpectedHashForRowCount(int rowCount)
throws UnsupportedOperationException {
String hash = EXPECTED_HASHES.get(rowCount);
if (hash == null) {
throw new UnsupportedOperationException("No hash for that row count");
}
return hash;
}
}
10 changes: 5 additions & 5 deletions sdks/java/io/jdbc/pom.xml
Expand Up @@ -105,11 +105,6 @@
<version>2.1.1</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>

<!-- compile dependencies -->
<dependency>
<groupId>com.google.auto.value</groupId>
Expand Down Expand Up @@ -168,5 +163,10 @@
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-io-common</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

0 comments on commit 720f093

Please sign in to comment.