jyothsnakonisa commented on code in PR #4411:
URL: https://github.com/apache/cassandra/pull/4411#discussion_r2456667946


##########
test/unit/org/apache/cassandra/cql3/validation/miscellaneous/CommentAndSecurityLabelTest.java:
##########
@@ -0,0 +1,506 @@
+/*
+ * 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.validation.miscellaneous;
+
+import org.junit.Test;
+
+import com.datastax.driver.core.ResultSet;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.ColumnIdentifier;
+import org.apache.cassandra.db.marshal.UserType;
+import org.apache.cassandra.schema.ColumnMetadata;
+import org.apache.cassandra.schema.KeyspaceParams;
+import org.apache.cassandra.schema.Schema;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.schema.TableParams;
+
+import static org.apache.cassandra.utils.ByteBufferUtil.bytes;
+import static org.junit.Assert.assertEquals;
+
+public class CommentAndSecurityLabelTest extends CQLTester
+{
+    public static final String KEYSPACE_NAME = "ks_comment";
+    public static final String SECURITY_KEYSPACE = "ks_security";
+    public static final String TABLE_NAME = "tbl_comment";
+    public static final String SECURITY_TABLE_NAME = "tbl_security";
+    public static final String TYPE_NAME = "address";
+
+    // Test data constants
+    private static final String TEST_COMMENT = "Test comment";
+    private static final String UPDATED_COMMENT = "Updated comment";
+    private static final String TEST_LABEL = "TEST_LABEL";
+    private static final String UPDATED_LABEL = "UPDATED_LABEL";
+
+    enum ObjectType
+    {KEYSPACE, TABLE, COLUMN, TYPE, FIELD}
+
+    @Test
+    public void testCommentOnKeyspace()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        testCommentLifecycle(ObjectType.KEYSPACE, KEYSPACE_NAME, 
KEYSPACE_NAME);
+    }
+
+    @Test
+    public void testSecurityLabelOnKeyspace()
+    {
+        createKeyspaceWithName(SECURITY_KEYSPACE);
+        testSecurityLabelLifecycle(ObjectType.KEYSPACE, SECURITY_KEYSPACE, 
SECURITY_KEYSPACE);
+
+        // Test provider warning
+        ResultSet result = executeNet(String.format("SECURITY LABEL FOR 
test_provider ON KEYSPACE %s IS 'SENSITIVE'", SECURITY_KEYSPACE));
+        assertWarningsContain(result.getExecutionInfo().getWarnings(), 
"Provider is not yet implemented");
+        assertSecurityLabel(ObjectType.KEYSPACE, SECURITY_KEYSPACE, 
SECURITY_KEYSPACE, "SENSITIVE");
+    }
+
+    @Test
+    public void testCommentOnTable()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        createTableWithName(KEYSPACE_NAME, TABLE_NAME);
+        String tableRef = String.format("%s.%s", KEYSPACE_NAME, TABLE_NAME);
+        testCommentLifecycle(ObjectType.TABLE, KEYSPACE_NAME, tableRef);
+    }
+
+    @Test
+    public void testSecurityLabelOnTable()
+    {
+        createKeyspaceWithName(SECURITY_KEYSPACE);
+        createTableWithName(SECURITY_KEYSPACE, SECURITY_TABLE_NAME);
+        String tableRef = String.format("%s.%s", SECURITY_KEYSPACE, 
SECURITY_TABLE_NAME);
+        testSecurityLabelLifecycle(ObjectType.TABLE, SECURITY_KEYSPACE, 
tableRef);
+
+        // Test provider warning
+        ResultSet result = executeNet(String.format("SECURITY LABEL FOR 
my_provider ON TABLE %s IS 'CONFIDENTIAL'", tableRef));
+        assertWarningsContain(result.getExecutionInfo().getWarnings(), 
"Provider is not yet implemented");
+        assertSecurityLabel(ObjectType.TABLE, SECURITY_KEYSPACE, tableRef, 
"CONFIDENTIAL");
+    }
+
+    @Test
+    public void testCommentOnColumn()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        createTableWithName(KEYSPACE_NAME, TABLE_NAME);
+        String columnRef = String.format("%s.%s.name", KEYSPACE_NAME, 
TABLE_NAME);
+        testCommentLifecycle(ObjectType.COLUMN, KEYSPACE_NAME, columnRef);
+    }
+
+    @Test
+    public void testSecurityLabelOnColumn()
+    {
+        createKeyspaceWithName(SECURITY_KEYSPACE);
+        String createTableQuery = String.format("CREATE TABLE %s.%s (id int 
PRIMARY KEY, ssn text, name text)", SECURITY_KEYSPACE, SECURITY_TABLE_NAME);
+        createTable(createTableQuery);
+        String columnRef = String.format("%s.%s.ssn", SECURITY_KEYSPACE, 
SECURITY_TABLE_NAME);
+        testSecurityLabelLifecycle(ObjectType.COLUMN, SECURITY_KEYSPACE, 
columnRef);
+
+        // Test provider warning
+        ResultSet result = executeNet(String.format("SECURITY LABEL FOR 
data_classifier ON COLUMN %s IS 'PII'", columnRef));
+        assertWarningsContain(result.getExecutionInfo().getWarnings(), 
"Provider is not yet implemented");
+        assertSecurityLabel(ObjectType.COLUMN, SECURITY_KEYSPACE, columnRef, 
"PII");
+    }
+
+    @Test
+    public void testCommentOnType()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        execute(String.format("CREATE TYPE %s.%s (street text, city text, zip 
int)", KEYSPACE_NAME, TYPE_NAME));
+        String typeRef = String.format("%s.%s", KEYSPACE_NAME, TYPE_NAME);
+        testCommentLifecycle(ObjectType.TYPE, KEYSPACE_NAME, typeRef);
+    }
+
+    @Test
+    public void testSecurityLabelOnType()
+    {
+        createKeyspaceWithName(SECURITY_KEYSPACE);
+        String typeName = "personal_info";
+        execute(String.format("CREATE TYPE %s.%s (ssn text, dob date)", 
SECURITY_KEYSPACE, typeName));
+        String typeRef = String.format("%s.%s", SECURITY_KEYSPACE, typeName);
+        testSecurityLabelLifecycle(ObjectType.TYPE, SECURITY_KEYSPACE, 
typeRef);
+
+        // Test provider warning
+        ResultSet result = executeNet(String.format("SECURITY LABEL FOR 
security_provider ON TYPE %s IS 'RESTRICTED'", typeRef));
+        assertWarningsContain(result.getExecutionInfo().getWarnings(), 
"Provider is not yet implemented");
+        assertSecurityLabel(ObjectType.TYPE, SECURITY_KEYSPACE, typeRef, 
"RESTRICTED");
+    }
+
+    @Test
+    public void testCommentOnField()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        execute(String.format("CREATE TYPE %s.geo_position (latitude double, 
longitude double, altitude double)", KEYSPACE_NAME));
+        String fieldRef = String.format("%s.geo_position.latitude", 
KEYSPACE_NAME);
+        testCommentLifecycle(ObjectType.FIELD, KEYSPACE_NAME, fieldRef);
+    }
+
+    @Test
+    public void testSecurityLabelOnField()
+    {
+        createKeyspaceWithName(SECURITY_KEYSPACE);
+        execute(String.format("CREATE TYPE %s.patient_record (ssn text, 
diagnosis text, treatment text)", SECURITY_KEYSPACE));
+        String fieldRef = String.format("%s.patient_record.ssn", 
SECURITY_KEYSPACE);
+        testSecurityLabelLifecycle(ObjectType.FIELD, SECURITY_KEYSPACE, 
fieldRef);
+
+        // Test provider warning
+        ResultSet result = executeNet(String.format("SECURITY LABEL FOR 
healthcare_provider ON FIELD %s IS 'PHI'", fieldRef));
+        assertWarningsContain(result.getExecutionInfo().getWarnings(), 
"Provider is not yet implemented");
+        assertSecurityLabel(ObjectType.FIELD, SECURITY_KEYSPACE, fieldRef, 
"PHI");
+    }
+
+    @Test
+    public void testFieldWithUseKeyspace()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        execute(String.format("CREATE TYPE %s.address (street text, city text, 
zip int)", KEYSPACE_NAME));
+        execute(String.format("USE %s", KEYSPACE_NAME));
+
+        // Test unqualified field reference with USE KEYSPACE context
+        setComment(ObjectType.FIELD, "address.street", "Street address");
+        setSecurityLabel(ObjectType.FIELD, "address.street", "PUBLIC");
+        setComment(ObjectType.FIELD, "address.city", "City name");
+        setSecurityLabel(ObjectType.FIELD, "address.city", "PUBLIC");
+
+        // Verify
+        assertComment(ObjectType.FIELD, KEYSPACE_NAME, "address.street", 
"Street address");
+        assertSecurityLabel(ObjectType.FIELD, KEYSPACE_NAME, "address.street", 
"PUBLIC");
+        assertComment(ObjectType.FIELD, KEYSPACE_NAME, "address.city", "City 
name");
+        assertSecurityLabel(ObjectType.FIELD, KEYSPACE_NAME, "address.city", 
"PUBLIC");
+    }
+
+    @Test
+    public void testErrorCases()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        createTableWithName(KEYSPACE_NAME, TABLE_NAME);
+        execute(String.format("CREATE TYPE %s.test_type (field1 text, field2 
int)", KEYSPACE_NAME));
+
+        // Test non-existent keyspace
+        String commentOnKeyspace = "COMMENT ON KEYSPACE nonexistent IS 
'comment'";
+        assertInvalidMessage("Keyspace 'nonexistent' doesn't exist", 
commentOnKeyspace);
+
+        // Test non-existent table
+        String commentOnTableQuery = String.format("COMMENT ON TABLE 
%s.nonexistent IS 'comment'", KEYSPACE_NAME);
+        assertInvalidMessage(String.format("Table '%s.nonexistent' doesn't 
exist", KEYSPACE_NAME), commentOnTableQuery);
+
+        // Test non-existent column
+        String commentOnColumnQuery = String.format("COMMENT ON COLUMN 
%s.%s.nonexistent IS 'comment'", KEYSPACE_NAME, TABLE_NAME);
+        assertInvalidMessage("Column 'nonexistent' doesn't exist", 
commentOnColumnQuery);
+
+        // Test non-existent type
+        String commentOnType = String.format("COMMENT ON TYPE %s.nonexistent 
IS 'comment'", KEYSPACE_NAME);
+        assertInvalidMessage("Type", commentOnType);
+
+        // Test non-existent type for field
+        String commentOnNonExistentType = String.format("COMMENT ON FIELD 
%s.nonexistent.somefield IS 'comment'", KEYSPACE_NAME);
+        assertInvalidMessage("doesn't exist", commentOnNonExistentType);
+
+        // Test non-existent field
+        String commentOnNonExistentField = String.format("COMMENT ON FIELD 
%s.test_type.nonexistent IS 'comment'", KEYSPACE_NAME);
+        assertInvalidMessage("doesn't exist", commentOnNonExistentField);
+    }
+
+    @Test
+    public void testMultipleOperations()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        createTableWithName(KEYSPACE_NAME, TABLE_NAME);
+        execute(String.format("CREATE TYPE %s.contact_info (phone text, 
address text)", KEYSPACE_NAME));
+
+        // Set comments and labels on multiple objects
+        String tableRef = String.format("%s.%s", KEYSPACE_NAME, TABLE_NAME);
+        String columnRef = String.format("%s.%s.name", KEYSPACE_NAME, 
TABLE_NAME);
+        String typeRef = String.format("%s.contact_info", KEYSPACE_NAME);
+
+        setComment(ObjectType.TABLE, tableRef, "User table");
+        setSecurityLabel(ObjectType.TABLE, tableRef, "USER_DATA");
+        setComment(ObjectType.COLUMN, columnRef, "User name");
+        setSecurityLabel(ObjectType.COLUMN, columnRef, "PUBLIC");
+        setComment(ObjectType.TYPE, typeRef, "Contact information");
+        setSecurityLabel(ObjectType.TYPE, typeRef, "PERSONAL");
+
+        // Verify all are set correctly
+        assertComment(ObjectType.TABLE, KEYSPACE_NAME, tableRef, "User table");
+        assertSecurityLabel(ObjectType.TABLE, KEYSPACE_NAME, tableRef, 
"USER_DATA");
+        assertComment(ObjectType.COLUMN, KEYSPACE_NAME, columnRef, "User 
name");
+        assertSecurityLabel(ObjectType.COLUMN, KEYSPACE_NAME, columnRef, 
"PUBLIC");
+        assertComment(ObjectType.TYPE, KEYSPACE_NAME, typeRef, "Contact 
information");
+        assertSecurityLabel(ObjectType.TYPE, KEYSPACE_NAME, typeRef, 
"PERSONAL");
+    }
+
+    @Test
+    public void testEmptyAndSpecialCharacters()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        createTableWithName(KEYSPACE_NAME, TABLE_NAME);
+        String tableRef = String.format("%s.%s", KEYSPACE_NAME, TABLE_NAME);
+
+        // Test empty string
+        setComment(ObjectType.TABLE, tableRef, "");
+        assertComment(ObjectType.TABLE, KEYSPACE_NAME, tableRef, "");
+
+        // Test special characters
+        String specialComment = "Comment with \"quotes\" and 'apostrophes' and 
\nnewlines";
+        setComment(ObjectType.TABLE, tableRef, specialComment);
+        assertComment(ObjectType.TABLE, KEYSPACE_NAME, tableRef, "Comment with 
\"quotes\" and 'apostrophes' and \nnewlines");
+
+        // Test Unicode characters
+        String unicodeComment = "Unicode comment: 测试 ñoño 🚀";
+        setComment(ObjectType.TABLE, tableRef, unicodeComment);
+        assertComment(ObjectType.TABLE, KEYSPACE_NAME, tableRef, 
unicodeComment);
+    }
+
+    @Test
+    public void testUseKeyspaceContext()
+    {
+        createKeyspaceWithName(KEYSPACE_NAME);
+        createTableWithName(KEYSPACE_NAME, TABLE_NAME);
+        execute(String.format("CREATE TYPE %s.test_type (field1 text, field2 
int)", KEYSPACE_NAME));
+
+        // Use the keyspace to set current context
+        execute(String.format("USE %s", KEYSPACE_NAME));
+
+        // Test unqualified names with USE KEYSPACE context
+        setComment(ObjectType.TABLE, TABLE_NAME, "Table comment via USE");
+        setSecurityLabel(ObjectType.TABLE, TABLE_NAME, "TABLE_LABEL");
+        setComment(ObjectType.COLUMN, TABLE_NAME + ".name", "Column comment 
via USE");
+        setSecurityLabel(ObjectType.COLUMN, TABLE_NAME + ".name", 
"COLUMN_LABEL");
+        setComment(ObjectType.TYPE, "test_type", "Type comment via USE");
+        setSecurityLabel(ObjectType.TYPE, "test_type", "TYPE_LABEL");
+
+        // Verify all are set correctly using the current keyspace context
+        assertComment(ObjectType.TABLE, KEYSPACE_NAME, TABLE_NAME, "Table 
comment via USE");
+        assertSecurityLabel(ObjectType.TABLE, KEYSPACE_NAME, TABLE_NAME, 
"TABLE_LABEL");
+        assertComment(ObjectType.COLUMN, KEYSPACE_NAME, TABLE_NAME + ".name", 
"Column comment via USE");
+        assertSecurityLabel(ObjectType.COLUMN, KEYSPACE_NAME, TABLE_NAME + 
".name", "COLUMN_LABEL");
+        assertComment(ObjectType.TYPE, KEYSPACE_NAME, "test_type", "Type 
comment via USE");
+        assertSecurityLabel(ObjectType.TYPE, KEYSPACE_NAME, "test_type", 
"TYPE_LABEL");
+    }
+
+
+    @Test
+    public void testCommentAndSecurityLabelOnVirtualTableFails()

Review Comment:
   Added tests.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to