|
| 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 | +package org.apache.beam.sdk.io.gcp.spanner.changestreams.dao; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.times; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | +import static org.mockito.Mockito.when; |
| 27 | + |
| 28 | +import com.google.cloud.spanner.DatabaseClient; |
| 29 | +import com.google.cloud.spanner.Dialect; |
| 30 | +import com.google.cloud.spanner.Options.RpcPriority; |
| 31 | +import com.google.cloud.spanner.ReadOnlyTransaction; |
| 32 | +import com.google.cloud.spanner.ResultSet; |
| 33 | +import com.google.cloud.spanner.Statement; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.mockito.ArgumentCaptor; |
| 37 | + |
| 38 | +public class ChangeStreamDaoTest { |
| 39 | + private DatabaseClient databaseClient; |
| 40 | + private RpcPriority rpcPriority; |
| 41 | + private ChangeStreamDao changeStreamDao; |
| 42 | + private static final String CHANGE_STREAM_NAME = "testCS"; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setUp() { |
| 46 | + databaseClient = mock(DatabaseClient.class); |
| 47 | + rpcPriority = mock(RpcPriority.class); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testPartitionOptionMutable() { |
| 52 | + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); |
| 53 | + when(databaseClient.readOnlyTransaction()).thenReturn(readOnlyTransaction); |
| 54 | + |
| 55 | + ResultSet resultSet = mock(ResultSet.class); |
| 56 | + when(readOnlyTransaction.executeQuery(any())).thenReturn(resultSet); |
| 57 | + when(resultSet.next()).thenReturn(true).thenReturn(false); |
| 58 | + when(resultSet.getString(0)).thenReturn("partition_mode"); |
| 59 | + when(resultSet.getString(1)).thenReturn("MUTABLE_KEY_RANGE"); |
| 60 | + |
| 61 | + ChangeStreamDao changeStreamDao = |
| 62 | + new ChangeStreamDao( |
| 63 | + CHANGE_STREAM_NAME, |
| 64 | + databaseClient, |
| 65 | + rpcPriority, |
| 66 | + "testjob", |
| 67 | + Dialect.GOOGLE_STANDARD_SQL); |
| 68 | + |
| 69 | + assertEquals(true, changeStreamDao.isMutableKeyRangeChangeStream()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testPartitionOptionImmutable() { |
| 74 | + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); |
| 75 | + when(databaseClient.readOnlyTransaction()).thenReturn(readOnlyTransaction); |
| 76 | + |
| 77 | + ResultSet resultSet = mock(ResultSet.class); |
| 78 | + when(readOnlyTransaction.executeQuery(any())).thenReturn(resultSet); |
| 79 | + when(resultSet.next()).thenReturn(true).thenReturn(false); |
| 80 | + when(resultSet.getString(0)).thenReturn("partition_mode"); |
| 81 | + when(resultSet.getString(1)).thenReturn("IMMUTABLE_KEY_RANGE"); |
| 82 | + |
| 83 | + ChangeStreamDao changeStreamDao = |
| 84 | + new ChangeStreamDao( |
| 85 | + CHANGE_STREAM_NAME, |
| 86 | + databaseClient, |
| 87 | + rpcPriority, |
| 88 | + "testjob", |
| 89 | + Dialect.GOOGLE_STANDARD_SQL); |
| 90 | + |
| 91 | + assertEquals(false, changeStreamDao.isMutableKeyRangeChangeStream()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testPartitionOptionEmpty() { |
| 96 | + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); |
| 97 | + when(databaseClient.readOnlyTransaction()).thenReturn(readOnlyTransaction); |
| 98 | + |
| 99 | + ResultSet resultSet = mock(ResultSet.class); |
| 100 | + when(readOnlyTransaction.executeQuery(any())).thenReturn(resultSet); |
| 101 | + when(resultSet.next()).thenReturn(true).thenReturn(false); |
| 102 | + when(resultSet.getString(0)).thenReturn("partition_mode"); |
| 103 | + when(resultSet.getString(1)).thenReturn(""); |
| 104 | + |
| 105 | + ChangeStreamDao changeStreamDao = |
| 106 | + new ChangeStreamDao( |
| 107 | + CHANGE_STREAM_NAME, |
| 108 | + databaseClient, |
| 109 | + rpcPriority, |
| 110 | + "testjob", |
| 111 | + Dialect.GOOGLE_STANDARD_SQL); |
| 112 | + |
| 113 | + assertEquals(false, changeStreamDao.isMutableKeyRangeChangeStream()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testPartitionModeCaching() { |
| 118 | + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); |
| 119 | + when(databaseClient.readOnlyTransaction()).thenReturn(readOnlyTransaction); |
| 120 | + |
| 121 | + ResultSet resultSet = mock(ResultSet.class); |
| 122 | + when(readOnlyTransaction.executeQuery(any())).thenReturn(resultSet); |
| 123 | + when(resultSet.next()).thenReturn(true).thenReturn(false); |
| 124 | + when(resultSet.getString(0)).thenReturn("partition_mode"); |
| 125 | + when(resultSet.getString(1)).thenReturn("MUTABLE_KEY_RANGE"); |
| 126 | + |
| 127 | + ChangeStreamDao changeStreamDao = |
| 128 | + new ChangeStreamDao( |
| 129 | + CHANGE_STREAM_NAME, |
| 130 | + databaseClient, |
| 131 | + rpcPriority, |
| 132 | + "testjob", |
| 133 | + Dialect.GOOGLE_STANDARD_SQL); |
| 134 | + |
| 135 | + // first call triggers the read |
| 136 | + assertEquals(true, changeStreamDao.isMutableKeyRangeChangeStream()); |
| 137 | + // second call should use cached partitionMode and NOT call |
| 138 | + // databaseClient.readOnlyTransaction() again |
| 139 | + assertEquals(true, changeStreamDao.isMutableKeyRangeChangeStream()); |
| 140 | + |
| 141 | + verify(databaseClient, times(1)).readOnlyTransaction(); |
| 142 | + } |
| 143 | + |
| 144 | + // New tests for PostgreSQL branch to verify the chosen TVF in the generated |
| 145 | + // SQL. |
| 146 | + @Test |
| 147 | + public void testChangeStreamQueryPostgresMutable() { |
| 148 | + // Arrange: metadata read returning MUTABLE_KEY_RANGE |
| 149 | + ReadOnlyTransaction metaTx = mock(ReadOnlyTransaction.class); |
| 150 | + when(databaseClient.readOnlyTransaction()).thenReturn(metaTx); |
| 151 | + |
| 152 | + ResultSet metaResult = mock(ResultSet.class); |
| 153 | + when(metaTx.executeQuery(any())).thenReturn(metaResult); |
| 154 | + when(metaResult.next()).thenReturn(true).thenReturn(false); |
| 155 | + when(metaResult.getString(0)).thenReturn("partition_mode"); |
| 156 | + when(metaResult.getString(1)).thenReturn("MUTABLE_KEY_RANGE"); |
| 157 | + |
| 158 | + // Arrange: single-use transaction for the actual change stream query |
| 159 | + ReadOnlyTransaction singleUseTx = mock(ReadOnlyTransaction.class); |
| 160 | + when(databaseClient.singleUse()).thenReturn(singleUseTx); |
| 161 | + |
| 162 | + ResultSet queryResult = mock(ResultSet.class); |
| 163 | + // We don't need queryResult to return rows; just to satisfy the call. |
| 164 | + when(singleUseTx.executeQuery(any(), any(), any())).thenReturn(queryResult); |
| 165 | + |
| 166 | + ChangeStreamDao changeStreamDao = |
| 167 | + new ChangeStreamDao( |
| 168 | + CHANGE_STREAM_NAME, databaseClient, rpcPriority, "testjob", Dialect.POSTGRESQL); |
| 169 | + |
| 170 | + // Act: call the method that constructs and executes the statement |
| 171 | + changeStreamDao.changeStreamQuery(null, null, null, 0L); |
| 172 | + |
| 173 | + // Assert: capture the Statement passed to singleUse().executeQuery and verify |
| 174 | + // SQL |
| 175 | + ArgumentCaptor<Statement> captor = ArgumentCaptor.forClass(Statement.class); |
| 176 | + verify(singleUseTx).executeQuery(captor.capture(), any(), any()); |
| 177 | + Statement captured = captor.getValue(); |
| 178 | + String sql = captured.getSql(); // adjust if different accessor is used |
| 179 | + assertTrue( |
| 180 | + "Expected SQL to contain read_proto_bytes_", |
| 181 | + sql.contains("read_proto_bytes_" + CHANGE_STREAM_NAME)); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testChangeStreamQueryPostgresImmutable() { |
| 186 | + // Arrange: metadata read returning IMMUTABLE_KEY_RANGE |
| 187 | + ReadOnlyTransaction metaTx = mock(ReadOnlyTransaction.class); |
| 188 | + when(databaseClient.readOnlyTransaction()).thenReturn(metaTx); |
| 189 | + |
| 190 | + ResultSet metaResult = mock(ResultSet.class); |
| 191 | + when(metaTx.executeQuery(any())).thenReturn(metaResult); |
| 192 | + when(metaResult.next()).thenReturn(true).thenReturn(false); |
| 193 | + when(metaResult.getString(0)).thenReturn("partition_mode"); |
| 194 | + when(metaResult.getString(1)).thenReturn("IMMUTABLE_KEY_RANGE"); |
| 195 | + |
| 196 | + // Arrange: single-use transaction for the actual change stream query |
| 197 | + ReadOnlyTransaction singleUseTx = mock(ReadOnlyTransaction.class); |
| 198 | + when(databaseClient.singleUse()).thenReturn(singleUseTx); |
| 199 | + |
| 200 | + ResultSet queryResult = mock(ResultSet.class); |
| 201 | + when(singleUseTx.executeQuery(any(), any(), any())).thenReturn(queryResult); |
| 202 | + |
| 203 | + ChangeStreamDao changeStreamDao = |
| 204 | + new ChangeStreamDao( |
| 205 | + CHANGE_STREAM_NAME, databaseClient, rpcPriority, "testjob", Dialect.POSTGRESQL); |
| 206 | + |
| 207 | + // Act |
| 208 | + changeStreamDao.changeStreamQuery(null, null, null, 0L); |
| 209 | + |
| 210 | + // Assert |
| 211 | + ArgumentCaptor<Statement> captor = ArgumentCaptor.forClass(Statement.class); |
| 212 | + verify(singleUseTx).executeQuery(captor.capture(), any(), any()); |
| 213 | + Statement captured = captor.getValue(); |
| 214 | + String sql = captured.getSql(); |
| 215 | + assertTrue( |
| 216 | + "Expected SQL to contain read_json_", sql.contains("read_json_" + CHANGE_STREAM_NAME)); |
| 217 | + } |
| 218 | +} |
0 commit comments