Skip to content

Commit 2edf87b

Browse files
committed
Add unit tests for age_in_millis field in IndexLifecycleExplainResponse
1 parent b51bd9f commit 2edf87b

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/IndexLifecycleExplainResponseTests.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
import org.elasticsearch.xcontent.ParseField;
2121
import org.elasticsearch.xcontent.ToXContentObject;
2222
import org.elasticsearch.xcontent.XContentBuilder;
23+
import org.elasticsearch.xcontent.XContentFactory;
2324
import org.elasticsearch.xcontent.XContentParser;
2425

2526
import java.io.IOException;
2627
import java.util.List;
28+
import java.util.Map;
2729
import java.util.Objects;
2830
import java.util.function.Supplier;
2931

3032
import static org.hamcrest.Matchers.containsString;
3133
import static org.hamcrest.Matchers.equalTo;
34+
import static org.hamcrest.Matchers.hasKey;
3235
import static org.hamcrest.Matchers.is;
36+
import static org.hamcrest.Matchers.not;
3337
import static org.hamcrest.Matchers.notNullValue;
3438
import static org.hamcrest.Matchers.nullValue;
3539
import static org.hamcrest.Matchers.startsWith;
@@ -110,14 +114,16 @@ public void testInvalidStepDetails() {
110114
assertThat(exception.getMessage(), containsString("=null"));
111115
}
112116

113-
public void testIndexAges() {
117+
public void testIndexAges() throws IOException {
114118
IndexLifecycleExplainResponse unmanagedExplainResponse = randomUnmanagedIndexExplainResponse();
115119
assertThat(unmanagedExplainResponse.getLifecycleDate(), is(nullValue()));
116120
assertThat(unmanagedExplainResponse.getAge(System::currentTimeMillis), is(TimeValue.MINUS_ONE));
117121

118122
assertThat(unmanagedExplainResponse.getIndexCreationDate(), is(nullValue()));
119123
assertThat(unmanagedExplainResponse.getTimeSinceIndexCreation(System::currentTimeMillis), is(nullValue()));
120124

125+
assertAgeInMillisXContentAbsentForUnmanagedResponse(unmanagedExplainResponse);
126+
121127
IndexLifecycleExplainResponse managedExplainResponse = IndexLifecycleExplainResponse.newManagedIndexResponse(
122128
"indexName",
123129
12345L,
@@ -155,6 +161,46 @@ public void testIndexAges() {
155161
is(equalTo(TimeValue.timeValueMillis(now - managedExplainResponse.getIndexCreationDate())))
156162
);
157163
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+
158204
}
159205

160206
@Override

0 commit comments

Comments
 (0)