From b39602f15d5cc4d0eb295f86fc521db44f57274b Mon Sep 17 00:00:00 2001 From: YuLuo Date: Mon, 12 Aug 2024 23:28:22 +0800 Subject: [PATCH] [improve] add MetricsDataRedisCodec unit test (#2514) Signed-off-by: yuluo-yx Co-authored-by: tomsun28 --- .../store/MetricsDataRedisCodecTest.java | 64 +++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/warehouse/src/test/java/org/apache/hertzbeat/warehouse/store/MetricsDataRedisCodecTest.java b/warehouse/src/test/java/org/apache/hertzbeat/warehouse/store/MetricsDataRedisCodecTest.java index b42fe9dff68..a71e98f6fdd 100644 --- a/warehouse/src/test/java/org/apache/hertzbeat/warehouse/store/MetricsDataRedisCodecTest.java +++ b/warehouse/src/test/java/org/apache/hertzbeat/warehouse/store/MetricsDataRedisCodecTest.java @@ -17,32 +17,88 @@ package org.apache.hertzbeat.warehouse.store; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.common.entity.message.CollectRep; import org.apache.hertzbeat.warehouse.store.realtime.redis.MetricsDataRedisCodec; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; /** * Test case for {@link MetricsDataRedisCodec} */ + +@Slf4j class MetricsDataRedisCodecTest { + private MetricsDataRedisCodec codec; + @BeforeEach void setUp() { + + codec = new MetricsDataRedisCodec(); } @Test - void decodeKey() { + void testEncodeKey() { + + String key = "testKey"; + ByteBuffer encodedKey = codec.encodeKey(key); + String decodedKey = StandardCharsets.UTF_8.decode(encodedKey).toString(); + + assertEquals(key, decodedKey); } @Test - void decodeValue() { + void testDecodeKey() { + + String key = "testKey"; + ByteBuffer buffer = ByteBuffer.wrap(key.getBytes(StandardCharsets.UTF_8)); + String decodedKey = codec.decodeKey(buffer); + + assertEquals(key, decodedKey); } @Test - void encodeKey() { + void testEncodeValue() { + + CollectRep.MetricsData metricsData = Mockito.mock(CollectRep.MetricsData.class); + byte[] bytes = new byte[] {1, 2, 3}; + Mockito.when(metricsData.toByteArray()).thenReturn(bytes); + + ByteBuffer encodedValue = codec.encodeValue(metricsData); + assertArrayEquals(bytes, encodedValue.array()); } @Test - void encodeValue() { + void testDecodeValue() { + + CollectRep.MetricsData metricsData = Mockito.mock(CollectRep.MetricsData.class); + byte[] bytes = new byte[] {1, 2, 3}; + + ByteBuffer buffer = ByteBuffer.wrap(bytes); + + try { + Mockito.mockStatic(CollectRep.MetricsData.class); + Mockito.when(CollectRep.MetricsData.parseFrom(buffer)).thenReturn(metricsData); + + CollectRep.MetricsData decodedValue = codec.decodeValue(buffer); + + assertEquals(metricsData, decodedValue); + } + catch (Exception e) { + log.error(e.getMessage()); + fail("Exception thrown during decodeValue test"); + } + finally { + Mockito.clearAllCaches(); + } } + }