|
| 1 | +package software.amazon.cloudwatchlogs.emf.environment; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.powermock.api.mockito.PowerMockito.mock; |
| 5 | + |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | +import org.powermock.api.mockito.PowerMockito; |
| 10 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 11 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 12 | +import software.amazon.cloudwatchlogs.emf.config.Configuration; |
| 13 | +import software.amazon.cloudwatchlogs.emf.config.SystemWrapper; |
| 14 | +import software.amazon.cloudwatchlogs.emf.model.MetricsContext; |
| 15 | +import software.amazon.cloudwatchlogs.emf.sinks.AgentSink; |
| 16 | +import software.amazon.cloudwatchlogs.emf.sinks.ConsoleSink; |
| 17 | +import software.amazon.cloudwatchlogs.emf.sinks.Endpoint; |
| 18 | +import software.amazon.cloudwatchlogs.emf.sinks.ISink; |
| 19 | + |
| 20 | +@RunWith(PowerMockRunner.class) |
| 21 | +@PrepareForTest({SystemWrapper.class, AgentBasedEnvironment.class}) |
| 22 | +public class AgentBasedEnvironmentTest { |
| 23 | + public static class AgentBasedEnvironmentTestImplementation extends AgentBasedEnvironment { |
| 24 | + protected AgentBasedEnvironmentTestImplementation(Configuration config) { |
| 25 | + super(config); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public boolean probe() { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String getType() { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void configureContext(MetricsContext context) {} |
| 40 | + } |
| 41 | + |
| 42 | + private Configuration configuration; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setup() { |
| 46 | + this.configuration = new Configuration(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testGetSinkWithDefaultEndpoint() throws Exception { |
| 51 | + AgentSink mockedSink = mock(AgentSink.class); |
| 52 | + PowerMockito.whenNew(AgentSink.class) |
| 53 | + .withAnyArguments() |
| 54 | + .then( |
| 55 | + invocation -> { |
| 56 | + Endpoint endpoint = invocation.getArgument(2); |
| 57 | + assertEquals(Endpoint.DEFAULT_TCP_ENDPOINT, endpoint); |
| 58 | + return mockedSink; |
| 59 | + }); |
| 60 | + |
| 61 | + AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
| 62 | + ISink sink = env.getSink(); |
| 63 | + |
| 64 | + assertEquals(mockedSink, sink); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testGetSinkWithConfiguredEndpoint() throws Exception { |
| 69 | + String endpointUrl = "http://configured-endpoint:1234"; |
| 70 | + configuration.setAgentEndpoint(endpointUrl); |
| 71 | + AgentSink mockedSink = mock(AgentSink.class); |
| 72 | + PowerMockito.whenNew(AgentSink.class) |
| 73 | + .withAnyArguments() |
| 74 | + .then( |
| 75 | + invocation -> { |
| 76 | + Endpoint endpoint = invocation.getArgument(2); |
| 77 | + assertEquals(Endpoint.fromURL(endpointUrl), endpoint); |
| 78 | + return mockedSink; |
| 79 | + }); |
| 80 | + |
| 81 | + AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
| 82 | + ISink sink = env.getSink(); |
| 83 | + |
| 84 | + assertEquals(mockedSink, sink); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testGetSinkOverrideToStdOut() { |
| 89 | + configuration.setShouldWriteToStdout(true); |
| 90 | + |
| 91 | + AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
| 92 | + ISink sink = env.getSink(); |
| 93 | + |
| 94 | + assertEquals(ConsoleSink.class, sink.getClass()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testGetSinkOverrideToStdOutFailFastOnImproperOverride() throws Exception { |
| 99 | + configuration.setShouldWriteToStdout(false); |
| 100 | + |
| 101 | + testGetSinkWithDefaultEndpoint(); |
| 102 | + } |
| 103 | +} |
0 commit comments