-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
log4j2-elasticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/Deserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...sticsearch-core/src/main/java/org/appenders/log4j2/elasticsearch/JacksonDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...search-core/src/test/java/org/appenders/log4j2/elasticsearch/JacksonDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
} |