generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
203 additions
and
1 deletion.
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
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,33 @@ | ||
plugins { | ||
id 'java-library' | ||
id 'groovy' | ||
id("io.micronaut.build.internal.mqtt-tests") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
annotationProcessor(mn.micronaut.inject.java) | ||
|
||
implementation(projects.micronautMqttv5) | ||
implementation(mn.micronaut.inject) | ||
implementation(mn.micronaut.jackson.databind) | ||
|
||
runtimeOnly(mnLogging.logback.classic) | ||
|
||
testAnnotationProcessor(mn.micronaut.inject.java) | ||
|
||
testImplementation(projects.testSuiteUtils) | ||
|
||
testImplementation(libs.awaitility) | ||
testImplementation(libs.junit.jupiter.api) | ||
testImplementation(mnTest.micronaut.test.junit5) | ||
|
||
testRuntimeOnly(libs.junit.jupiter.engine) | ||
} | ||
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} |
32 changes: 32 additions & 0 deletions
32
test-suite-jackson/src/main/java/example/micronaut/Odour.java
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,32 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package example.micronaut; | ||
|
||
import io.micronaut.core.annotation.Introspected; | ||
|
||
@Introspected | ||
public class Odour { | ||
|
||
private final String name; | ||
|
||
public Odour(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
test-suite-jackson/src/main/java/example/micronaut/SmellListener.java
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,40 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package example.micronaut; | ||
|
||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.mqtt.annotation.MqttSubscriber; | ||
import io.micronaut.mqtt.annotation.Topic; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@MqttSubscriber | ||
public class SmellListener { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(SmellListener.class); | ||
private Odour smell; | ||
|
||
@Topic("house/livingroom/smell") | ||
public void receive(Odour data) { | ||
LOG.info("smell: {}", smell); | ||
smell = data; | ||
} | ||
|
||
@NonNull | ||
public String getSmell() { | ||
return smell == null ? "" : smell.getName(); | ||
} | ||
} |
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,14 @@ | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<!-- encoders are assigned the type | ||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> | ||
<encoder> | ||
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="info"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
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,5 @@ | ||
persistence false | ||
allow_anonymous true | ||
connection_messages true | ||
log_type all | ||
listener 1883 |
37 changes: 37 additions & 0 deletions
37
test-suite-jackson/src/test/java/example/micronaut/AbstractMQTTTest.java
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,37 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import org.testcontainers.containers.BindMode; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public abstract class AbstractMQTTTest { | ||
|
||
protected static GenericContainer mqttContainer = new GenericContainer(DockerImageName.parse("eclipse-mosquitto:1.6.12")) | ||
.withExposedPorts(1883) | ||
.waitingFor(new LogMessageWaitStrategy().withRegEx("(?s).*mosquitto version 1.6.12 running.*")) | ||
.withClasspathResourceMapping("mosquitto.conf", | ||
"/mosquitto/config/mosquitto.conf", | ||
BindMode.READ_ONLY); | ||
|
||
static { | ||
mqttContainer.start(); | ||
} | ||
|
||
protected ApplicationContext startContext() { | ||
return ApplicationContext.run(getConfiguration(), "test"); | ||
} | ||
|
||
protected Map<String, Object> getConfiguration() { | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("mqtt.client.server-uri", "tcp://localhost:" + mqttContainer.getMappedPort(1883)); | ||
config.put("mqtt.client.client-id", UUID.randomUUID().toString()); | ||
config.put("spec.name", this.getClass().getSimpleName()); | ||
return config; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
test-suite-jackson/src/test/java/example/micronaut/SubscriptionTest.java
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,35 @@ | ||
package example.micronaut; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.mqtt.annotation.Topic; | ||
import io.micronaut.mqtt.annotation.v5.MqttPublisher; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class SubscriptionTest extends AbstractMQTTTest { | ||
|
||
@Test | ||
void checkSubscriptionsAreReceived() { | ||
try(ApplicationContext applicationContext = startContext()) { | ||
SmellClient client = applicationContext.getBean(SmellClient.class); | ||
SmellListener listener = applicationContext.getBean(SmellListener.class); | ||
|
||
client.publishLivingroomSmell(new Odour("cheesy")); | ||
|
||
await() | ||
.atMost(5, TimeUnit.SECONDS) | ||
.untilAsserted(() -> assertEquals("cheesy", listener.getSmell())); | ||
} | ||
} | ||
|
||
@MqttPublisher | ||
interface SmellClient { | ||
|
||
@Topic("house/livingroom/smell") | ||
void publishLivingroomSmell(Odour data); | ||
} | ||
} |
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