Skip to content

Commit

Permalink
Merge pull request #3050 from yurake/3049-fix-third-party-code-assess…
Browse files Browse the repository at this point in the history
…ment

3049 fix third party code assessment
  • Loading branch information
yurake authored Feb 15, 2023
2 parents 30aabba + 2bfcb8c commit 0e01648
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,17 @@ class RabbitmqConverterTest {
@Test
void testConvert() throws Exception {
Channel channel = getChannel();

channel.exchangeDeclare(messageExchangeName, TOPIC, true, false, Map.of());
String queue = channel.queueDeclare(queueName, true, false, false, Map.of()).getQueue();
String queue = channel.queueDeclare(queueName, true, false, false, Map.of())
.getQueue();
logger.log(Level.INFO, "Test queue: {0}", queue);
// channel.queueBind(queue, messageExchangeName, messageRoutingKey);
channel.queueBind(queue, messageExchangeName, messageRoutingKey);
// channel.exchangeDeclare(messageExchangeName, TOPIC, true);
// String queueName = channel.queueDeclare().getQueue();
// channel.queueBind(queueName, messageExchangeName, messageRoutingKey);

AtomicReference<MsgBean> receivedMsg = new AtomicReference<>(null);

String consumerTag = channel.basicConsume(queueName, false, "myConsumerTag",
new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body)
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body)
throws IOException {
long deliveryTag = envelope.getDeliveryTag();
MsgBean msgbean = MsgUtils.splitBody(new String(body, UTF_8),
Expand All @@ -78,19 +70,15 @@ public void handleDelivery(String consumerTag,

MsgBean msgbean = new MsgBean(CreateId.createid(), message, "Publish");
String body = MsgUtils.createBody(msgbean, splitkey);

AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
.contentType("text/plain")
.build();
.contentType("text/plain").build();
channel.basicPublish(convertExchangeName, converterRoutingKey, props,
body.getBytes(UTF_8));
channel.basicPublish(convertExchangeName, converterRoutingKey, props,
body.getBytes(UTF_8));
channel.basicPublish(convertExchangeName, converterRoutingKey, props,
body.getBytes(UTF_8));

await().atMost(10, SECONDS).untilAtomic(receivedMsg, notNullValue());

channel.basicCancel(consumerTag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public static void stopReceived() {
public MsgBean putMsg() throws NoSuchAlgorithmException {
MsgBean msgbean = new MsgBean(CreateId.createid(), message, "Put");
String body = MsgUtils.createBody(msgbean, splitkey);
try (JMSContext context = connectionFactory
.createContext(Session.AUTO_ACKNOWLEDGE)) {
try (JMSContext context = createContext()) {
context.createProducer().send(context.createQueue(queuename),
context.createTextMessage(body));
}
Expand All @@ -80,10 +79,7 @@ public MsgBean putMsg() throws NoSuchAlgorithmException {
@Override
public MsgBean getMsg() throws RuntimeException {
MsgBean msgbean = new MsgBean(0, "No Data.", "Get");
try (JMSContext context = connectionFactory
.createContext(Session.AUTO_ACKNOWLEDGE);
JMSConsumer consumer = context
.createConsumer(context.createQueue(queuename))) {
try (JMSConsumer consumer = createConsumerQueue(createContext())) {
String resp = consumer.receiveBody(String.class);

if (!Objects.isNull(resp)) {
Expand All @@ -99,27 +95,35 @@ public MsgBean getMsg() throws RuntimeException {
public MsgBean publishMsg() throws NoSuchAlgorithmException {
MsgBean msgbean = new MsgBean(CreateId.createid(), message, "Publish");
String body = MsgUtils.createBody(msgbean, splitkey);
try (JMSContext context = connectionFactory
.createContext(Session.AUTO_ACKNOWLEDGE)) {
try (JMSContext context = createContext()) {
context.createProducer().send(context.createTopic(topicname),
context.createTextMessage(body));
}
}
logger.log(Level.INFO, msgbean.getFullmsg());
return msgbean;
}

@Override
public void run() {
while (isEnableReceived) {
try (JMSContext context = connectionFactory
.createContext(Session.AUTO_ACKNOWLEDGE);
JMSConsumer consumer = context
.createConsumer(context.createTopic(topicname))) {
try (JMSConsumer consumer = createConsumerTopic(createContext())) {
amqconsumer.consume(consumer);
} catch (Exception e) {
logger.log(Level.SEVERE, "Subscribe Error.", e);
stopReceived();
}
}
}

private JMSContext createContext() {
return connectionFactory.createContext(Session.AUTO_ACKNOWLEDGE);
}

private JMSConsumer createConsumerTopic(JMSContext context) {
return context.createConsumer(context.createTopic(topicname));
}

private JMSConsumer createConsumerQueue(JMSContext context) {
return context.createConsumer(context.createQueue(queuename));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Msg {
private int id;
private String msg;

// Constructor
public Msg() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.hazelcast.core.HazelcastInstance;

@ApplicationScoped
public class HazelcastService {
public final class HazelcastService {

private static String address = ConfigProvider.getConfig().getValue(
"hazelcast.address",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package webapp.tier.service;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.when;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.security.NoSuchAlgorithmException;
import java.util.LinkedHashMap;
Expand All @@ -29,7 +28,7 @@
class MongodbServiceTest {

@Inject
private MongodbService mongosvc;
MongodbService mongosvc;

private String respbody = "message: Hello k8s-3tier-webapp with quarkus";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ public class MysqlService implements Database {
@ConfigProperty(name = "mysql.delete.msg")
String deletesql;

@ConfigProperty(name = "mysql.id")
String sqlkey;

@ConfigProperty(name = "mysql.body")
String sqlbody;

private final Logger logger = Logger.getLogger(this.getClass().getSimpleName());

public boolean connectionStatus() {
Expand All @@ -64,8 +58,8 @@ public boolean connectionStatus() {
@Override
public MsgBean insertMsg() throws SQLException, NoSuchAlgorithmException {
MsgBean msgbean = new MsgBean(CreateId.createid(), message, "Insert");
String sql = insertsql.replace(sqlkey, MsgUtils.intToString(msgbean.getId()))
.replace(sqlbody, msgbean.getMessage());
String sql = insertsql.replace("msgid", MsgUtils.intToString(msgbean.getId()))
.replace("msgbody", msgbean.getMessage());

try (Connection con = ds.getConnection();
Statement stmt = con.createStatement()) {
Expand All @@ -89,7 +83,7 @@ public List<MsgBean> selectMsg() throws SQLException {
ResultSet rs = stmt.executeQuery(selectsql)) {
logger.log(Level.INFO, "Select SQL: {0}", selectsql);
while (rs.next()) {
MsgBean msgbean = new MsgBean(MsgUtils.stringToInt(rs.getString("id")),
MsgBean msgbean = new MsgBean(rs.getString("id"),
rs.getString("msg"), "Select");
logger.log(Level.INFO, msgbean.getFullmsg());
msglist.add(msgbean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ quarkus.hibernate-orm.dialect=org.hibernate.dialect.MySQLDialect
mysql.insert.msg=INSERT INTO msg (id, msg) VALUES (msgid, 'msgbody')
mysql.select.msg=SELECT * FROM msg
mysql.delete.msg=DELETE FROM msg
mysql.id=msgid
mysql.body=msgbody
mysql.msg.quarkus=throuth quarkus

# cache
quarkus.cache.caffeine."mysql_select_msg".expire-after-write=1S
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public List<MsgBean> selectMsg() throws SQLException {
ResultSet rs = stmt.executeQuery(selectsql)) {
logger.log(Level.INFO, "Select SQL: {0}", selectsql);
while (rs.next()) {
MsgBean msgbean = new MsgBean(MsgUtils.stringToInt(rs.getString("id")),
MsgBean msgbean = new MsgBean(rs.getString("id"),
rs.getString("msg"), "Select");
logger.log(Level.INFO, msgbean.getFullmsg());
msglist.add(msgbean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ public class RedisService implements Consumer<RedisNotification> {

private final Logger logger = Logger.getLogger(this.getClass().getSimpleName());

@ConfigProperty(name = "redis.split.key")
String splitkey;

private static String channel = ConfigProvider.getConfig().getValue("redis.channel",
String.class);

@ConfigProperty(name = "redis.split.key")
String splitkey;
@ConfigProperty(name = "common.message")
String message;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package webapp.tier.service;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;

import javax.inject.Inject;

Expand All @@ -16,7 +15,7 @@
class RandomServiceTest {

@Inject
private RandomService svc;
RandomService svc;

@Test
void testDeliverrandomCase0Error() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package webapp.tier.schedule;

import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;

import javax.inject.Inject;

Expand All @@ -12,7 +12,7 @@
class CallRandomPublshScheduleTest {

@Inject
private CallRandomPublshSchedule rps;
CallRandomPublshSchedule rps;

@Test
void testCallRandomPublsh() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class MsgBean {
String message;
String fullmsg = "MsgBean init error";

// Constructor
public MsgBean() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import org.eclipse.microprofile.health.HealthCheckResponse;

public class HealthCheckUtils {
public final class HealthCheckUtils {

private static final Logger logger = Logger
.getLogger(HealthCheckUtils.class.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import webapp.tier.bean.MsgBean;

public class MsgUtils {
public final class MsgUtils {

private MsgUtils() {
}
Expand Down

0 comments on commit 0e01648

Please sign in to comment.