public void testFromXContent() throws IOException { SearchSortValues sortValues = createTestItem(); XContentType xcontentType = randomFrom(XContentType.values()); XContentBuilder builder = XContentFactory.contentBuilder(xcontentType); if (randomBoolean()) { builder.prettyPrint(); } builder .startObject(); // we need to wrap xContent output in proper object to create a parser for // it builder = sortValues.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); XContentParser parser = createParser(builder); parser .nextToken(); // skip to the elements field name token, fromXContent advances from there if // called from ourside parser.nextToken(); if (sortValues.sortValues().length > 0) { SearchSortValues parsed = SearchSortValues.fromXContent(parser); assertToXContentEquivalent( builder.bytes(), toXContent(parsed, xcontentType, true), xcontentType); parser.nextToken(); } assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); assertNull(parser.nextToken()); }
public void testToXContent() throws IOException { SearchSortValues sortValues = new SearchSortValues(new Object[] {1, "foo", 3.0}); XContentBuilder builder = JsonXContent.contentBuilder(); builder.startObject(); sortValues.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); assertEquals("{\"sort\":[1,\"foo\",3.0]}", builder.string()); }
public void testSerialization() throws IOException { SearchSortValues sortValues = createTestItem(); try (BytesStreamOutput output = new BytesStreamOutput()) { sortValues.writeTo(output); try (StreamInput in = output.bytes().streamInput()) { SearchSortValues deserializedCopy = new SearchSortValues(in); assertEquals(sortValues, deserializedCopy); assertEquals(sortValues.hashCode(), deserializedCopy.hashCode()); assertNotSame(sortValues, deserializedCopy); } } }
private static SearchSortValues mutate(SearchSortValues original) { Object[] sortValues = original.sortValues(); if (sortValues.length == 0) { return new SearchSortValues(new Object[] {1}); } return new SearchSortValues(Arrays.copyOf(sortValues, sortValues.length + 1)); }
private static SearchSortValues copy(SearchSortValues original) { return new SearchSortValues(Arrays.copyOf(original.sortValues(), original.sortValues().length)); }