GetSnapshotHistoryTaskRunner extends AbstractSnapshotTaskRunner { protected String callBridge(RestHttpHelper restHelper, String bridgeURL) { log.info("Making bridge call to get snapshot history. URL: {}", bridgeURL); try { RestHttpHelper.HttpResponse response = restHelper.get(bridgeURL); int statusCode = response.getStatusCode(); if (statusCode != 200) { throw new RuntimeException("Unexpected response code: " + statusCode); } return response.getResponseBody(); } catch (Exception e) { throw new TaskException("Exception encountered attempting to " + "get snapshot history. " + "Error reported: " + e.getMessage(), e); } } GetSnapshotHistoryTaskRunner(String bridgeAppHost,
                                        String bridgeAppPort,
                                        String bridgeAppUser,
                                        String bridgeAppPass); @Override String getName(); @Override String performTask(String taskParameters);  }
@Test public void testCallBridgeSuccess() throws Exception { String bridgeURL = "bridge-url"; String history = "history"; Date historyDate = new Date(); SnapshotHistoryItem historyItem = new SnapshotHistoryItem(); historyItem.setHistory(history); historyItem.setHistoryDate(historyDate); List<SnapshotHistoryItem> historyItems = new ArrayList<>(); historyItems.add(historyItem); GetSnapshotHistoryBridgeResult bridgeResult = new GetSnapshotHistoryBridgeResult(); bridgeResult.setHistoryItems(historyItems); InputStream resultStream = IOUtil.writeStringToStream(bridgeResult.serialize()); RestHttpHelper.HttpResponse response = RestHttpHelper.HttpResponse.buildMock(200, null, resultStream); EasyMock.expect(restHelper.get(bridgeURL)) .andReturn(response); replayMocks(); String callResult = taskRunner.callBridge(restHelper, bridgeURL); GetSnapshotHistoryBridgeResult taskResult = GetSnapshotHistoryBridgeResult.deserialize(callResult); SnapshotHistoryItem item = taskResult.getHistoryItems().get(0); assertEquals(history, item.getHistory()); assertEquals(historyDate, item.getHistoryDate()); }
@Test public void testCallBridgeFailure() throws Exception { String bridgeURL = "bridge-url"; InputStream resultStream = IOUtil.writeStringToStream("Error"); RestHttpHelper.HttpResponse response = RestHttpHelper.HttpResponse.buildMock(500, null, resultStream); EasyMock.expect(restHelper.get(bridgeURL)) .andReturn(response); replayMocks(); try { taskRunner.callBridge(restHelper, bridgeURL); fail("Exception expected on 500 response"); } catch (TaskException e) { } }