|
| 1 | +/* |
| 2 | + * Copyright 2018 ABSA Group Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package za.co.absa.cobrix.spark.cobol.source.integration |
| 18 | + |
| 19 | +import org.scalatest.wordspec.AnyWordSpec |
| 20 | +import za.co.absa.cobrix.spark.cobol.source.base.SparkTestBase |
| 21 | +import za.co.absa.cobrix.spark.cobol.source.fixtures.{BinaryFileFixture, TextComparisonFixture} |
| 22 | + |
| 23 | +class Test38StrictBinaryTypeSpec extends AnyWordSpec with SparkTestBase with BinaryFileFixture with TextComparisonFixture { |
| 24 | + private val copybook = |
| 25 | + """ 01 R. |
| 26 | + 05 NUM1 PIC 9(9) COMP. |
| 27 | + 05 NUM2 PIC 9(9) COMP-9. |
| 28 | + 05 NUM3 PIC S9(9) COMP-5. |
| 29 | + 05 NUM4 PIC S9(9) COMP-9. |
| 30 | + """ |
| 31 | + |
| 32 | + val dataSimple: Array[Byte] = Array( |
| 33 | + 0xFF, 0xFF, 0xFF, 0xFE, // |
| 34 | + 0xFF, 0xFF, 0xFF, 0xFE, // |
| 35 | + 0xFF, 0xFF, 0xFF, 0xFE, // -2 |
| 36 | + 0xFF, 0xFF, 0xFF, 0xFE, // -16777217 |
| 37 | + 0xF0, 0x00, 0x01, 0xF0, // |
| 38 | + 0xF0, 0x00, 0x01, 0xF0, // |
| 39 | + 0x84, 0xF0, 0x21, 0x67, // -2064637593 |
| 40 | + 0x67, 0x21, 0xF0, 0x84 // -2064637593 |
| 41 | + ).map(_.toByte) |
| 42 | + |
| 43 | + "binary fields handling" should { |
| 44 | + "be decoded with the default behavior" in { |
| 45 | + withTempBinFile("strict_bin_fields", ".tmp", dataSimple) { tempFile => |
| 46 | + val expectedSchema = |
| 47 | + """root |
| 48 | + | |-- NUM1: integer (nullable = true) |
| 49 | + | |-- NUM2: integer (nullable = true) |
| 50 | + | |-- NUM3: integer (nullable = true) |
| 51 | + | |-- NUM4: integer (nullable = true) |
| 52 | + |""".stripMargin |
| 53 | + |
| 54 | + val expectedData = """{"NUM3":-2,"NUM4":-16777217},{"NUM3":-2064637593,"NUM4":-2064637593}""" |
| 55 | + |
| 56 | + val df = spark.read |
| 57 | + .format("cobol") |
| 58 | + .option("copybook_contents", copybook) |
| 59 | + .option("record_format", "F") |
| 60 | + .option("pedantic", "true") |
| 61 | + .load(tempFile) |
| 62 | + |
| 63 | + val actualSchema = df.schema.treeString |
| 64 | + val actualData = df.toJSON.collect().mkString(",") |
| 65 | + |
| 66 | + compareText(actualSchema, expectedSchema) |
| 67 | + assert(actualData == expectedData) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments