OperationChainHandler implements OutputOperationHandler<OperationChain<OUT>, OUT> { @Override public OUT doOperation(final OperationChain<OUT> operationChain, final Context context, final Store store) throws OperationException { prepareOperationChain(operationChain, context, store); Object result = null; for (final Operation op : operationChain.getOperations()) { updateOperationInput(op, result); result = store.handleOperation(op, context); } return (OUT) result; } OperationChainHandler(final OperationChainValidator opChainValidator, final List<OperationChainOptimiser> opChainOptimisers); @Override OUT doOperation(final OperationChain<OUT> operationChain, final Context context, final Store store); OperationChain<O> prepareOperationChain(final OperationChain<O> operationChain, final Context context, final Store store);  }
@Test public void shouldHandleOperationChain() throws OperationException { final OperationChainValidator opChainValidator = mock(OperationChainValidator.class); final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList(); final OperationChainHandler opChainHandler = new OperationChainHandler(opChainValidator, opChainOptimisers); final Context context = mock(Context.class); final Store store = mock(Store.class); final User user = mock(User.class); final StoreProperties storeProperties = new StoreProperties(); final GetAdjacentIds op1 = mock(GetAdjacentIds.class); final GetElements op2 = mock(GetElements.class); final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2)); final Entity expectedResult = new Entity(TestGroups.ENTITY); given(context.getUser()).willReturn(user); given(store.getProperties()).willReturn(storeProperties); given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult()); given(store.handleOperation(op1, context)).willReturn(new WrappedCloseableIterable<>(Collections .singletonList(new EntitySeed()))); given(store.handleOperation(op2, context)).willReturn(expectedResult); final Object result = opChainHandler.doOperation(opChain, context, store); assertSame(expectedResult, result); }
@Test public void shouldHandleNonInputOperation() throws OperationException { final OperationChainValidator opChainValidator = mock(OperationChainValidator.class); final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList(); final OperationChainHandler opChainHandler = new OperationChainHandler(opChainValidator, opChainOptimisers); final Context context = mock(Context.class); final Store store = mock(Store.class); final User user = mock(User.class); final StoreProperties storeProperties = new StoreProperties(); final GetAllElements op = mock(GetAllElements.class); final OperationChain opChain = new OperationChain(Collections.singletonList(op)); final Entity expectedResult = new Entity(TestGroups.ENTITY); given(context.getUser()).willReturn(user); given(store.getProperties()).willReturn(storeProperties); given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult()); given(store.handleOperation(op, context)).willReturn(expectedResult); final Object result = opChainHandler.doOperation(opChain, context, store); assertSame(expectedResult, result); }
@Test public void shouldHandleNestedOperationChain() throws OperationException { final OperationChainValidator opChainValidator = mock(OperationChainValidator.class); final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList(); final OperationChainHandler opChainHandler = new OperationChainHandler(opChainValidator, opChainOptimisers); final Context context = mock(Context.class); final Store store = mock(Store.class); final User user = mock(User.class); final StoreProperties storeProperties = new StoreProperties(); final GetAdjacentIds op1 = mock(GetAdjacentIds.class); final GetElements op2 = mock(GetElements.class); final Limit op3 = mock(Limit.class); final OperationChain opChain1 = new OperationChain(Arrays.asList(op1, op2)); final OperationChain opChain2 = new OperationChain(Arrays.asList(opChain1, op3)); final Entity entityA = new Entity.Builder().group(TestGroups.ENTITY).vertex("A").build(); final Entity entityB = new Entity.Builder().group(TestGroups.ENTITY).vertex("B").build(); given(context.getUser()).willReturn(user); given(store.getProperties()).willReturn(storeProperties); given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult()); given(store.handleOperation(op1, context)).willReturn(new WrappedCloseableIterable<>(Lists.newArrayList(new EntitySeed("A"), new EntitySeed("B")))); given(store.handleOperation(op2, context)).willReturn(new WrappedCloseableIterable<>(Lists.newArrayList(entityA, entityB))); given(store.handleOperation(op3, context)).willReturn(entityA); final Object result = opChainHandler.doOperation(opChain2, context, store); assertSame(entityA, result); }