SQL: Add toXcontent test for SqlResponse (elastic/x-pack-elasticsearch#2726)

Original commit: elastic/x-pack-elasticsearch@aa7b8f1a91
This commit is contained in:
Igor Motov 2017-10-11 19:55:59 -04:00 committed by GitHub
parent 2026198dd4
commit 06b4c043b9
1 changed files with 41 additions and 1 deletions

View File

@ -6,16 +6,24 @@
package org.elasticsearch.xpack.sql.plugin.sql.action;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.AbstractStreamableTestCase;
import org.elasticsearch.xpack.sql.execution.search.ScrollCursorTests;
import org.elasticsearch.xpack.sql.plugin.SqlPlugin;
import org.elasticsearch.xpack.sql.plugin.sql.action.SqlResponse.ColumnInfo;
import org.elasticsearch.xpack.sql.session.Cursor;
import java.io.IOException;
import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.hasSize;
public class SqlResponseTests extends AbstractStreamableTestCase<SqlResponse> {
static Cursor randomCursor() {
@ -62,5 +70,37 @@ public class SqlResponseTests extends AbstractStreamableTestCase<SqlResponse> {
return new SqlResponse();
}
// NOCOMMIT add tests for toXcontent
public void testToXContent() throws IOException {
SqlResponse testInstance = createTestInstance();
XContentBuilder builder = testInstance.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);
Map<String, Object> rootMap = XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2();
logger.info(builder.string());
assertEquals(testInstance.size(), rootMap.get("size"));
if (testInstance.columns() != null) {
List<?> columns = (List<?>) rootMap.get("columns");
assertThat(columns, hasSize(testInstance.columns().size()));
for (int i = 0; i < columns.size(); i++) {
Map<?, ?> columnMap = (Map<?, ?>) columns.get(i);
ColumnInfo columnInfo = testInstance.columns().get(i);
assertEquals(columnInfo.name(), columnMap.get("name"));
assertEquals(columnInfo.esType(), columnMap.get("type"));
}
} else {
assertNull(rootMap.get("columns"));
}
List<?> rows = ((List<?>) rootMap.get("rows"));
assertThat(rows, hasSize(testInstance.rows().size()));
for (int i = 0; i < rows.size(); i++) {
List<?> row = (List<?>) rows.get(i);
assertEquals(row, testInstance.rows().get(i));
}
if (testInstance.cursor() != Cursor.EMPTY) {
assertEquals(rootMap.get(SqlRequest.CURSOR.getPreferredName()), Cursor.encodeToString(testInstance.cursor()));
}
}
}