NamedOperationScoreResolver implements ScoreResolver<NamedOperation> { @Override public Integer getScore(final NamedOperation operation) { return getScore(operation, null); } NamedOperationScoreResolver();  NamedOperationScoreResolver(final NamedOperationCache cache); @Override Integer getScore(final NamedOperation operation); @Override Integer getScore(final NamedOperation operation, final ScoreResolver defaultScoreResolver);  }
@Test public void shouldGetScore() throws CacheOperationFailedException { final Integer expectedScore = 5; final String opName = "otherOp"; final NamedOperation<Element, Iterable<? extends Element>> namedOp = mock(NamedOperation.class); namedOp.setOperationName(opName); final NamedOperationDetail namedOpDetail = mock(NamedOperationDetail.class); final NamedOperationCache cache = mock(NamedOperationCache.class); final NamedOperationScoreResolver resolver = new NamedOperationScoreResolver(cache); given(cache.getFromCache(namedOpDetail.getOperationName())).willReturn(namedOpDetail); given(namedOpDetail.getOperationName()).willReturn(opName); given(namedOpDetail.getScore()).willReturn(expectedScore); final Integer result = resolver.getScore(namedOp); assertEquals(expectedScore, result); }
@Test public void shouldGetScoreFromOperationsInParameters() throws CacheOperationFailedException { final Integer expectedScore = 8; final String opName = "otherOp"; final NamedOperation<Element, Iterable<? extends Element>> namedOp = mock(NamedOperation.class); namedOp.setOperationName(opName); Operation operation = new GetAllElements(); Map<String, Object> paramMap = Maps.newHashMap(); paramMap.put("test param", operation); namedOp.setParameters(paramMap); final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>(); opScores.put(GetAllElements.class, 3); final ScoreResolver scoreResolver = new DefaultScoreResolver(opScores); final NamedOperationDetail namedOpDetail = mock(NamedOperationDetail.class); final NamedOperationCache cache = mock(NamedOperationCache.class); final NamedOperationScoreResolver resolver = new NamedOperationScoreResolver(cache); given(cache.getFromCache(namedOpDetail.getOperationName())).willReturn(namedOpDetail); given(namedOpDetail.getOperationName()).willReturn(opName); given(namedOpDetail.getScore()).willReturn(5); final List<Operation> operations = new ArrayList<>(); operations.add(operation); given(namedOp.getOperations()).willReturn(operations); final Integer result = resolver.getScore(namedOp, scoreResolver); assertEquals(expectedScore, result); }
@Test public void shouldDefaultScoreFromOpChainIfNamedOpScoreEmpty() throws CacheOperationFailedException { final String namedOpName = "namedOp"; final Map parametersMap = new HashMap<>(); final Integer expectedScore = 7; final NamedOperation<Element, Iterable<? extends Element>> namedOp = mock(NamedOperation.class); final NamedOperationCache cache = mock(NamedOperationCache.class); final ScoreResolver defaultScoreResolver = mock(DefaultScoreResolver.class); final NamedOperationDetail namedOpDetail = mock(NamedOperationDetail.class); final NamedOperationScoreResolver resolver = new NamedOperationScoreResolver(cache); final OperationChain opChain = new OperationChain(); namedOp.setOperationName(namedOpName); namedOp.setParameters(parametersMap); given(namedOpDetail.getOperationChain(parametersMap)).willReturn(opChain); given(namedOpDetail.getScore()).willReturn(null); given(cache.getFromCache(namedOpDetail.getOperationName())).willReturn(namedOpDetail); given(defaultScoreResolver.getScore(opChain)).willReturn(7); final Integer result = resolver.getScore(namedOp, defaultScoreResolver); assertEquals(expectedScore, result); }
@Test public void shouldCatchExceptionForCacheFailures() { final NamedOperation<Element, Iterable<? extends Element>> namedOp = mock(NamedOperation.class); final NamedOperationScoreResolver resolver = new NamedOperationScoreResolver(); final Integer result = resolver.getScore(namedOp); assertNull(result); }