Search in sources :

Example 1 with RETURNS_DEEP_STUBS

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);
    }
}
Also used : ROLE_MOBILE(org.opennms.web.api.Authentication.ROLE_MOBILE) ROLE_READONLY(org.opennms.web.api.Authentication.ROLE_READONLY) Arrays(java.util.Arrays) ROLE_DELEGATE(org.opennms.web.api.Authentication.ROLE_DELEGATE) Assert.assertNotNull(org.junit.Assert.assertNotNull) SecurityContext(javax.ws.rs.core.SecurityContext) ROLE_ADMIN(org.opennms.web.api.Authentication.ROLE_ADMIN) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) Assert.assertNull(org.junit.Assert.assertNull) WebApplicationException(javax.ws.rs.WebApplicationException) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) ROLE_USER(org.opennms.web.api.Authentication.ROLE_USER) ROLE_REST(org.opennms.web.api.Authentication.ROLE_REST) Mockito.mock(org.mockito.Mockito.mock) WebApplicationException(javax.ws.rs.WebApplicationException) SecurityContext(javax.ws.rs.core.SecurityContext) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet)

Example 2 with RETURNS_DEEP_STUBS

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;
    });
}
Also used : VersionContext(org.neo4j.io.pagecache.context.VersionContext) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) TestExecutorCaffeineCacheFactory$(org.neo4j.cypher.internal.cache.TestExecutorCaffeineCacheFactory$) CursorContext(org.neo4j.io.pagecache.context.CursorContext) Config(org.neo4j.configuration.Config) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) NullLogProvider(org.neo4j.logging.NullLogProvider) Answer(org.mockito.stubbing.Answer) ImpermanentDbmsExtension(org.neo4j.test.extension.ImpermanentDbmsExtension) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Inject(org.neo4j.test.extension.Inject) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) VersionContext(org.neo4j.io.pagecache.context.VersionContext) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) NULL(org.neo4j.io.pagecache.tracing.cursor.PageCursorTracer.NULL) CompilerFactory(org.neo4j.cypher.internal.CompilerFactory) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryStatistics(org.neo4j.graphdb.QueryStatistics) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution) Mockito.mock(org.mockito.Mockito.mock) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryStatistics(org.neo4j.graphdb.QueryStatistics) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) CursorContext(org.neo4j.io.pagecache.context.CursorContext) QueryExecution(org.neo4j.kernel.impl.query.QueryExecution) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with RETURNS_DEEP_STUBS

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;
    }
}
Also used : Response(org.eclipse.jetty.server.Response) Request(org.eclipse.jetty.server.Request) Response(org.eclipse.jetty.server.Response) ConsoleAppenderFactory(io.dropwizard.logging.ConsoleAppenderFactory) BeforeEach(org.junit.jupiter.api.BeforeEach) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SyslogAppenderFactory(io.dropwizard.logging.SyslogAppenderFactory) Mockito.timeout(org.mockito.Mockito.timeout) RequestLogFactory(io.dropwizard.request.logging.RequestLogFactory) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpChannelState(org.eclipse.jetty.server.HttpChannelState) Appender(ch.qos.logback.core.Appender) Jackson(io.dropwizard.jackson.Jackson) YamlConfigurationFactory(io.dropwizard.configuration.YamlConfigurationFactory) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) BaseValidator(io.dropwizard.validation.BaseValidator) TimeZone(java.util.TimeZone) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DiscoverableSubtypeResolver(io.dropwizard.jackson.DiscoverableSubtypeResolver) ResourceConfigurationSourceProvider(io.dropwizard.configuration.ResourceConfigurationSourceProvider) FileAppenderFactory(io.dropwizard.logging.FileAppenderFactory) Mockito.when(org.mockito.Mockito.when) BootstrapLogging(io.dropwizard.logging.BootstrapLogging) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CustomRequestLog(org.eclipse.jetty.server.CustomRequestLog) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) CustomRequestLog(org.eclipse.jetty.server.CustomRequestLog) HttpChannelState(org.eclipse.jetty.server.HttpChannelState) Request(org.eclipse.jetty.server.Request) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Test(org.junit.jupiter.api.Test)

Aggregations

RETURNS_DEEP_STUBS (org.mockito.Mockito.RETURNS_DEEP_STUBS)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.when (org.mockito.Mockito.when)3 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 Mockito.verify (org.mockito.Mockito.verify)2 Answer (org.mockito.stubbing.Answer)2 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 Appender (ch.qos.logback.core.Appender)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ResourceConfigurationSourceProvider (io.dropwizard.configuration.ResourceConfigurationSourceProvider)1 YamlConfigurationFactory (io.dropwizard.configuration.YamlConfigurationFactory)1 DiscoverableSubtypeResolver (io.dropwizard.jackson.DiscoverableSubtypeResolver)1 Jackson (io.dropwizard.jackson.Jackson)1 BootstrapLogging (io.dropwizard.logging.BootstrapLogging)1 ConsoleAppenderFactory (io.dropwizard.logging.ConsoleAppenderFactory)1 FileAppenderFactory (io.dropwizard.logging.FileAppenderFactory)1 SyslogAppenderFactory (io.dropwizard.logging.SyslogAppenderFactory)1 RequestLogFactory (io.dropwizard.request.logging.RequestLogFactory)1 BaseValidator (io.dropwizard.validation.BaseValidator)1