41
41
import java .util .HashMap ;
42
42
import java .util .List ;
43
43
import java .util .Map ;
44
+ import java .util .Optional ;
44
45
import java .util .concurrent .TimeUnit ;
45
46
import java .util .function .Function ;
47
+ import java .util .function .Predicate ;
46
48
import java .util .stream .Collectors ;
47
49
48
50
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
@@ -104,6 +106,11 @@ public void testFullPolicySnapshot() throws Exception {
104
106
105
107
createSnapshotPolicy (policyName , "snap" , "*/1 * * * * ?" , repoId , indexName , true );
106
108
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
+
107
114
// Check that the snapshot was actually taken
108
115
assertBusy (() -> {
109
116
Response response = client ().performRequest (new Request ("GET" , "/_snapshot/" + repoId + "/_all" ));
@@ -112,17 +119,16 @@ public void testFullPolicySnapshot() throws Exception {
112
119
snapshotResponseMap = XContentHelper .convertToMap (XContentType .JSON .xContent (), is , true );
113
120
}
114
121
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" );
126
132
assertNotNull (metadata );
127
133
assertThat (metadata .get ("policy" ), equalTo (policyName ));
128
134
});
0 commit comments