Skip to content

Commit

Permalink
add error test case #2753
Browse files Browse the repository at this point in the history
  • Loading branch information
yurake committed Oct 15, 2022
1 parent b009f06 commit 571a05b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
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;
Expand All @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
}
}

0 comments on commit 571a05b

Please sign in to comment.