Add utility method to cast profile configurations to specific classes#5333
Conversation
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
9dc7c18 to
f234d16
Compare
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
There was a problem hiding this comment.
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 toProfileContextinterface to transform raw configuration to typed configuration classes - Implemented the method in
ProfileContextImplby delegating toConfiguration.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.
| 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I mocked only methods which are used in the test.
This also enables the configuration validation for profile configuration parameters.
openhab-core/bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/Configuration.java
Lines 77 to 81 in 9b854e2
Signed-off-by: Christoph Weitkamp github@christophweitkamp.de