|
| 1 | +package com.avides.spring.rabbit.listener; |
| 2 | + |
| 3 | +import static org.easymock.EasyMock.eq; |
| 4 | +import static org.easymock.EasyMock.expect; |
| 5 | +import static org.easymock.EasyMock.mock; |
| 6 | +import static org.powermock.api.easymock.PowerMock.replayAll; |
| 7 | +import static org.powermock.api.easymock.PowerMock.verifyAll; |
| 8 | + |
| 9 | +import org.easymock.EasyMock; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.powermock.api.easymock.annotation.MockStrict; |
| 14 | +import org.powermock.core.classloader.annotations.PowerMockIgnore; |
| 15 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 16 | +import org.powermock.reflect.Whitebox; |
| 17 | +import org.springframework.amqp.core.MessagePostProcessor; |
| 18 | +import org.springframework.amqp.core.MessageProperties; |
| 19 | +import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 20 | + |
| 21 | +import io.micrometer.core.instrument.Counter; |
| 22 | +import io.micrometer.core.instrument.MeterRegistry; |
| 23 | +import io.micrometer.core.instrument.Tag; |
| 24 | +import io.micrometer.core.instrument.Tags; |
| 25 | + |
| 26 | +@RunWith(PowerMockRunner.class) |
| 27 | +@PowerMockIgnore("javax.management.*") |
| 28 | +public class OptionalResponseSpringRabbitListenerTest |
| 29 | +{ |
| 30 | + private OptionalResponseSpringRabbitListener<Object> successRabbitListener; |
| 31 | + |
| 32 | + private OptionalResponseSpringRabbitListener<Object> failureRabbitListener; |
| 33 | + |
| 34 | + @MockStrict |
| 35 | + private RabbitTemplate responseRabbitTemplate; |
| 36 | + |
| 37 | + @MockStrict |
| 38 | + private MeterRegistry meterRegistry; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setup() |
| 42 | + { |
| 43 | + successRabbitListener = new SuccessOptionalSpringRabbitListener(); |
| 44 | + Whitebox.setInternalState(successRabbitListener, meterRegistry); |
| 45 | + |
| 46 | + failureRabbitListener = new FailureOptionalSpringRabbitListener(); |
| 47 | + Whitebox.setInternalState(failureRabbitListener, meterRegistry); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testHandleEventWithoutReplyTo() |
| 52 | + { |
| 53 | + var tags = Tags.of(Tag.of("listener", "SuccessOptionalSpringRabbitListener"), Tag.of("from", "sender-app")); |
| 54 | + |
| 55 | + expect(meterRegistry.counter("rabbit.listener.event", tags)).andReturn(mock(Counter.class)); |
| 56 | + expect(meterRegistry.counter("rabbit.listener.event.total.duration.milliseconds", tags)).andReturn(mock(Counter.class)); |
| 57 | + |
| 58 | + replayAll(); |
| 59 | + var messageProperties = new MessageProperties(); |
| 60 | + messageProperties.setAppId("sender-app"); |
| 61 | + messageProperties.setCorrelationId("request1"); |
| 62 | + successRabbitListener.handle("", messageProperties); |
| 63 | + verifyAll(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testHandleEventWithoutResponse() |
| 68 | + { |
| 69 | + var tags = Tags.of(Tag.of("listener", "FailureOptionalSpringRabbitListener"), Tag.of("from", "sender-app")); |
| 70 | + |
| 71 | + expect(meterRegistry.counter("rabbit.listener.event", tags)).andReturn(mock(Counter.class)); |
| 72 | + expect(meterRegistry.counter("rabbit.listener.event.total.duration.milliseconds", tags)).andReturn(mock(Counter.class)); |
| 73 | + |
| 74 | + replayAll(); |
| 75 | + var messageProperties = new MessageProperties(); |
| 76 | + messageProperties.setAppId("sender-app"); |
| 77 | + messageProperties.setCorrelationId("request1"); |
| 78 | + messageProperties.setReplyTo("response-queue"); |
| 79 | + failureRabbitListener.handle("", messageProperties); |
| 80 | + verifyAll(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testHandleEventWithoutResponseAndAppId() |
| 85 | + { |
| 86 | + var tags = Tags.of(Tag.of("listener", "FailureOptionalSpringRabbitListener"), Tag.of("from", "UNKNOWN")); |
| 87 | + |
| 88 | + expect(meterRegistry.counter("rabbit.listener.event", tags)).andReturn(mock(Counter.class)); |
| 89 | + expect(meterRegistry.counter("rabbit.listener.event.total.duration.milliseconds", tags)).andReturn(mock(Counter.class)); |
| 90 | + |
| 91 | + replayAll(); |
| 92 | + var messageProperties = new MessageProperties(); |
| 93 | + messageProperties.setCorrelationId("request1"); |
| 94 | + messageProperties.setReplyTo("response-queue"); |
| 95 | + failureRabbitListener.handle("", messageProperties); |
| 96 | + verifyAll(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testHandleEvent() |
| 101 | + { |
| 102 | + var tags = Tags.of(Tag.of("listener", "SuccessOptionalSpringRabbitListener"), Tag.of("from", "sender-app")); |
| 103 | + |
| 104 | + responseRabbitTemplate.convertAndSend(eq(""), eq("response-queue"), eq("response"), EasyMock.anyObject(MessagePostProcessor.class)); |
| 105 | + expect(meterRegistry.counter("rabbit.listener.event", tags)).andReturn(mock(Counter.class)); |
| 106 | + expect(meterRegistry.counter("rabbit.listener.event.total.duration.milliseconds", tags)).andReturn(mock(Counter.class)); |
| 107 | + |
| 108 | + replayAll(); |
| 109 | + var messageProperties = new MessageProperties(); |
| 110 | + messageProperties.setAppId("sender-app"); |
| 111 | + messageProperties.setCorrelationId("request1"); |
| 112 | + messageProperties.setReplyTo("response-queue"); |
| 113 | + successRabbitListener.handle("", messageProperties); |
| 114 | + verifyAll(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testHandleEventWithoutAppId() |
| 119 | + { |
| 120 | + var tags = Tags.of(Tag.of("listener", "SuccessOptionalSpringRabbitListener"), Tag.of("from", "UNKNOWN")); |
| 121 | + |
| 122 | + responseRabbitTemplate.convertAndSend(eq(""), eq("response-queue"), eq("response"), EasyMock.anyObject(MessagePostProcessor.class)); |
| 123 | + expect(meterRegistry.counter("rabbit.listener.event", tags)).andReturn(mock(Counter.class)); |
| 124 | + expect(meterRegistry.counter("rabbit.listener.event.total.duration.milliseconds", tags)).andReturn(mock(Counter.class)); |
| 125 | + |
| 126 | + replayAll(); |
| 127 | + var messageProperties = new MessageProperties(); |
| 128 | + messageProperties.setCorrelationId("request1"); |
| 129 | + messageProperties.setReplyTo("response-queue"); |
| 130 | + successRabbitListener.handle("", messageProperties); |
| 131 | + verifyAll(); |
| 132 | + } |
| 133 | + |
| 134 | + private class SuccessOptionalSpringRabbitListener extends OptionalResponseSpringRabbitListener<Object> |
| 135 | + { |
| 136 | + public SuccessOptionalSpringRabbitListener() |
| 137 | + { |
| 138 | + super(responseRabbitTemplate); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected Object processRequest(Object requestObject) |
| 143 | + { |
| 144 | + return "response"; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private class FailureOptionalSpringRabbitListener extends OptionalResponseSpringRabbitListener<Object> |
| 149 | + { |
| 150 | + public FailureOptionalSpringRabbitListener() |
| 151 | + { |
| 152 | + super(responseRabbitTemplate); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + protected Object processRequest(Object requestObject) |
| 157 | + { |
| 158 | + return null; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments