|
| 1 | +package com.linkedin.venice.controller; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | +import static org.testng.Assert.assertFalse; |
| 6 | +import static org.testng.Assert.assertTrue; |
| 7 | + |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.Set; |
| 10 | +import javax.servlet.http.HttpServletRequest; |
| 11 | +import org.testng.annotations.BeforeMethod; |
| 12 | +import org.testng.annotations.Test; |
| 13 | +import spark.Request; |
| 14 | + |
| 15 | + |
| 16 | +public class AuditInfoTest { |
| 17 | + private static final String TEST_URL = "http://localhost/test"; |
| 18 | + private static final String METHOD_GET = "GET"; |
| 19 | + private static final String CLIENT_IP = "127.0.0.1"; |
| 20 | + private static final int CLIENT_PORT = 8080; |
| 21 | + private static final String PARAM_1 = "param1"; |
| 22 | + private static final String PARAM_2 = "param2"; |
| 23 | + private static final String VALUE_1 = "value1"; |
| 24 | + private static final String VALUE_2 = "value2"; |
| 25 | + private static final String AUDIT_PREFIX = "[AUDIT]"; |
| 26 | + private static final String SUCCESS = "SUCCESS"; |
| 27 | + private static final String FAILURE = "FAILURE"; |
| 28 | + private static final String ERROR_MESSAGE = "Some error"; |
| 29 | + |
| 30 | + private Request request; |
| 31 | + private AuditInfo auditInfo; |
| 32 | + private HttpServletRequest httpServletRequest; |
| 33 | + |
| 34 | + @BeforeMethod |
| 35 | + public void setUp() { |
| 36 | + request = mock(Request.class); |
| 37 | + when(request.url()).thenReturn(TEST_URL); |
| 38 | + when(request.requestMethod()).thenReturn(METHOD_GET); |
| 39 | + when(request.ip()).thenReturn(CLIENT_IP); |
| 40 | + |
| 41 | + Set<String> queryParams = new HashSet<>(); |
| 42 | + queryParams.add(PARAM_1); |
| 43 | + queryParams.add(PARAM_2); |
| 44 | + |
| 45 | + when(request.queryParams()).thenReturn(queryParams); |
| 46 | + when(request.queryParams(PARAM_1)).thenReturn(VALUE_1); |
| 47 | + when(request.queryParams(PARAM_2)).thenReturn(VALUE_2); |
| 48 | + |
| 49 | + httpServletRequest = mock(HttpServletRequest.class); |
| 50 | + when(httpServletRequest.getRemotePort()).thenReturn(CLIENT_PORT); |
| 51 | + when(request.raw()).thenReturn(httpServletRequest); |
| 52 | + |
| 53 | + auditInfo = new AuditInfo(request); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testToStringReturnsExpectedFormat() { |
| 58 | + String result = auditInfo.toString(); |
| 59 | + assertTrue(result.contains(AUDIT_PREFIX)); |
| 60 | + assertTrue(result.contains(METHOD_GET)); |
| 61 | + assertTrue(result.contains(TEST_URL)); |
| 62 | + assertTrue(result.contains(PARAM_1 + "=" + VALUE_1)); |
| 63 | + assertTrue(result.contains(PARAM_2 + "=" + VALUE_2)); |
| 64 | + assertTrue(result.contains("ClientIP: " + CLIENT_IP + ":" + CLIENT_PORT)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testSuccessStringReturnsExpectedFormat() { |
| 69 | + String result = auditInfo.successString(); |
| 70 | + assertTrue(result.contains(AUDIT_PREFIX)); |
| 71 | + assertTrue(result.contains(SUCCESS)); |
| 72 | + assertTrue(result.contains(METHOD_GET)); |
| 73 | + assertTrue(result.contains(TEST_URL)); |
| 74 | + assertTrue(result.contains("ClientIP: " + CLIENT_IP)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testFailureStringReturnsExpectedFormat() { |
| 79 | + String result = auditInfo.failureString(ERROR_MESSAGE); |
| 80 | + assertTrue(result.contains(AUDIT_PREFIX)); |
| 81 | + assertTrue(result.contains(FAILURE)); |
| 82 | + assertTrue(result.contains(ERROR_MESSAGE)); |
| 83 | + assertTrue(result.contains(METHOD_GET)); |
| 84 | + assertTrue(result.contains(TEST_URL)); |
| 85 | + assertTrue(result.contains("ClientIP: " + CLIENT_IP)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testFailureStringHandlesNullErrorMessage() { |
| 90 | + String result = auditInfo.failureString(null); |
| 91 | + assertTrue(result.contains(AUDIT_PREFIX)); |
| 92 | + assertFalse(result.contains("null")); |
| 93 | + } |
| 94 | +} |
0 commit comments