Skip to content

Commit

Permalink
Merge pull request #6 from nationalarchives/add-api-gateway-proxy-res…
Browse files Browse the repository at this point in the history
…ponse

Return APIGatewayProxyResponseEvent with success response
  • Loading branch information
vimleshtna authored Mar 26, 2024
2 parents a9657b2 + 65d5cc1 commit a4b45a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package uk.gov.nationalarchives.draftmetadatavalidator

import cats.effect.IO
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
import com.amazonaws.services.lambda.runtime.events.{APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent}
import graphql.codegen.GetCustomMetadata.customMetadata.CustomMetadata
import graphql.codegen.GetCustomMetadata.{customMetadata => cm}
import graphql.codegen.GetDisplayProperties.displayProperties.DisplayProperties
Expand Down Expand Up @@ -38,7 +38,7 @@ class Lambda {
private val updateConsignmentStatusClient = new GraphQLClient[ucs.Data, ucs.Variables](apiUrl)
private val graphQlApi: GraphQlApi = GraphQlApi(keycloakUtils, customMetadataClient, updateConsignmentStatusClient, displayPropertiesClient)

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

val s3Files = S3Files(S3Utils(s3Async(s3Endpoint)))
Expand All @@ -48,7 +48,11 @@ class Lambda {
_ <- s3Files.downloadFile(bucket, draftMetadata)
hasErrors <- validateMetadata(draftMetadata)
_ <- if (hasErrors) s3Files.uploadFile(bucket, draftMetadata) else IO.unit
} yield ()
} yield {
val response = new APIGatewayProxyResponseEvent()
response.setStatusCode(200)
response
}
}.unsafeRunSync()(cats.effect.unsafe.implicits.global)

private def validateMetadata(draftMetadata: DraftMetadata): IO[Boolean] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
import com.github.tomakehurst.wiremock.client.WireMock.{aResponse, get, put, urlEqualTo}
import com.github.tomakehurst.wiremock.stubbing.StubMapping
import org.mockito.MockitoSugar.mock
import org.scalatest.matchers.should.Matchers.{convertToAnyShouldWrapper, equal}

import java.nio.file.{Files, Paths}
import scala.jdk.CollectionConverters.MapHasAsJava
Expand Down Expand Up @@ -44,7 +45,8 @@ class LambdaSpec extends ExternalServicesSpec {
val pathParams = Map("consignmentId" -> consignmentId).asJava
val event = new APIGatewayProxyRequestEvent()
event.setPathParameters(pathParams)
new Lambda().handleRequest(createEvent, mockContext)
val response = new Lambda().handleRequest(createEvent, mockContext)
response.getStatusCode should equal(200)
}

"handleRequest" should "download the draft metadata csv file, validate it and re-upload to s3 bucket if it has any errors" in {
Expand All @@ -55,6 +57,7 @@ class LambdaSpec extends ExternalServicesSpec {
val pathParams = Map("consignmentId" -> consignmentId).asJava
val event = new APIGatewayProxyRequestEvent()
event.setPathParameters(pathParams)
new Lambda().handleRequest(createEvent, mockContext)
val response = new Lambda().handleRequest(createEvent, mockContext)
response.getStatusCode should equal(200)
}
}

0 comments on commit a4b45a1

Please sign in to comment.