@Test public void testFindAndUpdateUpsert() { template.insert(new Person("Tom", 21)); template.insert(new Person("Dick", 22)); Query query = new Query(Criteria.where("firstName").is("Harry")); Update update = new Update().set("age", 23); Person p = template.findAndModify( query, update, new FindAndModifyOptions().upsert(true).returnNew(true), Person.class); assertThat(p.getFirstName(), is("Harry")); assertThat(p.getAge(), is(23)); }
/** @see DATAMONGO-234 */ @Test public void testFindAndUpdate() { template.insert(new Person("Tom", 21)); template.insert(new Person("Dick", 22)); template.insert(new Person("Harry", 23)); Query query = new Query(Criteria.where("firstName").is("Harry")); Update update = new Update().inc("age", 1); Person p = template.findAndModify(query, update, Person.class); // return old assertThat(p.getFirstName(), is("Harry")); assertThat(p.getAge(), is(23)); p = template.findOne(query, Person.class); assertThat(p.getAge(), is(24)); p = template.findAndModify(query, update, Person.class, "person"); assertThat(p.getAge(), is(24)); p = template.findOne(query, Person.class); assertThat(p.getAge(), is(25)); p = template.findAndModify( query, update, new FindAndModifyOptions().returnNew(true), Person.class); assertThat(p.getAge(), is(26)); p = template.findAndModify(query, update, null, Person.class, "person"); assertThat(p.getAge(), is(26)); p = template.findOne(query, Person.class); assertThat(p.getAge(), is(27)); Query query2 = new Query(Criteria.where("firstName").is("Mary")); p = template.findAndModify( query2, update, new FindAndModifyOptions().returnNew(true).upsert(true), Person.class); assertThat(p.getFirstName(), is("Mary")); assertThat(p.getAge(), is(1)); }
@Test @FileParameters(value = "src/test/resources/test.csv", mapper = PersonMapper.class) public void loadParamsFromFileWithCustomMapper(Person person) { assertTrue(person.getAge() > 0); }
@Test public void multipleOperations_ShouldReturnWorkCorrectly() { boolean isAdded = persons.addPerson("*****@*****.**", "Pesho", 28, "Plovdiv"); assertTrue(isAdded); assertEquals(1, persons.size()); isAdded = persons.addPerson("*****@*****.**", "Pesho2", 222, "Plovdiv222"); assertFalse(isAdded); assertEquals(1, persons.size()); persons.addPerson("*****@*****.**", "Kiril", 22, "Plovdiv"); assertEquals(2, persons.size()); persons.addPerson("*****@*****.**", "Asen", 22, "Sofia"); assertEquals(3, persons.size()); Person person = persons.findPerson("non-existing person"); assertNull(person); person = persons.findPerson("*****@*****.**"); assertNotNull(person); assertEquals("*****@*****.**", person.getEmail()); assertEquals("Pesho", person.getName()); assertEquals(28, person.getAge()); assertEquals("Plovdiv", person.getTown()); List<Person> personsGmail = (List<Person>) persons.findPersons("gmail.com"); assertArrayEquals( new String[] {"*****@*****.**", "*****@*****.**"}, personsGmail.stream().map(p -> p.getEmail()).toArray()); List<Person> personsPeshoPlovdiv = (List<Person>) persons.findPersons("Pesho", "Plovdiv"); assertArrayEquals( new String[] {"*****@*****.**"}, personsPeshoPlovdiv.stream().map(p -> p.getEmail()).toArray()); List<Person> personsPeshoSofia = (List<Person>) persons.findPersons("Pesho", "Sofia"); assertEquals(0, personsPeshoSofia.size()); List<Person> personsKiroPlovdiv = (List<Person>) persons.findPersons("Kiro", "Plovdiv"); assertEquals(0, personsKiroPlovdiv.size()); List<Person> personsAge22To28 = (List<Person>) persons.findPersons(22, 28); assertArrayEquals( new String[] {"*****@*****.**", "*****@*****.**", "*****@*****.**"}, personsAge22To28.stream().map(p -> p.getEmail()).toArray()); List<Person> personsAge22To28Plovdiv = (List<Person>) persons.findPersons(22, 28, "Plovdiv"); assertArrayEquals( new String[] {"*****@*****.**", "*****@*****.**"}, personsAge22To28Plovdiv.stream().map(p -> p.getEmail()).toArray()); boolean isDeleted = persons.deletePerson("*****@*****.**"); assertTrue(isDeleted); isDeleted = persons.deletePerson("*****@*****.**"); assertFalse(isDeleted); person = persons.findPerson("*****@*****.**"); assertNull(person); personsGmail = (List<Person>) persons.findPersons("gmail.com"); assertArrayEquals( new String[] {"*****@*****.**"}, personsGmail.stream().map(p -> p.getEmail()).toArray()); personsPeshoPlovdiv = (List<Person>) persons.findPersons("Pesho", "Plovdiv"); assertArrayEquals( new String[] {}, personsPeshoPlovdiv.stream().map(p -> p.getEmail()).toArray()); personsPeshoSofia = (List<Person>) persons.findPersons("Pesho", "Sofia"); assertEquals(0, personsPeshoSofia.size()); personsKiroPlovdiv = (List<Person>) persons.findPersons("Kiro", "Plovdiv"); assertEquals(0, personsKiroPlovdiv.size()); personsAge22To28 = (List<Person>) persons.findPersons(22, 28); assertArrayEquals( new String[] {"*****@*****.**", "*****@*****.**"}, personsAge22To28.stream().map(p -> p.getEmail()).toArray()); personsAge22To28Plovdiv = (List<Person>) persons.findPersons(22, 28, "Plovdiv"); assertArrayEquals( new String[] {"*****@*****.**"}, personsAge22To28Plovdiv.stream().map(p -> p.getEmail()).toArray()); }