IfScoreResolver implements ScoreResolver<If> { @Override public Integer getScore(final If operation) { throw new UnsupportedOperationException("Default Score Resolver has not been provided."); }  @Override Integer getScore(final If operation); @Override Integer getScore(final If operation, final ScoreResolver defaultScoreResolver);  }
@Test public void shouldGetDefaultScoreWithNoOperationScores() { final IfScoreResolver resolver = new IfScoreResolver(); final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(new LinkedHashMap<>()); final If operation = new If(); final int score = resolver.getScore(operation, defaultResolver); assertEquals(2, score); }
@Test public void shouldGetScoreWithFullyPopulatedOperation() { final Count count = mock(Count.class); final GetAllElements getAllElements = mock(GetAllElements.class); final GetWalks getWalks = mock(GetWalks.class); final Conditional conditional = mock(Conditional.class); given(conditional.getTransform()).willReturn(count); final If operation = new If.Builder<>() .conditional(conditional) .then(getWalks) .otherwise(getAllElements) .build(); final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>(); opScores.put(Count.class, 1); opScores.put(GetAllElements.class, 3); opScores.put(GetWalks.class, 4); final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores); final IfScoreResolver resolver = new IfScoreResolver(); final int score = resolver.getScore(operation, defaultResolver); assertEquals(4, score); }
@Test public void shouldGetScoreWithOperationChainAsAnOperation() { final GetElements getElements = mock(GetElements.class); final ToMap toMap = mock(ToMap.class); final Map map = mock(Map.class); final OperationChain conditionalChain = mock(OperationChain.class); final List<Operation> conditionalOps = new LinkedList<>(); conditionalOps.add(getElements); conditionalOps.add(toMap); conditionalOps.add(map); given(conditionalChain.getOperations()).willReturn(conditionalOps); final Conditional conditional = mock(Conditional.class); given(conditional.getTransform()).willReturn(conditionalChain); final GetAdjacentIds getAdjacentIds = mock(GetAdjacentIds.class); final GetAllElements getAllElements = mock(GetAllElements.class); final If operation = new If.Builder<>() .conditional(conditional) .then(getAdjacentIds) .otherwise(getAllElements) .build(); final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>(); opScores.put(Operation.class, 1); opScores.put(GetElements.class, 2); opScores.put(ToMap.class, 2); opScores.put(Map.class, 3); opScores.put(GetAdjacentIds.class, 3); opScores.put(GetAllElements.class, 4); final IfScoreResolver resolver = new IfScoreResolver(); final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores); final int score = resolver.getScore(operation, defaultResolver); assertEquals(11, score); }
@Test public void shouldGetScoreForNestedOperations() { final Map map = mock(Map.class); final Conditional conditional = mock(Conditional.class); given(conditional.getTransform()).willReturn(map); final GetWalks getWalks = mock(GetWalks.class); given(getWalks.getOperations()).willReturn( Collections.singletonList( new OperationChain<>( new GetAdjacentIds(), new GetAdjacentIds()))); final GetAllElements getAllElements = new GetAllElements(); final If operation = new If.Builder<>() .conditional(conditional) .then(getWalks) .otherwise(getAllElements) .build(); final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>(); opScores.put(Operation.class, 1); opScores.put(Map.class, 3); opScores.put(GetAdjacentIds.class, 2); opScores.put(GetAllElements.class, 3); final IfScoreResolver resolver = new IfScoreResolver(); final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores); final int score = resolver.getScore(operation, defaultResolver); assertEquals(7, score); }
@Test public void shouldThrowErrorWhenNoDefaultResolverConfigured() { final IfScoreResolver resolver = new IfScoreResolver(); final If operation = new If.Builder<>() .conditional(new Conditional()) .then(new GetAllElements()) .build(); try { resolver.getScore(operation); fail("Exception expected"); } catch (final UnsupportedOperationException e) { assertTrue(e.getMessage().contains("Default Score Resolver has not been provided.")); } }