20
20
21
21
import org .elasticsearch .common .bytes .BytesReference ;
22
22
import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
23
+ import org .elasticsearch .common .io .stream .StreamInput ;
23
24
import org .elasticsearch .common .settings .Settings ;
25
+ import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
24
26
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
27
+ import org .elasticsearch .common .xcontent .XContent ;
28
+ import org .elasticsearch .common .xcontent .XContentBuilder ;
25
29
import org .elasticsearch .common .xcontent .XContentParser ;
30
+ import org .elasticsearch .common .xcontent .XContentType ;
26
31
import org .elasticsearch .index .query .MatchAllQueryBuilder ;
27
32
import org .elasticsearch .index .query .QueryBuilder ;
28
33
import org .elasticsearch .painless .PainlessExecuteAction .Request .ContextSetup ;
29
34
import org .elasticsearch .script .Script ;
30
35
import org .elasticsearch .script .ScriptContext ;
31
36
import org .elasticsearch .script .ScriptType ;
32
37
import org .elasticsearch .search .SearchModule ;
33
- import org .elasticsearch .test .AbstractStreamableXContentTestCase ;
38
+ import org .elasticsearch .test .AbstractStreamableTestCase ;
34
39
35
40
import java .io .IOException ;
41
+ import java .io .UncheckedIOException ;
36
42
import java .util .Collections ;
37
43
38
- public class PainlessExecuteRequestTests extends AbstractStreamableXContentTestCase <PainlessExecuteAction .Request > {
44
+ import static org .hamcrest .Matchers .equalTo ;
45
+
46
+ public class PainlessExecuteRequestTests extends AbstractStreamableTestCase <PainlessExecuteAction .Request > {
47
+
48
+ // Testing XContent serialization manually here, because the xContentType field in ContextSetup determines
49
+ // how the request needs to parse and the xcontent serialization framework randomizes that. The XContentType
50
+ // is not known and accessable when the test request instance is created in the xcontent serialization framework.
51
+ // Changing that is a big change. Writing a custom xcontent test here is the best option for now, because as far
52
+ // as I know this request class is the only case where this is a problem.
53
+ public final void testFromXContent () throws Exception {
54
+ for (int i = 0 ; i < 20 ; i ++) {
55
+ PainlessExecuteAction .Request testInstance = createTestInstance ();
56
+ ContextSetup contextSetup = testInstance .getContextSetup ();
57
+ XContent xContent = randomFrom (XContentType .values ()).xContent ();
58
+ if (contextSetup != null && contextSetup .getXContentType () != null ) {
59
+ xContent = contextSetup .getXContentType ().xContent ();
60
+ }
61
+
62
+ try (XContentBuilder builder = XContentBuilder .builder (xContent )) {
63
+ builder .value (testInstance );
64
+ StreamInput instanceInput = BytesReference .bytes (builder ).streamInput ();
65
+ try (XContentParser parser = xContent .createParser (xContentRegistry (), LoggingDeprecationHandler .INSTANCE , instanceInput )) {
66
+ PainlessExecuteAction .Request result = PainlessExecuteAction .Request .parse (parser );
67
+ assertThat (result , equalTo (testInstance ));
68
+ }
69
+ }
70
+ }
71
+ }
39
72
40
73
@ Override
41
74
protected NamedWriteableRegistry getNamedWriteableRegistry () {
@@ -60,16 +93,6 @@ protected PainlessExecuteAction.Request createBlankInstance() {
60
93
return new PainlessExecuteAction .Request ();
61
94
}
62
95
63
- @ Override
64
- protected PainlessExecuteAction .Request doParseInstance (XContentParser parser ) throws IOException {
65
- return PainlessExecuteAction .Request .parse (parser );
66
- }
67
-
68
- @ Override
69
- protected boolean supportsUnknownFields () {
70
- return false ;
71
- }
72
-
73
96
public void testValidate () {
74
97
Script script = new Script (ScriptType .STORED , null , randomAlphaOfLength (10 ), Collections .emptyMap ());
75
98
PainlessExecuteAction .Request request = new PainlessExecuteAction .Request (script , null , null );
@@ -78,20 +101,24 @@ public void testValidate() {
78
101
assertEquals ("Validation Failed: 1: only inline scripts are supported;" , e .getMessage ());
79
102
}
80
103
81
- private static ContextSetup randomContextSetup () {
104
+ private static ContextSetup randomContextSetup () {
82
105
String index = randomBoolean () ? randomAlphaOfLength (4 ) : null ;
83
106
QueryBuilder query = randomBoolean () ? new MatchAllQueryBuilder () : null ;
84
- // TODO: pass down XContextType to createTestInstance() method.
85
- // otherwise the document itself is different causing test failures.
86
- // This should be done in a separate change as the test instance is created before xcontent type is randomly picked and
87
- // all the createTestInstance() methods need to be changed, which will make this a big chnage
88
- // BytesReference doc = randomBoolean() ? new BytesArray("{}") : null;
89
107
BytesReference doc = null ;
108
+ XContentType xContentType = randomFrom (XContentType .values ());
109
+ if (randomBoolean ()) {
110
+ try {
111
+ XContentBuilder xContentBuilder = XContentBuilder .builder (xContentType .xContent ());
112
+ xContentBuilder .startObject ();
113
+ xContentBuilder .endObject ();
114
+ doc = BytesReference .bytes (xContentBuilder );
115
+ } catch (IOException e ) {
116
+ throw new UncheckedIOException (e );
117
+ }
118
+ }
90
119
91
120
ContextSetup contextSetup = new ContextSetup (index , doc , query );
92
- // if (doc != null) {
93
- // contextSetup.setXContentType(XContentType.JSON);
94
- // }
121
+ contextSetup .setXContentType (xContentType );
95
122
return contextSetup ;
96
123
}
97
124
}
0 commit comments