use of org.mongodb.morphia.testmodel.Address 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.mongodb.morphia.testmodel.Address in project morphia by mongodb.
the class TestDAO method testExists.
@Test
public void testExists() throws Exception {
final HotelDAO hotelDAO = new HotelDAO(getMorphia(), getMongoClient());
Hotel borg = new Hotel();
borg.setName("Hotel Borg");
borg.setStars(4);
borg.setTakesCreditCards(true);
borg.setStartDate(new Date());
borg.setType(Hotel.Type.LEISURE);
final Address address = new Address();
address.setStreet("Posthusstraeti 11");
address.setPostCode("101");
address.setSecretWord("philodendron");
borg.setAddress(address);
hotelDAO.save(borg);
Hotel grand = new Hotel();
grand.setName("The Grand Budapest Hotel");
grand.setStars(5);
grand.setStartDate(new Date());
grand.setType(Hotel.Type.LEISURE);
getDs().save(new Photo());
hotelDAO.save(grand);
assertTrue(hotelDAO.exists(getDs().find(Hotel.class).field("address").exists()));
assertFalse(hotelDAO.exists("name", "Hotel California"));
}
use of org.mongodb.morphia.testmodel.Address in project morphia by mongodb.
the class TestDAO method testDAO.
@Test
public void testDAO() throws Exception {
getMorphia().map(Hotel.class);
final Hotel borg = new Hotel();
borg.setName("Hotel Borg");
borg.setStars(4);
borg.setTakesCreditCards(true);
borg.setStartDate(new Date());
borg.setType(Hotel.Type.LEISURE);
final Address address = new Address();
address.setStreet("Posthusstraeti 11");
address.setPostCode("101");
address.setSecretWord("philodendron");
borg.setAddress(address);
final HotelDAO hotelDAO = new HotelDAO(getMorphia(), getMongoClient());
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 DBObject dbObject = getMorphia().toDBObject(borg);
assertNull(((DBObject) dbObject.get("address")).get("secretWord"));
Assert.assertNull(hotelLoaded.getAddress().getSecretWord());
final Hotel hotelByValue = hotelDAO.findOne("name", "Hotel Borg");
assertNotNull(hotelByValue);
assertEquals(borg.getStartDate(), hotelByValue.getStartDate());
assertTrue(hotelDAO.exists("stars", 4));
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);
hotelDAO.save(hilton);
final List<Hotel> allHotels = hotelDAO.find().asList();
assertEquals(2, allHotels.size());
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));
// try updating
final UpdateOperations<Hotel> mods = hotelDAO.createUpdateOperations().inc("stars", 1);
hotelDAO.update(hotelDAO.createQuery().filter("stars", 4), mods);
assertEquals(2, hotelDAO.count(hotelDAO.createQuery().filter("stars", 5)));
hotelDAO.deleteById(borg.getId());
assertEquals(1, hotelDAO.count());
hotelDAO.getCollection().drop();
assertEquals(0, hotelDAO.count());
}
use of org.mongodb.morphia.testmodel.Address in project morphia by mongodb.
the class TestDatastore method testEmbedded.
@Test
public void testEmbedded() throws Exception {
getDs().delete(getDs().find(Hotel.class));
final Hotel borg = new Hotel();
borg.setName("Hotel Borg");
borg.setStars(4);
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);
getDs().save(borg);
assertEquals(1, getDs().getCount(Hotel.class));
assertNotNull(borg.getId());
final Hotel hotelLoaded = getDs().get(Hotel.class, borg.getId());
assertEquals(borg.getName(), hotelLoaded.getName());
assertEquals(borg.getAddress().getPostCode(), hotelLoaded.getAddress().getPostCode());
}
use of org.mongodb.morphia.testmodel.Address in project morphia by mongodb.
the class TestMapping method performBasicMappingTest.
private void performBasicMappingTest() {
final DBCollection hotels = getDb().getCollection("hotels");
final DBCollection agencies = getDb().getCollection("agencies");
getMorphia().map(Hotel.class);
getMorphia().map(TravelAgency.class);
final Hotel borg = new Hotel();
borg.setName("Hotel Borg");
borg.setStars(4);
borg.setTakesCreditCards(true);
borg.setStartDate(new Date());
borg.setType(Hotel.Type.LEISURE);
borg.getTags().add("Swimming pool");
borg.getTags().add("Room service");
borg.setTemp("A temporary transient value");
borg.getPhoneNumbers().add(new PhoneNumber(354, 5152233, PhoneNumber.Type.PHONE));
borg.getPhoneNumbers().add(new PhoneNumber(354, 5152244, PhoneNumber.Type.FAX));
final Address address = new Address();
address.setStreet("Posthusstraeti 11");
address.setPostCode("101");
borg.setAddress(address);
BasicDBObject hotelDbObj = (BasicDBObject) getMorphia().toDBObject(borg);
assertTrue(!(((DBObject) ((List) hotelDbObj.get("phoneNumbers")).get(0)).containsField(Mapper.CLASS_NAME_FIELDNAME)));
hotels.save(hotelDbObj);
Hotel borgLoaded = getMorphia().fromDBObject(getDs(), Hotel.class, hotelDbObj, new DefaultEntityCache());
assertEquals(borg.getName(), borgLoaded.getName());
assertEquals(borg.getStars(), borgLoaded.getStars());
assertEquals(borg.getStartDate(), borgLoaded.getStartDate());
assertEquals(borg.getType(), borgLoaded.getType());
assertEquals(borg.getAddress().getStreet(), borgLoaded.getAddress().getStreet());
assertEquals(borg.getTags().size(), borgLoaded.getTags().size());
assertEquals(borg.getTags(), borgLoaded.getTags());
assertEquals(borg.getPhoneNumbers().size(), borgLoaded.getPhoneNumbers().size());
assertEquals(borg.getPhoneNumbers().get(1), borgLoaded.getPhoneNumbers().get(1));
assertNull(borgLoaded.getTemp());
assertTrue(borgLoaded.getPhoneNumbers() instanceof Vector);
assertNotNull(borgLoaded.getId());
final TravelAgency agency = new TravelAgency();
agency.setName("Lastminute.com");
agency.getHotels().add(borgLoaded);
final BasicDBObject agencyDbObj = (BasicDBObject) getMorphia().toDBObject(agency);
agencies.save(agencyDbObj);
final TravelAgency agencyLoaded = getMorphia().fromDBObject(getDs(), TravelAgency.class, agencies.findOne(new BasicDBObject(Mapper.ID_KEY, agencyDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
assertEquals(agency.getName(), agencyLoaded.getName());
assertEquals(1, agency.getHotels().size());
assertEquals(agency.getHotels().get(0).getName(), borg.getName());
// try clearing values
borgLoaded.setAddress(null);
borgLoaded.getPhoneNumbers().clear();
borgLoaded.setName(null);
hotelDbObj = (BasicDBObject) getMorphia().toDBObject(borgLoaded);
hotels.save(hotelDbObj);
hotelDbObj = (BasicDBObject) hotels.findOne(new BasicDBObject(Mapper.ID_KEY, hotelDbObj.get(Mapper.ID_KEY)));
borgLoaded = getMorphia().fromDBObject(getDs(), Hotel.class, hotelDbObj, new DefaultEntityCache());
assertNull(borgLoaded.getAddress());
assertEquals(0, borgLoaded.getPhoneNumbers().size());
assertNull(borgLoaded.getName());
}
Aggregations