diff --git a/application/consumer-activemq-quarkus/src/main/java/webapp/tier/service/ActiveMqSubscribeService.java b/application/consumer-activemq-quarkus/src/main/java/webapp/tier/service/ActiveMqSubscribeService.java index 205e14f4a..496008a2b 100644 --- a/application/consumer-activemq-quarkus/src/main/java/webapp/tier/service/ActiveMqSubscribeService.java +++ b/application/consumer-activemq-quarkus/src/main/java/webapp/tier/service/ActiveMqSubscribeService.java @@ -32,26 +32,35 @@ public class ActiveMqSubscribeService implements Runnable { String topicname; private final Logger logger = Logger.getLogger(this.getClass().getSimpleName()); - private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); - + private final ScheduledExecutorService scheduler = Executors + .newSingleThreadScheduledExecutor(); + private static boolean isEnableReceived = true; + void onStart(@Observes StartupEvent ev) { scheduler.scheduleWithFixedDelay(this, 0L, 5L, TimeUnit.SECONDS); logger.log(Level.INFO, "Subscribe is starting..."); } void onStop(@Observes ShutdownEvent ev) { + stopReceived(); scheduler.shutdown(); logger.log(Level.INFO, "Subscribe is stopping..."); } + + public static void stopReceived() { + ActiveMqSubscribeService.isEnableReceived = false; + } public void run() { - try (JMSContext context = connectionFactory - .createContext(Session.AUTO_ACKNOWLEDGE); - JMSConsumer consumer = context - .createConsumer(context.createTopic(topicname))) { - amqdelconsumer.consume(consumer); - } catch (Exception e) { - logger.log(Level.SEVERE, "Subscribe Error.", e); + while (isEnableReceived) { + try (JMSContext context = connectionFactory + .createContext(Session.AUTO_ACKNOWLEDGE); + JMSConsumer consumer = context + .createConsumer(context.createTopic(topicname))) { + amqdelconsumer.consume(consumer); + } catch (Exception e) { + logger.log(Level.SEVERE, "Subscribe Error.", e); + } } } } diff --git a/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/ActiveMqSubscribeServiceErrorTest.java b/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/ActiveMqSubscribeServiceErrorTest.java new file mode 100644 index 000000000..cf6e7f690 --- /dev/null +++ b/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/ActiveMqSubscribeServiceErrorTest.java @@ -0,0 +1,81 @@ +package webapp.tier.service; + +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import javax.inject.Inject; +import javax.jms.ConnectionFactory; +import javax.jms.JMSContext; +import javax.jms.Session; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import io.quarkus.artemis.test.ArtemisTestResource; +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.junit.QuarkusMock; +import io.quarkus.test.junit.QuarkusTest; +import webapp.tier.bean.MsgBean; +import webapp.tier.util.CreateId; +import webapp.tier.util.MsgUtils; + +@QuarkusTest +@QuarkusTestResource(ArtemisTestResource.class) +class ActiveMqSubscribeServiceErrorTest { + + @Inject + ConnectionFactory connectionFactory; + + @ConfigProperty(name = "common.message") + String message; + @ConfigProperty(name = "activemq.split.key") + String splitkey; + @ConfigProperty(name = "activemq.topic.name") + String topicname; + + private static final String respbody = "Test Response OK from Mock"; + + private static MockDeliverService mock = null; + + @BeforeAll + static void init() { + mock = mock(MockDeliverService.class); + } + + @BeforeEach + void setup() { + clearInvocations(mock); + } + + @AfterEach + void tearDown() { + reset(mock); + } + + @Test + void testSubscribeError() { + when(mock.random()).thenThrow(new RuntimeException("Rest response error")); + QuarkusMock.installMockForType(mock, MockDeliverService.class); + + try { + MsgBean msgbean = new MsgBean(CreateId.createid(), message, "Publish"); + String body = MsgUtils.createBody(msgbean, splitkey); + try (JMSContext context = connectionFactory + .createContext(Session.AUTO_ACKNOWLEDGE)) { + context.createProducer().send(context.createTopic(topicname), + context.createTextMessage(body)); + } + Thread.sleep(1000); + assertThat(msgbean.getFullmsg(), not(containsString(respbody))); + verify(mock, atLeastOnce()).random(); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } +} diff --git a/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/ActiveMqSubscribeServiceTest.java b/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/ActiveMqSubscribeServiceTest.java index 512a501a3..a8adcd073 100644 --- a/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/ActiveMqSubscribeServiceTest.java +++ b/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/ActiveMqSubscribeServiceTest.java @@ -3,6 +3,7 @@ import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.*; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; import javax.inject.Inject; import javax.jms.ConnectionFactory; @@ -10,10 +11,14 @@ import javax.jms.Session; import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import io.quarkus.artemis.test.ArtemisTestResource; import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.junit.QuarkusMock; import io.quarkus.test.junit.QuarkusTest; import webapp.tier.bean.MsgBean; import webapp.tier.util.CreateId; @@ -33,10 +38,30 @@ class ActiveMqSubscribeServiceTest { @ConfigProperty(name = "activemq.topic.name") String topicname; - private static final String respbody = "message: Hello k8s-3tier-webapp with quarkus"; + private static final String respbody = "Test Response OK from Mock"; + + private static MockDeliverService mock = null; + + @BeforeAll + static void init() { + mock = mock(MockDeliverService.class); + } + + @BeforeEach + void setup() { + clearInvocations(mock); + } + + @AfterEach + void tearDown() { + reset(mock); + } @Test void testSubscribe() { + when(mock.random()).thenReturn(respbody); + QuarkusMock.installMockForType(mock, MockDeliverService.class); + try { MsgBean msgbean = new MsgBean(CreateId.createid(), message, "Publish"); String body = MsgUtils.createBody(msgbean, splitkey); @@ -46,7 +71,8 @@ void testSubscribe() { context.createTextMessage(body)); } Thread.sleep(1000); - assertThat(msgbean.getFullmsg(), containsString(respbody)); + assertThat(msgbean.getFullmsg(), not(containsString(respbody))); + verify(mock, atLeastOnce()).random(); } catch (Exception e) { e.printStackTrace(); fail(); diff --git a/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/MockDeliverService.java b/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/MockDeliverService.java index 32e0a950f..8b81759eb 100644 --- a/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/MockDeliverService.java +++ b/application/consumer-activemq-quarkus/src/test/java/webapp/tier/service/MockDeliverService.java @@ -1,5 +1,8 @@ package webapp.tier.service; +import java.util.logging.Level; +import java.util.logging.Logger; + import javax.enterprise.context.ApplicationScoped; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -13,8 +16,11 @@ @Consumes(MediaType.APPLICATION_JSON) public class MockDeliverService { + private final Logger logger = Logger.getLogger(this.getClass().getSimpleName()); + @GET public String random() { + logger.log(Level.INFO, "Received rest request form consumer"); return "Test"; } } \ No newline at end of file