|
17 | 17 |
|
18 | 18 | package org.apache.seatunnel.connectors.seatunnel.iotdbv2; |
19 | 19 |
|
| 20 | +import org.apache.seatunnel.api.configuration.ReadonlyConfig; |
| 21 | +import org.apache.seatunnel.api.configuration.util.ConfigValidator; |
| 22 | +import org.apache.seatunnel.api.configuration.util.OptionRule; |
| 23 | +import org.apache.seatunnel.api.configuration.util.OptionValidationException; |
| 24 | +import org.apache.seatunnel.api.options.ConnectorCommonOptions; |
| 25 | +import org.apache.seatunnel.connectors.seatunnel.iotdbv2.config.IoTDBv2CommonOptions; |
| 26 | +import org.apache.seatunnel.connectors.seatunnel.iotdbv2.config.IoTDBv2SinkOptions; |
| 27 | +import org.apache.seatunnel.connectors.seatunnel.iotdbv2.config.IoTDBv2SourceOptions; |
20 | 28 | import org.apache.seatunnel.connectors.seatunnel.iotdbv2.sink.IoTDBv2SinkFactory; |
21 | 29 | import org.apache.seatunnel.connectors.seatunnel.iotdbv2.source.IoTDBv2SourceFactory; |
22 | 30 |
|
23 | 31 | import org.junit.jupiter.api.Assertions; |
24 | 32 | import org.junit.jupiter.api.Test; |
25 | 33 |
|
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
| 38 | + |
26 | 39 | class IoTDBFactoryTest { |
27 | 40 |
|
| 41 | + private final OptionRule sourceRule = new IoTDBv2SourceFactory().optionRule(); |
| 42 | + private final OptionRule sinkRule = new IoTDBv2SinkFactory().optionRule(); |
| 43 | + |
28 | 44 | @Test |
29 | 45 | void optionRule() { |
30 | | - Assertions.assertNotNull((new IoTDBv2SourceFactory()).optionRule()); |
31 | | - Assertions.assertNotNull((new IoTDBv2SinkFactory()).optionRule()); |
| 46 | + Assertions.assertNotNull(sourceRule); |
| 47 | + Assertions.assertNotNull(sinkRule); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void omittedSqlDialectIsAccepted() { |
| 52 | + Assertions.assertDoesNotThrow(() -> validate(validSourceConfig(), sourceRule)); |
| 53 | + Assertions.assertDoesNotThrow(() -> validate(validSinkConfig(), sinkRule)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void supportedSqlDialectsAreAcceptedCaseInsensitively() { |
| 58 | + for (String dialect : Arrays.asList("tree", "table", "TrEe", "TaBlE")) { |
| 59 | + Map<String, Object> sourceConfig = validSourceConfig(); |
| 60 | + sourceConfig.put(IoTDBv2CommonOptions.SQL_DIALECT.key(), dialect); |
| 61 | + Assertions.assertDoesNotThrow(() -> validate(sourceConfig, sourceRule)); |
| 62 | + |
| 63 | + Map<String, Object> sinkConfig = validSinkConfig(); |
| 64 | + sinkConfig.put(IoTDBv2CommonOptions.SQL_DIALECT.key(), dialect); |
| 65 | + Assertions.assertDoesNotThrow(() -> validate(sinkConfig, sinkRule)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void unsupportedSqlDialectIsRejected() { |
| 71 | + Map<String, Object> sourceConfig = validSourceConfig(); |
| 72 | + sourceConfig.put(IoTDBv2CommonOptions.SQL_DIALECT.key(), "unsupported"); |
| 73 | + OptionValidationException sourceException = |
| 74 | + Assertions.assertThrows( |
| 75 | + OptionValidationException.class, () -> validate(sourceConfig, sourceRule)); |
| 76 | + Assertions.assertTrue( |
| 77 | + sourceException.getMessage().contains(IoTDBv2CommonOptions.SQL_DIALECT.key())); |
| 78 | + |
| 79 | + Map<String, Object> sinkConfig = validSinkConfig(); |
| 80 | + sinkConfig.put(IoTDBv2CommonOptions.SQL_DIALECT.key(), "unsupported"); |
| 81 | + OptionValidationException sinkException = |
| 82 | + Assertions.assertThrows( |
| 83 | + OptionValidationException.class, () -> validate(sinkConfig, sinkRule)); |
| 84 | + Assertions.assertTrue( |
| 85 | + sinkException.getMessage().contains(IoTDBv2CommonOptions.SQL_DIALECT.key())); |
| 86 | + } |
| 87 | + |
| 88 | + private void validate(Map<String, Object> config, OptionRule rule) { |
| 89 | + ConfigValidator.of(ReadonlyConfig.fromMap(config)).validate(rule); |
| 90 | + } |
| 91 | + |
| 92 | + private Map<String, Object> validSourceConfig() { |
| 93 | + Map<String, Object> config = commonConfig(); |
| 94 | + config.put(IoTDBv2SourceOptions.SQL.key(), "select * from root.test"); |
| 95 | + config.put(ConnectorCommonOptions.SCHEMA.key(), Collections.emptyMap()); |
| 96 | + return config; |
| 97 | + } |
| 98 | + |
| 99 | + private Map<String, Object> validSinkConfig() { |
| 100 | + Map<String, Object> config = commonConfig(); |
| 101 | + config.put(IoTDBv2SinkOptions.STORAGE_GROUP.key(), "root.test"); |
| 102 | + config.put(IoTDBv2SinkOptions.KEY_DEVICE.key(), "device"); |
| 103 | + return config; |
| 104 | + } |
| 105 | + |
| 106 | + private Map<String, Object> commonConfig() { |
| 107 | + Map<String, Object> config = new HashMap<>(); |
| 108 | + config.put( |
| 109 | + IoTDBv2CommonOptions.NODE_URLS.key(), Collections.singletonList("127.0.0.1:6667")); |
| 110 | + config.put(IoTDBv2CommonOptions.USERNAME.key(), "root"); |
| 111 | + config.put(IoTDBv2CommonOptions.PASSWORD.key(), "root"); |
| 112 | + return config; |
32 | 113 | } |
33 | 114 | } |
0 commit comments