Search in sources :

Example 21 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.

the class TaskQueueTest method testBulkAddImplicitTransactionEnlistment_NoTaskOptions.

@Test
public void testBulkAddImplicitTransactionEnlistment_NoTaskOptions() {
    MockQueueBulkAddApiHelper helper = newBasicAddRequest();
    com.google.apphosting.datastore.proto2api.DatastoreV3Pb.Transaction pbTxn = com.google.apphosting.datastore.proto2api.DatastoreV3Pb.Transaction.newBuilder().setHandle(44).setApp(APP).build();
    helper.expectedRequest.getAddRequestBuilder(0).setTransaction(pbTxn);
    Transaction txn = mock(Transaction.class);
    when(txn.getId()).thenReturn("44");
    when(txn.getApp()).thenReturn(APP);
    DatastoreService ds = mock(DatastoreService.class);
    when(ds.getCurrentTransaction(null)).thenReturn(txn);
    Queue q = helper.getQueue(ds);
    q.add();
}
Also used : Transaction(com.google.appengine.api.datastore.Transaction) DatastoreService(com.google.appengine.api.datastore.DatastoreService) DatastoreV3Pb(com.google.apphosting.datastore.DatastoreV3Pb) Test(org.junit.Test)

Example 22 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.

the class TransactionCleanupFilterTest method testAbandonedTransactions_allRolledBack.

@Test
public void testAbandonedTransactions_allRolledBack() throws ServletException, IOException {
    DatastoreService datastoreMock = mock(DatastoreService.class);
    Transaction txn1 = mock(Transaction.class);
    when(txn1.getId()).thenReturn("txn1");
    Transaction txn2 = mock(Transaction.class);
    when(txn2.getId()).thenReturn("txn2");
    when(datastoreMock.getActiveTransactions()).thenReturn(Lists.newArrayList(txn1, txn2));
    MyTransactionCleanupFilter filter = new MyTransactionCleanupFilter(datastoreMock);
    filter.init(null);
    FilterChain chain = mock(FilterChain.class);
    filter.doFilter(null, null, chain);
    verify(chain, atLeastOnce()).doFilter(null, null);
    verify(txn1, atLeastOnce()).rollback();
    verify(txn2, atLeastOnce()).rollback();
}
Also used : Transaction(com.google.appengine.api.datastore.Transaction) DatastoreService(com.google.appengine.api.datastore.DatastoreService) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Example 23 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.

the class TransactionCleanupFilterTest method testNoAbandonedTransactions.

@Test
public void testNoAbandonedTransactions() throws ServletException, IOException {
    DatastoreService datastoreMock = mock(DatastoreService.class);
    when(datastoreMock.getActiveTransactions()).thenReturn(ImmutableList.of());
    MyTransactionCleanupFilter filter = new MyTransactionCleanupFilter(datastoreMock);
    filter.init(null);
    FilterChain chain = mock(FilterChain.class);
    filter.doFilter(null, null, chain);
    verify(chain, atLeastOnce()).doFilter(null, null);
}
Also used : DatastoreService(com.google.appengine.api.datastore.DatastoreService) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Example 24 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.

the class DatastoreViewerServlet method deleteEntities.

private void deleteEntities(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    int numDeleted = 0;
    int numKeys = Integer.parseInt(req.getParameter(NUM_KEYS));
    for (int i = 1; i <= numKeys; i++) {
        String key = req.getParameter(KEY + i);
        if (key != null) {
            ds.delete(KeyFactory.stringToKey(key));
            numDeleted++;
        }
    }
    String message = String.format("%d entit%s deleted. If your app uses memcache to cache entities " + "(e.g. uses Objectify), you may see stale results unless you flush memcache.", numDeleted, numDeleted == 1 ? "y" : "ies");
    resp.sendRedirect(String.format("%s&msg=%s", req.getParameter("next"), urlencode(message)));
}
Also used : DatastoreService(com.google.appengine.api.datastore.DatastoreService) LocalDatastoreService(com.google.appengine.api.datastore.dev.LocalDatastoreService)

Example 25 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project appengine-java-standard by GoogleCloudPlatform.

the class DatastoreViewerServlet method doGetIndexes.

private void doGetIndexes(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Empty namespace parameter equals to no namespace specified
    String requestedNamespace = req.getParameter(NAMESPACE);
    String namespace = requestedNamespace != null ? requestedNamespace : "";
    String savedNamespace = NamespaceManager.get();
    try {
        NamespaceManager.set(namespace);
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Map<Index, IndexState> indexes = ds.getIndexes();
        req.setAttribute(INDEXES, indexes);
        req.setAttribute(APPLICATION_NAME, ApiProxy.getCurrentEnvironment().getAppId());
        try {
            getServletContext().getRequestDispatcher("/_ah/adminConsole?subsection=" + Subsection.indexDetails.name()).forward(req, resp);
        } catch (ServletException e) {
            throw new RuntimeException("Could not forward request", e);
        }
    } finally {
        NamespaceManager.set(savedNamespace);
    }
}
Also used : ServletException(javax.servlet.ServletException) DatastoreService(com.google.appengine.api.datastore.DatastoreService) LocalDatastoreService(com.google.appengine.api.datastore.dev.LocalDatastoreService) Index(com.google.appengine.api.datastore.Index) IndexState(com.google.appengine.api.datastore.Index.IndexState)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)59 Entity (com.google.appengine.api.datastore.Entity)38 Test (org.junit.Test)29 Key (com.google.appengine.api.datastore.Key)22 Query (com.google.appengine.api.datastore.Query)20 Transaction (com.google.appengine.api.datastore.Transaction)15 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)6 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)6 LocalDatastoreService (com.google.appengine.api.datastore.dev.LocalDatastoreService)6 Date (java.util.Date)5 Filter (com.google.appengine.api.datastore.Query.Filter)4 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)3 ArrayList (java.util.ArrayList)3 FilterChain (javax.servlet.FilterChain)3 BlobKey (com.google.appengine.api.blobstore.BlobKey)2 DatastoreServiceConfig (com.google.appengine.api.datastore.DatastoreServiceConfig)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2 CompositeFilter (com.google.appengine.api.datastore.Query.CompositeFilter)2 ReadPolicy (com.google.appengine.api.datastore.ReadPolicy)2 Text (com.google.appengine.api.datastore.Text)2