Refactor first activity strategy and add tests for all strategies
This commit is contained in:
		
							parent
							
								
									315202d9df
								
							
						
					
					
						commit
						8124192441
					
				@ -167,6 +167,7 @@ public abstract class AbstractActivityManager<Key, Metadata> implements Activity
 | 
			
		||||
            if (hasExpired) {
 | 
			
		||||
                states.remove(key);
 | 
			
		||||
                onStateExpire(key, metadata);
 | 
			
		||||
                shouldReport = true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (shouldReport && lastReportedTime < lastRecordedTime) {
 | 
			
		||||
 | 
			
		||||
@ -45,9 +45,6 @@ public class FirstEventActivityStrategy implements ActivityStrategy {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public synchronized boolean onReportingPeriodEnd() {
 | 
			
		||||
        if (!firstEventReceived) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        firstEventReceived = false;
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,70 @@
 | 
			
		||||
/**
 | 
			
		||||
 * ThingsBoard, Inc. ("COMPANY") CONFIDENTIAL
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright © 2016-2023 ThingsBoard, Inc. All Rights Reserved.
 | 
			
		||||
 *
 | 
			
		||||
 * NOTICE: All information contained herein is, and remains
 | 
			
		||||
 * the property of ThingsBoard, Inc. and its suppliers,
 | 
			
		||||
 * if any.  The intellectual and technical concepts contained
 | 
			
		||||
 * herein are proprietary to ThingsBoard, Inc.
 | 
			
		||||
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 | 
			
		||||
 * patents in process, and are protected by trade secret or copyright law.
 | 
			
		||||
 *
 | 
			
		||||
 * Dissemination of this information or reproduction of this material is strictly forbidden
 | 
			
		||||
 * unless prior written permission is obtained from COMPANY.
 | 
			
		||||
 *
 | 
			
		||||
 * Access to the source code contained herein is hereby forbidden to anyone except current COMPANY employees,
 | 
			
		||||
 * managers or contractors who have executed Confidentiality and Non-disclosure agreements
 | 
			
		||||
 * explicitly covering such access.
 | 
			
		||||
 *
 | 
			
		||||
 * The copyright notice above does not evidence any actual or intended publication
 | 
			
		||||
 * or disclosure  of  this source code, which includes
 | 
			
		||||
 * information that is confidential and/or proprietary, and is a trade secret, of  COMPANY.
 | 
			
		||||
 * ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC  PERFORMANCE,
 | 
			
		||||
 * OR PUBLIC DISPLAY OF OR THROUGH USE  OF THIS  SOURCE CODE  WITHOUT
 | 
			
		||||
 * THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED,
 | 
			
		||||
 * AND IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES.
 | 
			
		||||
 * THE RECEIPT OR POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION
 | 
			
		||||
 * DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS,
 | 
			
		||||
 * OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT  MAY DESCRIBE, IN WHOLE OR IN PART.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.common.transport.activity.strategy;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertThrows;
 | 
			
		||||
 | 
			
		||||
public class ActivityStrategyFactoryTest {
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCreateAllEventsStrategy() {
 | 
			
		||||
        ActivityStrategy strategy = ActivityStrategyFactory.createStrategy("ALL");
 | 
			
		||||
        assertInstanceOf(AllEventsActivityStrategy.class, strategy, "Should return an instance of AllEventsActivityStrategy.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCreateFirstEventStrategy() {
 | 
			
		||||
        ActivityStrategy strategy = ActivityStrategyFactory.createStrategy("FIRST");
 | 
			
		||||
        assertInstanceOf(FirstEventActivityStrategy.class, strategy, "Should return an instance of FirstEventActivityStrategy.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCreateLastEventStrategy() {
 | 
			
		||||
        ActivityStrategy strategy = ActivityStrategyFactory.createStrategy("LAST");
 | 
			
		||||
        assertInstanceOf(LastEventActivityStrategy.class, strategy, "Should return an instance of LastEventActivityStrategy.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCreateFirstAndLastEventStrategy() {
 | 
			
		||||
        ActivityStrategy strategy = ActivityStrategyFactory.createStrategy("FIRST_AND_LAST");
 | 
			
		||||
        assertInstanceOf(FirstAndLastEventActivityStrategy.class, strategy, "Should return an instance of FirstAndLastEventActivityStrategy.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCreateUnknownStrategy() {
 | 
			
		||||
        assertThrows(IllegalArgumentException.class, () -> ActivityStrategyFactory.createStrategy("UNKNOWN"),
 | 
			
		||||
                "Should throw IllegalArgumentException for unknown strategy names.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,57 @@
 | 
			
		||||
/**
 | 
			
		||||
 * ThingsBoard, Inc. ("COMPANY") CONFIDENTIAL
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright © 2016-2023 ThingsBoard, Inc. All Rights Reserved.
 | 
			
		||||
 *
 | 
			
		||||
 * NOTICE: All information contained herein is, and remains
 | 
			
		||||
 * the property of ThingsBoard, Inc. and its suppliers,
 | 
			
		||||
 * if any.  The intellectual and technical concepts contained
 | 
			
		||||
 * herein are proprietary to ThingsBoard, Inc.
 | 
			
		||||
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 | 
			
		||||
 * patents in process, and are protected by trade secret or copyright law.
 | 
			
		||||
 *
 | 
			
		||||
 * Dissemination of this information or reproduction of this material is strictly forbidden
 | 
			
		||||
 * unless prior written permission is obtained from COMPANY.
 | 
			
		||||
 *
 | 
			
		||||
 * Access to the source code contained herein is hereby forbidden to anyone except current COMPANY employees,
 | 
			
		||||
 * managers or contractors who have executed Confidentiality and Non-disclosure agreements
 | 
			
		||||
 * explicitly covering such access.
 | 
			
		||||
 *
 | 
			
		||||
 * The copyright notice above does not evidence any actual or intended publication
 | 
			
		||||
 * or disclosure  of  this source code, which includes
 | 
			
		||||
 * information that is confidential and/or proprietary, and is a trade secret, of  COMPANY.
 | 
			
		||||
 * ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC  PERFORMANCE,
 | 
			
		||||
 * OR PUBLIC DISPLAY OF OR THROUGH USE  OF THIS  SOURCE CODE  WITHOUT
 | 
			
		||||
 * THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED,
 | 
			
		||||
 * AND IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES.
 | 
			
		||||
 * THE RECEIPT OR POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION
 | 
			
		||||
 * DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS,
 | 
			
		||||
 * OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT  MAY DESCRIBE, IN WHOLE OR IN PART.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.common.transport.activity.strategy;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.BeforeEach;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertTrue;
 | 
			
		||||
 | 
			
		||||
public class AllEventsActivityStrategyTest {
 | 
			
		||||
 | 
			
		||||
    private AllEventsActivityStrategy strategy;
 | 
			
		||||
 | 
			
		||||
    @BeforeEach
 | 
			
		||||
    public void setUp() {
 | 
			
		||||
        strategy = new AllEventsActivityStrategy();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnActivity() {
 | 
			
		||||
        assertTrue(strategy.onActivity(), "onActivity() should always return true.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnReportingPeriodEnd() {
 | 
			
		||||
        assertTrue(strategy.onReportingPeriodEnd(), "onReportingPeriodEnd() should always return true.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,68 @@
 | 
			
		||||
/**
 | 
			
		||||
 * ThingsBoard, Inc. ("COMPANY") CONFIDENTIAL
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright © 2016-2023 ThingsBoard, Inc. All Rights Reserved.
 | 
			
		||||
 *
 | 
			
		||||
 * NOTICE: All information contained herein is, and remains
 | 
			
		||||
 * the property of ThingsBoard, Inc. and its suppliers,
 | 
			
		||||
 * if any.  The intellectual and technical concepts contained
 | 
			
		||||
 * herein are proprietary to ThingsBoard, Inc.
 | 
			
		||||
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 | 
			
		||||
 * patents in process, and are protected by trade secret or copyright law.
 | 
			
		||||
 *
 | 
			
		||||
 * Dissemination of this information or reproduction of this material is strictly forbidden
 | 
			
		||||
 * unless prior written permission is obtained from COMPANY.
 | 
			
		||||
 *
 | 
			
		||||
 * Access to the source code contained herein is hereby forbidden to anyone except current COMPANY employees,
 | 
			
		||||
 * managers or contractors who have executed Confidentiality and Non-disclosure agreements
 | 
			
		||||
 * explicitly covering such access.
 | 
			
		||||
 *
 | 
			
		||||
 * The copyright notice above does not evidence any actual or intended publication
 | 
			
		||||
 * or disclosure  of  this source code, which includes
 | 
			
		||||
 * information that is confidential and/or proprietary, and is a trade secret, of  COMPANY.
 | 
			
		||||
 * ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC  PERFORMANCE,
 | 
			
		||||
 * OR PUBLIC DISPLAY OF OR THROUGH USE  OF THIS  SOURCE CODE  WITHOUT
 | 
			
		||||
 * THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED,
 | 
			
		||||
 * AND IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES.
 | 
			
		||||
 * THE RECEIPT OR POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION
 | 
			
		||||
 * DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS,
 | 
			
		||||
 * OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT  MAY DESCRIBE, IN WHOLE OR IN PART.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.common.transport.activity.strategy;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.BeforeEach;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertFalse;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertTrue;
 | 
			
		||||
 | 
			
		||||
public class FirstAndLastEventActivityStrategyTest {
 | 
			
		||||
 | 
			
		||||
    private FirstAndLastEventActivityStrategy strategy;
 | 
			
		||||
 | 
			
		||||
    @BeforeEach
 | 
			
		||||
    public void setUp() {
 | 
			
		||||
        strategy = new FirstAndLastEventActivityStrategy();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnActivity_FirstCall() {
 | 
			
		||||
        assertTrue(strategy.onActivity(), "First call of onActivity() should return true.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnActivity_SubsequentCalls() {
 | 
			
		||||
        assertTrue(strategy.onActivity(), "First call of onActivity() should return true.");
 | 
			
		||||
        assertFalse(strategy.onActivity(), "Subsequent calls of onActivity() should return false.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnReportingPeriodEnd() {
 | 
			
		||||
        assertTrue(strategy.onActivity(), "First call of onActivity() should return true.");
 | 
			
		||||
        assertTrue(strategy.onReportingPeriodEnd(), "onReportingPeriodEnd() should always return true.");
 | 
			
		||||
        assertTrue(strategy.onActivity(), "onActivity() should return true after onReportingPeriodEnd() for the next reporting period");
 | 
			
		||||
        assertTrue(strategy.onReportingPeriodEnd(), "onReportingPeriodEnd() should always return true.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,67 @@
 | 
			
		||||
/**
 | 
			
		||||
 * ThingsBoard, Inc. ("COMPANY") CONFIDENTIAL
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright © 2016-2023 ThingsBoard, Inc. All Rights Reserved.
 | 
			
		||||
 *
 | 
			
		||||
 * NOTICE: All information contained herein is, and remains
 | 
			
		||||
 * the property of ThingsBoard, Inc. and its suppliers,
 | 
			
		||||
 * if any.  The intellectual and technical concepts contained
 | 
			
		||||
 * herein are proprietary to ThingsBoard, Inc.
 | 
			
		||||
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 | 
			
		||||
 * patents in process, and are protected by trade secret or copyright law.
 | 
			
		||||
 *
 | 
			
		||||
 * Dissemination of this information or reproduction of this material is strictly forbidden
 | 
			
		||||
 * unless prior written permission is obtained from COMPANY.
 | 
			
		||||
 *
 | 
			
		||||
 * Access to the source code contained herein is hereby forbidden to anyone except current COMPANY employees,
 | 
			
		||||
 * managers or contractors who have executed Confidentiality and Non-disclosure agreements
 | 
			
		||||
 * explicitly covering such access.
 | 
			
		||||
 *
 | 
			
		||||
 * The copyright notice above does not evidence any actual or intended publication
 | 
			
		||||
 * or disclosure  of  this source code, which includes
 | 
			
		||||
 * information that is confidential and/or proprietary, and is a trade secret, of  COMPANY.
 | 
			
		||||
 * ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC  PERFORMANCE,
 | 
			
		||||
 * OR PUBLIC DISPLAY OF OR THROUGH USE  OF THIS  SOURCE CODE  WITHOUT
 | 
			
		||||
 * THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED,
 | 
			
		||||
 * AND IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES.
 | 
			
		||||
 * THE RECEIPT OR POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION
 | 
			
		||||
 * DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS,
 | 
			
		||||
 * OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT  MAY DESCRIBE, IN WHOLE OR IN PART.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.common.transport.activity.strategy;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.BeforeEach;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertFalse;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertTrue;
 | 
			
		||||
 | 
			
		||||
public class FirstEventActivityStrategyTest {
 | 
			
		||||
 | 
			
		||||
    private FirstEventActivityStrategy strategy;
 | 
			
		||||
 | 
			
		||||
    @BeforeEach
 | 
			
		||||
    public void setUp() {
 | 
			
		||||
        strategy = new FirstEventActivityStrategy();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnActivity_FirstCall() {
 | 
			
		||||
        assertTrue(strategy.onActivity(), "First call of onActivity() should return true.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnActivity_SubsequentCalls() {
 | 
			
		||||
        assertTrue(strategy.onActivity(), "First call of onActivity() should return true.");
 | 
			
		||||
        assertFalse(strategy.onActivity(), "Subsequent calls of onActivity() should return false.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnReportingPeriodEnd() {
 | 
			
		||||
        assertTrue(strategy.onActivity(), "First call of onActivity() should return true.");
 | 
			
		||||
        assertFalse(strategy.onReportingPeriodEnd(), "onReportingPeriodEnd() should always return false.");
 | 
			
		||||
        assertTrue(strategy.onActivity(), "onActivity() should return true after onReportingPeriodEnd().");
 | 
			
		||||
        assertFalse(strategy.onReportingPeriodEnd(), "onReportingPeriodEnd() should always return false.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,58 @@
 | 
			
		||||
/**
 | 
			
		||||
 * ThingsBoard, Inc. ("COMPANY") CONFIDENTIAL
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright © 2016-2023 ThingsBoard, Inc. All Rights Reserved.
 | 
			
		||||
 *
 | 
			
		||||
 * NOTICE: All information contained herein is, and remains
 | 
			
		||||
 * the property of ThingsBoard, Inc. and its suppliers,
 | 
			
		||||
 * if any.  The intellectual and technical concepts contained
 | 
			
		||||
 * herein are proprietary to ThingsBoard, Inc.
 | 
			
		||||
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 | 
			
		||||
 * patents in process, and are protected by trade secret or copyright law.
 | 
			
		||||
 *
 | 
			
		||||
 * Dissemination of this information or reproduction of this material is strictly forbidden
 | 
			
		||||
 * unless prior written permission is obtained from COMPANY.
 | 
			
		||||
 *
 | 
			
		||||
 * Access to the source code contained herein is hereby forbidden to anyone except current COMPANY employees,
 | 
			
		||||
 * managers or contractors who have executed Confidentiality and Non-disclosure agreements
 | 
			
		||||
 * explicitly covering such access.
 | 
			
		||||
 *
 | 
			
		||||
 * The copyright notice above does not evidence any actual or intended publication
 | 
			
		||||
 * or disclosure  of  this source code, which includes
 | 
			
		||||
 * information that is confidential and/or proprietary, and is a trade secret, of  COMPANY.
 | 
			
		||||
 * ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC  PERFORMANCE,
 | 
			
		||||
 * OR PUBLIC DISPLAY OF OR THROUGH USE  OF THIS  SOURCE CODE  WITHOUT
 | 
			
		||||
 * THE EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED,
 | 
			
		||||
 * AND IN VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES.
 | 
			
		||||
 * THE RECEIPT OR POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION
 | 
			
		||||
 * DOES NOT CONVEY OR IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS,
 | 
			
		||||
 * OR TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT  MAY DESCRIBE, IN WHOLE OR IN PART.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.common.transport.activity.strategy;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.BeforeEach;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertFalse;
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertTrue;
 | 
			
		||||
 | 
			
		||||
public class LastEventActivityStrategyTest {
 | 
			
		||||
 | 
			
		||||
    private LastEventActivityStrategy strategy;
 | 
			
		||||
 | 
			
		||||
    @BeforeEach
 | 
			
		||||
    public void setUp() {
 | 
			
		||||
        strategy = new LastEventActivityStrategy();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnActivity() {
 | 
			
		||||
        assertFalse(strategy.onActivity(), "onActivity() should always return false.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testOnReportingPeriodEnd() {
 | 
			
		||||
        assertTrue(strategy.onReportingPeriodEnd(), "onReportingPeriodEnd() should always return true.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user