mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Add utility method to cast profile configurations to specific classes (#5333)
* Add utility method to cast profile configurations to specific classes * Replace ProfileContext implementation in ScriptProfileTest by a mock Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
+9
-39
@@ -12,24 +12,14 @@
|
||||
*/
|
||||
package org.openhab.core.automation.module.script.profile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.openhab.core.automation.module.script.profile.ScriptProfile.CONFIG_COMMAND_FROM_ITEM_SCRIPT;
|
||||
import static org.openhab.core.automation.module.script.profile.ScriptProfile.CONFIG_STATE_FROM_ITEM_SCRIPT;
|
||||
import static org.openhab.core.automation.module.script.profile.ScriptProfile.CONFIG_TO_HANDLER_SCRIPT;
|
||||
import static org.openhab.core.automation.module.script.profile.ScriptProfile.CONFIG_TO_ITEM_SCRIPT;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.openhab.core.automation.module.script.profile.ScriptProfile.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -419,32 +409,12 @@ public class ScriptProfileTest extends JavaTest {
|
||||
}
|
||||
|
||||
public ProfileContext build() {
|
||||
return new ProfileContext() {
|
||||
@Override
|
||||
public Configuration getConfiguration() {
|
||||
return new Configuration(configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledExecutorService getExecutorService() {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Class<? extends State>> getAcceptedDataTypes() {
|
||||
return acceptedDataTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Class<? extends Command>> getAcceptedCommandTypes() {
|
||||
return acceptedCommandTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Class<? extends Command>> getHandlerAcceptedCommandTypes() {
|
||||
return handlerAcceptedCommandTypes;
|
||||
}
|
||||
};
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -56,6 +56,11 @@ public class ProfileContextImpl implements ProfileContext {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getConfigurationAs(Class<T> configurationClass) {
|
||||
return getConfiguration().as(configurationClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduledExecutorService getExecutorService() {
|
||||
return ThreadPoolManager.getScheduledPool(THREAD_POOL_NAME);
|
||||
|
||||
+8
@@ -38,6 +38,14 @@ public interface ProfileContext {
|
||||
*/
|
||||
Configuration getConfiguration();
|
||||
|
||||
/**
|
||||
* Returns the configuration of the profile and transforms it to the given class.
|
||||
*
|
||||
* @param configurationClass configuration class
|
||||
* @return configuration of profile in form of the given class
|
||||
*/
|
||||
<T> T getConfigurationAs(Class<T> configurationClass);
|
||||
|
||||
/**
|
||||
* Get a scheduler to be used within profiles (if needed at all)
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user