-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalise lambda functionality into module (#2795)
* split event from flow * push decoding message up a level * define strict config layer (keep typesafe at the edge) * Apply auto-formatting rules * update tests * add config test * add an abstraction for lamdba apps * add tests for lambdaapp * add common lib * use lambda lib in relation_embedder * shift tests to lambda lib * use lambda lib in batcher * fix tests, move sqseventops test * add lambda module to ci tests * self-linting * ensure lib folders are added * fix gitignore config * Apply auto-formatting rules * fix tests * rejig lambda app to account for type erasure bug :( * Apply auto-formatting rules * use should matchers to be consistent * update after trying to run locally * Apply auto-formatting rules * remaining merge compile issues --------- Co-authored-by: Github on behalf of Wellcome Collection <[email protected]>
- Loading branch information
Showing
38 changed files
with
479 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 10 additions & 2 deletions
12
...ne/relation_embedder/lib/Downstream.scala → ...c/main/scala/weco/lambda/Downstream.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...elation_embedder/lib/ElasticBuilder.scala → ...in/scala/weco/lambda/ElasticBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...on_embedder/lib/LambdaConfiguration.scala → ...ala/weco/lambda/LambdaConfiguration.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
configString=knownConfigValue |
4 changes: 2 additions & 2 deletions
4
...tion_embedder/lib/ConfigurationTest.scala → ...scala/weco/lambda/ConfigurationTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
common/lambda/src/test/scala/weco/lambda/SQSEventOpsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package weco.lambda | ||
|
||
import com.amazonaws.services.lambda.runtime.events.SQSEvent | ||
import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage | ||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.collection.JavaConverters._ | ||
import weco.json.JsonUtil._ | ||
|
||
class SQSEventOpsTest extends AnyFunSpec with Matchers { | ||
|
||
import SQSEventOps._ | ||
|
||
describe("Using the implicit class SQSEventOps") { | ||
it("extracts values from an SQSEvent where the message is a String") { | ||
val fakeMessage = new SQSMessage() | ||
fakeMessage.setBody("{\"Message\":\"A/C\"}") | ||
val fakeSQSEvent = new SQSEvent() | ||
fakeSQSEvent.setRecords(List(fakeMessage).asJava) | ||
|
||
val paths = fakeSQSEvent.extract[String]() | ||
|
||
paths shouldBe List("A/C") | ||
} | ||
|
||
case class TestMessage(value: String) | ||
|
||
it("extracts values from an SQSEvent where the message is a JSON object") { | ||
val fakeMessage = new SQSMessage() | ||
fakeMessage.setBody("{\"Message\":\"{\\\"value\\\": \\\"A/C\\\"}\"}") | ||
val fakeSQSEvent = new SQSEvent() | ||
fakeSQSEvent.setRecords(List(fakeMessage).asJava) | ||
|
||
val paths = fakeSQSEvent.extract[TestMessage]() | ||
|
||
paths shouldBe List(TestMessage("A/C")) | ||
} | ||
|
||
it("extracts multiple values from an SQSEvent") { | ||
val fakeMessage1 = new SQSMessage() | ||
fakeMessage1.setBody("{\"Message\":\"A/C\"}") | ||
val fakeMessage2 = new SQSMessage() | ||
fakeMessage2.setBody("{\"Message\":\"A/E\"}") | ||
val fakeSQSEvent = new SQSEvent() | ||
fakeSQSEvent.setRecords(List(fakeMessage1, fakeMessage2).asJava) | ||
|
||
val paths = fakeSQSEvent.extract[String]() | ||
|
||
paths shouldBe List("A/C", "A/E") | ||
} | ||
|
||
it( | ||
"extracts values from an SQSEvent where the message is a JSON object with multiple fields, only taking the ones we want" | ||
) { | ||
val fakeMessage = new SQSMessage() | ||
fakeMessage.setBody( | ||
"{\"Message\":\"{\\\"value\\\": \\\"A/C\\\", \\\"other\\\": \\\"D/E\\\"}\"}" | ||
) | ||
val fakeSQSEvent = new SQSEvent() | ||
fakeSQSEvent.setRecords(List(fakeMessage).asJava) | ||
|
||
val paths = fakeSQSEvent.extract[TestMessage]() | ||
|
||
paths shouldBe List(TestMessage("A/C")) | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
common/lambda/src/test/scala/weco/lambda/SQSLambdaAppTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package weco.lambda | ||
|
||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.must.Matchers | ||
import weco.fixtures.RandomGenerators | ||
import weco.lambda.helpers.{ConfigurationTestHelpers, SQSLambdaAppHelpers} | ||
|
||
class SQSLambdaAppTest | ||
extends AnyFunSpec | ||
with ConfigurationTestHelpers | ||
with SQSLambdaAppHelpers | ||
with RandomGenerators | ||
with Matchers { | ||
|
||
it( | ||
"creates a lambda app with a config, and allows execution of a processEvent function" | ||
) { | ||
val lambdaApp = new TestLambdaApp() | ||
val eventString = randomAlphanumeric() | ||
|
||
lambdaApp.handleRequest(createSqsEvent(List(eventString)), | ||
null | ||
) mustBe eventString + expectedConfigString | ||
} | ||
|
||
it( | ||
"creates a lambda app with a config, and allows execution of a processEvent function, handling multiple events" | ||
) { | ||
val lambdaApp = new TestLambdaApp() | ||
val eventString1 = randomAlphanumeric() | ||
val eventString2 = randomAlphanumeric() | ||
|
||
lambdaApp.handleRequest(createSqsEvent(List(eventString1, eventString2)), | ||
null | ||
) mustBe eventString1 + eventString2 + expectedConfigString | ||
} | ||
|
||
it("fails if the processEvent function fails") { | ||
val lambdaApp = new FailingTestLambdaApp() | ||
val eventString = randomAlphanumeric() | ||
|
||
a[Throwable] shouldBe thrownBy { | ||
lambdaApp.handleRequest(createSqsEvent(List(eventString)), null) | ||
} | ||
} | ||
|
||
it("fails if the processEvent function takes too long") { | ||
val lambdaApp = new SleepingTestLambdaApp() | ||
val eventString = randomAlphanumeric() | ||
|
||
a[Throwable] shouldBe thrownBy { | ||
lambdaApp.handleRequest(createSqsEvent(List(eventString)), null) | ||
} | ||
} | ||
} |
Oops, something went wrong.