Skip to content

Add utility method to cast profile configurations to specific classes#5333

Merged
holgerfriedrich merged 2 commits intoopenhab:mainfrom
cweitkamp:feature-profile-configuration-validation
Feb 13, 2026
Merged

Add utility method to cast profile configurations to specific classes#5333
holgerfriedrich merged 2 commits intoopenhab:mainfrom
cweitkamp:feature-profile-configuration-validation

Conversation

@cweitkamp
Copy link
Contributor

@cweitkamp cweitkamp commented Feb 8, 2026

  • Add utility method to cast profile configurations to specific classes

This also enables the configuration validation for profile configuration parameters.

public <T> T as(Class<T> configurationClass) {
synchronized (properties) {
return ConfigParser.configurationAs(properties, configurationClass);
}
}

Signed-off-by: Christoph Weitkamp github@christophweitkamp.de

@cweitkamp cweitkamp requested a review from a team as a code owner February 8, 2026 12:16
@cweitkamp cweitkamp added the enhancement An enhancement or new feature of the Core label Feb 8, 2026
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
@cweitkamp cweitkamp force-pushed the feature-profile-configuration-validation branch from 9dc7c18 to f234d16 Compare February 8, 2026 12:54
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds a convenience method getConfigurationAs(Class<T>) to the ProfileContext interface to enable type-safe configuration access with automatic validation for profile configuration parameters. This follows an established pattern already used in BaseModuleHandler for automation modules and delegates to the existing Configuration.as() method which uses reflection to map configuration properties to typed configuration beans.

Changes:

  • Added getConfigurationAs() method to ProfileContext interface to transform raw configuration to typed configuration classes
  • Implemented the method in ProfileContextImpl by delegating to Configuration.as()
  • Refactored test helper to use Mockito mocks instead of anonymous inner class implementation

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/profiles/ProfileContext.java Added new interface method getConfigurationAs() with JavaDoc
bundles/org.openhab.core.thing/src/main/java/org/openhab/core/thing/internal/ProfileContextImpl.java Implemented getConfigurationAs() by delegating to Configuration.as()
bundles/org.openhab.core.automation.module.script/src/test/java/org/openhab/core/automation/module/script/profile/ScriptProfileTest.java Refactored ProfileContextBuilder to use Mockito mocks; consolidated static imports to wildcard style

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +412 to +417
ProfileContext mockedProfileContext = mock(ProfileContext.class);
when(mockedProfileContext.getConfiguration()).thenReturn(new Configuration(configuration));
when(mockedProfileContext.getAcceptedDataTypes()).thenReturn(acceptedDataTypes);
when(mockedProfileContext.getAcceptedCommandTypes()).thenReturn(acceptedCommandTypes);
when(mockedProfileContext.getHandlerAcceptedCommandTypes()).thenReturn(handlerAcceptedCommandTypes);
return mockedProfileContext;
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The mock setup for ProfileContext doesn't include a stub for the new getConfigurationAs method. While this works for the current ScriptProfile implementation (which only calls getConfiguration()), it creates a potential issue if future code calls getConfigurationAs() on this mock. Consider adding a stub that delegates to the Configuration object's as() method to ensure complete mock behavior.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mocked only methods which are used in the test.

Copy link
Member

Choose a reason for hiding this comment

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

Fine for me.

@holgerfriedrich holgerfriedrich merged commit 15ec1c8 into openhab:main Feb 13, 2026
11 checks passed
@cweitkamp cweitkamp deleted the feature-profile-configuration-validation branch February 13, 2026 16:59
@holgerfriedrich holgerfriedrich added this to the 5.2 milestone Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement An enhancement or new feature of the Core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants