Skip to content

Commit

Permalink
[improve] add MetricsDataRedisCodec unit test (apache#2514)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
yuluo-yx and tomsun28 authored Aug 12, 2024
1 parent 86c7c2d commit b39602f
Showing 1 changed file with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}

0 comments on commit b39602f

Please sign in to comment.