Skip to content

Commit

Permalink
Merge pull request #34 from nationalarchives/TDRD-88-update-metadata-…
Browse files Browse the repository at this point in the history
…validator

TDRD 88 - Enable draft metadata validator to be triggered from a step function
  • Loading branch information
annielh authored May 2, 2024
2 parents d4119a2 + fe347ec commit 904fc7a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
10 changes: 5 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Dependencies._

ThisBuild / scalaVersion := "2.13.10"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "uk.gov.nationalarchives"
ThisBuild / scalaVersion := "2.13.10"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "uk.gov.nationalarchives"

lazy val root = (project in file("."))
.settings(
Expand All @@ -27,8 +27,8 @@ lazy val root = (project in file("."))
)

(assembly / assemblyMergeStrategy) := {
case PathList("META-INF", xs@_*) => MergeStrategy.discard
case _ => MergeStrategy.first
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
case _ => MergeStrategy.first
}

(Test / fork) := true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.gov.nationalarchives.draftmetadatavalidator

import cats.effect.IO
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.events.{APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent}
import com.amazonaws.services.lambda.runtime.{Context, RequestHandler}
import com.amazonaws.services.lambda.runtime.events.{APIGatewayProxyResponseEvent}
import graphql.codegen.GetCustomMetadata.customMetadata.CustomMetadata
import graphql.codegen.GetCustomMetadata.{customMetadata => cm}
import graphql.codegen.GetDisplayProperties.displayProperties.DisplayProperties
Expand Down Expand Up @@ -30,10 +30,11 @@ import java.net.URI
import java.sql.Timestamp
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util
import java.util.UUID
import scala.concurrent.ExecutionContext.Implicits.global

class Lambda {
class Lambda extends RequestHandler[java.util.Map[String, Object], APIGatewayProxyResponseEvent] {

implicit val backend: SttpBackend[Identity, Any] = HttpURLConnectionBackend()
implicit val keycloakDeployment: TdrKeycloakDeployment = TdrKeycloakDeployment(authUrl, "tdr", timeToLiveSecs)
Expand All @@ -46,12 +47,11 @@ class Lambda {
private val addOrUpdateBulkFileMetadataClient = new GraphQLClient[afm.Data, afm.Variables](apiUrl)
private val graphQlApi: GraphQlApi = GraphQlApi(keycloakUtils, customMetadataClient, updateConsignmentStatusClient, addOrUpdateBulkFileMetadataClient, displayPropertiesClient)

def handleRequest(event: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent = {
val pathParam = event.getPathParameters

def handleRequest(input: java.util.Map[String, Object], context: Context): APIGatewayProxyResponseEvent = {
val consignmentId = extractConsignmentId(input)
val s3Files = S3Files(S3Utils(s3Async(s3Endpoint)))
for {
draftMetadata <- IO(DraftMetadata(UUID.fromString(pathParam.get("consignmentId"))))
draftMetadata <- IO(DraftMetadata(UUID.fromString(consignmentId)))
_ <- s3Files.downloadFile(bucket, draftMetadata)
hasErrors <- validateMetadata(draftMetadata)
_ <- if (hasErrors) s3Files.uploadFile(bucket, draftMetadata) else IO.unit
Expand All @@ -62,6 +62,15 @@ class Lambda {
}
}.unsafeRunSync()(cats.effect.unsafe.implicits.global)

private def extractConsignmentId(input: util.Map[String, Object]): String = {
val inputParameters = input match {
case stepFunctionInput if stepFunctionInput.containsKey("consignmentId") => stepFunctionInput
case apiProxyRequestInput if apiProxyRequestInput.containsKey("pathParameters") =>
apiProxyRequestInput.get("pathParameters").asInstanceOf[util.Map[String, Object]]
}
inputParameters.get("consignmentId").toString
}

private def validateMetadata(draftMetadata: DraftMetadata): IO[Boolean] = {
val clientSecret = getClientSecret(clientSecretPath, endpoint)
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
import scala.jdk.CollectionConverters.MapHasAsJava

object LambdaRunner extends App {
val pathParams = Map("consignmentId" -> "f82af3bf-b742-454c-9771-bfd6c5eae749").asJava
val event = new APIGatewayProxyRequestEvent()
event.setPathParameters(pathParams)
new Lambda().handleRequest(event, null)
val input = Map("consignmentId" -> "f82af3bf-b742-454c-9771-bfd6c5eae749".asInstanceOf[Object]).asJava
new Lambda().handleRequest(input, null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.jdk.CollectionConverters.MapHasAsJava

class LambdaSpec extends ExternalServicesSpec {

val consignmentId = "f82af3bf-b742-454c-9771-bfd6c5eae749"
val consignmentId: Object = "f82af3bf-b742-454c-9771-bfd6c5eae749"
val mockContext: Context = mock[Context]

def mockS3GetResponse(fileName: String): StubMapping = {
Expand All @@ -31,21 +31,12 @@ class LambdaSpec extends ExternalServicesSpec {
)
}

def createEvent: APIGatewayProxyRequestEvent = {
val pathParams = Map("consignmentId" -> consignmentId).asJava
val event = new APIGatewayProxyRequestEvent()
event.setPathParameters(pathParams)
event
}

"handleRequest" should "download the draft metadata csv file, validate and save to db if it has no errors" in {
authOkJson()
graphqlOkJson(true)
mockS3GetResponse("sample.csv")
val pathParams = Map("consignmentId" -> consignmentId).asJava
val event = new APIGatewayProxyRequestEvent()
event.setPathParameters(pathParams)
val response = new Lambda().handleRequest(createEvent, mockContext)
val input = Map("consignmentId" -> consignmentId).asJava
val response = new Lambda().handleRequest(input, mockContext)
response.getStatusCode should equal(200)
}

Expand All @@ -54,10 +45,8 @@ class LambdaSpec extends ExternalServicesSpec {
graphqlOkJson()
mockS3GetResponse("invalid-sample.csv")
mockS3PutResponse()
val pathParams = Map("consignmentId" -> consignmentId).asJava
val event = new APIGatewayProxyRequestEvent()
event.setPathParameters(pathParams)
val response = new Lambda().handleRequest(createEvent, mockContext)
val input = Map("consignmentId" -> consignmentId).asJava
val response = new Lambda().handleRequest(input, mockContext)
response.getStatusCode should equal(200)
}
}

0 comments on commit 904fc7a

Please sign in to comment.