Skip to content

Commit

Permalink
#2755 Signaling the data source that has a problem:
Browse files Browse the repository at this point in the history
- fixed generate many events by ModbusDataSource;
- corrected test InitializeDataSourceRtTest;
- refactor InitializeDataSourceRtMockUtils;
  • Loading branch information
Limraj committed Feb 28, 2024
1 parent 1be40ff commit 03eaa31
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ synchronized protected void doPoll(long time) {
// Raise an event.
raiseEvent(DATA_SOURCE_EXCEPTION_EVENT, time, true, new LocalizableMessage("event.pollingError",
messageException.getMessage()));
return;
}
else
// Deactivate any existing event.
Expand All @@ -117,6 +118,7 @@ synchronized protected void doPoll(long time) {
if (pointError != null) {
// Raise an event.
raiseEvent(POINT_READ_EXCEPTION_EVENT, time, true, pointError);
return;
}
else
// Deactivate any existing event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ abstract public class ModbusDataSource extends PollingDataSource implements
public static final int POINT_WRITE_EXCEPTION_EVENT = 2;
public static final int DATA_SOURCE_EXCEPTION_EVENT = 3;
public static final int MONITOR_WRITE_EXCEPTION_EVENT = 4;
public static final int INITIALIZATION_EXCEPTION_EVENT = 5;

private ModbusMaster modbusMaster;
private BatchRead<ModbusPointLocatorRT> batchRead;
Expand Down Expand Up @@ -154,9 +155,9 @@ public void removeDataPoint(DataPointRT dataPoint) {
protected void doPoll(long time) {
try {
checkInitialized(modbusMaster, this);
returnToNormal(DATA_SOURCE_EXCEPTION_EVENT, time);
returnToNormal(INITIALIZATION_EXCEPTION_EVENT, time);
} catch (Throwable e) {
raiseEvent(DATA_SOURCE_EXCEPTION_EVENT, time, true,
raiseEvent(INITIALIZATION_EXCEPTION_EVENT, time, true,
getLocalExceptionMessage(e));
return;
}
Expand Down Expand Up @@ -355,10 +356,10 @@ protected void initialize(ModbusMaster modbusMaster) {
modbusMaster.init();

// Deactivate any existing event.
returnToNormal(DATA_SOURCE_EXCEPTION_EVENT,
returnToNormal(INITIALIZATION_EXCEPTION_EVENT,
System.currentTimeMillis());
} catch (Throwable e) {
raiseEvent(DATA_SOURCE_EXCEPTION_EVENT, System.currentTimeMillis(),
raiseEvent(INITIALIZATION_EXCEPTION_EVENT, System.currentTimeMillis(),
true, getLocalExceptionMessage(e));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ protected void addEventTypes(List<EventTypeVO> ets) {
new LocalizableMessage("event.ds.pointWrite")));
ets.add(createEventType(ModbusDataSource.MONITOR_WRITE_EXCEPTION_EVENT,
new LocalizableMessage("event.ds.monitorWrite")));
ets.add(createEventType(ModbusDataSource.INITIALIZATION_EXCEPTION_EVENT,
new LocalizableMessage("event.ds.initialization")));
}

private static final ExportCodes EVENT_CODES = new ExportCodes();
Expand All @@ -66,6 +68,8 @@ protected void addEventTypes(List<EventTypeVO> ets) {
"POINT_WRITE_EXCEPTION");
EVENT_CODES.addElement(ModbusDataSource.MONITOR_WRITE_EXCEPTION_EVENT,
"MONITOR_WRITE_EXCEPTION");
EVENT_CODES.addElement(ModbusDataSource.INITIALIZATION_EXCEPTION_EVENT,
"INITIALIZATION_EXCEPTION");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
import com.serotonin.mango.rt.RuntimeManager;
import com.serotonin.mango.rt.dataImage.DataPointRT;
import com.serotonin.mango.rt.dataImage.PointValueCache;
import com.serotonin.mango.rt.maint.BackgroundProcessing;
import com.serotonin.mango.vo.DataPointVO;
import com.serotonin.mango.vo.TimePeriodType;
import com.serotonin.mango.vo.dataSource.meta.MetaDataSourceVO;
import com.serotonin.mango.vo.dataSource.meta.MetaPointLocatorVO;
import com.serotonin.mango.vo.dataSource.virtual.VirtualDataSourceVO;
import com.serotonin.mango.vo.dataSource.virtual.VirtualPointLocatorVO;
import com.serotonin.mango.web.ContextWrapper;
import com.serotonin.mango.web.dwr.MiscDwr;
import com.serotonin.web.content.ContentGenerator;
import org.directwebremoting.WebContextFactory;
Expand All @@ -28,23 +26,18 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.scada_lts.dao.SystemSettingsDAO;
import org.scada_lts.mango.service.DataPointService;
import org.scada_lts.mango.service.DataSourceService;
import org.scada_lts.mango.service.PointValueService;
import org.scada_lts.mango.service.SystemSettingsService;
import org.scada_lts.web.beans.ApplicationBeans;
import org.scada_lts.web.ws.services.DataPointServiceWebSocket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.serotonin.mango.rt.dataSource.DataPointUnreliableUtils.*;
import static com.serotonin.mango.rt.dataSource.DataPointUnreliableUtils.resetUnreliableDataPoints;
import static com.serotonin.mango.util.InitializeDataSourceRtMockUtils.reset;
import static com.serotonin.mango.util.InitializeDataSourceRtMockUtils.set;
import static org.mockito.ArgumentMatchers.anyString;
import static org.powermock.api.mockito.PowerMockito.*;
import static com.serotonin.mango.util.InitializeDataSourceRtMockUtils.resetUnreliable;
import static com.serotonin.mango.util.InitializeDataSourceRtMockUtils.setUnreliable;
import static org.mockito.Mockito.mock;
import static utils.mock.RuntimeMockUtils.runtimeManagerMock;

@RunWith(PowerMockRunner.class)
@PrepareForTest({WebContextFactory.class, Common.class, MiscDwr.class, SystemSettingsDAO.class,
Expand Down Expand Up @@ -80,40 +73,7 @@ public class DataPointUnreliableUtilsTest {
@Before
public void configMock() throws Exception {

DataSourceService dataSourceService = mock(DataSourceService.class);
whenNew(DataSourceService.class).withNoArguments().thenReturn(dataSourceService);

DataPointService dataPointService = mock(DataPointService.class);
whenNew(DataPointService.class).withNoArguments().thenReturn(dataPointService);

SystemSettingsService systemSettingsService = mock(SystemSettingsService.class);
whenNew(SystemSettingsService.class).withNoArguments().thenReturn(systemSettingsService);

SystemSettingsDAO systemSettingsDAO = mock(SystemSettingsDAO.class);
whenNew(SystemSettingsDAO.class).withNoArguments().thenReturn(systemSettingsDAO);

mockStatic(SystemSettingsDAO.class);
when(SystemSettingsDAO.getValue(anyString())).thenReturn("");
when(SystemSettingsDAO.getFutureDateLimit()).thenReturn(1L);

PointValueService mangoPointValues = mock(PointValueService.class);
whenNew(PointValueService.class).withNoArguments().thenReturn(mangoPointValues);

mockStatic(ApplicationBeans.class);
DataPointServiceWebSocket dataPointServiceWebSocket = mock(DataPointServiceWebSocket.class);
when(ApplicationBeans.getDataPointServiceWebSocketBean()).thenReturn(dataPointServiceWebSocket);

ContextWrapper contextWrapper = mock(ContextWrapper.class);
EventManager eventManager = mock(EventManager.class);
BackgroundProcessing backgroundProcessing = mock(BackgroundProcessing.class);

runtimeManager = new RuntimeManager();
when(contextWrapper.getRuntimeManager()).thenReturn(runtimeManager);
when(contextWrapper.getEventManager()).thenReturn(eventManager);
when(contextWrapper.getBackgroundProcessing()).thenReturn(backgroundProcessing);

Common.ctx = contextWrapper;
Common.timer.init();
runtimeManagerMock(this.runtimeManager = new RuntimeManager(), mock(EventManager.class));

configData();
}
Expand Down Expand Up @@ -213,7 +173,7 @@ private void configData() {
public void when_setUnreliableDataPoints_with_virtual_points_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);

//when:
setUnreliableDataPoints(virtualDataPoints);
Expand All @@ -228,7 +188,7 @@ public void when_setUnreliableDataPoints_with_virtual_points_then_unreliable_tru
public void when_setUnreliableDataPoints_with_meta_points_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);

//when:
setUnreliableDataPoints(metaDataPoints);
Expand All @@ -243,7 +203,7 @@ public void when_setUnreliableDataPoints_with_meta_points_then_unreliable_true()
public void when_setUnreliableDataPoints_with_meta_points_with_context_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);

//when:
setUnreliableDataPoints(metaDataPointsWithContext);
Expand Down Expand Up @@ -291,7 +251,7 @@ public void when_getRunningDataSource_meta_data_source_with_context_then_initial
public void when_setUnreliableDataPoint_with_virtual_point_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(virtualDataPoint1.getId());

//when:
Expand All @@ -306,7 +266,7 @@ public void when_setUnreliableDataPoint_with_virtual_point_then_unreliable_true(
public void when_setUnreliableDataPoint_with_meta_point_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint1.getId());

//when:
Expand All @@ -321,7 +281,7 @@ public void when_setUnreliableDataPoint_with_meta_point_then_unreliable_true() {
public void when_setUnreliableDataPoint_with_meta_point_with_context_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint6WithVirtualDataPoint1.getId());

//when:
Expand All @@ -336,7 +296,7 @@ public void when_setUnreliableDataPoint_with_meta_point_with_context_then_unreli
public void when_setUnreliableDataPoints_with_virtual_points_then_meta_points_unreliable_false() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);

//when:
setUnreliableDataPoints(virtualDataPoints);
Expand All @@ -351,7 +311,7 @@ public void when_setUnreliableDataPoints_with_virtual_points_then_meta_points_un
public void when_setUnreliableDataPoints_with_virtual_points_from_meta_points_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);

//when:
setUnreliableDataPoints(virtualDataPoints);
Expand All @@ -366,7 +326,7 @@ public void when_setUnreliableDataPoints_with_virtual_points_from_meta_points_th
public void when_setUnreliableDataPoint_with_meta_point_from_context_meta_points_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint10WithVirtualDataPoint1.getId());

//when:
Expand All @@ -382,7 +342,7 @@ public void when_setUnreliableDataPoint_with_meta_point_from_context_meta_points
public void when_setUnreliableDataPoint_with_meta_point_from_context_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint10WithVirtualDataPoint1.getId());

//when:
Expand All @@ -398,7 +358,7 @@ public void when_setUnreliableDataPoint_with_meta_point_from_context_then_unreli
public void when_setUnreliableDataPoints_with_virtual_points_from_context_meta_points_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);

//when:
setUnreliableDataPoints(virtualDataPoints);
Expand All @@ -413,7 +373,7 @@ public void when_setUnreliableDataPoints_with_virtual_points_from_context_meta_p
public void when_setUnreliableDataPoint_with_virtual_point_from_context_meta_points_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(virtualDataPoint1.getId());

//when:
Expand All @@ -429,7 +389,7 @@ public void when_setUnreliableDataPoint_with_virtual_point_from_context_meta_poi
public void when_setUnreliableDataPoint_with_virtual_point_from_context_meta_point_then_unreliable_true() {

//given:
reset(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
resetUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(virtualDataPoint2.getId());
DataPointRT metaDataPointRT = runtimeManager.getDataPoint(metaDataPoint9WithVirtualDataPoint2.getId());

Expand All @@ -445,7 +405,7 @@ public void when_setUnreliableDataPoint_with_virtual_point_from_context_meta_poi
public void when_resetUnreliableDataPoint_with_virtual_point_then_unreliable_false() {

//given:
set(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
setUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(virtualDataPoint1.getId());

//when:
Expand All @@ -460,7 +420,7 @@ public void when_resetUnreliableDataPoint_with_virtual_point_then_unreliable_fal
public void when_resetUnreliableDataPoint_with_meta_point_then_unreliable_false() {

//given:
set(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
setUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint1.getId());

//when:
Expand All @@ -475,7 +435,7 @@ public void when_resetUnreliableDataPoint_with_meta_point_then_unreliable_false(
public void when_resetUnreliableDataPoint_with_meta_point_with_context_then_unreliable_false() {

//given:
set(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
setUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint6WithVirtualDataPoint1.getId());

//when:
Expand All @@ -490,7 +450,7 @@ public void when_resetUnreliableDataPoint_with_meta_point_with_context_then_unre
public void when_resetUnreliableDataPoint_with_meta_point_from_context_meta_point_then_unreliable_false() {

//given:
set(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
setUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint10WithVirtualDataPoint1.getId());

//when:
Expand All @@ -506,7 +466,7 @@ public void when_resetUnreliableDataPoint_with_meta_point_from_context_meta_poin
public void when_resetUnreliableDataPoints_with_meta_point_then_unreliable_false() {

//given:
set(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
setUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint1.getId());

//when:
Expand All @@ -521,7 +481,7 @@ public void when_resetUnreliableDataPoints_with_meta_point_then_unreliable_false
public void when_resetUnreliableDataPoints_with_meta_points_then_unreliable_false() {

//given:
set(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
setUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(metaDataPoint1.getId());

//when:
Expand All @@ -536,7 +496,7 @@ public void when_resetUnreliableDataPoints_with_meta_points_then_unreliable_fals
public void when_resetUnreliableDataPoints_with_virtual_point_from_context_meta_point_then_meta_points_unreliable_false() {

//given:
set(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
setUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(virtualDataPoint1.getId());

//when:
Expand All @@ -552,7 +512,7 @@ public void when_resetUnreliableDataPoints_with_virtual_point_from_context_meta_
public void when_resetUnreliableDataPoints_with_virtual_point_from_context_meta_point_then_meta_point_unreliable_false() {

//given:
set(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
setUnreliable(virtualDataPoints, metaDataPoints, metaDataPointsWithContext);
DataPointRT dataPointRT = runtimeManager.getDataPoint(virtualDataPoint2.getId());
DataPointRT metaDataPointRT = runtimeManager.getDataPoint(metaDataPoint9WithVirtualDataPoint2.getId());

Expand Down
Loading

0 comments on commit 03eaa31

Please sign in to comment.