-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor PipelinesDataFlowModelParser to take in an InputStream instead of a file path #4289
Merged
graytaylor0
merged 2 commits into
opensearch-project:main
from
graytaylor0:RefactorParser2
Mar 20, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
71 changes: 71 additions & 0 deletions
71
...main/java/org/opensearch/dataprepper/pipeline/parser/PipelineConfigurationFileReader.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,71 @@ | ||
package org.opensearch.dataprepper.pipeline.parser; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.FileFilter; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class PipelineConfigurationFileReader implements PipelineConfigurationReader { | ||
private static final Logger LOG = LoggerFactory.getLogger(PipelineConfigurationFileReader.class); | ||
private final String pipelineConfigurationFileLocation; | ||
|
||
public PipelineConfigurationFileReader(final String pipelineConfigurationFileLocation) { | ||
this.pipelineConfigurationFileLocation = pipelineConfigurationFileLocation; | ||
} | ||
|
||
@Override | ||
public List<InputStream> getPipelineConfigurationInputStreams() { | ||
return getInputStreamsForConfigurationFiles(); | ||
} | ||
|
||
private List<InputStream> getInputStreamsForConfigurationFiles() { | ||
final File configurationLocation = new File(pipelineConfigurationFileLocation); | ||
|
||
if (configurationLocation.isFile()) { | ||
final List<InputStream> inputStreams = Stream.of(configurationLocation).map(this::getInputStreamForFile) | ||
.filter(Objects::nonNull).collect(Collectors.toList()); | ||
|
||
if (inputStreams.size() != 1) { | ||
throw new ParseException(format("Pipeline configuration file not loadable at %s", configurationLocation.getName())); | ||
} | ||
return inputStreams; | ||
} else if (configurationLocation.isDirectory()) { | ||
FileFilter yamlFilter = pathname -> (pathname.getName().endsWith(".yaml") || pathname.getName().endsWith(".yml")); | ||
List<InputStream> inputStreams = Stream.of(configurationLocation.listFiles(yamlFilter)) | ||
.map(this::getInputStreamForFile) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toList()); | ||
|
||
if (inputStreams.isEmpty()) { | ||
LOG.error("Pipelines configuration file not found at {}", pipelineConfigurationFileLocation); | ||
throw new ParseException( | ||
format("Pipelines configuration file not found at %s", pipelineConfigurationFileLocation)); | ||
} | ||
|
||
return inputStreams; | ||
} else { | ||
LOG.error("Pipelines configuration file not found at {}", pipelineConfigurationFileLocation); | ||
throw new ParseException(format("Pipelines configuration file not found at %s", pipelineConfigurationFileLocation)); | ||
} | ||
} | ||
|
||
private InputStream getInputStreamForFile(final File pipelineConfigurationFile) { | ||
|
||
try { | ||
return new FileInputStream(pipelineConfigurationFile); | ||
} catch (IOException e) { | ||
LOG.warn("Unable to load pipeline configuration file {}", pipelineConfigurationFile.getName()); | ||
return null; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...src/main/java/org/opensearch/dataprepper/pipeline/parser/PipelineConfigurationReader.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,14 @@ | ||
package org.opensearch.dataprepper.pipeline.parser; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
public interface PipelineConfigurationReader { | ||
|
||
/** | ||
* | ||
* @return a List of InputStream that contains each of the pipeline configurations. | ||
* the caller of this method is responsible for closing these input streams after they are used | ||
*/ | ||
List<InputStream> getPipelineConfigurationInputStreams(); | ||
} |
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
114 changes: 114 additions & 0 deletions
114
.../java/org/opensearch/dataprepper/pipeline/parser/PipelineConfigurationFileReaderTest.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,114 @@ | ||
package org.opensearch.dataprepper.pipeline.parser; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class PipelineConfigurationFileReaderTest { | ||
|
||
@TempDir | ||
Path tempDir; | ||
|
||
@Test | ||
void getPipelineConfigurationInputStreams_from_directory_with_no_yaml_files_should_throw() { | ||
final PipelineConfigurationReader objectUnderTest = | ||
new PipelineConfigurationFileReader(TestConfigurationProvider.EMPTY_PIPELINE_DIRECTOTRY); | ||
|
||
|
||
final RuntimeException actualException = assertThrows(RuntimeException.class, | ||
objectUnderTest::getPipelineConfigurationInputStreams); | ||
assertThat(actualException.getMessage(), equalTo( | ||
String.format("Pipelines configuration file not found at %s", TestConfigurationProvider.EMPTY_PIPELINE_DIRECTOTRY))); | ||
} | ||
|
||
@Test | ||
void getPipelineConfigurationInputStreams_with_a_configuration_file_which_does_not_exist_should_throw() { | ||
final PipelineConfigurationReader objectUnderTest = | ||
new PipelineConfigurationFileReader("file_does_not_exist.yml"); | ||
|
||
final RuntimeException actualException = assertThrows(RuntimeException.class, | ||
objectUnderTest::getPipelineConfigurationInputStreams); | ||
assertThat(actualException.getMessage(), equalTo("Pipelines configuration file not found at file_does_not_exist.yml")); | ||
} | ||
|
||
@Test | ||
void getPipelineConfigurationInputStreams_with_a_configuration_file_exists_and_is_not_loadable_should_throw() throws IOException { | ||
final String yamlContent = UUID.randomUUID().toString(); | ||
final Path file = tempDir.resolve("test-pipeline.yaml"); | ||
Files.writeString(file, yamlContent); | ||
|
||
file.toFile().setReadable(false, false); | ||
|
||
final PipelineConfigurationReader objectUnderTest = | ||
new PipelineConfigurationFileReader(file.toString()); | ||
|
||
final RuntimeException actualException = assertThrows(RuntimeException.class, | ||
objectUnderTest::getPipelineConfigurationInputStreams); | ||
assertThat(actualException.getMessage(), equalTo("Pipeline configuration file not loadable at test-pipeline.yaml")); | ||
} | ||
|
||
@Test | ||
void getPipelineConfigurationInput_streams_from_existing_file() throws IOException { | ||
|
||
final String yamlContent = UUID.randomUUID().toString(); | ||
final Path file = tempDir.resolve("test-pipeline.yaml"); | ||
Files.writeString(file, yamlContent); | ||
|
||
final PipelineConfigurationReader objectUnderTest = | ||
new PipelineConfigurationFileReader(file.toString()); | ||
|
||
final List<InputStream> inputStreams = objectUnderTest.getPipelineConfigurationInputStreams(); | ||
|
||
assertThat(inputStreams.size(), equalTo(1)); | ||
|
||
try (final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStreams.get(0), StandardCharsets.UTF_8))) { | ||
final String content = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())); | ||
assertThat(content, equalTo(yamlContent)); | ||
} | ||
} | ||
|
||
@Test | ||
void getPipelineConfigurationInput_streams_from_existing_directory() throws IOException { | ||
|
||
|
||
final String yamlContentPipelineOne = UUID.randomUUID().toString(); | ||
final String yamlContentPipelineTwo = UUID.randomUUID().toString(); | ||
|
||
Files.writeString(tempDir.resolve("test-pipeline-1.yaml"), yamlContentPipelineOne); | ||
Files.writeString(tempDir.resolve("tset-pipeline-2.yml"), yamlContentPipelineTwo); | ||
|
||
final PipelineConfigurationReader objectUnderTest = | ||
new PipelineConfigurationFileReader(tempDir.toString()); | ||
|
||
final List<InputStream> inputStreams = objectUnderTest.getPipelineConfigurationInputStreams(); | ||
|
||
assertThat(inputStreams.size(), equalTo(2)); | ||
|
||
try (final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStreams.get(0), StandardCharsets.UTF_8))) { | ||
final String content = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())); | ||
assertThat(content, equalTo(yamlContentPipelineOne)); | ||
} | ||
|
||
try (final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStreams.get(1), StandardCharsets.UTF_8))) { | ||
final String content = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())); | ||
assertThat(content, equalTo(yamlContentPipelineTwo)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, I think we should throw an exception since we are looking for a single file.