Skip to content
This repository has been archived by the owner on Jul 26, 2021. It is now read-only.

Commit

Permalink
Factor out file reading from YamlPathConfigReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Galic committed Apr 10, 2017
1 parent 0c3687c commit 91c94f4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/main/scala/com/gu/tip/YamlPathConfigReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import net.jcazevedo.moultingyaml._
import net.jcazevedo.moultingyaml.DefaultYamlProtocol

import scala.io.Source._
import scala.util.{Success, Try}

// $COVERAGE-OFF$
case class Path(name: String, description: String)
Expand Down Expand Up @@ -54,16 +53,15 @@ class Paths(val paths: Map[String, EnrichedPath]) {
object YamlPathConfigReader extends LazyLogging {
import TipYamlProtocol._

def apply(filename: String): Paths = try {
println("Hello World")
println(filename)
val source = fromFile(getClass.getClassLoader.getResource(filename).getPath).mkString
val pathList = source.parseYaml.convertTo[List[Path]]
private def readFile(filename: String): String =
Option(getClass.getClassLoader.getResource(filename)).map {
path => fromFile(path.getPath).mkString
}.getOrElse(throw new FileNotFoundException(s"Path definition file not found on the classpath: $filename"))

def apply(filename: String): Paths = {
val pathList = readFile(filename).parseYaml.convertTo[List[Path]]
val paths: Map[String, EnrichedPath] = pathList.map(path => path.name -> new EnrichedPath(path)).toMap
new Paths(paths)
} catch {
case e: DeserializationException => throw new DeserializationException(s"Syntax problem with path config $filename file. Refer to README for the correct syntax: ", e)
case e => throw new FileNotFoundException(s"Path definition file not found on the classpath: $filename")
}
}

5 changes: 4 additions & 1 deletion src/test/scala/com/gu/tip/YamlPathConfigReaderTest.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.gu.tip

import java.io.FileNotFoundException

import org.scalatest.{FlatSpec, Matchers}
import com.gu.tip.{EnrichedPath, Path, Paths, YamlPathConfigReader}

class YamlPathConfigReaderTest extends FlatSpec with Matchers {

behavior of "PathDefinitionReader"

"YamlPathConfigReader" should "convert tip.yaml definition to Paths type" in {
YamlPathConfigReader("tip.yaml") shouldBe a [Paths]
}
Expand Down

0 comments on commit 91c94f4

Please sign in to comment.