S3ArtifactStore { public String getLatestPrefix(String pipeline, String stage, String job, String pipelineCounter) { String prefix = String.format("%s/%s/%s/%s.", pipeline, stage, job, pipelineCounter); ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName(bucket) .withPrefix(prefix) .withDelimiter("/"); ObjectListing listing = client.listObjects(listObjectsRequest); if (listing != null) { List<String> commonPrefixes = listing.getCommonPrefixes(); List<String> stageCounters = Lists.map(commonPrefixes, input -> input.replaceAll(prefix, "").replaceAll("/", "")); if (stageCounters.size() > 0) { int maxStageCounter = Integer.valueOf(stageCounters.get(0)); for (int i = 1; i < stageCounters.size(); i++) { int stageCounter = Integer.valueOf(stageCounters.get(i)); if (stageCounter > maxStageCounter) { maxStageCounter = stageCounter; } } return prefix + maxStageCounter; } } return null; } S3ArtifactStore(AmazonS3 client, String bucket);  S3ArtifactStore(GoEnvironment env, String bucket);  S3ArtifactStore(String bucket); void setStorageClass(String storageClass); void put(String from, String to); void put(String from, String to, ObjectMetadata metadata); void put(PutObjectRequest putObjectRequest); String pathString(String pathOnS3); void get(String from, String to); ObjectMetadata getMetadata(String key); void getPrefix(String prefix, String to); boolean bucketExists(); boolean exists(String bucket, String key); RevisionStatus getLatest(Artifact artifact); String getLatestPrefix(String pipeline, String stage, String job, String pipelineCounter); static AmazonS3 getS3client(GoEnvironment env);  }
@Test public void shouldReturnNullWhenObjectListingIsNull() { doReturn(null).when(mockClient).listObjects(any(ListObjectsRequest.class)); S3ArtifactStore store = new S3ArtifactStore(mockClient, "foo-bar"); String prefix = store.getLatestPrefix("pipeline", "stage", "job", "1"); assertNull(prefix); }
@Test public void shouldReturnNullWhenObjectListingIsSize0() { ObjectListing listing = new ObjectListing(); doReturn(listing).when(mockClient).listObjects(any(ListObjectsRequest.class)); S3ArtifactStore store = new S3ArtifactStore(mockClient, "foo-bar"); String prefix = store.getLatestPrefix("pipeline", "stage", "job", "1"); assertNull(prefix); }
@Test public void shouldReturnTheLatestStageCounter() { ObjectListing listing = new ObjectListing(); List<String> commonPrefixes = new ArrayList<>(); commonPrefixes.add("pipeline/stage/job/1.2"); commonPrefixes.add("pipeline/stage/job/1.1"); commonPrefixes.add("pipeline/stage/job/1.7"); listing.setCommonPrefixes(commonPrefixes); doReturn(listing).when(mockClient).listObjects(any(ListObjectsRequest.class)); S3ArtifactStore store = new S3ArtifactStore(mockClient, "foo-bar"); String prefix = store.getLatestPrefix("pipeline", "stage", "job", "1"); assertEquals("pipeline/stage/job/1.7", prefix); }