/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.cassandra.cql3;

import org.junit.BeforeClass;
import org.junit.Test;

import org.apache.cassandra.SchemaLoader;
import org.apache.cassandra.db.marshal.Int32Type;
import org.apache.cassandra.schema.KeyspaceParams;
import org.apache.cassandra.utils.ByteBufferUtil;

import static org.junit.Assert.assertEquals;

public class ThriftCompatibilityTest extends SchemaLoader
{
    @BeforeClass
    public static void defineSchema() throws Exception
    {
        // The before class annotation of SchemaLoader will prepare the service so no need to do it here
        SchemaLoader.createKeyspace("thriftcompat",
                                    KeyspaceParams.simple(1),
                                    SchemaLoader.jdbcCFMD("thriftcompat", "JdbcInteger", Int32Type.instance)
                                                .addColumnDefinition(integerColumn("thriftcompat", "JdbcInteger")));
    }

    private static UntypedResultSet execute(String query)
    {
        return QueryProcessor.executeInternal(query);
    }

    /** Test For CASSANDRA-8178 */
    @Test
    public void testNonTextComparator() throws Throwable
    {
        // the comparator is IntegerType, and there is a column named 42 with a UTF8Type validation type
        execute("INSERT INTO \"thriftcompat\".\"JdbcInteger\" (key, \"42\") VALUES (0x00000001, 'abc')");
        execute("UPDATE \"thriftcompat\".\"JdbcInteger\" SET \"42\" = 'abc' WHERE key = 0x00000001");
        execute("DELETE \"42\" FROM \"thriftcompat\".\"JdbcInteger\" WHERE key = 0x00000000");
        UntypedResultSet results = execute("SELECT key, \"42\" FROM \"thriftcompat\".\"JdbcInteger\"");
        assertEquals(1, results.size());
        UntypedResultSet.Row row = results.iterator().next();
        assertEquals(ByteBufferUtil.bytes(1), row.getBytes("key"));
        assertEquals("abc", row.getString("42"));
    }
}

@@ -835,16 +835,47 @@
 .cql3;%0A%0A
+import java.util.Collections;%0A%0A
 import o
@@ -896,16 +896,16 @@
 eClass;%0A
-
 import o
@@ -962,16 +962,113 @@
 Loader;%0A
+import org.apache.cassandra.config.CFMetaData;%0Aimport org.apache.cassandra.db.marshal.BytesType;%0A
 import o
@@ -1105,24 +1105,73 @@
 .Int32Type;%0A
+import org.apache.cassandra.db.marshal.UTF8Type;%0A
 import org.a
@@ -1209,16 +1209,157 @@
 Params;%0A
+import org.apache.cassandra.thrift.CfDef;%0Aimport org.apache.cassandra.thrift.ColumnDef;%0Aimport org.apache.cassandra.thrift.ThriftConversion;%0A
 import o
@@ -1445,16 +1445,79 @@
 tEquals;
+%0Aimport static org.apache.cassandra.utils.ByteBufferUtil.bytes;
 %0A%0Apublic
@@ -1808,35 +1808,169 @@
 at%22,
+ KeyspaceParams.simple(1), thriftCFM(%22thriftcompat%22, %22JdbcInteger%22));
 %0A    
+%7D%0A%0A
     
-      
+public static CFMetaData thriftCFM(String keyspace, String table)%0A
     
+%7B%0A
         
     
@@ -1969,133 +1969,260 @@
     
-          KeyspaceParams.simple(1),%0A                                    SchemaLoader.jdbcCFMD(%22thriftcompat%22, %22JdbcInteger%22, 
+ColumnDef column = new ColumnDef();%0A        column.setName(bytes(42))%0A              .setValidation_class(UTF8Type.instance.toString());%0A%0A        CfDef cf = new CfDef(keyspace, table);%0A        cf.setColumn_type(%22Standard%22)%0A          .setComparator_type(
 Int3
@@ -2235,16 +2235,27 @@
 instance
+.toString()
 )%0A      
@@ -2262,36 +2262,133 @@
     
-                            
+.setDefault_validation_class(UTF8Type.instance.toString())%0A          .setKey_validation_class(BytesType.instance.toString())%0A
     
@@ -2398,73 +2398,109 @@
    .
-add
+set
 Column
-Definition(integerColumn(%22thriftcompat%22, %22JdbcInteger%22))
+_metadata(Collections.singletonList(column));%0A%0A        return ThriftConversion.fromThrift(cf
 );%0A 

Make ThriftCompatibilityTest use an actual Thrift CF