|
| 1 | +/* |
| 2 | + * Copyright © 2019 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cdap.plugin.db.source; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import org.apache.hadoop.mapreduce.InputSplit; |
| 21 | +import org.apache.hadoop.mapreduce.JobContext; |
| 22 | +import org.apache.hadoop.mapreduce.lib.db.DBConfiguration; |
| 23 | +import org.apache.hadoop.mapreduce.lib.db.DataDrivenDBInputFormat; |
| 24 | +import org.junit.Assert; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.mockito.Mock; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.mockito.runners.MockitoJUnitRunner; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +@RunWith(MockitoJUnitRunner.class) |
| 36 | +public class DataDrivenETLDBInputFormatTest { |
| 37 | + |
| 38 | + @Mock |
| 39 | + private JobContext mockJobContext; |
| 40 | + @Mock |
| 41 | + private DBConfiguration mockDbConfiguration; |
| 42 | + |
| 43 | + private DataDrivenETLDBInputFormat inputFormat; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() { |
| 47 | + inputFormat = Mockito.spy(new DataDrivenETLDBInputFormat()); |
| 48 | + Mockito.doReturn(mockDbConfiguration).when(inputFormat).getDBConf(); |
| 49 | + Mockito.doReturn("id").when(mockDbConfiguration).getInputOrderBy(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testGetSplitsAddsNullSplit() throws IOException { |
| 54 | + DataDrivenDBInputFormat.DataDrivenDBInputSplit existingSplit = |
| 55 | + new DataDrivenDBInputFormat.DataDrivenDBInputSplit("id >= 0", "id < 100"); |
| 56 | + List<InputSplit> initialSplits = ImmutableList.of(existingSplit); |
| 57 | + Mockito.doReturn(initialSplits).when(inputFormat).getBaseSplits(mockJobContext); |
| 58 | + |
| 59 | + List<InputSplit> finalSplits = inputFormat.getSplits(mockJobContext); |
| 60 | + |
| 61 | + Assert.assertEquals("A new split for NULLs should be added", 2, finalSplits.size()); |
| 62 | + |
| 63 | + DataDrivenDBInputFormat.DataDrivenDBInputSplit nullSplit = |
| 64 | + (DataDrivenDBInputFormat.DataDrivenDBInputSplit) finalSplits.get(1); |
| 65 | + Assert.assertEquals("id IS NULL", nullSplit.getLowerClause()); |
| 66 | + Assert.assertEquals("id IS NULL", nullSplit.getUpperClause()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testGetSplitsDoesNotAddNullSplitIfPresent() throws IOException { |
| 71 | + DataDrivenDBInputFormat.DataDrivenDBInputSplit existingSplit = |
| 72 | + new DataDrivenDBInputFormat.DataDrivenDBInputSplit("id >= 0", "id < 100"); |
| 73 | + DataDrivenDBInputFormat.DataDrivenDBInputSplit nullSplit = |
| 74 | + new DataDrivenDBInputFormat.DataDrivenDBInputSplit("id IS NULL", "id IS NULL"); |
| 75 | + List<InputSplit> initialSplits = ImmutableList.of(existingSplit, nullSplit); |
| 76 | + |
| 77 | + Mockito.doReturn(initialSplits).when(inputFormat).getBaseSplits(mockJobContext); |
| 78 | + |
| 79 | + List<InputSplit> finalSplits = inputFormat.getSplits(mockJobContext); |
| 80 | + |
| 81 | + Assert.assertEquals("Should not add a duplicate NULL split", 2, finalSplits.size()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testGetSplitsDoesNotAddNullSplitIfSelectAllPresent() throws IOException { |
| 86 | + DataDrivenDBInputFormat.DataDrivenDBInputSplit existingSplit = |
| 87 | + new DataDrivenDBInputFormat.DataDrivenDBInputSplit("1=1", "1=1"); |
| 88 | + List<InputSplit> initialSplits = ImmutableList.of(existingSplit); |
| 89 | + |
| 90 | + Mockito.doReturn(initialSplits).when(inputFormat).getBaseSplits(mockJobContext); |
| 91 | + |
| 92 | + List<InputSplit> finalSplits = inputFormat.getSplits(mockJobContext); |
| 93 | + |
| 94 | + Assert.assertEquals("Should not add a NULL split", 1, finalSplits.size()); |
| 95 | + } |
| 96 | +} |
0 commit comments