unit-testingdropwizardamazon-sqslombokelastic-mq

why ElastiMq keeps requiring server and check parameters since I am passing it


I am using AWS SQS for the first time. I work in certain company which blocks us via firewall to connect from our local network to SQS for some reasons beyond my control. I can upload anything I have developed to our TomCat in AWS or even run via our Linux command line with our ec2-user (upload via WinSCP and trigger via Putty).

Since I need to provide certain solution using sqs but I can't debug accessing it from my Eclipse I am interested to mock sqs. Additionally, I see advantages on such approach from test perspective. That said, after few hours searching I found exactly what I need: http://www.schibsted.pl/blog/mocking-amazon-sqs-with-elasticmq/

I downloaded the example (https://github.com/JanGurda/elastc-mq-rule-sample) but I can't start it. I understand that there is an elasticmq embbeded which should be started together with such jar. I tried other way by downloading the elasticmq and started it before running the sample but I am still getting the same output.

Basically, I am getting this output

usage: java -jar project.jar [-h] [-v] {server,check} ...

positional arguments:
  {server,check}         available commands

optional arguments:
  -h, --help             show this help message and exit
  -v, --version          show the application version and exit

It seems I am doing something very silly but I didn't find a north. PS. I checked and the pom has lombok and elasticmq properly settup.

pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>pl.schibsted.spid</groupId>
    <artifactId>elastc-mq-rule-sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-core</artifactId>
            <version>0.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-sqs</artifactId>
            <version>1.10.1</version>
        </dependency>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-testing</artifactId>
            <version>0.8.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.elasticmq</groupId>
            <artifactId>elasticmq-rest-sqs_2.11</artifactId>
            <version>0.8.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>pl.schibsted.spid.elasticmq.server.ElasticMqRuleSampleApplication</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <includes>
                        <include>**/ITest*.java</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-tests</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

DropwizardAppRule:

public class ITestPingResource {

    @ClassRule
    public static DropwizardAppRule<ElasticMqRuleSampleApplicationConfiguration> app =
            new DropwizardAppRule<>(ElasticMqRuleSampleApplication.class,
                    ITestPingResource.class.getClassLoader().getResource("test.yml").getPath());

    @ClassRule
    public static SqsRule sqs = new SqsRule(SqsRuleConfiguration.builder()
            .queue("sample-queue").port(8888).build());

    private Client client = ClientBuilder.newClient();

    @After
    public void tearDown() {
        sqs.purgeAllQueues();
    }

    @Test
    public void shouldPublishProcessedRequestPayload() throws Exception {
        // given
        String toSend = "abcdefgh";
        // when
        Response response = client
                .target("http://127.0.0.1:" + app.getLocalPort() + "/ping")
                .request().post(Entity.json(toSend));
        // then
        assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
        List<Message> messagesFromQueue = sqs.getQueue("sample-queue").read(10);
        assertEquals(1, messagesFromQueue.size());
        assertEquals("ABCDEFGH", messagesFromQueue.get(0).getBody());
    }
}

test.yml

queueUrl: http://localhost:8888/queue/sample-queue
awsAccessKey: x
awsSecretKey: x

main:

public class ElasticMqRuleSampleApplication extends Application<ElasticMqRuleSampleApplicationConfiguration> {

    public static void main(String[] args) throws Exception {
        new ElasticMqRuleSampleApplication().run(args);
    }

    @Override
    public void run(ElasticMqRuleSampleApplicationConfiguration configuration, Environment environment) throws Exception {
        PingResource resource = new PingResource(configuration);
        environment.jersey().register(resource);
    }

}

Solution

  • Demetrio,

    The error you get is simply standard output of Dropwizard. You should use “server” parameter to start Dropwizard application. So command you use to start Dropwizard is java -jar <> server.

    If you however would like to run sample integration test (which I discussed in my article) just use Maven. Type mvn clean install. It’ll build project and run integration test.

    Thanks