-
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.
Add tests for extracting paths from message in lambda batcher (#2757)
* add sqs event ops for batcher lambda * Apply auto-formatting rules * oops missing file * Apply auto-formatting rules --------- Co-authored-by: Github on behalf of Wellcome Collection <wellcomedigitalplatform@wellcome.ac.uk>
- Loading branch information
Showing
3 changed files
with
53 additions
and
19 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
26 changes: 26 additions & 0 deletions
26
...line/relation_embedder/batcher/src/main/scala/weco/pipeline/batcher/lib/SQSEventOps.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,26 @@ | ||
package weco.pipeline.batcher.lib | ||
|
||
import com.amazonaws.services.lambda.runtime.events.SQSEvent | ||
import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage | ||
import scala.collection.JavaConverters._ | ||
|
||
object SQSEventOps { | ||
|
||
/** Messages consumed by Lambda from SQS are taken from a queue populated by | ||
* an SNS topic. The actual message we are interested in is a String | ||
* containing the path. However, the matryoshka-like nature of these things | ||
* means the lambda receives | ||
* - an event containing | ||
* - a `Records` list, each Record containing | ||
* - an SQS Message with a JSON body containing | ||
* - an SNS notification containing | ||
* - a `Message`, which is the actual content we want | ||
*/ | ||
implicit class ExtractPathFromSqsEvent(event: SQSEvent) { | ||
def extractPaths: List[String] = | ||
event.getRecords.asScala.toList.flatMap(extractPathFromMessage) | ||
|
||
private def extractPathFromMessage(message: SQSMessage): Option[String] = | ||
ujson.read(message.getBody).obj.get("Message").flatMap(_.strOpt) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...line/relation_embedder/batcher/src/test/scala/weco/pipeline/batcher/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,24 @@ | ||
package weco.pipeline.batcher | ||
|
||
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._ | ||
|
||
class SQSEventOpsTest extends AnyFunSpec with Matchers { | ||
import lib.SQSEventOps._ | ||
|
||
describe("Using the implicit class SQSEventOps") { | ||
it("extracts paths from an SQSEvent") { | ||
val fakeMessage = new SQSMessage() | ||
fakeMessage.setBody("{\"Message\":\"A/C\"}") | ||
val fakeSQSEvent = new SQSEvent() | ||
fakeSQSEvent.setRecords(List(fakeMessage).asJava) | ||
|
||
val paths = fakeSQSEvent.extractPaths | ||
|
||
paths shouldBe List("A/C") | ||
} | ||
} | ||
} |