Sort { public JsonObject toJsonObject() { JsonObject sortDefinition = new JsonObject(); if (order != null) { sortDefinition.add("order", new JsonPrimitive(order.toString())); } if (missing != null) { sortDefinition.add("missing", new JsonPrimitive(missing.toString())); } if (unmapped != null) { sortDefinition.add("ignore_unmapped", new JsonPrimitive(unmapped)); } if(unmappedType != null) { sortDefinition.add("unmapped_type", new JsonPrimitive(unmappedType)); } JsonObject sortObject = new JsonObject(); sortObject.add(field, sortDefinition); return sortObject; } Sort(String field);  Sort(String field, Sorting order); void setMissing(Object m); void setIgnoreUnmapped(); void setUnmappedType(String unmappedType); JsonObject toJsonObject();  }
@Test public void testJsonSerializationForSimpleFieldSort() throws JSONException { String expectedJson = "{\"my_field\":{}}"; Sort s = new Sort("my_field"); JsonObject actualJsonObject = s.toJsonObject(); JSONAssert.assertEquals(expectedJson, new Gson().toJson(actualJsonObject), false); }
@Test public void testJsonSerializationForAscOrder() throws JSONException { String expectedJson = "{\"my_field\":{\"order\":\"asc\"}}"; Sort s = new Sort("my_field", Sort.Sorting.ASC); JsonObject actualJsonObject = s.toJsonObject(); JSONAssert.assertEquals(expectedJson, new Gson().toJson(actualJsonObject), false); }
@Test public void testJsonSerializationForDescOrder() throws JSONException { String expectedJson = "{\"my_field\":{\"order\":\"desc\"}}"; Sort s = new Sort("my_field", Sorting.DESC); JsonObject actualJsonObject = s.toJsonObject(); JSONAssert.assertEquals(expectedJson, new Gson().toJson(actualJsonObject), false); }