Skip to content

Commit

Permalink
Merge branch 'main' into update/sbt-assembly-2.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
vimleshtna authored Sep 20, 2023
2 parents bf1b07d + dc2218a commit 2e62164
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ dynamodb {
referenceCounter = ${?REFERENCE_COUNTER}
queryParam = "numberofrefs"
queryParam = ${?QUERY_PARAM}
referenceLimit = 5000
referenceLimit = ${?REFERENCE_LIMIT}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ import scala.util.{Failure, Success, Try}
class Lambda extends RequestHandler[APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent] {

override def handleRequest(event: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent = {
val limit = config.getString("dynamodb.referenceLimit").toInt
val queryParams = event.getQueryStringParameters
val queryParam: String = config.getString("dynamodb.queryParam")
val convertQueryToInt: Try[Int] = Try(queryParams.get(queryParam).toInt)
convertQueryToInt match {
val numberOfRefsQuery: Try[Int] = Try {
val numberOfRefs = queryParams.get(queryParam).toInt
if (numberOfRefs > limit) throw new IllegalArgumentException(s"$queryParam is greater than $limit") else numberOfRefs
}
numberOfRefsQuery match {
case Success(numberOfReferences) =>
process(Input(numberOfReferences))
case Failure(exception) =>
val response = new APIGatewayProxyResponseEvent()
logger.error(exception.getMessage)
logger.error(exception.toString)
response.setStatusCode(500)
response.setBody(exception.getMessage)
response
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dynamodb {
keyVal = "fileCounter"
referenceCounter = "referenceCounter"
queryParam = "numberofrefs"
referenceLimit = 5000
}
47 changes: 46 additions & 1 deletion src/test/scala/uk/gov/nationalarchives/LambdaTest.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package uk.gov.nationalarchives

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
import com.amazonaws.services.lambda.runtime.events.{APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent}
import com.amazonaws.services.lambda.runtime.{ClientContext, CognitoIdentity, Context, LambdaLogger}
import com.dimafeng.testcontainers.DynaliteContainer
import com.typesafe.config.{Config, ConfigFactory, ConfigValueFactory}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import uk.gov.nationalarchives.referencegenerator.Lambda
import uk.gov.nationalarchives.utils.TestContainerUtils

import scala.jdk.CollectionConverters.MapHasAsJava

class LambdaTest extends AnyFlatSpec with Matchers with TestContainerUtils {
override def config: Config = ConfigFactory.load()

override def afterContainersStart(containers: containerDef.Container): Unit = super.afterContainersStart(containers)

val mockContext: Context = new Context {
override def getAwsRequestId: String = "testRequestId"
override def getLogGroupName: String = "testLogGroupName"
override def getLogStreamName: String = "testLogStreamName"
override def getFunctionName: String = "testFunctionName"
override def getFunctionVersion: String = "testFunctionVersion"
override def getInvokedFunctionArn: String = "testInvokedFunctionArn"
override def getIdentity: CognitoIdentity = ???
override def getClientContext: ClientContext = ???
override def getRemainingTimeInMillis: Int = ???
override def getMemoryLimitInMB: Int = ???
override def getLogger: LambdaLogger = ???
}

"The Lambda class" should "return an APIGateWayResponseEvent with the correct number of references" in withContainers { case container: DynaliteContainer =>
val client = createDynamoDbClient(container)
val input = Lambda.Input(numberOfReferences = 3)
Expand Down Expand Up @@ -40,4 +57,32 @@ class LambdaTest extends AnyFlatSpec with Matchers with TestContainerUtils {
actual.getStatusCode shouldBe expected.getStatusCode
actual.getBody should include(expected.getBody)
}

"The Lambda class" should "return an APIGateWayResponseEvent with body containing exception message if numberofrefs isn't an integer" in {
val lambda = new Lambda()
val queryParam = config.getString("dynamodb.queryParam")
val queryParams = Map(queryParam -> "abc").asJava
val event = new APIGatewayProxyRequestEvent()
event.setQueryStringParameters(queryParams)

val result = lambda.handleRequest(event, mockContext)
val expected: APIGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent()
.withStatusCode(500)
.withBody(s"""For input string: "abc"""")
result shouldBe expected
}

"The Lambda class" should "return an APIGateWayResponseEvent with body containing exception message if numberofrefs exceeds the limit" in {
val lambda = new Lambda()
val queryParam = config.getString("dynamodb.queryParam")
val queryParams = Map(queryParam -> "5001").asJava
val event = new APIGatewayProxyRequestEvent()
event.setQueryStringParameters(queryParams)

val result = lambda.handleRequest(event, mockContext)
val expected: APIGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent()
.withStatusCode(500)
.withBody(s"""$queryParam is greater than 5000""")
result shouldBe expected
}
}
2 changes: 1 addition & 1 deletion terraform/da-terraform-configurations
1 change: 1 addition & 0 deletions terraform/root_main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module "reference_generator_lambda" {
REFERENCE_KEY_VALUE = "fileCounter"
REFERENCE_COUNTER = "referenceCounter"
QUERY_PARAM = "numberofrefs"
REFERENCE_LIMIT = 5000
}
runtime = "java11"
timeout_seconds = 60
Expand Down

0 comments on commit 2e62164

Please sign in to comment.