11package com .salesforce .dockerfileimageupdate .storage ;
22
3+
34import com .amazonaws .services .s3 .AmazonS3 ;
45import com .amazonaws .services .s3 .model .ListObjectsV2Result ;
6+ import com .amazonaws .services .s3 .model .ListObjectsV2Request ;
57import com .amazonaws .services .s3 .model .S3Object ;
68import com .amazonaws .services .s3 .model .S3ObjectInputStream ;
79import com .amazonaws .services .s3 .model .S3ObjectSummary ;
810import com .salesforce .dockerfileimageupdate .utils .DockerfileGitHubUtil ;
911import org .testng .annotations .Test ;
12+ import static org .mockito .ArgumentMatchers .any ;
1013
1114import java .io .ByteArrayInputStream ;
1215import java .io .IOException ;
@@ -36,12 +39,60 @@ public void testUpdateStoreThrowsExceptionWhenBucketDoesNotExist() throws IOExce
3639 verify (amazonS3 , times (0 )).putObject ("store" , "image" , "tag" );
3740 }
3841
42+ @ Test
43+ public void testGetStoreContentReturnsStoreContentWithTruncatedResults () throws InterruptedException {
44+ AmazonS3 amazonS3 = mock (AmazonS3 .class );
45+ S3BackedImageTagStore s3BackedImageTagStore = spy (new S3BackedImageTagStore (amazonS3 , "store" ));
46+ DockerfileGitHubUtil dockerfileGitHubUtil = mock (DockerfileGitHubUtil .class );
47+ ListObjectsV2Result listObjectsV2Result1 = mock (ListObjectsV2Result .class );
48+ ListObjectsV2Result listObjectsV2Result2 = mock (ListObjectsV2Result .class );
49+
50+ S3ObjectSummary s3ObjectSummary = mock (S3ObjectSummary .class );
51+ List <S3ObjectSummary > s3ObjectSummaryList = new ArrayList <>();
52+ s3ObjectSummaryList .add (s3ObjectSummary );
53+
54+ Date date = mock (Date .class );
55+ S3Object s3Object = mock (S3Object .class );
56+ S3Object s3Object2 = mock (S3Object .class );
57+ String tag = "tag" ;
58+ String tag2 = "tag2" ;
59+ byte tagBytes [] = tag .getBytes ();
60+ byte tagBytes2 [] = tag2 .getBytes ();
61+ S3ObjectInputStream objectContent = new S3ObjectInputStream (new ByteArrayInputStream (tagBytes ), null );
62+ S3ObjectInputStream objectContent2 = new S3ObjectInputStream (new ByteArrayInputStream (tagBytes2 ), null );
63+ s3Object .setObjectContent (objectContent );
64+ s3Object2 .setObjectContent (objectContent2 );
65+
66+ when (amazonS3 .listObjectsV2 (any (ListObjectsV2Request .class ))).thenReturn (listObjectsV2Result1 , listObjectsV2Result2 );
67+ when (listObjectsV2Result1 .getObjectSummaries ()).thenReturn (s3ObjectSummaryList );
68+ when (listObjectsV2Result1 .isTruncated ()).thenReturn (true );
69+ when (listObjectsV2Result2 .getObjectSummaries ()).thenReturn (s3ObjectSummaryList );
70+ when (listObjectsV2Result2 .isTruncated ()).thenReturn (false );
71+ when (s3ObjectSummary .getLastModified ()).thenReturn (date , date );
72+ when (s3ObjectSummary .getKey ()).thenReturn ("domain!namespace!image" , "domain!namespace!image2" );
73+ when (amazonS3 .getObject ("store" , "domain!namespace!image" )).thenReturn (s3Object );
74+ when (amazonS3 .getObject ("store" , "domain!namespace!image2" )).thenReturn (s3Object2 );
75+ when (s3Object .getObjectContent ()).thenReturn (objectContent );
76+ when (s3Object2 .getObjectContent ()).thenReturn (objectContent2 );
77+
78+ List <ImageTagStoreContent > actualResult = s3BackedImageTagStore .getStoreContent (dockerfileGitHubUtil , "store" );
79+
80+ verify (amazonS3 ).getObject ("store" , "domain!namespace!image" );
81+ verify (amazonS3 ).getObject ("store" , "domain!namespace!image2" );
82+ assertEquals (actualResult .size (), 2 );
83+ assertEquals (actualResult .get (0 ).getImageName (), "domain/namespace/image" );
84+ assertEquals (actualResult .get (0 ).getTag (), "tag" );
85+ assertEquals (actualResult .get (1 ).getImageName (), "domain/namespace/image2" );
86+ assertEquals (actualResult .get (1 ).getTag (), "tag2" );
87+ }
88+
3989 @ Test
4090 public void testGetStoreContentReturnsStoreContent () throws InterruptedException {
4191 AmazonS3 amazonS3 = mock (AmazonS3 .class );
4292 S3BackedImageTagStore s3BackedImageTagStore = spy (new S3BackedImageTagStore (amazonS3 , "store" ));
4393 DockerfileGitHubUtil dockerfileGitHubUtil = mock (DockerfileGitHubUtil .class );
4494 ListObjectsV2Result listObjectsV2Result = mock (ListObjectsV2Result .class );
95+
4596 S3ObjectSummary s3ObjectSummary = mock (S3ObjectSummary .class );
4697 List <S3ObjectSummary > s3ObjectSummaryListList = Collections .singletonList (s3ObjectSummary );
4798 Date date = mock (Date .class );
@@ -51,8 +102,9 @@ public void testGetStoreContentReturnsStoreContent() throws InterruptedException
51102 S3ObjectInputStream objectContent = new S3ObjectInputStream (new ByteArrayInputStream (tagBytes ), null );
52103 s3Object .setObjectContent (objectContent );
53104
54- when (amazonS3 .listObjectsV2 ("store" )).thenReturn (listObjectsV2Result );
105+ when (amazonS3 .listObjectsV2 (any ( ListObjectsV2Request . class ) )).thenReturn (listObjectsV2Result );
55106 when (listObjectsV2Result .getObjectSummaries ()).thenReturn (s3ObjectSummaryListList );
107+ when (listObjectsV2Result .isTruncated ()).thenReturn (false );
56108 when (s3ObjectSummary .getLastModified ()).thenReturn (date );
57109 when (s3ObjectSummary .getKey ()).thenReturn ("domain!namespace!image" );
58110 when (amazonS3 .getObject ("store" , "domain!namespace!image" )).thenReturn (s3Object );
@@ -97,8 +149,9 @@ public void testGetStoreContentReturnsStoreContentSorted() throws InterruptedExc
97149 when (s3ObjectSummaryIterator .next ()).thenReturn (s3ObjectSummary , s3ObjectSummary );
98150 when (s3ObjectSummaryIterator .hasNext ()).thenReturn (true , true , false );
99151 when (s3ObjectSummaryList .iterator ()).thenReturn (s3ObjectSummaryIterator );
100- when (amazonS3 .listObjectsV2 ("store" )).thenReturn (listObjectsV2Result );
152+ when (amazonS3 .listObjectsV2 (any ( ListObjectsV2Request . class ) )).thenReturn (listObjectsV2Result );
101153 when (listObjectsV2Result .getObjectSummaries ()).thenReturn (s3ObjectSummaryList );
154+ when (listObjectsV2Result .isTruncated ()).thenReturn (false );
102155 when (s3ObjectSummary .getLastModified ()).thenReturn (date1 , date2 );
103156 when (s3ObjectSummary .getKey ()).thenReturn (key1 , key2 );
104157 when (amazonS3 .getObject ("store" , key1 )).thenReturn (s3Object1 );
0 commit comments