-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathLogstashConfigConverterIT.java
123 lines (100 loc) · 5.46 KB
/
LogstashConfigConverterIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.dataprepper.logstash;
import org.opensearch.dataprepper.model.configuration.PipelinesDataFlowModel;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasKey;
/**
* Integration tests for the Logstash Configuration Converter.
* <p>
* These tests use all the <code>.conf</code> files from the
* <code>src/test/resources/org/opensearch/dataprepper/logstash</code> directory.
* Each file will be tested by converting and then validating that it can deserialize
* into a {@link PipelinesDataFlowModel} instance. Additionally, any files with a
* matching <code>.expected.yaml</code> file will be compared against that YAML file.
* <p>
* You can add more test cases by adding both a <code>.conf</code> file and a
* <code>.expected.yaml</code> file. If the mapping is non-deterministic, you can forego
* adding the <code>.expected.yaml</code> file.
*/
public class LogstashConfigConverterIT {
private static final String OUTPUT_DIRECTORY = "build/resources/test/org/opensearch/dataprepper/logstash/";
private LogstashConfigConverter createObjectUnderTest() {
return new LogstashConfigConverter();
}
@ParameterizedTest
@ArgumentsSource(LogstashToYamlPathsProviders.class)
void convertLogstashConfigurationToPipeline_should_return_converted_file_with_the_expected_YAML(final String configurationPath, final String expectedYamlPath) throws IOException {
final String actualPath = createObjectUnderTest().convertLogstashConfigurationToPipeline(configurationPath, OUTPUT_DIRECTORY);
assertThat(actualPath, notNullValue());
final String dataPrepperConfigurationString = Files.readString(Path.of(actualPath));
final String expectedDataPrepperConfigurationString = Files.readString(Path.of(expectedYamlPath));
assertThat(dataPrepperConfigurationString, equalTo(expectedDataPrepperConfigurationString));
}
@ParameterizedTest
@ArgumentsSource(LogstashPathsProviders.class)
void convertLogstashConfigurationToPipeline_should_return_valid_PipelinesDataFlowModel_with_the_single_known_pipeline(final String configurationPath) throws IOException {
final String actualPath = createObjectUnderTest().convertLogstashConfigurationToPipeline(configurationPath, OUTPUT_DIRECTORY);
assertThat(actualPath, notNullValue());
final String dataPrepperConfigurationString = Files.readString(Path.of(actualPath));
assertThat(dataPrepperConfigurationString, notNullValue());
final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
final PipelinesDataFlowModel pipelinesDataFlowModel = objectMapper.readValue(dataPrepperConfigurationString, PipelinesDataFlowModel.class);
assertThat(pipelinesDataFlowModel, notNullValue());
assertThat(pipelinesDataFlowModel.getPipelines(), notNullValue());
assertThat(pipelinesDataFlowModel.getPipelines().size(), equalTo(1));
assertThat(pipelinesDataFlowModel.getPipelines(), hasKey("logstash-converted-pipeline"));
assertThat(pipelinesDataFlowModel.getPipelines().get("logstash-converted-pipeline"), notNullValue());
}
/**
* Provides arguments for a Logstash configuration path. This will include
* all Logstash configuration files, whether they have an expected YAML or not.
*/
static class LogstashPathsProviders implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
return provideLogstashConfigurationFiles()
.map(Arguments::of);
}
}
/**
* Provides JUnit5 arguments for Logstash configuration paths along with the
* expected YAML path.
*/
static class LogstashToYamlPathsProviders implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
return provideLogstashConfigurationFiles()
.filter(logstashPath -> Files.exists(Paths.get(getExpectedFileName(logstashPath))))
.map(logstashPath -> Arguments.of(logstashPath, getExpectedFileName(logstashPath)));
}
private String getExpectedFileName(final String file) {
return file.replace(".conf", ".expected.yaml");
}
}
private static Stream<String> provideLogstashConfigurationFiles() {
final File directoryPath = new File("src/test/resources/org/opensearch/dataprepper/logstash");
return Arrays.stream(Objects.requireNonNull(directoryPath.listFiles((dir, name) -> name.endsWith(".conf"))))
.filter(File::isFile)
.map(File::getPath);
}
}