/** * Creates the faked output of an internal rule to start process of other rules. If we wrap the * input to be analyzed into a RuleOutput the entire rule processing works exactly the same way * and and the input can be access of type {@link Tags#ROOT_TAG}. * * @param input * The input to be wrapped in output. * @return RuleOutput providing the input as value of {@link Tags#ROOT_TAG}. */ public static RuleOutput triggerRuleOutput(Object input) { return new RuleOutput(TRIGGER_RULE, Tags.ROOT_TAG, Collections.<ConditionFailure> emptyList(), Collections.singleton(Tags.rootTag(input))); }
@Test public void validRootTagInput() { rocks.inspectit.server.diagnosis.engine.tag.Tag tag = rocks.inspectit.server.diagnosis.engine.tag.Tags.rootTag("TestInput"); assertThat(tag.getType(), equalTo(rocks.inspectit.server.diagnosis.engine.tag.Tags.ROOT_TAG)); assertThat(tag.getValue().toString(), equalTo("TestInput")); assertThat(tag.getParent(), equalTo(null)); assertThat(tag.getState(), equalTo(TagState.LEAF)); }
@Test public void nullRootTagInput() { rocks.inspectit.server.diagnosis.engine.tag.Tag tag = rocks.inspectit.server.diagnosis.engine.tag.Tags.rootTag(null); assertThat(tag.getType(), equalTo(rocks.inspectit.server.diagnosis.engine.tag.Tags.ROOT_TAG)); assertThat(tag.getValue(), equalTo(null)); assertThat(tag.getParent(), equalTo(null)); assertThat(tag.getState(), equalTo(TagState.LEAF)); } }
@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 noMatch2() throws RuleDefinitionException, SessionException { RuleDefinition ruleC = Rules.define(RuleC.class); Tag rootTag = Tags.rootTag("input"); Tag tagA1 = Tags.tag("A", "A1", rootTag); Tag tagB1 = Tags.tag("B", "B1", rootTag); RuleOutput outputB1 = new RuleOutput("RuleA", "A", Collections.<ConditionFailure> emptyList(), Arrays.asList(tagB1)); when(outputStorage.findLatestResultsByTagType(Matchers.<Set<String>> anyObject())).thenReturn(Arrays.asList(outputB1)); Collection<RuleInput> ruleInputs = session.collectInputs(ruleC, outputStorage); assertThat(ruleInputs, empty()); } }
@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)); }
@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(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); } }
@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 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 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 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 multipleTagTypesMultipleInputs2() throws RuleDefinitionException, SessionException { RuleDefinition ruleC = Rules.define(RuleC.class); Tag rootTag = Tags.rootTag("input"); Tag tagA1 = Tags.tag("A", "A1", rootTag); Tag tagB1 = Tags.tag("B", "B1", tagA1); Tag tagA2 = Tags.tag("A", "A2", rootTag); Tag tagB2 = Tags.tag("B", "B2", tagA2); RuleOutput outputB1 = new RuleOutput("RuleB", "B", Collections.<ConditionFailure> emptyList(), Arrays.asList(tagB1)); RuleOutput outputB2 = new RuleOutput("RuleB", "B", Collections.<ConditionFailure> emptyList(), Arrays.asList(tagB2)); when(outputStorage.findLatestResultsByTagType(Matchers.<Set<String>> anyObject())).thenReturn(Arrays.asList(outputB1, outputB2)); Collection<RuleInput> ruleInputs = session.collectInputs(ruleC, outputStorage); assertThat(ruleInputs, hasSize(2)); assertThat(ruleInputs, containsInAnyOrder(new RuleInput(tagB1, Sets.newHashSet(tagA1, tagB1)), new RuleInput(tagB2, Sets.newHashSet(tagA2, tagB2)))); }
String input = "input"; Multimap<String, RuleOutput> outputMap = ArrayListMultimap.create(); Tag tagA = Tags.tag("A", input, Tags.rootTag(input)); Tag tagB = Tags.tag("B", input, tagA); Tag tagC = Tags.tag("C", input, tagB);
@Test(expectedExceptions = RuntimeException.class) public void collectProblemInstancesWithRuntimeExceptionProblemContext() { CauseStructure causeStructure = new CauseStructure(CauseType.SINGLE, SourceType.TIMERDATA); AggregatedDiagnosisData aggregatedInvocationSequenceData = null; aggregatedInvocationSequenceData = DiagnosisDataAggregator.getInstance().getAggregatedDiagnosisData(secondChildSequence); DiagnosisDataAggregator.getInstance().aggregate(aggregatedInvocationSequenceData, secondChildSequence); Multimap<String, Tag> tagMap = ArrayListMultimap.create(); Tag tagOne = new Tag(RuleConstants.DIAGNOSIS_TAG_GLOBAL_CONTEXT, secondChildSequence, Tags.rootTag(secondChildSequence)); Tag tagTwo = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CONTEXT, "Test", tagOne); Tag tagThree = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CAUSE, aggregatedInvocationSequenceData, tagTwo); Tag tagFour = new Tag(RuleConstants.DIAGNOSIS_TAG_CAUSE_STRUCTURE, causeStructure, tagThree); tagMap.put("D", tagFour); when(sessionContext.getInput()).thenReturn(secondChildSequence); when(sessionContext.getStorage()).thenReturn(storage); when(storage.mapTags(TagState.LEAF)).thenReturn(tagMap); ProblemOccurrenceResultCollector problemInstanceResultCollector = new ProblemOccurrenceResultCollector(); List<ProblemOccurrence> problemOccurrence = problemInstanceResultCollector.collect(sessionContext); assertThat(problemOccurrence, hasSize(0)); }
@Test(expectedExceptions = RuntimeException.class) public void collectProblemInstancesWithRuntimeExceptionCauseStructure() { AggregatedDiagnosisData aggregatedInvocationSequenceData = null; aggregatedInvocationSequenceData = DiagnosisDataAggregator.getInstance().getAggregatedDiagnosisData(secondChildSequence); DiagnosisDataAggregator.getInstance().aggregate(aggregatedInvocationSequenceData, secondChildSequence); Multimap<String, Tag> tagMap = ArrayListMultimap.create(); Tag tagOne = new Tag(RuleConstants.DIAGNOSIS_TAG_GLOBAL_CONTEXT, secondChildSequence, Tags.rootTag(secondChildSequence)); Tag tagTwo = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CONTEXT, new CauseCluster(secondChildSequence), tagOne); Tag tagThree = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CAUSE, aggregatedInvocationSequenceData, tagTwo); Tag tagFour = new Tag(RuleConstants.DIAGNOSIS_TAG_CAUSE_STRUCTURE, "Test", tagThree); tagMap.put("D", tagFour); when(sessionContext.getInput()).thenReturn(secondChildSequence); when(sessionContext.getStorage()).thenReturn(storage); when(storage.mapTags(TagState.LEAF)).thenReturn(tagMap); ProblemOccurrenceResultCollector problemInstanceResultCollector = new ProblemOccurrenceResultCollector(); List<ProblemOccurrence> problemOccurrence = problemInstanceResultCollector.collect(sessionContext); assertThat(problemOccurrence, hasSize(0)); }
@Test public void collectProblemInstances() { CauseStructure causeStructure = new CauseStructure(CauseType.SINGLE, SourceType.TIMERDATA); AggregatedDiagnosisData aggregatedInvocationSequenceData = DiagnosisDataAggregator.getInstance().getAggregatedDiagnosisData(secondChildSequence); DiagnosisDataAggregator.getInstance().aggregate(aggregatedInvocationSequenceData, secondChildSequence); Multimap<String, Tag> tagMap = ArrayListMultimap.create(); Tag tagOne = new Tag(RuleConstants.DIAGNOSIS_TAG_GLOBAL_CONTEXT, secondChildSequence, Tags.rootTag(secondChildSequence)); Tag tagTwo = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CONTEXT, new CauseCluster(secondChildSequence), tagOne); Tag tagThree = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CAUSE, aggregatedInvocationSequenceData, tagTwo); Tag tagFour = new Tag(RuleConstants.DIAGNOSIS_TAG_CAUSE_STRUCTURE, causeStructure, tagThree); tagMap.put("Test", tagFour); when(sessionContext.getInput()).thenReturn(secondChildSequence); when(sessionContext.getStorage()).thenReturn(storage); when(storage.mapTags(TagState.LEAF)).thenReturn(tagMap); ProblemOccurrenceResultCollector problemInstanceResultCollector = new ProblemOccurrenceResultCollector(); List<ProblemOccurrence> problemOccurrence = problemInstanceResultCollector.collect(sessionContext); assertThat(problemOccurrence, hasSize(1)); }
@Test(expectedExceptions = RuntimeException.class) public void collectProblemInstancesWithRuntimeExceptionRootCause() { CauseStructure causeStructure = new CauseStructure(CauseType.SINGLE, SourceType.TIMERDATA); AggregatedDiagnosisData aggregatedInvocationSequenceData = null; aggregatedInvocationSequenceData = DiagnosisDataAggregator.getInstance().getAggregatedDiagnosisData(secondChildSequence); DiagnosisDataAggregator.getInstance().aggregate(aggregatedInvocationSequenceData, secondChildSequence); Multimap<String, Tag> tagMap = ArrayListMultimap.create(); Tag tagOne = new Tag(RuleConstants.DIAGNOSIS_TAG_GLOBAL_CONTEXT, secondChildSequence, Tags.rootTag(secondChildSequence)); Tag tagTwo = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CONTEXT, new CauseCluster(secondChildSequence), tagOne); Tag tagThree = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CAUSE, "Test", tagTwo); Tag tagFour = new Tag(RuleConstants.DIAGNOSIS_TAG_CAUSE_STRUCTURE, causeStructure, tagThree); tagMap.put("D", tagFour); when(sessionContext.getInput()).thenReturn(secondChildSequence); when(sessionContext.getStorage()).thenReturn(storage); when(storage.mapTags(TagState.LEAF)).thenReturn(tagMap); ProblemOccurrenceResultCollector problemInstanceResultCollector = new ProblemOccurrenceResultCollector(); List<ProblemOccurrence> problemOccurrence = problemInstanceResultCollector.collect(sessionContext); assertThat(problemOccurrence, hasSize(0)); }
@Test(expectedExceptions = RuntimeException.class) public void collectProblemInstancesWithRuntimeExceptionGlobalContext() { CauseStructure causeStructure = new CauseStructure(CauseType.SINGLE, SourceType.TIMERDATA); AggregatedDiagnosisData aggregatedInvocationSequenceData = null; aggregatedInvocationSequenceData = DiagnosisDataAggregator.getInstance().getAggregatedDiagnosisData(secondChildSequence); DiagnosisDataAggregator.getInstance().aggregate(aggregatedInvocationSequenceData, secondChildSequence); Multimap<String, Tag> tagMap = ArrayListMultimap.create(); Tag tagOne = new Tag(RuleConstants.DIAGNOSIS_TAG_GLOBAL_CONTEXT, "Test", Tags.rootTag(secondChildSequence)); Tag tagTwo = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CONTEXT, new CauseCluster(secondChildSequence), tagOne); Tag tagThree = new Tag(RuleConstants.DIAGNOSIS_TAG_PROBLEM_CAUSE, aggregatedInvocationSequenceData, tagTwo); Tag tagFour = new Tag(RuleConstants.DIAGNOSIS_TAG_CAUSE_STRUCTURE, causeStructure, tagThree); tagMap.put("D", tagFour); when(sessionContext.getInput()).thenReturn(secondChildSequence); when(sessionContext.getStorage()).thenReturn(storage); when(storage.mapTags(TagState.LEAF)).thenReturn(tagMap); ProblemOccurrenceResultCollector problemInstanceResultCollector = new ProblemOccurrenceResultCollector(); List<ProblemOccurrence> problemOccurrence = problemInstanceResultCollector.collect(sessionContext); assertThat(problemOccurrence, hasSize(0)); }