Search in sources :

Example 1 with ConnectionHandler

use of org.mule.runtime.api.connection.ConnectionHandler in project mule by mulesoft.

the class PagingProviderProducerTestCase method setUp.

@Before
public void setUp() throws MuleException {
    when(config.getValue()).thenReturn("config");
    ConnectionHandler handler = mock(ConnectionHandler.class);
    when(handler.getConnection()).thenReturn(new Object());
    when(extensionConnectionSupplier.getConnection(executionContext)).thenReturn(handler);
}
Also used : ConnectionHandler(org.mule.runtime.api.connection.ConnectionHandler) Matchers.anyObject(org.mockito.Matchers.anyObject) Before(org.junit.Before)

Example 2 with ConnectionHandler

use of org.mule.runtime.api.connection.ConnectionHandler in project mule by mulesoft.

the class PagingProviderProducerTestCase method produceWithDifferentConnections.

@Test
public void produceWithDifferentConnections() throws Exception {
    ConnectionHandler connectionHandler = mock(ConnectionHandler.class);
    when(extensionConnectionSupplier.getConnection(any())).thenReturn(connectionHandler);
    produce();
    produce();
    verify(connectionHandler, times(2)).getConnection();
    verify(connectionHandler, times(2)).release();
}
Also used : ConnectionHandler(org.mule.runtime.api.connection.ConnectionHandler) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with ConnectionHandler

use of org.mule.runtime.api.connection.ConnectionHandler in project mule by mulesoft.

the class PagingProviderProducerTestCase method produceWithStickyConnection.

@Test
public void produceWithStickyConnection() throws Exception {
    when(delegate.useStickyConnections()).thenReturn(true);
    producer = createProducer();
    ConnectionHandler connectionHandler = mock(ConnectionHandler.class);
    when(extensionConnectionSupplier.getConnection(any())).thenReturn(connectionHandler);
    produce();
    produce();
    verify(connectionHandler, times(1)).getConnection();
    verify(connectionHandler, never()).release();
    producer.close();
    verify(connectionHandler).release();
}
Also used : ConnectionHandler(org.mule.runtime.api.connection.ConnectionHandler) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 4 with ConnectionHandler

use of org.mule.runtime.api.connection.ConnectionHandler in project mule by mulesoft.

the class ConnectionArgumentResolverTestCase method resolve.

@Test
public void resolve() throws Exception {
    ConnectionHandler connectionHandler = mock(ConnectionHandler.class);
    final Object connection = new Object();
    when(connectionHandler.getConnection()).thenReturn(connection);
    when(operationContext.getVariable(CONNECTION_PARAM)).thenReturn(connectionHandler);
    assertThat(resolver.resolve(operationContext).get(), is(sameInstance(connection)));
}
Also used : ConnectionHandler(org.mule.runtime.api.connection.ConnectionHandler) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 5 with ConnectionHandler

use of org.mule.runtime.api.connection.ConnectionHandler in project mule by mulesoft.

the class ConnectionArgumentResolver method resolve.

/**
 * Returns the connection previously set on the {@code executionContext} under the key
 * {@link ExtensionProperties#CONNECTION_PARAM}
 *
 * @param executionContext an {@link ExecutionContext}
 * @return the connection
 * @throws IllegalArgumentException if the connection was not set
 * @throws ClassCastException if {@code executionContext} is not an {@link ExecutionContextAdapter}
 */
@Override
public LazyValue<Object> resolve(ExecutionContext executionContext) {
    return new LazyValue<>(() -> {
        ConnectionHandler connectionHandler = ((ExecutionContextAdapter<ComponentModel>) executionContext).getVariable(CONNECTION_PARAM);
        checkArgument(connectionHandler != null, "No connection was provided for the component [" + executionContext.getComponentModel().getName() + "]");
        try {
            return connectionHandler.getConnection();
        } catch (ConnectionException e) {
            throw new MuleRuntimeException(I18nMessageFactory.createStaticMessage(String.format("Error was found trying to obtain a connection to execute %s '%s' of extension '%s'", getComponentModelTypeName(executionContext.getComponentModel()), executionContext.getComponentModel().getName(), executionContext.getExtensionModel().getName())), e);
        }
    });
}
Also used : LazyValue(org.mule.runtime.api.util.LazyValue) ConnectionHandler(org.mule.runtime.api.connection.ConnectionHandler) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ExecutionContextAdapter(org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter) ConnectionException(org.mule.runtime.api.connection.ConnectionException)

Aggregations

ConnectionHandler (org.mule.runtime.api.connection.ConnectionHandler)5 Test (org.junit.Test)3 SmallTest (org.mule.tck.size.SmallTest)3 Before (org.junit.Before)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1 ConnectionException (org.mule.runtime.api.connection.ConnectionException)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 LazyValue (org.mule.runtime.api.util.LazyValue)1 ExecutionContextAdapter (org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter)1