Skip to content

Add unit tests to improve test coverage#57

Merged
orpiske merged 2 commits intowanaku-ai:mainfrom
orpiske:ci-issue-55
Jan 31, 2026
Merged

Add unit tests to improve test coverage#57
orpiske merged 2 commits intowanaku-ai:mainfrom
orpiske:ci-issue-55

Conversation

@orpiske
Copy link
Copy Markdown
Contributor

@orpiske orpiske commented Jan 31, 2026

Summary

  • Add tests for PropertyFileProvider: file loading, null URI handling, error cases
  • Add tests for FileConfigurationWriter: file writing, URI generation, error handling
  • Add tests for DiscoveryHelper: address resolution with 'auto' keyword support
  • Add tests for TokenEndpoint: URL construction strategies and autoResolve logic

Coverage Improvements

Module Class Before After
capabilities-config-provider-file PropertyFileProvider 0% 100%
capabilities-config-provider-file FileConfigurationWriter 0% 100%
capabilities-security TokenEndpoint 0% 100%
capabilities-discovery DiscoveryHelper 0% 68%

Addresses #55

Test plan

  • All new tests pass
  • Full test suite passes (mvn clean test)

Summary by Sourcery

Add unit tests to validate file-based configuration, discovery utilities, and token endpoint behavior, and wire up required test dependencies for the affected modules.

Build:

  • Add JUnit Jupiter as a test-scoped dependency in the discovery and security module POMs.

Tests:

  • Add comprehensive tests for FileConfigurationWriter covering file creation, overwrites, encoding, and error handling.
  • Add tests for PropertyFileProvider to cover normal loading, null URI behavior, empty files, and invalid paths.
  • Add tests for DiscoveryHelper registration address resolution, including explicit values, auto keyword handling, and edge cases.
  • Add tests for TokenEndpoint URL construction methods and autoResolve strategy under various input scenarios.

- PropertyFileProviderTest: file loading, null URI handling, error cases
- FileConfigurationWriterTest: file writing, URI generation, error handling
- DiscoveryHelperTest: address resolution with 'auto' keyword support
- TokenEndpointTest: URL construction strategies and autoResolve logic

Addresses wanaku-ai#55
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Jan 31, 2026

Reviewer's Guide

Adds JUnit 5 as a test dependency to the discovery and security modules and introduces comprehensive unit tests for file-based configuration providers, discovery address resolution, and token endpoint URL construction/auto-resolution logic to significantly improve coverage.

Class diagram for new test coverage relationships

classDiagram
    class PropertyFileProvider
    class FileConfigurationWriter
    class DiscoveryHelper
    class TokenEndpoint

    class PropertyFileProviderTest
    class FileConfigurationWriterTest
    class DiscoveryHelperTest
    class TokenEndpointTest

    PropertyFileProviderTest --> PropertyFileProvider : tests
    FileConfigurationWriterTest --> FileConfigurationWriter : tests
    DiscoveryHelperTest --> DiscoveryHelper : tests
    TokenEndpointTest --> TokenEndpoint : tests
Loading

File-Level Changes

Change Details Files
Introduce unit tests for FileConfigurationWriter behavior and error handling.
  • Add TempDir-based tests verifying files are written with expected content and URIs are returned
  • Test overwriting existing files and writing empty, multiline, and Unicode content
  • Verify String-path constructor behavior
  • Assert invalid directory usage results in ConfigWriteException
capabilities-config-providers/capabilities-config-provider-file/src/test/java/ai/wanaku/capabilities/sdk/config/provider/file/FileConfigurationWriterTest.java
Introduce unit tests for PropertyFileProvider covering normal, null, and error scenarios.
  • Test loading of standard key/value properties from a valid file
  • Verify null URI yields non-null but empty Properties
  • Assert non-existent file triggers WanakuException
  • Cover empty properties file behavior
  • Test properties containing URLs, paths, and whitespace in keys/values
capabilities-config-providers/capabilities-config-provider-file/src/test/java/ai/wanaku/capabilities/sdk/config/provider/file/PropertyFileProviderTest.java
Add unit tests for DiscoveryHelper registration address resolution, including 'auto' handling.
  • Validate passthrough behavior for explicit IPs, hostnames, null, and empty strings
  • Ensure lowercase 'auto' resolves to a concrete address and is case-sensitive
  • Test no-arg resolveRegistrationAddress returns a non-empty IP-like value and matches IPv4 or IPv6 format
capabilities-discovery/src/test/java/ai/wanaku/capabilities/sdk/discovery/util/DiscoveryHelperTest.java
Add unit tests for TokenEndpoint URL construction helpers and autoResolve behavior.
  • Verify direct returns the provided URI unchanged
  • Check fromBaseUrl appends the OpenID Connect token path
  • Check forDiscovery appends the discovery OIDC path
  • Exercise autoResolve behavior for direct passthrough, realms/wanaku base URLs, empty token endpoint, and null token endpoint
capabilities-security/src/test/java/ai/wanaku/capabilities/sdk/security/TokenEndpointTest.java
Configure JUnit Jupiter as a test dependency for discovery and security modules.
  • Add junit-jupiter test-scoped dependency to capabilities-discovery module POM
  • Add junit-jupiter test-scoped dependency to capabilities-security module POM
capabilities-discovery/pom.xml
capabilities-security/pom.xml

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 2 issues, and left some high level feedback:

  • The TokenEndpointTest expectations for URLs with trailing slashes and empty tokenEndpointUri (e.g., the double // and /protocol/openid-connect/token without a base) look brittle; consider normalizing URL joining in TokenEndpoint and updating tests to assert the normalized behavior instead of relying on these edge-case string concatenations.
  • The DiscoveryHelperTest methods that assert on IP address format (resolveRegistrationAddressResolvesAutoKeyword and related) may be environment-sensitive (hostnames vs. IPs, IPv4 vs. IPv6); consider loosening these assertions or isolating them from specific format assumptions to avoid flakiness across different CI/runtime environments.
  • In FileConfigurationWriterTest.writesContentWithUnicodeCharacters, you only assert file existence; it would be more robust to also assert that the file contents match the original Unicode string to fully verify encoding/decoding behavior.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `TokenEndpointTest` expectations for URLs with trailing slashes and empty `tokenEndpointUri` (e.g., the double `//` and `/protocol/openid-connect/token` without a base) look brittle; consider normalizing URL joining in `TokenEndpoint` and updating tests to assert the normalized behavior instead of relying on these edge-case string concatenations.
- The `DiscoveryHelperTest` methods that assert on IP address format (`resolveRegistrationAddressResolvesAutoKeyword` and related) may be environment-sensitive (hostnames vs. IPs, IPv4 vs. IPv6); consider loosening these assertions or isolating them from specific format assumptions to avoid flakiness across different CI/runtime environments.
- In `FileConfigurationWriterTest.writesContentWithUnicodeCharacters`, you only assert file existence; it would be more robust to also assert that the file contents match the original Unicode string to fully verify encoding/decoding behavior.

## Individual Comments

### Comment 1
<location> `capabilities-config-providers/capabilities-config-provider-file/src/test/java/ai/wanaku/capabilities/sdk/config/provider/file/FileConfigurationWriterTest.java:87-96` </location>
<code_context>
+    }
+
+    @Test
+    void writesContentWithUnicodeCharacters() throws IOException {
+        FileConfigurationWriter writer = new FileConfigurationWriter(tempDir.toFile());
+        String unicodeData = "key=日本語\nother=émoji🎉";
+
+        URI uri = writer.write("unicode.txt", unicodeData);
+
+        Path writtenFile = Path.of(uri);
+        assertTrue(Files.exists(writtenFile));
+    }
+}
</code_context>

<issue_to_address>
**suggestion (testing):** Strengthen the Unicode test by asserting round-trip content equality, not just file existence

This test currently only checks that a file exists, which duplicates coverage from other tests. To validate Unicode handling (including emojis and other non-ASCII characters), please also assert the written contents, e.g. `assertEquals(unicodeData, Files.readString(writtenFile));`, so encoding issues are caught rather than just file creation.

```suggestion
    @Test
    void writesContentWithUnicodeCharacters() throws IOException {
        FileConfigurationWriter writer = new FileConfigurationWriter(tempDir.toFile());
        String unicodeData = "key=日本語\nother=émoji🎉";

        URI uri = writer.write("unicode.txt", unicodeData);

        Path writtenFile = Path.of(uri);
        assertTrue(Files.exists(writtenFile));
        assertEquals(unicodeData, Files.readString(writtenFile));
    }
```
</issue_to_address>

### Comment 2
<location> `capabilities-config-providers/capabilities-config-provider-file/src/test/java/ai/wanaku/capabilities/sdk/config/provider/file/FileConfigurationWriterTest.java:68-77` </location>
<code_context>
+    }
+
+    @Test
+    void throwsConfigWriteExceptionForInvalidDirectory() {
+        File nonExistentDir = new File("/non/existent/path/that/does/not/exist");
+        FileConfigurationWriter writer = new FileConfigurationWriter(nonExistentDir);
</code_context>

<issue_to_address>
**suggestion (testing):** Consider adding a test for write failures on an existing but non-writable directory

If your CI setup allows, you could point the writer at a temporary directory made read-only and assert that `write` throws `ConfigWriteException`. That would validate the failure path during the actual write call rather than only at directory resolution time.

```suggestion
    @Test
    void throwsConfigWriteExceptionForInvalidDirectory() {
        File nonExistentDir = new File("/non/existent/path/that/does/not/exist");
        FileConfigurationWriter writer = new FileConfigurationWriter(nonExistentDir);

        assertThrows(ConfigWriteException.class, () -> writer.write("file.txt", "data"));
    }

    @Test
    void throwsConfigWriteExceptionForNonWritableDirectory() throws IOException {
        Path readOnlyDir = Files.createTempDirectory("readOnlyDir");
        File nonWritableDir = readOnlyDir.toFile();
        assertTrue(nonWritableDir.setWritable(false, false), "Failed to make directory non-writable");

        FileConfigurationWriter writer = new FileConfigurationWriter(nonWritableDir);

        assertThrows(ConfigWriteException.class, () -> writer.write("file.txt", "data"));
    }

    @Test
    void writesMultilineContent() throws IOException {
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

- Add content assertion to Unicode test for round-trip verification
- Add test for non-writable directory error handling
- Loosen DiscoveryHelper IP format assertions to avoid CI flakiness
@orpiske orpiske merged commit ef79c18 into wanaku-ai:main Jan 31, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant