|
| 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.apache.spark.sql.DataFrame |
| 20 | +import org.scalatest.wordspec.AnyWordSpec |
| 21 | +import za.co.absa.cobrix.spark.cobol.mocks.CustomRecordExtractorMock |
| 22 | +import za.co.absa.cobrix.spark.cobol.source.base.SparkTestBase |
| 23 | +import za.co.absa.cobrix.spark.cobol.source.fixtures.BinaryFileFixture |
| 24 | + |
| 25 | +class Test39RecordExtractorSelfCheck extends AnyWordSpec with SparkTestBase with BinaryFileFixture { |
| 26 | + private val copybook = |
| 27 | + """ 01 R. |
| 28 | + 03 A PIC X(2). |
| 29 | + """ |
| 30 | + private val data = "AABBCCDDEEFF" |
| 31 | + |
| 32 | + "Record extractor supporting indexes" should { |
| 33 | + "should work with indexes" in { |
| 34 | + val expected = """[{"A":"AA"},{"A":"BB"},{"A":"CC"},{"A":"DD"},{"A":"EE"},{"A":"FF"}]""" |
| 35 | + |
| 36 | + withTempBinFile("custom_re", ".dat", data.getBytes) { tmpFileName => |
| 37 | + val df = getDataFrame(tmpFileName, Map( |
| 38 | + "record_extractor" -> "za.co.absa.cobrix.spark.cobol.mocks.FixedRecordExtractor", |
| 39 | + "input_split_records" -> "2") |
| 40 | + ) |
| 41 | + |
| 42 | + val actual = df.toJSON.collect().mkString("[", ",", "]") |
| 43 | + |
| 44 | + assert(actual == expected) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + "Record extractor not supporting indexes" should { |
| 50 | + "should fail self checks" ignore /* Not implemented yet */ { |
| 51 | + withTempBinFile("custom_re", ".dat", data.getBytes) { tmpFileName => |
| 52 | + val df = getDataFrame(tmpFileName, Map( |
| 53 | + "record_extractor" -> "za.co.absa.cobrix.spark.cobol.mocks.FixedRecordExtractorNoIndex", |
| 54 | + "input_split_records" -> "2") |
| 55 | + ) |
| 56 | + |
| 57 | + val ex = intercept[RuntimeException] { |
| 58 | + df.show(false) |
| 59 | + df.count() |
| 60 | + } |
| 61 | + |
| 62 | + assert(ex.getMessage == "") |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + "should still work if self checks is turned off" in { |
| 67 | + withTempBinFile("custom_re", ".dat", data.getBytes) { tmpFileName => |
| 68 | + val df = getDataFrame(tmpFileName, Map( |
| 69 | + "enable_self_checks" -> "false", |
| 70 | + "record_extractor" -> "za.co.absa.cobrix.spark.cobol.mocks.FixedRecordExtractorNoIndex", |
| 71 | + "input_split_records" -> "2") |
| 72 | + ) |
| 73 | + |
| 74 | + // No guarantees regarding the correct record count at this point |
| 75 | + assert(df.count() > 4) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + "should still work if indexes are disabled" in { |
| 80 | + val expected = """[{"A":"AA"},{"A":"BB"},{"A":"CC"},{"A":"DD"},{"A":"EE"},{"A":"FF"}]""" |
| 81 | + |
| 82 | + withTempBinFile("custom_re", ".dat", data.getBytes) { tmpFileName => |
| 83 | + val df = getDataFrame(tmpFileName, Map( |
| 84 | + "record_extractor" -> "za.co.absa.cobrix.spark.cobol.mocks.FixedRecordExtractorNoIndex", |
| 85 | + "enable_indexes" -> "false") |
| 86 | + ) |
| 87 | + |
| 88 | + val actual = df.toJSON.collect().mkString("[", ",", "]") |
| 89 | + |
| 90 | + assert(actual == expected) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private def getDataFrame(inputPath: String, extraOptions: Map[String, String] = Map.empty[String, String]): DataFrame = { |
| 96 | + spark |
| 97 | + .read |
| 98 | + .format("cobol") |
| 99 | + .option("copybook_contents", copybook) |
| 100 | + .option("encoding", "ascii") |
| 101 | + .options(extraOptions) |
| 102 | + .load(inputPath) |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments