Improve Test coverage of InfluxLogger
[nonrtric/plt/ranpm.git] / influxlogger / src / main / java / org / oran / pmlog / DataFromKafkaTopic.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 java.io.ByteArrayInputStream;
24 import java.util.zip.GZIPInputStream;
25
26 import lombok.Getter;
27 import lombok.ToString;
28
29 import org.apache.kafka.common.header.Header;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @ToString
34 public class DataFromKafkaTopic {
35     @Getter
36     private final byte[] key;
37     @Getter
38     private final byte[] value;
39     @Getter
40     private String stringValue = null;
41     private static final Logger logger = LoggerFactory.getLogger(DataFromKafkaTopic.class);
42
43     public final Iterable<Header> headers;
44
45     private static byte[] noBytes = new byte[0];
46
47     public DataFromKafkaTopic(Iterable<Header> headers, byte[] key, byte[] value) {
48         this.key = key == null ? noBytes : key;
49         this.value = value == null ? noBytes : value;
50         this.headers = headers;
51     }
52
53     public String valueAsString() {
54         if (stringValue == null) {
55             if (isZipped()) {
56                 stringValue = unzip(this.value);
57             } else {
58                 stringValue = new String(this.value);
59             }
60
61         }
62         return this.stringValue;
63     }
64
65     private String unzip(byte[] bytes) {
66         try (final GZIPInputStream gzipInput = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
67             return new String(gzipInput.readAllBytes());
68         } catch (Exception e) {
69             logger.error("Could not unzip received info, reason: {}, typeId: {}", e.getMessage(),
70                     getTypeIdFromHeaders());
71             return "";
72         }
73     }
74
75     public static final String ZIPPED_PROPERTY = "gzip";
76     public static final String TYPE_ID_PROPERTY = "type-id";
77
78     public boolean isZipped() {
79         if (headers == null) {
80             return false;
81         }
82         for (Header h : headers) {
83             if (h.key().equals(ZIPPED_PROPERTY)) {
84                 return true;
85             }
86         }
87         return false;
88     }
89
90     public String getTypeIdFromHeaders() {
91         if (headers == null) {
92             return "";
93         }
94         for (Header h : headers) {
95             if (h.key().equals(TYPE_ID_PROPERTY)) {
96                 return new String(h.value());
97             }
98         }
99         return "";
100     }
101 }