com.google.appengine.api.NamespaceManager Java Examples
The following examples show how to use
com.google.appengine.api.NamespaceManager.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryNamespaceTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testQueryListWithNamespaceChange() throws Exception { Key parentKey = createQueryNamespaceTestParent("testQueryListWithNamespaceChange"); Entity bob = createEntity("QLWNC", parentKey) .withProperty("name", "Bob") .withProperty("lastName", "Smith") .store(); try { Query query = new Query("QLWNC"); List<Entity> list = service.prepare(query).asList(withDefaults()); final String previousNS = NamespaceManager.get(); NamespaceManager.set("QwertyNS"); try { assertEquals(1, list.size()); } finally { NamespaceManager.set(previousNS); } } finally { service.delete(bob.getKey(), parentKey); } }
Example #2
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testQueriesOnlyReturnResultsInCurrentNamespace() { deleteNsKinds("one", "foo"); deleteNsKinds("two", "foo"); sync(); NamespaceManager.set("one"); Entity fooOne = new Entity("foo"); service.put(fooOne); NamespaceManager.set("two"); Entity fooTwo = new Entity("foo"); service.put(fooTwo); sync(); List<Entity> listTwo = service.prepare(new Query("foo").setAncestor(fooTwo.getKey())).asList(withDefaults()); assertEquals(Collections.singletonList(fooTwo), listTwo); NamespaceManager.set("one"); List<Entity> listOne = service.prepare(new Query("foo").setAncestor(fooOne.getKey())).asList(withDefaults()); assertEquals(Collections.singletonList(fooOne), listOne); service.delete(fooOne.getKey()); service.delete(fooTwo.getKey()); }
Example #3
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test(expected = IllegalArgumentException.class) public void testQueriesByAncestorInOtherNamespaceThrowsIllegalArgumentException() { deleteNsKinds("one", "foo"); deleteNsKinds("two", "foo"); sync(); NamespaceManager.set("one"); Entity fooOne = new Entity("foo"); service.put(fooOne); NamespaceManager.set("two"); Entity fooTwo = new Entity("foo"); service.put(fooTwo); sync(); // java.lang.IllegalArgumentException: Namespace of ancestor key and query must match. service.prepare(new Query("foo").setAncestor(fooOne.getKey())).asList(withDefaults()); }
Example #4
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testQueryConsidersCurrentNamespaceWhenCreatedNotWhenPreparedOrExecuted() { deleteNsKinds("one", "foo"); deleteNsKinds("two", "foo"); sync(); NamespaceManager.set("one"); Entity fooOne = new Entity("foo"); service.put(fooOne); NamespaceManager.set("two"); Entity fooTwo = new Entity("foo"); service.put(fooTwo); sync(); Query query = new Query("foo").setAncestor(fooTwo.getKey()); // query created in namespace "two" NamespaceManager.set("one"); PreparedQuery preparedQuery = service.prepare(query); assertEquals(fooTwo, preparedQuery.asSingleEntity()); service.delete(fooOne.getKey()); service.delete(fooTwo.getKey()); }
Example #5
Source File: SecurityChecker.java From io2014-codelabs with Apache License 2.0 | 6 votes |
/** * Creates {@link com.google.appengine.api.datastore.Query} from the specified kindName. If the kindName has * _private suffix, the key will be created under a namespace for the * specified {@link com.google.appengine.api.users.User}. * * @param kindName * Name of kind * @param user * {@link com.google.appengine.api.users.User} of the requestor * @return {@link com.google.appengine.api.datastore.Query} */ public Query createKindQueryWithNamespace(String kindName, User user) { // save the original namespace String origNamespace = NamespaceManager.get(); // set namespace based on the kind suffix if (kindName.startsWith(KIND_PREFIX_PRIVATE)) { String userId = getUserId(user); NamespaceManager.set(userId); } else { NamespaceManager.set(NAMESPACE_DEFAULT); } // create a key Query q = new Query(kindName); // restore the original namespace NamespaceManager.set(origNamespace); return q; }
Example #6
Source File: SecurityChecker.java From io2014-codelabs with Apache License 2.0 | 6 votes |
/** * Creates a {@link com.google.appengine.api.datastore.Key} from the specified kindName and CloudEntity id. If * the kindName has _private suffix, the key will be created under a namespace * for the specified {@link com.google.appengine.api.users.User}. * * @param kindName * Name of kind * @param id * CloudEntity id * @param user * {@link com.google.appengine.api.users.User} of the requestor * @return {@link com.google.appengine.api.datastore.Key} */ public Key createKeyWithNamespace(String kindName, String id, User user) { // save the original namespace String origNamespace = NamespaceManager.get(); // set namespace based on the kind suffix if (kindName.startsWith(SecurityChecker.KIND_PREFIX_PRIVATE)) { String userId = getUserId(user); NamespaceManager.set(userId); } else { NamespaceManager.set(SecurityChecker.NAMESPACE_DEFAULT); } // create a key Key k = KeyFactory.createKey(kindName, id); // restore the original namespace NamespaceManager.set(origNamespace); return k; }
Example #7
Source File: NamespaceExtensionContainer.java From appengine-tck with Apache License 2.0 | 6 votes |
private void runWithinNamespaces(EventContext<Test> context, String[] namespaces) { final List<FailedNamespaceException> exceptions = new ArrayList<>(); final String original = NamespaceManager.get(); try { for (String namespace : namespaces) { try { NamespaceManager.set(namespace); context.proceed(); } catch (Exception e) { exceptions.add(new FailedNamespaceException(e, namespace)); } } } finally { NamespaceManager.set(original); } if (exceptions.size() > 1) { throw new MultipleExceptions(exceptions); } else if (exceptions.size() == 1) { throw exceptions.get(0); } }
Example #8
Source File: SecurityChecker.java From solutions-mobile-backend-starter-java with Apache License 2.0 | 6 votes |
/** * Creates a {@link Key} from the specified kindName and CloudEntity id. If * the kindName has _private suffix, the key will be created under a namespace * for the specified {@link User}. * * @param kindName * Name of kind * @param id * CloudEntity id * @param user * {@link User} of the requestor * @return {@link Key} */ public Key createKeyWithNamespace(String kindName, String id, User user) { // save the original namespace String origNamespace = NamespaceManager.get(); // set namespace based on the kind suffix if (kindName.startsWith(SecurityChecker.KIND_PREFIX_PRIVATE)) { String userId = getUserId(user); NamespaceManager.set(userId); } else { NamespaceManager.set(SecurityChecker.NAMESPACE_DEFAULT); } // create a key Key k = KeyFactory.createKey(kindName, id); // restore the original namespace NamespaceManager.set(origNamespace); return k; }
Example #9
Source File: SecurityChecker.java From solutions-mobile-backend-starter-java with Apache License 2.0 | 6 votes |
/** * Creates {@link Query} from the specified kindName. If the kindName has * _private suffix, the key will be created under a namespace for the * specified {@link User}. * * @param kindName * Name of kind * @param user * {@link User} of the requestor * @return {@link Query} */ public Query createKindQueryWithNamespace(String kindName, User user) { // save the original namespace String origNamespace = NamespaceManager.get(); // set namespace based on the kind suffix if (kindName.startsWith(KIND_PREFIX_PRIVATE)) { String userId = getUserId(user); NamespaceManager.set(userId); } else { NamespaceManager.set(NAMESPACE_DEFAULT); } // create a key Query q = new Query(kindName); // restore the original namespace NamespaceManager.set(origNamespace); return q; }
Example #10
Source File: SchemaTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testKindMetadata() { // check non empty namespace only for (int i = 1; i < namespaceDat.length; i++) { NamespaceManager.set(namespaceDat[i]); Query q = new Query("__kind__").addSort(Entity.KEY_RESERVED_PROPERTY); int count = 0; for (Entity e : service.prepare(q).asIterable()) { // do not count those stats entities for namespace. if (!e.getKey().getName().startsWith("__Stat_Ns_")) { count++; } } // For each namespace, only 3 user defined kinds. assertEquals(3, count); // check a specified namespace Key key1 = Entities.createKindKey("testing"); q.setFilter(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.EQUAL, key1)); assertEquals(1, service.prepare(q).countEntities(fo)); Entity ke = service.prepare(q).asSingleEntity(); assertEquals("testing", ke.getKey().getName()); assertEquals(namespaceDat[i], ke.getKey().getNamespace()); assertEquals(namespaceDat[i], ke.getNamespace()); } }
Example #11
Source File: SchemaTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testPropertyMetadata() { NamespaceManager.set(namespaceDat[2]); // sort by kind/property, kindDat[1] < kindDat[0] < kindDat[2] Query q = new Query("__property__").addSort(Entity.KEY_RESERVED_PROPERTY).setKeysOnly(); // filter out properties for kind "testing" Key key1 = Entities.createPropertyKey(kindDat[0], "urlData"); Key key2 = Entities.createPropertyKey(kindDat[2], "urlData"); q.setFilter(CompositeFilterOperator.and( new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, key1), new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, key2))); List<Entity> el = service.prepare(q).asList(fo); // un-indexed property, textData, will not be returned in __property__ queries. assertEquals(13, el.size()); for (int i = 0; i < el.size(); i++) { assertEquals(namespaceDat[2], el.get(i).getKey().getNamespace()); assertEquals(kindDat[2], el.get(i).getKey().getParent().getName()); if (i == 0) { assertEquals("adressData", el.get(0).getKey().getName()); } else if (i == el.size() - 1) { assertEquals("urlData", el.get(el.size() - 1).getKey().getName()); } } }
Example #12
Source File: TaskQueueTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testUserNameSpace() { String testMethodTag = "testUserNameSpace"; NamespaceManager.set("junittest"); TaskOptions taskOptions = TaskOptions.Builder .withMethod(TaskOptions.Method.POST) .param(TEST_RUN_ID, testRunId) .param(TEST_METHOD_TAG, testMethodTag) .url("/queuetask/addentity"); // task name explicitly not specified. QueueFactory.getQueue(E2E_TESTING).add(taskOptions); Entity entity = dsUtil.waitForTaskThenFetchEntity(waitInterval, retryMax, testMethodTag); Map<String, String> expectedParams = dsUtil.createParamMap(testMethodTag); dsUtil.assertTaskParamsMatchEntityProperties(expectedParams, entity); }
Example #13
Source File: NamespaceTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testQuery() { NamespaceManager.set(""); Query query = new Query("__namespace__"); int nsCount = service.prepare(query) .countEntities(FetchOptions.Builder.withDefaults()); assertTrue(nsCount > 0); String ns = ""; for (Entity readRec : service.prepare(query).asIterable()) { ns = readRec.getKey().getName() + "," + ns; } for (int i = 0; i < namespaceDat.length; i++) { if (!namespaceDat[i].equals("")) { assertTrue(ns.indexOf(namespaceDat[i]) >= 0); } else { assertTrue(ns.indexOf("null") >= 0); } } }
Example #14
Source File: UpdateCountsServlet.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws java.io.IOException { // Update the count for the current namespace. updateCount("request"); // Update the count for the "-global-" namespace. String namespace = NamespaceManager.get(); try { // "-global-" is namespace reserved by the application. NamespaceManager.set("-global-"); updateCount("request"); } finally { NamespaceManager.set(namespace); } resp.setContentType("text/plain"); resp.getWriter().println("Counts are now updated."); }
Example #15
Source File: SomeRequestServlet.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // Increment the count for the current namespace asynchronously. QueueFactory.getDefaultQueue() .add(TaskOptions.Builder.withUrl("/_ah/update_count").param("countName", "SomeRequest")); // Increment the global count and set the // namespace locally. The namespace is // transferred to the invoked request and // executed asynchronously. String namespace = NamespaceManager.get(); try { NamespaceManager.set("-global-"); QueueFactory.getDefaultQueue() .add(TaskOptions.Builder.withUrl("/_ah/update_count").param("countName", "SomeRequest")); } finally { NamespaceManager.set(namespace); } resp.setContentType("text/plain"); resp.getWriter().println("Counts are being updated."); }
Example #16
Source File: TestMetadataServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
private void populate(DatastoreService ds, String namespace) { NamespaceManager.set(namespace); Entity e = new Entity("Fun"); e.setProperty("me", "yes"); e.setProperty("you", 23); e.setUnindexedProperty("haha", 0); ds.put(e); Entity s = new Entity("Strange"); ArrayList nowhereList = new ArrayList<Integer>(); nowhereList.add(1); nowhereList.add(2); nowhereList.add(3); s.setProperty("nowhere", nowhereList); ds.put(s); Entity s2 = new Entity("Stranger"); s2.setProperty("missing", new ArrayList<Integer>()); ds.put(s2); }
Example #17
Source File: DeferredTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testDeferredUserNS() { String testMethodTag = "testDeferredUserNS"; String specifiedNameSpace = "the_testDeferredUserNS"; Map<String, String> paramMap = dsUtil.createParamMap(testMethodTag); NamespaceManager.set(specifiedNameSpace); TaskOptions taskOptions = TaskOptions.Builder.withPayload(new ExecDeferred(dsUtil, paramMap)); // no task name specified. QueueFactory.getQueue(E2E_TESTING_DEFERRED).add(taskOptions); Entity entity = dsUtil.waitForTaskThenFetchEntity(waitInterval, retryMax, testMethodTag); Map<String, String> expectedMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); expectedMap.putAll(paramMap); expectedMap.put("X-AppEngine-Current-Namespace", specifiedNameSpace); dsUtil.assertTaskParamsMatchEntityProperties(expectedMap, entity); }
Example #18
Source File: NamespaceTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Before public void createData() throws InterruptedException { List<Entity> eList = new ArrayList<Entity>(); for (int i = 0; i < namespaceDat.length; i++) { NamespaceManager.set(namespaceDat[i]); Query q = new Query(kindName); if (service.prepare(q).countEntities(FetchOptions.Builder.withDefaults()) == 0) { for (int c = 0; c < count; c++) { Entity newRec = new Entity(kindName); newRec.setProperty("jobType", stringDat[i] + c); eList.add(newRec); } } } if (eList.size() > 0) { service.put(eList); sync(waitTime); } }
Example #19
Source File: DatastoreSessionStore.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override public Map<String, SessionData> getAllSessions() { final String originalNamespace = NamespaceManager.get(); NamespaceManager.set(""); try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); PreparedQuery pq = ds.prepare(new Query(SESSION_ENTITY_TYPE)); Map<String, SessionData> sessions = new HashMap<>(); for (Entity entity : pq.asIterable()) { sessions.put(entity.getKey().getName(), createSessionFromEntity(entity)); } return sessions; } finally { NamespaceManager.set(originalNamespace); } }
Example #20
Source File: IndexTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testNamespaceWithBug() throws InterruptedException, ParseException { String ns = "ns-indextest"; String indexName = "ns-index"; int docCount = 5; NamespaceManager.set(ns); SearchService searchService2 = SearchServiceFactory.getSearchService(); Index index = searchService2.getIndex(IndexSpec.newBuilder() .setName(indexName) .build()); delDocs(index); addDocs(index, docCount); GetIndexesRequest request = GetIndexesRequest.newBuilder() .setIndexNamePrefix(indexName) .setOffset(0) .setNamespace(ns) .setLimit(10) .build(); assertEquals(ns, request.getNamespace()); GetResponse<Index> response = searchService2.getIndexes(request); List<Index> listIndexes = response.getResults(); for (Index oneIndex : listIndexes) { assertEquals(ns, listIndexes.get(0).getNamespace()); assertEquals(indexName, listIndexes.get(0).getName()); verifyDocCount(oneIndex, docCount); } assertEquals(ns, searchService2.getNamespace()); NamespaceManager.set(""); }
Example #21
Source File: IndexTest.java From appengine-tck with Apache License 2.0 | 5 votes |
public void testNamespace() throws InterruptedException, ParseException { String ns = "ns-indextest"; String indexName = "ns-index"; int docCount = 5; NamespaceManager.set(ns); Index index = searchService.getIndex(IndexSpec.newBuilder() .setName(indexName) .build()); delDocs(index); addDocs(index, docCount); GetIndexesRequest request = GetIndexesRequest.newBuilder() .setIndexNamePrefix(indexName) .setOffset(0) .setNamespace(ns) .setLimit(10) .build(); assertEquals(ns, request.getNamespace()); GetResponse<Index> response = searchService.getIndexes(request); List<Index> listIndexes = response.getResults(); for (Index oneIndex : listIndexes) { assertEquals(ns, listIndexes.get(0).getNamespace()); assertEquals(indexName, listIndexes.get(0).getName()); verifyDocCount(oneIndex, docCount); } assertEquals(ns, searchService.getNamespace()); NamespaceManager.set(""); }
Example #22
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 5 votes |
private void deleteNsKinds(String namespace, String kind) { String originalNs = NamespaceManager.get(); NamespaceManager.set(namespace); List<Entity> entities = service.prepare(new Query(kind)).asList(withDefaults()); deleteEntityList(entities); NamespaceManager.set(originalNs); }
Example #23
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testQueryOnSomePropertyWithKeyInDifferentNamespace() { NamespaceManager.set("one"); Key keyInNamespaceOne = KeyFactory.createKey("kind", 1); NamespaceManager.set("two"); Query query = new Query("kind").setFilter(new Query.FilterPredicate("someProperty", EQUAL, keyInNamespaceOne)); PreparedQuery preparedQuery = service.prepare(query); preparedQuery.asSingleEntity(); // should not throw IllegalArgumentException as in previous test preparedQuery.asIterator().hasNext(); // should not throw IllegalArgumentException as in previous test preparedQuery.asList(withDefaults()).size(); // should not throw IllegalArgumentException as in previous test }
Example #24
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testKeysCreatedUnderDifferentNamespacesAreNotEqual() throws Exception { NamespaceManager.set("one"); Key key1 = KeyFactory.createKey("Test", 1); NamespaceManager.set("two"); Key key2 = KeyFactory.createKey("Test", 1); assertFalse(key1.equals(key2)); }
Example #25
Source File: SchemaTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testEntityGroupMetadata() throws EntityNotFoundException { if (service.getDatastoreAttributes().getDatastoreType() == DatastoreAttributes.DatastoreType.HIGH_REPLICATION) { NamespaceManager.set(namespaceDat[2]); Entity entity1 = new Entity(kindDat[2]); entity1.setProperty("name", "entity1"); entity1.setProperty("timestamp", new Date()); Key k1 = service.put(entity1); Key entityGroupKey = Entities.createEntityGroupKey(k1); long version1 = Entities.getVersionProperty(service.get(entityGroupKey)); Entity entity2 = new Entity(kindDat[2]); entity2.setProperty("name", "entity2"); entity2.setProperty("timestamp", new Date()); service.put(entity2); // Get entity1's version again. There should be no change. long version2 = Entities.getVersionProperty(service.get(entityGroupKey)); assertEquals(version1, version2); Entity entity3 = new Entity(kindDat[2], k1); entity3.setProperty("name", "entity3"); entity3.setProperty("timestamp", new Date()); service.put(entity3); // Get entity1's version again. There should be change since it is used as parent. long version3 = Entities.getVersionProperty(service.get(entityGroupKey)); assertTrue(version3 > version1); // clean test data service.delete(entity3.getKey(), entity2.getKey(), k1); } }
Example #26
Source File: NamespaceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDiffNamespace() { NamespaceManager.set(namespaceDat[1]); Query q = new Query(kindName); q.setFilter(new FilterPredicate("jobType", Query.FilterOperator.EQUAL, stringDat[2] + 1)); int ttl = service.prepare(q).countEntities(FetchOptions.Builder.withDefaults()); assertEquals(0, ttl); }
Example #27
Source File: NamespaceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testEntity() { for (String ns : namespaceDat) { NamespaceManager.set(ns); Query query = new Query(kindName); Entity readRec = service.prepare(query).asIterator().next(); assertEquals(ns, readRec.getNamespace()); String appId = readRec.getAppId(); appId = appId.substring(appId.indexOf("~") + 1); assertEquals(SystemProperty.applicationId.get(), appId); assertTrue(readRec.hasProperty("jobType")); } }
Example #28
Source File: NamespaceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testSort() { for (String ns : namespaceDat) { NamespaceManager.set(ns); doSort(kindName, "jobType", 3, Query.SortDirection.ASCENDING); doSort(kindName, "jobType", 3, Query.SortDirection.DESCENDING); } }
Example #29
Source File: NamespaceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testFilter() { for (int i = 0; i < namespaceDat.length; i++) { NamespaceManager.set(namespaceDat[i]); doAllFilters(kindName, "jobType", stringDat[i] + 1); } }
Example #30
Source File: SchemaTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Before public void createData() throws InterruptedException { List<Entity> eList = new ArrayList<Entity>(); for (int i = 0; i < namespaceDat.length; i++) { NamespaceManager.set(namespaceDat[i]); for (int k = 0; k < kindDat.length; k++) { Query q = new Query(kindDat[k]); if (service.prepare(q).countEntities(fo) == 0) { for (int c = 0; c < count; c++) { Entity newRec = new Entity(kindDat[k]); newRec.setProperty("name", kindDat[k] + c); newRec.setProperty("timestamp", new Date()); newRec.setProperty("shortBlobData", new ShortBlob("shortBlobData".getBytes())); newRec.setProperty("intData", 12345); newRec.setProperty("textData", new Text("textData")); newRec.setProperty("floatData", new Double(12345.12345)); newRec.setProperty("booleanData", true); newRec.setProperty("urlData", new Link("http://www.google.com")); newRec.setProperty("emailData", new Email("[email protected]")); newRec.setProperty("phoneData", new PhoneNumber("408-123-4567")); newRec.setProperty("adressData", new PostalAddress("123 st. CA 12345")); newRec.setProperty("ratingData", new Rating(55)); newRec.setProperty("geoptData", new GeoPt((float) 12.12, (float) 98.98)); newRec.setProperty("categoryData", new Category("abc")); eList.add(newRec); } } } } if (eList.size() > 0) { service.put(eList); sync(waitTime); } }