|
| 1 | +package com.wavefront.agent.preprocessor; |
| 2 | + |
| 3 | +import com.wavefront.agent.formatter.DataFormat; |
| 4 | +import com.wavefront.api.agent.preprocessor.ReportableEntityPreprocessor; |
| 5 | +import com.wavefront.api.agent.preprocessor.Preprocessor; |
| 6 | +import com.wavefront.data.ReportableEntityType; |
| 7 | +import org.easymock.EasyMock; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.io.ByteArrayOutputStream; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.PrintStream; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.function.Supplier; |
| 18 | + |
| 19 | +import static org.easymock.EasyMock.expect; |
| 20 | +import static org.easymock.EasyMock.replay; |
| 21 | +import static org.easymock.EasyMock.verify; |
| 22 | +import static org.junit.Assert.*; |
| 23 | + |
| 24 | +public class InteractivePreprocessorTesterTest { |
| 25 | + |
| 26 | + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 27 | + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); |
| 28 | + private final PrintStream originalOut = System.out; |
| 29 | + private final PrintStream originalErr = System.err; |
| 30 | + private final InputStream originalIn = System.in; |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() { |
| 34 | + System.setOut(new PrintStream(outContent)); |
| 35 | + System.setErr(new PrintStream(errContent)); |
| 36 | + // System.in will be set per test method |
| 37 | + } |
| 38 | + |
| 39 | + @After |
| 40 | + public void tearDown() { |
| 41 | + System.setOut(originalOut); |
| 42 | + System.setErr(originalErr); |
| 43 | + System.setIn(originalIn); |
| 44 | + } |
| 45 | + |
| 46 | + private void setSystemIn(String input) { |
| 47 | + System.setIn(new ByteArrayInputStream(input.getBytes())); |
| 48 | + } |
| 49 | + |
| 50 | + private String getOutput() { |
| 51 | + return outContent.toString().trim(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testInteractiveTest_TraceEntity_ReportsSpan() { |
| 56 | + setSystemIn("test-span\n"); |
| 57 | + Supplier<ReportableEntityPreprocessor> mockPreprocessorSupplier = EasyMock.mock(Supplier.class); |
| 58 | + ReportableEntityPreprocessor mockPreprocessor = EasyMock.mock(ReportableEntityPreprocessor.class); |
| 59 | + expect(mockPreprocessorSupplier.get()).andReturn(mockPreprocessor).anyTimes(); |
| 60 | + |
| 61 | + // Preprocessor does not block or modify for this test, so no specific expectations on its methods |
| 62 | + expect(mockPreprocessor.forSpan()).andReturn(new Preprocessor() {}).anyTimes(); |
| 63 | + expect(mockPreprocessor.forPointLine()).andReturn(new Preprocessor<>()).anyTimes(); |
| 64 | + |
| 65 | + replay(mockPreprocessorSupplier, mockPreprocessor); |
| 66 | + |
| 67 | + InteractivePreprocessorTester tester = new InteractivePreprocessorTester( |
| 68 | + mockPreprocessorSupplier, |
| 69 | + ReportableEntityType.TRACE, |
| 70 | + "2878", |
| 71 | + Collections.emptyList() |
| 72 | + ); |
| 73 | + |
| 74 | + boolean hasNext = tester.interactiveTest(); |
| 75 | + |
| 76 | + assertFalse(hasNext); |
| 77 | + // Verify System.out contains the serialized span. The actual spanId/traceId are random, so use regex. |
| 78 | + String expectedOutputRegex = "Rejected: test-span"; |
| 79 | + assertTrue("Output should contain serialized span matching regex: " + getOutput(), getOutput().matches(expectedOutputRegex)); |
| 80 | + |
| 81 | + verify(mockPreprocessorSupplier, mockPreprocessor); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testInteractiveTest_PointEntity_ReportsPoint() { |
| 86 | + setSystemIn("some.metric 10.0 source=test\n"); |
| 87 | + Supplier<ReportableEntityPreprocessor> mockPreprocessorSupplier = EasyMock.mock(Supplier.class); |
| 88 | + ReportableEntityPreprocessor mockPreprocessor = EasyMock.mock(ReportableEntityPreprocessor.class); |
| 89 | + expect(mockPreprocessorSupplier.get()).andReturn(mockPreprocessor).anyTimes(); |
| 90 | + expect(mockPreprocessor.forReportPoint()).andReturn(new Preprocessor() {}).anyTimes(); |
| 91 | + expect(mockPreprocessor.forPointLine()).andReturn(new Preprocessor<>()).anyTimes(); |
| 92 | + |
| 93 | + replay(mockPreprocessorSupplier, mockPreprocessor); |
| 94 | + |
| 95 | + InteractivePreprocessorTester tester = new InteractivePreprocessorTester( |
| 96 | + mockPreprocessorSupplier, |
| 97 | + ReportableEntityType.POINT, |
| 98 | + "2878", |
| 99 | + Collections.emptyList() |
| 100 | + ); |
| 101 | + |
| 102 | + boolean hasNext = tester.interactiveTest(); |
| 103 | + |
| 104 | + assertFalse("Should indicate more input, as 'some.metric...' was followed by a newline", hasNext); |
| 105 | + |
| 106 | + String actualOutput = outContent.toString().trim(); |
| 107 | + assertTrue("Output should contain serialized report point", actualOutput.startsWith("\"some.metric\" 10.0")); |
| 108 | + assertTrue("Output should contain serialized report point", actualOutput.endsWith("source=\"test\"")); |
| 109 | + |
| 110 | + verify(mockPreprocessorSupplier, mockPreprocessor); |
| 111 | + } |
| 112 | +} |
0 commit comments