Skip to content

Instantly share code, notes, and snippets.

@fabito
Created June 4, 2014 03:54
Show Gist options
  • Save fabito/facbdd34400b1c4ef735 to your computer and use it in GitHub Desktop.
Save fabito/facbdd34400b1c4ef735 to your computer and use it in GitHub Desktop.
SearchServices behaves different from other NamespaceManager compatible services such as MemcacheServices and DatastoreServices. After a NamaespaceManager.set() invocation its necessary to obtain a new SearchService instance from SearchServiceFactory. http://stackoverflow.com/questions/11019473/change-namespacemanager-on-full-text-search-gae
package com.ciandt.d1.commons.cloud;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.google.appengine.api.NamespaceManager;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.search.Document;
import com.google.appengine.api.search.Field;
import com.google.appengine.api.search.IndexSpec;
import com.google.appengine.api.search.SearchService;
import com.google.appengine.api.search.SearchServiceFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
public class GaeNamespaceManagerTests {
private static final String EMPTY = "";
private static final String TWO = "--two--";
final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig(),
new LocalMemcacheServiceTestConfig());
@Before
public void setup() throws Exception {
helper.setUp();
}
@Test
public void testSearchService() {
SearchService searchService = SearchServiceFactory.getSearchService();
searchService.getIndex(indexSpec()).put(document());
NamespaceManager.set(TWO);
assertNull(searchService.getIndex(indexSpec()).get("123456")); //fails
NamespaceManager.set(EMPTY);
assertEquals("123456",searchService.getIndex(indexSpec()).get("123456").getId());
}
private Document document() {
return Document.newBuilder()
.addField(Field.newBuilder().setName("myField").setText("myFieldValue"))
.setId("123456").build();
}
private IndexSpec indexSpec() {
return IndexSpec.newBuilder().setName("MyIndex").build();
}
@Test
public void testMemcacheService() {
MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
memcacheService.put("mykey", "myobj");
NamespaceManager.set(TWO);
assertNull(memcacheService.get("mykey"));
NamespaceManager.set(EMPTY);
assertEquals("myobj", memcacheService.get("mykey"));
}
@Test
public void testDatastoreService() {
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Entity entity = new Entity("MyKind", 1l);
datastoreService.put(entity);
NamespaceManager.set(TWO);
try {
datastoreService.get(KeyFactory.createKey("MyKind", 1l));
Assert.fail();
} catch (EntityNotFoundException e) {
}
NamespaceManager.set(EMPTY);
try {
datastoreService.get(KeyFactory.createKey("MyKind", 1l));
} catch (EntityNotFoundException e) {
Assert.fail();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment