|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.nereids.trees.plans.commands; |
| 19 | + |
| 20 | +import org.apache.doris.catalog.Env; |
| 21 | +import org.apache.doris.common.AnalysisException; |
| 22 | +import org.apache.doris.mysql.privilege.PrivPredicate; |
| 23 | +import org.apache.doris.nereids.parser.NereidsParser; |
| 24 | +import org.apache.doris.qe.ConnectContext; |
| 25 | +import org.apache.doris.qe.ResultSetMetaData; |
| 26 | +import org.apache.doris.qe.help.HelpModule; |
| 27 | +import org.apache.doris.qe.help.HelpTopic; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Assertions; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.mockito.MockedStatic; |
| 32 | +import org.mockito.Mockito; |
| 33 | + |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.List; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
| 39 | +class SchemaOnlyCommandMetadataTest { |
| 40 | + private static List<String> names(ResultSetMetaData metadata) { |
| 41 | + return metadata.getColumns().stream().map(column -> column.getName()).collect(Collectors.toList()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void helpTopicDoesNotReadDescriptionOrExample() throws Exception { |
| 46 | + HelpModule module = Mockito.mock(HelpModule.class); |
| 47 | + HelpTopic topic = Mockito.mock(HelpTopic.class); |
| 48 | + Mockito.when(module.getTopic("topic")).thenReturn(topic); |
| 49 | + try (MockedStatic<HelpModule> ignored = Mockito.mockStatic(HelpModule.class)) { |
| 50 | + ignored.when(HelpModule::getInstance).thenReturn(module); |
| 51 | + HelpCommand command = new HelpCommand("topic"); |
| 52 | + Assertions.assertEquals(Arrays.asList("name", "description", "example"), names(command.getMetaData(null))); |
| 53 | + Mockito.verifyNoInteractions(topic); |
| 54 | + Assertions.assertEquals(names(command.doRun(null, null).getMetaData()), names(command.getMetaData(null))); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void helpKeywordWithOneTopicUsesTopicHeader() throws Exception { |
| 60 | + HelpModule module = Mockito.mock(HelpModule.class); |
| 61 | + Mockito.when(module.listTopicByKeyword("keyword")).thenReturn(Collections.singletonList("topic")); |
| 62 | + Mockito.when(module.getTopic("topic")).thenReturn(Mockito.mock(HelpTopic.class)); |
| 63 | + try (MockedStatic<HelpModule> ignored = Mockito.mockStatic(HelpModule.class)) { |
| 64 | + ignored.when(HelpModule::getInstance).thenReturn(module); |
| 65 | + HelpCommand command = new HelpCommand("keyword"); |
| 66 | + Assertions.assertEquals(Arrays.asList("name", "description", "example"), names(command.getMetaData(null))); |
| 67 | + Assertions.assertEquals(names(command.doRun(null, null).getMetaData()), names(command.getMetaData(null))); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void helpKeywordAndCategoryHeadersMatchExecution() throws Exception { |
| 73 | + HelpModule module = Mockito.mock(HelpModule.class); |
| 74 | + Mockito.when(module.listTopicByKeyword("keyword")).thenReturn(Arrays.asList("first", "second")); |
| 75 | + Mockito.when(module.listCategoryByName("categories")).thenReturn(Arrays.asList("first", "second")); |
| 76 | + Mockito.when(module.listCategoryByName("category")).thenReturn(Collections.singletonList("first")); |
| 77 | + try (MockedStatic<HelpModule> ignored = Mockito.mockStatic(HelpModule.class)) { |
| 78 | + ignored.when(HelpModule::getInstance).thenReturn(module); |
| 79 | + for (String mark : Arrays.asList("keyword", "categories", "category", "missing")) { |
| 80 | + HelpCommand command = new HelpCommand(mark); |
| 81 | + List<String> expected = mark.equals("categories") |
| 82 | + ? Arrays.asList("source_category_name", "name", "is_it_category") |
| 83 | + : Arrays.asList("name", "is_it_category"); |
| 84 | + Assertions.assertEquals(expected, names(command.getMetaData(null))); |
| 85 | + Assertions.assertEquals(names(command.doRun(null, null).getMetaData()), names(command.getMetaData(null))); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void invalidHelpAndSnapshotFiltersAreRejected() throws Exception { |
| 92 | + Assertions.assertThrows(AnalysisException.class, () -> new HelpCommand("").getMetaData(null)); |
| 93 | + Env env = Mockito.mock(Env.class, Mockito.RETURNS_DEEP_STUBS); |
| 94 | + Mockito.when(env.getAccessManager().checkGlobalPriv(Mockito.nullable(ConnectContext.class), |
| 95 | + Mockito.eq(PrivPredicate.ADMIN))).thenReturn(true); |
| 96 | + try (MockedStatic<Env> ignored = Mockito.mockStatic(Env.class)) { |
| 97 | + ignored.when(Env::getCurrentEnv).thenReturn(env); |
| 98 | + ShowSnapshotCommand command = (ShowSnapshotCommand) new NereidsParser() |
| 99 | + .parseSingle("SHOW SNAPSHOT ON example_repo WHERE TIMESTAMP = '2026-01-01-00-00-00'"); |
| 100 | + Assertions.assertThrows(AnalysisException.class, () -> command.getMetaData(null)); |
| 101 | + Mockito.verify(env, Mockito.never()).getBackupHandler(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void filteredSnapshotsResolveWithoutRepositoryAccess() throws Exception { |
| 107 | + Env env = Mockito.mock(Env.class, Mockito.RETURNS_DEEP_STUBS); |
| 108 | + Mockito.when(env.getAccessManager().checkGlobalPriv(Mockito.nullable(ConnectContext.class), |
| 109 | + Mockito.eq(PrivPredicate.ADMIN))).thenReturn(true); |
| 110 | + try (MockedStatic<Env> ignored = Mockito.mockStatic(Env.class)) { |
| 111 | + ignored.when(Env::getCurrentEnv).thenReturn(env); |
| 112 | + for (String filter : Arrays.asList("", " WHERE SNAPSHOT = 'sample'", |
| 113 | + " WHERE SNAPSHOT = 'sample' AND TIMESTAMP = '2026-01-01-00-00-00'")) { |
| 114 | + ShowSnapshotCommand command = (ShowSnapshotCommand) new NereidsParser() |
| 115 | + .parseSingle("SHOW SNAPSHOT ON example_repo" + filter); |
| 116 | + Assertions.assertEquals(filter.contains("TIMESTAMP") |
| 117 | + ? ShowSnapshotCommand.SNAPSHOT_DETAIL : ShowSnapshotCommand.SNAPSHOT_ALL, |
| 118 | + names(command.getMetaData(null))); |
| 119 | + } |
| 120 | + Mockito.verify(env, Mockito.never()).getBackupHandler(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments