Skip to content

Refactor: Improve robustness in CosmosDBFilterExpressionConverter#5338

Open
ryujungkyun wants to merge 1 commit intospring-projects:mainfrom
ryujungkyun:refactor/issue-5337-cosmosdb-converter
Open

Refactor: Improve robustness in CosmosDBFilterExpressionConverter#5338
ryujungkyun wants to merge 1 commit intospring-projects:mainfrom
ryujungkyun:refactor/issue-5337-cosmosdb-converter

Conversation

@ryujungkyun
Copy link

Resolves #5337
This PR improves the robustness and readability of CosmosDBFilterExpressionConverter.

The changes are intentionally small and focused, addressing edge cases in the constructor and simplifying the doKey method to use more idiomatic Optional patterns.

All changes were made following a TDD-based approach, ensuring existing behavior is preserved while improving code quality.

  1. Improve Constructor Robustness

before
Pasted Graphic 4

The constructor previously collected the provided columns directly into a Map using Collectors.toMap.

Problems addressed:
Duplicate column names would cause IllegalStateException
No explicit validation for null input

Improvements:
Pasted Graphic 5
Added Assert.notNull(columns, ...) to fail fast
Applied distinct() to safely handle duplicate column names

  1. Simplify and Clarify doKey Method
Pasted Graphic 13

The previous implementation relied on Optional.isPresent() followed by Optional.get(), which is verbose and less expressive.

Using Optional.get() without proper guarding can result in a NoSuchElementException and offers little advantage over traditional null checks. In such cases, it effectively behaves the same as accessing a potentially null value directly.

Improvement:
image
Replaced with orElseThrow to clearly express that missing metadata configuration is an exceptional case
Improved readability and control flow

Testing (TDD)
The changes were implemented following a Test-Driven Development (TDD) approach.

Initially, I verified that an exception was thrown due to duplicate columns.
Pasted Graphic

Pasted Graphic 1

Verified that the constructor throws an IllegalArgumentException when the input is null

Pasted Graphic 2 Pasted Graphic 3

After modifying the application, I verified via unit tests that duplicate columns are handled safely without throwing exceptions

Pasted Graphic 6 Pasted Graphic 7

Verified that null input is explicitly rejected with an IllegalArgumentException

Pasted Graphic 8 Pasted Graphic 9

This test ensures the robustness of the doKey method. It initializes the converter with a specific field (e.g., "country") and attempts to create a filter expression using an unconfigured field (e.g., "city"). The test asserts that this invalid operation triggers an IllegalArgumentException with the message "No metadata field city has been configured", confirming that the orElseThrow logic works as expected.

Pasted Graphic 10 Pasted Graphic 11

I executed the full unit test suite to verify test isolation.
✓ doKey should throw clear exception for unknown keys

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.

Improve robustness and readability in CosmosDBFilterExpressionConverter

4 participants