<commit_before>/*
 * Copyright (c) 2002-2018 "Neo4j,"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.neo4j.driver.internal.async.inbound;

import io.netty.channel.Channel;
import io.netty.channel.ChannelConfig;
import io.netty.channel.DefaultChannelId;
import io.netty.util.Attribute;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;

import java.util.HashMap;
import java.util.Map;

import org.neo4j.driver.internal.spi.AutoReadManagingResponseHandler;
import org.neo4j.driver.internal.spi.ResponseHandler;
import org.neo4j.driver.internal.value.IntegerValue;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.Neo4jException;

import static java.util.Collections.emptyMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
import static org.neo4j.driver.internal.messaging.request.ResetMessage.RESET;
import static org.neo4j.driver.v1.Values.value;

class InboundMessageDispatcherTest
{
    private static final String FAILURE_CODE = "Neo.ClientError.Security.Unauthorized";
    private static final String FAILURE_MESSAGE = "Error Message";

    @Test
    void shouldFailWhenCreatedWithNullChannel()
    {
        assertThrows( NullPointerException.class, () -> new InboundMessageDispatcher( null, DEV_NULL_LOGGING ) );
    }

    @Test
    void shouldFailWhenCreatedWithNullLogging()
    {
        assertThrows( NullPointerException.class, () -> new InboundMessageDispatcher( mock( Channel.class ), null ) );
    }

    @Test
    void shouldDequeHandlerOnSuccess()
    {
        InboundMessageDispatcher dispatcher = newDispatcher();

        ResponseHandler handler = mock( ResponseHandler.class );
        dispatcher.enqueue( handler );
        assertEquals( 1, dispatcher.queuedHandlersCount() );

        Map<String,Value> metadata = new HashMap<>();
        metadata.put( "key1", value( 1 ) );
        metadata.put( "key2", value( "2" ) );
        dispatcher.handleSuccessMessage( metadata );

        assertEquals( 0, dispatcher.queuedHandlersCount() );
        verify( handler ).onSuccess( metadata );
    }

    @Test
    void shouldDequeHandlerOnFailure()
    {
        InboundMessageDispatcher dispatcher = newDispatcher();

        ResponseHandler handler = mock( ResponseHandler.class );
        dispatcher.enqueue( handler );
        assertEquals( 1, dispatcher.queuedHandlersCount() );

        dispatcher.handleFailureMessage( FAILURE_CODE, FAILURE_MESSAGE );

        // "RESET after failure" handler should remain queued
        assertEquals( 1, dispatcher.queuedHandlersCount() );
        verifyFailure( handler );
        assertEquals( FAILURE_CODE, ((Neo4jException) dispatcher.currentError()).code() );
        assertEquals( FAILURE_MESSAGE, dispatcher.currentError().getMessage() );
    }

    @Test
    void shouldSendResetOnFailure()
    {
        Channel channel = mock( Channel.class );
        InboundMessageDispatcher dispatcher = newDispatcher( channel );

        dispatcher.enqueue( mock( ResponseHandler.class ) );
        assertEquals( 1, dispatcher.queuedHandlersCount() );

        dispatcher.handleFailureMessage( FAILURE_CODE, FAILURE_MESSAGE );
<commit_msg>Fix mocking in a unit test<commit_after>/*
 * Copyright (c) 2002-2018 "Neo4j,"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.neo4j.driver.internal.async.inbound;

import io.netty.channel.Channel;
import io.netty.channel.ChannelConfig;
import io.netty.channel.DefaultChannelId;
import io.netty.util.Attribute;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;

import java.util.HashMap;
import java.util.Map;

import org.neo4j.driver.internal.spi.AutoReadManagingResponseHandler;
import org.neo4j.driver.internal.spi.ResponseHandler;
import org.neo4j.driver.internal.value.IntegerValue;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.Neo4jException;

import static java.util.Collections.emptyMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
import static org.neo4j.driver.internal.messaging.request.ResetMessage.RESET;
import static org.neo4j.driver.v1.Values.value;

class InboundMessageDispatcherTest
{
    private static final String FAILURE_CODE = "Neo.ClientError.Security.Unauthorized";
    private static final String FAILURE_MESSAGE = "Error Message";

    @Test
    void shouldFailWhenCreatedWithNullChannel()
    {
        assertThrows( NullPointerException.class, () -> new InboundMessageDispatcher( null, DEV_NULL_LOGGING ) );
    }

    @Test
    void shouldFailWhenCreatedWithNullLogging()
    {
        assertThrows( NullPointerException.class, () -> new InboundMessageDispatcher( newChannelMock(), null ) );
    }

    @Test
    void shouldDequeHandlerOnSuccess()
    {
        InboundMessageDispatcher dispatcher = newDispatcher();

        ResponseHandler handler = mock( ResponseHandler.class );
        dispatcher.enqueue( handler );
        assertEquals( 1, dispatcher.queuedHandlersCount() );

        Map<String,Value> metadata = new HashMap<>();
        metadata.put( "key1", value( 1 ) );
        metadata.put( "key2", value( "2" ) );
        dispatcher.handleSuccessMessage( metadata );

        assertEquals( 0, dispatcher.queuedHandlersCount() );
        verify( handler ).onSuccess( metadata );
    }

    @Test
    void shouldDequeHandlerOnFailure()
    {
        InboundMessageDispatcher dispatcher = newDispatcher();

        ResponseHandler handler = mock( ResponseHandler.class );
        dispatcher.enqueue( handler );
        assertEquals( 1, dispatcher.queuedHandlersCount() );

        dispatcher.handleFailureMessage( FAILURE_CODE, FAILURE_MESSAGE );

        // "RESET after failure" handler should remain queued
        assertEquals( 1, dispatcher.queuedHandlersCount() );
        verifyFailure( handler );
        assertEquals( FAILURE_CODE, ((Neo4jException) dispatcher.currentError()).code() );
        assertEquals( FAILURE_MESSAGE, dispatcher.currentError().getMessage() );
    }

    @Test
    void shouldSendResetOnFailure()
    {
        Channel channel = newChannelMock();
        InboundMessageDispatcher dispatcher = newDispatcher( channel );

        dispatcher.enqueue( mock( ResponseHandler.class ) );
        assertEquals( 1, dispatcher.queuedHandlersCount() );

        dispatcher.handleFailureMessage( FAILURE_CODE, FAILURE_MESSAGE );

{'fraction_non_alphanumeric': 0.09706774519716886, 'fraction_numerical': 0.009212448039546118, 'mean_word_length': 4.052213393870602, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4269082', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2489, 'n_words': 603}