use of org.mockito.Mockito.RETURNS_DEEP_STUBS in project opennms by OpenNMS.
the class SecurityHelperTest method assertUserEditPrivileges.
private void assertUserEditPrivileges(boolean isAllowed, String ackUser, String... roles) {
final Set<String> userRoles = new HashSet<>(Arrays.asList(roles));
SecurityContext securityContext = mock(SecurityContext.class, RETURNS_DEEP_STUBS);
when(securityContext.getUserPrincipal().getName()).thenReturn(USER);
when(securityContext.isUserInRole(anyString())).thenAnswer((Answer) invocation -> {
final String role = invocation.getArgumentAt(0, String.class);
return userRoles.contains(role);
});
WebApplicationException ex = null;
try {
SecurityHelper.assertUserEditCredentials(securityContext, ackUser);
} catch (WebApplicationException e) {
ex = e;
}
if (isAllowed) {
assertNull("Should be allowed, but got: " + ex, ex);
} else {
assertNotNull("Should not be allowed, but passed.", ex);
}
}
use of org.mockito.Mockito.RETURNS_DEEP_STUBS in project neo4j by neo4j.
the class SnapshotExecutionEngineTest method setUp.
@BeforeEach
void setUp() throws Exception {
transactionalContext = mock(TransactionalContext.class, RETURNS_DEEP_STUBS);
KernelStatement kernelStatement = mock(KernelStatement.class);
executor = mock(SnapshotExecutionEngine.QueryExecutor.class);
versionContext = mock(VersionContext.class);
statistics = mock(QueryStatistics.class);
executionEngine = new SnapshotExecutionEngine(new GraphDatabaseCypherService(db), config, TestExecutorCaffeineCacheFactory$.MODULE$, NullLogProvider.getInstance(), mock(CompilerFactory.class));
when(transactionalContext.kernelTransaction().cursorContext()).thenReturn(new CursorContext(NULL, versionContext));
when(transactionalContext.statement()).thenReturn(kernelStatement);
var innerExecution = mock(QueryExecution.class);
when(executor.execute(any())).thenAnswer((Answer<QueryExecution>) invocationOnMock -> {
MaterialisedResult materialisedResult = invocationOnMock.getArgument(0);
materialisedResult.onResultCompleted(statistics);
return innerExecution;
});
}
use of org.mockito.Mockito.RETURNS_DEEP_STUBS in project dropwizard by dropwizard.
the class LogbackClassicRequestLogFactoryTest method testLogFormat.
@Test
void testLogFormat() throws Exception {
final LogbackClassicRequestLogFactory factory = new LogbackClassicRequestLogFactory();
@SuppressWarnings("unchecked") final Appender<ILoggingEvent> appender = mock(Appender.class);
final Request request = mock(Request.class);
final Response response = mock(Response.class, RETURNS_DEEP_STUBS);
final HttpChannelState channelState = mock(HttpChannelState.class);
factory.setAppenders(Collections.singletonList((context, applicationName, layoutFactory, levelFilterFactory, asyncAppenderFactory) -> appender));
final String tz = TimeZone.getAvailableIDs((int) TimeUnit.HOURS.toMillis(-5))[0];
factory.setTimeZone(TimeZone.getTimeZone(tz));
CustomRequestLog logger = null;
try {
when(channelState.isInitial()).thenReturn(true);
when(request.getRemoteHost()).thenReturn("10.0.0.1");
// Jetty log format compares against System.currentTimeMillis, so there
// isn't a way for us to set our own clock
when(request.getTimeStamp()).thenReturn(System.currentTimeMillis());
when(request.getMethod()).thenReturn("GET");
when(request.getRequestURI()).thenReturn("/test/things");
when(request.getProtocol()).thenReturn("HTTP/1.1");
when(request.getHttpChannelState()).thenReturn(channelState);
when(response.getCommittedMetaData().getStatus()).thenReturn(200);
when(response.getHttpChannel().getBytesWritten()).thenReturn(8290L);
final ArgumentCaptor<ILoggingEvent> captor = ArgumentCaptor.forClass(ILoggingEvent.class);
logger = (CustomRequestLog) factory.build("my-app");
logger.log(request, response);
verify(appender, timeout(1000)).doAppend(captor.capture());
final ILoggingEvent event = captor.getValue();
assertThat(event.getFormattedMessage()).startsWith("10.0.0.1").doesNotContain("%").contains("\"GET /test/things HTTP/1.1\"").contains("-0500");
} catch (Exception e) {
if (logger != null) {
logger.stop();
}
throw e;
}
}
Aggregations