Search in sources :

Example 1 with EntityJsonPrinter

use of com.google.cloud.teleport.templates.common.DatastoreConverters.EntityJsonPrinter in project DataflowTemplates by GoogleCloudPlatform.

the class DatastoreConvertersTest method testCheckSameKey.

/**
 * Unit test for {@link DatastoreConverters.CheckSameKey}.
 */
@Test
@Category(NeedsRunner.class)
public void testCheckSameKey() throws Exception {
    Entity dupKeyEntity = Entity.newBuilder().setKey(entities.get(0).getKey()).putProperties("SomeBSProp", Value.newBuilder().setStringValue("Some BS Value").build()).build();
    // copy all entities
    ArrayList<Entity> testEntitiesWithConflictKey = new ArrayList<>(entities);
    // Add the duplicate entity at the end of the list
    testEntitiesWithConflictKey.add(dupKeyEntity);
    List<String> expectedErrors = new ArrayList<>();
    EntityJsonPrinter entityJsonPrinter = new EntityJsonPrinter();
    for (Entity e : Arrays.asList(entities.get(0), dupKeyEntity)) {
        expectedErrors.add(ErrorMessage.newBuilder().setMessage("Duplicate Datastore Key").setData(entityJsonPrinter.print(e)).build().toJson());
    }
    TupleTag<Entity> goodTag = new TupleTag<Entity>("entities") {
    };
    TupleTag<String> errorTag = new TupleTag<String>("errors") {
    };
    PCollectionTuple results = pipeline.apply("Create", Create.of(testEntitiesWithConflictKey)).apply("RemoveDupKeys", CheckSameKey.newBuilder().setGoodTag(goodTag).setErrorTag(errorTag).build());
    PAssert.that(results.get(goodTag)).containsInAnyOrder(entities.subList(1, entities.size()));
    PAssert.that(results.get(errorTag)).containsInAnyOrder(expectedErrors);
    pipeline.run();
}
Also used : JsonToEntity(com.google.cloud.teleport.templates.common.DatastoreConverters.JsonToEntity) Entity(com.google.datastore.v1.Entity) EntityJsonPrinter(com.google.cloud.teleport.templates.common.DatastoreConverters.EntityJsonPrinter) ArrayList(java.util.ArrayList) TupleTag(org.apache.beam.sdk.values.TupleTag) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 2 with EntityJsonPrinter

use of com.google.cloud.teleport.templates.common.DatastoreConverters.EntityJsonPrinter in project DataflowTemplates by GoogleCloudPlatform.

the class DatastoreConvertersTest method testCheckNoKeyAllInvalid.

/**
 * Test {@link DatastoreConverters.CheckNoKey} with only invalid entities.
 */
@Test
@Category(NeedsRunner.class)
public void testCheckNoKeyAllInvalid() throws Exception {
    // Create test data
    List<Entity> testEntitiesWithNoKey = new ArrayList<>();
    List<String> expectedErrors = new ArrayList<>();
    EntityJsonPrinter entityJsonPrinter = new EntityJsonPrinter();
    for (int i = 0; i < entities.size(); i++) {
        Entity noKeyEntity = Entity.newBuilder().putProperties("street", Value.newBuilder().setStringValue("Some street").build()).putProperties("number", Value.newBuilder().setIntegerValue(1L).build()).build();
        testEntitiesWithNoKey.add(noKeyEntity);
        expectedErrors.add(ErrorMessage.newBuilder().setMessage("Datastore Entity Without Key").setData(entityJsonPrinter.print(noKeyEntity)).build().toJson());
    }
    // Run the test
    TupleTag<Entity> successTag = new TupleTag<Entity>("entities") {
    };
    TupleTag<String> failureTag = new TupleTag<String>("failures") {
    };
    PCollectionTuple results = pipeline.apply("Create", Create.of(testEntitiesWithNoKey)).apply("RemoveNoKeys", CheckNoKey.newBuilder().setSuccessTag(successTag).setFailureTag(failureTag).build());
    // Check the results
    PAssert.that(results.get(successTag)).empty();
    PAssert.that(results.get(failureTag)).containsInAnyOrder(expectedErrors);
    pipeline.run();
}
Also used : JsonToEntity(com.google.cloud.teleport.templates.common.DatastoreConverters.JsonToEntity) Entity(com.google.datastore.v1.Entity) EntityJsonPrinter(com.google.cloud.teleport.templates.common.DatastoreConverters.EntityJsonPrinter) ArrayList(java.util.ArrayList) TupleTag(org.apache.beam.sdk.values.TupleTag) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 3 with EntityJsonPrinter

use of com.google.cloud.teleport.templates.common.DatastoreConverters.EntityJsonPrinter in project DataflowTemplates by GoogleCloudPlatform.

the class DatastoreConvertersTest method testCheckNoKeyBothCorrectAndInvalid.

/**
 * Test {@link DatastoreConverters.CheckNoKey} with both correct and invalid entities.
 */
@Test
@Category(NeedsRunner.class)
public void testCheckNoKeyBothCorrectAndInvalid() throws Exception {
    // Create test data
    List<Entity> testEntitiesWithNoKey = new ArrayList<>();
    List<String> expectedErrors = new ArrayList<>();
    EntityJsonPrinter entityJsonPrinter = new EntityJsonPrinter();
    for (int i = 0; i < entities.size(); i++) {
        Entity noKeyEntity = Entity.newBuilder().putProperties("street", Value.newBuilder().setStringValue("Some street").build()).putProperties("number", Value.newBuilder().setIntegerValue(i).build()).build();
        testEntitiesWithNoKey.add(noKeyEntity);
        expectedErrors.add(ErrorMessage.newBuilder().setMessage("Datastore Entity Without Key").setData(entityJsonPrinter.print(noKeyEntity)).build().toJson());
    }
    List<Entity> testEntities = new ArrayList<>(entities);
    testEntities.addAll(testEntitiesWithNoKey);
    // Run the test
    TupleTag<Entity> successTag = new TupleTag<Entity>("entities") {
    };
    TupleTag<String> failureTag = new TupleTag<String>("failures") {
    };
    PCollectionTuple results = pipeline.apply("Create", Create.of(testEntities)).apply("RemoveNoKeys", CheckNoKey.newBuilder().setSuccessTag(successTag).setFailureTag(failureTag).build());
    // Check the results
    PAssert.that(results.get(successTag)).containsInAnyOrder(entities);
    PAssert.that(results.get(failureTag)).containsInAnyOrder(expectedErrors);
    pipeline.run();
}
Also used : JsonToEntity(com.google.cloud.teleport.templates.common.DatastoreConverters.JsonToEntity) Entity(com.google.datastore.v1.Entity) EntityJsonPrinter(com.google.cloud.teleport.templates.common.DatastoreConverters.EntityJsonPrinter) ArrayList(java.util.ArrayList) TupleTag(org.apache.beam.sdk.values.TupleTag) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

EntityJsonPrinter (com.google.cloud.teleport.templates.common.DatastoreConverters.EntityJsonPrinter)3 JsonToEntity (com.google.cloud.teleport.templates.common.DatastoreConverters.JsonToEntity)3 Entity (com.google.datastore.v1.Entity)3 ArrayList (java.util.ArrayList)3 PCollectionTuple (org.apache.beam.sdk.values.PCollectionTuple)3 TupleTag (org.apache.beam.sdk.values.TupleTag)3 Test (org.junit.Test)3 Category (org.junit.experimental.categories.Category)3