From b036ef0f6d6dd0d01c8b2ae3735fa0cbd3136341 Mon Sep 17 00:00:00 2001 From: rfoltyns Date: Sun, 25 Sep 2022 23:24:19 +0100 Subject: [PATCH] Add Deserializer interface --- .../log4j2/elasticsearch/Deserializer.java | 30 ++++++++++ .../elasticsearch/JacksonDeserializer.java | 41 ++++++++++++++ .../JacksonDeserializerTest.java | 55 +++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/Deserializer.java create mode 100644 log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/JacksonDeserializer.java create mode 100644 log4j2-elasticsearch-core/src/test/java/org/appenders/log4j2/elasticsearch/JacksonDeserializerTest.java diff --git a/log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/Deserializer.java b/log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/Deserializer.java new file mode 100644 index 00000000..4b12a2c7 --- /dev/null +++ b/log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/Deserializer.java @@ -0,0 +1,30 @@ +package org.appenders.log4j2.elasticsearch; + +/*- + * #%L + * log4j2-elasticsearch + * %% + * Copyright (C) 2022 Rafal Foltynski + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import java.io.IOException; +import java.io.InputStream; + +public interface Deserializer { + + T read(InputStream inputStream) throws IOException; + +} diff --git a/log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/JacksonDeserializer.java b/log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/JacksonDeserializer.java new file mode 100644 index 00000000..2de0ff1f --- /dev/null +++ b/log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/JacksonDeserializer.java @@ -0,0 +1,41 @@ +package org.appenders.log4j2.elasticsearch; + +/*- + * #%L + * log4j2-elasticsearch + * %% + * Copyright (C) 2022 Rafal Foltynski + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.fasterxml.jackson.databind.ObjectReader; + +import java.io.IOException; +import java.io.InputStream; + +public class JacksonDeserializer implements Deserializer { + + private final ObjectReader objectReader; + + public JacksonDeserializer(final ObjectReader objectReader) { + this.objectReader = objectReader; + } + + @Override + public T read(final InputStream inputStream) throws IOException { + return objectReader.readValue(inputStream); + } + +} diff --git a/log4j2-elasticsearch-core/src/test/java/org/appenders/log4j2/elasticsearch/JacksonDeserializerTest.java b/log4j2-elasticsearch-core/src/test/java/org/appenders/log4j2/elasticsearch/JacksonDeserializerTest.java new file mode 100644 index 00000000..1256fd04 --- /dev/null +++ b/log4j2-elasticsearch-core/src/test/java/org/appenders/log4j2/elasticsearch/JacksonDeserializerTest.java @@ -0,0 +1,55 @@ +package org.appenders.log4j2.elasticsearch; + +/*- + * #%L + * log4j2-elasticsearch + * %% + * Copyright (C) 2022 Rafal Foltynski + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class JacksonDeserializerTest { + + @Test + public void readsInputStream() throws Exception { + + // given + final Deserializer deserializer = new JacksonDeserializer<>( + new ObjectMapper().readerFor(JacksonSerializerTest.Dummy.class)); + + final String expectedValue = UUID.randomUUID().toString(); + final JacksonSerializerTest.Dummy dummy = new JacksonSerializerTest.Dummy(expectedValue); + final Serializer serializer = new JacksonSerializer<>(new ObjectMapper().writerFor(JacksonSerializerTest.Dummy.class)); + final byte[] bytes = serializer.writeAsBytes(dummy); + final InputStream inputStream = new ByteArrayInputStream(bytes); + + // when + final JacksonSerializerTest.Dummy result = deserializer.read(inputStream); + + // then + assertEquals(expectedValue, result.getExpectedContent()); + + } + +}