/** * {@inheritDoc} */ @Override public Object getKey(E element) { if (element instanceof InvocationSequenceData) { InvocationSequenceData invoc = (InvocationSequenceData) element; if ((invoc.getNestedSequences() == null) || invoc.getNestedSequences().isEmpty()) { return Boolean.TRUE; } } return Boolean.FALSE; }
/** * {@inheritDoc} */ @Override public Object[] getChildren(Object o) { InvocationSequenceData invocationSequenceData = (InvocationSequenceData) o; List<InvocationSequenceData> nestedSequences = invocationSequenceData.getNestedSequences(); return nestedSequences.toArray(); }
/** * Returns the input sequence including its nested sequences as a stream. * * @param sequence * sequence to represent as a stream * @return returns a {@link Stream} including the given sequence and all nested ones */ public static Stream<InvocationSequenceData> asStream(InvocationSequenceData sequence) { return Stream.concat(Stream.of(sequence), sequence.getNestedSequences().stream().flatMap(InvocationSequenceDataHelper::asStream)); }
/** * Sets the parent to all nested sequences of the invocation to the correct one. * * @param parent * Parent to start from. */ private void connectChildren(InvocationSequenceData parent) { if (null != parent.getNestedSequences()) { for (InvocationSequenceData child : parent.getNestedSequences()) { child.setParentSequence(parent); connectChildren(child); } } }
@Test public void rootCauseMustBeNullWhenMethodIdentIsEqualAndInvocationHasNotTimerData() { double highDuration = RANDOM.nextDouble() + 1000; List<InvocationSequenceData> nestedSequences = new ArrayList<InvocationSequenceData>(); nestedSequences.add(new InvocationSequenceData(DEF_DATE, PLATFORM_IDENT, SENSOR_TYPE_IDENT, METHOD_IDENT_EQUAL)); nestedSequences.add(new InvocationSequenceData(DEF_DATE, PLATFORM_IDENT, SENSOR_TYPE_IDENT, METHOD_IDENT_EQUAL)); when(problemContext.getCommonContext()).thenReturn(commonContext); when(commonContext.getMethodIdent()).thenReturn(1L); when(commonContext.getDuration()).thenReturn(highDuration); when(commonContext.getNestedSequences()).thenReturn(nestedSequences); AggregatedDiagnosisData rootCause = problemCauseRule.action(); assertThat("The returned root cause rule must be null", rootCause, nullValue()); }
@Test public void problemContextMustBeTheSameInvocationIfItIsTheOnlyOneAndIsTheInvoker() { long methodIdent = 108L; Timestamp defDate = new Timestamp(new Date().getTime()); long platformIdent = RANDOM.nextLong(); long sensorTypeIdent = RANDOM.nextLong(); InvocationSequenceData parentSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, methodIdent); InvocationSequenceData childSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, methodIdent); List<InvocationSequenceData> rawInvocations = new ArrayList<InvocationSequenceData>(); parentSequence.getNestedSequences().add(childSequence); rawInvocations.add(parentSequence); when(timeWastingOperation.getRawInvocationsSequenceElements()).thenReturn(rawInvocations); CauseCluster problemContext = problemContextRule.action(); assertThat("The returned problemContext must be the invoker", problemContext.getCommonContext(), is(parentSequence)); }
@Test public void timerDataMustReturnAClonedElementOfTheGlobalContextIfItHasNoNestedSequences() { long platformIdent = 108; double highDuration = 2000; TimerData timerData = new TimerData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); timerData.calculateExclusiveMin(1); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); when(globalContext.getTimerData()).thenReturn(timerData); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getMethodIdent()).thenReturn(platformIdent); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Method ident must be the same than the global context", timeWastingOperationsResults.get(0).getMethodIdent(), is(platformIdent)); }
@Test public void timerDataMustReturnAEmptyListOfAggregatedDiagnosisDataWhenTheDurationIsZero() { long platformIdent = 108; long sensorTypeIdent = 1; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); TimerData firstSeqTimerData = new TimerData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); firstSeqTimerData.calculateExclusiveMin(200d); firstChildSequence.setTimerData(firstSeqTimerData); InvocationSequenceData secondChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstChildSequence); nestedSequences.add(secondChildSequence); when(globalContext.getDuration()).thenReturn(new Double(0)); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return an array of AggregatedDiagnosisData not empty", timeWastingOperationsResults, hasSize(0)); }
@Test public void timerDataMustReturnANotEmptyListOfAggregatedDiagnosisDataWhenTheDurationIsTooLong() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); TimerData firstSeqTimerData = new TimerData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); firstSeqTimerData.calculateExclusiveMin(200d); firstSeqTimerData.setExclusiveDuration(200d); firstChildSequence.setTimerData(firstSeqTimerData); InvocationSequenceData secondChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstChildSequence); nestedSequences.add(secondChildSequence); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return a list of AggregatedDiagnosisData not empty", timeWastingOperationsResults, not(hasSize(0))); }
@Test public void timerDataMustReturnANotNullListOfAggregatedDiagnosisData() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); TimerData firstSeqTimerData = new TimerData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); firstSeqTimerData.calculateExclusiveMin(200d); firstSeqTimerData.setExclusiveDuration(200d); firstChildSequence.setTimerData(firstSeqTimerData); InvocationSequenceData secondChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstChildSequence); nestedSequences.add(secondChildSequence); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("The returned list of AggregatedDiagnosisData must not be null", timeWastingOperationsResults, notNullValue()); }
@Test public void timerDataMustReturnAListOfAggregatedDiagnosisDataWithOneElement() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); TimerData firstSeqTimerData = new TimerData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); firstSeqTimerData.calculateExclusiveMin(200d); firstSeqTimerData.setExclusiveDuration(200d); firstChildSequence.setTimerData(firstSeqTimerData); InvocationSequenceData secondChildSequence = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstChildSequence); nestedSequences.add(secondChildSequence); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return a list with one AggregatedDiagnosisData", timeWastingOperationsResults, hasSize(1)); }
@BeforeMethod public void init() throws BusinessException { root = new InvocationSequenceData(); root.setId(1); level11 = new InvocationSequenceData(); level11.setId(2); level12 = new InvocationSequenceData(); level12.setId(3); level21 = new InvocationSequenceData(); level21.setId(4); root.getNestedSequences().add(level11); root.getNestedSequences().add(level12); level12.getNestedSequences().add(level21); when(stringValueSource.getStringValues(root, cachedDataService)).thenReturn(new String[] { "node/root/" }); when(stringValueSource.getStringValues(level11, cachedDataService)).thenReturn(new String[] { "node/level_1_1/" }); when(stringValueSource.getStringValues(level12, cachedDataService)).thenReturn(new String[] { "node/level_1_2/" }); when(stringValueSource.getStringValues(level21, cachedDataService)).thenReturn(new String[] { "node/level_2_1/", "node/level_2_1/multiple" }); when(cachedDataService.getApplicationForId(anyInt())).thenReturn(null); when(cachedDataService.getBusinessTransactionForId(anyInt(), anyInt())).thenReturn(null); }
@Test public void sqlStatementDataMustReturnAEmptyListOfAggregatedDiagnosisDataWhenTheDurationIsZero() { long platformIdent = 108; long sensorTypeIdent = 1; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); nestedSequences.add(firstMethod); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(secondMethod); when(globalContext.getDuration()).thenReturn(new Double(0)); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return an array of AggregatedDiagnosisData not empty", timeWastingOperationsResults, hasSize(0)); }
@Test public void sqlStatementDataMustReturnANotEmptyListOfAggregatedDiagnosisDataWhenTheDurationIsTooLong() { long platformIdent = 108; long sensorTypeIdent = 1; double highDuration = 2000; Timestamp defDate = new Timestamp(new Date().getTime()); List<InvocationSequenceData> nestedSequences = new ArrayList<>(); InvocationSequenceData firstMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 1L); SqlStatementData firstSqlStatementData = new SqlStatementData(); firstSqlStatementData.calculateExclusiveMin(200d); firstSqlStatementData.setExclusiveDuration(200d); firstSqlStatementData.setCount(1); firstMethod.setSqlStatementData(firstSqlStatementData); InvocationSequenceData secondMethod = new InvocationSequenceData(defDate, platformIdent, sensorTypeIdent, 2L); nestedSequences.add(firstMethod); nestedSequences.add(secondMethod); when(globalContext.getDuration()).thenReturn(highDuration); when(globalContext.getNestedSequences()).thenReturn(nestedSequences); List<AggregatedDiagnosisData> timeWastingOperationsResults = timeWastingOperationsRule.action(); assertThat("Action method must return a group of AggregatedDiagnosisData not empty", timeWastingOperationsResults, not(hasSize(0))); }
@Test public void buildOneInvocationSequence() { InvocationSequenceData invoc01 = createSequence(null); InvocationSequenceData _invoc02 = createSequence(null); InvocationSequenceData _invoc03 = createSequence(null); InvocationSequenceData _invoc04 = createSequence(null); invoc01.getNestedSequences().add(_invoc02); _invoc02.getNestedSequences().add(_invoc03); invoc01.getNestedSequences().add(_invoc04); builder.setMode(Mode.SINGLE).setInvocationSequence(invoc01); InvocationTreeElement tree = builder.build(); Map<Object, InvocationTreeElement> lookupMap = InvocationTreeUtil.buildLookupMap(tree); assertThat(lookupMap.size(), is(4)); assertThat(tree.getDataElement(), is(invoc01)); assertThat(tree.getChildren(), contains(hasProperty("dataElement", is(_invoc02)), hasProperty("dataElement", is(_invoc04)))); assertThat(tree.getChildren().get(0).getChildren(), contains(hasProperty("dataElement", is(_invoc03)))); assertThat(tree.getChildren().get(1).getChildren(), is(empty())); verifyZeroInteractions(spanService, invocationService); }
@Test public void buildOneInvocationSequenceWithSpanWithoutSpanService() { Span span01 = createServerSpan(null); InvocationSequenceData invoc01 = createSequence(span01); InvocationSequenceData _invoc02 = createSequence(null); invoc01.getNestedSequences().add(_invoc02); builder.setMode(Mode.SINGLE).setInvocationSequence(invoc01); InvocationTreeElement tree = builder.build(); Map<Object, InvocationTreeElement> lookupMap = InvocationTreeUtil.buildLookupMap(tree); assertThat(lookupMap.size(), is(3)); assertThat(tree.getDataElement(), isA((Class) SpanIdent.class)); assertThat(tree.getChildren(), contains(hasProperty("dataElement", is(invoc01)))); verifyZeroInteractions(spanService, invocationService); }
@BeforeMethod public void init() { businessTxDefinition.setBusinessTransactionDefinitionName(BUSINESS_TX_DEF_NAME); isd1 = new InvocationSequenceData(); isd1.setId(1); isd2 = new InvocationSequenceData(); isd2.setId(2); isd1.getNestedSequences().add(isd2); }
@Test public void buildWithNestedExceptions() { Span span01 = createServerSpan(null); InvocationSequenceData invoc01 = createSequence(span01); InvocationSequenceData _invoc02 = createSequence(null); invoc01.getNestedSequences().add(_invoc02); invoc01.setNestedExceptions(true); doReturn(Arrays.asList(span01)).when(spanService).getSpans(1337L); doReturn(Arrays.asList(invoc01)).when(invocationService).getInvocationSequenceDetail(1337L); builder.setSpanService(spanService).setInvocationService(invocationService).setTraceId(1337L); InvocationTreeElement tree = builder.build(); Map<Object, InvocationTreeElement> lookupMap = InvocationTreeUtil.buildLookupMap(tree); assertThat(lookupMap.size(), is(3)); assertThat(tree.hasNestedExceptions(), is(true)); assertThat(tree.hasNestedSqls(), is(false)); }
@Test public void buildWithNestedSQLs() { Span span01 = createServerSpan(null); InvocationSequenceData invoc01 = createSequence(span01); InvocationSequenceData _invoc02 = createSequence(null); invoc01.getNestedSequences().add(_invoc02); invoc01.setNestedSqlStatements(true); doReturn(Arrays.asList(span01)).when(spanService).getSpans(1337L); doReturn(Arrays.asList(invoc01)).when(invocationService).getInvocationSequenceDetail(1337L); builder.setSpanService(spanService).setInvocationService(invocationService).setTraceId(1337L); InvocationTreeElement tree = builder.build(); Map<Object, InvocationTreeElement> lookupMap = InvocationTreeUtil.buildLookupMap(tree); assertThat(lookupMap.size(), is(3)); assertThat(tree.hasNestedExceptions(), is(false)); assertThat(tree.hasNestedSqls(), is(true)); } }
@Test public void buildOneInvocationSequenceWithSpan() { Span span01 = createServerSpan(null); InvocationSequenceData invoc01 = createSequence(span01); InvocationSequenceData _invoc02 = createSequence(null); invoc01.getNestedSequences().add(_invoc02); doReturn(span01).when(spanService).get(span01.getSpanIdent()); builder.setSpanService(spanService).setMode(Mode.SINGLE).setInvocationSequence(invoc01); InvocationTreeElement tree = builder.build(); Map<Object, InvocationTreeElement> lookupMap = InvocationTreeUtil.buildLookupMap(tree); assertThat(lookupMap.size(), is(3)); assertThat(tree.getDataElement(), is(span01)); assertThat(tree.getChildren(), contains(hasProperty("dataElement", is(invoc01)))); verify(spanService).get(span01.getSpanIdent()); verifyNoMoreInteractions(spanService); verifyZeroInteractions(invocationService); }