4141import java .util .HashMap ;
4242import java .util .List ;
4343import java .util .Map ;
44+ import java .util .Optional ;
4445import java .util .concurrent .TimeUnit ;
4546import java .util .function .Function ;
47+ import java .util .function .Predicate ;
4648import java .util .stream .Collectors ;
4749
4850import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
@@ -104,6 +106,11 @@ public void testFullPolicySnapshot() throws Exception {
104106
105107 createSnapshotPolicy (policyName , "snap" , "*/1 * * * * ?" , repoId , indexName , true );
106108
109+ // A test for whether the repository's snapshots have any snapshots starting with "snap-"
110+ Predicate <Map <String , Object >> repoHasSnapshot = snapMap -> Optional .ofNullable ((String ) snapMap .get ("snapshot" ))
111+ .map (snapName -> snapName .startsWith ("snap-" ))
112+ .orElse (false );
113+
107114 // Check that the snapshot was actually taken
108115 assertBusy (() -> {
109116 Response response = client ().performRequest (new Request ("GET" , "/_snapshot/" + repoId + "/_all" ));
@@ -112,17 +119,16 @@ public void testFullPolicySnapshot() throws Exception {
112119 snapshotResponseMap = XContentHelper .convertToMap (XContentType .JSON .xContent (), is , true );
113120 }
114121 assertThat (snapshotResponseMap .size (), greaterThan (0 ));
115- final Map <String , Object > snapResponse ;
116- try {
117- Map <String , Object > responseMap = ((List <Map <String , Object >>) snapshotResponseMap .get ("responses" )).get (0 );
118- List <Map <String , Object >> snapshots = (List <Map <String , Object >>) responseMap .get ("snapshots" );
119- assertTrue (snapshots .stream ().anyMatch (s -> s .containsKey ("snapshot" ) && s .get ("snapshot" ).toString ().startsWith ("snap-" )));
120- snapResponse = snapshots .get (0 );
121- } catch (Exception e ) {
122- throw new AssertionError ("failed to find snapshot response in " + snapshotResponseMap , e );
123- }
124- assertThat (snapResponse .get ("indices" ), equalTo (Collections .singletonList (indexName )));
125- Map <String , Object > metadata = (Map <String , Object >) snapResponse .get ("metadata" );
122+ List <Map <String , Object >> snapResponse = ((List <Map <String , Object >>) snapshotResponseMap .get ("responses" )).stream ()
123+ .peek (m -> logger .info ("--> responses: {}" , m ))
124+ .map (m -> (List <Map <String , Object >>) m .get ("snapshots" ))
125+ .peek (allReposSnapshots -> logger .info ("--> all repository's snapshots: {}" , allReposSnapshots ))
126+ .filter (allReposSnapshots -> allReposSnapshots .stream ().anyMatch (repoHasSnapshot ))
127+ .peek (allRepos -> logger .info ("--> snapshots with 'snap-' snapshot: {}" , allRepos ))
128+ .findFirst ()
129+ .orElseThrow (() -> new AssertionError ("failed to find snapshot response in " + snapshotResponseMap ));
130+ assertThat (snapResponse .get (0 ).get ("indices" ), equalTo (Collections .singletonList (indexName )));
131+ Map <String , Object > metadata = (Map <String , Object >) snapResponse .get (0 ).get ("metadata" );
126132 assertNotNull (metadata );
127133 assertThat (metadata .get ("policy" ), equalTo (policyName ));
128134 });
0 commit comments