zstan commented on a change in pull request #484: URL: https://github.com/apache/ignite-3/pull/484#discussion_r764788342
########## File path: modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/DdlWithMockedManagersTest.java ########## @@ -0,0 +1,461 @@ +/* + * 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.ignite.internal.processors.query.calcite.exec; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import org.apache.ignite.configuration.schemas.store.DataStorageConfiguration; +import org.apache.ignite.configuration.schemas.table.HashIndexConfigurationSchema; +import org.apache.ignite.configuration.schemas.table.PartialIndexConfigurationSchema; +import org.apache.ignite.configuration.schemas.table.SortedIndexConfigurationSchema; +import org.apache.ignite.configuration.schemas.table.TablesConfiguration; +import org.apache.ignite.internal.affinity.AffinityUtils; +import org.apache.ignite.internal.baseline.BaselineManager; +import org.apache.ignite.internal.configuration.schema.ExtendedTableConfigurationSchema; +import org.apache.ignite.internal.configuration.testframework.ConfigurationExtension; +import org.apache.ignite.internal.configuration.testframework.InjectConfiguration; +import org.apache.ignite.internal.processors.query.calcite.SqlQueryProcessor; +import org.apache.ignite.internal.raft.Loza; +import org.apache.ignite.internal.schema.SchemaDescriptor; +import org.apache.ignite.internal.schema.SchemaUtils; +import org.apache.ignite.internal.table.distributed.TableManager; +import org.apache.ignite.internal.testframework.IgniteAbstractTest; +import org.apache.ignite.internal.tx.TxManager; +import org.apache.ignite.lang.ColumnAlreadyExistsException; +import org.apache.ignite.lang.ColumnNotFoundException; +import org.apache.ignite.lang.IgniteException; +import org.apache.ignite.lang.IgniteInternalException; +import org.apache.ignite.lang.IndexAlreadyExistsException; +import org.apache.ignite.lang.NodeStoppingException; +import org.apache.ignite.lang.TableAlreadyExistsException; +import org.apache.ignite.network.ClusterLocalConfiguration; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.MessagingService; +import org.apache.ignite.network.NetworkAddress; +import org.apache.ignite.network.TopologyService; +import org.apache.ignite.raft.client.Peer; +import org.apache.ignite.raft.client.service.RaftGroupService; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +/** Mock ddl usage. */ +@ExtendWith({MockitoExtension.class, ConfigurationExtension.class}) +@MockitoSettings(strictness = Strictness.LENIENT) +public class DdlWithMockedManagersTest extends IgniteAbstractTest { + /** Table partitions. */ + private static final int PARTITIONS = 32; + + /** Node name. */ + private static final String NODE_NAME = "node1"; + + /** Schema manager. */ + @Mock + private BaselineManager bm; + + /** Topology service. */ + @Mock + private TopologyService ts; + + /** Cluster service. */ + @Mock(lenient = true) + private ClusterService cs; + + /** Raft manager. */ + @Mock + private Loza rm; + + /** TX manager. */ + @Mock(lenient = true) + private TxManager tm; + + /** Tables configuration. */ + @InjectConfiguration( + internalExtensions = ExtendedTableConfigurationSchema.class, + polymorphicExtensions = { + HashIndexConfigurationSchema.class, SortedIndexConfigurationSchema.class, PartialIndexConfigurationSchema.class + } + ) + + private TablesConfiguration tblsCfg; + + /** Data storage configuration. */ + @InjectConfiguration + private DataStorageConfiguration dataStorageCfg; + + TableManager tblManager; + + SqlQueryProcessor queryProc; + + /** Test node. */ + private final ClusterNode node = new ClusterNode( + UUID.randomUUID().toString(), + NODE_NAME, + new NetworkAddress("127.0.0.1", 2245) + ); + + /** Returns current method name. */ + private static String getCurrentMethodName() { + return StackWalker.getInstance() + .walk(s -> s.skip(1).findFirst()) + .get() + .getMethodName(); + } + + /** Stop configuration manager. */ + @AfterEach + void after() { + try { + Objects.requireNonNull(queryProc).stop(); + } catch (Exception e) { + fail(e); + } + + Objects.requireNonNull(tblManager).stop(); + } + + /** Inner initialisation. */ + @BeforeEach + void before() throws NodeStoppingException { + tblManager = mockManagers(); + + queryProc = new SqlQueryProcessor(cs, tblManager); + + queryProc.start(); + } + + /** + * Tests create a table through public API. + */ + @Test + public void testCreateTable() throws Exception { + SqlQueryProcessor finalQueryProc = queryProc; + + String curMethodName = getCurrentMethodName(); + + String newTblSql = String.format("CREATE TABLE %s (c1 int PRIMARY KEY, c2 varbinary(255)) " + + "with partitions=1,replicas=1", curMethodName); + + queryProc.query("PUBLIC", newTblSql); + + assertTrue(tblManager.tables().stream().anyMatch(t -> t.name() + .equalsIgnoreCase("PUBLIC." + curMethodName))); + + String finalNewTblSql1 = newTblSql; + + assertThrows(TableAlreadyExistsException.class, () -> finalQueryProc.query("PUBLIC", finalNewTblSql1)); + + assertThrows(IgniteInternalException.class, () -> finalQueryProc.query("PUBLIC", Review comment: ticket created. -- 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: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org