/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.sql.plugin.sql.action;

import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
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.sql.JDBCType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SqlResponseTests extends AbstractStreamableTestCase<SqlResponse> {
    static Cursor randomCursor() {
        return randomBoolean() ? Cursor.EMPTY : ScrollCursorTests.randomScrollCursor();
    }

    @Override
    protected NamedWriteableRegistry getNamedWriteableRegistry() {
        return new NamedWriteableRegistry(SqlPlugin.getNamedWriteables());
    }

    @Override
    protected SqlResponse createTestInstance() {
        int columnCount = between(1, 10);

        List<ColumnInfo> columns = null;
        if (randomBoolean()) {
            columns = new ArrayList<>(columnCount);
            for (int i = 0; i < columnCount; i++) {
                columns.add(new ColumnInfo(randomAlphaOfLength(10), randomAlphaOfLength(10), randomFrom(JDBCType.values()), randomInt(25)));
            }
        }

        List<List<Object>> rows;
        if (randomBoolean()) {
            rows = Collections.emptyList();
        } else {
            int rowCount = between(1, 10);
            rows = new ArrayList<>(rowCount);
            for (int r = 0; r < rowCount; r++) {
                List<Object> row = new ArrayList<>(rowCount);
                for (int c = 0; c < columnCount; c++) {
                    row.add(randomBoolean() ? randomAlphaOfLength(10) : randomInt());
                }
                rows.add(row);
            }
        }

        return new SqlResponse(randomCursor(), randomNonNegativeLong(), columnCount, columns, rows);
    }

    @Override
    protected SqlResponse createBlankInstance() {
        return new SqlResponse();
    }

    // NOCOMMIT add tests for toXcontent
}

@@ -357,16 +357,242 @@
 gistry;%0A
+import org.elasticsearch.common.xcontent.ToXContent;%0Aimport org.elasticsearch.common.xcontent.XContentBuilder;%0Aimport org.elasticsearch.common.xcontent.XContentFactory;%0Aimport org.elasticsearch.common.xcontent.XContentHelper;%0A
 import o
@@ -641,16 +641,16 @@
 stCase;%0A
-
 import o
@@ -894,16 +894,44 @@
 ursor;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -1028,16 +1028,84 @@
 il.List;
+%0Aimport java.util.Map;%0A%0Aimport static org.hamcrest.Matchers.hasSize;
 %0A%0Apublic
@@ -2681,43 +2681,1542 @@
     
-// NOCOMMIT add tests for toXcontent
+public void testToXContent() throws IOException %7B%0A        SqlResponse testInstance = createTestInstance();%0A%0A        XContentBuilder builder = testInstance.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);%0A        Map%3CString, Object%3E rootMap = XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2();%0A%0A        logger.info(builder.string());%0A%0A        assertEquals(testInstance.size(), rootMap.get(%22size%22));%0A        if (testInstance.columns() != null) %7B%0A            List%3C?%3E columns = (List%3C?%3E) rootMap.get(%22columns%22);%0A            assertThat(columns, hasSize(testInstance.columns().size()));%0A            for (int i = 0; i %3C columns.size(); i++) %7B%0A                Map%3C?, ?%3E columnMap = (Map%3C?, ?%3E) columns.get(i);%0A                ColumnInfo columnInfo = testInstance.columns().get(i);%0A                assertEquals(columnInfo.name(), columnMap.get(%22name%22));%0A                assertEquals(columnInfo.esType(), columnMap.get(%22type%22));%0A            %7D%0A        %7D else %7B%0A            assertNull(rootMap.get(%22columns%22));%0A        %7D%0A%0A        List%3C?%3E rows = ((List%3C?%3E) rootMap.get(%22rows%22));%0A        assertThat(rows, hasSize(testInstance.rows().size()));%0A        for (int i = 0; i %3C rows.size(); i++) %7B%0A            List%3C?%3E row = (List%3C?%3E) rows.get(i);%0A            assertEquals(row, testInstance.rows().get(i));%0A        %7D%0A%0A        if (testInstance.cursor() != Cursor.EMPTY) %7B%0A            assertEquals(rootMap.get(SqlRequest.CURSOR.getPreferredName()), Cursor.encodeToString(testInstance.cursor()));%0A        %7D%0A    %7D
 %0A%7D%0A

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