|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.mongodb.source.split; |
| 19 | + |
| 20 | +import org.apache.seatunnel.connectors.seatunnel.mongodb.internal.MongodbClientProvider; |
| 21 | + |
| 22 | +import org.apache.commons.lang3.tuple.ImmutablePair; |
| 23 | + |
| 24 | +import org.bson.BsonDocument; |
| 25 | +import org.bson.BsonString; |
| 26 | +import org.bson.Document; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.MockitoAnnotations; |
| 31 | + |
| 32 | +import com.mongodb.MongoNamespace; |
| 33 | +import com.mongodb.client.MongoCollection; |
| 34 | +import com.mongodb.client.MongoDatabase; |
| 35 | + |
| 36 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 | +import static org.mockito.Mockito.when; |
| 38 | + |
| 39 | +public class SamplingSplitStrategyTest { |
| 40 | + |
| 41 | + @Mock private MongodbClientProvider clientProvider; |
| 42 | + |
| 43 | + @Mock private MongoCollection<BsonDocument> collection; |
| 44 | + |
| 45 | + @Mock private MongoDatabase database; |
| 46 | + |
| 47 | + private SamplingSplitStrategy strategy; |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + public void setUp() { |
| 51 | + MockitoAnnotations.openMocks(this); |
| 52 | + strategy = new SamplingSplitStrategy(clientProvider, "splitKey", null, null, 100L, 1000L); |
| 53 | + when(clientProvider.getDefaultCollection()).thenReturn(collection); |
| 54 | + when(clientProvider.getDefaultDatabase()).thenReturn(database); |
| 55 | + |
| 56 | + MongoNamespace namespace = new MongoNamespace("databaseName", "collectionName"); |
| 57 | + when(collection.getNamespace()).thenReturn(namespace); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testGetDocumentNumAndAvgSize() { |
| 62 | + BsonDocument statsCmd = new BsonDocument("collStats", new BsonString("collectionName")); |
| 63 | + Document res = new Document(); |
| 64 | + res.put("count", "1.3360484963E10"); |
| 65 | + res.put("avgObjSize", 200.0); |
| 66 | + |
| 67 | + when(database.runCommand(statsCmd)).thenReturn(res); |
| 68 | + |
| 69 | + ImmutablePair<Long, Long> result = strategy.getDocumentNumAndAvgSize(); |
| 70 | + |
| 71 | + assertEquals(Long.valueOf(13360484963L), result.getLeft()); |
| 72 | + assertEquals(Long.valueOf(200), result.getRight()); |
| 73 | + } |
| 74 | +} |
0 commit comments