2424
2525import static org .elasticsearch .xcontent .ToXContent .EMPTY_PARAMS ;
2626import static org .hamcrest .Matchers .equalTo ;
27+ import static org .hamcrest .Matchers .instanceOf ;
2728import static org .junit .Assert .assertNotSame ;
2829
2930public class SamplingServiceSampleStatsTests extends AbstractWireSerializingTestCase <SampleStats > {
@@ -148,13 +149,19 @@ public void testToXContent() throws IOException {
148149 parserMap .get ("samples_rejected_for_max_samples_exceeded" ),
149150 equalTo (sampleStats .getSamplesRejectedForMaxSamplesExceeded ())
150151 );
151- assertThat (parserMap .get ("samples_rejected_for_condition" ), equalTo (sampleStats .getSamplesRejectedForCondition ()));
152- assertThat (parserMap .get ("samples_rejected_for_rate" ), equalTo (sampleStats .getSamplesRejectedForRate ()));
153- assertThat (parserMap .get ("samples_rejected_for_exception" ), equalTo (sampleStats .getSamplesRejectedForException ()));
154- assertThat (parserMap .get ("samples_rejected_for_size" ), equalTo (sampleStats .getSamplesRejectedForSize ()));
155- assertThat (parserMap .get ("samples_accepted" ), equalTo (sampleStats .getSamples ()));
156- assertThat (parserMap .get ("time_sampling_millis" ), equalTo (sampleStats .getTimeSampling ().millis ()));
157- assertThat (parserMap .get ("time_compiling_condition_millis" ), equalTo (sampleStats .getTimeCompilingCondition ().millis ()));
152+ assertNumberEqualsLong (parserMap .get ("samples_rejected_for_condition" ), sampleStats .getSamplesRejectedForCondition ());
153+ assertNumberEqualsLong (parserMap .get ("samples_rejected_for_rate" ), sampleStats .getSamplesRejectedForRate ());
154+ assertNumberEqualsLong (parserMap .get ("samples_rejected_for_exception" ), sampleStats .getSamplesRejectedForException ());
155+ assertNumberEqualsLong (parserMap .get ("samples_rejected_for_size" ), sampleStats .getSamplesRejectedForSize ());
156+ assertNumberEqualsLong (parserMap .get ("samples_accepted" ), sampleStats .getSamples ());
157+ assertNumberEqualsLong (
158+ ((Number ) parserMap .get ("time_sampling_millis" )).longValue (),
159+ sampleStats .getTimeSampling ().millis ()
160+ );
161+ assertNumberEqualsLong (
162+ ((Number ) parserMap .get ("time_compiling_condition_millis" )).longValue (),
163+ sampleStats .getTimeCompilingCondition ().millis ()
164+ );
158165 if (humanReadable ) {
159166 assertThat (parserMap .get ("time_sampling" ), equalTo (sampleStats .getTimeSampling ().toHumanReadableString (1 )));
160167 assertThat (
@@ -174,4 +181,13 @@ public void testToXContent() throws IOException {
174181 }
175182 }
176183 }
184+
185+ /*
186+ * The XContentParser::map will return numbers as Integers if they are small enough. In that case, the actual and expected will not be
187+ * equal if the expected is a long. This method gets the long value of the actual result before asserting that they are equal.
188+ */
189+ private void assertNumberEqualsLong (Object actual , long expected ) {
190+ assertThat (actual , instanceOf (Number .class ));
191+ assertThat (((Number ) actual ).longValue (), equalTo (expected ));
192+ }
177193}
0 commit comments