/** * Use a script execution to create an atomic put-if-absent operation that fulfills the contract * of {@link Map#putIfAbsent(Object, Object)} */ @Test public void complexScriptExecutionSimulatingPutIfAbsent() { Customer ned = new Customer("Ned", "Stark"); ned.setId("ned-stark"); // #1: on first insert null has to be returned assertThat(operations.scriptOps().execute(createExecutablePutIfAbsentScript(ned)), nullValue()); // #2: change the firstname and put the object again, we expect a return value. ned.setFirstname("Eddard"); assertThat( operations.scriptOps().execute(createExecutablePutIfAbsentScript(ned)), notNullValue()); // #3: make sure the entity has not been altered by #2 assertThat(repository.findOne(ned.getId()).getFirstname(), is("Ned")); assertThat(repository.count(), is(1L)); }
@Before public void setUp() { if (!operations.collectionExists(Customer.class)) { operations.createCollection(Customer.class); } // just make sure we remove everything properly operations.getCollection("system.js").remove(new BasicDBObject()); repository.deleteAll(); }