|
20 | 20 | import org.elasticsearch.xcontent.ParseField;
|
21 | 21 | import org.elasticsearch.xcontent.ToXContentObject;
|
22 | 22 | import org.elasticsearch.xcontent.XContentBuilder;
|
| 23 | +import org.elasticsearch.xcontent.XContentFactory; |
23 | 24 | import org.elasticsearch.xcontent.XContentParser;
|
24 | 25 |
|
25 | 26 | import java.io.IOException;
|
26 | 27 | import java.util.List;
|
| 28 | +import java.util.Map; |
27 | 29 | import java.util.Objects;
|
28 | 30 | import java.util.function.Supplier;
|
29 | 31 |
|
30 | 32 | import static org.hamcrest.Matchers.containsString;
|
31 | 33 | import static org.hamcrest.Matchers.equalTo;
|
| 34 | +import static org.hamcrest.Matchers.hasKey; |
32 | 35 | import static org.hamcrest.Matchers.is;
|
| 36 | +import static org.hamcrest.Matchers.not; |
33 | 37 | import static org.hamcrest.Matchers.notNullValue;
|
34 | 38 | import static org.hamcrest.Matchers.nullValue;
|
35 | 39 | import static org.hamcrest.Matchers.startsWith;
|
@@ -110,14 +114,16 @@ public void testInvalidStepDetails() {
|
110 | 114 | assertThat(exception.getMessage(), containsString("=null"));
|
111 | 115 | }
|
112 | 116 |
|
113 |
| - public void testIndexAges() { |
| 117 | + public void testIndexAges() throws IOException { |
114 | 118 | IndexLifecycleExplainResponse unmanagedExplainResponse = randomUnmanagedIndexExplainResponse();
|
115 | 119 | assertThat(unmanagedExplainResponse.getLifecycleDate(), is(nullValue()));
|
116 | 120 | assertThat(unmanagedExplainResponse.getAge(System::currentTimeMillis), is(TimeValue.MINUS_ONE));
|
117 | 121 |
|
118 | 122 | assertThat(unmanagedExplainResponse.getIndexCreationDate(), is(nullValue()));
|
119 | 123 | assertThat(unmanagedExplainResponse.getTimeSinceIndexCreation(System::currentTimeMillis), is(nullValue()));
|
120 | 124 |
|
| 125 | + assertAgeInMillisXContentAbsentForUnmanagedResponse(unmanagedExplainResponse); |
| 126 | + |
121 | 127 | IndexLifecycleExplainResponse managedExplainResponse = IndexLifecycleExplainResponse.newManagedIndexResponse(
|
122 | 128 | "indexName",
|
123 | 129 | 12345L,
|
@@ -155,6 +161,46 @@ public void testIndexAges() {
|
155 | 161 | is(equalTo(TimeValue.timeValueMillis(now - managedExplainResponse.getIndexCreationDate())))
|
156 | 162 | );
|
157 | 163 | assertThat(managedExplainResponse.getTimeSinceIndexCreation(() -> 0L), is(equalTo(TimeValue.ZERO)));
|
| 164 | + |
| 165 | + long expectedAgeInMillisForThisCase = Math.max(0L, now - managedExplainResponse.getLifecycleDate()); |
| 166 | + assertAgeInMillisXContent(managedExplainResponse, expectedAgeInMillisForThisCase, now); |
| 167 | + } |
| 168 | + |
| 169 | + protected void assertAgeInMillisXContent( |
| 170 | + final IndexLifecycleExplainResponse managedExplainResponse, |
| 171 | + final long expectedAgeInMillis, |
| 172 | + final long now |
| 173 | + ) throws IOException { |
| 174 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 175 | + managedExplainResponse.nowSupplier = () -> now; |
| 176 | + try (builder) { |
| 177 | + managedExplainResponse.toXContent(builder, ToXContentObject.EMPTY_PARAMS); |
| 178 | + } |
| 179 | + final String json = Strings.toString(builder); |
| 180 | + |
| 181 | + try (XContentParser parser = createParser(builder.contentType().xContent(), json)) { |
| 182 | + Map<String, Object> parsedMap = parser.map(); |
| 183 | + |
| 184 | + assertThat(parsedMap, hasKey("age_in_millis")); |
| 185 | + final long actualParsedAgeInMillis = ((Number) parsedMap.get("age_in_millis")).longValue(); |
| 186 | + assertThat(actualParsedAgeInMillis, equalTo((Number) expectedAgeInMillis)); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + protected void assertAgeInMillisXContentAbsentForUnmanagedResponse(final IndexLifecycleExplainResponse unmanagedExplainResponse) |
| 191 | + throws IOException { |
| 192 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 193 | + try (builder) { |
| 194 | + unmanagedExplainResponse.toXContent(builder, ToXContentObject.EMPTY_PARAMS); |
| 195 | + } |
| 196 | + final String json = Strings.toString(builder); |
| 197 | + |
| 198 | + try (XContentParser parser = createParser(builder.contentType().xContent(), json)) { |
| 199 | + Map<String, Object> parsedMap = parser.map(); |
| 200 | + |
| 201 | + assertThat(parsedMap, not(hasKey("age_in_millis"))); |
| 202 | + } |
| 203 | + |
158 | 204 | }
|
159 | 205 |
|
160 | 206 | @Override
|
|
0 commit comments