FederatedByAggregateMethodInvocationHandlerImpl implements FederatedServiceMethodInvocationHandler { public Object invoke(FederatedServiceCollection collection, Method method, Object[] args) throws ServiceAreaServiceException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { List<Object> results = new ArrayList<Object>(); for (FederatedService service : collection.getAllServices()) results.add(method.invoke(service, args)); return aggregateResults(results); } FederatedByAggregateMethodInvocationHandlerImpl(EMethodAggregationType aggregationType); EMethodAggregationType getAggregationType(); Object invoke(FederatedServiceCollection collection, Method method, Object[] args);  }
@SuppressWarnings("unchecked") @Test public void testList() throws Exception { SimpleFederatedService mockServiceA = Mockito.mock(SimpleFederatedService.class); Mockito.when(mockServiceA.getValuesAsList()).thenReturn(Arrays.asList("a","b")); SimpleFederatedService mockServiceB = Mockito.mock(SimpleFederatedService.class); Mockito.when(mockServiceB.getValuesAsList()).thenReturn(Arrays.asList("c","d", "a")); Set<FederatedService> services = new HashSet<FederatedService>(); services.add(mockServiceA); services.add(mockServiceB); FederatedServiceCollection mockCollection = Mockito.mock(FederatedServiceCollectionImpl.class); Mockito.when(mockCollection.getAllServices()).thenReturn(services); Method method = SimpleFederatedService.class.getDeclaredMethod("getValuesAsList"); FederatedServiceMethodInvocationHandler handler = new FederatedByAggregateMethodInvocationHandlerImpl(EMethodAggregationType.LIST); List<String> results = (List<String>) handler.invoke(mockCollection, method, new Object[] {}); Mockito.verify(mockServiceA).getValuesAsList(); Mockito.verify(mockServiceB).getValuesAsList(); assertEquals(5,results.size()); Collections.sort(results); assertEquals("a",results.get(0)); assertEquals("a",results.get(1)); assertEquals("b",results.get(2)); assertEquals("c",results.get(3)); assertEquals("d",results.get(4)); }
@SuppressWarnings("unchecked") @Test public void testMap() throws Exception { Map<String,String> vA = new HashMap<String, String>(); vA.put("a","a1"); vA.put("b","b2"); Map<String,String> vB = new HashMap<String, String>(); vB.put("c","c3"); vB.put("d","d4"); SimpleFederatedService mockServiceA = Mockito.mock(SimpleFederatedService.class); Mockito.when(mockServiceA.getValuesAsMap()).thenReturn(vA); SimpleFederatedService mockServiceB = Mockito.mock(SimpleFederatedService.class); Mockito.when(mockServiceB.getValuesAsMap()).thenReturn(vB); Set<FederatedService> services = new HashSet<FederatedService>(); services.add(mockServiceA); services.add(mockServiceB); FederatedServiceCollection mockCollection = Mockito.mock(FederatedServiceCollectionImpl.class); Mockito.when(mockCollection.getAllServices()).thenReturn(services); Method method = SimpleFederatedService.class.getDeclaredMethod("getValuesAsMap"); FederatedServiceMethodInvocationHandler handler = new FederatedByAggregateMethodInvocationHandlerImpl(EMethodAggregationType.MAP); Map<String,String> results = (Map<String,String>) handler.invoke(mockCollection, method, new Object[] {}); Mockito.verify(mockServiceA).getValuesAsMap(); Mockito.verify(mockServiceB).getValuesAsMap(); assertEquals(4,results.size()); assertEquals("a1",results.get("a")); assertEquals("b2",results.get("b")); assertEquals("c3",results.get("c")); assertEquals("d4",results.get("d")); }