This is an automated email from the ASF dual-hosted git repository. fmariani pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 548570e16495b82c4507912de0a26d4a56804bb9 Author: chandrashekhar <[email protected]> AuthorDate: Sun Feb 8 10:58:23 2026 +0530 Test cases for Plc4X consumer. --- .../camel/component/plc4x/Plc4XConsumerTest.java | 75 ++++++++++++++++------ 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/components/camel-plc4x/src/test/java/org/apache/camel/component/plc4x/Plc4XConsumerTest.java b/components/camel-plc4x/src/test/java/org/apache/camel/component/plc4x/Plc4XConsumerTest.java index 77b9540b5bdc..accd04be1c49 100644 --- a/components/camel-plc4x/src/test/java/org/apache/camel/component/plc4x/Plc4XConsumerTest.java +++ b/components/camel-plc4x/src/test/java/org/apache/camel/component/plc4x/Plc4XConsumerTest.java @@ -1,32 +1,67 @@ -/* - * 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.plc4x; +import org.apache.camel.Processor; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.plc4x.java.api.exceptions.PlcConnectionException; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -// TODO: implement me -public class Plc4XConsumerTest { +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ScheduledFuture; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class Plc4XConsumerTest { + + private Plc4XEndpoint endpoint; + private Processor processor; + private Plc4XConsumer consumer; + + @BeforeEach + void setUp() { + endpoint = mock(Plc4XEndpoint.class); + processor = mock(Processor.class); + + when(endpoint.getTags()).thenReturn(Collections.emptyMap()); + when(endpoint.getTrigger()).thenReturn(null); // untriggered + when(endpoint.getCamelContext()).thenReturn(new DefaultCamelContext()); + + consumer = new Plc4XConsumer(endpoint, processor); + } + @Test - public void doStart() { + void doStart() throws Exception { + doNothing().when(endpoint).setupConnection(); + + consumer.doStart(); + + verify(endpoint, times(1)).setupConnection(); + } + + @Test + void doStartBadStart() throws Exception { + doThrow(new PlcConnectionException("fail")) + .when(endpoint).setupConnection(); + + consumer.doStart(); + + verify(endpoint).setupConnection(); + assertFalse(consumer.isStarted()); } @Test - public void doStop() { + void doStop() throws Exception { + ScheduledFuture<?> future = mock(ScheduledFuture.class); + var field = Plc4XConsumer.class.getDeclaredField("future"); + field.setAccessible(true); + field.set(consumer, future); + + consumer.doStop(); + + verify(future).cancel(true); } }
