Skip to content

Commit

Permalink
Merge pull request #4293 from guardian/fix-reading-config-arrays
Browse files Browse the repository at this point in the history
FIX `getOptionalStringSet` in CommonConfig
  • Loading branch information
twrichards authored Jun 26, 2024
2 parents 215409a + 93bcbb5 commit 66ed046
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ abstract class CommonConfig(resources: GridConfigResources) extends AwsClientBui

final def getOptionalStringSet(key: String): Option[Set[String]] = Try {
configuration.getOptional[Seq[String]](key)
}.recover {
case _:ConfigException.WrongType => configuration.getOptional[String](key).map(_.split(",").toSeq.map(_.trim))
}.toOption.flatten.map(_.toSet)
}.getOrElse(
configuration.getOptional[String](key).map(_.split(",").toSeq.map(_.trim))
).map(_.toSet)

final def getStringSet(key: String): Set[String] = getOptionalStringSet(key).getOrElse(Set.empty)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.gu.mediaservice.lib.config

import org.mockito.Mockito.when
import org.scalatest.funsuite.AnyFunSuiteLike
import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper
import org.scalatestplus.mockito.MockitoSugar
import play.api.Configuration

class CommonConfigTest extends AnyFunSuiteLike with MockitoSugar {

private val commonConf = mock[CommonConfig]
when(commonConf.configuration).thenReturn(Configuration(
"setAsCommaSepString" -> "a, b,c",
"setAsActualArray" -> Set("a", "b", "c")
))

test("testGetOptionalStringSet") {
commonConf.getOptionalStringSet("doesnt.exist") shouldBe None
commonConf.getOptionalStringSet("setAsCommaSepString") shouldBe Some(Set("a", "b", "c"))
commonConf.getOptionalStringSet("setAsActualArray") shouldBe Some(Set("a", "b", "c"))
}

test("testGetStringSet") {
commonConf.getStringSet("doesnt.exist") shouldBe Set.empty
commonConf.getStringSet("setAsCommaSepString") shouldBe Set("a", "b", "c")
commonConf.getStringSet("setAsActualArray") shouldBe Set("a", "b", "c")
}

}

0 comments on commit 66ed046

Please sign in to comment.