|
| 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.datasource.maxcompute; |
| 19 | + |
| 20 | +import org.apache.doris.common.DdlException; |
| 21 | +import org.apache.doris.common.maxcompute.MCProperties; |
| 22 | + |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +public class MaxComputeExternalCatalogTest { |
| 30 | + @Test |
| 31 | + public void testSplitByteSizeErrorMessage() { |
| 32 | + Map<String, String> props = new HashMap<>(); |
| 33 | + addRequiredProperties(props); |
| 34 | + props.put(MCProperties.SPLIT_STRATEGY, MCProperties.SPLIT_BY_BYTE_SIZE_STRATEGY); |
| 35 | + props.put(MCProperties.SPLIT_BYTE_SIZE, "1048576"); |
| 36 | + |
| 37 | + MaxComputeExternalCatalog catalog = new MaxComputeExternalCatalog(1L, "mc_catalog", null, props, ""); |
| 38 | + |
| 39 | + DdlException exception = Assert.assertThrows(DdlException.class, catalog::checkProperties); |
| 40 | + Assert.assertTrue(exception.getMessage().contains( |
| 41 | + MCProperties.SPLIT_BYTE_SIZE + " must be greater than or equal to 10485760")); |
| 42 | + Assert.assertFalse(exception.getMessage().contains(MCProperties.SPLIT_ROW_COUNT)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testInitSkipsValidationByDefault() { |
| 47 | + Map<String, String> props = createRequiredProperties(true); |
| 48 | + TestMaxComputeExternalCatalog catalog = new TestMaxComputeExternalCatalog(props); |
| 49 | + |
| 50 | + catalog.initForTest(); |
| 51 | + |
| 52 | + Assert.assertNull(catalog.checkedProjectName); |
| 53 | + Assert.assertNull(catalog.checkedNamespaceSchemaProjectName); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testInitValidatesProjectWhenValidationEnabled() { |
| 58 | + Map<String, String> props = createRequiredProperties(false); |
| 59 | + props.put(MCProperties.VALIDATE_CONNECTION, "true"); |
| 60 | + TestMaxComputeExternalCatalog catalog = new TestMaxComputeExternalCatalog(props); |
| 61 | + |
| 62 | + catalog.initForTest(); |
| 63 | + |
| 64 | + Assert.assertEquals("mc_project", catalog.checkedProjectName); |
| 65 | + Assert.assertNull(catalog.checkedNamespaceSchemaProjectName); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testInitValidatesSchemaWhenNamespaceSchemaEnabled() { |
| 70 | + Map<String, String> props = createRequiredProperties(true); |
| 71 | + props.put(MCProperties.VALIDATE_CONNECTION, "true"); |
| 72 | + TestMaxComputeExternalCatalog catalog = new TestMaxComputeExternalCatalog(props); |
| 73 | + |
| 74 | + catalog.initForTest(); |
| 75 | + |
| 76 | + Assert.assertNull(catalog.checkedProjectName); |
| 77 | + Assert.assertEquals("mc_project", catalog.checkedNamespaceSchemaProjectName); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testInitReportsInaccessibleProject() { |
| 82 | + Map<String, String> props = createRequiredProperties(false); |
| 83 | + props.put(MCProperties.VALIDATE_CONNECTION, "true"); |
| 84 | + TestMaxComputeExternalCatalog catalog = new TestMaxComputeExternalCatalog(props); |
| 85 | + catalog.projectExists = false; |
| 86 | + |
| 87 | + RuntimeException exception = Assert.assertThrows(RuntimeException.class, catalog::initForTest); |
| 88 | + |
| 89 | + Assert.assertTrue(exception.getMessage().contains("Failed to validate MaxCompute project 'mc_project'")); |
| 90 | + Assert.assertTrue(exception.getMessage().contains("does not exist or is not accessible")); |
| 91 | + Assert.assertNull(catalog.checkedNamespaceSchemaProjectName); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testInitReportsUnsupportedNamespaceSchema() { |
| 96 | + Map<String, String> props = createRequiredProperties(true); |
| 97 | + props.put(MCProperties.VALIDATE_CONNECTION, "true"); |
| 98 | + TestMaxComputeExternalCatalog catalog = new TestMaxComputeExternalCatalog(props); |
| 99 | + catalog.threeTierModel = false; |
| 100 | + |
| 101 | + RuntimeException exception = Assert.assertThrows(RuntimeException.class, catalog::initForTest); |
| 102 | + |
| 103 | + Assert.assertTrue(exception.getMessage().contains("Failed to validate MaxCompute project 'mc_project'")); |
| 104 | + Assert.assertTrue(exception.getMessage().contains("does not support namespace schema")); |
| 105 | + } |
| 106 | + |
| 107 | + private static Map<String, String> createRequiredProperties(boolean enableNamespaceSchema) { |
| 108 | + Map<String, String> props = new HashMap<>(); |
| 109 | + addRequiredProperties(props); |
| 110 | + props.put(MCProperties.ENABLE_NAMESPACE_SCHEMA, Boolean.toString(enableNamespaceSchema)); |
| 111 | + return props; |
| 112 | + } |
| 113 | + |
| 114 | + private static void addRequiredProperties(Map<String, String> props) { |
| 115 | + props.put(MCProperties.PROJECT, "mc_project"); |
| 116 | + props.put(MCProperties.ENDPOINT, "http://service.cn-beijing.maxcompute.aliyun-inc.com/api"); |
| 117 | + props.put(MCProperties.ACCESS_KEY, "access_key"); |
| 118 | + props.put(MCProperties.SECRET_KEY, "secret_key"); |
| 119 | + } |
| 120 | + |
| 121 | + private static class TestMaxComputeExternalCatalog extends MaxComputeExternalCatalog { |
| 122 | + private boolean projectExists = true; |
| 123 | + private boolean threeTierModel = true; |
| 124 | + private String checkedProjectName; |
| 125 | + private String checkedNamespaceSchemaProjectName; |
| 126 | + |
| 127 | + private TestMaxComputeExternalCatalog(Map<String, String> props) { |
| 128 | + super(1L, "mc_catalog", null, props, ""); |
| 129 | + } |
| 130 | + |
| 131 | + private void initForTest() { |
| 132 | + initLocalObjectsImpl(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + protected boolean maxComputeProjectExists(String projectName) { |
| 137 | + checkedProjectName = projectName; |
| 138 | + return projectExists; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected void validateMaxComputeNamespaceSchemaAccess(String projectName) { |
| 143 | + checkedNamespaceSchemaProjectName = projectName; |
| 144 | + if (!threeTierModel) { |
| 145 | + throw new RuntimeException("does not support namespace schema"); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments