@Test public void add() { RuleInput input = new RuleInput(Tags.tag("A", "input")); sessionContext.addExecution(rules.iterator().next(), input); assertThat(sessionContext.getExecutions().get(rules.iterator().next()), containsInAnyOrder(input)); } }
@Test public void multipleTagTypesSingleInput() throws RuleDefinitionException, SessionException { RuleDefinition ruleC = Rules.define(RuleC.class); Tag rootTag = Tags.rootTag("input"); Tag tagA = Tags.tag("A", "A1", rootTag); Tag tagB = Tags.tag("B", "B1", tagA); RuleOutput outputB = new RuleOutput("RuleB", "B", Collections.<ConditionFailure> emptyList(), Collections.singletonList(tagB)); when(outputStorage.findLatestResultsByTagType(Matchers.<Set<String>> anyObject())).thenReturn(Collections.singleton(outputB)); List<RuleInput> ruleInputs = new ArrayList<>(session.collectInputs(ruleC, outputStorage)); assertThat(ruleInputs, hasSize(1)); assertThat(ruleInputs.get(0).getRoot(), equalTo(tagB)); assertThat(ruleInputs.get(0).getUnraveled(), containsInAnyOrder(tagB, tagA)); }
throw new RuleExecutionException("If resultQuantity is MULTIPLE ensure that either an Array or a Collection is defined as return value", context); transformed.addAll(Tags.tags(getResultTag(), context.getRuleInput().getRoot(), values)); break; case SINGLE: default: transformed.add(Tags.tag(getResultTag(), result, context.getRuleInput().getRoot()));
@Test public void shouldInjectValue() throws RuleExecutionException { // prepare when(input.getUnraveled()).thenReturn(Tags.tags("T1", null, "InjectedValue")); // execute new TagInjection("T1", RuleDummy.tagStringValueField()).execute(context); // verify assertThat(ruleImpl.tagStringValueField, is("InjectedValue")); }
@Test public void singleTagTypeSingleInput() throws RuleDefinitionException, SessionException { RuleDefinition ruleB = Rules.define(RuleB.class); Tag rootTag = Tags.rootTag("input"); Tag tagA = Tags.tag("A", "input", rootTag); RuleOutput outputA = new RuleOutput("RuleA", "A", Collections.<ConditionFailure> emptyList(), Collections.singletonList(tagA)); when(outputStorage.findLatestResultsByTagType(Matchers.<Set<String>> anyObject())).thenReturn(Collections.singleton(outputA)); List<RuleInput> ruleInputs = new ArrayList<>(session.collectInputs(ruleB, outputStorage)); assertThat(ruleInputs, hasSize(1)); assertThat(ruleInputs.get(0).getRoot(), equalTo(tagA)); }
@Test public void shouldInjectTag() throws RuleExecutionException { // prepare when(input.getUnraveled()).thenReturn(Tags.tags("T1", null, "InjectedValue")); // execute new TagInjection("T1", RuleDummy.tagAsTagField(), TagValue.InjectionStrategy.BY_TAG).execute(context); // verify assertThat(ruleImpl.tagAsTagField, is(Tags.tag("T1", "InjectedValue"))); } }
@Test public void filterOne() throws RuleDefinitionException, SessionException { RuleDefinition ruleB = Rules.define(RuleB.class); RuleInput inputA1 = new RuleInput(Tags.tag("A", "A1")); RuleInput inputA2 = new RuleInput(Tags.tag("A", "A2")); RuleInput inputA3 = new RuleInput(Tags.tag("A", "A3")); RuleInput inputB = new RuleInput(Tags.tag("B", "XY")); RuleInput inputC = new RuleInput(Tags.tag("C", "XY")); Collection<RuleInput> inputs = Arrays.asList(inputA1, inputA2, inputA3, inputB, inputC); Multimap<RuleDefinition, RuleInput> executions = ArrayListMultimap.create(); executions.put(ruleB, inputA1); inputs = session.filterProcessedInputs(executions, ruleB, inputs); assertThat(inputs, containsInAnyOrder(inputA2, inputA3, inputB, inputC)); }
@Test(expectedExceptions = RuleDefinitionException.class) public void shouldFailDueToQuantityAndResultMismatch() throws RuleDefinitionException, RuleExecutionException { Tag rootTag = Tags.rootTag("Input"); when(dummy.action()).thenReturn("Fail"); when(this.input.getRoot()).thenReturn(rootTag); // Execute and fail. ActionMethod would expect array/collection as result from ruleImpl // implementation. // But receives "Fail" String new ActionMethod(RuleDummy.actionMethod(), "T2", Action.Quantity.MULTIPLE).execute(context); } }
/** * {@inheritDoc} * * @throws RuleExecutionException * If tag injection fails. */ @Override public Object determineValueToInject(ExecutionContext context) throws RuleExecutionException { // extract the required type form the unraveled list of input Tags for (Tag tag : context.getRuleInput().getUnraveled()) { if (tag.getType().equals(getType())) { return InjectionStrategy.BY_VALUE.equals(getInjectionStrategy()) ? tag.getValue() : tag; } } throw new RuleExecutionException("Unable to find Tag: " + getType() + " in RuleInput.", context); }
if (tags.size() == requiredInputTags.size()) { inputs.add(new RuleInput(leafTag, tags)); } else {
@Test public void shouldProduceSingleTagWithSingleObjectValue() throws Exception { // prepare Mocks Tag rootTag = Tags.rootTag("Input"); Tag expectedResultTag = new Tag("T1", "oneResult", rootTag); when(dummy.action()).thenReturn("oneResult"); when(input.getRoot()).thenReturn(rootTag); // Create TestMethod ActionMethod action = new ActionMethod(RuleDummy.actionMethod(), "T1", Action.Quantity.SINGLE); // execute Collection<Tag> result = action.execute(context); // verify assertThat(result, hasSize(1)); assertThat(result, hasItem(expectedResultTag)); }
@Test public void filterTwo() throws RuleDefinitionException, SessionException { RuleDefinition ruleB = Rules.define(RuleB.class); RuleDefinition ruleA = Rules.define(RuleA.class); RuleInput inputA1 = new RuleInput(Tags.tag("A", "A1")); RuleInput inputA2 = new RuleInput(Tags.tag("A", "A2")); RuleInput inputA3 = new RuleInput(Tags.tag("A", "A3")); RuleInput inputB = new RuleInput(Tags.tag("B", "XY")); RuleInput inputC = new RuleInput(Tags.tag("C", "XY")); Collection<RuleInput> inputs = Arrays.asList(inputA1, inputA2, inputA3, inputB, inputC); Multimap<RuleDefinition, RuleInput> executions = ArrayListMultimap.create(); executions.put(ruleA, inputA1); executions.put(ruleB, inputA2); executions.put(ruleB, inputA3); inputs = session.filterProcessedInputs(executions, ruleB, inputs); assertThat(inputs, containsInAnyOrder(inputA1, inputB, inputC)); }
@Test public void shouldProduceMultipleTagsFromSingleArray() throws Exception { Tag rootTag = Tags.rootTag("Input"); // prepare Mocks when(dummy.action2()).thenReturn(new String[] { "one", "two", "three" }); when(this.input.getRoot()).thenReturn(rootTag); // Create TestMethod ActionMethod action = new ActionMethod(RuleDummy.action2Method(), "T2", Action.Quantity.MULTIPLE); // execute Collection<Tag> result = action.execute(context); // verify Collection<Tag> tags = Tags.tags("T2", rootTag, "one", "two", "three"); assertThat(result, containsInAnyOrder(tags.toArray())); }
@Test public void singleTagTypeMultipleInputs() throws RuleDefinitionException, SessionException { RuleDefinition ruleB = Rules.define(RuleB.class); Tag rootTag = Tags.rootTag("input"); Tag tagA1 = Tags.tag("A", "A1", rootTag); Tag tagA2 = Tags.tag("A", "A2", rootTag); Tag tagA3 = Tags.tag("A", "A3", rootTag); RuleOutput outputA = new RuleOutput("RuleA", "A", Collections.<ConditionFailure> emptyList(), Arrays.asList(tagA1, tagA2, tagA3)); when(outputStorage.findLatestResultsByTagType(Matchers.<Set<String>> anyObject())).thenReturn(Collections.singleton(outputA)); Collection<RuleInput> ruleInputs = session.collectInputs(ruleB, outputStorage); assertThat(ruleInputs, hasSize(3)); assertThat(ruleInputs, containsInAnyOrder(new RuleInput(tagA1, Sets.newHashSet(tagA1)), new RuleInput(tagA2, Sets.newHashSet(tagA2)), new RuleInput(tagA3, Sets.newHashSet(tagA3)))); }
@Test public void shouldProduceMultipleTagsFromCollection() throws Exception { // prepare Mocks Tag rootTag = Tags.rootTag("Input"); when(dummy.action2()).thenReturn(new String[] { "one", "two", "three" }); when(this.input.getRoot()).thenReturn(rootTag); // Create TestMethod ActionMethod action = new ActionMethod(RuleDummy.action2Method(), "T2", Action.Quantity.MULTIPLE); // execute Collection<Tag> result = action.execute(context); // verify Collection<Tag> tags = Tags.tags("T2", rootTag, "one", "two", "three"); assertThat(result, containsInAnyOrder(tags.toArray())); }
@Test public void multipleTagTypesMultipleInputs1() throws RuleDefinitionException, SessionException { RuleDefinition ruleC = Rules.define(RuleC.class); Tag rootTag = Tags.rootTag("input"); Tag tagA = Tags.tag("A", "A1", rootTag); Tag tagB1 = Tags.tag("B", "B1", tagA); Tag tagB2 = Tags.tag("B", "B2", tagA); RuleOutput outputB = new RuleOutput("RuleB", "B", Collections.<ConditionFailure> emptyList(), Arrays.asList(tagB1, tagB2)); when(outputStorage.findLatestResultsByTagType(Matchers.<Set<String>> anyObject())).thenReturn(Collections.singleton(outputB)); Collection<RuleInput> ruleInputs = session.collectInputs(ruleC, outputStorage); assertThat(ruleInputs, hasSize(2)); assertThat(ruleInputs, containsInAnyOrder(new RuleInput(tagB1, Sets.newHashSet(tagA, tagB1)), new RuleInput(tagB2, Sets.newHashSet(tagA, tagB2)))); }
@Test public void shouldProduceSingleTagWithArrayValue() throws Exception { // prepare Mocks Tag rootTag = Tags.rootTag("Input"); Tag expectedResultTag = new Tag("T1", new String[] { "one", "two", "three" }, rootTag); when(dummy.action()).thenReturn(new String[] { "one", "two", "three" }); when(this.input.getRoot()).thenReturn(rootTag); // Create TestMethod ActionMethod action = new ActionMethod(RuleDummy.actionMethod(), "T1", Action.Quantity.SINGLE); // execute Collection<Tag> result = action.execute(context); // verify assertThat(result, hasSize(1)); assertThat(result, containsInAnyOrder(expectedResultTag)); }
@Test public void filterAll() throws RuleDefinitionException, SessionException { RuleDefinition ruleB = Rules.define(RuleB.class); RuleDefinition ruleA = Rules.define(RuleA.class); RuleInput inputA1 = new RuleInput(Tags.tag("A", "A1")); RuleInput inputA2 = new RuleInput(Tags.tag("A", "A2")); RuleInput inputA3 = new RuleInput(Tags.tag("A", "A3")); RuleInput inputB = new RuleInput(Tags.tag("B", "XY")); RuleInput inputC = new RuleInput(Tags.tag("C", "XY")); Collection<RuleInput> inputs = Arrays.asList(inputA1, inputA2, inputA3, inputB, inputC); Multimap<RuleDefinition, RuleInput> executions = ArrayListMultimap.create(); executions.put(ruleB, inputA1); executions.put(ruleB, inputA2); executions.put(ruleB, inputA3); executions.put(ruleB, inputB); executions.put(ruleB, inputC); inputs = session.filterProcessedInputs(executions, ruleB, inputs); assertThat(inputs, empty()); } }
@Test public void simpleRuleExecution() throws RuleDefinitionException, RuleExecutionException { RuleDefinition ruleDefinition = Rules.define(RuleA.class); String inputStr = "hallo"; RuleInput input = new RuleInput(Tags.tag(Tags.ROOT_TAG, inputStr)); RuleOutput output = ruleDefinition.execute(input, Collections.<String, Object> emptyMap()); assertThat(output.getRuleName(), equalTo(ruleDefinition.getName())); assertThat(output.getTags(), hasSize(1)); assertThat(output.getTags().iterator().next().getValue(), equalTo((Object) (inputStr + inputStr))); }
@Test public void ruleWithSessionVariableExecution() throws RuleDefinitionException, RuleExecutionException { RuleDefinition ruleDefinition = Rules.define(RuleWithSessionVariable.class); String inputStr = "hallo"; String sessionVar = "sessionVar"; RuleInput input = new RuleInput(Tags.tag(Tags.ROOT_TAG, inputStr)); RuleOutput output = ruleDefinition.execute(input, Collections.singletonMap(sessionVar, (Object) sessionVar)); assertThat(output.getRuleName(), equalTo(ruleDefinition.getName())); assertThat(output.getTags(), hasSize(1)); assertThat(output.getTags().iterator().next().getValue(), equalTo((Object) (inputStr + sessionVar))); }