CreateSnapshotTaskRunner extends SpaceModifyingSnapshotTaskRunner { protected String callBridge(RestHttpHelper restHelper, String snapshotURL, String snapshotBody) throws Exception { log.info("Making SNAPSHOT call to URL {} with body {}", snapshotURL, snapshotBody); 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 != 200 && statusCode != 201) { String responseStr = response.getResponseBody(); try { String m = getMessageValue(responseStr); if (m != null) { responseStr = m; } } catch (IOException ex) { log.warn(ex.getMessage(), ex); } if (statusCode == 409) { throw new ServerConflictException(responseStr); } else { throw new RuntimeException(responseStr + " (" + statusCode + ")"); } } return response.getResponseBody(); } CreateSnapshotTaskRunner(StorageProvider snapshotProvider,
                                    SnapshotStorageProvider unwrappedSnapshotProvider,
                                    String dcHost,
                                    String dcPort,
                                    String dcStoreId,
                                    String dcAccountName,
                                    String dcSnapshotUser,
                                    String bridgeAppHost,
                                    String bridgeAppPort,
                                    String bridgeAppUser,
                                    String bridgeAppPass,
                                    String bridgeMemberId); @Override String getName(); @Override String performTask(String taskParameters);  }
@Test public void testCallBridgeSuccess() throws Exception { String snapshotId = "snapshot-id"; String snapshotURL = "snapshot-url"; String snapshotBody = "snapshot-body"; CreateSnapshotBridgeResult bridgeResult = new CreateSnapshotBridgeResult(snapshotId, SnapshotStatus.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); CreateSnapshotTaskResult taskResult = CreateSnapshotTaskResult.deserialize(callResult); assertEquals(snapshotId, taskResult.getSnapshotId()); assertEquals(SnapshotStatus.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 (RuntimeException e) { } }
@Test public void testCallBridgeFailureConflict() throws Exception { String snapshotURL = "snapshot-url"; String snapshotBody = "snapshot-body"; String errorMessage = "Error"; InputStream resultStream = IOUtil.writeStringToStream(errorMessage); RestHttpHelper.HttpResponse response = RestHttpHelper.HttpResponse.buildMock(409, 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 409 response"); } catch (ServerConflictException e) { assertEquals(errorMessage, e.getMessage()); } }