41
41
import org .elasticsearch .repositories .fs .FsRepository ;
42
42
import org .elasticsearch .rest .RestStatus ;
43
43
import org .elasticsearch .snapshots .RestoreInfo ;
44
- import org .elasticsearch . snapshots . SnapshotInfo ;
44
+ import org .mockito . internal . util . collections . Sets ;
45
45
46
46
import java .io .IOException ;
47
47
import java .util .Collections ;
48
48
import java .util .HashMap ;
49
49
import java .util .Map ;
50
- import java .util .stream .Collectors ;
51
50
52
- import static org .hamcrest .Matchers .contains ;
53
51
import static org .hamcrest .Matchers .equalTo ;
54
52
import static org .hamcrest .Matchers .greaterThan ;
53
+ import static org .hamcrest .Matchers .hasSize ;
55
54
import static org .hamcrest .Matchers .is ;
56
55
57
56
public class SnapshotIT extends ESRestHighLevelClientTestCase {
@@ -61,14 +60,14 @@ private AcknowledgedResponse createTestRepository(String repository, String type
61
60
request .settings (settings , XContentType .JSON );
62
61
request .type (type );
63
62
return execute (request , highLevelClient ().snapshot ()::createRepository ,
64
- highLevelClient ().snapshot ()::createRepositoryAsync );
63
+ highLevelClient ().snapshot ()::createRepositoryAsync );
65
64
}
66
65
67
66
private CreateSnapshotResponse createTestSnapshot (CreateSnapshotRequest createSnapshotRequest ) throws IOException {
68
67
// assumes the repository already exists
69
68
70
69
return execute (createSnapshotRequest , highLevelClient ().snapshot ()::create ,
71
- highLevelClient ().snapshot ()::createAsync );
70
+ highLevelClient ().snapshot ()::createAsync );
72
71
}
73
72
74
73
public void testCreateRepository () throws IOException {
@@ -84,7 +83,7 @@ public void testSnapshotGetRepositoriesUsingParams() throws IOException {
84
83
GetRepositoriesRequest request = new GetRepositoriesRequest ();
85
84
request .repositories (new String []{testRepository });
86
85
GetRepositoriesResponse response = execute (request , highLevelClient ().snapshot ()::getRepository ,
87
- highLevelClient ().snapshot ()::getRepositoryAsync );
86
+ highLevelClient ().snapshot ()::getRepositoryAsync );
88
87
assertThat (1 , equalTo (response .repositories ().size ()));
89
88
}
90
89
@@ -93,19 +92,19 @@ public void testSnapshotGetDefaultRepositories() throws IOException {
93
92
assertTrue (createTestRepository ("test" , FsRepository .TYPE , "{\" location\" : \" .\" }" ).isAcknowledged ());
94
93
95
94
GetRepositoriesResponse response = execute (new GetRepositoriesRequest (), highLevelClient ().snapshot ()::getRepository ,
96
- highLevelClient ().snapshot ()::getRepositoryAsync );
95
+ highLevelClient ().snapshot ()::getRepositoryAsync );
97
96
assertThat (2 , equalTo (response .repositories ().size ()));
98
97
}
99
98
100
99
public void testSnapshotGetRepositoriesNonExistent () {
101
100
String repository = "doesnotexist" ;
102
101
GetRepositoriesRequest request = new GetRepositoriesRequest (new String []{repository });
103
102
ElasticsearchException exception = expectThrows (ElasticsearchException .class , () -> execute (request ,
104
- highLevelClient ().snapshot ()::getRepository , highLevelClient ().snapshot ()::getRepositoryAsync ));
103
+ highLevelClient ().snapshot ()::getRepository , highLevelClient ().snapshot ()::getRepositoryAsync ));
105
104
106
105
assertThat (exception .status (), equalTo (RestStatus .NOT_FOUND ));
107
106
assertThat (exception .getMessage (), equalTo (
108
- "Elasticsearch exception [type=repository_missing_exception, reason=[" + repository + "] missing]" ));
107
+ "Elasticsearch exception [type=repository_missing_exception, reason=[" + repository + "] missing]" ));
109
108
}
110
109
111
110
public void testSnapshotDeleteRepository () throws IOException {
@@ -114,12 +113,12 @@ public void testSnapshotDeleteRepository() throws IOException {
114
113
115
114
GetRepositoriesRequest request = new GetRepositoriesRequest ();
116
115
GetRepositoriesResponse response = execute (request , highLevelClient ().snapshot ()::getRepository ,
117
- highLevelClient ().snapshot ()::getRepositoryAsync );
116
+ highLevelClient ().snapshot ()::getRepositoryAsync );
118
117
assertThat (1 , equalTo (response .repositories ().size ()));
119
118
120
119
DeleteRepositoryRequest deleteRequest = new DeleteRepositoryRequest (repository );
121
120
AcknowledgedResponse deleteResponse = execute (deleteRequest , highLevelClient ().snapshot ()::deleteRepository ,
122
- highLevelClient ().snapshot ()::deleteRepositoryAsync );
121
+ highLevelClient ().snapshot ()::deleteRepositoryAsync );
123
122
124
123
assertTrue (deleteResponse .isAcknowledged ());
125
124
}
@@ -130,7 +129,7 @@ public void testVerifyRepository() throws IOException {
130
129
131
130
VerifyRepositoryRequest request = new VerifyRepositoryRequest ("test" );
132
131
VerifyRepositoryResponse response = execute (request , highLevelClient ().snapshot ()::verifyRepository ,
133
- highLevelClient ().snapshot ()::verifyRepositoryAsync );
132
+ highLevelClient ().snapshot ()::verifyRepositoryAsync );
134
133
assertThat (response .getNodes ().size (), equalTo (1 ));
135
134
}
136
135
@@ -153,25 +152,31 @@ public void testCreateSnapshot() throws IOException {
153
152
if (waitForCompletion == false ) {
154
153
// If we don't wait for the snapshot to complete we have to cancel it to not leak the snapshot task
155
154
AcknowledgedResponse deleteResponse = execute (
156
- new DeleteSnapshotRequest (repository , snapshot ),
157
- highLevelClient ().snapshot ()::delete , highLevelClient ().snapshot ()::deleteAsync
155
+ new DeleteSnapshotRequest (repository , snapshot ),
156
+ highLevelClient ().snapshot ()::delete , highLevelClient ().snapshot ()::deleteAsync
158
157
);
159
158
assertTrue (deleteResponse .isAcknowledged ());
160
159
}
161
160
}
162
161
163
162
public void testGetSnapshots () throws IOException {
164
- String repository = "test_repository" ;
163
+ String repository1 = "test_repository1" ;
164
+ String repository2 = "test_repository2" ;
165
165
String snapshot1 = "test_snapshot1" ;
166
166
String snapshot2 = "test_snapshot2" ;
167
167
168
- AcknowledgedResponse putRepositoryResponse = createTestRepository (repository , FsRepository .TYPE , "{\" location\" : \" .\" }" );
168
+ AcknowledgedResponse putRepositoryResponse =
169
+ createTestRepository (repository1 , FsRepository .TYPE , "{\" location\" : \" loc1\" }" );
169
170
assertTrue (putRepositoryResponse .isAcknowledged ());
170
171
171
- CreateSnapshotRequest createSnapshotRequest1 = new CreateSnapshotRequest (repository , snapshot1 );
172
+ AcknowledgedResponse putRepositoryResponse2 =
173
+ createTestRepository (repository2 , FsRepository .TYPE , "{\" location\" : \" loc2\" }" );
174
+ assertTrue (putRepositoryResponse2 .isAcknowledged ());
175
+
176
+ CreateSnapshotRequest createSnapshotRequest1 = new CreateSnapshotRequest (repository1 , snapshot1 );
172
177
createSnapshotRequest1 .waitForCompletion (true );
173
178
CreateSnapshotResponse putSnapshotResponse1 = createTestSnapshot (createSnapshotRequest1 );
174
- CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest (repository , snapshot2 );
179
+ CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest (repository2 , snapshot2 );
175
180
createSnapshotRequest2 .waitForCompletion (true );
176
181
Map <String , Object > originalMetadata = randomUserMetadata ();
177
182
createSnapshotRequest2 .userMetadata (originalMetadata );
@@ -180,28 +185,26 @@ public void testGetSnapshots() throws IOException {
180
185
assertEquals (RestStatus .OK , putSnapshotResponse1 .status ());
181
186
assertEquals (RestStatus .OK , putSnapshotResponse2 .status ());
182
187
183
- GetSnapshotsRequest request ;
184
- if ( randomBoolean ()) {
185
- request = new GetSnapshotsRequest ( repository );
186
- } else if ( randomBoolean ()) {
187
- request = new GetSnapshotsRequest ( repository , new String [] { "_all" } );
188
+ GetSnapshotsRequest request = new GetSnapshotsRequest (
189
+ randomFrom ( new String []{ "_all" }, new String []{ "*" }, new String []{ repository1 , repository2 }),
190
+ randomFrom ( new String []{ "_all" }, new String []{ "*" }, new String []{ snapshot1 , snapshot2 })
191
+ );
192
+ request . ignoreUnavailable ( true );
188
193
189
- } else {
190
- request = new GetSnapshotsRequest (repository , new String [] {snapshot1 , snapshot2 });
191
- }
192
194
GetSnapshotsResponse response = execute (request , highLevelClient ().snapshot ()::get , highLevelClient ().snapshot ()::getAsync );
193
195
194
- assertEquals ( 2 , response .getSnapshots (). size ( ));
195
- assertThat (response .getSnapshots (). stream (). map (( s ) -> s . snapshotId (). getName ()). collect ( Collectors . toList ()),
196
- contains ( "test_snapshot1" , "test_snapshot2" ));
197
- response .getSnapshots (). stream ()
198
- . filter ( s -> s . snapshotId ().getName (). equals ( "test_snapshot2" ))
199
- . findFirst ()
200
- . map ( SnapshotInfo :: userMetadata )
201
- . ifPresentOrElse ( metadata -> assertEquals ( originalMetadata , metadata ),
202
- () -> assertNull ( "retrieved metadata is null, expected non-null metadata" , originalMetadata ));
196
+ assertThat ( response .isFailed (), is ( false ));
197
+ assertThat (response .getRepositories (), equalTo ( Sets . newSet ( repository1 , repository2 )));
198
+
199
+ assertThat ( response .getSnapshots (repository1 ), hasSize ( 1 ));
200
+ assertThat ( response . getSnapshots ( repository1 ). get ( 0 ). snapshotId ().getName (), equalTo ( snapshot1 ));
201
+
202
+ assertThat ( response . getSnapshots ( repository2 ), hasSize ( 1 ));
203
+ assertThat ( response . getSnapshots ( repository2 ). get ( 0 ). snapshotId (). getName (), equalTo ( snapshot2 ));
204
+ assertThat ( response . getSnapshots ( repository2 ). get ( 0 ). userMetadata (), equalTo ( originalMetadata ));
203
205
}
204
206
207
+
205
208
public void testSnapshotsStatus () throws IOException {
206
209
String testRepository = "test" ;
207
210
String testSnapshot = "snapshot" ;
@@ -223,7 +226,7 @@ public void testSnapshotsStatus() throws IOException {
223
226
request .repository (testRepository );
224
227
request .snapshots (new String []{testSnapshot });
225
228
SnapshotsStatusResponse response = execute (request , highLevelClient ().snapshot ()::status ,
226
- highLevelClient ().snapshot ()::statusAsync );
229
+ highLevelClient ().snapshot ()::statusAsync );
227
230
assertThat (response .getSnapshots ().size (), equalTo (1 ));
228
231
assertThat (response .getSnapshots ().get (0 ).getSnapshot ().getRepository (), equalTo (testRepository ));
229
232
assertThat (response .getSnapshots ().get (0 ).getSnapshot ().getSnapshotId ().getName (), equalTo (testSnapshot ));
@@ -260,7 +263,7 @@ public void testRestoreSnapshot() throws IOException {
260
263
request .renameReplacement (restoredIndex );
261
264
262
265
RestoreSnapshotResponse response = execute (request , highLevelClient ().snapshot ()::restore ,
263
- highLevelClient ().snapshot ()::restoreAsync );
266
+ highLevelClient ().snapshot ()::restoreAsync );
264
267
265
268
RestoreInfo restoreInfo = response .getRestoreInfo ();
266
269
assertThat (restoreInfo .name (), equalTo (testSnapshot ));
@@ -301,17 +304,17 @@ private static Map<String, Object> randomUserMetadata() {
301
304
for (int i = 0 ; i < fields ; i ++) {
302
305
if (randomBoolean ()) {
303
306
metadata .put (randomValueOtherThanMany (metadata ::containsKey , () -> randomAlphaOfLengthBetween (2 ,10 )),
304
- randomAlphaOfLengthBetween (5 , 5 ));
307
+ randomAlphaOfLengthBetween (5 , 5 ));
305
308
} else {
306
309
Map <String , Object > nested = new HashMap <>();
307
310
long nestedFields = randomLongBetween (0 , 4 );
308
311
for (int j = 0 ; j < nestedFields ; j ++) {
309
312
nested .put (randomValueOtherThanMany (nested ::containsKey , () -> randomAlphaOfLengthBetween (2 ,10 )),
310
- randomAlphaOfLengthBetween (5 , 5 ));
313
+ randomAlphaOfLengthBetween (5 , 5 ));
311
314
}
312
315
metadata .put (randomValueOtherThanMany (metadata ::containsKey , () -> randomAlphaOfLengthBetween (2 ,10 )), nested );
313
316
}
314
317
}
315
318
return metadata ;
316
319
}
317
- }
320
+ }
0 commit comments