<commit_before>package uk.ac.wellcome.platform.api.works.v2

import com.twitter.finagle.http.Status
import com.twitter.finatra.http.EmbeddedHttpServer
import uk.ac.wellcome.models.work.internal.{Concept, Subject, Unidentifiable}

class ApiV2WorksIncludesTest extends ApiV2WorksTestBase {
  it(
    "includes a list of identifiers on a list endpoint if we pass ?include=identifiers") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>
        val works = createIdentifiedWorks(count = 2).sortBy { _.canonicalId }

        val identifier0 = createSourceIdentifier
        val identifier1 = createSourceIdentifier

        val work0 = works(0).copy(otherIdentifiers = List(identifier0))
        val work1 = works(1).copy(otherIdentifiers = List(identifier1))

        insertIntoElasticsearch(indexNameV2, itemType, work0, work1)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works?include=identifiers",
            andExpect = Status.Ok,
            withJsonBody =
              s"""
                              |{
                              |  ${resultList(apiPrefix, totalResults = 2)},
                              |  "results": [
                              |   {
                              |     "type": "Work",
                              |     "id": "${work0.canonicalId}",
                              |     "title": "${work0.title}",
                              |     "contributors": [ ],
                              |     "identifiers": [ ${identifier(
                   work0.sourceIdentifier)}, ${identifier(identifier0)} ],
                              |     "genres": [ ],
                              |     "production": [ ]
                              |   },
                              |   {
                              |     "type": "Work",
                              |     "id": "${work1.canonicalId}",
                              |     "title": "${work1.title}",
                              |     "contributors": [ ],
                              |     "identifiers": [ ${identifier(
                   work1.sourceIdentifier)}, ${identifier(identifier1)} ],
                              |     "genres": [ ],
                              |     "production": [ ]
                              |   }
                              |  ]
                              |}
          """.stripMargin
          )
        }
    }
  }

  it(
    "includes a list of identifiers on a single work endpoint if we pass ?include=identifiers") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>
        val otherIdentifier = createSourceIdentifier
        val work = createIdentifiedWorkWith(
          otherIdentifiers = List(otherIdentifier)
        )
        insertIntoElasticsearch(indexNameV2, itemType, work)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works/${work.canonicalId}?include=identifiers",
            andExpect = Status.Ok,
            withJsonBody =
              s"""
                              |{
                              | "@context": "https://localhost:8888/$apiPrefix/context.json",
                              | "type": "Work",
                              | "id": "${work.canonicalId}",
                              | "title": "${work.title}",
                              | "contributors": [ ],
                              | "identifiers": [ ${identifier(
                   work.sourceIdentifier)}, ${identifier(otherIdentifier)} ],
                              | "genres": [ ],
                              | "production": [ ]
                              |}
          """.stripMargin
          )
        }
    }
  }

  it("renders the items if the items include is present") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>
        val work = createIdentifiedWorkWith(
          items = createIdentifiedItems(count = 1) :+ createUnidentifiableItemWith()
        )

        insertIntoElasticsearch(indexNameV2, itemType, work)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works/${work.canonicalId}?include=items",
            andExpect = Status.Ok,
            withJsonBody = s"""
                              |{
                              | "@context": "https://localhost:8888/$apiPrefix/context.json",
                              | "type": "Work",
                              | "id": "${work.canonicalId}",
                              | "title": "${work.title}",
                              | "contributors": [ ],
                              | "items": [ ${items(work.items)} ],
                              | "genres": [ ],
                              | "production": [ ]
                              |}
          """.stripMargin
          )
        }
    }
  }

  it(
    "includes a list of subjects on a list endpoint if we pass ?include=subjects") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>
        val works = createIdentifiedWorks(count = 2).sortBy { _.canonicalId }


        val subjects1 = List(Subject("ornitology", List(Unidentifiable(Concept("ornitology")))))
        val subjects2 = List(Subject("flying cars", List(Unidentifiable(Concept("flying cars")))))
        val work0 = works(0).copy(subjects = subjects1)
        val work1 = works(1).copy(subjects = subjects2)

        insertIntoElasticsearch(indexNameV2, itemType, work0, work1)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works?include=subjects",
            andExpect = Status.Ok,
            withJsonBody =
              s"""
                 |{
                 |  ${resultList(apiPrefix, totalResults = 2)},
                 |  "results": [
                 |   {
                 |     "type": "Work",
                 |     "id": "${work0.canonicalId}",
                 |     "title": "${work0.title}",
                 |     "contributors": [ ],
                 |     "subjects": [ ${subjects(subjects1)}],
                 |     "genres": [ ],
                 |     "production": [ ]
                 |   },
                 |   {
                 |     "type": "Work",
                 |     "id": "${work1.canonicalId}",
                 |     "title": "${work1.title}",
                 |     "contributors": [ ],
                 |     "subjects": [ ${subjects(subjects2)}],
                 |     "genres": [ ],
                 |     "production": [ ]
                 |   }
                 |  ]
                 |}
          """.stripMargin
          )
        }
    }
  }

}<commit_msg>Add test for single work endpoint and subjects include<commit_after>package uk.ac.wellcome.platform.api.works.v2

import com.twitter.finagle.http.Status
import com.twitter.finatra.http.EmbeddedHttpServer
import uk.ac.wellcome.models.work.internal.{Concept, Subject, Unidentifiable}

class ApiV2WorksIncludesTest extends ApiV2WorksTestBase {
  it(
    "includes a list of identifiers on a list endpoint if we pass ?include=identifiers") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>
        val works = createIdentifiedWorks(count = 2).sortBy { _.canonicalId }

        val identifier0 = createSourceIdentifier
        val identifier1 = createSourceIdentifier

        val work0 = works(0).copy(otherIdentifiers = List(identifier0))
        val work1 = works(1).copy(otherIdentifiers = List(identifier1))

        insertIntoElasticsearch(indexNameV2, itemType, work0, work1)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works?include=identifiers",
            andExpect = Status.Ok,
            withJsonBody =
              s"""
                              |{
                              |  ${resultList(apiPrefix, totalResults = 2)},
                              |  "results": [
                              |   {
                              |     "type": "Work",
                              |     "id": "${work0.canonicalId}",
                              |     "title": "${work0.title}",
                              |     "contributors": [ ],
                              |     "identifiers": [ ${identifier(
                   work0.sourceIdentifier)}, ${identifier(identifier0)} ],
                              |     "genres": [ ],
                              |     "production": [ ]
                              |   },
                              |   {
                              |     "type": "Work",
                              |     "id": "${work1.canonicalId}",
                              |     "title": "${work1.title}",
                              |     "contributors": [ ],
                              |     "identifiers": [ ${identifier(
                   work1.sourceIdentifier)}, ${identifier(identifier1)} ],
                              |     "genres": [ ],
                              |     "production": [ ]
                              |   }
                              |  ]
                              |}
          """.stripMargin
          )
        }
    }
  }

  it(
    "includes a list of identifiers on a single work endpoint if we pass ?include=identifiers") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>
        val otherIdentifier = createSourceIdentifier
        val work = createIdentifiedWorkWith(
          otherIdentifiers = List(otherIdentifier)
        )
        insertIntoElasticsearch(indexNameV2, itemType, work)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works/${work.canonicalId}?include=identifiers",
            andExpect = Status.Ok,
            withJsonBody =
              s"""
                              |{
                              | "@context": "https://localhost:8888/$apiPrefix/context.json",
                              | "type": "Work",
                              | "id": "${work.canonicalId}",
                              | "title": "${work.title}",
                              | "contributors": [ ],
                              | "identifiers": [ ${identifier(
                   work.sourceIdentifier)}, ${identifier(otherIdentifier)} ],
                              | "genres": [ ],
                              | "production": [ ]
                              |}
          """.stripMargin
          )
        }
    }
  }

  it("renders the items if the items include is present") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>
        val work = createIdentifiedWorkWith(
          items = createIdentifiedItems(count = 1) :+ createUnidentifiableItemWith()
        )

        insertIntoElasticsearch(indexNameV2, itemType, work)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works/${work.canonicalId}?include=items",
            andExpect = Status.Ok,
            withJsonBody = s"""
                              |{
                              | "@context": "https://localhost:8888/$apiPrefix/context.json",
                              | "type": "Work",
                              | "id": "${work.canonicalId}",
                              | "title": "${work.title}",
                              | "contributors": [ ],
                              | "items": [ ${items(work.items)} ],
                              | "genres": [ ],
                              | "production": [ ]
                              |}
          """.stripMargin
          )
        }
    }
  }

  it(
    "includes a list of subjects on a list endpoint if we pass ?include=subjects") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>
        val works = createIdentifiedWorks(count = 2).sortBy { _.canonicalId }


        val subjects1 = List(Subject("ornitology", List(Unidentifiable(Concept("ornitology")))))
        val subjects2 = List(Subject("flying cars", List(Unidentifiable(Concept("flying cars")))))
        val work0 = works(0).copy(subjects = subjects1)
        val work1 = works(1).copy(subjects = subjects2)

        insertIntoElasticsearch(indexNameV2, itemType, work0, work1)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works?include=subjects",
            andExpect = Status.Ok,
            withJsonBody =
              s"""
                 |{
                 |  ${resultList(apiPrefix, totalResults = 2)},
                 |  "results": [
                 |   {
                 |     "type": "Work",
                 |     "id": "${work0.canonicalId}",
                 |     "title": "${work0.title}",
                 |     "contributors": [ ],
                 |     "subjects": [ ${subjects(subjects1)}],
                 |     "genres": [ ],
                 |     "production": [ ]
                 |   },
                 |   {
                 |     "type": "Work",
                 |     "id": "${work1.canonicalId}",
                 |     "title": "${work1.title}",
                 |     "contributors": [ ],
                 |     "subjects": [ ${subjects(subjects2)}],
                 |     "genres": [ ],
                 |     "production": [ ]
                 |   }
                 |  ]
                 |}
          """.stripMargin
          )
        }
    }
  }

  it(
    "includes a list of subjects on a single work endpoint if we pass ?include=subjects") {
    withV2Api {
      case (apiPrefix, _, indexNameV2, itemType, server: EmbeddedHttpServer) =>

        val subject = List(Subject("ornitology", List(Unidentifiable(Concept("ornitology")))))
        val work = createIdentifiedWork.copy(subjects = subject)

        insertIntoElasticsearch(indexNameV2, itemType, work)

        eventually {
          server.httpGet(
            path = s"/$apiPrefix/works/${work.canonicalId}?include=subjects",
            andExpect = Status.Ok,
            withJsonBody =
              s"""
                 |{
                 |"@context": "https://localhost:8888/$apiPrefix/context.json",
                 |     "type": "Work",
                 |     "id": "${work.canonicalId}",
                 |     "title": "${work.title}",
                 |     "contributors": [ ],
                 |     "subjects": [ ${subjects(subject)}],
                 |     "genres": [ ],
                 |     "production": [ ]
                 |   }
          """.stripMargin
          )
        }
    }
  }

}
{'fraction_non_alphanumeric': 0.12229969920700028, 'fraction_numerical': 0.008955427946404157, 'mean_word_length': 1.2094849720586014, 'pattern_counts': {'":': 100, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1094887', 'n_tokens_mistral': 3730, 'n_tokens_neox': 3642, 'n_words': 633}