From 5e2e3a3be350a9f5baa5ce0e3dd9b651f2dc9eb9 Mon Sep 17 00:00:00 2001 From: Xin Chen <37373516+DDreame@users.noreply.github.com> Date: Sun, 28 May 2023 14:43:58 +0800 Subject: [PATCH] add UnitTest about MessageConvert (#1358) * add UnitTest about MessageConvert * add UnitTest about MessageConvert * add UnitTest about MessageConvert --- .../common/toolkit/MessageConvert.java | 4 + .../common/toolkit/MessageConvertTest.java | 110 ++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java diff --git a/infra/common/src/main/java/cn/hippo4j/common/toolkit/MessageConvert.java b/infra/common/src/main/java/cn/hippo4j/common/toolkit/MessageConvert.java index 3be85745..6a5d1fbc 100644 --- a/infra/common/src/main/java/cn/hippo4j/common/toolkit/MessageConvert.java +++ b/infra/common/src/main/java/cn/hippo4j/common/toolkit/MessageConvert.java @@ -31,6 +31,10 @@ import lombok.SneakyThrows; */ public class MessageConvert { + private MessageConvert(){ + + } + /** * {@link Message} to {@link MessageWrapper}. * diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java new file mode 100644 index 00000000..88677d9f --- /dev/null +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/MessageConvertTest.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.common.toolkit; + +import cn.hippo4j.common.model.ThreadPoolRunStateInfo; +import cn.hippo4j.common.monitor.*; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; + +/*** + * @description : Todo + * @author : DDDreame + * @date : 2023/5/27 23:24 + */ +public class MessageConvertTest { + + @Test + public void testConvert() { + AbstractMessage message = new RuntimeMessage(); + List runtimeMessages = new ArrayList<>(); + ThreadPoolRunStateInfo poolRunState = ThreadPoolRunStateInfo.builder() + .tpId("testTPid") + .activeSize(4) + .poolSize(12) + .completedTaskCount(8L) + .largestPoolSize(12) + .currentLoad("6") + .clientLastRefreshTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) + .peakLoad("20") + .queueSize(0) + .queueRemainingCapacity(512) + .rejectCount(0L) + .timestamp(System.currentTimeMillis()) + .build(); + RuntimeMessage runtimeMessage = BeanUtil.convert(poolRunState, RuntimeMessage.class); + runtimeMessage.setGroupKey("test-groupKeys"); + runtimeMessages.add(runtimeMessage); + + message.setMessageType(MessageTypeEnum.RUNTIME); + message.setMessages(runtimeMessages); + MessageWrapper messageWrapper = MessageConvert.convert(message); + Assertions.assertNotNull(messageWrapper); + } + + @Test + public void testMessageWrapperConvert() { + AbstractMessage message = new RuntimeMessage(); + List runtimeMessages = new ArrayList<>(); + ThreadPoolRunStateInfo poolRunState = ThreadPoolRunStateInfo.builder() + .tpId("testTPid") + .activeSize(4) + .poolSize(12) + .completedTaskCount(8L) + .largestPoolSize(12) + .currentLoad("6") + .clientLastRefreshTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) + .peakLoad("20") + .queueSize(0) + .queueRemainingCapacity(512) + .rejectCount(0L) + .timestamp(System.currentTimeMillis()) + .build(); + RuntimeMessage runtimeMessage = BeanUtil.convert(poolRunState, RuntimeMessage.class); + runtimeMessage.setGroupKey("test-groupKeys"); + runtimeMessages.add(runtimeMessage); + + message.setMessageType(MessageTypeEnum.RUNTIME); + message.setMessages(runtimeMessages); + MessageWrapper messageWrapper = MessageConvert.convert(message); + Message messageResult = MessageConvert.convert(messageWrapper); + Assertions.assertNotNull(messageResult); + Assertions.assertEquals(message, messageResult); + } + + @Test + public void testMessageWrapperConvertException() { + Assertions.assertThrows(Exception.class, ()->{ + Map data1 = new HashMap<>(); + data1.put("key1", "value1"); + data1.put("key2", 123); + Map data2 = new HashMap<>(); + data2.put("key3", true); + data2.put("key4", 3.14); + List> contentParams = Arrays.asList(data1, data2); + Class responseClass = String.class; + MessageTypeEnum messageType = MessageTypeEnum.DEFAULT; + MessageWrapper messageWrapper = new MessageWrapper(contentParams, responseClass, messageType); + MessageConvert.convert(messageWrapper); + }); + } +}