use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreTest method testNewBatch.
@Test
public void testNewBatch() {
Batch batch = DATASTORE.newBatch();
Entity entity1 = Entity.newBuilder(ENTITY1).clear().build();
Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
Entity entity4 = Entity.newBuilder(KEY4).set("value", StringValue.of("value")).build();
Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
List<Entity> entities = batch.add(entity4, PARTIAL_ENTITY2, entity5);
Entity entity6 = entities.get(1);
assertSame(entity4, entities.get(0));
assertEquals(PARTIAL_ENTITY2.getNames(), entity6.getNames());
assertEquals(PARTIAL_ENTITY2.getKey().getProjectId(), entity6.getKey().getProjectId());
assertEquals(PARTIAL_ENTITY2.getKey().getNamespace(), entity6.getKey().getNamespace());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors());
assertEquals(PARTIAL_ENTITY2.getKey().getKind(), entity6.getKey().getKind());
assertEquals(PARTIAL_ENTITY2.getKey(), IncompleteKey.newBuilder(entity6.getKey()).build());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entity6.getKey().getAncestors());
assertNotEquals(PARTIAL_ENTITY2.getKey(), entity6.getKey());
assertSame(entity5, entities.get(2));
batch.addWithDeferredIdAllocation(PARTIAL_ENTITY3);
batch.put(ENTITY3, entity1, entity2);
Batch.Response response = batch.submit();
entities = DATASTORE.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
assertEquals(entity1, entities.get(0));
assertEquals(entity2, entities.get(1));
assertEquals(ENTITY3, entities.get(2));
assertEquals(entity4, entities.get(3));
assertEquals(entity5, entities.get(4));
assertEquals(entity6, entities.get(5));
assertEquals(6, entities.size());
List<Key> generatedKeys = response.getGeneratedKeys();
assertEquals(1, generatedKeys.size());
assertEquals(PARTIAL_ENTITY3.getNames(), DATASTORE.get(generatedKeys.get(0)).getNames());
assertEquals(PARTIAL_ENTITY3.getKey(), IncompleteKey.newBuilder(generatedKeys.get(0)).build());
try {
batch.submit();
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals("FAILED_PRECONDITION", expected.getReason());
}
batch = DATASTORE.newBatch();
batch.delete(entity4.getKey(), entity5.getKey(), entity6.getKey());
batch.update(ENTITY1, ENTITY2, ENTITY3);
batch.submit();
entities = DATASTORE.fetch(KEY1, KEY2, KEY3, entity4.getKey(), entity5.getKey(), entity6.getKey());
assertEquals(ENTITY1, entities.get(0));
assertEquals(ENTITY2, entities.get(1));
assertEquals(ENTITY3, entities.get(2));
assertNull(entities.get(3));
assertNull(entities.get(4));
assertNull(entities.get(5));
assertEquals(6, entities.size());
}
use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreTest method testNewTransactionRollback.
@Test
public void testNewTransactionRollback() {
Transaction transaction = DATASTORE.newTransaction();
transaction.add(ENTITY3);
Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").set("list3", StringValue.of("bla"), StringValue.newBuilder("bla").build()).build();
transaction.update(entity2);
transaction.delete(KEY1);
transaction.rollback();
// should be safe to repeat rollback calls
transaction.rollback();
try {
transaction.commit();
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals("FAILED_PRECONDITION", expected.getReason());
}
List<Entity> list = DATASTORE.fetch(KEY1, KEY2, KEY3);
assertEquals(ENTITY1, list.get(0));
assertEquals(ENTITY2, list.get(1));
assertNull(list.get(2));
assertEquals(3, list.size());
}
use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreTest method testNewTransactionCommit.
@Test
public void testNewTransactionCommit() {
Transaction transaction = DATASTORE.newTransaction();
transaction.add(ENTITY3);
Entity entity2 = Entity.newBuilder(ENTITY2).clear().setNull("bla").build();
transaction.update(entity2);
transaction.delete(KEY1);
transaction.commit();
assertFalse(transaction.isActive());
List<Entity> list = DATASTORE.fetch(KEY1, KEY2, KEY3);
assertNull(list.get(0));
assertEquals(entity2, list.get(1));
assertEquals(ENTITY3, list.get(2));
assertEquals(3, list.size());
try {
transaction.commit();
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals("FAILED_PRECONDITION", expected.getReason());
}
try {
transaction.rollback();
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals("FAILED_PRECONDITION", expected.getReason());
}
}
use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreTest method testTransactionWithQuery.
@Test
public void testTransactionWithQuery() throws InterruptedException {
Query<Entity> query = Query.newEntityQueryBuilder().setKind(KIND2).setFilter(PropertyFilter.hasAncestor(KEY2)).setNamespace(NAMESPACE).build();
Transaction transaction = DATASTORE.newTransaction();
QueryResults<Entity> results = transaction.run(query);
assertTrue(results.hasNext());
assertEquals(ENTITY2, results.next());
assertFalse(results.hasNext());
transaction.add(ENTITY3);
transaction.commit();
assertEquals(ENTITY3, DATASTORE.get(KEY3));
transaction = DATASTORE.newTransaction();
results = transaction.run(query);
assertTrue(results.hasNext());
assertEquals(ENTITY2, results.next());
assertFalse(results.hasNext());
transaction.delete(ENTITY3.getKey());
// update entity2 during the transaction
DATASTORE.put(Entity.newBuilder(ENTITY2).clear().build());
try {
transaction.commit();
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals("ABORTED", expected.getReason());
}
}
use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreTest method testTransactionWithRead.
@Test
public void testTransactionWithRead() {
Transaction transaction = DATASTORE.newTransaction();
assertNull(transaction.get(KEY3));
transaction.add(ENTITY3);
transaction.commit();
assertEquals(ENTITY3, DATASTORE.get(KEY3));
transaction = DATASTORE.newTransaction();
assertEquals(ENTITY3, transaction.get(KEY3));
// update entity3 during the transaction
DATASTORE.put(Entity.newBuilder(ENTITY2).clear().set("from", "datastore").build());
transaction.update(Entity.newBuilder(ENTITY2).clear().set("from", "transaction").build());
try {
transaction.commit();
fail("Expecting a failure");
} catch (DatastoreException expected) {
assertEquals("ABORTED", expected.getReason());
}
}
Aggregations