|
| 1 | +package io.logz.sawmill.processors; |
| 2 | + |
| 3 | +import io.logz.sawmill.Doc; |
| 4 | +import io.logz.sawmill.ProcessResult; |
| 5 | +import io.logz.sawmill.Processor; |
| 6 | +import io.logz.sawmill.exceptions.ProcessorConfigurationException; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.util.Base64; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import static io.logz.sawmill.utils.FactoryUtils.createProcessor; |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 17 | + |
| 18 | +public class Base64DecodeProcessorTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testNullOrEmptyConfigurationFieldsShouldFailCreator() { |
| 22 | + Stream.of( |
| 23 | + createConfig(null, "target"), |
| 24 | + createConfig("", "target"), |
| 25 | + createConfig("source", null), |
| 26 | + createConfig("source", "")) |
| 27 | + .forEach((config) -> assertThatThrownBy(() -> createProcessor(Base64DecodeProcessor.class, config)) |
| 28 | + .isInstanceOf(ProcessorConfigurationException.class) |
| 29 | + .hasMessageContaining("sourceField, targetField can not be null or empty")); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testDecode() { |
| 34 | + Map<String, Object> config = new HashMap<>(); |
| 35 | + config.put("sourceField", "message"); |
| 36 | + config.put("targetField", "message_decoded"); |
| 37 | + |
| 38 | + Map<String, Object> map = new HashMap<>(); |
| 39 | + String encodedMessage = Base64.getEncoder().encodeToString("testEmptyFieldsShouldFail".getBytes()); |
| 40 | + map.put("message", encodedMessage); |
| 41 | + Doc doc = new Doc(map); |
| 42 | + |
| 43 | + ProcessResult result; |
| 44 | + Processor processor = createProcessor(Base64DecodeProcessor.class, config); |
| 45 | + try { |
| 46 | + result = processor.process(doc); |
| 47 | + } catch (InterruptedException e) { throw new RuntimeException(e); } |
| 48 | + |
| 49 | + assertThat(result != null && result.isSucceeded()).isTrue(); |
| 50 | + assertThat(doc.getField(config.get("sourceField").toString()).toString()).isEqualTo(encodedMessage); |
| 51 | + assertThat(doc.getField(config.get("targetField").toString()).toString()) |
| 52 | + .isEqualTo("testEmptyFieldsShouldFail"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testNonStringFieldShouldFail() { |
| 57 | + Map<String, Object> config = new HashMap<>(); |
| 58 | + config.put("sourceField", "numberField"); |
| 59 | + config.put("targetField", "noop"); |
| 60 | + |
| 61 | + Map<String, Object> map = new HashMap<>(); |
| 62 | + map.put("message", "testSingleNonStringFieldShouldFail"); |
| 63 | + map.put("numberField", 123); |
| 64 | + Doc doc = new Doc(map); |
| 65 | + |
| 66 | + Processor processor = createProcessor(Base64DecodeProcessor.class, config); |
| 67 | + |
| 68 | + ProcessResult result; |
| 69 | + |
| 70 | + try { |
| 71 | + result = processor.process(doc); |
| 72 | + } catch (InterruptedException e) { throw new RuntimeException(e); } |
| 73 | + assertThat(result != null && !result.isSucceeded()).isTrue(); |
| 74 | + assertThat(result.getError().isPresent()).isTrue(); |
| 75 | + assertThat(result.getError().get().getMessage()) |
| 76 | + .isEqualTo("field is missing from doc"); |
| 77 | + assertThat(doc.hasField(config.get("targetField").toString())).isFalse(); |
| 78 | + assertThat(doc.getField("message").toString()).isEqualTo(map.get("message").toString()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testMissingFieldShouldFail() { |
| 83 | + Map<String, Object> config = new HashMap<>(); |
| 84 | + config.put("sourceField", "foo"); |
| 85 | + config.put("targetField", "foo_decoded"); |
| 86 | + |
| 87 | + Map<String, Object> map = new HashMap<>(); |
| 88 | + map.put("message", "testAllFieldsMissingShouldFail"); |
| 89 | + |
| 90 | + Doc doc = new Doc(map); |
| 91 | + |
| 92 | + ProcessResult result; |
| 93 | + Processor processor = createProcessor(Base64DecodeProcessor.class, config); |
| 94 | + try { |
| 95 | + result = processor.process(doc); |
| 96 | + } catch (InterruptedException e) { throw new RuntimeException(e); } |
| 97 | + |
| 98 | + assertThat(result != null && !result.isSucceeded()).isTrue(); |
| 99 | + assertThat(result.getError().get().getMessage()) |
| 100 | + .isEqualTo("field is missing from doc"); |
| 101 | + assertThat(doc.hasField(config.get("targetField").toString())).isFalse(); |
| 102 | + assertThat(doc.getField("message").toString()).isEqualTo(map.get("message").toString()); |
| 103 | + } |
| 104 | + |
| 105 | + private Map<String, Object> createConfig(String sourceField, String targetField) { |
| 106 | + Map<String, Object> config = new HashMap<>(); |
| 107 | + config.put("sourceField", sourceField); |
| 108 | + config.put("targetField", targetField); |
| 109 | + return config; |
| 110 | + } |
| 111 | +} |
0 commit comments