Skip to content

Commit

Permalink
Merge pull request #43 from target/PNA-2106
Browse files Browse the repository at this point in the history
Error Handling for Cache
  • Loading branch information
bmcecilia3 committed Jun 12, 2024
2 parents 1b4cb0a + 721e8cb commit 78664f9
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,28 @@ public DeviceHealthResponse getHealth() {
} else {
deviceHealthResponse = new DeviceHealthResponse(cashDrawerDevice.getDeviceName(), DeviceHealth.NOTREADY);
}
Objects.requireNonNull(cacheManager.getCache("cashDrawerHealth")).put("health", deviceHealthResponse);
try {
Objects.requireNonNull(cacheManager.getCache("cashDrawerHealth")).put("health", deviceHealthResponse);
} catch (Exception exception) {
LOGGER.error("getCache(cashDrawerHealth) Failed: " + exception.getMessage());
}
return deviceHealthResponse;
}

public DeviceHealthResponse getStatus() {
if (Objects.requireNonNull(cacheManager.getCache("cashDrawerHealth")).get("health") != null) {
if(connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
try {
if (cacheManager != null && Objects.requireNonNull(cacheManager.getCache("cashDrawerHealth")).get("health") != null) {
if (connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("cashDrawerHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("cashDrawerHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
}
catch (Exception exception) {
return getHealth();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,27 @@ public DeviceHealthResponse getHealth() {
} else {
deviceHealthResponse = new DeviceHealthResponse(micrDevice.getDeviceName(), DeviceHealth.NOTREADY);
}
Objects.requireNonNull(cacheManager.getCache("micrHealth")).put("health", deviceHealthResponse);
try {
Objects.requireNonNull(cacheManager.getCache("micrHealth")).put("health", deviceHealthResponse);
} catch (Exception exception) {
LOGGER.error("getCache(micrHealth) Failed: " + exception.getMessage());
}
return deviceHealthResponse;
}

public DeviceHealthResponse getStatus() {
if(Objects.requireNonNull(cacheManager.getCache("micrHealth")).get("health") != null) {
if(connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
try {
if (cacheManager != null && Objects.requireNonNull(cacheManager.getCache("micrHealth")).get("health") != null) {
if (connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("micrHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("micrHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
} catch (Exception exception) {
return getHealth();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,27 @@ public DeviceHealthResponse getHealth() {
} else {
deviceHealthResponse = new DeviceHealthResponse(lineDisplayDevice.getDeviceName(), DeviceHealth.NOTREADY);
}
Objects.requireNonNull(cacheManager.getCache("lineDisplayHealth")).put("health", deviceHealthResponse);
try {
Objects.requireNonNull(cacheManager.getCache("lineDisplayHealth")).put("health", deviceHealthResponse);
} catch (Exception exception) {
LOGGER.error("getCache(lineDisplayHealth) Failed: " + exception.getMessage());
}
return deviceHealthResponse;
}

public DeviceHealthResponse getStatus() {
if(Objects.requireNonNull(cacheManager.getCache("lineDisplayHealth")).get("health") != null) {
if(connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
try {
if (cacheManager != null && Objects.requireNonNull(cacheManager.getCache("lineDisplayHealth")).get("health") != null) {
if (connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("lineDisplayHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("lineDisplayHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
} catch (Exception exception) {
return getHealth();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,27 @@ public DeviceHealthResponse getHealth() {
} else {
deviceHealthResponse = new DeviceHealthResponse(printerDevice.getDeviceName(), DeviceHealth.NOTREADY);
}
Objects.requireNonNull(cacheManager.getCache("printerHealth")).put("health", deviceHealthResponse);
try {
Objects.requireNonNull(cacheManager.getCache("printerHealth")).put("health", deviceHealthResponse);
} catch (Exception exception) {
LOGGER.error("getCache(printerHealth) Failed: " + exception.getMessage());
}
return deviceHealthResponse;
}

public DeviceHealthResponse getStatus() {
if(Objects.requireNonNull(cacheManager.getCache("printerHealth")).get("health") != null) {
if(connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
try {
if (cacheManager != null && Objects.requireNonNull(cacheManager.getCache("printerHealth")).get("health") != null) {
if (connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("printerHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("printerHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
} catch (Exception exception) {
return getHealth();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,27 @@ public DeviceHealthResponse getHealth() {
} else {
deviceHealthResponse = new DeviceHealthResponse(scaleDevice.getDeviceName(), DeviceHealth.NOTREADY);
}
Objects.requireNonNull(cacheManager.getCache("scaleHealth")).put("health", deviceHealthResponse);
try {
Objects.requireNonNull(cacheManager.getCache("scaleHealth")).put("health", deviceHealthResponse);
} catch (Exception exception) {
LOGGER.error("getCache(scaleHealth) Failed: " + exception.getMessage());
}
return deviceHealthResponse;
}

public DeviceHealthResponse getStatus() {
if(Objects.requireNonNull(cacheManager.getCache("scaleHealth")).get("health") != null) {
if(connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
try {
if (cacheManager != null && Objects.requireNonNull(cacheManager.getCache("scaleHealth")).get("health") != null) {
if (connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("scaleHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
return getHealth();
}
return (DeviceHealthResponse) Objects.requireNonNull(cacheManager.getCache("scaleHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
} catch (Exception exception) {
return getHealth();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,28 @@ public List<DeviceHealthResponse> getHealth(ScannerType scannerType) {
}
}
}
Objects.requireNonNull(cacheManager.getCache("scannerHealth")).put("health", response);
try {
Objects.requireNonNull(cacheManager.getCache("scannerHealth")).put("health", response);
} catch (Exception exception) {
LOGGER.error("getCache(scannerHealth) Failed: " + exception.getMessage());
}
LOGGER.trace("getHealth(out)");
return response;
}

public List<DeviceHealthResponse> getStatus() {
if(Objects.requireNonNull(cacheManager.getCache("scannerHealth")).get("health") != null) {
if(connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
try {
if (cacheManager != null && Objects.requireNonNull(cacheManager.getCache("scannerHealth")).get("health") != null) {
if (connectStatus == ConnectEnum.CHECK_HEALTH) {
connectStatus = ConnectEnum.HEALTH_UPDATED;
return getHealth(ScannerType.BOTH);
}
return (List<DeviceHealthResponse>) Objects.requireNonNull(cacheManager.getCache("scannerHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
return getHealth(ScannerType.BOTH);
}
return (List<DeviceHealthResponse>) Objects.requireNonNull(cacheManager.getCache("scannerHealth")).get("health").get();
} else {
LOGGER.debug("Not able to retrieve from cache, checking getHealth()");
} catch (Exception exception) {
return getHealth(ScannerType.BOTH);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,22 @@ public void getHealth_WhenDeviceOnline_ShouldReturnReadyHealthResponse() {
assertEquals(expected.toString(), testCache.get("health").get().toString());
}

@Test
public void getHealth_WhenCacheFails_ShouldReturnReadyHealthResponse() {
//arrange
when(mockCashDrawerDevice.isConnected()).thenReturn(true);
when(mockCashDrawerDevice.getDeviceName()).thenReturn("cashDrawer");
when(mockCacheManager.getCache("cashDrawerHealth")).thenReturn(null);
DeviceHealthResponse expected = new DeviceHealthResponse("cashDrawer", DeviceHealth.READY);

//act
DeviceHealthResponse deviceHealthResponse = cashDrawerManagerCache.getHealth();

//assert
assertEquals("cashDrawer", deviceHealthResponse.getDeviceName());
assertEquals(DeviceHealth.READY, deviceHealthResponse.getHealthStatus());
}

@Test
public void getStatus_WhenCacheExists() {
//arrange
Expand Down Expand Up @@ -392,4 +408,19 @@ public void getStatus_WhenCacheNull_WhenDeviceOnline() {
//assert
assertEquals(expected.toString(), deviceHealthResponse.toString());
}
}

@Test
public void getStatus_WhenCacheNull_CallHealth() {
//arrange
when(mockCashDrawerDevice.isConnected()).thenReturn(true);
when(mockCashDrawerDevice.getDeviceName()).thenReturn("cashDrawer");
when(mockCacheManager.getCache("cashDrawerHealth")).thenReturn(null);
DeviceHealthResponse expected = new DeviceHealthResponse("cashDrawer", DeviceHealth.READY);

//act
DeviceHealthResponse deviceHealthResponse = cashDrawerManagerCache.getStatus();

//assert
assertEquals(expected.toString(), deviceHealthResponse.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,22 @@ public void getHealth_WhenDeviceOnline_ShouldReturnReadyHealthResponse() {
assertEquals(expected.toString(), testCache.get("health").get().toString());
}

@Test
public void getHealth_WhenCacheNull_ShouldReturnReadyHealthResponse() {
//arrange
when(mockMicrDevice.isConnected()).thenReturn(true);
when(mockMicrDevice.getDeviceName()).thenReturn("micr");
when(mockCacheManager.getCache("micrHealth")).thenReturn(null);
DeviceHealthResponse expected = new DeviceHealthResponse("micr", DeviceHealth.READY);

//act
DeviceHealthResponse deviceHealthResponse = micrManagerCacheClient.getHealth();

//assert
assertEquals("micr", deviceHealthResponse.getDeviceName());
assertEquals(DeviceHealth.READY, deviceHealthResponse.getHealthStatus());
}

@Test
public void getStatus_WhenCacheExists() {
//arrange
Expand Down Expand Up @@ -454,4 +470,19 @@ public void getStatus_WhenCacheNull_WhenDeviceOnline() {
//assert
assertEquals(expected.toString(), deviceHealthResponse.toString());
}
}

@Test
public void getStatus_WhenCacheNull_CallHealth() {
//arrange
when(mockMicrDevice.isConnected()).thenReturn(true);
when(mockMicrDevice.getDeviceName()).thenReturn("micr");
when(mockCacheManager.getCache("micrHealth")).thenReturn(null);
DeviceHealthResponse expected = new DeviceHealthResponse("micr", DeviceHealth.READY);

//act
DeviceHealthResponse deviceHealthResponse = micrManagerCacheClient.getStatus();

//assert
assertEquals(expected.toString(), deviceHealthResponse.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ public void getHealth_WhenDeviceOnline_ShouldReturnReadyHealthResponse() {
assertEquals(expected.toString(), testCache.get("health").get().toString());
}

@Test
public void getHealth_CacheNull_ShouldReturnReadyHealthResponse() {
//arrange
when(mockLineDisplayDevice.isConnected()).thenReturn(true);
when(mockLineDisplayDevice.getDeviceName()).thenReturn("lineDisplay");
when(mockCacheManager.getCache("lineDisplayHealth")).thenReturn(null);
DeviceHealthResponse expected = new DeviceHealthResponse("lineDisplay", DeviceHealth.READY);

//act
DeviceHealthResponse deviceHealthResponse = lineDisplayManagerCache.getHealth();

//assert
assertEquals("lineDisplay", deviceHealthResponse.getDeviceName());
assertEquals(DeviceHealth.READY, deviceHealthResponse.getHealthStatus());
}

@Test
public void getStatus_WhenCacheExists() {
//arrange
Expand Down Expand Up @@ -365,4 +381,19 @@ public void getStatus_WhenCacheNull_WhenDeviceOnline() {
//assert
assertEquals(expected.toString(), deviceHealthResponse.toString());
}
}

@Test
public void getStatus_WhenCacheNull_CallHealth() {
//arrange
when(mockLineDisplayDevice.isConnected()).thenReturn(true);
when(mockLineDisplayDevice.getDeviceName()).thenReturn("lineDisplay");
when(mockCacheManager.getCache("lineDisplayHealth")).thenReturn(null);
DeviceHealthResponse expected = new DeviceHealthResponse("lineDisplay", DeviceHealth.READY);

//act
DeviceHealthResponse deviceHealthResponse = lineDisplayManagerCache.getStatus();

//assert
assertEquals(expected.toString(), deviceHealthResponse.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,22 @@ public void getHealth_WhenDeviceOnline_ShouldReturnReadyHealthResponse() {
assertEquals(expected.toString(), testCache.get("health").get().toString());
}

@Test
public void getHealth_CacheNull_ShouldReturnReadyHealthResponse() {
//arrange
when(mockPrinterDevice.isConnected()).thenReturn(true);
when(mockPrinterDevice.getDeviceName()).thenReturn("printer");
when(mockCacheManager.getCache("printerHealth")).thenReturn(null);
DeviceHealthResponse expected = new DeviceHealthResponse("printer", DeviceHealth.READY);

//act
DeviceHealthResponse deviceHealthResponse = printerManagerCacheFuture.getHealth();

//assert
assertEquals("printer", deviceHealthResponse.getDeviceName());
assertEquals(DeviceHealth.READY, deviceHealthResponse.getHealthStatus());
}

@Test
public void getStatus_WhenCacheExists() {
//arrange
Expand Down Expand Up @@ -505,4 +521,19 @@ public void getStatus_WhenCacheNull_WhenDeviceOnline() {
//assert
assertEquals(expected.toString(), deviceHealthResponse.toString());
}

@Test
public void getStatus_WhenCacheNull_CallHealth() {
//arrange
when(mockPrinterDevice.isConnected()).thenReturn(true);
when(mockPrinterDevice.getDeviceName()).thenReturn("printer");
when(mockCacheManager.getCache("printerHealth")).thenReturn(null);
DeviceHealthResponse expected = new DeviceHealthResponse("printer", DeviceHealth.READY);

//act
DeviceHealthResponse deviceHealthResponse = printerManagerCacheFuture.getStatus();

//assert
assertEquals(expected.toString(), deviceHealthResponse.toString());
}
}
Loading

0 comments on commit 78664f9

Please sign in to comment.