Skip to content

Commit

Permalink
Unit tests for ConfigParser (close #51)
Browse files Browse the repository at this point in the history
  • Loading branch information
spenes committed Jan 4, 2024
1 parent 2aeb575 commit 286fea3
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"field1": "value1",
"field2": 10,
"field3": true,
"field4": {
"field41": "value41",
"field42": "value42"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"field1": "value1",
"field2": 10,
"field3": true,
"field4":
"field41": "value41",
"field42": "value42"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"field1": "value1",
"field2": 10,
"field3": true,
"field4": {
"field42": "value42"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"field1": "value1",
"field2": 10,
"field3": true,
"field4": {
"field41": ${CONFIG_PARSER_TEST_ENV},
"field42": "value42"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"field1": "value1",
"field2": 10,
"field3": true,
"field4": {
"field41": ${UNSET_ENV_VAR},
"field42": "value42"
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* 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
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 path = Paths.get("src/test/resources/config_parser_test/iglu_resolver_nonexist.json")
ConfigParser
.igluResolverFromFile[IO](path)
.value
.map(
_ must beLike { case Left(s"Error reading $_/iglu_resolver_nonexist.json file from filesystem: $_/iglu_resolver_nonexist.json") =>
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 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 path = Paths.get("src/test/resources/config_parser_test/config_nonexist.hocon")
ConfigParser
.configFromFile[IO, TestConfig](path)
.value
.map(
_ must beLike { case Left(s"Error reading $_/config_nonexist.hocon file from filesystem: $_/config_nonexist.hocon") =>
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]
}
}
1 change: 1 addition & 0 deletions project/BuildSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 286fea3

Please sign in to comment.