|
| 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, software |
| 13 | + * 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 com.dtstack.flink.sql.source.kafka; |
| 20 | + |
| 21 | +import org.apache.flink.api.common.serialization.SimpleStringSchema; |
| 22 | +import org.apache.flink.streaming.connectors.kafka.internals.KafkaTopicPartition; |
| 23 | +import org.apache.flink.types.Row; |
| 24 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +/** |
| 37 | + * @auther tiezhu |
| 38 | + * @date 2021/4/27 4:33 下午 |
| 39 | + */ |
| 40 | +public class DtKafkaDeserializationSchemaWrapperTest { |
| 41 | + public Map<KafkaTopicPartition, Long> specificEndOffsets; |
| 42 | + |
| 43 | + private DtKafkaDeserializationSchemaWrapper<String> kafkaDeserializationSchemaWrapper; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() { |
| 47 | + String topic = "test"; |
| 48 | + Map<String, Object> valueMap = new HashMap<>(); |
| 49 | + |
| 50 | + valueMap.put("1", 10); |
| 51 | + |
| 52 | + specificEndOffsets = valueMap |
| 53 | + .entrySet() |
| 54 | + .stream() |
| 55 | + .collect(Collectors.toMap( |
| 56 | + (Map.Entry<String, Object> entry) |
| 57 | + -> new KafkaTopicPartition(topic, Integer.parseInt(entry.getKey())), |
| 58 | + (Map.Entry<String, Object> entry) |
| 59 | + -> Long.valueOf(entry.getValue().toString())) |
| 60 | + ); |
| 61 | + |
| 62 | + kafkaDeserializationSchemaWrapper = new DtKafkaDeserializationSchemaWrapper<>(new SimpleStringSchema(), specificEndOffsets); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testIsEndOfStream() { |
| 67 | + String topic = "test"; |
| 68 | + |
| 69 | + Assert.assertFalse(kafkaDeserializationSchemaWrapper.isEndOfStream(topic)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testDeserialize() throws Exception { |
| 74 | + String str = "test"; |
| 75 | + ConsumerRecord<byte[], byte[]> record = new ConsumerRecord<>( |
| 76 | + "test", |
| 77 | + 1, |
| 78 | + 11, |
| 79 | + str.getBytes(StandardCharsets.UTF_8), |
| 80 | + str.getBytes(StandardCharsets.UTF_8) |
| 81 | + ); |
| 82 | + |
| 83 | + String deserialize = kafkaDeserializationSchemaWrapper.deserialize(record); |
| 84 | + |
| 85 | + Assert.assertNull(deserialize); |
| 86 | + } |
| 87 | +} |
0 commit comments