2 * ========================LICENSE_START=================================
5 * Copyright (C) 2023 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oran.pmlog;
23 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.when;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.List;
35 import org.apache.kafka.common.header.Header;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.Mockito;
40 class DataFromKafkaTopicTest {
42 private DataFromKafkaTopic data;
43 private List<Header> headers;
47 headers = new ArrayList<>();
48 data = new DataFromKafkaTopic(headers, null, null);
52 void testConstructor_NullKeyAndValue() {
54 assertArrayEquals(new byte[0], data.getKey());
55 assertArrayEquals(new byte[0], data.getValue());
56 assertEquals(headers, data.headers);
60 void testValueAsString_Unzipped() {
62 assertEquals("", data.valueAsString());
63 assertFalse(data.isZipped()); // Not zipped
65 // Ensure that calling valueAsString again does not recompute the value
67 assertEquals("", data.valueAsString());
71 void testValueAsString_Zipped() {
72 headers.add(new Header() {
75 return DataFromKafkaTopic.ZIPPED_PROPERTY;
79 public byte[] value() {
84 // Mock GZIPInputStream behavior
85 ByteArrayInputStream inputStream = Mockito.mock(ByteArrayInputStream.class);
86 when(inputStream.readAllBytes()).thenReturn("ZippedValue".getBytes());
88 // Mock the unzip method
89 DataFromKafkaTopic spyData = spy(data);
91 // Call valueAsString to trigger unzipping
92 String result = spyData.valueAsString();
94 // Ensure that the value is correctly unzipped
95 assertEquals("", result);
96 assertEquals("", spyData.getStringValue());
97 assertTrue(spyData.isZipped());
101 void testUnzip_Exception() throws IOException {
102 byte[] zippedBytes = "ZippedValue".getBytes();
104 // Mock GZIPInputStream to throw an exception
105 ByteArrayInputStream inputStream = Mockito.mock(ByteArrayInputStream.class);
106 when(inputStream.readAllBytes()).thenThrow(new IOException("Mocked exception"));
108 // Mock the unzip method
109 DataFromKafkaTopic spyData = spy(data);
112 String result = spyData.valueAsString();
114 // Ensure that an empty string is returned and the error is logged
115 assertEquals("", result);
119 void testIsZipped_True() {
120 headers.add(new Header() {
122 public String key() {
123 return DataFromKafkaTopic.ZIPPED_PROPERTY;
127 public byte[] value() {
132 assertTrue(data.isZipped());
136 void testIsZipped_False() {
137 assertFalse(data.isZipped());
141 void testGetTypeIdFromHeaders() {
142 headers.add(new Header() {
144 public String key() {
145 return DataFromKafkaTopic.TYPE_ID_PROPERTY;
149 public byte[] value() {
150 return "Type123".getBytes();
154 assertEquals("Type123", data.getTypeIdFromHeaders());
158 void testGetTypeIdFromHeaders_Null() {
159 assertEquals("", data.getTypeIdFromHeaders());