-
Notifications
You must be signed in to change notification settings - Fork 21
/
AddUuidsTransformTest.java
99 lines (92 loc) · 3.67 KB
/
AddUuidsTransformTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2020 Google LLC
*
* 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.pubsublite.beam;
import com.google.cloud.pubsublite.proto.PubSubMessage;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.protobuf.ByteString;
import java.util.HashSet;
import java.util.Set;
import org.apache.beam.sdk.extensions.protobuf.ProtoCoder;
import org.apache.beam.sdk.testing.PAssert;
import org.apache.beam.sdk.testing.TestPipeline;
import org.apache.beam.sdk.testing.TestStream;
import org.apache.beam.sdk.transforms.SerializableFunction;
import org.apache.beam.sdk.values.PCollection;
import org.joda.time.Duration;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
@SuppressWarnings({
"nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
})
public final class AddUuidsTransformTest {
@Rule public final TestPipeline pipeline = TestPipeline.create();
private static PubSubMessage newMessage(int identifier) {
return PubSubMessage.newBuilder()
.setKey(ByteString.copyFromUtf8(Integer.toString(identifier)))
.build();
}
private static SerializableFunction<Iterable<PubSubMessage>, Void> identifiersInAnyOrder(
Set<Integer> identifiers) {
return messages -> {
Set<Uuid> uuids = new HashSet<>();
messages.forEach(
message -> {
int identifier = Integer.parseInt(message.getKey().toStringUtf8());
if (!identifiers.remove(identifier)) {
throw new IllegalStateException("Duplicate element " + identifier);
}
if (!uuids.add(
Uuid.of(
Iterables.getOnlyElement(
message.getAttributesMap().get(Uuid.DEFAULT_ATTRIBUTE).getValuesList())))) {
throw new IllegalStateException("Invalid duplicate Uuid: " + message.toString());
}
});
if (!identifiers.isEmpty()) {
throw new IllegalStateException("Elements not in collection: " + identifiers);
}
return null;
};
}
@Test
public void messagesSameBatch() {
TestStream<PubSubMessage> messageStream =
TestStream.create(ProtoCoder.of(PubSubMessage.class))
.addElements(newMessage(1), newMessage(2), newMessage(85))
.advanceWatermarkToInfinity();
PCollection<PubSubMessage> outputs =
pipeline.apply(messageStream).apply(new AddUuidsTransform());
PAssert.that(outputs).satisfies(identifiersInAnyOrder(Sets.newHashSet(1, 2, 85)));
pipeline.run();
}
@Test
public void messagesTimeDelayed() {
TestStream<PubSubMessage> messageStream =
TestStream.create(ProtoCoder.of(PubSubMessage.class))
.addElements(newMessage(1), newMessage(2))
.advanceProcessingTime(Duration.standardDays(1))
.addElements(newMessage(85))
.advanceWatermarkToInfinity();
PCollection<PubSubMessage> outputs =
pipeline.apply(messageStream).apply(new AddUuidsTransform());
PAssert.that(outputs).satisfies(identifiersInAnyOrder(Sets.newHashSet(1, 2, 85)));
pipeline.run();
}
}