ryan-highley commented on code in PR #15088: URL: https://github.com/apache/camel/pull/15088#discussion_r1714312987
########## components/camel-tahu/src/test/java/org/apache/camel/component/tahu/SparkplugTCKService.java: ########## @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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 org.apache.camel.component.tahu; + +import java.nio.charset.StandardCharsets; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.NotifyBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.tahu.services.HiveMQService; +import org.apache.camel.component.tahu.services.HiveMQServiceFactory; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.builder.PredicateBuilder; +import org.apache.camel.test.infra.common.services.TestService; +import org.apache.camel.test.infra.core.MockUtils; +import org.eclipse.paho.client.mqttv3.IMqttMessageListener; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.junit.jupiter.Testcontainers; + +import static org.apache.camel.test.junit5.TestSupport.bodyAs; +import static org.junit.jupiter.api.Assertions.fail; + +@Testcontainers +public class SparkplugTCKService implements TestService, BeforeEachCallback, AfterEachCallback { + + private static final Logger LOG = LoggerFactory.getLogger(SparkplugTCKService.class); + + public static final String SPARKPLUG_TCK_TEST_CONTROL_TOPIC = "SPARKPLUG_TCK/TEST_CONTROL"; + public static final String SPARKPLUG_TCK_LOG_TOPIC = "SPARKPLUG_TCK/LOG"; + public static final String SPARKPLUG_TCK_RESULT_TOPIC = "SPARKPLUG_TCK/RESULT"; + + @RegisterExtension + HiveMQService hiveMQService = HiveMQServiceFactory.createService(); + + private final SparkplugTCKMessageListener spTckMessageListener; + + private MqttClient mqttClient; + + public SparkplugTCKService() { + this.spTckMessageListener = new SparkplugTCKMessageListener(); + } + + String getMqttHostAddress() { + if (!hiveMQService.isRunning()) { + hiveMQService.initialize(); + } + + return hiveMQService.getMqttHostAddress(); + } + + @Override + public void beforeEach(ExtensionContext extensionContext) { + LOG.trace("beforeEach called"); + + spTckResultMockEndpoint.expectedMessageCount(1); + spTckResultMockEndpoint.message(0).body(String.class).contains("OVERALL: PASS"); + + spTckResultMockNotify = new NotifyBuilder(monitorCamelContext) + .from("direct:" + SparkplugTCKService.SPARKPLUG_TCK_RESULT_TOPIC) + .whenCompleted(1) + .create(); + + spTckLogMockNotify = new NotifyBuilder(monitorCamelContext) + .from("direct:" + SparkplugTCKService.SPARKPLUG_TCK_LOG_TOPIC) + .filter(PredicateBuilder.and( + // Filter the expected log messages--anything else causes the TCK test to fail + bodyAs(String.class).not().contains("Creating simulated host application"), + bodyAs(String.class).not().contains("Waiting for the Edge and Device to come online"), + bodyAs(String.class).not().contains("Edge Send Complex Data"), + bodyAs(String.class).not().contains("Host Application is online, so using that"))) + .whenCompleted(1) + .create(); + + LOG.trace("beforeEach completed"); + } + + @Override + public void afterEach(ExtensionContext extensionContext) { + LOG.trace("afterEach called"); + + MockEndpoint.resetMocks(monitorCamelContext); + + LOG.trace("afterEach completed"); + } + + private CamelContext monitorCamelContext; + private ProducerTemplate monitorProducerTemplate; + + MockEndpoint spTckLogMockEndpoint; + MockEndpoint spTckResultMockEndpoint; + + NotifyBuilder spTckResultMockNotify; + NotifyBuilder spTckLogMockNotify; + + @Override + public void beforeAll(ExtensionContext extensionContext) { + LOG.trace("beforeAll called"); + + try { + startMonitorCamelContext(extensionContext); + startClient(extensionContext); + } finally { + LOG.trace("beforeAll completed"); + } + } + + private void startClient(ExtensionContext extensionContext) { + try { + mqttClient = new MqttClient( + getMqttHostAddress(), "Tahu-Test-" + MqttClient.generateClientId(), new MemoryPersistence()); + + MqttConnectOptions mqttConnectOptions = new MqttConnectOptions(); + if (hiveMQService.getUserName() != null) { + mqttConnectOptions.setUserName(hiveMQService.getUserName()); + mqttConnectOptions.setPassword(hiveMQService.getUserPassword()); + } + + mqttClient.connect(mqttConnectOptions); + + mqttClient.subscribe(SPARKPLUG_TCK_RESULT_TOPIC, spTckMessageListener); + mqttClient.subscribe(SPARKPLUG_TCK_LOG_TOPIC, spTckMessageListener); + + } catch (MqttException e) { + fail("Exception caught connecting MQTT test client", e); + } + } Review Comment: Removed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org