Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dependencies {
implementation library.java.proto_google_cloud_secret_manager_v1
implementation library.java.protobuf_java
testImplementation project(path: ":sdks:java:core", configuration: "shadowTest")
testImplementation project(path: ":runners:direct-java", configuration: "shadow")
testImplementation library.java.mockito_core
testRuntimeOnly library.java.slf4j_jdk14
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.coders.KvCoder;
Expand All @@ -41,10 +43,13 @@
import org.apache.beam.sdk.transforms.Combine;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.GroupByKey;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.transforms.Redistribute;
import org.apache.beam.sdk.transforms.Sum;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.TypeDescriptor;
import org.apache.beam.sdk.values.TypeDescriptors;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
Expand Down Expand Up @@ -141,6 +146,22 @@ public static void tearDown() throws IOException {
}
}

private static <K, V extends Comparable<? super V>> PCollection<KV<K, List<V>>> sortGroupedValues(
PCollection<KV<K, Iterable<V>>> input,
TypeDescriptor<K> keyType,
TypeDescriptor<V> valueType) {

return input.apply(
MapElements.into(TypeDescriptors.kvs(keyType, TypeDescriptors.lists(valueType)))
.via(
kv -> {
List<V> values = new ArrayList<>();
kv.getValue().forEach(values::add);
Collections.sort(values);
return KV.of(kv.getKey(), values);
}));
}

@Test
public void testGroupByKeyWithValidGcpSecretOption() throws Exception {
if (gcpSecretVersionName == null) {
Expand All @@ -165,13 +186,17 @@ public void testGroupByKeyWithValidGcpSecretOption() throws Exception {
Create.of(ungroupedPairs)
.withCoder(KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of())));

PCollection<KV<String, Iterable<Integer>>> output = input.apply(GroupByKey.create());
PCollection<KV<String, List<Integer>>> normalizedOutput =
sortGroupedValues(
input.apply(GroupByKey.create()),
TypeDescriptors.strings(),
TypeDescriptors.integers());

PAssert.that(output)
PAssert.that(normalizedOutput)
.containsInAnyOrder(
KV.of("k1", Arrays.asList(3, 4)),
KV.of("k5", Arrays.asList(Integer.MAX_VALUE, Integer.MIN_VALUE)),
KV.of("k2", Arrays.asList(66, -33)),
KV.of("k5", Arrays.asList(Integer.MIN_VALUE, Integer.MAX_VALUE)),
KV.of("k2", Arrays.asList(-33, 66)),
KV.of("k3", Arrays.asList(0)));

p.run();
Expand Down Expand Up @@ -201,13 +226,17 @@ public void testGroupByKeyWithValidGcpHsmGeneratedSecretOption() throws Exceptio
Create.of(ungroupedPairs)
.withCoder(KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of())));

PCollection<KV<String, Iterable<Integer>>> output = input.apply(GroupByKey.create());
PCollection<KV<String, List<Integer>>> normalizedOutput =
sortGroupedValues(
input.apply(GroupByKey.create()),
TypeDescriptors.strings(),
TypeDescriptors.integers());

PAssert.that(output)
PAssert.that(normalizedOutput)
.containsInAnyOrder(
KV.of("k1", Arrays.asList(3, 4)),
KV.of("k5", Arrays.asList(Integer.MAX_VALUE, Integer.MIN_VALUE)),
KV.of("k2", Arrays.asList(66, -33)),
KV.of("k5", Arrays.asList(Integer.MIN_VALUE, Integer.MAX_VALUE)),
KV.of("k2", Arrays.asList(-33, 66)),
KV.of("k3", Arrays.asList(0)));

p.run();
Expand Down Expand Up @@ -240,13 +269,17 @@ public void testGroupByKeyWithExistingGcpHsmGeneratedSecretOption() throws Excep
Create.of(ungroupedPairs)
.withCoder(KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of())));

PCollection<KV<String, Iterable<Integer>>> output = input.apply(GroupByKey.create());
PCollection<KV<String, List<Integer>>> normalizedOutput =
sortGroupedValues(
input.apply(GroupByKey.create()),
TypeDescriptors.strings(),
TypeDescriptors.integers());

PAssert.that(output)
PAssert.that(normalizedOutput)
.containsInAnyOrder(
KV.of("k1", Arrays.asList(3, 4)),
KV.of("k5", Arrays.asList(Integer.MAX_VALUE, Integer.MIN_VALUE)),
KV.of("k2", Arrays.asList(66, -33)),
KV.of("k5", Arrays.asList(Integer.MIN_VALUE, Integer.MAX_VALUE)),
KV.of("k2", Arrays.asList(-33, 66)),
KV.of("k3", Arrays.asList(0)));

p.run();
Expand Down
Loading