|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.cassandra.cql3; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.BeforeClass; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import org.apache.cassandra.ServerTestUtils; |
| 29 | +import org.apache.cassandra.db.ConsistencyLevel; |
| 30 | +import org.apache.cassandra.transport.ClientNotificationsTest; |
| 31 | +import org.apache.cassandra.transport.Event; |
| 32 | +import org.apache.cassandra.transport.ProtocolVersion; |
| 33 | +import org.apache.cassandra.transport.SimpleClient; |
| 34 | +import org.apache.cassandra.transport.messages.RegisterMessage; |
| 35 | +import org.apache.cassandra.transport.messages.ResultMessage; |
| 36 | + |
| 37 | +import static java.lang.String.format; |
| 38 | +import static org.apache.cassandra.transport.Event.SchemaChange.Change.UPDATED; |
| 39 | +import static org.apache.cassandra.transport.Event.SchemaChange.Target.TABLE; |
| 40 | +import static org.apache.cassandra.transport.Event.SchemaChange.Target.TYPE; |
| 41 | +import static org.junit.Assert.assertEquals; |
| 42 | + |
| 43 | +public class CommentAndSecurityLabelClientTest extends CQLTester |
| 44 | +{ |
| 45 | + private static final String KEYSPACE_NAME = "ks_comment"; |
| 46 | + private static final String TABLE_NAME = "tbl_comment"; |
| 47 | + private static final String COLUMN_NAME = "name"; |
| 48 | + private static final String TYPE_NAME = "type_comment"; |
| 49 | + private static final String FIELD_NAME = "f1"; |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static void setUpClass() |
| 53 | + { |
| 54 | + ServerTestUtils.daemonInitialization(); |
| 55 | + CQLTester.setUpClass(); |
| 56 | + } |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setup() |
| 60 | + { |
| 61 | + requireNetwork(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testClientResponses() throws IOException |
| 66 | + { |
| 67 | + createSchema(); |
| 68 | + try (SimpleClient client = newSimpleClient(ProtocolVersion.CURRENT)) |
| 69 | + { |
| 70 | + // Register to receive schema change notifications, the client uses a simple event handler to |
| 71 | + // record and inspect these. |
| 72 | + client.execute(new RegisterMessage(List.of(Event.Type.SCHEMA_CHANGE))); |
| 73 | + ClientNotificationsTest.EventHandler handler = new ClientNotificationsTest.EventHandler(); |
| 74 | + client.setEventHandler(handler); |
| 75 | + |
| 76 | + // Comments |
| 77 | + makeRequestAndVerify(client, handler, |
| 78 | + format("COMMENT ON KEYSPACE %s IS 'test comment'", KEYSPACE_NAME), |
| 79 | + new Event.SchemaChange(UPDATED, KEYSPACE_NAME)); |
| 80 | + makeRequestAndVerify(client, handler, |
| 81 | + format("COMMENT ON TABLE %s.%s IS 'test comment'", KEYSPACE_NAME, TABLE_NAME), |
| 82 | + new Event.SchemaChange(UPDATED, TABLE, KEYSPACE_NAME, TABLE_NAME)); |
| 83 | + makeRequestAndVerify(client, handler, |
| 84 | + format("COMMENT ON COLUMN %s.%s.%s IS 'test comment'", KEYSPACE_NAME, TABLE_NAME, COLUMN_NAME), |
| 85 | + new Event.SchemaChange(UPDATED, TABLE, KEYSPACE_NAME, TABLE_NAME)); |
| 86 | + makeRequestAndVerify(client, handler, |
| 87 | + format("COMMENT ON TYPE %s.%s IS 'test comment'", KEYSPACE_NAME, TYPE_NAME), |
| 88 | + new Event.SchemaChange(UPDATED, TYPE, KEYSPACE_NAME, TYPE_NAME)); |
| 89 | + makeRequestAndVerify(client, handler, |
| 90 | + format("COMMENT ON FIELD %s.%s.%s IS 'test comment'", KEYSPACE_NAME, TYPE_NAME, FIELD_NAME), |
| 91 | + new Event.SchemaChange(UPDATED, TYPE, KEYSPACE_NAME, TYPE_NAME)); |
| 92 | + |
| 93 | + // Security labels |
| 94 | + makeRequestAndVerify(client, handler, |
| 95 | + format("SECURITY LABEL ON KEYSPACE %s IS 'test label'", KEYSPACE_NAME), |
| 96 | + new Event.SchemaChange(UPDATED, KEYSPACE_NAME)); |
| 97 | + makeRequestAndVerify(client, handler, |
| 98 | + format("SECURITY LABEL ON TABLE %s.%s IS 'test label'", KEYSPACE_NAME, TABLE_NAME), |
| 99 | + new Event.SchemaChange(UPDATED, TABLE, KEYSPACE_NAME, TABLE_NAME)); |
| 100 | + makeRequestAndVerify(client, handler, |
| 101 | + format("SECURITY LABEL ON COLUMN %s.%s.%s IS 'test label'", KEYSPACE_NAME, TABLE_NAME, COLUMN_NAME), |
| 102 | + new Event.SchemaChange(UPDATED, TABLE, KEYSPACE_NAME, TABLE_NAME)); |
| 103 | + makeRequestAndVerify(client, handler, |
| 104 | + format("SECURITY LABEL ON TYPE %s.%s IS 'test label'", KEYSPACE_NAME, TYPE_NAME), |
| 105 | + new Event.SchemaChange(UPDATED, TYPE, KEYSPACE_NAME, TYPE_NAME)); |
| 106 | + makeRequestAndVerify(client, handler, |
| 107 | + format("SECURITY LABEL ON FIELD %s.%s.%s IS 'test label'", KEYSPACE_NAME, TYPE_NAME, FIELD_NAME), |
| 108 | + new Event.SchemaChange(UPDATED, TYPE, KEYSPACE_NAME, TYPE_NAME)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void makeRequestAndVerify(SimpleClient client, |
| 113 | + ClientNotificationsTest.EventHandler handler, |
| 114 | + String cql, |
| 115 | + Event.SchemaChange expected) |
| 116 | + { |
| 117 | + // Assert that the correct response type and content is received |
| 118 | + ResultMessage result = client.execute(cql, ConsistencyLevel.ONE); |
| 119 | + assertEquals(result.kind, ResultMessage.Kind.SCHEMA_CHANGE); |
| 120 | + ResultMessage.SchemaChange message = (ResultMessage.SchemaChange) result; |
| 121 | + assertEquals(message.change, expected); |
| 122 | + // Verify that the expected notification was received and passed to the event handler |
| 123 | + handler.assertNextEvent(expected); |
| 124 | + } |
| 125 | + |
| 126 | + private void createSchema() |
| 127 | + { |
| 128 | + createKeyspace(format("CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}", KEYSPACE_NAME)); |
| 129 | + createTable(format("CREATE TABLE %s.%s (id int PRIMARY KEY, name text)", KEYSPACE_NAME, TABLE_NAME)); |
| 130 | + execute(format("CREATE TYPE %s.%s (%s int)", KEYSPACE_NAME, TYPE_NAME, FIELD_NAME)); |
| 131 | + } |
| 132 | +} |
0 commit comments