4141import org .elasticsearch .repositories .fs .FsRepository ;
4242import org .elasticsearch .rest .RestStatus ;
4343import org .elasticsearch .snapshots .RestoreInfo ;
44- import org .elasticsearch . snapshots . SnapshotInfo ;
44+ import org .mockito . internal . util . collections . Sets ;
4545
4646import java .io .IOException ;
4747import java .util .Collections ;
4848import java .util .HashMap ;
4949import java .util .Map ;
50- import java .util .stream .Collectors ;
5150
52- import static org .hamcrest .Matchers .contains ;
5351import static org .hamcrest .Matchers .equalTo ;
5452import static org .hamcrest .Matchers .greaterThan ;
53+ import static org .hamcrest .Matchers .hasSize ;
5554import static org .hamcrest .Matchers .is ;
5655
5756public class SnapshotIT extends ESRestHighLevelClientTestCase {
@@ -61,14 +60,14 @@ private AcknowledgedResponse createTestRepository(String repository, String type
6160 request .settings (settings , XContentType .JSON );
6261 request .type (type );
6362 return execute (request , highLevelClient ().snapshot ()::createRepository ,
64- highLevelClient ().snapshot ()::createRepositoryAsync );
63+ highLevelClient ().snapshot ()::createRepositoryAsync );
6564 }
6665
6766 private CreateSnapshotResponse createTestSnapshot (CreateSnapshotRequest createSnapshotRequest ) throws IOException {
6867 // assumes the repository already exists
6968
7069 return execute (createSnapshotRequest , highLevelClient ().snapshot ()::create ,
71- highLevelClient ().snapshot ()::createAsync );
70+ highLevelClient ().snapshot ()::createAsync );
7271 }
7372
7473 public void testCreateRepository () throws IOException {
@@ -84,7 +83,7 @@ public void testSnapshotGetRepositoriesUsingParams() throws IOException {
8483 GetRepositoriesRequest request = new GetRepositoriesRequest ();
8584 request .repositories (new String []{testRepository });
8685 GetRepositoriesResponse response = execute (request , highLevelClient ().snapshot ()::getRepository ,
87- highLevelClient ().snapshot ()::getRepositoryAsync );
86+ highLevelClient ().snapshot ()::getRepositoryAsync );
8887 assertThat (1 , equalTo (response .repositories ().size ()));
8988 }
9089
@@ -93,19 +92,19 @@ public void testSnapshotGetDefaultRepositories() throws IOException {
9392 assertTrue (createTestRepository ("test" , FsRepository .TYPE , "{\" location\" : \" .\" }" ).isAcknowledged ());
9493
9594 GetRepositoriesResponse response = execute (new GetRepositoriesRequest (), highLevelClient ().snapshot ()::getRepository ,
96- highLevelClient ().snapshot ()::getRepositoryAsync );
95+ highLevelClient ().snapshot ()::getRepositoryAsync );
9796 assertThat (2 , equalTo (response .repositories ().size ()));
9897 }
9998
10099 public void testSnapshotGetRepositoriesNonExistent () {
101100 String repository = "doesnotexist" ;
102101 GetRepositoriesRequest request = new GetRepositoriesRequest (new String []{repository });
103102 ElasticsearchException exception = expectThrows (ElasticsearchException .class , () -> execute (request ,
104- highLevelClient ().snapshot ()::getRepository , highLevelClient ().snapshot ()::getRepositoryAsync ));
103+ highLevelClient ().snapshot ()::getRepository , highLevelClient ().snapshot ()::getRepositoryAsync ));
105104
106105 assertThat (exception .status (), equalTo (RestStatus .NOT_FOUND ));
107106 assertThat (exception .getMessage (), equalTo (
108- "Elasticsearch exception [type=repository_missing_exception, reason=[" + repository + "] missing]" ));
107+ "Elasticsearch exception [type=repository_missing_exception, reason=[" + repository + "] missing]" ));
109108 }
110109
111110 public void testSnapshotDeleteRepository () throws IOException {
@@ -114,12 +113,12 @@ public void testSnapshotDeleteRepository() throws IOException {
114113
115114 GetRepositoriesRequest request = new GetRepositoriesRequest ();
116115 GetRepositoriesResponse response = execute (request , highLevelClient ().snapshot ()::getRepository ,
117- highLevelClient ().snapshot ()::getRepositoryAsync );
116+ highLevelClient ().snapshot ()::getRepositoryAsync );
118117 assertThat (1 , equalTo (response .repositories ().size ()));
119118
120119 DeleteRepositoryRequest deleteRequest = new DeleteRepositoryRequest (repository );
121120 AcknowledgedResponse deleteResponse = execute (deleteRequest , highLevelClient ().snapshot ()::deleteRepository ,
122- highLevelClient ().snapshot ()::deleteRepositoryAsync );
121+ highLevelClient ().snapshot ()::deleteRepositoryAsync );
123122
124123 assertTrue (deleteResponse .isAcknowledged ());
125124 }
@@ -130,7 +129,7 @@ public void testVerifyRepository() throws IOException {
130129
131130 VerifyRepositoryRequest request = new VerifyRepositoryRequest ("test" );
132131 VerifyRepositoryResponse response = execute (request , highLevelClient ().snapshot ()::verifyRepository ,
133- highLevelClient ().snapshot ()::verifyRepositoryAsync );
132+ highLevelClient ().snapshot ()::verifyRepositoryAsync );
134133 assertThat (response .getNodes ().size (), equalTo (1 ));
135134 }
136135
@@ -153,25 +152,31 @@ public void testCreateSnapshot() throws IOException {
153152 if (waitForCompletion == false ) {
154153 // If we don't wait for the snapshot to complete we have to cancel it to not leak the snapshot task
155154 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
158157 );
159158 assertTrue (deleteResponse .isAcknowledged ());
160159 }
161160 }
162161
163162 public void testGetSnapshots () throws IOException {
164- String repository = "test_repository" ;
163+ String repository1 = "test_repository1" ;
164+ String repository2 = "test_repository2" ;
165165 String snapshot1 = "test_snapshot1" ;
166166 String snapshot2 = "test_snapshot2" ;
167167
168- AcknowledgedResponse putRepositoryResponse = createTestRepository (repository , FsRepository .TYPE , "{\" location\" : \" .\" }" );
168+ AcknowledgedResponse putRepositoryResponse =
169+ createTestRepository (repository1 , FsRepository .TYPE , "{\" location\" : \" loc1\" }" );
169170 assertTrue (putRepositoryResponse .isAcknowledged ());
170171
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 );
172177 createSnapshotRequest1 .waitForCompletion (true );
173178 CreateSnapshotResponse putSnapshotResponse1 = createTestSnapshot (createSnapshotRequest1 );
174- CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest (repository , snapshot2 );
179+ CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest (repository2 , snapshot2 );
175180 createSnapshotRequest2 .waitForCompletion (true );
176181 Map <String , Object > originalMetadata = randomUserMetadata ();
177182 createSnapshotRequest2 .userMetadata (originalMetadata );
@@ -180,28 +185,26 @@ public void testGetSnapshots() throws IOException {
180185 assertEquals (RestStatus .OK , putSnapshotResponse1 .status ());
181186 assertEquals (RestStatus .OK , putSnapshotResponse2 .status ());
182187
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 );
188193
189- } else {
190- request = new GetSnapshotsRequest (repository , new String [] {snapshot1 , snapshot2 });
191- }
192194 GetSnapshotsResponse response = execute (request , highLevelClient ().snapshot ()::get , highLevelClient ().snapshot ()::getAsync );
193195
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 ));
203205 }
204206
207+
205208 public void testSnapshotsStatus () throws IOException {
206209 String testRepository = "test" ;
207210 String testSnapshot = "snapshot" ;
@@ -223,7 +226,7 @@ public void testSnapshotsStatus() throws IOException {
223226 request .repository (testRepository );
224227 request .snapshots (new String []{testSnapshot });
225228 SnapshotsStatusResponse response = execute (request , highLevelClient ().snapshot ()::status ,
226- highLevelClient ().snapshot ()::statusAsync );
229+ highLevelClient ().snapshot ()::statusAsync );
227230 assertThat (response .getSnapshots ().size (), equalTo (1 ));
228231 assertThat (response .getSnapshots ().get (0 ).getSnapshot ().getRepository (), equalTo (testRepository ));
229232 assertThat (response .getSnapshots ().get (0 ).getSnapshot ().getSnapshotId ().getName (), equalTo (testSnapshot ));
@@ -260,7 +263,7 @@ public void testRestoreSnapshot() throws IOException {
260263 request .renameReplacement (restoredIndex );
261264
262265 RestoreSnapshotResponse response = execute (request , highLevelClient ().snapshot ()::restore ,
263- highLevelClient ().snapshot ()::restoreAsync );
266+ highLevelClient ().snapshot ()::restoreAsync );
264267
265268 RestoreInfo restoreInfo = response .getRestoreInfo ();
266269 assertThat (restoreInfo .name (), equalTo (testSnapshot ));
@@ -301,17 +304,17 @@ private static Map<String, Object> randomUserMetadata() {
301304 for (int i = 0 ; i < fields ; i ++) {
302305 if (randomBoolean ()) {
303306 metadata .put (randomValueOtherThanMany (metadata ::containsKey , () -> randomAlphaOfLengthBetween (2 ,10 )),
304- randomAlphaOfLengthBetween (5 , 5 ));
307+ randomAlphaOfLengthBetween (5 , 5 ));
305308 } else {
306309 Map <String , Object > nested = new HashMap <>();
307310 long nestedFields = randomLongBetween (0 , 4 );
308311 for (int j = 0 ; j < nestedFields ; j ++) {
309312 nested .put (randomValueOtherThanMany (nested ::containsKey , () -> randomAlphaOfLengthBetween (2 ,10 )),
310- randomAlphaOfLengthBetween (5 , 5 ));
313+ randomAlphaOfLengthBetween (5 , 5 ));
311314 }
312315 metadata .put (randomValueOtherThanMany (metadata ::containsKey , () -> randomAlphaOfLengthBetween (2 ,10 )), nested );
313316 }
314317 }
315318 return metadata ;
316319 }
317- }
320+ }
0 commit comments