/** * Store and call an arbitrary JavaScript function (in this case a simple echo script) via its * name. */ @Test public void saveAndCallScriptViaName() { operations .scriptOps() .register( new NamedMongoScript( "echoScript", new ExecutableMongoScript("function(x) { return x; }"))); Object o = operations.scriptOps().call("echoScript", "Hello echo...!"); assertThat(o, is((Object) "Hello echo...!")); }
/** * 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)); }