Skip to content

Commit

Permalink
Use 'source system' to fetch correct schema
Browse files Browse the repository at this point in the history
  • Loading branch information
TomJKing committed Aug 19, 2024
1 parent 4ed4152 commit a6e6f64
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LoadController(dataLoadInitiation: DataLoadInitiation, dataLoadProcessor:
.out(jsonBody[String])

val initiateLoadRoute: HttpRoutes[IO] =
Http4sServerInterpreter[IO]().toRoutes(initiateLoadEndpoint.serverLogicSuccess(ac => _ => dataLoadInitiation.initiateConsignmentLoad(ac.token)))
Http4sServerInterpreter[IO]().toRoutes(initiateLoadEndpoint.serverLogicSuccess(ac => input => dataLoadInitiation.initiateConsignmentLoad(ac.token, input)))

val completeLoadRoute: HttpRoutes[IO] =
Http4sServerInterpreter[IO]().toRoutes(completeLoadEndpoint.serverLogicSuccess(ac => ci => dataLoadProcessor.trigger(ci._2, ac.token)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ import cats.effect.IO
import uk.gov.nationalarchives.tdr.keycloak.Token
import uk.gov.nationalarchives.tdr.transfer.service.ApplicationConfig
import uk.gov.nationalarchives.tdr.transfer.service.api.model.LoadModel.{AWSS3LoadDestination, LoadDetails, TransferConfiguration}
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum.SourceSystem
import uk.gov.nationalarchives.tdr.transfer.service.services.GraphQlApiService
import uk.gov.nationalarchives.tdr.transfer.service.services.dataload.DataLoadInitiation.s3Config

import java.util.UUID

class DataLoadInitiation(graphQlApiService: GraphQlApiService) {
def initiateConsignmentLoad(token: Token): IO[LoadDetails] = {
def initiateConsignmentLoad(token: Token, sourceSystem: SourceSystem): IO[LoadDetails] = {
for {
addConsignmentResult <- graphQlApiService.addConsignment(token)
consignmentId = addConsignmentResult.consignmentid.get
_ <- graphQlApiService.startUpload(token, consignmentId)
result <- loadDetails(consignmentId, token.userId)
result <- loadDetails(consignmentId, token.userId, sourceSystem)
} yield result
}

private def loadDetails(transferId: UUID, userId: UUID): IO[LoadDetails] = {
private def loadDetails(transferId: UUID, userId: UUID, sourceSystem: SourceSystem): IO[LoadDetails] = {
val recordsS3Bucket = AWSS3LoadDestination(s"${s3Config.recordsUploadBucket}", s"$userId/$transferId")
val metadataS3Bucket = AWSS3LoadDestination(s"${s3Config.metadataUploadBucket}", s"$transferId/dataload")
val metadataProperties = MetadataLoadConfiguration.metadataLoadConfiguration()
val metadataProperties = MetadataLoadConfiguration.metadataLoadConfiguration(sourceSystem)
val transferConfiguration = TransferConfiguration(metadataProperties)
IO(LoadDetails(transferId, recordsLoadDestination = recordsS3Bucket, metadataLoadDestination = metadataS3Bucket, transferConfiguration))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ package uk.gov.nationalarchives.tdr.transfer.service.services.dataload

import uk.gov.nationalarchives.tdr.transfer.service.ApplicationConfig
import uk.gov.nationalarchives.tdr.transfer.service.api.model.LoadModel.MetadataPropertyDetails
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum.SourceSystem
import uk.gov.nationalarchives.tdr.transfer.service.services.schema.SchemaHandler

import scala.jdk.CollectionConverters._

object MetadataLoadConfiguration {
private val schemaConfig: ApplicationConfig.Schema = ApplicationConfig.appConfig.schema

def metadataLoadConfiguration(): Set[MetadataPropertyDetails] = {
val schema = SchemaHandler.schema(schemaConfig.dataLoadSharePointLocation)
private def sourceSystemSchemaMapping(sourceSystem: SourceSystem): String = sourceSystem match {
case SourceSystemEnum.SharePoint => schemaConfig.dataLoadSharePointLocation
case _ => throw new RuntimeException(s"Source System '$sourceSystem' not mapped to schema")
}

def metadataLoadConfiguration(sourceSystem: SourceSystem): Set[MetadataPropertyDetails] = {
val schemaLocation = sourceSystemSchemaMapping(sourceSystem)
val schema = SchemaHandler.schema(schemaLocation)
val properties = schema.get("properties").properties().asScala
val requiredProperties = schema.get("required").asScala.map(_.asText()).toSet
properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import graphql.codegen.AddConsignment.addConsignment.AddConsignment
import uk.gov.nationalarchives.tdr.keycloak.Token
import uk.gov.nationalarchives.tdr.transfer.service.BaseSpec
import uk.gov.nationalarchives.tdr.transfer.service.api.model.LoadModel.{AWSS3LoadDestination, LoadDetails, TransferConfiguration}
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum
import uk.gov.nationalarchives.tdr.transfer.service.services.GraphQlApiService

import java.util.UUID
Expand Down Expand Up @@ -35,7 +36,7 @@ class DataLoadInitiationSpec extends BaseSpec {
)

val service = new DataLoadInitiation(mockGraphQlApiService)
val result = service.initiateConsignmentLoad(mockToken).unsafeRunSync()
val result = service.initiateConsignmentLoad(mockToken, SourceSystemEnum.SharePoint).unsafeRunSync()
result shouldBe expectedResult
verify(mockGraphQlApiService, times(1)).addConsignment(mockToken)
verify(mockGraphQlApiService, times(1)).startUpload(mockToken, consignmentId, None)
Expand All @@ -48,7 +49,7 @@ class DataLoadInitiationSpec extends BaseSpec {
val service = new DataLoadInitiation(mockGraphQlApiService)

val exception = intercept[RuntimeException] {
service.initiateConsignmentLoad(mockToken).attempt.unsafeRunSync()
service.initiateConsignmentLoad(mockToken, SourceSystemEnum.SharePoint).attempt.unsafeRunSync()
}
exception.getMessage shouldBe "Error adding consignment"
verify(mockGraphQlApiService, times(1)).addConsignment(mockToken)
Expand All @@ -63,7 +64,7 @@ class DataLoadInitiationSpec extends BaseSpec {
when(mockGraphQlApiService.startUpload(mockToken, consignmentId)).thenThrow(new RuntimeException("Error starting upload"))

val service = new DataLoadInitiation(mockGraphQlApiService)
val response = service.initiateConsignmentLoad(mockToken).attempt.unsafeRunSync()
val response = service.initiateConsignmentLoad(mockToken, SourceSystemEnum.SharePoint).attempt.unsafeRunSync()

response.isLeft should equal(true)
response.left.value.getMessage should equal("Error starting upload")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ package uk.gov.nationalarchives.tdr.transfer.service.services.dataload

import uk.gov.nationalarchives.tdr.transfer.service.BaseSpec
import uk.gov.nationalarchives.tdr.transfer.service.api.model.LoadModel.MetadataPropertyDetails
import uk.gov.nationalarchives.tdr.transfer.service.api.model.SourceSystem.SourceSystemEnum
import uk.gov.nationalarchives.tdr.transfer.service.services.schema.SchemaHandler

class MetadataLoadConfigurationSpec extends BaseSpec {
"'metadataLoadConfiguration'" should "return the correct metadata configuration for the given source system" in {
val result = MetadataLoadConfiguration.metadataLoadConfiguration()
val result = MetadataLoadConfiguration.metadataLoadConfiguration(SourceSystemEnum.SharePoint)
result.size shouldBe 4
result.contains(MetadataPropertyDetails("FileRef", true)) shouldBe true
result.contains(MetadataPropertyDetails("File_x0020_Size", true)) shouldBe true
result.contains(MetadataPropertyDetails("SHA256ClientSideChecksum", true)) shouldBe true
result.contains(MetadataPropertyDetails("Modified", true)) shouldBe true
}

"'metadataLoadConfiguration'" should "return an error if source system not mapped to a schema" in {
val mockSourceSystem = mock[SourceSystemEnum.SourceSystem]

val exception = intercept[RuntimeException] {
MetadataLoadConfiguration.metadataLoadConfiguration(mockSourceSystem)
}

exception.getMessage shouldBe s"Source System '$mockSourceSystem' not mapped to schema"
}
}

0 comments on commit a6e6f64

Please sign in to comment.