Search in sources :

Example 1 with HotelDAO

use of org.mongodb.morphia.testdaos.HotelDAO 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"));
}
Also used : Address(org.mongodb.morphia.testmodel.Address) HotelDAO(org.mongodb.morphia.testdaos.HotelDAO) Photo(org.mongodb.morphia.query.TestQuery.Photo) Hotel(org.mongodb.morphia.testmodel.Hotel) Date(java.util.Date) Test(org.junit.Test)

Example 2 with HotelDAO

use of org.mongodb.morphia.testdaos.HotelDAO 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());
}
Also used : FindOptions(org.mongodb.morphia.query.FindOptions) Address(org.mongodb.morphia.testmodel.Address) HotelDAO(org.mongodb.morphia.testdaos.HotelDAO) DBObject(com.mongodb.DBObject) Hotel(org.mongodb.morphia.testmodel.Hotel) Date(java.util.Date) Test(org.junit.Test)

Example 3 with HotelDAO

use of org.mongodb.morphia.testdaos.HotelDAO in project morphia by mongodb.

the class TestDAO method testSaveEntityWithId.

@Test
public void testSaveEntityWithId() throws Exception {
    final HotelDAO hotelDAO = new HotelDAO(getMorphia(), getMongoClient());
    final Hotel borg = new Hotel();
    borg.setName("Hotel Borg");
    borg.setStars(4);
    hotelDAO.save(borg);
    final Hotel hotelLoaded = hotelDAO.get(borg.getId());
    hotelLoaded.setStars(5);
    hotelDAO.save(hotelLoaded);
    final Hotel hotelReloaded = hotelDAO.get(borg.getId());
    assertEquals(5, hotelReloaded.getStars());
}
Also used : HotelDAO(org.mongodb.morphia.testdaos.HotelDAO) Hotel(org.mongodb.morphia.testmodel.Hotel) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 HotelDAO (org.mongodb.morphia.testdaos.HotelDAO)3 Hotel (org.mongodb.morphia.testmodel.Hotel)3 Date (java.util.Date)2 Address (org.mongodb.morphia.testmodel.Address)2 DBObject (com.mongodb.DBObject)1 FindOptions (org.mongodb.morphia.query.FindOptions)1 Photo (org.mongodb.morphia.query.TestQuery.Photo)1