Removed TimeUnit from scheduled update supported CF configs
This commit is contained in:
		
							parent
							
								
									7567ac25cf
								
							
						
					
					
						commit
						b54906a9ef
					
				@ -60,6 +60,7 @@ import java.util.Map;
 | 
			
		||||
import java.util.concurrent.ConcurrentHashMap;
 | 
			
		||||
import java.util.concurrent.CopyOnWriteArrayList;
 | 
			
		||||
import java.util.concurrent.ScheduledFuture;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.function.BiConsumer;
 | 
			
		||||
 | 
			
		||||
import static org.thingsboard.server.utils.CalculatedFieldUtils.fromProto;
 | 
			
		||||
@ -453,7 +454,7 @@ public class CalculatedFieldManagerMessageProcessor extends AbstractContextAware
 | 
			
		||||
            log.debug("[{}][{}] Dynamic arguments refresh task for CF already exists!", tenantId, cf.getId());
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        long refreshDynamicSourceInterval = scheduledCfConfig.getTimeUnit().toMillis(scheduledCfConfig.getScheduledUpdateInterval());
 | 
			
		||||
        long refreshDynamicSourceInterval = TimeUnit.SECONDS.toMillis(scheduledCfConfig.getScheduledUpdateInterval());
 | 
			
		||||
        var scheduledMsg = new CalculatedFieldDynamicArgumentsRefreshMsg(tenantId, cfCtx.getCfId());
 | 
			
		||||
 | 
			
		||||
        ScheduledFuture<?> scheduledFuture = systemContext
 | 
			
		||||
 | 
			
		||||
@ -326,8 +326,7 @@ public class CalculatedFieldCtx {
 | 
			
		||||
                && other.calculatedField.getConfiguration() instanceof ScheduledUpdateSupportedCalculatedFieldConfiguration otherConfig) {
 | 
			
		||||
            boolean refreshTriggerChanged = thisConfig.isScheduledUpdateEnabled() != otherConfig.isScheduledUpdateEnabled();
 | 
			
		||||
            boolean refreshIntervalChanged = thisConfig.getScheduledUpdateInterval() != otherConfig.getScheduledUpdateInterval();
 | 
			
		||||
            boolean timeUnitChanged = thisConfig.getTimeUnit() != otherConfig.getTimeUnit();
 | 
			
		||||
            return refreshTriggerChanged || refreshIntervalChanged || timeUnitChanged;
 | 
			
		||||
            return refreshTriggerChanged || refreshIntervalChanged;
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -800,7 +800,6 @@ public class CalculatedFieldIntegrationTest extends CalculatedFieldControllerTes
 | 
			
		||||
 | 
			
		||||
        // Enable scheduled refresh with a 6-second interval
 | 
			
		||||
        cfg.setScheduledUpdateInterval(6);
 | 
			
		||||
        cfg.setTimeUnit(TimeUnit.SECONDS);
 | 
			
		||||
 | 
			
		||||
        cf.setConfiguration(cfg);
 | 
			
		||||
        CalculatedField savedCalculatedField = doPost("/api/calculatedField", cf, CalculatedField.class);
 | 
			
		||||
 | 
			
		||||
@ -17,16 +17,8 @@ package org.thingsboard.server.common.data.cf.configuration;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonIgnore;
 | 
			
		||||
 | 
			
		||||
import java.util.EnumSet;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
public interface ScheduledUpdateSupportedCalculatedFieldConfiguration extends CalculatedFieldConfiguration {
 | 
			
		||||
 | 
			
		||||
    Set<TimeUnit> SUPPORTED_TIME_UNITS =
 | 
			
		||||
            EnumSet.of(TimeUnit.SECONDS, TimeUnit.MINUTES, TimeUnit.HOURS);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @JsonIgnore
 | 
			
		||||
    boolean isScheduledUpdateEnabled();
 | 
			
		||||
 | 
			
		||||
@ -34,20 +26,8 @@ public interface ScheduledUpdateSupportedCalculatedFieldConfiguration extends Ca
 | 
			
		||||
 | 
			
		||||
    void setScheduledUpdateInterval(int interval);
 | 
			
		||||
 | 
			
		||||
    TimeUnit getTimeUnit();
 | 
			
		||||
 | 
			
		||||
    void setTimeUnit(TimeUnit timeUnit);
 | 
			
		||||
 | 
			
		||||
    default void validate(long minAllowedScheduledUpdateInterval) {
 | 
			
		||||
        var timeUnit = getTimeUnit();
 | 
			
		||||
        if (timeUnit == null) {
 | 
			
		||||
            throw new IllegalArgumentException("Scheduled update time unit should be specified!");
 | 
			
		||||
        }
 | 
			
		||||
        if (!SUPPORTED_TIME_UNITS.contains(timeUnit)) {
 | 
			
		||||
            throw new IllegalArgumentException("Unsupported scheduled update time unit: " + timeUnit +
 | 
			
		||||
                                               ". Allowed: " + SUPPORTED_TIME_UNITS);
 | 
			
		||||
        }
 | 
			
		||||
        if (timeUnit.toSeconds(getScheduledUpdateInterval()) < minAllowedScheduledUpdateInterval) {
 | 
			
		||||
        if (getScheduledUpdateInterval() < minAllowedScheduledUpdateInterval) {
 | 
			
		||||
            throw new IllegalArgumentException("Scheduled update interval is less than configured " +
 | 
			
		||||
                                               "minimum allowed interval in tenant profile: " + minAllowedScheduledUpdateInterval);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -30,7 +30,6 @@ import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
public class GeofencingCalculatedFieldConfiguration implements ArgumentsBasedCalculatedFieldConfiguration, ScheduledUpdateSupportedCalculatedFieldConfiguration {
 | 
			
		||||
@ -38,7 +37,6 @@ public class GeofencingCalculatedFieldConfiguration implements ArgumentsBasedCal
 | 
			
		||||
    private EntityCoordinates entityCoordinates;
 | 
			
		||||
    private List<ZoneGroupConfiguration> zoneGroups;
 | 
			
		||||
    private int scheduledUpdateInterval;
 | 
			
		||||
    private TimeUnit timeUnit;
 | 
			
		||||
 | 
			
		||||
    private Output output;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -17,8 +17,6 @@ package org.thingsboard.server.common.data.cf.configuration;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
import org.junit.jupiter.api.extension.ExtendWith;
 | 
			
		||||
import org.junit.jupiter.params.ParameterizedTest;
 | 
			
		||||
import org.junit.jupiter.params.provider.EnumSource;
 | 
			
		||||
import org.mockito.junit.jupiter.MockitoExtension;
 | 
			
		||||
import org.thingsboard.server.common.data.cf.configuration.geofencing.GeofencingCalculatedFieldConfiguration;
 | 
			
		||||
 | 
			
		||||
@ -26,39 +24,18 @@ import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThatCode;
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
 | 
			
		||||
import static org.thingsboard.server.common.data.cf.configuration.ScheduledUpdateSupportedCalculatedFieldConfiguration.SUPPORTED_TIME_UNITS;
 | 
			
		||||
 | 
			
		||||
@ExtendWith(MockitoExtension.class)
 | 
			
		||||
class ScheduledUpdateSupportedCalculatedFieldConfigurationTest {
 | 
			
		||||
public class ScheduledUpdateSupportedCalculatedFieldConfigurationTest {
 | 
			
		||||
 | 
			
		||||
    @ParameterizedTest
 | 
			
		||||
    @EnumSource(TimeUnit.class)
 | 
			
		||||
    void validateShouldThrowWhenScheduledUpdateIntervalIsSetButTimeUnitIsNotSupported(TimeUnit timeUnit) {
 | 
			
		||||
    @Test
 | 
			
		||||
    void validateShouldThrowWhenScheduledUpdateIntervalIsSetButTimeUnitIsNotSupported() {
 | 
			
		||||
        int scheduledUpdateInterval = 60;
 | 
			
		||||
        int minAllowedInterval = (int) timeUnit.toSeconds(scheduledUpdateInterval - 1);
 | 
			
		||||
        int minAllowedInterval = scheduledUpdateInterval - 1;
 | 
			
		||||
 | 
			
		||||
        var cfg = new GeofencingCalculatedFieldConfiguration();
 | 
			
		||||
        cfg.setScheduledUpdateInterval(scheduledUpdateInterval);
 | 
			
		||||
        cfg.setTimeUnit(timeUnit);
 | 
			
		||||
 | 
			
		||||
        if (SUPPORTED_TIME_UNITS.contains(timeUnit)) {
 | 
			
		||||
            assertThatCode(() -> cfg.validate(minAllowedInterval)).doesNotThrowAnyException();
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        assertThatThrownBy(() -> cfg.validate(minAllowedInterval))
 | 
			
		||||
                .isInstanceOf(IllegalArgumentException.class)
 | 
			
		||||
                .hasMessage("Unsupported scheduled update time unit: " + timeUnit + ". Allowed: " + SUPPORTED_TIME_UNITS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void validateShouldThrowWhenScheduledUpdateIntervalIsSetButTimeUnitIsNotSpecified() {
 | 
			
		||||
        var cfg = new GeofencingCalculatedFieldConfiguration();
 | 
			
		||||
        cfg.setScheduledUpdateInterval(60);
 | 
			
		||||
        cfg.setTimeUnit(null);
 | 
			
		||||
 | 
			
		||||
        assertThatThrownBy(() -> cfg.validate(0))
 | 
			
		||||
                .isInstanceOf(IllegalArgumentException.class)
 | 
			
		||||
                .hasMessage("Scheduled update time unit should be specified!");
 | 
			
		||||
        assertThatCode(() -> cfg.validate(minAllowedInterval)).doesNotThrowAnyException();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
@ -67,7 +44,6 @@ class ScheduledUpdateSupportedCalculatedFieldConfigurationTest {
 | 
			
		||||
 | 
			
		||||
        var cfg = new GeofencingCalculatedFieldConfiguration();
 | 
			
		||||
        cfg.setScheduledUpdateInterval(1);
 | 
			
		||||
        cfg.setTimeUnit(TimeUnit.HOURS);
 | 
			
		||||
 | 
			
		||||
        assertThatThrownBy(() -> cfg.validate(minAllowedInterval))
 | 
			
		||||
                .isInstanceOf(IllegalArgumentException.class)
 | 
			
		||||
 | 
			
		||||
@ -26,7 +26,6 @@ import org.thingsboard.server.common.data.cf.configuration.ReferencedEntityKey;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThatCode;
 | 
			
		||||
@ -144,7 +143,6 @@ public class GeofencingCalculatedFieldConfigurationTest {
 | 
			
		||||
    @Test
 | 
			
		||||
    void scheduledUpdateEnabledWhenIntervalIsGreaterThanZeroAndDynamicArgumentsPresent() {
 | 
			
		||||
        var cfg = new GeofencingCalculatedFieldConfiguration();
 | 
			
		||||
        cfg.setTimeUnit(TimeUnit.SECONDS);
 | 
			
		||||
        var zoneGroupConfigurationMock = mock(ZoneGroupConfiguration.class);
 | 
			
		||||
        when(zoneGroupConfigurationMock.hasDynamicSource()).thenReturn(true);
 | 
			
		||||
        cfg.setZoneGroups(List.of(zoneGroupConfigurationMock));
 | 
			
		||||
 | 
			
		||||
@ -46,7 +46,6 @@ import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThat;
 | 
			
		||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
 | 
			
		||||
@ -119,7 +118,6 @@ public class CalculatedFieldServiceTest extends AbstractServiceTest {
 | 
			
		||||
 | 
			
		||||
        // Set a scheduled interval to some value
 | 
			
		||||
        cfg.setScheduledUpdateInterval(600);
 | 
			
		||||
        cfg.setTimeUnit(TimeUnit.SECONDS);
 | 
			
		||||
 | 
			
		||||
        // Create & save Calculated Field
 | 
			
		||||
        CalculatedField cf = new CalculatedField();
 | 
			
		||||
@ -170,7 +168,6 @@ public class CalculatedFieldServiceTest extends AbstractServiceTest {
 | 
			
		||||
 | 
			
		||||
        // Enable scheduling with an interval below tenant min
 | 
			
		||||
        cfg.setScheduledUpdateInterval(600);
 | 
			
		||||
        cfg.setTimeUnit(TimeUnit.SECONDS);
 | 
			
		||||
 | 
			
		||||
        // Create & save Calculated Field
 | 
			
		||||
        CalculatedField cf = new CalculatedField();
 | 
			
		||||
@ -254,7 +251,6 @@ public class CalculatedFieldServiceTest extends AbstractServiceTest {
 | 
			
		||||
        // Enable scheduling with an interval greater than tenant min
 | 
			
		||||
        int valueFromConfig = min + 100;
 | 
			
		||||
        cfg.setScheduledUpdateInterval(valueFromConfig);
 | 
			
		||||
        cfg.setTimeUnit(TimeUnit.SECONDS);
 | 
			
		||||
 | 
			
		||||
        // Create & save Calculated Field
 | 
			
		||||
        CalculatedField cf = new CalculatedField();
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user