diff --git a/modules/runtime-common/src/test/resources/config_parser_test/config.hocon b/modules/runtime-common/src/test/resources/config_parser_test/config.hocon new file mode 100644 index 0000000..89aba02 --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/config.hocon @@ -0,0 +1,9 @@ +{ + "field1": "value1", + "field2": 10, + "field3": true, + "field4": { + "field41": "value41", + "field42": "value42" + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/config_correct_namespace.hocon b/modules/runtime-common/src/test/resources/config_parser_test/config_correct_namespace.hocon new file mode 100644 index 0000000..ed8ec5e --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/config_correct_namespace.hocon @@ -0,0 +1,9 @@ +snowplow { + "field1": "value1", + "field2": 10, + "field3": true, + "field4": { + "field41": "value41", + "field42": "value42" + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/config_invalid.hocon b/modules/runtime-common/src/test/resources/config_parser_test/config_invalid.hocon new file mode 100644 index 0000000..adf602a --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/config_invalid.hocon @@ -0,0 +1,9 @@ +{ + "field1": "value1", + "field2": 10, + "field3": true, + "field4": + "field41": "value41", + "field42": "value42" + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/config_missing_field.hocon b/modules/runtime-common/src/test/resources/config_parser_test/config_missing_field.hocon new file mode 100644 index 0000000..952cea2 --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/config_missing_field.hocon @@ -0,0 +1,8 @@ +{ + "field1": "value1", + "field2": 10, + "field3": true, + "field4": { + "field42": "value42" + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/config_with_set_env.hocon b/modules/runtime-common/src/test/resources/config_parser_test/config_with_set_env.hocon new file mode 100644 index 0000000..27cd959 --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/config_with_set_env.hocon @@ -0,0 +1,9 @@ +{ + "field1": "value1", + "field2": 10, + "field3": true, + "field4": { + "field41": ${CONFIG_PARSER_TEST_ENV}, + "field42": "value42" + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/config_with_substitution.hocon b/modules/runtime-common/src/test/resources/config_parser_test/config_with_substitution.hocon new file mode 100644 index 0000000..d922980 --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/config_with_substitution.hocon @@ -0,0 +1,13 @@ +{ + "field1": ${subfield.a}, + "field2": 10, + "field3": true, + "field4": { + "field41": ${subfield.b}, + "field42": "value42" + } + "subfield": { + "a": "sub1" + "b": "sub2" + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/config_with_unset_env.hocon b/modules/runtime-common/src/test/resources/config_parser_test/config_with_unset_env.hocon new file mode 100644 index 0000000..ddf2fbf --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/config_with_unset_env.hocon @@ -0,0 +1,9 @@ +{ + "field1": "value1", + "field2": 10, + "field3": true, + "field4": { + "field41": ${UNSET_ENV_VAR}, + "field42": "value42" + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/config_wrong_namespace.hocon b/modules/runtime-common/src/test/resources/config_parser_test/config_wrong_namespace.hocon new file mode 100644 index 0000000..1e016b6 --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/config_wrong_namespace.hocon @@ -0,0 +1,9 @@ +wrongnamespace { + "field1": "value1", + "field2": 10, + "field3": true, + "field4": { + "field41": "value41", + "field42": "value42" + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver.json b/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver.json new file mode 100644 index 0000000..9fa1429 --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver.json @@ -0,0 +1,28 @@ +{ + "schema": "iglu:com.snowplowanalytics.iglu/resolver-config/jsonschema/1-0-1", + "data": { + "cacheSize": 500, + "repositories": [ + { + "name": "Iglu Central", + "priority": 0, + "vendorPrefixes": [ "com.snowplowanalytics" ], + "connection": { + "http": { + "uri": "http://iglucentral.com" + } + } + }, + { + "name": "Iglu Central - GCP Mirror", + "priority": 1, + "vendorPrefixes": [ "com.snowplowanalytics" ], + "connection": { + "http": { + "uri": "http://mirror01.iglucentral.com" + } + } + } + ] + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver_invalid.json b/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver_invalid.json new file mode 100644 index 0000000..0ea0bbd --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver_invalid.json @@ -0,0 +1,27 @@ +{ + "schema": "iglu:com.snowplowanalytics.iglu/resolver-config/jsonschema/1-0-1", + "data": { + "cacheSize": 500, + "repositories": [ + "name": "Iglu Central", + "priority": 0, + "vendorPrefixes": [ "com.snowplowanalytics" ], + "connection": { + "http": { + "uri": "http://iglucentral.com" + } + } + }, + { + "name": "Iglu Central - GCP Mirror", + "priority": 1, + "vendorPrefixes": [ "com.snowplowanalytics" ], + "connection": { + "http": { + "uri": "http://mirror01.iglucentral.com" + } + } + } + ] + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver_missing_field.json b/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver_missing_field.json new file mode 100644 index 0000000..07e1cbd --- /dev/null +++ b/modules/runtime-common/src/test/resources/config_parser_test/iglu_resolver_missing_field.json @@ -0,0 +1,27 @@ +{ + "schema": "iglu:com.snowplowanalytics.iglu/resolver-config/jsonschema/1-0-1", + "data": { + "repositories": [ + { + "name": "Iglu Central", + "priority": 0, + "vendorPrefixes": [ "com.snowplowanalytics" ], + "connection": { + "http": { + "uri": "http://iglucentral.com" + } + } + }, + { + "name": "Iglu Central - GCP Mirror", + "priority": 1, + "vendorPrefixes": [ "com.snowplowanalytics" ], + "connection": { + "http": { + "uri": "http://mirror01.iglucentral.com" + } + } + } + ] + } +} \ No newline at end of file diff --git a/modules/runtime-common/src/test/scala/com/snowplowanalytics/snowplow/runtime/ConfigParserSpec.scala b/modules/runtime-common/src/test/scala/com/snowplowanalytics/snowplow/runtime/ConfigParserSpec.scala new file mode 100644 index 0000000..3840aad --- /dev/null +++ b/modules/runtime-common/src/test/scala/com/snowplowanalytics/snowplow/runtime/ConfigParserSpec.scala @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved. + * + * This program is licensed to you under the Snowplow Community License Version 1.0, + * and you may not use this file except in compliance with the Snowplow Community License Version 1.0. + * You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0 + */ +package com.snowplowanalytics.snowplow.runtime + +import java.nio.file.Paths +import io.circe.literal._ +import io.circe.Decoder +import io.circe.generic.semiauto._ +import cats.effect.IO +import cats.effect.testing.specs2.CatsEffect +import org.specs2.Specification +import com.snowplowanalytics.iglu.client.resolver.Resolver + +class ConfigParserSpec extends Specification with CatsEffect { + import ConfigParserSpec._ + + def is = s2""" + ConfigParser should + parse iglu resolver json correctly $parseIgluResolverCorrectly + fail when iglu resolver json is missing required field $igluResolverMissingField + fail when iglu resolver json is invalid $igluResolverInvalid + fail when iglu resolver json file doesn't exist $igluResolverDoesNotExist + parse config with custom class correctly $parseCustomClassConfig + parse config with 'snowplow' namespace correctly $parseConfigWithCorrectNamespace + parse config with substitution correctly $parseConfigWithSubstitution + fail when wrong namespace is used in the config $configWithWrongNamespace + fail when config hocon is missing required field $configMissingField + fail when config hocon is invalid $configInvalid + fail when config hocon file doesn't exist $configDoesNotExist + parse config with env variable correctly $parseConfigWithEnvVariable + fail when env variable in the config isn't set $failWhenEnvVariableNotSet + """ + + def parseIgluResolverCorrectly = { + val expected = Resolver.ResolverConfig( + cacheSize = 500, + cacheTtl = None, + repositoryRefs = List( + json""" + { + "name": "Iglu Central", + "priority": 0, + "vendorPrefixes": [ "com.snowplowanalytics" ], + "connection": { + "http": { + "uri": "http://iglucentral.com" + } + } + } + """, + json""" + { + "name": "Iglu Central - GCP Mirror", + "priority": 1, + "vendorPrefixes": [ "com.snowplowanalytics" ], + "connection": { + "http": { + "uri": "http://mirror01.iglucentral.com" + } + } + } + """ + ) + ) + val path = Paths.get("src/test/resources/config_parser_test/iglu_resolver.json") + ConfigParser + .igluResolverFromFile[IO](path) + .value + .map(_ must beRight(expected)) + } + + def igluResolverMissingField = { + val expected = "DecodingFailure at .cacheSize: Missing required field" + val path = Paths.get("src/test/resources/config_parser_test/iglu_resolver_missing_field.json") + ConfigParser + .igluResolverFromFile[IO](path) + .value + .map(_ must beLeft(expected)) + } + + def igluResolverInvalid = { + val expected = + "String: 6: List should have ended with ] or had a comma, instead had token: ':' (if you want ':' to be part of a string value, then double-quote it)" + val path = Paths.get("src/test/resources/config_parser_test/iglu_resolver_invalid.json") + ConfigParser + .igluResolverFromFile[IO](path) + .value + .map(_ must beLeft(expected)) + } + + def igluResolverDoesNotExist = { + val expectedPattern = "Error reading .*/iglu_resolver_nonexist.json file from filesystem: .*/iglu_resolver_nonexist.json".r + val path = Paths.get("src/test/resources/config_parser_test/iglu_resolver_nonexist.json") + ConfigParser + .igluResolverFromFile[IO](path) + .value + .map(_ must beLike { case Left(expectedPattern(_*)) => ok }) + } + + def parseCustomClassConfig = { + val expected = TestConfig( + field1 = "value1", + field2 = 10, + field3 = true, + field4 = TestConfig.Subfield(field41 = "value41", field42 = "value42") + ) + val path = Paths.get("src/test/resources/config_parser_test/config.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beRight(expected)) + } + + def parseConfigWithCorrectNamespace = { + val expected = TestConfig( + field1 = "value1", + field2 = 10, + field3 = true, + field4 = TestConfig.Subfield(field41 = "value41", field42 = "value42") + ) + val path = Paths.get("src/test/resources/config_parser_test/config_correct_namespace.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beRight(expected)) + } + + def parseConfigWithSubstitution = { + val expected = TestConfig( + field1 = "sub1", + field2 = 10, + field3 = true, + field4 = TestConfig.Subfield(field41 = "sub2", field42 = "value42") + ) + val path = Paths.get("src/test/resources/config_parser_test/config_with_substitution.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beRight(expected)) + } + + def configWithWrongNamespace = { + val expected = "Cannot resolve config: DecodingFailure at .field1: Missing required field" + val path = Paths.get("src/test/resources/config_parser_test/config_wrong_namespace.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beLeft(expected)) + } + + def configMissingField = { + val expected = "Cannot resolve config: DecodingFailure at .field41: Missing required field" + val path = Paths.get("src/test/resources/config_parser_test/config_missing_field.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beLeft(expected)) + } + + def configInvalid = { + val expected = + "String: 6: Expecting close brace } or a comma, got ':' (if you intended ':' to be part of a key or string value, try enclosing the key or value in double quotes)" + val path = Paths.get("src/test/resources/config_parser_test/config_invalid.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beLeft(expected)) + } + + def configDoesNotExist = { + val expectedPattern = """Error reading .*/config_nonexist.hocon file from filesystem: .*/config_nonexist.hocon""".r + val path = Paths.get("src/test/resources/config_parser_test/config_nonexist.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beLike { case Left(expectedPattern(_*)) => ok }) + } + + def parseConfigWithEnvVariable = { + val expected = TestConfig( + field1 = "value1", + field2 = 10, + field3 = true, + field4 = TestConfig.Subfield(field41 = "envValue", field42 = "value42") + ) + val path = Paths.get("src/test/resources/config_parser_test/config_with_set_env.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beRight(expected)) + } + + def failWhenEnvVariableNotSet = { + val expected = "Cannot resolve config: String: 6: Could not resolve substitution to a value: ${UNSET_ENV_VAR}" + val path = Paths.get("src/test/resources/config_parser_test/config_with_unset_env.hocon") + ConfigParser + .configFromFile[IO, TestConfig](path) + .value + .map(_ must beLeft(expected)) + } +} + +object ConfigParserSpec { + + case class TestConfig( + field1: String, + field2: Int, + field3: Boolean, + field4: TestConfig.Subfield + ) + object TestConfig { + case class Subfield(field41: String, field42: String) + + implicit val testConfigDecoder: Decoder[TestConfig] = deriveDecoder[TestConfig] + implicit val subfieldDecoder: Decoder[Subfield] = deriveDecoder[Subfield] + } +} diff --git a/project/BuildSettings.scala b/project/BuildSettings.scala index 35b1faf..7b88e6c 100644 --- a/project/BuildSettings.scala +++ b/project/BuildSettings.scala @@ -42,6 +42,7 @@ object BuildSettings { scalacOptions += "-Ywarn-macros:after", scalacOptions += "-Wconf:origin=scala.collection.compat.*:s", Test / fork := true, + Test / envVars := Map("CONFIG_PARSER_TEST_ENV" -> "envValue"), addCompilerPlugin(Dependencies.betterMonadicFor), addCompilerPlugin(Dependencies.kindProjector), ThisBuild / autoAPIMappings := true,