<commit_before>package uk.ac.wellcome.platform.archive.registrar.http.fixtures

import java.net.URL

import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse}
import akka.stream.Materializer
import com.google.inject.Guice
import io.circe.Decoder
import org.scalatest.concurrent.ScalaFutures
import uk.ac.wellcome.json.JsonUtil._
import uk.ac.wellcome.platform.archive.common.config.models.OldHttpServerConfig
import uk.ac.wellcome.platform.archive.common.fixtures.RandomThings
import uk.ac.wellcome.platform.archive.common.modules._
import uk.ac.wellcome.platform.archive.registrar.common.models.StorageManifest
import uk.ac.wellcome.platform.archive.registrar.common.modules.VHSModule
import uk.ac.wellcome.storage.ObjectStore
import uk.ac.wellcome.storage.fixtures.LocalDynamoDb.Table
import uk.ac.wellcome.storage.fixtures.LocalVersionedHybridStore
import uk.ac.wellcome.storage.fixtures.S3.Bucket
import uk.ac.wellcome.storage.vhs.{EmptyMetadata, VersionedHybridStore}
import uk.ac.wellcome.test.fixtures.{Akka, TestWith}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

trait RegistrarHttpFixture
    extends LocalVersionedHybridStore
    with RandomThings
    with ScalaFutures
    with Akka {

  def withApp[R](
    table: Table,
    bucket: Bucket,
    s3Prefix: String,
    serverConfig: OldHttpServerConfig)(testWith: TestWith[AkkaHttpApp, R]) = {

    val progress = new AkkaHttpApp {
      val injector = Guice.createInjector(
        AkkaModule,
        VHSModule,
        DynamoClientModule
      )
    }
    testWith(progress)
  }

  def withConfiguredApp[R](
    testWith: TestWith[(VersionedHybridStore[StorageManifest,
                                             EmptyMetadata,
                                             ObjectStore[StorageManifest]],
                        String,
                        AkkaHttpApp),
                       R]) = {

    val host = "localhost"
    val port = randomPort
    val baseUrl = s"http://$host:$port"
    val contextUrl = new URL(
      "http://api.wellcomecollection.org/storage/v1/context.json")

    val serverConfig = OldHttpServerConfig(host, port, baseUrl, contextUrl)

    withLocalS3Bucket { bucket =>
      withLocalDynamoDbTable { table =>
        val s3Prefix = "archive"
        withTypeVHS[StorageManifest, EmptyMetadata, R](bucket, table, s3Prefix) {
          vhs =>
            withApp(table, bucket, s3Prefix, serverConfig) { progressHttp =>
              testWith((vhs, baseUrl, progressHttp))
            }
        }
      }
    }
  }

  def getT[T](entity: HttpEntity)(implicit decoder: Decoder[T],
                                  materializer: Materializer) = {
    val timeout = 300.millis

    val stringBody = entity
      .toStrict(timeout)
      .map(_.data)
      .map(_.utf8String)
      .value
      .get
      .get
    fromJson[T](stringBody).get
  }

  def whenRequestReady[R](r: HttpRequest)(testWith: TestWith[HttpResponse, R]) =
    withActorSystem { implicit actorSystem =>
      val request = Http().singleRequest(r)
      whenReady(request) { result =>
        testWith(result)
      }
    }
}
<commit_msg>Sort out the test fixture<commit_after>package uk.ac.wellcome.platform.archive.registrar.http.fixtures

import java.net.URL

import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpEntity, HttpRequest, HttpResponse}
import akka.stream.Materializer
import com.google.inject.Guice
import io.circe.Decoder
import org.scalatest.concurrent.ScalaFutures
import uk.ac.wellcome.json.JsonUtil._
import uk.ac.wellcome.platform.archive.common.config.models.{HTTPServerConfig, OldHttpServerConfig}
import uk.ac.wellcome.platform.archive.common.fixtures.RandomThings
import uk.ac.wellcome.platform.archive.common.modules._
import uk.ac.wellcome.platform.archive.registrar.common.models.StorageManifest
import uk.ac.wellcome.platform.archive.registrar.common.modules.VHSModule
import uk.ac.wellcome.platform.archive.registrar.http.RegistrarHTTP
import uk.ac.wellcome.storage.ObjectStore
import uk.ac.wellcome.storage.fixtures.LocalDynamoDb.Table
import uk.ac.wellcome.storage.fixtures.LocalVersionedHybridStore
import uk.ac.wellcome.storage.fixtures.S3.Bucket
import uk.ac.wellcome.storage.vhs.{EmptyMetadata, VersionedHybridStore}
import uk.ac.wellcome.test.fixtures.{Akka, TestWith}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

trait RegistrarHttpFixture
    extends LocalVersionedHybridStore
    with RandomThings
    with ScalaFutures
    with Akka {

  def withApp[R](
    table: Table,
    bucket: Bucket,
    s3Prefix: String,
    httpServerConfig: HTTPServerConfig,
    contextURL: URL)(testWith: TestWith[RegistrarHTTP, R]): R = {
    withTypeVHS[StorageManifest, EmptyMetadata, R](bucket, table) { vhs =>
      val registrarHTTP = new RegistrarHTTP(
        vhs = vhs,
        httpServerConfig = httpServerConfig,
        contextURL = contextURL
      )

      testWith(registrarHTTP)
    }

  def withConfiguredApp[R](
    testWith: TestWith[(VersionedHybridStore[StorageManifest,
                                             EmptyMetadata,
                                             ObjectStore[StorageManifest]],
                        String,
                        RegistrarHTTP),
                       R]): R = {
    val host = "localhost"
    val port = randomPort
    val externalBaseURL = s"http://$host:$port"
    val contextURL = new URL(
      "http://api.wellcomecollection.org/storage/v1/context.json")

    val httpServerConfig = HTTPServerConfig(
      host = host,
      port = port,
      externalBaseURL = externalBaseURL
    )

    withLocalS3Bucket { bucket =>
      withLocalDynamoDbTable { table =>
        val s3Prefix = "archive"
        withTypeVHS[StorageManifest, EmptyMetadata, R](bucket, table, s3Prefix) {
          vhs =>
            withApp(table, bucket, s3Prefix, httpServerConfig, contextURL) { progressHttp =>
              testWith((vhs, externalBaseURL, progressHttp))
            }
        }
      }
    }
  }

  def getT[T](entity: HttpEntity)(implicit decoder: Decoder[T],
                                  materializer: Materializer) = {
    val timeout = 300.millis

    val stringBody = entity
      .toStrict(timeout)
      .map(_.data)
      .map(_.utf8String)
      .value
      .get
      .get
    fromJson[T](stringBody).get
  }

  def whenRequestReady[R](r: HttpRequest)(testWith: TestWith[HttpResponse, R]) =
    withActorSystem { implicit actorSystem =>
      val request = Http().singleRequest(r)
      whenReady(request) { result =>
        testWith(result)
      }
    }
}

{'fraction_non_alphanumeric': 0.0970873786407767, 'fraction_numerical': 0.003286034353995519, 'mean_word_length': 2.8350515463917527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7156768', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1997, 'n_words': 350}