Skip to content

Commit

Permalink
TDR-3660 remove feature block for ref generator (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhz authored Jan 17, 2024
1 parent 9ad6263 commit affe519
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 25 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application.base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ fileUpload {
}

featureAccessBlock {
assignFileReferences = ${BLOCK_ASSIGN_FILE_REFERENCES}
blockValidationLibrary = ${BLOCK_VALIDATION_LIBRARY}
}

Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ frontend = {
}

featureAccessBlock {
assignFileReferences = true
blockValidationLibrary = false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 =>
Expand All @@ -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 =>
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit affe519

Please sign in to comment.