How to: Emulate Amazon Simple Queue Service (SQS) for Testing Purposes

A guide to creating a dynamic wiremock for SQS

Miro Barsocchi
Jul 13, 2018 · 3 min read

Amazon Simple Queue Service (SQS) is a fully managed message queuing service from Amazon.

If you want to integrate it, you may also want to test if it works. SQS has probably a sandbox to test it but if you don’t want to get stuck searching for it, or you don’t want to get crazy with login procedures, you can use a mock service that acts as an SQS queue* .

Why do we need this? Why can’t we use a simple Wiremock that always responds “200, OK”?

You can’t do it because the response of SQS is a dynamic response dependent from the request, and it’s based on some MD5 calculation.

MD5 calculation

What we have done is an extension of Wiremock that always responds something like “200, OK”, with all the fields correctly calculated for SQS. It was both easy and not-so-easy, because every easy task it’s never a “It’s just …“.

In this case, it was more of a “it’s just not enough to…” — just spinning a docker container up or adding a class to the classpath would simply not do.

How To, Step by step guide

First of all we need to define a class that extends theResponseDefinitionTransformer class with two easy to implement methods:

applyGobally that returns true, means that “all the requests will receive this response”.

getName it’s a method that Wiremock needs. Then you must implement the real method that responds correctly, the method is transform

calculateResponse does the dirty job, and it’s a dirty method (I know it could be refactored…)

This method gets the body of the request, splits it because it’s an url-like body, and then calculates the MD5 of all the attributes, types and values according to SQS documentation.

updateLengthAndBytes it’s a method to update the MD5 calculation.

To be honest, we also created an additional class SimplifiedMessageAttribute:

After this step, you can build an XML document (the implementation of this is not in this post because it‘s cumbersome) and include the calculated fields (md5OfMessageBody and md5OfMessageAttributes). For other fields, requestId and messageId, we used hard coded values. The final result should be something like this:

Response Example for SQS

After this, you can build your project and obtain a jar.

How can we integrate this into Wiremock?

That’s not so easy. What I’ve found is that you have to move your jar in the same path of the Wiremock jar and start the Wiremock with:

Wiremock has no plugin mechanisms, so what you are doing with this command is:

with -cp “*” you are telling Java “take all the jars in this folder”

… your main class is here: com.github.tomakehurst.wiremock.standalone.WireMockServerRunner

… and use this extension: your.package.name.SQSMock

From this point onwards, you can use it directly, build a docker container or whatever you want.

IT is not an easy task and, from my point of view, every layer added to simplify things is a new topic to learn and understand.

During this journey we have learnt about SQS, wiremock, mvn, unit test and obviously Java. I want to thank:

  • Giuseppe Gaeta (Tech Lead)
  • Matteo Moci (Software Dev Engineer II)
  • Camillo Quatrini (QA Evangelist)
  • Tiziana Battiston (Travel Agencies Manager, proofreader)

Ref:

* There are some restrictions: such service doesn’t work for not-string attributes.

Thanks for reading

Miro Barsocchi: Software tester and also Electronic engineer, radio speaker, actor, surfer, barman, but only two of these are seriously. You can find me on Twitter or Github or elsewhere.

Expedia Group Technology

Stories from the Expedia Group Technology teams

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store