Search in sources :

Example 1 with ParseEventFn

use of com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn in project DataflowJavaSDK-examples by GoogleCloudPlatform.

the class UserScoreTest method testTeamScoreSums.

/** Tests ExtractAndSumScore("team"). */
@Test
@Category(ValidatesRunner.class)
public void testTeamScoreSums() throws Exception {
    PCollection<String> input = p.apply(Create.of(GAME_EVENTS).withCoder(StringUtf8Coder.of()));
    PCollection<KV<String, Integer>> output = input.apply(ParDo.of(new ParseEventFn())).apply("ExtractTeamScore", new ExtractAndSumScore("team"));
    // Check the team score sums.
    PAssert.that(output).containsInAnyOrder(TEAM_SUMS);
    p.run().waitUntilFinish();
}
Also used : ExtractAndSumScore(com.google.cloud.dataflow.examples.complete.game.UserScore.ExtractAndSumScore) KV(org.apache.beam.sdk.values.KV) ParseEventFn(com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 2 with ParseEventFn

use of com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn in project DataflowJavaSDK-examples by GoogleCloudPlatform.

the class UserScoreTest method testParseEventFn.

/** Test the {@link ParseEventFn} {@link org.apache.beam.sdk.transforms.DoFn}. */
@Test
public void testParseEventFn() throws Exception {
    DoFnTester<String, GameActionInfo> parseEventFn = DoFnTester.of(new ParseEventFn());
    List<GameActionInfo> results = parseEventFn.processBundle(GAME_EVENTS_ARRAY);
    Assert.assertEquals(results.size(), 8);
    Assert.assertEquals(results.get(0).getUser(), "user0_MagentaKangaroo");
    Assert.assertEquals(results.get(0).getTeam(), "MagentaKangaroo");
    Assert.assertEquals(results.get(0).getScore(), new Integer(3));
}
Also used : GameActionInfo(com.google.cloud.dataflow.examples.complete.game.UserScore.GameActionInfo) ParseEventFn(com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn) Test(org.junit.Test)

Example 3 with ParseEventFn

use of com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn in project DataflowJavaSDK-examples by GoogleCloudPlatform.

the class HourlyTeamScoreTest method testUserScoresFilter.

/** Test the filtering. */
@Test
@Category(ValidatesRunner.class)
public void testUserScoresFilter() throws Exception {
    final Instant startMinTimestamp = new Instant(1447965680000L);
    PCollection<String> input = p.apply(Create.of(GAME_EVENTS).withCoder(StringUtf8Coder.of()));
    PCollection<KV<String, Integer>> output = input.apply("ParseGameEvent", ParDo.of(new ParseEventFn())).apply("FilterStartTime", Filter.by((GameActionInfo gInfo) -> gInfo.getTimestamp() > startMinTimestamp.getMillis())).apply(MapElements.into(TypeDescriptors.kvs(TypeDescriptors.strings(), TypeDescriptors.integers())).via((GameActionInfo gInfo) -> KV.of(gInfo.getUser(), gInfo.getScore())));
    PAssert.that(output).containsInAnyOrder(FILTERED_EVENTS);
    p.run().waitUntilFinish();
}
Also used : GameActionInfo(com.google.cloud.dataflow.examples.complete.game.UserScore.GameActionInfo) Instant(org.joda.time.Instant) KV(org.apache.beam.sdk.values.KV) ParseEventFn(com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 4 with ParseEventFn

use of com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn in project DataflowJavaSDK-examples by GoogleCloudPlatform.

the class UserScoreTest method testUserScoreSums.

/** Tests ExtractAndSumScore("user"). */
@Test
@Category(ValidatesRunner.class)
public void testUserScoreSums() throws Exception {
    PCollection<String> input = p.apply(Create.of(GAME_EVENTS).withCoder(StringUtf8Coder.of()));
    PCollection<KV<String, Integer>> output = input.apply(ParDo.of(new ParseEventFn())).apply("ExtractUserScore", new ExtractAndSumScore("user"));
    // Check the user score sums.
    PAssert.that(output).containsInAnyOrder(USER_SUMS);
    p.run().waitUntilFinish();
}
Also used : ExtractAndSumScore(com.google.cloud.dataflow.examples.complete.game.UserScore.ExtractAndSumScore) KV(org.apache.beam.sdk.values.KV) ParseEventFn(com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 5 with ParseEventFn

use of com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn in project DataflowJavaSDK-examples by GoogleCloudPlatform.

the class UserScoreTest method testUserScoresBadInput.

/** Test that bad input data is dropped appropriately. */
@Test
@Category(ValidatesRunner.class)
public void testUserScoresBadInput() throws Exception {
    PCollection<String> input = p.apply(Create.of(GAME_EVENTS2).withCoder(StringUtf8Coder.of()));
    PCollection<KV<String, Integer>> extract = input.apply(ParDo.of(new ParseEventFn())).apply(MapElements.into(TypeDescriptors.kvs(TypeDescriptors.strings(), TypeDescriptors.integers())).via((GameActionInfo gInfo) -> KV.of(gInfo.getUser(), gInfo.getScore())));
    PAssert.that(extract).empty();
    p.run().waitUntilFinish();
}
Also used : GameActionInfo(com.google.cloud.dataflow.examples.complete.game.UserScore.GameActionInfo) KV(org.apache.beam.sdk.values.KV) ParseEventFn(com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

ParseEventFn (com.google.cloud.dataflow.examples.complete.game.UserScore.ParseEventFn)5 Test (org.junit.Test)5 KV (org.apache.beam.sdk.values.KV)4 Category (org.junit.experimental.categories.Category)4 GameActionInfo (com.google.cloud.dataflow.examples.complete.game.UserScore.GameActionInfo)3 ExtractAndSumScore (com.google.cloud.dataflow.examples.complete.game.UserScore.ExtractAndSumScore)2 Instant (org.joda.time.Instant)1