@SuppressWarnings("unchecked") private void syncKeys() { final Datastore ds = getDatastore(); keyMap.clear(); final Map<Object, Object> map = (Map) object; for (final Map.Entry<Object, Object> e : map.entrySet()) { keyMap.put(e.getKey(), ds.getKey(e.getValue())); } }
private void syncKeys() { final Datastore ds = getDatastore(); listOfKeys.clear(); for (final Object e : ((Collection) object)) { listOfKeys.add(ds.getKey(e)); } } }
private void insertData() { final Datastore datastore = getDs(); Channel sportChannel = new Channel("Sport channel"); datastore.save(sportChannel); datastore.save(new Channel("Art channel")); Channel fitnessChannel = new Channel("Fitness channel"); datastore.save(fitnessChannel); final List<Key<Channel>> followedChannels = new ArrayList<Key<Channel>>(); followedChannels.add(datastore.getKey(sportChannel)); followedChannels.add(datastore.getKey(fitnessChannel)); datastore.save(new User("Roberto", datastore.getKey(sportChannel), followedChannels)); }
@Test public void testKeyComparisons() { final User user = new User("Luke Skywalker"); getDs().save(user); final Key<User> k1 = new Key<User>(User.class, "User", user.id); final Key<User> k2 = getDs().getKey(user); Assert.assertTrue(k1.equals(k2)); Assert.assertTrue(k2.equals(k1)); }
@Test public void keyMapping() { getMorphia().map(User.class, Channel.class); insertData(); final Datastore datastore = getDs(); User user = datastore.find(User.class).find(new FindOptions().limit(1)).tryNext(); List<Key<Channel>> followedChannels = user.followedChannels; Channel channel = datastore.find(Channel.class).filter("name", "Sport channel") .find(new FindOptions().limit(1)) .tryNext(); Key<Channel> key = datastore.getKey(channel); Assert.assertTrue(followedChannels.contains(key)); }
@Test public final void testGetKeyWithoutFetching() { // TODO us: exclusion does not work properly with maven + junit4 if (!LazyFeatureDependencies.testDependencyFullFilled()) { return; } RootEntity root = new RootEntity(); final ReferencedEntity reference = new ReferencedEntity(); reference.parent = root; root.r = reference; reference.setFoo("bar"); final Key<ReferencedEntity> k = getDs().save(reference); final String keyAsString = k.getId().toString(); getDs().save(root); root = getDs().get(root); final ReferencedEntity p = root.r; assertIsProxy(p); assertNotFetched(p); Assert.assertEquals(keyAsString, getDs().getKey(p).getId().toString()); // still not fetched? assertNotFetched(p); p.getFoo(); // should be fetched now. assertFetched(p); }
@Test public final void testGetKeyWithoutFetching() { if (!LazyFeatureDependencies.testDependencyFullFilled()) { return; } RootEntity root = new RootEntity(); final ReferencedEntity reference = new ReferencedEntity(); root.r = reference; reference.setFoo("bar"); final Key<ReferencedEntity> k = getDs().save(reference); final String keyAsString = k.getId().toString(); getDs().save(root); root = getDs().get(root); final ReferencedEntity p = root.r; assertIsProxy(p); assertNotFetched(p); Assert.assertEquals(keyAsString, getDs().getKey(p).getId().toString()); // still not fetched? assertNotFetched(p); p.getFoo(); // should be fetched now. assertFetched(p); }
@Test public void testEntityUpdate() { final Datastore datastore = getDs(); Versioned entity = new Versioned(); entity.setName("Value 1"); datastore.save(entity); entity = datastore.get(Versioned.class, entity.getId()); Assert.assertEquals("Value 1", entity.getName()); Assert.assertEquals(1, entity.getVersion().longValue()); entity.setName("Value 2"); datastore.save(entity); entity = datastore.get(Versioned.class, entity.getId()); Assert.assertEquals("Value 2", entity.getName()); Assert.assertEquals(2, entity.getVersion().longValue()); UpdateOperations<Versioned> ops = datastore.createUpdateOperations(Versioned.class); ops.set("name", "Value 3"); Assert.assertEquals(1, datastore.update(entity, ops).getUpdatedCount()); Assert.assertEquals(0, datastore.update(entity, ops).getUpdatedCount()); entity = datastore.get(Versioned.class, entity.getId()); Assert.assertEquals("Value 3", entity.getName()); Assert.assertEquals(3, entity.getVersion().longValue()); ops = datastore.createUpdateOperations(Versioned.class); ops.set("name", "Value 4"); datastore.update(datastore.getKey(entity), ops); entity = datastore.get(Versioned.class, entity.getId()); Assert.assertEquals("Value 4", entity.getName()); Assert.assertEquals(4, entity.getVersion().longValue()); }
@Test public void testIt() throws Exception { getMorphia().map(Employee.class); getDs().save(new Employee("Mister", "GOD", null, 0)); final Employee boss = getDs().find(Employee.class).field("manager").equal(null) .find(new FindOptions().limit(1)) .next(); // get an employee without a manager Assert.assertNotNull(boss); final Key<Employee> key = getDs().save(new Employee("Scott", "Hernandez", getDs().getKey(boss), 150 * 1000)); Assert.assertNotNull(key); final UpdateResults res = getDs().update(boss, getDs().createUpdateOperations(Employee.class) .addToSet("underlings", key)); //add Scott as an employee of his manager Assert.assertNotNull(res); Assert.assertTrue("Should update existing document", res.getUpdatedExisting()); Assert.assertEquals("Should update one document", 1, res.getUpdatedCount()); final Employee scottsBoss = getDs().find(Employee.class).filter("underlings", key) .find(new FindOptions().limit(1)) .next(); // get Scott's boss Assert.assertNotNull(scottsBoss); Assert.assertEquals(boss.id, scottsBoss.id); }