Search in sources :

Example 21 with EntityProto

use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project nomulus by google.

the class InitSqlTestUtils method bytesToEntity.

/**
 * Deserializes raw bytes into {@link Entity}.
 */
public static Entity bytesToEntity(byte[] bytes) {
    EntityProto proto = new EntityProto();
    proto.parseFrom(bytes);
    return EntityTranslator.createFromPb(proto);
}
Also used : EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Example 22 with EntityProto

use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project nomulus by google.

the class EntityWrapperTest method testDifferentKeysNotEqual.

@Test
void testDifferentKeysNotEqual() {
    EntityProto proto1 = EntityTranslator.convertToPb(new Entity(TEST_ENTITY_KIND, ARBITRARY_KEY_ID));
    EntityProto proto2 = EntityTranslator.convertToPb(new Entity(TEST_ENTITY_KIND, ARBITRARY_KEY_ID + 1));
    // Construct entity objects from the two protos.
    Entity e1 = EntityTranslator.createFromPb(proto1);
    Entity e2 = EntityTranslator.createFromPb(proto2);
    EntityWrapper ce1 = new EntityWrapper(e1);
    EntityWrapper ce2 = new EntityWrapper(e2);
    assertThat(ce1).isNotEqualTo(ce2);
    assertThat(ce1.hashCode()).isNotEqualTo(ce2.hashCode());
}
Also used : Entity(com.google.appengine.api.datastore.Entity) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto) Test(org.junit.jupiter.api.Test)

Example 23 with EntityProto

use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project nomulus by google.

the class EntityWrapperTest method testEquals.

@Test
void testEquals() {
    // Create an entity with a key and some properties.
    Entity entity = new Entity(TEST_ENTITY_KIND, ARBITRARY_KEY_ID);
    // Note that we need to specify these as long for property comparisons to work because that's
    // how they are deserialized from protos.
    entity.setProperty("eeny", 100L);
    entity.setProperty("meeny", 200L);
    entity.setProperty("miney", 300L);
    EntityProto proto1 = EntityTranslator.convertToPb(entity);
    EntityProto proto2 = EntityTranslator.convertToPb(entity);
    // Reorder the property list of proto2 (the protobuf stores this as a repeated field, so
    // we just have to clear and re-add them in a different order).
    ImmutableList<Property> properties = ImmutableList.of(proto2.getProperty(2), proto2.getProperty(0), proto2.getProperty(1));
    proto2.clearProperty();
    for (Property property : properties) {
        proto2.addProperty(property);
    }
    // Construct entity objects from the two protos.
    Entity e1 = EntityTranslator.createFromPb(proto1);
    Entity e2 = EntityTranslator.createFromPb(proto2);
    // Ensure that we have a normalized representation.
    EntityWrapper ce1 = new EntityWrapper(e1);
    EntityWrapper ce2 = new EntityWrapper(e2);
    assertThat(ce1).isEqualTo(ce2);
    assertThat(ce1.hashCode()).isEqualTo(ce2.hashCode());
    // Ensure that the original entity is equal.
    assertThat(new EntityWrapper(entity)).isEqualTo(ce1);
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Property(com.google.storage.onestore.v3.OnestoreEntity.Property) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto) Test(org.junit.jupiter.api.Test)

Example 24 with EntityProto

use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project nomulus by google.

the class EntityWrapperTest method testDifferentPropertiesNotEqual.

@Test
void testDifferentPropertiesNotEqual() {
    Entity entity = new Entity(TEST_ENTITY_KIND, ARBITRARY_KEY_ID);
    // Note that we need to specify these as long for property comparisons to work because that's
    // how they are deserialized from protos.
    entity.setProperty("eeny", 100L);
    entity.setProperty("meeny", 200L);
    entity.setProperty("miney", 300L);
    EntityProto proto1 = EntityTranslator.convertToPb(entity);
    entity.setProperty("tiger!", 400);
    EntityProto proto2 = EntityTranslator.convertToPb(entity);
    // Construct entity objects from the two protos.
    Entity e1 = EntityTranslator.createFromPb(proto1);
    Entity e2 = EntityTranslator.createFromPb(proto2);
    EntityWrapper ce1 = new EntityWrapper(e1);
    EntityWrapper ce2 = new EntityWrapper(e2);
    // The keys should still be the same.
    assertThat(e1).isEqualTo(e2);
    assertThat(ce1).isNotEqualTo(ce2);
    assertThat(ce1.hashCode()).isNotEqualTo(ce2.hashCode());
}
Also used : Entity(com.google.appengine.api.datastore.Entity) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto) Test(org.junit.jupiter.api.Test)

Example 25 with EntityProto

use of com.google.storage.onestore.v3.OnestoreEntity.EntityProto in project nomulus by google.

the class EntityImports method fixMutationEntityProtoBytes.

private static void fixMutationEntityProtoBytes(EntityProto entityProto, String appId) {
    for (OnestoreEntity.Property property : entityProto.mutableRawPropertys()) {
        if (Objects.equals(property.getName(), "entityProtoBytes")) {
            OnestoreEntity.PropertyValue value = property.getValue();
            EntityProto fixedProto = fixEntity(bytesToEntityProto(value.getStringValueAsBytes()), appId);
            value.setStringValueAsBytes(fixedProto.toByteArray());
            return;
        }
    }
}
Also used : OnestoreEntity(com.google.storage.onestore.v3.OnestoreEntity) EntityProto(com.google.storage.onestore.v3.OnestoreEntity.EntityProto)

Aggregations

EntityProto (com.google.storage.onestore.v3.OnestoreEntity.EntityProto)32 Property (com.google.storage.onestore.v3.OnestoreEntity.Property)13 Path (com.google.storage.onestore.v3.OnestoreEntity.Path)9 Reference (com.google.storage.onestore.v3.OnestoreEntity.Reference)9 ByteString (com.google.protobuf.ByteString)7 ImmutableList (com.google.common.collect.ImmutableList)6 Map (java.util.Map)6 PropertyValue (com.google.storage.onestore.v3.OnestoreEntity.PropertyValue)5 Entity (com.google.appengine.api.datastore.Entity)3 Extent (com.google.appengine.api.datastore.dev.LocalDatastoreService.Extent)3 Profile (com.google.appengine.api.datastore.dev.LocalDatastoreService.Profile)3 Cost (com.google.apphosting.datastore.DatastoreV3Pb.Cost)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Element (com.google.storage.onestore.v3.OnestoreEntity.Path.Element)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 WeakHashMap (java.util.WeakHashMap)3