|
7 | 7 | package org.elasticsearch.xpack.monitoring.collector.ccr; |
8 | 8 |
|
9 | 9 | import org.elasticsearch.ElasticsearchException; |
| 10 | +import org.elasticsearch.common.Strings; |
10 | 11 | import org.elasticsearch.common.bytes.BytesReference; |
| 12 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
11 | 13 | import org.elasticsearch.common.xcontent.XContentHelper; |
12 | 14 | import org.elasticsearch.common.xcontent.XContentType; |
| 15 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
13 | 16 | import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; |
14 | 17 | import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; |
15 | 18 | import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringDoc; |
| 19 | +import org.elasticsearch.xpack.core.monitoring.exporter.MonitoringTemplateUtils; |
16 | 20 | import org.elasticsearch.xpack.monitoring.exporter.BaseMonitoringDocTestCase; |
17 | 21 | import org.joda.time.DateTime; |
18 | 22 | import org.joda.time.DateTimeZone; |
19 | 23 | import org.junit.Before; |
20 | 24 |
|
21 | 25 | import java.io.IOException; |
22 | 26 | import java.util.Collections; |
| 27 | +import java.util.Map; |
23 | 28 | import java.util.NavigableMap; |
24 | 29 | import java.util.TreeMap; |
25 | 30 |
|
| 31 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 32 | +import static org.hamcrest.Matchers.anyOf; |
26 | 33 | import static org.hamcrest.Matchers.containsString; |
27 | 34 | import static org.hamcrest.Matchers.equalTo; |
28 | 35 | import static org.hamcrest.Matchers.hasToString; |
29 | 36 | import static org.hamcrest.Matchers.is; |
| 37 | +import static org.hamcrest.Matchers.notNullValue; |
30 | 38 | import static org.hamcrest.Matchers.nullValue; |
31 | 39 | import static org.mockito.Mockito.mock; |
32 | 40 |
|
@@ -174,4 +182,65 @@ public void testToXContent() throws IOException { |
174 | 182 | + "}")); |
175 | 183 | } |
176 | 184 |
|
| 185 | + public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { |
| 186 | + final NavigableMap<Long, ElasticsearchException> fetchExceptions = |
| 187 | + new TreeMap<>(Collections.singletonMap(1L, new ElasticsearchException("shard is sad"))); |
| 188 | + final ShardFollowNodeTaskStatus status = new ShardFollowNodeTaskStatus( |
| 189 | + "cluster_alias:leader_index", |
| 190 | + "follower_index", |
| 191 | + 0, |
| 192 | + 1, |
| 193 | + 1, |
| 194 | + 1, |
| 195 | + 1, |
| 196 | + 1, |
| 197 | + 1, |
| 198 | + 1, |
| 199 | + 1, |
| 200 | + 1, |
| 201 | + 100, |
| 202 | + 10, |
| 203 | + 0, |
| 204 | + 10, |
| 205 | + 100, |
| 206 | + 10, |
| 207 | + 10, |
| 208 | + 0, |
| 209 | + 10, |
| 210 | + fetchExceptions, |
| 211 | + 2); |
| 212 | + XContentBuilder builder = jsonBuilder(); |
| 213 | + builder.value(status); |
| 214 | + Map<String, Object> serializedStatus = XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(builder), false); |
| 215 | + |
| 216 | + Map<String, Object> template = |
| 217 | + XContentHelper.convertToMap(XContentType.JSON.xContent(), MonitoringTemplateUtils.loadTemplate("es"), false); |
| 218 | + Map<?, ?> ccrStatsMapping = (Map<?, ?>) XContentMapValues.extractValue("mappings.doc.properties.ccr_stats.properties", template); |
| 219 | + |
| 220 | + assertThat(serializedStatus.size(), equalTo(ccrStatsMapping.size())); |
| 221 | + for (Map.Entry<String, Object> entry : serializedStatus.entrySet()) { |
| 222 | + String fieldName = entry.getKey(); |
| 223 | + Map<?, ?> fieldMapping = (Map<?, ?>) ccrStatsMapping.get(fieldName); |
| 224 | + assertThat(fieldMapping, notNullValue()); |
| 225 | + |
| 226 | + Object fieldValue = entry.getValue(); |
| 227 | + String fieldType = (String) fieldMapping.get("type"); |
| 228 | + if (fieldValue instanceof Long || fieldValue instanceof Integer) { |
| 229 | + assertThat("expected long field type for field [" + fieldName + "]", fieldType, |
| 230 | + anyOf(equalTo("long"), equalTo("integer"))); |
| 231 | + } else if (fieldValue instanceof String) { |
| 232 | + assertThat("expected keyword field type for field [" + fieldName + "]", fieldType, |
| 233 | + anyOf(equalTo("keyword"), equalTo("text"))); |
| 234 | + } else { |
| 235 | + // Manual test specific object fields and if not just fail: |
| 236 | + if (fieldName.equals("fetch_exceptions")) { |
| 237 | + assertThat(XContentMapValues.extractValue("properties.from_seq_no.type", fieldMapping), equalTo("long")); |
| 238 | + assertThat(XContentMapValues.extractValue("properties.exception.type", fieldMapping), equalTo("text")); |
| 239 | + } else { |
| 240 | + fail("unexpected field value type [" + fieldValue.getClass() + "] for field [" + fieldName + "]"); |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + |
177 | 246 | } |
0 commit comments