FederatedOperationOutputHandler implements OutputOperationHandler<OP, O> { @Override public O doOperation(final OP operation, final Context context, final Store store) throws OperationException { final Collection<Graph> graphs = ((FederatedStore) store).getGraphs(context.getUser(), operation.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS), operation); final List<O> results = new ArrayList<>(graphs.size()); for (final Graph graph : graphs) { final OP updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); if (null != updatedOp) { O execute = null; try { execute = graph.execute(updatedOp, context); } catch (final Exception e) { if (!Boolean.valueOf(getSkipFailedFederatedStoreExecute(updatedOp))) { throw new OperationException(FederatedStoreUtil.createOperationErrorMsg(operation, graph.getGraphId(), e), e); } } if (null != execute) { results.add(execute); } } } try { return mergeResults(results, operation, context, store); } catch (final Exception e) { throw new OperationException(e); } }  @Override O doOperation(final OP operation, final Context context, final Store store);  }
@Test public void shouldMergeResultsFromFieldObjects() throws Exception { final OP op = getExampleOperation(); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStore1 = getMockStore(unusedSchema, storeProperties, o1); Store mockStore2 = getMockStore(unusedSchema, storeProperties, o2); Store mockStore3 = getMockStore(unusedSchema, storeProperties, o3); Store mockStore4 = getMockStore(unusedSchema, storeProperties, o4); FederatedStore mockStore = Mockito.mock(FederatedStore.class); LinkedHashSet<Graph> linkedGraphs = Sets.newLinkedHashSet(); linkedGraphs.add(getGraphWithMockStore(mockStore1)); linkedGraphs.add(getGraphWithMockStore(mockStore2)); linkedGraphs.add(getGraphWithMockStore(mockStore3)); linkedGraphs.add(getGraphWithMockStore(mockStore4)); Mockito.when(mockStore.getGraphs(user, null, op)).thenReturn(linkedGraphs); O theMergedResultsOfOperation = getFederatedHandler().doOperation(op, context, mockStore); validateMergeResultsFromFieldObjects(theMergedResultsOfOperation, o1, o2, o3, o4); verify(mockStore1).execute(any(OperationChain.class), any(Context.class)); verify(mockStore2).execute(any(OperationChain.class), any(Context.class)); verify(mockStore3).execute(any(OperationChain.class), any(Context.class)); verify(mockStore4).execute(any(OperationChain.class), any(Context.class)); }
@Test public void shouldMergeResultsFromFieldObjectsWithGivenGraphIds() throws Exception { final OP op = getExampleOperation(); op.addOption(KEY_OPERATION_OPTIONS_GRAPH_IDS, "1,3"); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStore1 = getMockStore(unusedSchema, storeProperties, o1); Store mockStore2 = getMockStore(unusedSchema, storeProperties, o2); Store mockStore3 = getMockStore(unusedSchema, storeProperties, o3); Store mockStore4 = getMockStore(unusedSchema, storeProperties, o4); FederatedStore mockStore = Mockito.mock(FederatedStore.class); LinkedHashSet<Graph> filteredGraphs = Sets.newLinkedHashSet(); filteredGraphs.add(getGraphWithMockStore(mockStore1)); filteredGraphs.add(getGraphWithMockStore(mockStore3)); Mockito.when(mockStore.getGraphs(user, "1,3", op)).thenReturn(filteredGraphs); O theMergedResultsOfOperation = getFederatedHandler().doOperation(op, context, mockStore); validateMergeResultsFromFieldObjects(theMergedResultsOfOperation, o1, o3); verify(mockStore1).execute(any(OperationChain.class), any(Context.class)); verify(mockStore2, never()).execute(any(OperationChain.class), any(Context.class)); verify(mockStore3).execute(any(OperationChain.class), any(Context.class)); verify(mockStore4, never()).execute(any(OperationChain.class), any(Context.class)); }
@Test public void shouldThrowException() throws Exception { final String message = "Test Exception"; final OP op = getExampleOperation(); op.addOption(KEY_OPERATION_OPTIONS_GRAPH_IDS, TEST_GRAPH_ID); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStoreInner = Mockito.mock(Store.class); given(mockStoreInner.getSchema()).willReturn(unusedSchema); given(mockStoreInner.getProperties()).willReturn(storeProperties); given(mockStoreInner.execute(any(OperationChain.class), any(Context.class))).willThrow(new RuntimeException(message)); FederatedStore mockStore = Mockito.mock(FederatedStore.class); HashSet<Graph> filteredGraphs = Sets.newHashSet(getGraphWithMockStore(mockStoreInner)); Mockito.when(mockStore.getGraphs(user, TEST_GRAPH_ID, op)).thenReturn(filteredGraphs); try { getFederatedHandler().doOperation(op, context, mockStore); fail("Exception not thrown"); } catch (OperationException e) { assertEquals(message, e.getCause().getMessage()); } }
@Test public void shouldReturnEmptyIterableWhenNoResults() throws Exception { final OP op = getExampleOperation(); op.addOption(KEY_OPERATION_OPTIONS_GRAPH_IDS, TEST_GRAPH_ID); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStoreInner = Mockito.mock(Store.class); given(mockStoreInner.getSchema()).willReturn(unusedSchema); given(mockStoreInner.getProperties()).willReturn(storeProperties); given(mockStoreInner.execute(any(OperationChain.class), eq(context))).willReturn(null); FederatedStore mockStore = Mockito.mock(FederatedStore.class); HashSet<Graph> filteredGraphs = Sets.newHashSet(getGraphWithMockStore(mockStoreInner)); Mockito.when(mockStore.getGraphs(user, TEST_GRAPH_ID, op)).thenReturn(filteredGraphs); final O results = getFederatedHandler().doOperation(op, context, mockStore); assertEquals(0, Iterables.size((Iterable) results)); }
@Test public void shouldNotThrowException() throws Exception { final OP op = getExampleOperation(); op.addOption(KEY_OPERATION_OPTIONS_GRAPH_IDS, "1,3"); op.addOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE, String.valueOf(true)); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStore1 = getMockStore(unusedSchema, storeProperties, o1); Store mockStore2 = getMockStore(unusedSchema, storeProperties, o2); Store mockStore3 = Mockito.mock(Store.class); given(mockStore3.getSchema()).willReturn(unusedSchema); given(mockStore3.getProperties()).willReturn(storeProperties); given(mockStore3.execute(any(OperationChain.class), eq(context))).willThrow(new RuntimeException("Test Exception")); Store mockStore4 = getMockStore(unusedSchema, storeProperties, o4); FederatedStore mockStore = Mockito.mock(FederatedStore.class); LinkedHashSet<Graph> filteredGraphs = Sets.newLinkedHashSet(); filteredGraphs.add(getGraphWithMockStore(mockStore1)); filteredGraphs.add(getGraphWithMockStore(mockStore3)); Mockito.when(mockStore.getGraphs(user, "1,3", op)).thenReturn(filteredGraphs); O theMergedResultsOfOperation = null; try { theMergedResultsOfOperation = getFederatedHandler().doOperation(op, context, mockStore); } catch (Exception e) { fail("Exception should not have been thrown: " + e.getMessage()); } validateMergeResultsFromFieldObjects(theMergedResultsOfOperation, o1); ArgumentCaptor<Context> context1Captor = ArgumentCaptor.forClass(Context.class); verify(mockStore1).execute(any(OperationChain.class), context1Captor.capture()); assertNotEquals(context.getJobId(), context1Captor.getValue().getJobId()); assertEquals(context.getUser(), context1Captor.getValue().getUser()); verify(mockStore2, never()).execute(any(OperationChain.class), any(Context.class)); ArgumentCaptor<Context> context3Captor = ArgumentCaptor.forClass(Context.class); verify(mockStore3).execute(any(OperationChain.class), context3Captor.capture()); assertNotEquals(context.getJobId(), context3Captor.getValue().getJobId()); assertEquals(context.getUser(), context3Captor.getValue().getUser()); verify(mockStore4, never()).execute(any(OperationChain.class), any(Context.class)); }