Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle /peripheral endpoint error when bad handscanner connected #35

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ public DeviceHealth findDevStatus(String devName) {
switch (devName){
case "flatbedscanner":
if(deviceAvailabilitySingleton.getScannerManager() != null) {
healthStatus = deviceAvailabilitySingleton.getScannerManager().getStatus().get(0).getHealthStatus();
healthStatus = deviceAvailabilitySingleton.getScannerManager().getScannerHealthStatus("FLATBED");
} else {
LOGGER.trace("Failed to Connect to " + devName);
}
break;
case "handscanner":
if(deviceAvailabilitySingleton.getScannerManager() != null) {
healthStatus = deviceAvailabilitySingleton.getScannerManager().getStatus().get(1).getHealthStatus();
healthStatus = deviceAvailabilitySingleton.getScannerManager().getScannerHealthStatus("HANDHELD");
} else {
LOGGER.trace("Failed to Connect to " + devName);
}
Expand Down Expand Up @@ -150,7 +150,6 @@ public DeviceHealth findDevStatus(String devName) {
return healthStatus;
}


public void subscribeToDeviceError(SseEmitter sseEmitter) throws IOException {
sseEmitter.onCompletion(() -> this.deviceErrorClientList.remove(sseEmitter));
sseEmitter.onTimeout(() -> this.deviceErrorClientList.remove(sseEmitter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ public List<DeviceHealthResponse> getStatus() {
}
}

public DeviceHealth getScannerHealthStatus(String scannerName) {
for(DeviceHealthResponse deviceHealthResponse: getStatus()) {
if(deviceHealthResponse.getDeviceName().equals(scannerName)) {
return deviceHealthResponse.getHealthStatus();
}
}
return new DeviceHealthResponse(scannerName, DeviceHealth.NOTREADY).getHealthStatus();
}

private void disableScanners() throws InterruptedException {
LOGGER.trace("disableScanners(in)");
List<Callable<Void>> taskList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,29 @@ void Test_findDevStatus_Scale() {
@Test
void Test_findDevStatus_FlatbedScanner() {
//arrange
List<DeviceHealthResponse> devReady = new ArrayList<>();
devReady.add(new DeviceHealthResponse("Flatbed", DeviceHealth.READY));
devReady.add(new DeviceHealthResponse("HandScanner", DeviceHealth.READY));
DeviceHealth expected = new DeviceHealthResponse("FLATBED", DeviceHealth.NOTREADY).getHealthStatus();
deviceAvailabilitySingleton.setScannerManager(mockScannerManager);
when(deviceAvailabilitySingleton.getScannerManager().getScannerHealthStatus("FLATBED")).thenReturn(expected);

//act
deviceAvailabilitySingleton.setScannerManager(mockScannerManager);
DeviceHealth actual = deviceAvailabilityService.findDevStatus("flatbedscanner");

//assert
when(deviceAvailabilitySingleton.getScannerManager().getStatus()).thenReturn(devReady);
assertEquals(DeviceHealth.READY, deviceAvailabilityService.findDevStatus("flatbedscanner"));
assertEquals(expected, actual);
}

@Test
void Test_findDevStatus_HandScanner() {
//arrange
List<DeviceHealthResponse> devReady = new ArrayList<>();
devReady.add(new DeviceHealthResponse("Flatbed", DeviceHealth.READY));
devReady.add(new DeviceHealthResponse("HandScanner", DeviceHealth.READY));
DeviceHealth expected = new DeviceHealthResponse("HANDHELD", DeviceHealth.READY).getHealthStatus();
deviceAvailabilitySingleton.setScannerManager(mockScannerManager);
when(deviceAvailabilitySingleton.getScannerManager().getScannerHealthStatus("HANDHELD")).thenReturn(expected);

//act
deviceAvailabilitySingleton.setScannerManager(mockScannerManager);
DeviceHealth actual = deviceAvailabilityService.findDevStatus("handscanner");

//assert
when(deviceAvailabilitySingleton.getScannerManager().getStatus()).thenReturn(devReady);
assertEquals(DeviceHealth.READY, deviceAvailabilityService.findDevStatus("handscanner"));
assertEquals(expected, actual);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
Expand Down Expand Up @@ -108,7 +109,7 @@ public void testInitialize() {
reconnectResults = new ArrayList<>();
reconnectResults.add(mockFuture);
scannerManager = new ScannerManager(scannerDevices, mockScannerLock);
scannerManagerCache = new ScannerManager(scannerDevices, mockScannerLock, mockCacheManager, mockExecutor, reconnectResults, true);
scannerManagerCache = Mockito.spy(new ScannerManager(scannerDevices, mockScannerLock, mockCacheManager, mockExecutor, reconnectResults, true));
}

@Test
Expand Down Expand Up @@ -555,4 +556,48 @@ public void getStatus_WhenCacheNull_WhenDeviceOnline() {
//assert
assertEquals(expectedList.toString(), deviceHealthResponseList.toString());
}

@Test
void getScannerHealthStatus_FlatbedScanner() {
//arrange
List<DeviceHealthResponse> devReady = new ArrayList<>();
devReady.add(new DeviceHealthResponse("FLATBED", DeviceHealth.READY));
devReady.add(new DeviceHealthResponse("HANDHELD", DeviceHealth.READY));
Mockito.doReturn(devReady).when(scannerManagerCache).getStatus();

//act
DeviceHealth actual = scannerManagerCache.getScannerHealthStatus("HANDHELD");

//assert
assertEquals(DeviceHealth.READY, actual);
}

@Test
void getScannerHealthStatus_HandScanner() {
//arrange
List<DeviceHealthResponse> devReady = new ArrayList<>();
devReady.add(new DeviceHealthResponse("FLATBED", DeviceHealth.READY));
devReady.add(new DeviceHealthResponse("HANDHELD", DeviceHealth.READY));
Mockito.doReturn(devReady).when(scannerManagerCache).getStatus();

//act
DeviceHealth actual = scannerManagerCache.getScannerHealthStatus("HANDHELD");

//assert
assertEquals(DeviceHealth.READY, actual);
}

@Test
void getScannerHealthStatus_HandheldScanner_missing() {
//arrange
List<DeviceHealthResponse> devReady = new ArrayList<>();
devReady.add(new DeviceHealthResponse("FLATBED", DeviceHealth.READY));
Mockito.doReturn(devReady).when(scannerManagerCache).getStatus();

//act
DeviceHealth actual = scannerManagerCache.getScannerHealthStatus("HANDHELD");

//assert
assertEquals(DeviceHealth.NOTREADY, actual);
}
}