WhileScoreResolver implements ScoreResolver<While> { @Override public Integer getScore(final While operation) { throw new UnsupportedOperationException("Default Score Resolver has not been provided."); }  @Override Integer getScore(final While operation); @Override Integer getScore(final While operation, final ScoreResolver defaultScoreResolver);  }
@Test public void shouldGetDefaultScoreWithNoOperationScores() { final WhileScoreResolver resolver = new WhileScoreResolver(); final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(new LinkedHashMap<>()); final While operation = new While(); final int score = resolver.getScore(operation, defaultResolver); assertEquals(0, score); }
@Test public void shouldGetScoreWithFullyPopulatedOperation() { final Object input = new EntitySeed(2); final Count count = mock(Count.class); final int repeats = 5; final GetElements getElements = mock(GetElements.class); final Conditional conditional = mock(Conditional.class); given(conditional.getTransform()).willReturn(count); final While operation = new While.Builder<>() .input(input) .conditional(conditional) .maxRepeats(repeats) .operation(getElements) .build(); final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>(); opScores.put(Count.class, 1); opScores.put(GetElements.class, 2); final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores); final WhileScoreResolver resolver = new WhileScoreResolver(); final int score = resolver.getScore(operation, defaultResolver); assertEquals(15, score); }
@Test public void shouldGetScoreWithOperationChainAsOperation() { final Object input = new EntitySeed(3); final int repeats = 3; final GetElements getElements = mock(GetElements.class); final Map map = mock(Map.class); final ToSet toSet = mock(ToSet.class); final OperationChain transformChain = mock(OperationChain.class); final List<Operation> transformOps = new LinkedList<>(); transformOps.add(map); transformOps.add(toSet); given(transformChain.getOperations()).willReturn(transformOps); final Conditional conditional = mock(Conditional.class); given(conditional.getTransform()).willReturn(transformChain); final While operation = new While.Builder<>() .input(input) .maxRepeats(repeats) .conditional(conditional) .operation(getElements) .build(); final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>(); opScores.put(Operation.class, 1); opScores.put(Map.class, 3); opScores.put(ToSet.class, 1); opScores.put(GetElements.class, 2); final WhileScoreResolver resolver = new WhileScoreResolver(); final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores); final int score = resolver.getScore(operation, defaultResolver); assertEquals(18, score); }
@Test public void shouldThrowErrorWhenNoDefaultResolverConfigured() { final WhileScoreResolver resolver = new WhileScoreResolver(); final While operation = new While.Builder<>() .conditional(new Conditional()) .operation(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.")); } }