ScoreOperationChainHandler implements OutputOperationHandler<ScoreOperationChain, Integer> { @Override public Integer doOperation(final ScoreOperationChain operation, final Context context, final Store store) throws OperationException { return getChainScore(operation.getOperationChain(), context.getUser()); }  @Override Integer doOperation(final ScoreOperationChain operation, final Context context, final Store store); int getChainScore(final Operations<?> operations, final User user); int getMaxUserAuthScore(final Set<String> opAuths); @JsonIgnore Map<Class<? extends Operation>, Integer> getOpScores(); @JsonIgnore void setOpScores(final Map<Class<? extends Operation>, Integer> opScores); @JsonGetter("opScores") Map<String, Integer> getOpScoresAsStrings(); @JsonSetter("opScores") void setOpScoresFromStrings(final Map<String, Integer> opScores); Map<String, Integer> getAuthScores(); void setAuthScores(final Map<String, Integer> authScores); Map<Class<? extends Operation>, ScoreResolver> getScoreResolvers(); void setScoreResolvers(final Map<Class<? extends Operation>, ScoreResolver> resolvers); void validateOpScores(); static Map<Class<? extends Operation>, ScoreResolver> getDefaultScoreResolvers();  }
@Test public void shouldExecuteScoreChainOperation() throws OperationException { final ScoreOperationChainHandler operationHandler = new ScoreOperationChainHandler(); final Context context = mock(Context.class); final Store store = mock(Store.class); final User user = mock(User.class); final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class); StoreProperties storeProperties = new StoreProperties(); final GetAdjacentIds op1 = mock(GetAdjacentIds.class); final GetElements op2 = mock(GetElements.class); final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2)); final Integer expectedResult = 2; given(context.getUser()).willReturn(user); Set<String> opAuths = new HashSet<>(); opAuths.add("TEST_USER"); given(user.getOpAuths()).willReturn(opAuths); given(scoreOperationChain.getOperationChain()).willReturn(opChain); given(store.getProperties()).willReturn(storeProperties); final Object result = operationHandler.doOperation( new ScoreOperationChain.Builder() .operationChain(opChain) .build(), context, store); assertSame(expectedResult, result); }
@Test public void shouldExecuteScoreChainOperationForNestedOperationChain() throws OperationException { final ScoreOperationChainHandler operationHandler = new ScoreOperationChainHandler(); final Context context = mock(Context.class); final Store store = mock(Store.class); final User user = mock(User.class); final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class); StoreProperties storeProperties = new StoreProperties(); final GetAdjacentIds op1 = mock(GetAdjacentIds.class); final GetElements op2 = mock(GetElements.class); final Limit op3 = mock(Limit.class); final OperationChain opChain1 = new OperationChain(Arrays.asList(op1, op2)); final OperationChain opChain = new OperationChain(Arrays.asList(opChain1, op3)); final Integer expectedResult = 3; given(context.getUser()).willReturn(user); Set<String> opAuths = new HashSet<>(); opAuths.add("TEST_USER"); given(user.getOpAuths()).willReturn(opAuths); given(scoreOperationChain.getOperationChain()).willReturn(opChain); given(store.getProperties()).willReturn(storeProperties); final Object result = operationHandler.doOperation( new ScoreOperationChain.Builder() .operationChain(opChain) .build(), context, store); assertSame(expectedResult, result); }
@Test public void shouldReturnZeroForANullOperationChain() throws OperationException { final ScoreOperationChainHandler handler = new ScoreOperationChainHandler(); final Context context = mock(Context.class); final Store store = mock(Store.class); final User user = mock(User.class); final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class); final StoreProperties properties = mock(StoreProperties.class); final OperationChain opChain = null; given(scoreOperationChain.getOperationChain()).willReturn(opChain); given(context.getUser()).willReturn(user); Set<String> opAuths = new HashSet<>(); opAuths.add("TEST_USER"); given(user.getOpAuths()).willReturn(opAuths); given(store.getProperties()).willReturn(properties); final Object result = handler.doOperation(new ScoreOperationChain.Builder() .operationChain(opChain) .build(), context, store); assertEquals(0, result); }