Skip to content

Commit

Permalink
Add Deserializer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rfoltyns committed Oct 5, 2022
1 parent 832c9a9 commit b036ef0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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> {

T read(InputStream inputStream) throws IOException;

}
Original file line number Diff line number Diff line change
@@ -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<T> implements Deserializer<T> {

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);
}

}
Original file line number Diff line number Diff line change
@@ -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<JacksonSerializerTest.Dummy> 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<JacksonSerializerTest.Dummy> 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());

}

}

0 comments on commit b036ef0

Please sign in to comment.