use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestDAO method testNewDAO.
@Test
public void testNewDAO() throws Exception {
getMorphia().map(Hotel.class);
final DAO<Hotel, ObjectId> hotelDAO = new BasicDAO<Hotel, ObjectId>(Hotel.class, getMongoClient(), getMorphia(), "morphia_test");
final Hotel borg = new Hotel();
borg.setName("Hotel Borg");
borg.setStars(3);
borg.setTakesCreditCards(true);
borg.setStartDate(new Date());
borg.setType(Hotel.Type.LEISURE);
final Address address = new Address();
address.setStreet("Posthusstraeti 11");
address.setPostCode("101");
borg.setAddress(address);
hotelDAO.deleteByQuery((Query<Hotel>) hotelDAO.find());
hotelDAO.save(borg);
assertEquals(1, hotelDAO.count());
assertNotNull(borg.getId());
final Hotel hotelLoaded = hotelDAO.get(borg.getId());
assertEquals(borg.getName(), hotelLoaded.getName());
assertEquals(borg.getAddress().getPostCode(), hotelLoaded.getAddress().getPostCode());
final Hotel hotelByValue = hotelDAO.findOne("name", "Hotel Borg");
assertNotNull(hotelByValue);
assertEquals(borg.getStartDate(), hotelByValue.getStartDate());
assertTrue(hotelDAO.exists("stars", 3));
final Hotel hilton = new Hotel();
hilton.setName("Hilton Hotel");
hilton.setStars(4);
hilton.setTakesCreditCards(true);
hilton.setStartDate(new Date());
hilton.setType(Hotel.Type.BUSINESS);
final Address hiltonAddress = new Address();
hiltonAddress.setStreet("Some street 44");
hiltonAddress.setPostCode("101");
hilton.setAddress(hiltonAddress);
hilton.getPhoneNumbers().add(new PhoneNumber(354, 1234567, PhoneNumber.Type.PHONE));
hotelDAO.save(hilton);
assertEquals(2, hotelDAO.find().asList().size());
assertEquals(2, hotelDAO.findIds().size());
List<ObjectId> names = hotelDAO.findIds("name", hilton.getName());
assertEquals(1, names.size());
assertEquals(hilton.getId(), names.get(0));
List<ObjectId> stars = hotelDAO.findIds(getDs().find(Hotel.class).field("stars").equal(4));
assertEquals(1, stars.size());
assertEquals(hilton.getId(), stars.get(0));
assertEquals(borg.getId(), hotelDAO.findOneId().getId());
assertEquals(hilton.getId(), hotelDAO.findOneId("name", hilton.getName()).getId());
assertEquals(hilton.getId(), hotelDAO.findOneId(getDs().find(Hotel.class).field("stars").equal(4)).getId());
assertEquals(1, hotelDAO.createQuery().asList(new FindOptions().skip(1).limit(10)).size());
assertEquals(1, hotelDAO.createQuery().asList(new FindOptions().limit(1)).size());
assertTrue(hotelDAO.exists("type", Hotel.Type.BUSINESS));
assertNotNull(hotelDAO.findOne("type", Hotel.Type.LEISURE));
assertEquals(1, hotelDAO.count(hotelDAO.createQuery().field("stars").notEqual(4)));
assertEquals(2, hotelDAO.count(hotelDAO.createQuery().field("stars").lessThan(5)));
assertEquals(1, hotelDAO.count(hotelDAO.createQuery().field("stars").greaterThanOrEq(4)));
assertEquals(2, hotelDAO.count(hotelDAO.createQuery().field("stars").lessThan(5)));
assertEquals(1, hotelDAO.count(hotelDAO.createQuery().field("phoneNumbers").sizeEq(1)));
assertEquals(1, hotelDAO.count(hotelDAO.createQuery().filter("stars", 4).order("address.address_street")));
assertEquals(hilton.getName(), hotelDAO.find(hotelDAO.createQuery().filter("stars", 4).order("address.address_street")).iterator().next().getName());
assertEquals(hilton.getName(), hotelDAO.find(hotelDAO.createQuery().filter("stars", 4).order("-address.address_street")).iterator().next().getName());
assertEquals(hilton.getName(), hotelDAO.find(hotelDAO.createQuery().filter("stars", 4).order("stars, -address.address_street")).iterator().next().getName());
hotelDAO.deleteById(borg.getId());
assertEquals(1, hotelDAO.count());
hotelDAO.getCollection().drop();
assertEquals(0, hotelDAO.count());
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestMapping method testObjectIdKeyedMap.
@Test
public void testObjectIdKeyedMap() throws Exception {
getMorphia().map(ContainsObjectIdKeyMap.class);
final ContainsObjectIdKeyMap map = new ContainsObjectIdKeyMap();
final ObjectId o1 = new ObjectId("111111111111111111111111");
final ObjectId o2 = new ObjectId("222222222222222222222222");
map.values.put(o1, "I'm 1s");
map.values.put(o2, "I'm 2s");
final Key<?> mapKey = getDs().save(map);
final ContainsObjectIdKeyMap mapLoaded = getDs().get(ContainsObjectIdKeyMap.class, mapKey.getId());
assertNotNull(mapLoaded);
assertEquals(2, mapLoaded.values.size());
assertNotNull(mapLoaded.values.get(o1));
assertNotNull(mapLoaded.values.get(o2));
assertNotNull(getDs().find(ContainsIntKeyMap.class).field("values.111111111111111111111111").exists());
assertEquals(0, getDs().find(ContainsIntKeyMap.class).field("values.111111111111111111111111").doesNotExist().count());
assertNotNull(getDs().find(ContainsIntKeyMap.class).field("values.4").doesNotExist());
assertEquals(0, getDs().find(ContainsIntKeyMap.class).field("values.4").exists().count());
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class EmbeddedReferenceType method testQueryFormat.
@Test
@SuppressWarnings("deprecation")
public void testQueryFormat() {
Assume.assumeTrue("This test requires Java 8", JAVA_8);
Query<ReferenceType> query = getDs().find(ReferenceType.class).field("id").equal(new ObjectId(0, 0, (short) 0, 0)).field("referenceType").equal(new ReferenceType(2, "far")).field("embeddedType").equal(new EmbeddedReferenceType(3, "strikes")).field("string").equal("some value").field("embeddedArray").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("embeddedSet").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("embeddedList").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("map.bar").equal(new EmbeddedReferenceType(1, "chance")).field("mapOfList.bar").in(singletonList(new EmbeddedReferenceType(1, "chance"))).field("mapOfList.foo").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 1).filter("text", "chance")).field("selfReference").equal(new ReferenceType(1, "blah")).field("mixedTypeList").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("mixedTypeList").in(singletonList(new EmbeddedReferenceType(1, "chance"))).field("mixedTypeMap.foo").equal(new ReferenceType(3, "strikes")).field("mixedTypeMap.bar").equal(new EmbeddedReferenceType(3, "strikes")).field("mixedTypeMapOfList.bar").in(singletonList(new EmbeddedReferenceType(1, "chance"))).field("mixedTypeMapOfList.foo").elemMatch(getDs().find(EmbeddedReferenceType.class).filter("number", 3).filter("text", "strikes")).field("referenceMap.foo").equal(new ReferenceType(1, "chance")).field("referenceMap.bar").equal(new EmbeddedReferenceType(1, "chance"));
DBObject dbObject = ((QueryImpl) query).getQueryObject();
Assert.assertEquals(BasicDBObject.parse(readFully("/QueryStructure.json")), dbObject);
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestSuperDatastore method testDeleteWillRemoveAnyDocumentWithAMatchingId.
@Test
public void testDeleteWillRemoveAnyDocumentWithAMatchingId() throws Exception {
// given
final String ns = "someCollectionName";
getDb().getCollection(ns).remove(new BasicDBObject());
final Rectangle rect = new Rectangle(10, 10);
ObjectId rectangleId = new ObjectId();
rect.setId(rectangleId);
getAds().save(ns, rect);
final Circle circle = new Circle();
circle.setId(new ObjectId());
getAds().save(ns, circle);
assertEquals(2, getAds().getCount(ns));
// when
getAds().delete(ns, Circle.class, rectangleId);
// then
assertEquals(1, getAds().getCount(ns));
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestSuperDatastore method testFind.
@Test
public void testFind() throws Exception {
final String ns = "hotels";
Rectangle rect = new Rectangle(10, 10);
ObjectId id = new ObjectId();
rect.setId(id);
getDb().getCollection(ns).remove(new BasicDBObject());
getAds().save(ns, rect);
assertEquals(1, getAds().getCount(ns));
Rectangle rectLoaded = getAds().find(ns, Rectangle.class).get();
assertEquals(rect.getId(), rectLoaded.getId());
assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
rect = new Rectangle(2, 1);
//saved to default collection name (kind)
getAds().save(rect);
assertEquals(1, getAds().getCount(rect));
rect.setId(null);
//saved to default collection name (kind)
getAds().save(rect);
assertEquals(2, getAds().getCount(rect));
rect = new Rectangle(4, 3);
getAds().save(ns, rect);
assertEquals(2, getAds().getCount(ns));
rectLoaded = getAds().find(ns, Rectangle.class).asList().get(1);
assertEquals(rect.getId(), rectLoaded.getId());
assertEquals(rect.getArea(), rectLoaded.getArea(), 0);
getAds().find(ns, Rectangle.class, "_id !=", "-1", 1, 1).get();
}
Aggregations