RestoreSnapshotTaskRunner extends AbstractSnapshotTaskRunner { protected String callBridge(RestHttpHelper restHelper, String snapshotURL, String snapshotBody) { log.info("Making RESTORE call to URL {} with body {}", snapshotURL, snapshotBody); try { Map<String, String> headers = new HashMap<>(); headers.put(HttpHeaders.CONTENT_TYPE, "application/json"); RestHttpHelper.HttpResponse response = restHelper.put(snapshotURL, snapshotBody, headers); int statusCode = response.getStatusCode(); if (statusCode != 201) { throw new RuntimeException("Unexpected response code: " + statusCode); } return response.getResponseBody(); } catch (Exception e) { throw new TaskException("Exception encountered attempting to " + "initiate snapshot request. " + "Error reported: " + e.getMessage(), e); } } RestoreSnapshotTaskRunner(StorageProvider snapshotProvider,
                                     SnapshotStorageProvider unwrappedSnapshotProvider,
                                     String dcHost,
                                     String dcPort,
                                     String dcStoreId,
                                     String dcSnapshotUser,
                                     String bridgeAppHost,
                                     String bridgeAppPort,
                                     String bridgeAppUser,
                                     String bridgeAppPass); @Override String getName(); @Override String performTask(String taskParameters);  }
@Test public void testCallBridgeSuccess() throws Exception { String restoreId = "restore-id"; String snapshotURL = "snapshot-url"; String snapshotBody = "snapshot-body"; CreateRestoreBridgeResult bridgeResult = new CreateRestoreBridgeResult(restoreId, RestoreStatus.INITIALIZED); InputStream resultStream = IOUtil.writeStringToStream(bridgeResult.serialize()); RestHttpHelper.HttpResponse response = RestHttpHelper.HttpResponse.buildMock(201, null, resultStream); Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json"); EasyMock.expect(restHelper.put(snapshotURL, snapshotBody, headers)) .andReturn(response); replayMocks(); String callResult = taskRunner.callBridge(restHelper, snapshotURL, snapshotBody); RestoreSnapshotTaskResult taskResult = RestoreSnapshotTaskResult.deserialize(callResult); assertEquals(restoreId, taskResult.getRestoreId()); assertEquals(RestoreStatus.INITIALIZED, taskResult.getStatus()); }
@Test public void testCallBridgeFailure() throws Exception { String snapshotURL = "snapshot-url"; String snapshotBody = "snapshot-body"; InputStream resultStream = IOUtil.writeStringToStream("Error"); RestHttpHelper.HttpResponse response = RestHttpHelper.HttpResponse.buildMock(500, null, resultStream); Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json"); EasyMock.expect(restHelper.put(snapshotURL, snapshotBody, headers)) .andReturn(response); replayMocks(); try { taskRunner.callBridge(restHelper, snapshotURL, snapshotBody); fail("Exception expected on 500 response"); } catch (TaskException e) { } }