From 46fa6bde7d028aa970454fd2e952b53ab42d8d7c Mon Sep 17 00:00:00 2001 From: pandaapo <35672972+pandaapo@users.noreply.github.com> Date: Mon, 22 May 2023 10:56:18 +0800 Subject: [PATCH] [ISSUE #3946]Add unit test method or class for classes: NetUtils, TopicResponse (#3947) * Add unit test methods for NetUtils, JsonUtils * Revoke redundant test method. Add test class. --- .../rocketmq/response/TopicResponseTest.java | 56 +++++++++++++++++++ .../eventmesh/common/utils/NetUtilsTest.java | 9 +++ 2 files changed, 65 insertions(+) create mode 100644 eventmesh-admin/eventmesh-admin-rocketmq/src/test/java/org/apache/eventmesh/admin/rocketmq/response/TopicResponseTest.java diff --git a/eventmesh-admin/eventmesh-admin-rocketmq/src/test/java/org/apache/eventmesh/admin/rocketmq/response/TopicResponseTest.java b/eventmesh-admin/eventmesh-admin-rocketmq/src/test/java/org/apache/eventmesh/admin/rocketmq/response/TopicResponseTest.java new file mode 100644 index 0000000000..311dc6efcf --- /dev/null +++ b/eventmesh-admin/eventmesh-admin-rocketmq/src/test/java/org/apache/eventmesh/admin/rocketmq/response/TopicResponseTest.java @@ -0,0 +1,56 @@ +/* + * 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 org.apache.eventmesh.admin.rocketmq.response; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class TopicResponseTest { + + @Test + public void testTopicResponse() { + String topic = "testtopic"; + String createdTime = "2023-05-17 10:30:00"; + TopicResponse topicResponse = new TopicResponse(topic, createdTime); + + assertEquals(topic, topicResponse.getTopic()); + assertEquals(createdTime, topicResponse.getCreatedTime()); + } + + @Test + public void testTopicResponseSerialization() throws Exception { + String topic = "testtopic"; + String createdTime = "2023-05-17 10:30:00"; + TopicResponse topicResponse = new TopicResponse(topic, createdTime); + + ObjectMapper objectMapper = new ObjectMapper(); + String json = objectMapper.writeValueAsString(topicResponse); + + assertTrue(json.contains("topic")); + assertTrue(json.contains("created_time")); + + TopicResponse deserializedResponse = objectMapper.readValue(json, TopicResponse.class); + + assertEquals(topic, deserializedResponse.getTopic()); + assertEquals(createdTime, deserializedResponse.getCreatedTime()); + } +} diff --git a/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/NetUtilsTest.java b/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/NetUtilsTest.java index ca62207a59..34d07a840e 100644 --- a/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/NetUtilsTest.java +++ b/eventmesh-common/src/test/java/org/apache/eventmesh/common/utils/NetUtilsTest.java @@ -20,6 +20,7 @@ import org.apache.eventmesh.common.enums.HttpMethod; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -70,4 +71,12 @@ public void testParsePostBody() throws Exception { Assert.assertEquals(expected, actual); } + + @Test + public void testSendSuccessResponseHeaders() throws IOException { + HttpExchange exchange = Mockito.mock(HttpExchange.class); + NetUtils.sendSuccessResponseHeaders(exchange); + Mockito.verify(exchange, Mockito.times(1)) + .sendResponseHeaders(Mockito.anyInt(), Mockito.anyLong()); + } }