FederatedOperationHandler implements OperationHandler<Operation> { public Object doOperation(final Operation 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); for (final Graph graph : graphs) { final Operation updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph); if (null != updatedOp) { try { graph.execute(updatedOp, context); } catch (final Exception e) { if (!Boolean.valueOf(getSkipFailedFederatedStoreExecute(updatedOp))) { throw new OperationException(FederatedStoreUtil.createOperationErrorMsg(operation, graph.getGraphId(), e), e); } } } } return null; }  Object doOperation(final Operation operation, final Context context, final Store store);  }
@Test public final void shouldMergeResultsFromFieldObjects() throws Exception { final Operation op = mock(Operation.class); final Operation opClone = mock(Operation.class); given(op.shallowClone()).willReturn(opClone); final OperationChain<?> opChainClone = OperationChain.wrap(opClone); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStore1 = getMockStore(unusedSchema, storeProperties); Store mockStore2 = getMockStore(unusedSchema, storeProperties); Store mockStore3 = getMockStore(unusedSchema, storeProperties); Store mockStore4 = getMockStore(unusedSchema, storeProperties); Graph graph1 = getGraphWithMockStore(mockStore1); Graph graph2 = getGraphWithMockStore(mockStore2); Graph graph3 = getGraphWithMockStore(mockStore3); Graph graph4 = getGraphWithMockStore(mockStore4); FederatedStore mockStore = mock(FederatedStore.class); LinkedHashSet<Graph> linkedGraphs = Sets.newLinkedHashSet(); linkedGraphs.add(graph1); linkedGraphs.add(graph2); linkedGraphs.add(graph3); linkedGraphs.add(graph4); when(mockStore.getGraphs(user, null, op)).thenReturn(linkedGraphs); new FederatedOperationHandler().doOperation(op, context, mockStore); verify(mockStore1).execute(eq(opChainClone), any(Context.class)); verify(mockStore2).execute(eq(opChainClone), any(Context.class)); verify(mockStore3).execute(eq(opChainClone), any(Context.class)); verify(mockStore4).execute(eq(opChainClone), any(Context.class)); }
@Test public final void shouldMergeResultsFromFieldObjectsWithGivenGraphIds() throws Exception { final Operation op = mock(Operation.class); final Operation opClone = mock(Operation.class); given(op.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)).willReturn("1,3"); given(op.shallowClone()).willReturn(opClone); final OperationChain<?> opChainClone = OperationChain.wrap(opClone); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStore1 = getMockStore(unusedSchema, storeProperties); Store mockStore2 = getMockStore(unusedSchema, storeProperties); Store mockStore3 = getMockStore(unusedSchema, storeProperties); Store mockStore4 = getMockStore(unusedSchema, storeProperties); Graph graph1 = getGraphWithMockStore(mockStore1); Graph graph3 = getGraphWithMockStore(mockStore3); FederatedStore mockStore = mock(FederatedStore.class); LinkedHashSet<Graph> filteredGraphs = Sets.newLinkedHashSet(); filteredGraphs.add(graph1); filteredGraphs.add(graph3); when(mockStore.getGraphs(user, "1,3", op)).thenReturn(filteredGraphs); new FederatedOperationHandler().doOperation(op, context, mockStore); verify(mockStore1).execute(eq(opChainClone), any(Context.class)); verify(mockStore2, never()).execute(eq(opChainClone), any(Context.class)); verify(mockStore3).execute(eq(opChainClone), any(Context.class)); verify(mockStore4, never()).execute(eq(opChainClone), any(Context.class)); }
@Test public void shouldThrowException() throws Exception { String message = "test exception"; final Operation op = mock(Operation.class); final String graphID = "1,3"; given(op.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)).willReturn(graphID); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStoreInner = getMockStore(unusedSchema, storeProperties); given(mockStoreInner.execute(any(OperationChain.class), any(Context.class))).willThrow(new RuntimeException(message)); FederatedStore mockStore = mock(FederatedStore.class); HashSet<Graph> filteredGraphs = Sets.newHashSet(getGraphWithMockStore(mockStoreInner)); when(mockStore.getGraphs(user, graphID, op)).thenReturn(filteredGraphs); try { new FederatedOperationHandler().doOperation(op, context, mockStore); fail("Exception Not thrown"); } catch (OperationException e) { assertEquals(message, e.getCause().getMessage()); } }
@Test public void shouldNotThrowExceptionBecauseSkipFlagSetTrue() throws Exception { final String graphID = "1,3"; final Operation op = mock(Operation.class); when(op.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)).thenReturn(graphID); when(op.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE)).thenReturn(String.valueOf(true)); when(op.getOption(eq(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE), any(String.class))).thenReturn(String.valueOf(true)); Schema unusedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); Store mockStore1 = getMockStore(unusedSchema, storeProperties); given(mockStore1.execute(any(OperationChain.class), eq(context))).willReturn(1); Store mockStore2 = getMockStore(unusedSchema, storeProperties); given(mockStore2.execute(any(OperationChain.class), eq(context))).willThrow(new RuntimeException("Test Exception")); FederatedStore mockStore = mock(FederatedStore.class); LinkedHashSet<Graph> filteredGraphs = Sets.newLinkedHashSet(); filteredGraphs.add(getGraphWithMockStore(mockStore1)); filteredGraphs.add(getGraphWithMockStore(mockStore2)); when(mockStore.getGraphs(user, graphID, op)).thenReturn(filteredGraphs); try { new FederatedOperationHandler().doOperation(op, context, mockStore); } catch (Exception e) { fail("Exception should not have been thrown: " + e.getMessage()); } final ArgumentCaptor<Context> contextCaptor1 = ArgumentCaptor.forClass(Context.class); verify(mockStore1, atLeastOnce()).execute(any(OperationChain.class), contextCaptor1.capture()); assertEquals(context.getUser(), contextCaptor1.getValue().getUser()); assertNotEquals(context.getJobId(), contextCaptor1.getValue().getJobId()); final ArgumentCaptor<Context> contextCaptor2 = ArgumentCaptor.forClass(Context.class); verify(mockStore2, atLeastOnce()).execute(any(OperationChain.class), contextCaptor2.capture()); assertEquals(context.getUser(), contextCaptor2.getValue().getUser()); assertNotEquals(context.getJobId(), contextCaptor2.getValue().getJobId()); }