2121
2222import org .elasticsearch .action .support .IndicesOptions ;
2323import org .elasticsearch .common .bytes .BytesReference ;
24+ import org .elasticsearch .common .io .stream .Writeable ;
2425import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
2526import org .elasticsearch .common .xcontent .ToXContent ;
2627import org .elasticsearch .common .xcontent .XContentBuilder ;
2728import org .elasticsearch .common .xcontent .XContentFactory ;
2829import org .elasticsearch .common .xcontent .XContentParser ;
2930import org .elasticsearch .common .xcontent .XContentType ;
30- import org .elasticsearch .test .ESTestCase ;
31+ import org .elasticsearch .test .AbstractWireSerializingTestCase ;
3132
3233import java .io .IOException ;
3334import java .util .ArrayList ;
3940import java .util .List ;
4041import java .util .Map ;
4142
42- public class RestoreSnapshotRequestTests extends ESTestCase {
43- // tests creating XContent and parsing with source(Map) equivalency
44- public void testToXContent () throws IOException {
45- String repo = randomAlphaOfLength (5 );
46- String snapshot = randomAlphaOfLength (10 );
47-
48- RestoreSnapshotRequest original = new RestoreSnapshotRequest (repo , snapshot );
49-
43+ public class RestoreSnapshotRequestTests extends AbstractWireSerializingTestCase <RestoreSnapshotRequest > {
44+ private RestoreSnapshotRequest randomState (RestoreSnapshotRequest instance ) {
5045 if (randomBoolean ()) {
5146 List <String > indices = new ArrayList <>();
5247 int count = randomInt (3 ) + 1 ;
@@ -55,16 +50,16 @@ public void testToXContent() throws IOException {
5550 indices .add (randomAlphaOfLength (randomInt (3 ) + 2 ));
5651 }
5752
58- original .indices (indices );
53+ instance .indices (indices );
5954 }
6055 if (randomBoolean ()) {
61- original .renamePattern (randomUnicodeOfLengthBetween (1 , 100 ));
56+ instance .renamePattern (randomUnicodeOfLengthBetween (1 , 100 ));
6257 }
6358 if (randomBoolean ()) {
64- original .renameReplacement (randomUnicodeOfLengthBetween (1 , 100 ));
59+ instance .renameReplacement (randomUnicodeOfLengthBetween (1 , 100 ));
6560 }
66- original .partial (randomBoolean ());
67- original .includeAliases (randomBoolean ());
61+ instance .partial (randomBoolean ());
62+ instance .includeAliases (randomBoolean ());
6863
6964 if (randomBoolean ()) {
7065 Map <String , Object > settings = new HashMap <>();
@@ -74,7 +69,7 @@ public void testToXContent() throws IOException {
7469 settings .put (randomAlphaOfLengthBetween (2 , 5 ), randomAlphaOfLengthBetween (2 , 5 ));
7570 }
7671
77- original .settings (settings );
72+ instance .settings (settings );
7873 }
7974 if (randomBoolean ()) {
8075 Map <String , Object > indexSettings = new HashMap <>();
@@ -83,28 +78,50 @@ public void testToXContent() throws IOException {
8378 for (int i = 0 ; i < count ; ++i ) {
8479 indexSettings .put (randomAlphaOfLengthBetween (2 , 5 ), randomAlphaOfLengthBetween (2 , 5 ));;
8580 }
86- original .indexSettings (indexSettings );
81+ instance .indexSettings (indexSettings );
8782 }
8883
89- original .includeGlobalState (randomBoolean ());
84+ instance .includeGlobalState (randomBoolean ());
9085
9186 if (randomBoolean ()) {
9287 Collection <IndicesOptions .WildcardStates > wildcardStates = randomSubsetOf (
9388 Arrays .asList (IndicesOptions .WildcardStates .values ()));
9489 Collection <IndicesOptions .Option > options = randomSubsetOf (
9590 Arrays .asList (IndicesOptions .Option .ALLOW_NO_INDICES , IndicesOptions .Option .IGNORE_UNAVAILABLE ));
9691
97- original .indicesOptions (new IndicesOptions (
92+ instance .indicesOptions (new IndicesOptions (
9893 options .isEmpty () ? IndicesOptions .Option .NONE : EnumSet .copyOf (options ),
9994 wildcardStates .isEmpty () ? IndicesOptions .WildcardStates .NONE : EnumSet .copyOf (wildcardStates )));
10095 }
10196
102- original .waitForCompletion (randomBoolean ());
97+ instance .waitForCompletion (randomBoolean ());
10398
10499 if (randomBoolean ()) {
105- original .masterNodeTimeout ("60s" );
100+ instance .masterNodeTimeout (randomTimeValue () );
106101 }
102+ return instance ;
103+ }
104+
105+ @ Override
106+ protected RestoreSnapshotRequest createTestInstance () {
107+ return randomState (new RestoreSnapshotRequest (randomAlphaOfLength (5 ), randomAlphaOfLength (10 )));
108+ }
109+
110+ @ Override
111+ protected Writeable .Reader <RestoreSnapshotRequest > instanceReader () {
112+ return RestoreSnapshotRequest ::new ;
113+ }
114+
115+ @ Override
116+ protected RestoreSnapshotRequest mutateInstance (RestoreSnapshotRequest instance ) throws IOException {
117+ RestoreSnapshotRequest copy = copyInstance (instance );
118+ // ensure that at least one property is different
119+ copy .repository ("copied-" + instance .repository ());
120+ return randomState (copy );
121+ }
107122
123+ public void testSource () throws IOException {
124+ RestoreSnapshotRequest original = createTestInstance ();
108125 XContentBuilder builder = original .toXContent (XContentFactory .jsonBuilder (), new ToXContent .MapParams (Collections .emptyMap ()));
109126 XContentParser parser = XContentType .JSON .xContent ().createParser (
110127 NamedXContentRegistry .EMPTY , null , BytesReference .bytes (builder ).streamInput ());
@@ -113,7 +130,7 @@ public void testToXContent() throws IOException {
113130 // we will only restore properties from the map that are contained in the request body. All other
114131 // properties are restored from the original (in the actual REST action this is restored from the
115132 // REST path and request parameters).
116- RestoreSnapshotRequest processed = new RestoreSnapshotRequest (repo , snapshot );
133+ RestoreSnapshotRequest processed = new RestoreSnapshotRequest (original . repository (), original . snapshot () );
117134 processed .masterNodeTimeout (original .masterNodeTimeout ());
118135 processed .waitForCompletion (original .waitForCompletion ());
119136
0 commit comments