|
| 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, |
| 13 | + * software 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.iotdb.db.metadata; |
| 20 | + |
| 21 | +import org.apache.iotdb.db.exception.metadata.IllegalParameterOfPathException; |
| 22 | +import org.apache.iotdb.db.schemaengine.schemaregion.utils.MetaFormatUtils; |
| 23 | +import org.apache.iotdb.rpc.TSStatusCode; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertThrows; |
| 32 | +import static org.junit.Assert.assertTrue; |
| 33 | + |
| 34 | +public class MetaFormatUtilsTest { |
| 35 | + |
| 36 | + private static final String PATH = "root.sg.d.s"; |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testNonNumericSdtProperties() { |
| 40 | + for (String property : new String[] {"compdev", "compmintime", "compmaxtime"}) { |
| 41 | + assertInvalidProperty(property, "invalid"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testNegativeSdtProperties() { |
| 47 | + for (String property : new String[] {"compdev", "compmintime", "compmaxtime"}) { |
| 48 | + assertInvalidProperty(property, "-1"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testSdtTimeOverflow() { |
| 54 | + for (String property : new String[] {"compmintime", "compmaxtime"}) { |
| 55 | + assertInvalidProperty(property, "9223372036854775808"); |
| 56 | + assertInvalidProperty(property, "-9223372036854775809"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testInvalidSdtTimeRange() { |
| 62 | + Map<String, String> props = sdtProps(); |
| 63 | + props.put("compmintime", "20"); |
| 64 | + props.put("compmaxtime", "10"); |
| 65 | + IllegalParameterOfPathException exception = |
| 66 | + assertThrows( |
| 67 | + IllegalParameterOfPathException.class, |
| 68 | + () -> MetaFormatUtils.checkTimeseriesProps(PATH, props)); |
| 69 | + |
| 70 | + assertUserParameterError(exception); |
| 71 | + assertTrue(exception.getMessage().contains("compmintime [20]")); |
| 72 | + assertTrue(exception.getMessage().contains("compmaxtime [10]")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testValidSdtTimeBoundsAndDefaults() throws IllegalParameterOfPathException { |
| 77 | + Map<String, String> props = sdtProps(); |
| 78 | + MetaFormatUtils.checkTimeseriesProps(PATH, props); |
| 79 | + props.put("compmintime", "0"); |
| 80 | + MetaFormatUtils.checkTimeseriesProps(PATH, props); |
| 81 | + props.put("compmaxtime", Long.toString(Long.MAX_VALUE)); |
| 82 | + MetaFormatUtils.checkTimeseriesProps(PATH, props); |
| 83 | + } |
| 84 | + |
| 85 | + private static void assertInvalidProperty(String property, String value) { |
| 86 | + Map<String, String> props = sdtProps(); |
| 87 | + props.put(property, value); |
| 88 | + IllegalParameterOfPathException exception = |
| 89 | + assertThrows( |
| 90 | + IllegalParameterOfPathException.class, |
| 91 | + () -> MetaFormatUtils.checkTimeseriesProps(PATH, props)); |
| 92 | + |
| 93 | + assertUserParameterError(exception); |
| 94 | + assertTrue(exception.getMessage().contains(property)); |
| 95 | + assertTrue(exception.getMessage().contains("[" + value + "]")); |
| 96 | + } |
| 97 | + |
| 98 | + private static void assertUserParameterError(IllegalParameterOfPathException exception) { |
| 99 | + assertEquals(TSStatusCode.ILLEGAL_PARAMETER.getStatusCode(), exception.getErrorCode()); |
| 100 | + assertTrue(exception.isUserException()); |
| 101 | + assertTrue(exception.getMessage().contains(PATH)); |
| 102 | + } |
| 103 | + |
| 104 | + private static Map<String, String> sdtProps() { |
| 105 | + Map<String, String> props = new HashMap<>(); |
| 106 | + props.put("loss", "sdt"); |
| 107 | + props.put("compdev", "0.01"); |
| 108 | + return props; |
| 109 | + } |
| 110 | +} |
0 commit comments