|
| 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.oracle; |
| 18 | + |
| 19 | +import io.cdap.cdap.api.data.format.StructuredRecord; |
| 20 | +import io.cdap.cdap.api.data.schema.Schema; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.MockitoJUnitRunner; |
| 26 | + |
| 27 | +import java.math.BigDecimal; |
| 28 | +import java.sql.ResultSet; |
| 29 | +import java.sql.ResultSetMetaData; |
| 30 | +import java.sql.Types; |
| 31 | + |
| 32 | +import static org.mockito.ArgumentMatchers.eq; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +/** |
| 36 | + * Unit Test class for the OracleSourceDBRecord |
| 37 | + */ |
| 38 | +@RunWith(MockitoJUnitRunner.class) |
| 39 | +public class OracleSourceDBRecordUnitTest { |
| 40 | + |
| 41 | + private static final int DEFAULT_PRECISION = 38; |
| 42 | + |
| 43 | + @Mock |
| 44 | + ResultSet resultSet; |
| 45 | + |
| 46 | + @Mock |
| 47 | + ResultSetMetaData resultSetMetaData; |
| 48 | + |
| 49 | + /** |
| 50 | + * Validate the precision less Numbers handling against following use cases. |
| 51 | + * 1. Ensure that for Number(0,-127) non nullable type a Number(38,0) is returned by default. |
| 52 | + * 2. Ensure that for Number(0,-127) non nullable type a Number(38,4) is returned, |
| 53 | + * if schema defined this as Number(38,4). |
| 54 | + * 3. Ensure that for Number(0,-127) nullable type a Number(38,0) is returned by default. |
| 55 | + * 4. Ensure that for Number(0,-127) nullable type a Number(38,4) is returned, |
| 56 | + * if schema defined this as Number(38,4). |
| 57 | + * @throws Exception |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void validatePrecisionLessDecimalParsing() throws Exception { |
| 61 | + Schema.Field field1 = Schema.Field.of("ID1", Schema.decimalOf(DEFAULT_PRECISION)); |
| 62 | + Schema.Field field2 = Schema.Field.of("ID2", Schema.decimalOf(DEFAULT_PRECISION, 4)); |
| 63 | + Schema.Field field3 = Schema.Field.of("ID3", Schema.nullableOf(Schema.decimalOf(DEFAULT_PRECISION))); |
| 64 | + Schema.Field field4 = Schema.Field.of("ID4", Schema.nullableOf(Schema.decimalOf(DEFAULT_PRECISION, 4))); |
| 65 | + |
| 66 | + Schema schema = Schema.recordOf( |
| 67 | + "dbRecord", |
| 68 | + field1, |
| 69 | + field2, |
| 70 | + field3, |
| 71 | + field4 |
| 72 | + ); |
| 73 | + |
| 74 | + when(resultSet.getMetaData()).thenReturn(resultSetMetaData); |
| 75 | + when(resultSet.getBigDecimal(eq(1), eq(0))).thenReturn(new BigDecimal("123")); |
| 76 | + when(resultSet.getBigDecimal(eq(2), eq(4))).thenReturn(new BigDecimal("123.4568")); |
| 77 | + when(resultSet.getBigDecimal(eq(3), eq(0))).thenReturn(new BigDecimal("123")); |
| 78 | + when(resultSet.getBigDecimal(eq(4), eq(4))).thenReturn(new BigDecimal("123.4568")); |
| 79 | + |
| 80 | + StructuredRecord.Builder builder = StructuredRecord.builder(schema); |
| 81 | + OracleSourceDBRecord dbRecord = new OracleSourceDBRecord(null, null); |
| 82 | + dbRecord.handleField(resultSet, builder, field1, 1, Types.NUMERIC, 0, -127); |
| 83 | + dbRecord.handleField(resultSet, builder, field2, 2, Types.NUMERIC, 0, -127); |
| 84 | + dbRecord.handleField(resultSet, builder, field3, 3, Types.NUMERIC, 0, -127); |
| 85 | + dbRecord.handleField(resultSet, builder, field4, 4, Types.NUMERIC, 0, -127); |
| 86 | + |
| 87 | + StructuredRecord record = builder.build(); |
| 88 | + Assert.assertEquals(record.getDecimal("ID1").toPlainString(), "123"); |
| 89 | + Assert.assertEquals(record.getDecimal("ID2").toPlainString(), "123.4568"); |
| 90 | + Assert.assertEquals(record.getDecimal("ID3").toPlainString(), "123"); |
| 91 | + Assert.assertEquals(record.getDecimal("ID4").toPlainString(), "123.4568"); |
| 92 | + } |
| 93 | +} |
0 commit comments