DescribedDataStoreOperation implements DataStoreOperation<Object, K, E> { @Override public Object execute(DataStore<K, E> store, RepositoryConfiguration configuration, Invocation invocation) { log.info("Trying to select the data from the data store"); final List<E> selection = selectOperation.execute(store, configuration, invocation); final QueryDescriptor descriptor = selectOperation.getDescriptor(); if (descriptor.getFunction() == null) { log.info("No function was specified for the current selection"); return selection; } log.info("Executing function " + descriptor.getFunction() + " on the selected items"); final DataFunction<?> function = functionRegistry.getFunction(descriptor.getFunction()); return function.apply(store, descriptor, configuration, selection); } DescribedDataStoreOperation(SelectDataStoreOperation<K, E> selectOperation,
                                       DataFunctionRegistry functionRegistry); @Override Object execute(DataStore<K, E> store, RepositoryConfiguration configuration, Invocation invocation); @Override String toString();  }
@Test public void testExecutionWithoutAnyFunctions() { final DefaultQueryDescriptor descriptor = new DefaultQueryDescriptor(false, null, 0, null, null, null, null, null); final List<Object> selection = new ArrayList<>(); final SpyingSelectDataStoreOperation<Object, Object> operationSpy = new SpyingSelectDataStoreOperation<>( descriptor, selection); final DescribedDataStoreOperation<Object, Object> operation = new DescribedDataStoreOperation<>(operationSpy, null); assertThat(operationSpy.isCalled(), is(false)); final Object result = operation.execute(new MemoryDataStore<>(Object.class), null, null); assertThat(operationSpy.isCalled(), is(true)); assertThat(result, Matchers.is(selection)); }
@Test public void testExecutionWithCustomFunction() { final DefaultQueryDescriptor descriptor = new DefaultQueryDescriptor(false, "xyz", 0, null, null, null, null, null); final List<Object> selection = new ArrayList<>(); final Object transformed = new Object(); final DefaultDataFunctionRegistry functionRegistry = new DefaultDataFunctionRegistry(); final SpyingDataFunction<Object> spy = new SpyingDataFunction<>(new DataFunction<Object>() { @Override public <K, E> Object apply(DataStore<K, E> dataStore, QueryDescriptor query, RepositoryConfiguration configuration, List<E> current) { return transformed; } }); functionRegistry.register("xyz", spy); final SpyingSelectDataStoreOperation<Object, Object> operationSpy = new SpyingSelectDataStoreOperation<>( descriptor, selection); final DescribedDataStoreOperation<Object, Object> operation = new DescribedDataStoreOperation<>(operationSpy, functionRegistry); assertThat(operationSpy.isCalled(), is(false)); final Object result = operation.execute(new MemoryDataStore<>(Object.class), null, null); assertThat(operationSpy.isCalled(), is(true)); assertThat(result, is(transformed)); assertThat(spy.getInvocations(), hasSize(1)); assertThat(spy.getInvocations().get(0).getSelection(), Matchers.<Object>is(selection)); }