/** * Default constructor. * * @param sourceType * timer sourceType * @param invocationSequenceData * invocationSequenceData is used to aggregate to AggregatedDiagnosisData * @param aggregationKey * aggregationKey of AggregatedDiagnosisData */ public AggregatedDiagnosisData(final SourceType sourceType, final InvocationSequenceData invocationSequenceData, final Object aggregationKey) { this.sourceType = sourceType; this.methodIdent = invocationSequenceData.getMethodIdent(); this.aggregationKey = aggregationKey; aggregate(invocationSequenceData); }
/** * {@inheritDoc} */ @Override public String[] getStringValues(InvocationSequenceData invocSequence, ICachedDataService cachedDataService) { MethodIdent mIdent = cachedDataService.getMethodIdentForId(invocSequence.getMethodIdent()); if (null != mIdent) { return new String[] { mIdent.getFullyQualifiedMethodSignature() }; } else { return new String[0]; } }
/** * {@inheritDoc} */ @Override public String getReadableString(Object object) { if (object instanceof InvocationSequenceData) { InvocationSequenceData data = (InvocationSequenceData) object; StringBuilder sb = new StringBuilder(); MethodIdent methodIdent = cachedDataService.getMethodIdentForId(data.getMethodIdent()); for (Column column : Column.values()) { sb.append(getStyledTextForColumn(data, methodIdent, column).toString()); sb.append('\t'); } return sb.toString(); } throw new RuntimeException("Could not create the human readable string!"); }
/** * {@inheritDoc} */ @Override public List<String> getColumnValues(Object object) { if (object instanceof InvocationSequenceData) { InvocationSequenceData data = (InvocationSequenceData) object; MethodIdent methodIdent = cachedDataService.getMethodIdentForId(data.getMethodIdent()); List<String> values = new ArrayList<>(); for (Column column : Column.values()) { values.add(getStyledTextForColumn(data, methodIdent, column).toString()); } return values; } throw new RuntimeException("Could not create the column values!"); }
@Test public void methodIdentNull() { Mockito.doReturn(1L).when(invocationSeuence).getMethodIdent(); Mockito.doReturn(null).when(cachedDataService).getMethodIdentForId(anyInt()); String[] values = valueSource.getStringValues(invocationSeuence, cachedDataService); assertThat(values, is(notNullValue())); assertThat(values.length, is(equalTo(0))); } }
/** * Get key for aggregation. * * @param invocationSequenceData * invocationSequenceData to key should be determined * @return key as object */ public Object getAggregationKey(InvocationSequenceData invocationSequenceData) { if (InvocationSequenceDataHelper.hasSQLData(invocationSequenceData)) { return new Pair<Long, String>(invocationSequenceData.getMethodIdent(), invocationSequenceData.getSqlStatementData().getSql()); } else if (InvocationSequenceDataHelper.hasHttpTimerData(invocationSequenceData)) { return new Pair<Long, String>(invocationSequenceData.getMethodIdent(), ((HttpTimerData) invocationSequenceData.getTimerData()).getHttpInfo().getUri()); } else { return invocationSequenceData.getMethodIdent(); } }
@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 methodIdentNull() { Mockito.doReturn(PARAMETER).when(parameterContentData).getContent(); Mockito.doReturn(INDEX).when(parameterContentData).getSignaturePosition(); Mockito.doReturn(1L).when(invocationSeuence).getMethodIdent(); Mockito.doReturn(null).when(cachedDataService).getMethodIdentForId(anyInt()); String methodSignature = PACKAGE + "." + CLASS + "." + METHOD + "()"; valueSource.setMethodSignature(methodSignature); valueSource.setParameterIndex(INDEX); String[] values = valueSource.getStringValues(invocationSeuence, cachedDataService); assertThat(values, is(notNullValue())); assertThat(values.length, is(equalTo(0))); }
/** * Create new InvocationIdentifier based on InvocationSequenceData. * * @param invocationSequenceData * the InvocationIdentifier the InvocationIdentifier is created */ public InvocationIdentifier(final InvocationSequenceData invocationSequenceData) { if (null == invocationSequenceData) { throw new IllegalArgumentException("Cannot create InvocationIdentifier when invocationSequenceData is null."); } this.methodIdent = invocationSequenceData.getMethodIdent(); this.invocationId = invocationSequenceData.getId(); TimerData timerData = null; if (InvocationSequenceDataHelper.hasSQLData(invocationSequenceData)) { timerData = invocationSequenceData.getSqlStatementData(); } else if (InvocationSequenceDataHelper.hasTimerData(invocationSequenceData)) { timerData = invocationSequenceData.getTimerData(); } else { throw new IllegalStateException("Timer data unknown."); } this.diagnosisTimerData = new DiagnosisTimerData(timerData); }
@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)); }
/** * {@inheritDoc} */ @Override public String[] getStringValues(InvocationSequenceData invocSequence, ICachedDataService cachedDataService) { MethodIdent mIdent = cachedDataService.getMethodIdentForId(invocSequence.getMethodIdent()); if ((null != mIdent) && Objects.equals(methodSignature, mIdent.getFullyQualifiedMethodSignature()) && InvocationSequenceDataHelper.hasCapturedParameters(invocSequence)) { for (ParameterContentData parameterContentData : InvocationSequenceDataHelper.getCapturedParameters(invocSequence, false)) { if (parameterIndex == parameterContentData.getSignaturePosition()) { return new String[] { parameterContentData.getContent() }; } } } return new String[0]; }
@Test private void musteReturnAnObjectWithALongStringIfTheTimerDataHasNotHttpTimerDataOrSqlData() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 108L); TimerData timerData = new TimerData(); invocationSequenceData.setTimerData(timerData); long aggregationKey = (long) DiagnosisDataAggregator.getInstance().getAggregationKey(invocationSequenceData); assertThat("The returned object must be the method ident of the invocationsequencedata", aggregationKey, is(invocationSequenceData.getMethodIdent())); } }
@Test private void ifTheAggregatedObjectIsNotDefineInTheMapItMustBeAggregated() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 2L); diagnosisDataAggregationPerformer = new DiagnosisDataAggregationPerformer(); TimerData timerData = new TimerData(new Timestamp(10), 10, 10, 108L); invocationSequenceData.setTimerData(timerData); diagnosisDataAggregationPerformer.aggregateInvocationSequenceData(invocationSequenceData); List<AggregatedDiagnosisData> resultList = diagnosisDataAggregationPerformer.getAggregationResultList(); assertThat("The aggregated object must have the same method ident that the invocationSequenceData", resultList.get(0).getMethodIdent(), equalTo(invocationSequenceData.getMethodIdent())); }
@Test public void retrieveURI() { methodIdent = new MethodIdent(); methodIdent.setPackageName(PACKAGE); methodIdent.setClassName(CLASS); methodIdent.setMethodName(METHOD); Mockito.doReturn(1L).when(invocationSeuence).getMethodIdent(); Mockito.doReturn(methodIdent).when(cachedDataService).getMethodIdentForId(anyInt()); String[] values = valueSource.getStringValues(invocationSeuence, cachedDataService); assertThat(values, hasItemInArray(PACKAGE + "." + CLASS + "." + METHOD + "()")); }
@Test public void noCapturedParameters() { methodIdent = new MethodIdent(); methodIdent.setPackageName(PACKAGE); methodIdent.setClassName(CLASS); methodIdent.setMethodName(METHOD); Set<ParameterContentData> pContentDataSet = new HashSet<>(); Mockito.doReturn(PARAMETER).when(parameterContentData).getContent(); Mockito.doReturn(INDEX).when(parameterContentData).getSignaturePosition(); Mockito.doReturn(1L).when(invocationSeuence).getMethodIdent(); Mockito.doReturn(pContentDataSet).when(invocationSeuence).getParameterContentData(); Mockito.doReturn(methodIdent).when(cachedDataService).getMethodIdentForId(anyInt()); String methodSignature = PACKAGE + "." + CLASS + "." + METHOD + "()"; valueSource.setMethodSignature(methodSignature); valueSource.setParameterIndex(INDEX); String[] values = valueSource.getStringValues(invocationSeuence, cachedDataService); assertThat(values, is(notNullValue())); assertThat(values.length, is(equalTo(0))); }
@Test public void wrongMethodIdent() { methodIdent = new MethodIdent(); methodIdent.setPackageName("someWrongPackage"); methodIdent.setClassName(CLASS); methodIdent.setMethodName(METHOD); Set<ParameterContentData> pContentDataSet = new HashSet<>(); pContentDataSet.add(parameterContentData); Mockito.doReturn(PARAMETER).when(parameterContentData).getContent(); Mockito.doReturn(INDEX).when(parameterContentData).getSignaturePosition(); Mockito.doReturn(1L).when(invocationSeuence).getMethodIdent(); Mockito.doReturn(pContentDataSet).when(invocationSeuence).getParameterContentData(); Mockito.doReturn(methodIdent).when(cachedDataService).getMethodIdentForId(anyInt()); String methodSignature = PACKAGE + "." + CLASS + "." + METHOD + "()"; valueSource.setMethodSignature(methodSignature); valueSource.setParameterIndex(INDEX); String[] values = valueSource.getStringValues(invocationSeuence, cachedDataService); assertThat(values, is(notNullValue())); assertThat(values.length, is(equalTo(0))); }
@Test public void wrongCapturedParameters() { methodIdent = new MethodIdent(); methodIdent.setPackageName(PACKAGE); methodIdent.setClassName(CLASS); methodIdent.setMethodName(METHOD); Set<ParameterContentData> pContentDataSet = new HashSet<>(); pContentDataSet.add(parameterContentData); Mockito.doReturn(PARAMETER).when(parameterContentData).getContent(); Mockito.doReturn(INDEX).when(parameterContentData).getSignaturePosition(); Mockito.doReturn(1L).when(invocationSeuence).getMethodIdent(); Mockito.doReturn(pContentDataSet).when(invocationSeuence).getParameterContentData(); Mockito.doReturn(methodIdent).when(cachedDataService).getMethodIdentForId(anyInt()); String methodSignature = PACKAGE + "." + CLASS + "." + METHOD + "()"; valueSource.setMethodSignature(methodSignature); valueSource.setParameterIndex(INDEX + 1); String[] values = valueSource.getStringValues(invocationSeuence, cachedDataService); assertThat(values, is(notNullValue())); assertThat(values.length, is(equalTo(0))); } }
@Test public void retrieveParameter() { methodIdent = new MethodIdent(); methodIdent.setPackageName(PACKAGE); methodIdent.setClassName(CLASS); methodIdent.setMethodName(METHOD); Set<ParameterContentData> pContentDataSet = new HashSet<>(); pContentDataSet.add(parameterContentData); Mockito.doReturn(PARAMETER).when(parameterContentData).getContent(); Mockito.doReturn(INDEX).when(parameterContentData).getSignaturePosition(); Mockito.doReturn(1L).when(invocationSeuence).getMethodIdent(); Mockito.doReturn(pContentDataSet).when(invocationSeuence).getParameterContentData(); Mockito.doReturn(methodIdent).when(cachedDataService).getMethodIdentForId(anyInt()); String methodSignature = PACKAGE + "." + CLASS + "." + METHOD + "()"; valueSource.setMethodSignature(methodSignature); valueSource.setParameterIndex(INDEX); String[] values = valueSource.getStringValues(invocationSeuence, cachedDataService); assertThat(values, hasItemInArray(PARAMETER)); }
@Test private void ifTheAggregatedObjectHasSQLDataAndIsDefinedInTheMapItMustBeAggregatedToTheDiagnosisAggregator() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 2L); diagnosisDataAggregationPerformer = new DiagnosisDataAggregationPerformer(); SqlStatementData sqlStatementData = new SqlStatementData(new Timestamp(10), 10, 10, 108L); sqlStatementData.setCount(1); sqlStatementData.setSql("blahblahblah"); invocationSequenceData.setSqlStatementData(sqlStatementData); HttpTimerData timerData = new HttpTimerData(new Timestamp(10), 10, 10, 108L); invocationSequenceData.setTimerData(timerData); Object key = new Pair<Long, String>(invocationSequenceData.getMethodIdent(), invocationSequenceData.getSqlStatementData().getSql()); diagnosisDataAggregationPerformer.diagnosisDataAggregationMap.put(key, alreadyAggregatedObject); diagnosisDataAggregationPerformer.aggregateInvocationSequenceData(invocationSequenceData); verify(alreadyAggregatedObject, times(1)).aggregate(invocationSequenceData); }
@Test private void ifTheAggregatedObjectHasHttpTimerDataAndIsDefinedInTheMapItMustBeAggregatedToTheDiagnosisAggregator() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 2L); diagnosisDataAggregationPerformer = new DiagnosisDataAggregationPerformer(); HttpTimerData timerData = new HttpTimerData(new Timestamp(10), 10, 10, 108L); HttpInfo httpInfo = new HttpInfo("URI", "requestMethod", "headerValue"); timerData.setHttpInfo(httpInfo); invocationSequenceData.setTimerData(timerData); Object key = new Pair<Long, String>(invocationSequenceData.getMethodIdent(), ((HttpTimerData) invocationSequenceData.getTimerData()).getHttpInfo().getUri()); diagnosisDataAggregationPerformer.diagnosisDataAggregationMap.put(key, alreadyAggregatedObject); diagnosisDataAggregationPerformer.aggregateInvocationSequenceData(invocationSequenceData); verify(alreadyAggregatedObject, times(1)).aggregate(invocationSequenceData); } }