CreateSnapshotTaskRunner extends SpaceModifyingSnapshotTaskRunner { @Override public String performTask(String taskParameters) { log.info("Performing SNAPSHOT task with parameters, " + "DuraCloud Host: {} DuraCloud Port: {} DuraCloud StoreID: {} " + "Account Name: {} DuraCloud Snapshot User: {} Bridge Host: {} " + "Bridge Port: {} Bridge User: {} Member Id: {}", dcHost, dcPort, dcStoreId, dcAccountName, getSnapshotUser(), getBridgeAppHost(), getBridgeAppPort(), getBridgeAppUser(), bridgeMemberId); CreateSnapshotTaskParameters taskParams = CreateSnapshotTaskParameters.deserialize(taskParameters); String spaceId = taskParams.getSpaceId(); String snapshotId = getSnapshotIdFromProperties(spaceId); if (snapshotId != null) { throw new StorageStateException( MessageFormat.format("A snapshot ({0}) + is already underway for this space ({1})", snapshotId, spaceId), null); } long now = System.currentTimeMillis(); snapshotId = generateSnapshotId(spaceId, now); Map<String, String> snapshotProps = new HashMap<>(); snapshotProps.put("duracloud-host", dcHost); snapshotProps.put("duracloud-space-id", spaceId); snapshotProps.put("duracloud-store-id", dcStoreId); snapshotProps.put(Constants.SNAPSHOT_ID_PROP, snapshotId); snapshotProps.put("snapshot-date", DateUtil.convertToStringVerbose(now)); snapshotProps.put("owner-id", dcAccountName); snapshotProps.put("description", taskParams.getDescription()); snapshotProps.put("user-email", taskParams.getUserEmail()); snapshotProps.put("member-id", bridgeMemberId); String serializedProps = buildSnapshotProps(snapshotProps); storeSnapshotProps(spaceId, serializedProps); addSnapshotIdToSpaceProps(spaceId, snapshotId); setSnapshotUserPermissions(spaceId); String snapshotURL = buildSnapshotURL(snapshotId); String callResult; try { String snapshotBody = buildSnapshotBody(taskParams); callResult = callBridge(createRestHelper(), snapshotURL, snapshotBody); } catch (Exception e) { try { removeSnapshotProps(spaceId); removeSnapshotIdFromSpaceProps(spaceId); } catch (Exception ex) { log.error("Failed to fully clean up snapshot props for " + spaceId + ": " + ex.getMessage(), ex); } if (!(e instanceof TaskException)) { throw new TaskException(e.getMessage()); } else { throw (TaskException) e; } } CreateSnapshotBridgeResult bridgeResult = CreateSnapshotBridgeResult.deserialize(callResult); log.info("SNAPSHOT created with ID {} and status {}", bridgeResult.getSnapshotId(), bridgeResult.getStatus()); return callResult; } 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 testPerformSnapshotPropertiesAlreadyExists() throws Exception { String spaceId = "space-id"; CreateSnapshotTaskParameters params = new CreateSnapshotTaskParameters(); params.setDescription("desc"); params.setSpaceId(spaceId); params.setUserEmail("test@duracloud.org"); String snapshotId = "snapshot-001"; Properties props = new Properties(); props.put(Constants.SNAPSHOT_ID_PROP, snapshotId); RetrievedContent content = new RetrievedContent(); content.setContentStream(new ByteArrayInputStream( (Constants.SNAPSHOT_ID_PROP + "=" + snapshotId).getBytes())); EasyMock.expect(this.snapshotProvider.getContent(spaceId, Constants.SNAPSHOT_PROPS_FILENAME)) .andReturn(content); replayMocks(); try { taskRunner.performTask(params.serialize()); fail("Exception expected on 500 response"); } catch (StorageStateException e) { assertTrue(e.getMessage().contains(snapshotId)); } }