MapHandler implements OutputOperationHandler<Map<I, O>, O> { @Override public O doOperation(final Map<I, O> operation, final Context context, final Store store) throws OperationException { if (null == operation) { throw new OperationException("Operation cannot be null"); } Object input = operation.getInput(); if (null == input) { throw new OperationException("Input cannot be null"); } try { for (final Function function : operation.getFunctions()) { if (null == function) { throw new OperationException("Function cannot be null"); } input = function.apply(input); } return (O) input; } catch (final ClassCastException c) { throw new OperationException("The input/output types of the functions were incompatible", c); } }  @Override O doOperation(final Map<I, O> operation, final Context context, final Store store);  }
@Test public void shouldHandleNullOperation() { final MapHandler<Integer, Integer> handler = new MapHandler<>(); final Map<Integer, Integer> operation = null; try { handler.doOperation(operation, context, store); } catch (final OperationException e) { assertTrue(e.getMessage().contains("Operation cannot be null")); } }
@Test public void shouldHandleNullInput() { final MapHandler<Integer, Integer> handler = new MapHandler<>(); final Map<Integer, Integer> operation = new Map.Builder<Integer>() .input(null) .first(function) .build(); try { handler.doOperation(operation, context, store); } catch (final OperationException e) { assertTrue(e.getMessage().contains("Input cannot be null")); } }
@Test public void shouldHandleNullFunction() { final MapHandler<Integer, Integer> handler = new MapHandler<>(); function = null; final Map<Integer, Integer> operation = new Map.Builder<Integer>() .input(input) .first(function) .build(); try { handler.doOperation(operation, context, store); } catch (final OperationException e) { assertTrue(e.getMessage().contains("Function cannot be null")); } }
@Test public void shouldReturnItemFromOperationWithMockFunction() throws OperationException { final MapHandler<Integer, Integer> handler = new MapHandler<>(); final Map<Integer, Integer> operation = new Map.Builder<Integer>() .input(input) .first(function) .build(); final Integer result = handler.doOperation(operation, context, store); assertNotNull(result); assertEquals(new Integer(6), result); }
@Test public void shouldMapSingleObject() throws OperationException { final MapHandler<Integer, String> handler = new MapHandler<>(); final Map<Integer, String> operation = new Map.Builder<Integer>() .input(7) .first(Object::toString) .build(); final String result = handler.doOperation(operation, context, store); assertNotNull(result); assertEquals("7", result); }
@Test public void shouldMapMultipleObjectsAtOnce() throws OperationException { final MapHandler<Iterable<Integer>, String> handler = new MapHandler<>(); final Map<Iterable<Integer>, String> operation = new Map.Builder<Iterable<Integer>>() .input(Arrays.asList(1, 2)) .first(Object::toString) .build(); final String result = handler.doOperation(operation, context, store); assertNotNull(result); assertEquals("[1, 2]", result); }
@Test public void shouldMapMultipleObjects() throws OperationException { final MapHandler<Iterable<Integer>, Iterable<String>> handler = new MapHandler<>(); final Map<Iterable<Integer>, Iterable<String>> operation = new Map.Builder<Iterable<Integer>>() .input(Arrays.asList(1, 2)) .first(new IterableFunction<Integer, String>(Object::toString)) .build(); final Iterable<String> result = handler.doOperation(operation, context, store); assertNotNull(result); assertEquals(Arrays.asList("1", "2"), Lists.newArrayList(result)); }
@Test public void shouldExtractFirstItem() throws OperationException { final MapHandler<Iterable<Iterable<Integer>>, Iterable<Integer>> handler = new MapHandler<>(); final Map<Iterable<Iterable<Integer>>, Iterable<Integer>> operation = new Map.Builder<Iterable<Iterable<Integer>>>() .input(Arrays.asList( Arrays.asList(1, 2), Arrays.asList(3, 4))) .first(new FirstItem<>()) .build(); final Iterable<Integer> result = handler.doOperation(operation, context, store); assertNotNull(result); assertEquals(Arrays.asList(1, 2), Lists.newArrayList(result)); }
@Test public void shouldFlatMapMultipleObjects() throws OperationException { final MapHandler<Iterable<Iterable<Integer>>, Iterable<Integer>> handler = new MapHandler<>(); final Map<Iterable<Iterable<Integer>>, Iterable<Integer>> operation = new Map.Builder<Iterable<Iterable<Integer>>>() .input(Arrays.asList( Arrays.asList(1, 2), Arrays.asList(3, 4))) .first(new IterableConcat<>()) .build(); final Iterable<Integer> result = handler.doOperation(operation, context, store); assertNotNull(result); assertEquals(Arrays.asList(1, 2, 3, 4), Lists.newArrayList(result)); }
@Test public void shouldReturnIterableFromOperation() throws OperationException { final Iterable<Iterable<Integer>> input = Arrays.asList( Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9)); final MapHandler<Iterable<Iterable<Integer>>, String> handler = new MapHandler<>(); final Map<Iterable<Iterable<Integer>>, String> operation = new Map.Builder<Iterable<Iterable<Integer>>>() .input(input) .first(new IterableFunction.Builder<Iterable<Integer>>() .first(new NthItem<>(1)) .then(Object::toString) .build()) .then(new NthItem<>(2)) .build(); final String results = handler.doOperation(operation, context, store); assertNotNull(results); assertEquals("8", results); }
@Test public void shouldProcessWalkObjects() throws OperationException { final Iterable<Iterable<Set<Edge>>> walks = Arrays.asList(walk, walk1); final Map<Iterable<Iterable<Set<Edge>>>, Iterable<Set<Edge>>> map = new Map.Builder<Iterable<Iterable<Set<Edge>>>>() .input(walks) .first(new IterableFunction.Builder<Iterable<Set<Edge>>>() .first(new FirstItem<>()) .build()) .build(); final MapHandler<Iterable<Iterable<Set<Edge>>>, Iterable<Set<Edge>>> handler = new MapHandler<>(); final Iterable<Set<Edge>> results = handler.doOperation(map, context, store); final Iterable<Iterable<Edge>> expectedResults = Arrays.asList( Sets.newHashSet(EDGE_AB), Sets.newHashSet(EDGE_CB)); assertNotNull(results); assertEquals(expectedResults, Lists.newArrayList(results)); }
@Test public void shouldProcessWalksInOperationChain() throws OperationException { final Iterable<Iterable<Set<Edge>>> walks = Arrays.asList(walk, walk1); final Map<Iterable<Iterable<Set<Edge>>>, Iterable<Edge>> map = new Map.Builder<Iterable<Iterable<Set<Edge>>>>() .input(walks) .first(new IterableFunction.Builder<Iterable<Set<Edge>>>() .first(new FirstItem<>()) .then(new FirstItem<>()) .build()) .build(); final ToVertices toVertices = new ToVertices.Builder() .edgeVertices(ToVertices.EdgeVertices.SOURCE) .build(); final ToSet<Object> toSet = new ToSet<>(); final OperationChain<Set<?>> opChain = new OperationChain.Builder() .first(map) .then(toVertices) .then(toSet) .build(); final OperationChainValidator opChainValidator = mock(OperationChainValidator.class); final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList(); given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult()); final OperationChainHandler<Set<?>> opChainHandler = new OperationChainHandler<>(opChainValidator, opChainOptimisers); given(store.handleOperation(map, context)).willReturn(Arrays.asList(EDGE_AB, EDGE_CB)); given(store.handleOperation(toVertices, context)).willReturn(Arrays.asList("A", "C")); given(store.handleOperation(toSet, context)).willReturn(Sets.newHashSet("A", "C")); final Iterable<?> results = opChainHandler.doOperation(opChain, context, store); assertThat(results, containsInAnyOrder("A", "C")); }
@Test public void shouldProcessWalksWithEdgeExtraction() throws OperationException { final Iterable<Walk> walks = Arrays.asList(walk, walk1); final Map<Iterable<Walk>, Iterable<Edge>> map = new Map.Builder<Iterable<Walk>>() .input(walks) .first(new IterableFunction.Builder<Walk>() .first(new ExtractWalkEdgesFromHop(1)) .then(new FirstItem<>()) .build()) .build(); final ToVertices toVertices = new ToVertices.Builder() .edgeVertices(ToVertices.EdgeVertices.SOURCE) .build(); final ToSet<Object> toSet = new ToSet<>(); final OperationChain<Set<?>> opChain = new OperationChain.Builder() .first(map) .then(toVertices) .then(toSet) .build(); final OperationChainValidator opChainValidator = mock(OperationChainValidator.class); final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList(); given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult()); final OperationChainHandler<Set<?>> opChainHandler = new OperationChainHandler<>(opChainValidator, opChainOptimisers); given(store.handleOperation(map, context)).willReturn(Arrays.asList(EDGE_BC, EDGE_BD)); given(store.handleOperation(toVertices, context)).willReturn(Arrays.asList("B", "B")); given(store.handleOperation(toSet, context)).willReturn(Sets.newHashSet("B", "B")); final Iterable<?> results = opChainHandler.doOperation(opChain, context, store); assertThat(results, contains("B")); }
@Test public void shouldBuildWithInvalidArgumentsAndFailExecution() throws OperationException { final List<Function> functions = new ArrayList<>(); final Function<Long, Double> func = Double::longBitsToDouble; final Function<String, Integer> func1 = Integer::valueOf; functions.add(new ToString()); functions.add(func); functions.add(func1); final Map map = new Map(); map.setInput(3); map.setFunctions(functions); final MapHandler handler = new MapHandler(); try { final Object result = handler.doOperation(map, context, store); fail("Exception expected"); } catch (final OperationException e) { assertTrue(e.getMessage().contains("The input/output types of the functions were incompatible")); } }