StatisticsFilter extends AbstractXMLFilter { @PipelineProperty(description = "The statistics data source to record statistics against.", displayPriority = 1) @PipelinePropertyDocRef(types = StatisticStoreDoc.DOCUMENT_TYPE) public void setStatisticsDataSource(final DocRef statisticStoreRef) { this.statisticStoreRef = statisticStoreRef; } @Inject  StatisticsFilter(final ErrorReceiverProxy errorReceiverProxy,
                            final LocationFactoryProxy locationFactory,
                            final Statistics statistics,
                            final StatisticStoreStore statisticStoreStore); @Override void startProcessing(); @Override void endProcessing(); @Override void setDocumentLocator(final Locator locator); @Override void startElement(final String uri, final String localName, final String qName, final Attributes atts); @Override void endElement(final String uri, final String localName, final String qName); @Override void characters(final char[] ch, final int start, final int length); @PipelineProperty(description = "The statistics data source to record statistics against.", displayPriority = 1) @PipelinePropertyDocRef(types = StatisticStoreDoc.DOCUMENT_TYPE) void setStatisticsDataSource(final DocRef statisticStoreRef);  }
@Test void test2GoodCountStats() { final String inputPath = INPUT_DIR + "input01_2goodCountEvents.xml"; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.COUNT); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setPrecision(1000L); statisticsDataSource.setEnabled(true); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); assertThat(testEvents.size()).as("Expecting 2 events").isEqualTo(2); assertThat(testEvents.get(0).getTimeMs()).isEqualTo(DateUtil.parseNormalDateTimeString("2000-01-01T00:00:00.000Z")); assertThat(testEvents.get(0).getName()).isEqualTo(STAT_NAME); assertThat(testEvents.get(0).getTagList().size()).isEqualTo(2); assertThat(testEvents.get(0).getTagList().get(0).getTag()).isEqualTo("tag1name"); assertThat(testEvents.get(0).getTagList().get(0).getValue()).isEqualTo("1tag1value"); assertThat(testEvents.get(0).getTagList().get(1).getTag()).isEqualTo("tag2name"); assertThat(testEvents.get(0).getTagList().get(1).getValue()).isEqualTo("1tag2value"); assertThat(testEvents.get(0).getCount()).isEqualTo(1L); assertThatThrownBy(() -> testEvents.get(0) .getValue()).isInstanceOf(RuntimeException.class); assertThat(testEvents.get(1).getTimeMs()).isEqualTo(DateUtil.parseNormalDateTimeString("2000-01-02T00:00:00.000Z")); assertThat(testEvents.get(1).getName()).isEqualTo(STAT_NAME); assertThat(testEvents.get(1).getTagList().size()).isEqualTo(2); assertThat(testEvents.get(1).getTagList().get(0).getTag()).isEqualTo("tag1name"); assertThat(testEvents.get(1).getTagList().get(0).getValue()).isEqualTo("2tag1value"); assertThat(testEvents.get(1).getTagList().get(1).getTag()).isEqualTo("tag2name"); assertThat(testEvents.get(1).getTagList().get(1).getValue()).isEqualTo("2tag2value"); assertThat(testEvents.get(1).getCount()).isEqualTo(1L); assertThatThrownBy(() -> testEvents.get(1) .getValue()).isInstanceOf(RuntimeException.class); }
@Test void test2GoodValueStats() { final String inputPath = INPUT_DIR + "input02_2goodValueEvents.xml"; final long precision = 60 * 1000L; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.VALUE); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setPrecision(precision); statisticsDataSource.setEnabled(true); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); assertThat(testEvents.size()).as("Expecting 2 events").isEqualTo(2); assertThat(testEvents.get(0).getTimeMs()).isEqualTo(DateUtil.parseNormalDateTimeString("2000-01-01T00:00:00.000Z")); assertThat(testEvents.get(0).getName()).isEqualTo(STAT_NAME); assertThat(testEvents.get(0).getTagList().size()).isEqualTo(2); assertThat(testEvents.get(0).getTagList().get(0).getTag()).isEqualTo("tag1name"); assertThat(testEvents.get(0).getTagList().get(0).getValue()).isEqualTo("1tag1value"); assertThat(testEvents.get(0).getTagList().get(1).getTag()).isEqualTo("tag2name"); assertThat(testEvents.get(0).getTagList().get(1).getValue()).isEqualTo("1tag2value"); assertThat(testEvents.get(0).getValue()).isCloseTo(1.5, within(JUNIT_DOUBLE_TOLERANCE)); assertThatThrownBy(() -> testEvents.get(0) .getCount()).isInstanceOf(RuntimeException.class); assertThat(testEvents.get(1).getTimeMs()).isEqualTo(DateUtil.parseNormalDateTimeString("2000-01-02T00:00:00.000Z")); assertThat(testEvents.get(1).getName()).isEqualTo(STAT_NAME); assertThat(testEvents.get(1).getTagList().size()).isEqualTo(2); assertThat(testEvents.get(1).getTagList().get(0).getTag()).isEqualTo("tag1name"); assertThat(testEvents.get(1).getTagList().get(0).getValue()).isEqualTo("2tag1value"); assertThat(testEvents.get(1).getTagList().get(1).getTag()).isEqualTo("tag2name"); assertThat(testEvents.get(1).getTagList().get(1).getValue()).isEqualTo("2tag2value"); assertThat(testEvents.get(1).getValue()).isCloseTo(3.9, within(JUNIT_DOUBLE_TOLERANCE)); assertThatThrownBy(() -> testEvents.get(0) .getCount()).isInstanceOf(RuntimeException.class); }
@Test void testBadStatType() { assertThatThrownBy(() -> { final String inputPath = INPUT_DIR + "input03_badType.xml"; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.COUNT); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setEnabled(true); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); }).isInstanceOf(RuntimeException.class); }
@Test void testNoCountElement() { final String inputPath = INPUT_DIR + "input05_noCountOrValue.xml"; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.COUNT); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setEnabled(true); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); assertThat(testEvents.size()).as("Expecting 1 event").isEqualTo(1); assertThat(testEvents.get(0).getCount()).isEqualTo(1L); }
@Test void testNoValueElement() { assertThatThrownBy(() -> { final String inputPath = INPUT_DIR + "input05_noCountOrValue.xml"; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.VALUE); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setEnabled(true); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); }).isInstanceOf(ProcessException.class); }
@Test void testDisabledStatDataSource() { assertThatThrownBy(() -> { final String inputPath = INPUT_DIR + "input01_2goodCountEvents.xml"; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.COUNT); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setEnabled(false); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); }).isInstanceOf(ProcessException.class); }
@Test void testInvalidElement() { assertThatThrownBy(() -> { final String inputPath = INPUT_DIR + "input06_invalidElement.xml"; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.COUNT); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setEnabled(true); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); }).isInstanceOf(ProcessException.class); }
@Test void testEmptyCountElement() { final String inputPath = INPUT_DIR + "input07_emptyCountElement.xml"; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.COUNT); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setEnabled(true); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); assertThat(testEvents.size()).as("Expecting 1 event").isEqualTo(1); assertThat(testEvents.get(0).getCount()).isEqualTo(1L); }
@Test void testNoStatisticElements() { final String inputPath = INPUT_DIR + "input08_noStatisticElements.xml"; final ByteArrayInputStream input = new ByteArrayInputStream(getString(inputPath).getBytes()); final StatisticStoreStore statisticStoreStore = getStore(); final DocRef docRef = statisticStoreStore.createDocument(STAT_NAME); final StatisticStoreDoc statisticsDataSource = statisticStoreStore.readDocument(docRef); statisticsDataSource.setStatisticType(StatisticType.COUNT); statisticsDataSource.setConfig(new StatisticsDataSourceData( Arrays.asList(new StatisticField("tag1name"), new StatisticField("tag2name")))); statisticsDataSource.setEnabled(true); statisticStoreStore.writeDocument(statisticsDataSource); final ErrorReceiverProxy errorReceiverProxy = new ErrorReceiverProxy(new FatalErrorReceiver()); final StatisticsFilter statisticsFilter = new StatisticsFilter(errorReceiverProxy, new LocationFactoryProxy(), this, statisticStoreStore); statisticsFilter.setStatisticsDataSource(docRef); ProcessorUtil.processXml(input, errorReceiverProxy, statisticsFilter, new LocationFactoryProxy()); assertThat(testEvents.size()).as("Expecting 0 event").isEqualTo(0); }