Search in sources :

Example 1 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project camel by apache.

the class CouchDbProducerTest method testStringBodyIsConvertedToJsonTree.

@Test
public void testStringBodyIsConvertedToJsonTree() throws Exception {
    when(msg.getMandatoryBody()).thenReturn("{ \"name\" : \"coldplay\" }");
    when(client.save(anyObject())).thenAnswer(new Answer<Response>() {

        @Override
        public Response answer(InvocationOnMock invocation) throws Throwable {
            assertTrue(invocation.getArguments()[0].getClass() + " but wanted " + JsonElement.class, invocation.getArguments()[0] instanceof JsonElement);
            return new Response();
        }
    });
    producer.process(exchange);
    verify(client).save(any(JsonObject.class));
}
Also used : Response(org.lightcouch.Response) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 2 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project camel by apache.

the class CouchbaseProducerTest method testTimeOutRetryThenSuccess.

@Test
public void testTimeOutRetryThenSuccess() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            return true;
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    when(exchange.getOut()).thenReturn(msg);
    producer.process(exchange);
    verify(of, times(2)).get();
    verify(msg).setBody(true);
}
Also used : Answer(org.mockito.stubbing.Answer) ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Test(org.junit.Test)

Example 3 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project camel by apache.

the class CouchbaseProducerTest method testTimeOutRetryTwiceThenSuccess.

@Test
public void testTimeOutRetryTwiceThenSuccess() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            return true;
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    when(exchange.getOut()).thenReturn(msg);
    producer.process(exchange);
    verify(of, times(3)).get();
    verify(msg).setBody(true);
}
Also used : Answer(org.mockito.stubbing.Answer) ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Test(org.junit.Test)

Example 4 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project camel by apache.

the class CouchbaseProducerTest method testExpiryTimeIsSet.

@Test
public void testExpiryTimeIsSet() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            return true;
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    // Mock out some headers so we can set an expiry
    int expiry = 5000;
    Map<String, Object> testHeaders = new HashMap<String, Object>();
    testHeaders.put("CCB_TTL", Integer.toString(expiry));
    when(msg.getHeaders()).thenReturn(testHeaders);
    when(msg.getHeader(HEADER_TTL, String.class)).thenReturn(Integer.toString(expiry));
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    Message outmsg = mock(Message.class);
    when(exchange.getOut()).thenReturn(msg);
    producer.process(exchange);
    verify(client).set(org.mockito.Matchers.anyString(), Mockito.eq(expiry), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class));
}
Also used : Message(org.apache.camel.Message) HashMap(java.util.HashMap) InvalidPayloadException(org.apache.camel.InvalidPayloadException) ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) Test(org.junit.Test)

Example 5 with InvocationOnMock

use of org.mockito.invocation.InvocationOnMock in project camel by apache.

the class HipchatComponentMultipleUsersTest method sendInOnlyMultipleUsers.

@Test
public void sendInOnlyMultipleUsers() throws Exception {
    result.expectedMessageCount(2);
    result.expectedHeaderValuesReceivedInAnyOrder(HipchatConstants.FROM_USER, Arrays.asList(new String[] { "@AUser", "@BUser" }));
    final String expectedResponse = "{\n" + "  \"items\" : [\n" + "    {\n" + "      \"date\" : \"2015-01-19T22:07:11.030740+00:00\",\n" + "      \"from\" : {\n" + "        \"id\" : 1647095,\n" + "        \"links\" : {\n" + "          \"self\" : \"https://api.hipchat.com/v2/user/1647095\"\n" + "        },\n" + "        \"mention_name\" : \"notifier\",\n" + "        \"name\" : \"Message Notifier\"\n" + "      },\n" + "      \"id\" : \"6567c6f7-7c1b-43cf-bed0-792b1d092919\",\n" + "      \"mentions\" : [ ],\n" + "      \"message\" : \"Unit test Alert\",\n" + "      \"type\" : \"message\"\n" + "    }\n" + "  ],\n" + "  \"links\" : {\n" + "    \"self\" : \"https://api.hipchat.com/v2/user/%40ShreyasPurohit/history/latest\"\n" + "  },\n" + "  \"maxResults\" : 1,\n" + "  \"startIndex\" : 0\n" + "}";
    HttpEntity mockHttpEntity = mock(HttpEntity.class);
    when(mockHttpEntity.getContent()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            return new ByteArrayInputStream(expectedResponse.getBytes(StandardCharsets.UTF_8));
        }
    });
    when(closeableHttpResponse.getEntity()).thenReturn(mockHttpEntity);
    when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, ""));
    assertMockEndpointsSatisfied();
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

InvocationOnMock (org.mockito.invocation.InvocationOnMock)947 Test (org.junit.Test)544 Answer (org.mockito.stubbing.Answer)245 Matchers.anyString (org.mockito.Matchers.anyString)104 HashMap (java.util.HashMap)101 ArrayList (java.util.ArrayList)96 Before (org.junit.Before)96 Mockito.doAnswer (org.mockito.Mockito.doAnswer)96 IOException (java.io.IOException)86 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)73 Test (org.testng.annotations.Test)59 CountDownLatch (java.util.concurrent.CountDownLatch)57 List (java.util.List)56 File (java.io.File)49 Configuration (org.apache.hadoop.conf.Configuration)46 AtomicReference (java.util.concurrent.atomic.AtomicReference)44 Context (android.content.Context)39 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)36 ServiceCallback (com.microsoft.azure.mobile.http.ServiceCallback)33 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32