|
| 1 | +package com.iluwatar.poison.pill; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.mockito.ArgumentCaptor; |
| 5 | + |
| 6 | +import static org.junit.Assert.assertEquals; |
| 7 | +import static org.junit.Assert.assertNotNull; |
| 8 | +import static org.junit.Assert.fail; |
| 9 | +import static org.mockito.Matchers.eq; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 13 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 14 | + |
| 15 | +/** |
| 16 | + * Date: 12/27/15 - 10:32 PM |
| 17 | + * |
| 18 | + * @author Jeroen Meulemeester |
| 19 | + */ |
| 20 | +public class ProducerTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testSend() throws Exception { |
| 24 | + final MqPublishPoint publishPoint = mock(MqPublishPoint.class); |
| 25 | + final Producer producer = new Producer("producer", publishPoint); |
| 26 | + verifyZeroInteractions(publishPoint); |
| 27 | + |
| 28 | + producer.send("Hello!"); |
| 29 | + |
| 30 | + final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class); |
| 31 | + verify(publishPoint).put(messageCaptor.capture()); |
| 32 | + |
| 33 | + final Message message = messageCaptor.getValue(); |
| 34 | + assertNotNull(message); |
| 35 | + assertEquals("producer", message.getHeader(Message.Headers.SENDER)); |
| 36 | + assertNotNull(message.getHeader(Message.Headers.DATE)); |
| 37 | + assertEquals("Hello!", message.getBody()); |
| 38 | + |
| 39 | + verifyNoMoreInteractions(publishPoint); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testStop() throws Exception { |
| 44 | + final MqPublishPoint publishPoint = mock(MqPublishPoint.class); |
| 45 | + final Producer producer = new Producer("producer", publishPoint); |
| 46 | + verifyZeroInteractions(publishPoint); |
| 47 | + |
| 48 | + producer.stop(); |
| 49 | + verify(publishPoint).put(eq(Message.POISON_PILL)); |
| 50 | + |
| 51 | + try { |
| 52 | + producer.send("Hello!"); |
| 53 | + fail("Expected 'IllegalStateException' at this point, since the producer has stopped!"); |
| 54 | + } catch (IllegalStateException e) { |
| 55 | + assertNotNull(e); |
| 56 | + assertNotNull(e.getMessage()); |
| 57 | + assertEquals("Producer Hello! was stopped and fail to deliver requested message [producer].", |
| 58 | + e.getMessage()); |
| 59 | + } |
| 60 | + |
| 61 | + verifyNoMoreInteractions(publishPoint); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments