Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,66 @@
*/
package org.apache.camel.component.plc4x;

import java.util.Collections;
import java.util.concurrent.ScheduledFuture;

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 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
void doStart() throws Exception {
doNothing().when(endpoint).setupConnection();

consumer.doStart();

verify(endpoint, times(1)).setupConnection();
}

@Test
public void doStart() {
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");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is reflection really needed?

Is there another way to test it maybe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed by looking at the producer test. hope this is ok.

field.setAccessible(true);
field.set(consumer, future);

consumer.doStop();

verify(future).cancel(true);
}

}