8
8
import com .carrotsearch .randomizedtesting .generators .CodepointSetGenerator ;
9
9
10
10
import org .elasticsearch .ElasticsearchException ;
11
+ import org .elasticsearch .common .bytes .BytesReference ;
11
12
import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
12
13
import org .elasticsearch .common .io .stream .Writeable ;
13
14
import org .elasticsearch .common .settings .Settings ;
14
15
import org .elasticsearch .common .unit .TimeValue ;
15
16
import org .elasticsearch .common .xcontent .DeprecationHandler ;
17
+ import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
16
18
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
19
+ import org .elasticsearch .common .xcontent .ToXContent ;
17
20
import org .elasticsearch .common .xcontent .XContentFactory ;
21
+ import org .elasticsearch .common .xcontent .XContentHelper ;
18
22
import org .elasticsearch .common .xcontent .XContentParseException ;
19
23
import org .elasticsearch .common .xcontent .XContentParser ;
20
24
import org .elasticsearch .common .xcontent .XContentType ;
36
40
import org .elasticsearch .test .ESTestCase ;
37
41
import org .elasticsearch .xpack .core .ml .datafeed .ChunkingConfig .Mode ;
38
42
import org .elasticsearch .xpack .core .ml .job .messages .Messages ;
43
+ import org .elasticsearch .xpack .core .ml .utils .ToXContentParams ;
39
44
import org .joda .time .DateTimeZone ;
40
45
41
46
import java .io .IOException ;
42
47
import java .util .ArrayList ;
43
48
import java .util .Collections ;
49
+ import java .util .HashMap ;
44
50
import java .util .List ;
51
+ import java .util .Map ;
45
52
import java .util .TimeZone ;
46
53
47
54
import static org .hamcrest .Matchers .containsString ;
48
55
import static org .hamcrest .Matchers .equalTo ;
49
56
import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
57
+ import static org .hamcrest .Matchers .hasEntry ;
58
+ import static org .hamcrest .Matchers .hasSize ;
50
59
import static org .hamcrest .Matchers .is ;
51
60
import static org .hamcrest .Matchers .lessThan ;
52
61
import static org .hamcrest .Matchers .not ;
@@ -63,6 +72,10 @@ public static DatafeedConfig createRandomizedDatafeedConfig(String jobId) {
63
72
}
64
73
65
74
public static DatafeedConfig createRandomizedDatafeedConfig (String jobId , long bucketSpanMillis ) {
75
+ return createRandomizedDatafeedConfigBuilder (jobId , bucketSpanMillis ).build ();
76
+ }
77
+
78
+ private static DatafeedConfig .Builder createRandomizedDatafeedConfigBuilder (String jobId , long bucketSpanMillis ) {
66
79
DatafeedConfig .Builder builder = new DatafeedConfig .Builder (randomValidDatafeedId (), jobId );
67
80
builder .setIndices (randomStringList (1 , 10 ));
68
81
builder .setTypes (randomStringList (0 , 10 ));
@@ -100,7 +113,7 @@ public static DatafeedConfig createRandomizedDatafeedConfig(String jobId, long b
100
113
if (aggHistogramInterval == null ) {
101
114
builder .setFrequency (TimeValue .timeValueSeconds (randomIntBetween (1 , 1_000_000 )));
102
115
} else {
103
- builder .setFrequency (TimeValue .timeValueMillis (randomIntBetween (1 , 5 ) * aggHistogramInterval ));
116
+ builder .setFrequency (TimeValue .timeValueSeconds (randomIntBetween (1 , 5 ) * aggHistogramInterval ));
104
117
}
105
118
}
106
119
if (randomBoolean ()) {
@@ -109,7 +122,7 @@ public static DatafeedConfig createRandomizedDatafeedConfig(String jobId, long b
109
122
if (randomBoolean ()) {
110
123
builder .setChunkingConfig (ChunkingConfigTests .createRandomizedChunk ());
111
124
}
112
- return builder . build () ;
125
+ return builder ;
113
126
}
114
127
115
128
@ Override
@@ -167,6 +180,33 @@ public void testFutureMetadataParse() throws IOException {
167
180
assertNotNull (DatafeedConfig .LENIENT_PARSER .apply (parser , null ).build ());
168
181
}
169
182
183
+ public void testToXContentForInternalStorage () throws IOException {
184
+ DatafeedConfig .Builder builder = createRandomizedDatafeedConfigBuilder ("foo" , 300 );
185
+
186
+ // headers are only persisted to cluster state
187
+ Map <String , String > headers = new HashMap <>();
188
+ headers .put ("header-name" , "header-value" );
189
+ builder .setHeaders (headers );
190
+ DatafeedConfig config = builder .build ();
191
+
192
+ ToXContent .MapParams params = new ToXContent .MapParams (Collections .singletonMap (ToXContentParams .FOR_INTERNAL_STORAGE , "true" ));
193
+
194
+ BytesReference forClusterstateXContent = XContentHelper .toXContent (config , XContentType .JSON , params , false );
195
+ XContentParser parser = XContentFactory .xContent (XContentType .JSON )
196
+ .createParser (xContentRegistry (), LoggingDeprecationHandler .INSTANCE , forClusterstateXContent .streamInput ());
197
+
198
+ DatafeedConfig parsedConfig = DatafeedConfig .LENIENT_PARSER .apply (parser , null ).build ();
199
+ assertThat (parsedConfig .getHeaders (), hasEntry ("header-name" , "header-value" ));
200
+
201
+ // headers are not written without the FOR_INTERNAL_STORAGE param
202
+ BytesReference nonClusterstateXContent = XContentHelper .toXContent (config , XContentType .JSON , ToXContent .EMPTY_PARAMS , false );
203
+ parser = XContentFactory .xContent (XContentType .JSON )
204
+ .createParser (xContentRegistry (), LoggingDeprecationHandler .INSTANCE , nonClusterstateXContent .streamInput ());
205
+
206
+ parsedConfig = DatafeedConfig .LENIENT_PARSER .apply (parser , null ).build ();
207
+ assertThat (parsedConfig .getHeaders ().entrySet (), hasSize (0 ));
208
+ }
209
+
170
210
public void testCopyConstructor () {
171
211
for (int i = 0 ; i < NUMBER_OF_TEST_RUNS ; i ++) {
172
212
DatafeedConfig datafeedConfig = createTestInstance ();
0 commit comments