|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + */ |
| 9 | + |
| 10 | +package org.opensearch.dataprepper.plugins.processor.mutateevent; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.opensearch.dataprepper.model.event.Event; |
| 14 | +import org.opensearch.dataprepper.model.event.JacksonEvent; |
| 15 | +import org.opensearch.dataprepper.model.processor.Processor; |
| 16 | +import org.opensearch.dataprepper.model.record.Record; |
| 17 | +import org.opensearch.dataprepper.test.plugins.DataPrepperPluginTest; |
| 18 | +import org.opensearch.dataprepper.test.plugins.PluginConfigurationFile; |
| 19 | +import org.opensearch.dataprepper.test.plugins.junit.BaseDataPrepperPluginStandardTestSuite; |
| 20 | + |
| 21 | +import java.util.*; |
| 22 | + |
| 23 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 24 | +import static org.hamcrest.CoreMatchers.is; |
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | + |
| 27 | +@DataPrepperPluginTest(pluginName = "add_entries", pluginType = Processor.class) |
| 28 | +class AddEntryProcessorIT extends BaseDataPrepperPluginStandardTestSuite { |
| 29 | + |
| 30 | + @Test |
| 31 | + void disable_root_keys_resolves_value_expression_from_root( |
| 32 | + @PluginConfigurationFile("iterate_on_with_disable_root_keys.yaml") final Processor<Record<Event>, Record<Event>> objectUnderTest) { |
| 33 | + final Map<String, Object> data = new HashMap<>(); |
| 34 | + data.put("alert_title", "SQL Injection Detected"); |
| 35 | + data.put("vulns", Arrays.asList( |
| 36 | + new HashMap<>(Map.of("cve", "CVE-2024-001")), |
| 37 | + new HashMap<>(Map.of("cve", "CVE-2024-002")))); |
| 38 | + |
| 39 | + final Record<Event> record = buildRecordWithEvent(data); |
| 40 | + final List<Record<Event>> result = (List<Record<Event>>) objectUnderTest.execute(Collections.singletonList(record)); |
| 41 | + |
| 42 | + List<Map<String, Object>> vulns = result.get(0).getData().get("vulns", List.class); |
| 43 | + assertThat(vulns.size(), equalTo(2)); |
| 44 | + assertThat(vulns.get(0).get("title"), equalTo("SQL Injection Detected")); |
| 45 | + assertThat(vulns.get(1).get("title"), equalTo("SQL Injection Detected")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void evaluate_when_on_element_filters_per_element( |
| 50 | + @PluginConfigurationFile("iterate_on_with_evaluate_when_on_element.yaml") final Processor<Record<Event>, Record<Event>> objectUnderTest) { |
| 51 | + final Map<String, Object> data = new HashMap<>(); |
| 52 | + data.put("vulns", Arrays.asList( |
| 53 | + new HashMap<>(Map.of("cve", "CVE-2024-001", "severity", "critical")), |
| 54 | + new HashMap<>(Map.of("cve", "CVE-2024-002", "severity", "low")), |
| 55 | + new HashMap<>(Map.of("cve", "CVE-2024-003", "severity", "critical")))); |
| 56 | + |
| 57 | + final Record<Event> record = buildRecordWithEvent(data); |
| 58 | + final List<Record<Event>> result = (List<Record<Event>>) objectUnderTest.execute(Collections.singletonList(record)); |
| 59 | + |
| 60 | + List<Map<String, Object>> vulns = result.get(0).getData().get("vulns", List.class); |
| 61 | + assertThat(vulns.size(), equalTo(3)); |
| 62 | + assertThat(vulns.get(0).get("flagged"), equalTo(true)); |
| 63 | + assertThat(vulns.get(1).containsKey("flagged"), is(false)); |
| 64 | + assertThat(vulns.get(2).get("flagged"), equalTo(true)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void both_flags_combined_resolves_root_value_with_per_element_condition( |
| 69 | + @PluginConfigurationFile("iterate_on_with_both_flags.yaml") final Processor<Record<Event>, Record<Event>> objectUnderTest) { |
| 70 | + final Map<String, Object> data = new HashMap<>(); |
| 71 | + data.put("alert_title", "SQL Injection Detected"); |
| 72 | + data.put("vulns", Arrays.asList( |
| 73 | + new HashMap<>(Map.of("cve", "CVE-2024-001", "severity", "critical")), |
| 74 | + new HashMap<>(Map.of("cve", "CVE-2024-002", "severity", "low")), |
| 75 | + new HashMap<>(Map.of("cve", "CVE-2024-003", "severity", "critical")))); |
| 76 | + |
| 77 | + final Record<Event> record = buildRecordWithEvent(data); |
| 78 | + final List<Record<Event>> result = (List<Record<Event>>) objectUnderTest.execute(Collections.singletonList(record)); |
| 79 | + |
| 80 | + List<Map<String, Object>> vulns = result.get(0).getData().get("vulns", List.class); |
| 81 | + assertThat(vulns.size(), equalTo(3)); |
| 82 | + assertThat(vulns.get(0).get("title"), equalTo("SQL Injection Detected")); |
| 83 | + assertThat(vulns.get(1).containsKey("title"), is(false)); |
| 84 | + assertThat(vulns.get(2).get("title"), equalTo("SQL Injection Detected")); |
| 85 | + } |
| 86 | + |
| 87 | + private static Record<Event> buildRecordWithEvent(final Map<String, Object> data) { |
| 88 | + return new Record<>(JacksonEvent.builder() |
| 89 | + .withData(data) |
| 90 | + .withEventType("event") |
| 91 | + .build()); |
| 92 | + } |
| 93 | +} |
0 commit comments