|
| 1 | +package org.owasp.esapi.logging.appender; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Rule; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.rules.TestName; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.mockito.ArgumentCaptor; |
| 9 | +import org.mockito.Mockito; |
| 10 | +import org.owasp.esapi.Logger; |
| 11 | +import org.owasp.esapi.Logger.EventType; |
| 12 | +import org.powermock.api.mockito.PowerMockito; |
| 13 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 14 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 15 | + |
| 16 | +import org.junit.Assert; |
| 17 | + |
| 18 | +@RunWith(PowerMockRunner.class) |
| 19 | +@PrepareForTest (LogPrefixAppender.class) |
| 20 | +public class LogPrefixAppenderTest { |
| 21 | + @Rule |
| 22 | + public TestName testName = new TestName(); |
| 23 | + |
| 24 | + private EventTypeLogSupplier etlsSpy; |
| 25 | + private String etlsSpyGet = "EVENT_TYPE"; |
| 26 | + |
| 27 | + private ClientInfoSupplier cisSpy; |
| 28 | + private String cisSpyGet = "CLIENT_INFO"; |
| 29 | + |
| 30 | + private ServerInfoSupplier sisSpy; |
| 31 | + private String sisSpyGet = "SERVER_INFO"; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void buildSupplierSpies() { |
| 35 | + etlsSpy = Mockito.spy(new EventTypeLogSupplier(Logger.EVENT_UNSPECIFIED)); |
| 36 | + cisSpy = Mockito.spy(new ClientInfoSupplier()); |
| 37 | + sisSpy = Mockito.spy(new ServerInfoSupplier(testName.getMethodName())); |
| 38 | + |
| 39 | + Mockito.when(etlsSpy.get()).thenReturn(etlsSpyGet); |
| 40 | + Mockito.when(cisSpy.get()).thenReturn(cisSpyGet); |
| 41 | + Mockito.when(sisSpy.get()).thenReturn(sisSpyGet); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void verifyDelegatePassthroughCreation() throws Exception { |
| 46 | + ArgumentCaptor<EventType> eventTypeCapture = ArgumentCaptor.forClass(EventType.class); |
| 47 | + ArgumentCaptor<String> logNameCapture = ArgumentCaptor.forClass(String.class); |
| 48 | + PowerMockito.whenNew(EventTypeLogSupplier.class).withArguments(eventTypeCapture.capture()).thenReturn(etlsSpy); |
| 49 | + PowerMockito.whenNew(ClientInfoSupplier.class).withNoArguments().thenReturn(cisSpy); |
| 50 | + PowerMockito.whenNew(ServerInfoSupplier.class).withArguments(logNameCapture.capture()).thenReturn(sisSpy); |
| 51 | + |
| 52 | + LogPrefixAppender lpa = new LogPrefixAppender(true, true, true, testName.getMethodName()+"-APPLICATION"); |
| 53 | + String result = lpa.appendTo( testName.getMethodName()+"-LOGGER", Logger.EVENT_UNSPECIFIED, testName.getMethodName()+"-MESSAGE"); |
| 54 | + |
| 55 | + //Based on the forced returns in the before block |
| 56 | + Assert.assertEquals("[EVENT_TYPE CLIENT_INFO -> SERVER_INFO] "+ testName.getMethodName()+"-MESSAGE", result); |
| 57 | + |
| 58 | + Assert.assertEquals(Logger.EVENT_UNSPECIFIED, eventTypeCapture.getValue()); |
| 59 | + Assert.assertEquals(testName.getMethodName()+"-LOGGER",logNameCapture.getValue()); |
| 60 | + |
| 61 | + Mockito.verify(etlsSpy, Mockito.times(1)).get(); |
| 62 | + Mockito.verify(cisSpy, Mockito.times(1)).get(); |
| 63 | + Mockito.verify(sisSpy, Mockito.times(1)).get(); |
| 64 | + |
| 65 | + Mockito.verify(cisSpy, Mockito.times(1)).setLogUserInfo(true); |
| 66 | + Mockito.verify(sisSpy, Mockito.times(1)).setLogServerIp(true); |
| 67 | + Mockito.verify(sisSpy, Mockito.times(1)).setLogApplicationName(true, testName.getMethodName()+"-APPLICATION"); |
| 68 | + |
| 69 | + Mockito.verifyNoMoreInteractions(etlsSpy, cisSpy,sisSpy); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void verifyDelegatePassthroughCreation2() throws Exception { |
| 74 | + ArgumentCaptor<EventType> eventTypeCapture = ArgumentCaptor.forClass(EventType.class); |
| 75 | + ArgumentCaptor<String> logNameCapture = ArgumentCaptor.forClass(String.class); |
| 76 | + PowerMockito.whenNew(EventTypeLogSupplier.class).withArguments(eventTypeCapture.capture()).thenReturn(etlsSpy); |
| 77 | + PowerMockito.whenNew(ClientInfoSupplier.class).withNoArguments().thenReturn(cisSpy); |
| 78 | + PowerMockito.whenNew(ServerInfoSupplier.class).withArguments(logNameCapture.capture()).thenReturn(sisSpy); |
| 79 | + |
| 80 | + LogPrefixAppender lpa = new LogPrefixAppender(false,false,false ,null); |
| 81 | + String result = lpa.appendTo( testName.getMethodName()+"-LOGGER", Logger.EVENT_UNSPECIFIED, testName.getMethodName()+"-MESSAGE"); |
| 82 | + |
| 83 | + //Based on the forced returns in the before block |
| 84 | + Assert.assertEquals("[EVENT_TYPE CLIENT_INFO -> SERVER_INFO] "+ testName.getMethodName()+"-MESSAGE", result); |
| 85 | + |
| 86 | + Assert.assertEquals(Logger.EVENT_UNSPECIFIED, eventTypeCapture.getValue()); |
| 87 | + Assert.assertEquals(testName.getMethodName()+"-LOGGER",logNameCapture.getValue()); |
| 88 | + |
| 89 | + Mockito.verify(etlsSpy, Mockito.times(1)).get(); |
| 90 | + Mockito.verify(cisSpy, Mockito.times(1)).get(); |
| 91 | + Mockito.verify(sisSpy, Mockito.times(1)).get(); |
| 92 | + |
| 93 | + Mockito.verify(cisSpy, Mockito.times(1)).setLogUserInfo(false); |
| 94 | + Mockito.verify(sisSpy, Mockito.times(1)).setLogServerIp(false); |
| 95 | + Mockito.verify(sisSpy, Mockito.times(1)).setLogApplicationName(false, null); |
| 96 | + |
| 97 | + Mockito.verifyNoMoreInteractions(etlsSpy, cisSpy,sisSpy); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments