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);
}
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());
}
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);
}
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());
}
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;
}
}
}
Aggregations