GetSnapshotContentsTaskRunner extends AbstractSnapshotTaskRunner { protected String callBridge(RestHttpHelper restHelper, String bridgeURL) { log.info("Making bridge call to get snapshot contents. 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 contents. " + "Error reported: " + e.getMessage(), e); } } GetSnapshotContentsTaskRunner(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 contentId = "contentId"; String propName = "prop-name"; String propValue = "prop-value"; Map<String, String> contentProps = new HashMap<>(); contentProps.put(propName, propValue); SnapshotContentItem contentItem = new SnapshotContentItem(); contentItem.setContentId(contentId); contentItem.setContentProperties(contentProps); List<SnapshotContentItem> contentItems = new ArrayList<>(); contentItems.add(contentItem); GetSnapshotContentBridgeResult bridgeResult = new GetSnapshotContentBridgeResult(); bridgeResult.setContentItems(contentItems); 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); GetSnapshotContentBridgeResult taskResult = GetSnapshotContentBridgeResult.deserialize(callResult); SnapshotContentItem item = taskResult.getContentItems().get(0); assertEquals(contentId, item.getContentId()); assertEquals(propValue, item.getContentProperties().get(propName)); }
@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) { } }