use of com.mercedesbenz.sechub.sharedkernel.UserContextService in project sechub by mercedes-benz.
the class DefaultSecurityLogServiceTest method beforeEach.
@BeforeEach
void beforeEach() {
logger = mock(Logger.class);
userContextService = mock(UserContextService.class);
// mock logsanitizer
logsanitzer = mock(LogSanitizer.class);
when(logsanitzer.sanitize(anyString(), anyInt())).thenAnswer(i -> SANITIZED + i.getArguments()[0]);
when(logsanitzer.sanitize(anyString(), anyInt(), anyBoolean())).thenAnswer(i -> SANITIZED + i.getArguments()[0]);
// mock obfuscation
authorizedValueObfuscator = mock(AuthorizeValueObfuscator.class);
when(authorizedValueObfuscator.obfuscate(anyString(), anyInt())).thenAnswer(i -> "obfuscated" + i.getArguments()[0]);
// mock http data providers
requestAttributesProvider = mock(RequestAttributesProvider.class);
request = mock(HttpServletRequest.class);
// final methods, so not by mockito
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
httpSession = mock(HttpSession.class);
when(request.getRemoteAddr()).thenReturn("fake-remote-addr");
when(httpSession.getId()).thenReturn("fake-http-session-id");
when(request.getRequestURI()).thenReturn("fake-request-uri");
when(requestAttributesProvider.getRequestAttributes()).thenReturn(attributes);
when(request.getSession()).thenReturn(httpSession);
Map<String, String> map = new LinkedHashMap<>();
map.put(KEY1, VALUE1);
map.put(KEY2, VALUE2);
map.put(KEY_AUTHORIZE, VALUE_AUTHORIZE);
map.put(KEY_AUTHORIZE_mixcased, VALUE_AUTHORIZE);
Iterator<String> it = map.keySet().iterator();
when(request.getHeaderNames()).thenReturn(new Enumeration<String>() {
@Override
public String nextElement() {
return it.next();
}
@Override
public boolean hasMoreElements() {
return it.hasNext();
}
});
for (String key : map.keySet()) {
when(request.getHeader(key)).thenReturn(map.get(key));
}
/* service to test uses a mocked logger */
serviceToTest = new DefaultSecurityLogService() {
Logger getLogger() {
return logger;
}
};
serviceToTest.logSanititzer = logsanitzer;
serviceToTest.userContextService = userContextService;
serviceToTest.requestAttributesProvider = requestAttributesProvider;
serviceToTest.authorizedValueObfuscator = authorizedValueObfuscator;
}
Aggregations