[persistence] Add persistence exclusion tests (#4482)

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
This commit is contained in:
Mark Herwege 2024-12-08 18:02:54 +01:00 committed by GitHub
parent b31ff66bba
commit 8e4cebdec2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -61,7 +61,9 @@ import org.openhab.core.persistence.QueryablePersistenceService;
import org.openhab.core.persistence.config.PersistenceAllConfig; import org.openhab.core.persistence.config.PersistenceAllConfig;
import org.openhab.core.persistence.config.PersistenceConfig; import org.openhab.core.persistence.config.PersistenceConfig;
import org.openhab.core.persistence.config.PersistenceGroupConfig; import org.openhab.core.persistence.config.PersistenceGroupConfig;
import org.openhab.core.persistence.config.PersistenceGroupExcludeConfig;
import org.openhab.core.persistence.config.PersistenceItemConfig; import org.openhab.core.persistence.config.PersistenceItemConfig;
import org.openhab.core.persistence.config.PersistenceItemExcludeConfig;
import org.openhab.core.persistence.filter.PersistenceFilter; import org.openhab.core.persistence.filter.PersistenceFilter;
import org.openhab.core.persistence.filter.PersistenceThresholdFilter; import org.openhab.core.persistence.filter.PersistenceThresholdFilter;
import org.openhab.core.persistence.registry.PersistenceServiceConfiguration; import org.openhab.core.persistence.registry.PersistenceServiceConfiguration;
@ -167,7 +169,7 @@ public class PersistenceManagerTest {
@Test @Test
public void appliesToItemWithItemConfig() { public void appliesToItemWithItemConfig() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceItemConfig(TEST_ITEM_NAME), addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceItemConfig(TEST_ITEM_NAME)),
PersistenceStrategy.Globals.UPDATE, null); PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM, TEST_STATE); manager.stateUpdated(TEST_ITEM, TEST_STATE);
@ -178,7 +180,7 @@ public class PersistenceManagerTest {
@Test @Test
public void doesNotApplyToItemWithItemConfig() { public void doesNotApplyToItemWithItemConfig() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceItemConfig(TEST_ITEM_NAME), addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceItemConfig(TEST_ITEM_NAME)),
PersistenceStrategy.Globals.UPDATE, null); PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM2, TEST_STATE); manager.stateUpdated(TEST_ITEM2, TEST_STATE);
@ -188,7 +190,7 @@ public class PersistenceManagerTest {
@Test @Test
public void appliesToGroupItemWithItemConfig() { public void appliesToGroupItemWithItemConfig() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceItemConfig(TEST_GROUP_ITEM_NAME), addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceItemConfig(TEST_GROUP_ITEM_NAME)),
PersistenceStrategy.Globals.UPDATE, null); PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_GROUP_ITEM, TEST_STATE); manager.stateUpdated(TEST_GROUP_ITEM, TEST_STATE);
@ -199,7 +201,7 @@ public class PersistenceManagerTest {
@Test @Test
public void appliesToItemWithGroupConfig() { public void appliesToItemWithGroupConfig() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceGroupConfig(TEST_GROUP_ITEM_NAME), addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceGroupConfig(TEST_GROUP_ITEM_NAME)),
PersistenceStrategy.Globals.UPDATE, null); PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM, TEST_STATE); manager.stateUpdated(TEST_ITEM, TEST_STATE);
@ -210,7 +212,7 @@ public class PersistenceManagerTest {
@Test @Test
public void doesNotApplyToItemWithGroupConfig() { public void doesNotApplyToItemWithGroupConfig() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceGroupConfig(TEST_GROUP_ITEM_NAME), addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceGroupConfig(TEST_GROUP_ITEM_NAME)),
PersistenceStrategy.Globals.UPDATE, null); PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM2, TEST_STATE); manager.stateUpdated(TEST_ITEM2, TEST_STATE);
@ -221,8 +223,8 @@ public class PersistenceManagerTest {
@Test @Test
public void appliesToItemWithAllConfig() { public void appliesToItemWithAllConfig() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceAllConfig(), PersistenceStrategy.Globals.UPDATE, addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceAllConfig()),
null); PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM, TEST_STATE); manager.stateUpdated(TEST_ITEM, TEST_STATE);
manager.stateUpdated(TEST_ITEM2, TEST_STATE); manager.stateUpdated(TEST_ITEM2, TEST_STATE);
@ -235,10 +237,52 @@ public class PersistenceManagerTest {
verifyNoMoreInteractions(persistenceServiceMock); verifyNoMoreInteractions(persistenceServiceMock);
} }
@Test
public void doesNotApplyToItemWithGroupConfigAndItemExclusion() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceGroupConfig(TEST_GROUP_ITEM_NAME),
new PersistenceItemExcludeConfig(TEST_ITEM_NAME)), PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM, TEST_STATE);
verifyNoMoreInteractions(persistenceServiceMock);
}
@Test
public void doesNotApplyToItemWithAllConfigAndItemExclusion() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID,
List.of(new PersistenceAllConfig(), new PersistenceItemExcludeConfig(TEST_ITEM_NAME)),
PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM, TEST_STATE);
manager.stateUpdated(TEST_ITEM2, TEST_STATE);
manager.stateUpdated(TEST_GROUP_ITEM, TEST_STATE);
verify(persistenceServiceMock).store(TEST_ITEM2, null);
verify(persistenceServiceMock).store(TEST_GROUP_ITEM, null);
verifyNoMoreInteractions(persistenceServiceMock);
}
@Test
public void doesNotApplyToItemWithAllConfigAndGroupExclusion() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID,
List.of(new PersistenceAllConfig(), new PersistenceGroupExcludeConfig(TEST_GROUP_ITEM_NAME)),
PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM, TEST_STATE);
manager.stateUpdated(TEST_ITEM2, TEST_STATE);
manager.stateUpdated(TEST_GROUP_ITEM, TEST_STATE);
verify(persistenceServiceMock).store(TEST_ITEM2, null);
verify(persistenceServiceMock).store(TEST_GROUP_ITEM, null);
verifyNoMoreInteractions(persistenceServiceMock);
}
@Test @Test
public void updatedStatePersistsEveryUpdate() { public void updatedStatePersistsEveryUpdate() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceAllConfig(), PersistenceStrategy.Globals.UPDATE, addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceAllConfig()),
null); PersistenceStrategy.Globals.UPDATE, null);
manager.stateUpdated(TEST_ITEM, TEST_STATE); manager.stateUpdated(TEST_ITEM, TEST_STATE);
manager.stateUpdated(TEST_ITEM, TEST_STATE); manager.stateUpdated(TEST_ITEM, TEST_STATE);
@ -250,8 +294,8 @@ public class PersistenceManagerTest {
@Test @Test
public void updatedStateDoesNotPersistWithChangeStrategy() { public void updatedStateDoesNotPersistWithChangeStrategy() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceAllConfig(), PersistenceStrategy.Globals.CHANGE, addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceAllConfig()),
null); PersistenceStrategy.Globals.CHANGE, null);
manager.stateUpdated(TEST_ITEM, TEST_STATE); manager.stateUpdated(TEST_ITEM, TEST_STATE);
verifyNoMoreInteractions(persistenceServiceMock); verifyNoMoreInteractions(persistenceServiceMock);
@ -259,8 +303,8 @@ public class PersistenceManagerTest {
@Test @Test
public void changedStatePersistsWithChangeStrategy() { public void changedStatePersistsWithChangeStrategy() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceAllConfig(), PersistenceStrategy.Globals.CHANGE, addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceAllConfig()),
null); PersistenceStrategy.Globals.CHANGE, null);
manager.stateChanged(TEST_ITEM, UnDefType.UNDEF, TEST_STATE); manager.stateChanged(TEST_ITEM, UnDefType.UNDEF, TEST_STATE);
@ -270,8 +314,8 @@ public class PersistenceManagerTest {
@Test @Test
public void changedStateDoesNotPersistWithUpdateStrategy() { public void changedStateDoesNotPersistWithUpdateStrategy() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceAllConfig(), PersistenceStrategy.Globals.UPDATE, addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceAllConfig()),
null); PersistenceStrategy.Globals.UPDATE, null);
manager.stateChanged(TEST_ITEM, UnDefType.UNDEF, TEST_STATE); manager.stateChanged(TEST_ITEM, UnDefType.UNDEF, TEST_STATE);
@ -329,7 +373,7 @@ public class PersistenceManagerTest {
return future; return future;
}); });
addConfiguration(TestModifiablePersistenceService.ID, new PersistenceAllConfig(), addConfiguration(TestModifiablePersistenceService.ID, List.of(new PersistenceAllConfig()),
PersistenceStrategy.Globals.FORECAST, null); PersistenceStrategy.Globals.FORECAST, null);
Instant time1 = Instant.now().minusSeconds(1000); Instant time1 = Instant.now().minusSeconds(1000);
@ -397,9 +441,9 @@ public class PersistenceManagerTest {
ArgumentCaptor<SchedulerRunnable> runnableCaptor = ArgumentCaptor.forClass(SchedulerRunnable.class); ArgumentCaptor<SchedulerRunnable> runnableCaptor = ArgumentCaptor.forClass(SchedulerRunnable.class);
when(cronSchedulerMock.schedule(runnableCaptor.capture(), any())).thenReturn(scheduledFutureMock); when(cronSchedulerMock.schedule(runnableCaptor.capture(), any())).thenReturn(scheduledFutureMock);
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceItemConfig(TEST_ITEM3_NAME), addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceItemConfig(TEST_ITEM3_NAME)),
new PersistenceCronStrategy("withoutFilter", "0 0 * * * ?"), null); new PersistenceCronStrategy("withoutFilter", "0 0 * * * ?"), null);
addConfiguration(TEST_QUERYABLE_PERSISTENCE_SERVICE_ID, new PersistenceItemConfig(TEST_ITEM3_NAME), addConfiguration(TEST_QUERYABLE_PERSISTENCE_SERVICE_ID, List.of(new PersistenceItemConfig(TEST_ITEM3_NAME)),
new PersistenceCronStrategy("withFilter", "0 * * * * ?"), new PersistenceCronStrategy("withFilter", "0 * * * * ?"),
new PersistenceThresholdFilter("test", BigDecimal.TEN, "", false)); new PersistenceThresholdFilter("test", BigDecimal.TEN, "", false));
@ -428,8 +472,8 @@ public class PersistenceManagerTest {
when(cronSchedulerMock.schedule(any(), any())).thenReturn(scheduledFutureMock); when(cronSchedulerMock.schedule(any(), any())).thenReturn(scheduledFutureMock);
PersistenceServiceConfiguration configuration = addConfiguration(TEST_PERSISTENCE_SERVICE_ID, PersistenceServiceConfiguration configuration = addConfiguration(TEST_PERSISTENCE_SERVICE_ID,
new PersistenceItemConfig(TEST_ITEM_NAME), new PersistenceCronStrategy("everyHour", "0 0 * * * ?"), List.of(new PersistenceItemConfig(TEST_ITEM_NAME)),
null); new PersistenceCronStrategy("everyHour", "0 0 * * * ?"), null);
manager.onReadyMarkerAdded(new ReadyMarker("", "")); manager.onReadyMarkerAdded(new ReadyMarker("", ""));
@ -444,8 +488,8 @@ public class PersistenceManagerTest {
@Test @Test
public void filterAppliesOnStateUpdate() { public void filterAppliesOnStateUpdate() {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, new PersistenceAllConfig(), PersistenceStrategy.Globals.UPDATE, addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(new PersistenceAllConfig()),
new PersistenceThresholdFilter("test", BigDecimal.TEN, "", false)); PersistenceStrategy.Globals.UPDATE, new PersistenceThresholdFilter("test", BigDecimal.TEN, "", false));
manager.stateUpdated(TEST_ITEM3, DecimalType.ZERO); manager.stateUpdated(TEST_ITEM3, DecimalType.ZERO);
manager.stateUpdated(TEST_ITEM3, DecimalType.ZERO); manager.stateUpdated(TEST_ITEM3, DecimalType.ZERO);
@ -459,8 +503,9 @@ public class PersistenceManagerTest {
* Add a configuration for restoring TEST_ITEM and mock the SafeCaller * Add a configuration for restoring TEST_ITEM and mock the SafeCaller
*/ */
private void setupPersistence(PersistenceConfig itemConfig) { private void setupPersistence(PersistenceConfig itemConfig) {
addConfiguration(TEST_PERSISTENCE_SERVICE_ID, itemConfig, PersistenceStrategy.Globals.RESTORE, null); addConfiguration(TEST_PERSISTENCE_SERVICE_ID, List.of(itemConfig), PersistenceStrategy.Globals.RESTORE, null);
addConfiguration(TEST_QUERYABLE_PERSISTENCE_SERVICE_ID, itemConfig, PersistenceStrategy.Globals.RESTORE, null); addConfiguration(TEST_QUERYABLE_PERSISTENCE_SERVICE_ID, List.of(itemConfig),
PersistenceStrategy.Globals.RESTORE, null);
when(safeCallerMock.create(queryablePersistenceServiceMock, QueryablePersistenceService.class)) when(safeCallerMock.create(queryablePersistenceServiceMock, QueryablePersistenceService.class))
.thenReturn(safeCallerBuilderMock); .thenReturn(safeCallerBuilderMock);
@ -473,16 +518,16 @@ public class PersistenceManagerTest {
* Add a configuration to the manager * Add a configuration to the manager
* *
* @param serviceId the persistence service id * @param serviceId the persistence service id
* @param itemConfig the item configuration * @param itemConfigs list item configurations
* @param strategy the strategy * @param strategy the strategy
* @param filter a persistence filter * @param filter a persistence filter
* @return the added strategy * @return the added strategy
*/ */
private PersistenceServiceConfiguration addConfiguration(String serviceId, PersistenceConfig itemConfig, private PersistenceServiceConfiguration addConfiguration(String serviceId, List<PersistenceConfig> itemConfigs,
PersistenceStrategy strategy, @Nullable PersistenceFilter filter) { PersistenceStrategy strategy, @Nullable PersistenceFilter filter) {
List<PersistenceFilter> filters = filter != null ? List.of(filter) : List.of(); List<PersistenceFilter> filters = filter != null ? List.of(filter) : List.of();
PersistenceItemConfiguration itemConfiguration = new PersistenceItemConfiguration(List.of(itemConfig), null, PersistenceItemConfiguration itemConfiguration = new PersistenceItemConfiguration(itemConfigs, null,
List.of(strategy), filters); List.of(strategy), filters);
List<PersistenceStrategy> strategies = PersistenceStrategy.Globals.STRATEGIES.containsValue(strategy) List<PersistenceStrategy> strategies = PersistenceStrategy.Globals.STRATEGIES.containsValue(strategy)