diff --git a/build.sbt b/build.sbt index f2d90e328..6b65f1c87 100644 --- a/build.sbt +++ b/build.sbt @@ -81,7 +81,8 @@ libraryDependencies ++= Seq( "uk.gov.nationalarchives" %% "tdr-auth-utils" % "0.0.171", "io.github.hakky54" % "logcaptor" % "2.9.0" % Test, "com.dimafeng" %% "testcontainers-scala-scalatest" % testContainersVersion % Test, - "com.dimafeng" %% "testcontainers-scala-postgresql" % testContainersVersion % Test + "com.dimafeng" %% "testcontainers-scala-postgresql" % testContainersVersion % Test, + "com.github.tomakehurst" % "wiremock-standalone" % "3.0.1" % Test, ) dependencyOverrides ++= Seq( diff --git a/src/main/resources/application.base.conf b/src/main/resources/application.base.conf index 03ca7263a..93fb244c1 100644 --- a/src/main/resources/application.base.conf +++ b/src/main/resources/application.base.conf @@ -32,7 +32,6 @@ fileUpload { } featureAccessBlock { - assignFileReferences = ${BLOCK_ASSIGN_FILE_REFERENCES} blockValidationLibrary = ${BLOCK_VALIDATION_LIBRARY} } diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index c4a193f91..76727b717 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -11,7 +11,6 @@ frontend = { } featureAccessBlock { - assignFileReferences = true blockValidationLibrary = false } diff --git a/src/main/scala/uk/gov/nationalarchives/tdr/api/utils/TreeNodesUtils.scala b/src/main/scala/uk/gov/nationalarchives/tdr/api/utils/TreeNodesUtils.scala index eccfdf2fb..f0057be6f 100644 --- a/src/main/scala/uk/gov/nationalarchives/tdr/api/utils/TreeNodesUtils.scala +++ b/src/main/scala/uk/gov/nationalarchives/tdr/api/utils/TreeNodesUtils.scala @@ -12,8 +12,6 @@ import scala.annotation.tailrec class TreeNodesUtils(uuidSource: UUIDSource, referenceGeneratorService: ReferenceGeneratorService, config: Config) { - private val referenceGeneratorFeatureBlock: Boolean = config.getBoolean("featureAccessBlock.assignFileReferences") - @tailrec private def innerFunction(originalPath: String, typeIdentifier: String, nodes: Map[String, TreeNode]): Map[String, TreeNode] = { val jioFile = new JIOFile(originalPath) @@ -33,17 +31,13 @@ class TreeNodesUtils(uuidSource: UUIDSource, referenceGeneratorService: Referenc val pathWithoutInitialSlash: String = if (path.startsWith("/")) path.tail else path innerFunction(pathWithoutInitialSlash, typeIdentifier, Map()) }.toMap - if (referenceGeneratorFeatureBlock) { - generatedNodes - } else { - val generatedReferences = referenceGeneratorService.getReferences(generatedNodes.size) - generatedReferences - .zip(generatedNodes.view) - .map { case (reference, (key, treenode)) => - key -> treenode.copy(reference = Some(reference)) - } - .toMap - } + val generatedReferences = referenceGeneratorService.getReferences(generatedNodes.size) + generatedReferences + .zip(generatedNodes.view) + .map { case (reference, (key, treenode)) => + key -> treenode.copy(reference = Some(reference)) + } + .toMap } } diff --git a/src/test/resources/application.conf b/src/test/resources/application.conf index f18834a53..115d47285 100644 --- a/src/test/resources/application.conf +++ b/src/test/resources/application.conf @@ -35,13 +35,12 @@ fileUpload { } featureAccessBlock { - assignFileReferences = true blockValidationLibrary = true } referenceGenerator { - referenceGeneratorUrl = "https://dummy-reference-url.com" + referenceGeneratorUrl = "http://localhost:8080" referenceLimit = 2 } -environment = "intg" +environment = "test" diff --git a/src/test/scala/uk/gov/nationalarchives/tdr/api/routes/FileRouteSpec.scala b/src/test/scala/uk/gov/nationalarchives/tdr/api/routes/FileRouteSpec.scala index e5ea4ac60..a5bb72759 100644 --- a/src/test/scala/uk/gov/nationalarchives/tdr/api/routes/FileRouteSpec.scala +++ b/src/test/scala/uk/gov/nationalarchives/tdr/api/routes/FileRouteSpec.scala @@ -2,14 +2,17 @@ package uk.gov.nationalarchives.tdr.api.routes import akka.http.scaladsl.model.headers.OAuth2BearerToken import com.dimafeng.testcontainers.PostgreSQLContainer +import com.github.tomakehurst.wiremock.WireMockServer +import com.github.tomakehurst.wiremock.client.WireMock +import com.github.tomakehurst.wiremock.client.WireMock.aResponse import io.circe.generic.extras.Configuration import io.circe.generic.extras.auto._ import org.scalatest.matchers.should.Matchers +import uk.gov.nationalarchives.tdr.api.service.FileMetadataService._ import uk.gov.nationalarchives.tdr.api.utils.TestAuthUtils._ import uk.gov.nationalarchives.tdr.api.utils.TestContainerUtils._ import uk.gov.nationalarchives.tdr.api.utils.TestUtils._ import uk.gov.nationalarchives.tdr.api.utils.{FixedUUIDSource, TestContainerUtils, TestRequest, TestUtils} -import uk.gov.nationalarchives.tdr.api.service.FileMetadataService._ import java.sql.{PreparedStatement, Types} import java.util.UUID @@ -67,6 +70,7 @@ class FileRouteSpec extends TestContainerUtils with Matchers with TestRequest { 1 ) } + val referenceMockServer = getReferencesMockServer(4) val res = runTestMutationFileMetadata("mutation_alldata_2", validUserToken()) val distinctDirectoryCount = 3 val fileCount = 5 @@ -85,6 +89,7 @@ class FileRouteSpec extends TestContainerUtils with Matchers with TestRequest { nameAndPath.isDefined should equal(true) nameAndPath.get.fileName should equal(nameAndPath.get.path.split("/").last) }) + referenceMockServer.stop() } "The api" should "return file ids matched with sequence ids for addFilesAndMetadata" in withContainers { case container: PostgreSQLContainer => @@ -93,9 +98,12 @@ class FileRouteSpec extends TestContainerUtils with Matchers with TestRequest { (clientSideProperties ++ serverSideProperties ++ defaultMetadataProperties).foreach(utils.addFileProperty) utils.createConsignment(consignmentId, userId) + val referenceMockServer = getReferencesMockServer(4) + val expectedResponse = expectedFilesAndMetadataMutationResponse("data_all") val response = runTestMutationFileMetadata("mutation_alldata_3", validUserToken()) expectedResponse.data.get.addFilesAndMetadata should equal(response.data.get.addFilesAndMetadata) + referenceMockServer.stop() } "allDescendants" should "return parents and all descendants for the given parent ids" in withContainers { case container: PostgreSQLContainer => @@ -206,4 +214,38 @@ class FileRouteSpec extends TestContainerUtils with Matchers with TestRequest { utils.createFile(UUID.fromString(fileFourId), consignmentId, fileName = "fileFourName", parentId = Some(UUID.fromString(folderId1))) utils.createFile(UUID.fromString(fileFiveId), consignmentId, fileName = "fileFiveName", parentId = Some(UUID.fromString(folderId1))) } + + private def getReferencesMockServer(additionalRefs: Int = 0): WireMockServer = { + val wiremockServer = new WireMockServer(8080) + WireMock.configureFor("localhost", 8080) + wiremockServer.start() + wiremockServer.stubFor( + WireMock + .get(WireMock.urlPathMatching("/test/.*")) + .inScenario("fetch references") + .willReturn( + aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withBody("[\"REF1\",\"REF2\"]") + ) + .willSetStateTo("fetch references 1") + ) + for (current <- 1 to additionalRefs) { + wiremockServer.stubFor( + WireMock + .get(WireMock.urlPathMatching("/test/.*")) + .inScenario("fetch references") + .whenScenarioStateIs(s"fetch references $current") + .willReturn( + aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withBody(s"[\"REF${Math.random()}\",\"REF${Math.random()}\"]") + ) + .willSetStateTo(s"fetch references ${current + 1}") + ) + } + wiremockServer + } } diff --git a/src/test/scala/uk/gov/nationalarchives/tdr/api/service/FileServiceSpec.scala b/src/test/scala/uk/gov/nationalarchives/tdr/api/service/FileServiceSpec.scala index 7c250945f..09964f180 100644 --- a/src/test/scala/uk/gov/nationalarchives/tdr/api/service/FileServiceSpec.scala +++ b/src/test/scala/uk/gov/nationalarchives/tdr/api/service/FileServiceSpec.scala @@ -690,6 +690,7 @@ class FileServiceSpec extends AnyFlatSpec with MockitoSugar with Matchers with S when(fileRepositoryMock.addFiles(fileRowCaptor.capture(), metadataRowCaptor.capture())).thenReturn(Future(())) when(fileStatusServiceMock.addFileStatuses(any[AddMultipleFileStatusesInput])).thenReturn(Future(Nil)) + when(referenceGeneratorServiceMock.getReferences(any[Int])).thenReturn(List("ref1", "ref2", "ref3", "ref4", "ref5")) mockCustomMetadataValuesResponse(customMetadataPropertiesRepositoryMock) val service = new FileService( @@ -785,7 +786,7 @@ class FileServiceSpec extends AnyFlatSpec with MockitoSugar with Matchers with S referenceGeneratorServiceMock, FixedTimeSource, fixedUuidSource, - ConfigFactory.load().withValue("featureAccessBlock.assignFileReferences", ConfigValueFactory.fromAnyRef("false")) + ConfigFactory.load() ) val expectedStatusInput = AddMultipleFileStatusesInput( diff --git a/src/test/scala/uk/gov/nationalarchives/tdr/api/service/ReferenceGeneratorServiceSpec.scala b/src/test/scala/uk/gov/nationalarchives/tdr/api/service/ReferenceGeneratorServiceSpec.scala index 121e3c87f..05cc4d055 100644 --- a/src/test/scala/uk/gov/nationalarchives/tdr/api/service/ReferenceGeneratorServiceSpec.scala +++ b/src/test/scala/uk/gov/nationalarchives/tdr/api/service/ReferenceGeneratorServiceSpec.scala @@ -16,7 +16,7 @@ class ReferenceGeneratorServiceSpec extends AnyFlatSpec with Matchers { "getReferences" should "return a list of references" in { val sttpBackendStub = SttpBackendStub.synchronous - .whenRequestMatches(_.uri.path.startsWith(List("intg", "counter"))) + .whenRequestMatches(_.uri.path.startsWith(List("test", "counter"))) .thenRespond("""["REF1","REF2"]""") val referenceGeneratorService = new ReferenceGeneratorService(ConfigFactory.load, SimpleHttpClient(sttpBackendStub)) val getReferences = referenceGeneratorService.getReferences(2) @@ -26,7 +26,7 @@ class ReferenceGeneratorServiceSpec extends AnyFlatSpec with Matchers { "getReferences" should "return the correct number of reference if 'referenceLimit' is exceeded in the request" in { val sttpBackendStub = SttpBackendStub.synchronous - .whenRequestMatches(_.uri.path.startsWith(List("intg", "counter"))) + .whenRequestMatches(_.uri.path.startsWith(List("test", "counter"))) .thenRespondCyclic("""["REF1","REF2"]""", """["REF3"]""") val referenceGeneratorService = new ReferenceGeneratorService(ConfigFactory.load, SimpleHttpClient(sttpBackendStub)) val getReferences = referenceGeneratorService.getReferences(3) diff --git a/src/test/scala/uk/gov/nationalarchives/tdr/api/utils/TreeNodeUtilsSpec.scala b/src/test/scala/uk/gov/nationalarchives/tdr/api/utils/TreeNodeUtilsSpec.scala index 4e6a89037..b3dedb8fe 100644 --- a/src/test/scala/uk/gov/nationalarchives/tdr/api/utils/TreeNodeUtilsSpec.scala +++ b/src/test/scala/uk/gov/nationalarchives/tdr/api/utils/TreeNodeUtilsSpec.scala @@ -18,7 +18,7 @@ class TreeNodeUtilsSpec extends AnyFlatSpec with MockitoSugar with Matchers with val result = TreeNodesUtils( new FixedUUIDSource, referenceGeneratorServiceMock, - ConfigFactory.load().withValue("featureAccessBlock.assignFileReferences", ConfigValueFactory.fromAnyRef("false")) + ConfigFactory.load() ).generateNodes(Set("/a/path/with/some/nested/folders/file"), fileTypeIdentifier) val expectedSize = 7 result.size should equal(expectedSize) @@ -65,7 +65,7 @@ class TreeNodeUtilsSpec extends AnyFlatSpec with MockitoSugar with Matchers with TreeNodesUtils( new FixedUUIDSource, referenceGeneratorServiceMock, - ConfigFactory.load().withValue("featureAccessBlock.assignFileReferences", ConfigValueFactory.fromAnyRef("false")) + ConfigFactory.load() ).generateNodes(Set(fileName), fileTypeIdentifier) } assert(result.getMessage === "some exception")