<commit_before>package org.globalnames
package resolver
package api

import java.io.File
import java.nio.file.Paths

import akka.event.Logging
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model.ContentTypes._
import akka.http.scaladsl.model.{HttpEntity, StatusCode}
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model.Uri.Path
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.apache.commons.io.FileUtils
import org.globalnames.resolver.model.Matches
import slick.driver.PostgresDriver.api._

import scala.collection.JavaConversions._
import com.typesafe.config.{ConfigFactory, ConfigRenderOptions}
import spray.json._
import DefaultJsonProtocol._
import Ops._
import akka.http.scaladsl.server.Route

class GnresolverMicroserviceIntegrationSpec extends SpecConfig with ApiSpecConfig
                                               with Service with ScalatestRouteTest {

  override val config = ConfigFactory.load()
  override val logger = Logging(system, getClass)
  override val database = Database.forConfig("postgresql-test")
  override val matcher = Matcher(Seq(), maxDistance = 2)

  describe("GnresolverMicroservice") {
    seed("test_api", "GnresolverMicroserviceIntegrationSpec")

    def test(description: String, method: String, url: String,
             statusCode: StatusCode, responseBody: Option[JsValue]): Unit = {
        it(s"$method $url ~> $statusCode") {
        val request = method match {
          case "GET" => Get(url)
          case "POST" => Post(url)
          case meth => fail(s"Method is not supported in testing: $meth")
        }

        request ~> Route.seal(routes) ~> check {
          status shouldBe statusCode
          if (responseBody.isDefined) {
            responseAs[String].parseJson shouldBe responseBody.value
          }
        }
      }
    }

    val testFolder = "test_data"
    val testFolderUri = ClassLoader.getSystemResource(testFolder).toURI
    FileUtils.listFiles(new File(testFolderUri), Array("conf"), true).foreach { f =>
      val testFilePath = Paths.get(testFolder, testFolderUri.relativize(f.toURI).getPath)
                              .normalize.toString
      val conf = ConfigFactory.load(testFilePath)

      val description = conf.getString("description")
      val statusCode: StatusCode = conf.getInt("response.status")
      val responseBody = conf.getOptionalObject("response.body").map { obj =>
                           obj.render(ConfigRenderOptions.concise).parseJson
                         }
      describe(description) {
        for { url <- conf.getStringList("request.urls")
              method <- conf.getStringList("request.methods") } {
          test(description, method, url, statusCode, responseBody)
        }
      }
    }

    describe("/name_strings") {
      describe("access by UUID") {
        it("returns one record if found") {
          Get("/api/name_strings/b701ec9e-efb0-5d5b-bf03-b920c00d0a77") ~> routes ~> check {
            status shouldBe OK
            val response = responseAs[Matches]
            response.matches should have size 1
            response.matches(0).nameString.name.value shouldBe "Favorinus horridus"
          }
        }<commit_msg>= Add path to test file to description<commit_after>package org.globalnames
package resolver
package api

import java.io.File
import java.nio.file.Paths

import akka.event.Logging
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.model.ContentTypes._
import akka.http.scaladsl.model.{HttpEntity, StatusCode}
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.model.Uri.Path
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.apache.commons.io.FileUtils
import org.globalnames.resolver.model.Matches
import slick.driver.PostgresDriver.api._

import scala.collection.JavaConversions._
import com.typesafe.config.{ConfigFactory, ConfigRenderOptions}
import spray.json._
import DefaultJsonProtocol._
import Ops._
import akka.http.scaladsl.server.Route

class GnresolverMicroserviceIntegrationSpec extends SpecConfig with ApiSpecConfig
                                               with Service with ScalatestRouteTest {

  override val config = ConfigFactory.load()
  override val logger = Logging(system, getClass)
  override val database = Database.forConfig("postgresql-test")
  override val matcher = Matcher(Seq(), maxDistance = 2)

  describe("GnresolverMicroservice") {
    seed("test_api", "GnresolverMicroserviceIntegrationSpec")

    def test(description: String, method: String, url: String,
             statusCode: StatusCode, responseBody: Option[JsValue]): Unit = {
        it(s"$method $url ~> $statusCode") {
        val request = method match {
          case "GET" => Get(url)
          case "POST" => Post(url)
          case meth => fail(s"Method is not supported in testing: $meth")
        }

        request ~> Route.seal(routes) ~> check {
          status shouldBe statusCode
          if (responseBody.isDefined) {
            responseAs[String].parseJson shouldBe responseBody.value
          }
        }
      }
    }

    val testFolder = "test_data"
    val testFolderUri = ClassLoader.getSystemResource(testFolder).toURI
    FileUtils.listFiles(new File(testFolderUri), Array("conf"), true).foreach { f =>
      val testFilePath = Paths.get(testFolder, testFolderUri.relativize(f.toURI).getPath)
                              .normalize.toString
      val conf = ConfigFactory.load(testFilePath)

      val description = conf.getString("description")
      val statusCode: StatusCode = conf.getInt("response.status")
      val responseBody = conf.getOptionalObject("response.body").map { obj =>
                           obj.render(ConfigRenderOptions.concise).parseJson
                         }
      describe(s"$description ($testFilePath)") {
        for { url <- conf.getStringList("request.urls")
              method <- conf.getStringList("request.methods") } {
          test(description, method, url, statusCode, responseBody)
        }
      }
    }

    describe("/name_strings") {
      describe("access by UUID") {
        it("returns one record if found") {
          Get("/api/name_strings/b701ec9e-efb0-5d5b-bf03-b920c00d0a77") ~> routes ~> check {
            status shouldBe OK
            val response = responseAs[Matches]
            response.matches should have size 1
            response.matches(0).nameString.name.value shouldBe "Favorinus horridus"
          }
        }
{'fraction_non_alphanumeric': 0.09949718116714917, 'fraction_numerical': 0.0060947737315252175, 'mean_word_length': 2.8955489614243324, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2774686', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1832, 'n_words': 388}