Improve Test coverage of InfluxLogger
[nonrtric/plt/ranpm.git] / influxlogger / src / test / java / org / oran / pmlog / DataFromKafkaTopicTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2023 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oran.pmlog;
22
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;
30
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;
39
40 class DataFromKafkaTopicTest {
41
42     private DataFromKafkaTopic data;
43     private List<Header> headers;
44
45     @BeforeEach
46     void setUp() {
47         headers = new ArrayList<>();
48         data = new DataFromKafkaTopic(headers, null, null);
49     }
50
51     @Test
52     void testConstructor_NullKeyAndValue() {
53         assertNotNull(data);
54         assertArrayEquals(new byte[0], data.getKey());
55         assertArrayEquals(new byte[0], data.getValue());
56         assertEquals(headers, data.headers);
57     }
58
59     @Test
60     void testValueAsString_Unzipped() {
61         data.valueAsString();
62         assertEquals("", data.valueAsString());
63         assertFalse(data.isZipped()); // Not zipped
64
65         // Ensure that calling valueAsString again does not recompute the value
66         data.valueAsString();
67         assertEquals("", data.valueAsString());
68     }
69
70     @Test
71     void testValueAsString_Zipped() {
72         headers.add(new Header() {
73             @Override
74             public String key() {
75                 return DataFromKafkaTopic.ZIPPED_PROPERTY;
76             }
77
78             @Override
79             public byte[] value() {
80                 return new byte[]{1};
81             }
82         });
83
84         // Mock GZIPInputStream behavior
85         ByteArrayInputStream inputStream = Mockito.mock(ByteArrayInputStream.class);
86         when(inputStream.readAllBytes()).thenReturn("ZippedValue".getBytes());
87
88         // Mock the unzip method
89         DataFromKafkaTopic spyData = spy(data);
90
91         // Call valueAsString to trigger unzipping
92         String result = spyData.valueAsString();
93
94         // Ensure that the value is correctly unzipped
95         assertEquals("", result);
96         assertEquals("", spyData.getStringValue());
97         assertTrue(spyData.isZipped());
98     }
99
100     @Test
101     void testUnzip_Exception() throws IOException {
102         byte[] zippedBytes = "ZippedValue".getBytes();
103
104         // Mock GZIPInputStream to throw an exception
105         ByteArrayInputStream inputStream = Mockito.mock(ByteArrayInputStream.class);
106         when(inputStream.readAllBytes()).thenThrow(new IOException("Mocked exception"));
107
108         // Mock the unzip method
109         DataFromKafkaTopic spyData = spy(data);
110
111         // Call unzip method
112         String result = spyData.valueAsString();
113
114         // Ensure that an empty string is returned and the error is logged
115         assertEquals("", result);
116     }
117
118     @Test
119     void testIsZipped_True() {
120         headers.add(new Header() {
121             @Override
122             public String key() {
123                 return DataFromKafkaTopic.ZIPPED_PROPERTY;
124             }
125
126             @Override
127             public byte[] value() {
128                 return new byte[0];
129             }
130         });
131
132         assertTrue(data.isZipped());
133     }
134
135     @Test
136     void testIsZipped_False() {
137         assertFalse(data.isZipped());
138     }
139
140     @Test
141     void testGetTypeIdFromHeaders() {
142         headers.add(new Header() {
143             @Override
144             public String key() {
145                 return DataFromKafkaTopic.TYPE_ID_PROPERTY;
146             }
147
148             @Override
149             public byte[] value() {
150                 return "Type123".getBytes();
151             }
152         });
153
154         assertEquals("Type123", data.getTypeIdFromHeaders());
155     }
156
157     @Test
158     void testGetTypeIdFromHeaders_Null() {
159         assertEquals("", data.getTypeIdFromHeaders());
160     }
161 }
162