Skip to content
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

feat/dispatcher-metadata-destination #127

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

simonhir
Copy link
Member

@simonhir simonhir commented Feb 26, 2025

Description

Implement dispatch destination via metadata file.

Reference

Issues closes #27

Summary by CodeRabbit

  • New Features

    • Introduced dynamic destination resolution for file dispatching that leverages metadata.
    • Added support for a new metadata dispatch binding key in configuration.
    • New MetadataHelper class for parsing metadata files and extracting index fields.
    • Added a new record for Metadata to encapsulate related data elements.
  • Bug Fixes

    • Enhanced error handling for file system access and metadata file parsing.
  • Tests

    • Expanded test coverage with new scenarios for metadata-driven dispatching.
    • Updated test constants and configurations to ensure clear, maintainable validations.

@simonhir simonhir self-assigned this Feb 26, 2025
Copy link
Contributor

coderabbitai bot commented Feb 26, 2025

Walkthrough

This PR updates the dispatching mechanism to support dynamic destination resolution using metadata. The modifications include adding a new method for resolving destination binding and updating the dispatch method signature to accept an additional destination parameter. Enhanced error handling is implemented via a new exception constructor and improved metadata parsing logic. A new helper class and record encapsulate metadata-related functionality, while configuration and test files are updated accordingly to support metadata-based dispatching.

Changes

Files Change Summary
.../dispatcher/application/usecase/DispatcherUseCase.java Updated dispatchFile method signature to include a destination parameter; added resolveDestinationBinding method for dynamic destination resolution; integrated error handling with FileSystemAccessException.
.../dispatcher/configuration/SwimDispatcherProperties.java Added new private field metadataDispatchBindingKey with validation annotation for destination binding configuration.
.../dispatcher/domain/exception/MetadataException.java Introduced a new constructor accepting both message and cause to enhance exception chaining.
.../dispatcher/domain/helper/MetadataHelper.java
.../dispatcher/domain/model/Metadata.java
.../dispatcher/domain/model/UseCase.java
Added a new Spring component MetadataHelper for parsing metadata and extracting index fields; created an immutable Metadata record; added a new field destinationViaMetadata in the UseCase model.
.../resources/application.yml Added a new configuration entry metadata-dispatch-binding-key: SWIM_Dispatch_Target under the swim section.
.../application/usecase/DispatcherUseCaseTest.java
.../application/usecase/ProtocolProcessingUseCaseTest.java
.../domain/model/FileTest.java
.../resources/application-test.yml
.../resources/files/example-metadata-destination.json
Enhanced tests by introducing new constants, updating file path references, refining imports, and adding new test cases and configuration for metadata destination handling.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant D as DispatcherUseCase
    participant M as MetadataHelper

    C->>D: processFile(file, useCase)
    alt UseCase requires metadata
        D->>M: parseMetadataFile(inputStream)
        M-->>D: Metadata (jsonNode, indexFields)
        D->>D: resolveDestinationBinding(useCase, file) returns destination
    end
    D->>D: dispatchFile(useCase, file, destination)
Loading

Possibly related PRs

Suggested labels

Component: Dispatch, Type: Feature

Suggested reviewers

  • rowe42

Poem

I'm a happy rabbit, hopping through the code,
Dispatching files with a newfound mode.
Metadata whispers secrets in every line,
New paths and tests now perfectly align.
With carrots of logic and joy so bright,
Our changes make the system just right! 🥕✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Free

📥 Commits

Reviewing files that changed from the base of the PR and between 114cbcf and a0e9ea9.

⛔ Files ignored due to path filters (1)
  • dispatch-service/src/test/resources/files/protocol.csv is excluded by !**/*.csv
📒 Files selected for processing (12)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/application/usecase/DispatcherUseCase.java (6 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/configuration/SwimDispatcherProperties.java (1 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/exception/MetadataException.java (1 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/helper/MetadataHelper.java (1 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/model/Metadata.java (1 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/model/UseCase.java (1 hunks)
  • dispatch-service/src/main/resources/application.yml (1 hunks)
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/application/usecase/DispatcherUseCaseTest.java (6 hunks)
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/application/usecase/ProtocolProcessingUseCaseTest.java (2 hunks)
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/domain/model/FileTest.java (1 hunks)
  • dispatch-service/src/test/resources/application-test.yml (1 hunks)
  • dispatch-service/src/test/resources/files/example-metadata-destination.json (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (10)
  • dispatch-service/src/main/resources/application.yml
  • dispatch-service/src/test/resources/files/example-metadata-destination.json
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/exception/MetadataException.java
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/application/usecase/ProtocolProcessingUseCaseTest.java
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/model/UseCase.java
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/domain/model/FileTest.java
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/model/Metadata.java
  • dispatch-service/src/test/resources/application-test.yml
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/configuration/SwimDispatcherProperties.java
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/helper/MetadataHelper.java

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (6)
dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/application/usecase/DispatcherUseCaseTest.java (2)

54-56: Consider renaming 'METATA_PATH' to 'METADATA_PATH'.
The name METATA_PATH appears to be a minor typo. A clearer name can improve readability.

- public static final String METATA_PATH = "test/inProcess/path/test.json";
+ public static final String METADATA_PATH = "test/inProcess/path/test.json";

131-149: Test coverage for metadata-based destination.
This new test method thoroughly checks the flow of reading metadata and dispatching to a dynamic destination. Consider adding a negative test scenario for missing or incomplete destination data in the metadata to enhance robustness further.

dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/application/usecase/DispatcherUseCase.java (3)

30-30: Usage of Strings.isNotBlank.
You are using org.apache.logging.log4j.util.Strings. Ensure this is consistent with string utility usage across the codebase to maintain uniformity.


135-135: Assigning default destination from action.
Using action.name() as a placeholder ensures a non-null destination. Proceed carefully if future actions have unexpected names.


202-228: Comprehensive destination resolution method.
This method handles metadata loading, parsing, fallback behavior, and exceptions. Consider logging how the fallback destination is chosen if metadata is absent or invalid for traceability.

dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/helper/MetadataHelper.java (1)

48-66: Consider additional validation for empty arrays and values

The method validates the structure well, but consider the following improvements:

  1. Check if the indexFieldsNode array is empty, as an empty array might not be meaningful
  2. Consider validating the value similar to how you validate the key, depending on your business requirements
if (indexFieldsNode == null || !indexFieldsNode.isArray()) {
    throw new MetadataException("Missing or invalid '" + METADATA_INDEX_FIELDS_KEY + "' in metadata JSON");
}
+if (indexFieldsNode.isEmpty()) {
+    throw new MetadataException("Empty '" + METADATA_INDEX_FIELDS_KEY + "' in metadata JSON");
+}

final Map<String, String> indexFields = new HashMap<>();
for (final JsonNode indexField : indexFieldsNode) {
    final String key = indexField.path(METADATA_KEY_KEY).asText();
    final String value = indexField.path(METADATA_VALUE_KEY).asText();
-    if (!key.isEmpty()) {
+    if (!key.isEmpty() && !value.isEmpty()) {
        indexFields.put(key, value);
    }
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7a8250c and 2ad77a9.

⛔ Files ignored due to path filters (1)
  • dispatch-service/src/test/resources/files/protocol.csv is excluded by !**/*.csv
📒 Files selected for processing (12)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/application/usecase/DispatcherUseCase.java (6 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/configuration/SwimDispatcherProperties.java (1 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/exception/MetadataException.java (1 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/helper/MetadataHelper.java (1 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/model/Metadata.java (1 hunks)
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/model/UseCase.java (1 hunks)
  • dispatch-service/src/main/resources/application.yml (1 hunks)
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/application/usecase/DispatcherUseCaseTest.java (6 hunks)
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/application/usecase/ProtocolProcessingUseCaseTest.java (2 hunks)
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/domain/model/FileTest.java (1 hunks)
  • dispatch-service/src/test/resources/application-test.yml (1 hunks)
  • dispatch-service/src/test/resources/files/example-metadata-destination.json (1 hunks)
✅ Files skipped from review due to trivial changes (4)
  • dispatch-service/src/test/resources/files/example-metadata-destination.json
  • dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/model/Metadata.java
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/domain/model/FileTest.java
  • dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/application/usecase/ProtocolProcessingUseCaseTest.java
🔇 Additional comments (21)
dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/configuration/SwimDispatcherProperties.java (1)

76-81: Property addition looks clean and well-documented.

The metadataDispatchBindingKey property is properly annotated with @NotBlank to ensure it's configured and includes clear Javadoc that explains its purpose and relationship with the UseCase.isDestinationViaMetadata() method.

dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/model/UseCase.java (1)

39-44: The new destinationViaMetadata flag is well-documented.

Good documentation that explains this flag's relationship with the SwimDispatcherProperties.getMetadataDispatchBindingKey() method and clarifies the fallback behavior when set to true. The default value of false is appropriate for backward compatibility.

dispatch-service/src/main/resources/application.yml (1)

93-94: New configuration property is properly categorized.

The new metadata-dispatch-binding-key configuration is well-organized with a relevant comment section labeled "# metadata", making it easy to locate related settings in the configuration file.

dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/exception/MetadataException.java (1)

8-11: Good addition of cause-aware constructor.

Adding a constructor that accepts both a message and a cause follows standard Java exception design patterns and improves error reporting by preserving the original exception chain. This will be helpful for debugging and error logging.

dispatch-service/src/test/resources/application-test.yml (1)

16-24: Add tests or validation for the new use-case configuration.
These lines introduce the test-meta-dest use case with destination-via-metadata. Ensure this new configuration is tested, especially to confirm that metadata-based dispatching behaves as expected.

dispatch-service/src/test/java/de/muenchen/oss/swim/dispatcher/application/usecase/DispatcherUseCaseTest.java (7)

23-23: Import usage check.
Importing ObjectMapper here seems valid, ensuring JSON parsing is properly handled.


34-34: Import usage check.
Importing MetadataHelper here integrates well with metadata parsing in the new tests.


49-49: Clarify test configuration.
Including MetadataHelper in the Spring context ensures it's injected and tested end-to-end. This is a good approach for verifying metadata-based dispatch.


117-119: Good use of mocks for metadata existence and URL retrieval.
The test setup adequately mocks file existence and presigned URL generation for metadata. This appears consistent with the new dispatch logic.


123-124: Verification logic appears correct.
Verifying the call to dispatchFile with the correct arguments ensures the underlying logic is functioning as intended.


162-162: Properly testing the missing metadata file scenario.
Returning false for file existence and expecting MetadataException is a valid check for handling absent metadata.


178-178: Ensuring zero calls to the legacy dispatch method signature.
This verification helps confirm that the updated dispatch logic with metadata is used instead of the older signature.

dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/application/usecase/DispatcherUseCase.java (6)

14-14: Ensure FileSystemAccessException usage is consistent.
This import is suitable for handling I/O-level issues when reading metadata files. Confirm that existing code references it correctly.


17-17: MetadataHelper import usage.
This helper centralizes metadata parsing logic, aligning with the new dynamic destination approach.


20-20: Metadata model import.
Importing Metadata record ensures typed access to metadata fields. No concerns here.


22-23: New I/O imports.
Imports for IOException and InputStream are valid for reading the metadata stream.


46-46: New metadataHelper dependency.
Injecting MetadataHelper facilitates metadata-based dispatch resolution. Looks correctly integrated.


144-145: Resolving destination from metadata.
Switching to metadata-based dispatch when action is DISPATCH is a clean, centralized approach.

dispatch-service/src/main/java/de/muenchen/oss/swim/dispatcher/domain/helper/MetadataHelper.java (3)

32-39: LGTM - Method handles parsing and error handling well

The parseMetadataFile method is well-structured with good error handling by wrapping IOExceptions in a domain-specific MetadataException. The NotNull annotation ensures proper input validation.


59-60: Be aware of potential null handling in JSON paths

Using path() with asText() will convert missing nodes to empty strings rather than null. This is working as intended with your empty check, but it's worth noting this behavior in case you expect different handling for missing fields in the future.


1-67: Overall well-structured component for metadata handling

The MetadataHelper class is well designed and implemented with:

  • Clear separation of concerns
  • Good error handling
  • Appropriate use of Spring and Lombok annotations
  • Well-documented methods with Javadoc
  • Constants for reusable string values

This will integrate well with the dispatcher functionality described in the PR objectives.

@simonhir simonhir marked this pull request as ready for review March 6, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feat] Dispatcher route based on metadata file
1 participant