Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds more meaningful error message on invalid enum values #1187

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ object CellXformer {
} else if (Asset::class.java.isAssignableFrom(type)) {
AssetRefXformer.decode(ctx, value, fieldName)
} else if (AtlanEnum::class.java.isAssignableFrom(type)) {
EnumXformer.decode(value, type as Class<AtlanEnum>)
EnumXformer.decode(value, type as Class<AtlanEnum>, fieldName)
} else if (AtlanStruct::class.java.isAssignableFrom(type)) {
StructXformer.decode(ctx.client, value, type as Class<AtlanStruct>)
} else if (AtlanTag::class.java.isAssignableFrom(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ object EnumXformer {
fun decode(
enum: String,
enumClass: Class<AtlanEnum>,
fieldName: String,
): AtlanEnum {
val method = enumClass.getMethod("fromValue", String::class.java)
return method.invoke(null, enum) as AtlanEnum
val result: Any? = method.invoke(null, enum)
if (result == null) {
throw IllegalArgumentException("$enumClass (in field $fieldName) does not have any matching value for: $enum")
} else {
return result as AtlanEnum
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* SPDX-License-Identifier: Apache-2.0
Copyright 2023 Atlan Pte. Ltd. */
package com.atlan.pkg.aim

import AssetImportCfg
import com.atlan.model.assets.Connection
import com.atlan.model.assets.Table
import com.atlan.model.enums.AtlanConnectorType
import com.atlan.pkg.PackageTest
import com.atlan.pkg.Utils
import java.nio.file.Paths
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

/**
* Test import of an invalid enumerated value.
*/
class InvalidEnumTest : PackageTest("ie") {
override val logger = Utils.getLogger(this.javaClass.name)

private val table = makeUnique("t1")

private val testFile = "invalid_enum_value.csv"

private val files =
listOf(
testFile,
"debug.log",
)

private fun prepFile(connectionQN: String) {
// Prepare a copy of the file with unique names for glossaries and tags
val input = Paths.get("src", "test", "resources", testFile).toFile()
val output = Paths.get(testDirectory, testFile).toFile()
input.useLines { lines ->
lines.forEach { line ->
val revised =
line
.replace("{{CONNECTION}}", connectionQN)
.replace("{{TABLE}}", table)
output.appendText("$revised\n")
}
}
}

override fun setup() {
val snowflakeConnection = Connection.findByName(client, "development", AtlanConnectorType.SNOWFLAKE)?.get(0)!!
prepFile(snowflakeConnection.qualifiedName)
}

override fun teardown() {
val snowflakeConnection = Connection.findByName(client, "development", AtlanConnectorType.SNOWFLAKE)?.get(0)!!
Table.select(client)
.where(Table.CONNECTION_QUALIFIED_NAME.eq(snowflakeConnection.qualifiedName))
.where(Table.NAME.eq(table))
.stream()
.findFirst()
.ifPresent {
Table.purge(client, it.guid)
}
}

@Test
fun failsWithMeaningfulError() {
val exception =
assertFailsWith<IllegalArgumentException> {
runCustomPackage(
AssetImportCfg(
assetsFile = Paths.get(testDirectory, testFile).toString(),
assetsUpsertSemantic = "upsert",
assetsFailOnErrors = true,
),
Importer::main,
)
}
assertEquals(
"""
class com.atlan.model.enums.CertificateStatus (in field certificateStatus) does not have any matching value for: CERTIFIED
""".trimIndent(),
exception.message,
)
}

@Test
fun filesCreated() {
validateFilesExist(files)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
qualifiedName,typeName,name,displayName,description,userDescription,ownerUsers,ownerGroups,certificateStatus,certificateStatusMessage,announcementType,announcementTitle,announcementMessage,assignedTerms,atlanTags,links,readme,starredDetails,connectorType,connectionQualifiedName,databaseQualifiedName,databaseName,schemaQualifiedName,schemaName
{{CONNECTION}}/ANALYTICS/WIDE_WORLD_IMPORTERS/{{TABLE}},Table,{{TABLE}},,,,,,CERTIFIED,,,,,,,,,,snowflake,{{CONNECTION}},{{CONNECTION}}/ANALYTICS,ANALYTICS,{{CONNECTION}}/ANALYTICS/WIDE_WORLD_IMPORTERS,WIDE_WORLD_IMPORTERS
Loading