Migrate netconf/netconf-netty-util to JUnit5.
JIRA: NETCONF-1310
Change-Id: Ic68bca28f642dc689fe4036117da87e07b063665
Signed-off-by: Samuel Schneider <samuel.schneider@pantheon.tech>
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.util.concurrent.Promise;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.opendaylight.netconf.api.NetconfSession;
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class AbstractChannelInitializerTest {
+@ExtendWith(MockitoExtension.class)
+class AbstractChannelInitializerTest {
@Mock
private Channel channel;
@Mock
private Promise<NetconfSession> sessionPromise;
- @Before
- public void setUp() throws Exception {
+ @BeforeEach
+ void setUp() {
doReturn(pipeline).when(channel).pipeline();
doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
}
@Test
- public void testInit() throws Exception {
+ void testInit() {
final TestingInitializer testingInitializer = new TestingInitializer();
testingInitializer.initialize(channel, sessionPromise);
verify(pipeline, times(4)).addLast(anyString(), any(ChannelHandler.class));
}
}
-}
\ No newline at end of file
+}
*/
package org.opendaylight.netconf.nettyutil;
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.opendaylight.netconf.api.CapabilityURN;
import org.opendaylight.netconf.api.NetconfSessionListener;
import org.opendaylight.netconf.api.messages.HelloMessage;
import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class AbstractNetconfSessionNegotiatorTest {
+@ExtendWith(MockitoExtension.class)
+class AbstractNetconfSessionNegotiatorTest {
@Mock
private NetconfSessionListener<TestingNetconfSession> listener;
@Mock
private HelloMessage helloBase11;
private NetconfXMLToHelloMessageDecoder xmlToHello;
- @Before
- public void setUp() {
+ @BeforeEach
+ void setUp() {
channel = new EmbeddedChannel();
xmlToHello = new NetconfXMLToHelloMessageDecoder();
channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
hello = HelloMessage.createClientHello(Set.of(), Optional.empty());
helloBase11 = HelloMessage.createClientHello(Set.of(CapabilityURN.BASE_1_1), Optional.empty());
- doReturn(promise).when(promise).setFailure(any());
negotiator = new TestSessionNegotiator(helloBase11, promise, channel, timer, listener, 100L);
}
@Test
- public void testStartNegotiation() {
+ void testStartNegotiation() {
enableTimerTask();
negotiator.startNegotiation();
assertEquals(helloBase11, channel.readOutbound());
}
@Test
- public void testStartNegotiationSsl() throws Exception {
+ void testStartNegotiationSsl() throws Exception {
doReturn(true).when(sslHandler).isSharable();
doNothing().when(sslHandler).handlerAdded(any());
doNothing().when(sslHandler).write(any(), any(), any());
}
@Test
- public void testStartNegotiationNotEstablished() throws Exception {
+ void testStartNegotiationNotEstablished() throws Exception {
final ChannelOutboundHandler closedDetector = spy(new CloseDetector());
channel.pipeline().addLast("closedDetector", closedDetector);
doReturn(false).when(promise).isDone();
}
@Test
- public void testGetSessionForHelloMessage() throws Exception {
+ void testGetSessionForHelloMessage() throws Exception {
enableTimerTask();
negotiator.startNegotiation();
final TestingNetconfSession session = negotiator.getSessionForHelloMessage(hello);
assertNotNull(session);
- assertThat(channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR), instanceOf(NetconfEOMAggregator.class));
- assertThat(channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER), instanceOf(EOMFramingMechanismEncoder.class));
+ assertInstanceOf(NetconfEOMAggregator.class, channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR));
+ assertInstanceOf(EOMFramingMechanismEncoder.class, channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER));
}
@Test
- public void testGetSessionForHelloMessageBase11() throws Exception {
+ void testGetSessionForHelloMessageBase11() throws Exception {
enableTimerTask();
negotiator.startNegotiation();
final TestingNetconfSession session = negotiator.getSessionForHelloMessage(helloBase11);
assertNotNull(session);
- assertThat(channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR), instanceOf(NetconfChunkAggregator.class));
- assertThat(channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER),
- instanceOf(ChunkedFramingMechanismEncoder.class));
+ assertInstanceOf(NetconfChunkAggregator.class, channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR));
+ assertInstanceOf(ChunkedFramingMechanismEncoder.class, channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER));
}
@Test
- public void testReplaceHelloMessageInboundHandler() throws Exception {
+ void testReplaceHelloMessageInboundHandler() throws Exception {
final List<Object> out = new ArrayList<>();
final byte[] msg = "<rpc/>".getBytes();
final ByteBuf msgBuf = Unpooled.wrappedBuffer(msg);
}
@Test
- public void testNegotiationFail() {
+ void testNegotiationFail() {
+ doReturn(promise).when(promise).setFailure(any());
+
enableTimerTask();
doReturn(true).when(timeout).cancel();
negotiator.startNegotiation();
*/
package org.opendaylight.netconf.nettyutil;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import java.io.EOFException;
import java.util.Optional;
import java.util.Set;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.opendaylight.netconf.api.NetconfSessionListener;
import org.opendaylight.netconf.api.NetconfTerminationReason;
import org.opendaylight.netconf.api.messages.HelloMessage;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
import org.opendaylight.yangtools.yang.common.Uint32;
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class AbstractNetconfSessionTest {
+@ExtendWith(MockitoExtension.class)
+class AbstractNetconfSessionTest {
private static final SessionIdType SESSION_ID = new SessionIdType(Uint32.ONE);
@Mock
private HelloMessage clientHello;
- @Before
- public void setUp() throws Exception {
- doNothing().when(listener).onMessage(any(TestingNetconfSession.class), any(NetconfMessage.class));
- doNothing().when(listener).onSessionUp(any(TestingNetconfSession.class));
- doNothing().when(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
- doNothing().when(listener).onSessionTerminated(any(TestingNetconfSession.class),
- any(NetconfTerminationReason.class));
-
- doReturn(writeFuture).when(channel).newPromise();
- doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class), any(ChannelPromise.class));
- doReturn(pipeline).when(channel).pipeline();
- doReturn(mock(ChannelFuture.class)).when(channel).close();
-
- doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
-
- doReturn(eventLoop).when(channel).eventLoop();
- doAnswer(invocation -> {
- invocation.<Runnable>getArgument(0).run();
- return null;
- }).when(eventLoop).execute(any(Runnable.class));
-
+ @BeforeEach
+ void setUp() {
clientHello = HelloMessage.createClientHello(Set.of(), Optional.empty());
}
@Test
- public void testHandleMessage() throws Exception {
+ void testHandleMessage() {
+ doNothing().when(listener).onMessage(any(TestingNetconfSession.class), any(NetconfMessage.class));
+
final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
testingNetconfSession.handleMessage(clientHello);
verify(listener).onMessage(testingNetconfSession, clientHello);
}
@Test
- public void testSessionUp() throws Exception {
+ void testSessionUp() {
+ doNothing().when(listener).onSessionUp(any(TestingNetconfSession.class));
+
final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
testingNetconfSession.sessionUp();
verify(listener).onSessionUp(testingNetconfSession);
}
@Test
- public void testClose() throws Exception {
+ void testClose() {
+ doReturn(mock(ChannelFuture.class)).when(channel).close();
+ doNothing().when(listener).onSessionUp(any(TestingNetconfSession.class));
+ doNothing().when(listener).onSessionTerminated(any(TestingNetconfSession.class),
+ any(NetconfTerminationReason.class));
+
final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
testingNetconfSession.sessionUp();
testingNetconfSession.close();
}
@Test
- public void testReplaceHandlers() throws Exception {
+ void testReplaceHandlers() {
+ doReturn(writeFuture).when(channel).newPromise();
+ doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class), any(ChannelPromise.class));
+ doReturn(pipeline).when(channel).pipeline();
+ doReturn(null).when(pipeline).replace(anyString(), anyString(), any(ChannelHandler.class));
+ doReturn(eventLoop).when(channel).eventLoop();
+ doAnswer(invocation -> {
+ invocation.<Runnable>getArgument(0).run();
+ return null;
+ }).when(eventLoop).execute(any(Runnable.class));
+
final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
final ChannelHandler mock = mock(ChannelHandler.class);
}
@Test
- public void testStartExi() throws Exception {
+ void testStartExi() {
TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
testingNetconfSession = spy(testingNetconfSession);
}
@Test
- public void testEndOfInput() throws Exception {
+ void testEndOfInput() {
+ doNothing().when(listener).onSessionUp(any(TestingNetconfSession.class));
+ doNothing().when(listener).onSessionDown(any(TestingNetconfSession.class), any(Exception.class));
+
final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
testingNetconfSession.endOfInput();
verifyNoMoreInteractions(listener);
}
@Test
- public void testSendMessage() throws Exception {
+ void testSendMessage() {
+ doReturn(writeFuture).when(channel).newPromise();
+ doReturn(writeFuture).when(channel).writeAndFlush(any(NetconfMessage.class), any(ChannelPromise.class));
+ doReturn(eventLoop).when(channel).eventLoop();
+ doAnswer(invocation -> {
+ invocation.<Runnable>getArgument(0).run();
+ return null;
+ }).when(eventLoop).execute(any(Runnable.class));
+
final TestingNetconfSession testingNetconfSession = new TestingNetconfSession(listener, channel, SESSION_ID);
final HelloMessage hello = HelloMessage.createClientHello(Set.of(), Optional.empty());
testingNetconfSession.sendMessage(hello);
*/
package org.opendaylight.netconf.nettyutil;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR;
import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER;
import io.netty.util.concurrent.Promise;
import java.util.Optional;
import java.util.Set;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.opendaylight.netconf.api.CapabilityURN;
import org.opendaylight.netconf.api.NetconfSessionListener;
import org.opendaylight.netconf.api.messages.HelloMessage;
import org.opendaylight.netconf.test.util.XmlFileLoader;
import org.w3c.dom.Document;
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class Netconf539Test {
+@ExtendWith(MockitoExtension.class)
+class Netconf539Test {
@Mock
private NetconfSessionListener<TestingNetconfSession> listener;
@Mock
private EmbeddedChannel channel;
private TestSessionNegotiator negotiator;
- @Before
- public void setUp() throws Exception {
+ @BeforeEach
+ void setUp() {
channel = new EmbeddedChannel();
channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
new ChannelInboundHandlerAdapter());
}
@Test
- public void testGetSessionForHelloMessageDefaultNs() throws Exception {
+ void testGetSessionForHelloMessageDefaultNs() throws Exception {
testGetSessionForHelloMessage("netconf539/client_hello_1.1.xml");
}
@Test
- public void testGetSessionForHelloMessageNsPrefix() throws Exception {
+ void testGetSessionForHelloMessageNsPrefix() throws Exception {
testGetSessionForHelloMessage("netconf539/client_hello_1.1_ns.xml");
}
final HelloMessage helloMessage = new HelloMessage(helloDocument);
final TestingNetconfSession session = negotiator.getSessionForHelloMessage(helloMessage);
assertNotNull(session);
- assertTrue("NetconfChunkAggregator was not installed in the Netconf pipeline",
- channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator);
- assertTrue("ChunkedFramingMechanismEncoder was not installed in the Netconf pipeline",
- channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof ChunkedFramingMechanismEncoder);
+ assertInstanceOf(NetconfChunkAggregator.class, channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR),
+ "NetconfChunkAggregator was not installed in the Netconf pipeline");
+ assertInstanceOf(ChunkedFramingMechanismEncoder.class, channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER),
+ "ChunkedFramingMechanismEncoder was not installed in the Netconf pipeline");
}
}
package org.opendaylight.netconf.nettyutil.handler;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class ChunkedFramingMechanismEncoderTest {
+@ExtendWith(MockitoExtension.class)
+class ChunkedFramingMechanismEncoderTest {
- private int chunkSize;
+ private static final int CHUNK_SIZE = 256;
@Mock
private ChannelHandlerContext ctx;
- @Before
- public void setUp() {
- chunkSize = 256;
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testIllegalSize() throws Exception {
- new ChunkedFramingMechanismEncoder(10);
+ @Test
+ void testIllegalSize() {
+ assertThrows(IllegalArgumentException.class, () -> new ChunkedFramingMechanismEncoder(10));
}
- @Test(expected = IllegalArgumentException.class)
- public void testIllegalSizeMax() throws Exception {
- new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE);
+ @Test
+ void testIllegalSizeMax() {
+ assertThrows(IllegalArgumentException.class, () -> new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE));
}
@Test
- public void testEncode() throws Exception {
- final ChunkedFramingMechanismEncoder encoder = new ChunkedFramingMechanismEncoder(chunkSize);
+ void testEncode() {
+ final ChunkedFramingMechanismEncoder encoder = new ChunkedFramingMechanismEncoder(CHUNK_SIZE);
final int lastChunkSize = 20;
- final ByteBuf src = Unpooled.wrappedBuffer(getByteArray(chunkSize * 4 + lastChunkSize));
+ final ByteBuf src = Unpooled.wrappedBuffer(getByteArray(CHUNK_SIZE * 4 + lastChunkSize));
final ByteBuf destination = Unpooled.buffer();
encoder.encode(ctx, src, destination);
package org.opendaylight.netconf.nettyutil.handler;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
-public class EOMFramingMechanismEncoderTest {
+class EOMFramingMechanismEncoderTest {
@Test
- public void testEncode() throws Exception {
+ void testEncode() {
final byte[] content = new byte[50];
final ByteBuf source = Unpooled.wrappedBuffer(content);
final ByteBuf destination = Unpooled.buffer();
*/
package org.opendaylight.netconf.nettyutil.handler;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
-public class NetconfChunkAggregatorTest {
+class NetconfChunkAggregatorTest {
private static final String CHUNKED_MESSAGE = "\n#4\n"
+ "<rpc"
+ "\n#18\n"
private final NetconfChunkAggregator agr = new NetconfChunkAggregator(4096);
@Test
- public void testMultipleChunks() throws Exception {
+ void testMultipleChunks() {
final var output = new ArrayList<>();
final var input = Unpooled.copiedBuffer(CHUNKED_MESSAGE.getBytes(StandardCharsets.UTF_8));
agr.decode(null, input, output);
}
@Test
- public void testOneChunks() throws Exception {
+ void testOneChunks() {
final var output = new ArrayList<>();
final var input = Unpooled.copiedBuffer(CHUNKED_MESSAGE_ONE.getBytes(StandardCharsets.UTF_8));
agr.decode(null, input, output);
*/
package org.opendaylight.netconf.nettyutil.handler;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
-public class NetconfEOMAggregatorTest {
+class NetconfEOMAggregatorTest {
private static final String COMM_1 = """
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply message-id="105"
private static NetconfEOMAggregator aggregator;
- @Before
- public void setUp() throws Exception {
+ @BeforeEach
+ void setUp() throws Exception {
aggregator = new NetconfEOMAggregator();
}
@Test
- public void testDecodeMessagesReadAtOnce() {
+ void testDecodeMessagesReadAtOnce() {
final ByteBuf in = Unpooled.copiedBuffer(COMM_1.getBytes());
final List<Object> out = new LinkedList<>();
}
@Test
- public void testDecodeMessagesReadByteByByte() {
+ void testDecodeMessagesReadByteByByte() {
final ByteBuf in = Unpooled.buffer();
final List<Object> out = new LinkedList<>();
}
@Test
- public void testDecodeMultipleStreams() {
+ void testDecodeMultipleStreams() {
final ByteBuf in = Unpooled.copiedBuffer(COMM_1.getBytes());
final List<Object> out = new LinkedList<>();
}
@Test
- public void testDecodeBufferReset() {
+ void testDecodeBufferReset() {
final ByteBuf in = Unpooled.buffer();
final List<Object> out = new LinkedList<>();
}
@Test
- public void testDecodeEmptyMessage() {
+ void testDecodeEmptyMessage() {
final ByteBuf in = Unpooled.buffer();
final List<Object> out = new LinkedList<>();
*/
package org.opendaylight.netconf.nettyutil.handler;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import io.netty.buffer.Unpooled;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.opendaylight.netconf.api.messages.NetconfMessage;
import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.netconf.nettyutil.handler.exi.EXIParameters;
import org.xmlunit.builder.DiffBuilder;
-public class NetconfEXIHandlersTest {
+class NetconfEXIHandlersTest {
private final String msgAsString = "<netconf-message/>";
private NetconfMessageToEXIEncoder netconfMessageToEXIEncoder;
private NetconfMessage msg;
private byte[] msgAsExi;
- @Before
- public void setUp() throws Exception {
+ @BeforeEach
+ void setUp() throws Exception {
final var codec = NetconfEXICodec.forParameters(EXIParameters.empty());
netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
}
@Test
- public void testEncodeDecode() throws Exception {
+ void testEncodeDecode() throws Exception {
final var buffer = Unpooled.buffer();
netconfMessageToEXIEncoder.encode(null, msg, buffer);
final int exiLength = msgAsExi.length;
.withTest(((NetconfMessage) out.get(0)).getDocument())
.checkForIdentical()
.build();
- assertFalse(diff.toString(), diff.hasDifferences());
+ assertFalse(diff.hasDifferences(), diff.toString());
}
}
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.opendaylight.netconf.api.messages.HelloMessage;
import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
import org.opendaylight.netconf.api.messages.NetconfMessage;
import org.opendaylight.netconf.api.xml.XmlUtil;
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class NetconfHelloMessageToXMLEncoderTest {
+@ExtendWith(MockitoExtension.class)
+class NetconfHelloMessageToXMLEncoderTest {
@Mock
private ChannelHandlerContext ctx;
@Test
- public void testEncode() throws Exception {
+ void testEncode() throws Exception {
final NetconfMessage msg = new HelloMessage(XmlUtil.readXmlToDocument(
"<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"),
NetconfHelloMessageAdditionalHeader.fromString("[tomas;10.0.0.0:10000;tcp;client;]"));
}
@Test
- public void testEncodeNoHeader() throws Exception {
+ void testEncodeNoHeader() throws Exception {
final NetconfMessage msg = new HelloMessage(XmlUtil.readXmlToDocument(
"<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
final ByteBuf destination = Unpooled.buffer();
}
@Test
- public void testEncodeNotHello() throws Exception {
+ void testEncodeNotHello() throws Exception {
final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument(
"<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
assertThrows(IllegalStateException.class, () -> new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, null));
*/
package org.opendaylight.netconf.nettyutil.handler;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import io.netty.buffer.Unpooled;
import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
-public class NetconfMessageFactoryTest {
+class NetconfMessageFactoryTest {
@Test
- public void testAuth() throws Exception {
+ void testAuth() throws Exception {
NetconfXMLToHelloMessageDecoder parser = new NetconfXMLToHelloMessageDecoder();
File authHelloFile = new File(getClass().getResource("/netconfMessages/client_hello_with_auth.xml").getFile());
package org.opendaylight.netconf.nettyutil.handler;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.common.collect.Iterables;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.CoreMatchers;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.opendaylight.netconf.api.messages.HelloMessage;
import org.opendaylight.netconf.api.xml.XmlUtil;
-public class NetconfXMLToHelloMessageDecoderTest {
+class NetconfXMLToHelloMessageDecoderTest {
@Test
- public void testDecodeWithHeader() throws Exception {
+ void testDecodeWithHeader() throws Exception {
final ByteBuf src = Unpooled.wrappedBuffer(String.format("%s\n%s",
"[tomas;10.0.0.0:10000;tcp;client;]",
"<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>").getBytes());
new NetconfXMLToHelloMessageDecoder().decode(null, src, out);
assertEquals(1, out.size());
- assertThat(out.get(0), CoreMatchers.instanceOf(HelloMessage.class));
- final HelloMessage hello = (HelloMessage) out.get(0);
+ final HelloMessage hello = assertInstanceOf(HelloMessage.class, out.get(0));
assertTrue(hello.getAdditionalHeader().isPresent());
assertEquals("[tomas;10.0.0.0:10000;tcp;client;]" + System.lineSeparator(),
hello.getAdditionalHeader().orElseThrow().toFormattedString());
}
@Test
- public void testDecodeNoHeader() throws Exception {
+ void testDecodeNoHeader() throws Exception {
final ByteBuf src =
Unpooled.wrappedBuffer("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
final List<Object> out = new ArrayList<>();
new NetconfXMLToHelloMessageDecoder().decode(null, src, out);
assertEquals(1, out.size());
- assertThat(out.get(0), CoreMatchers.instanceOf(HelloMessage.class));
- final HelloMessage hello = (HelloMessage) out.get(0);
+ final HelloMessage hello = assertInstanceOf(HelloMessage.class, out.get(0));
assertFalse(hello.getAdditionalHeader().isPresent());
}
@Test
- public void testDecodeCaching() throws Exception {
+ void testDecodeCaching() throws Exception {
final ByteBuf msg1 =
Unpooled.wrappedBuffer("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
final ByteBuf msg2 =
assertEquals(2, Iterables.size(decoder.getPostHelloNetconfMessages()));
}
- @Test(expected = IllegalStateException.class)
- public void testDecodeNotHelloReceived() throws Exception {
+ @Test
+ void testDecodeNotHelloReceived() {
final ByteBuf msg1 =
Unpooled.wrappedBuffer("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
final List<Object> out = new ArrayList<>();
NetconfXMLToHelloMessageDecoder decoder = new NetconfXMLToHelloMessageDecoder();
- decoder.decode(null, msg1, out);
+ assertThrows(IllegalStateException.class, () -> decoder.decode(null, msg1, out));
}
}
*/
package org.opendaylight.netconf.nettyutil.handler;
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import io.netty.buffer.Unpooled;
import java.util.ArrayList;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.opendaylight.netconf.api.messages.NetconfMessage;
import org.xml.sax.SAXParseException;
-public class NetconfXMLToMessageDecoderTest {
+class NetconfXMLToMessageDecoderTest {
@Test
- public void testDecodeNoMoreContent() throws Exception {
+ void testDecodeNoMoreContent() throws Exception {
final ArrayList<Object> out = new ArrayList<>();
new NetconfXMLToMessageDecoder().decode(null, Unpooled.buffer(), out);
assertEquals(0, out.size());
}
@Test
- public void testDecode() throws Exception {
+ void testDecode() throws Exception {
final ArrayList<Object> out = new ArrayList<>();
new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("<msg/>".getBytes()), out);
assertEquals(1, out.size());
}
@Test
- public void testDecodeWithLeadingLFAndXmlDecl() throws Exception {
+ void testDecodeWithLeadingLFAndXmlDecl() throws Exception {
/* Test that we accept XML documents with a line feed (0x0a) before the
* XML declaration in the XML prologue.
* A leading LF is the case reported in BUG-2838.
}
@Test
- public void testDecodeWithLeadingCRLFAndXmlDecl() throws Exception {
+ void testDecodeWithLeadingCRLFAndXmlDecl() throws Exception {
/* Test that we accept XML documents with both a carriage return and
* line feed (0x0d 0x0a) before the XML declaration in the XML prologue.
* Leading CRLF can be seen with some Cisco routers
}
@Test
- public void testDecodeGibberish() throws Exception {
+ void testDecodeGibberish() throws Exception {
/* Test that we reject inputs where we cannot find the xml start '<' character */
final ArrayList<Object> out = new ArrayList<>();
new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n?xml version>".getBytes()), out);
assertEquals(1, out.size());
- assertTrue((out.get(0) instanceof SAXParseException));
+ assertInstanceOf(SAXParseException.class, out.get(0));
}
@Test
- public void testDecodeOnlyWhitespaces() throws Exception {
+ void testDecodeOnlyWhitespaces() throws Exception {
/* Test that we handle properly a bunch of whitespaces.
*/
final ArrayList<Object> out = new ArrayList<>();
}
@Test
- public void testDecodeWithAllWhitespaces() throws Exception {
+ void testDecodeWithAllWhitespaces() throws Exception {
/* Test that every whitespace we want to skip is actually skipped.
*/
}
@Test
- public void testDecodeAfterInvalidXml() throws Exception {
+ void testDecodeAfterInvalidXml() throws Exception {
/* Test that decoding of the next message after an invalid XML is successful.
*/
final var out = new ArrayList<>();
buffer.writeBytes("<?xml version=\"1.0\"\u0006 encoding=\"UTF-8\"?><msg/>".getBytes());
decoder.decode(null, buffer, out);
assertEquals(1, out.size());
- assertThat(out.get(0), instanceOf(SAXParseException.class));
+ assertInstanceOf(SAXParseException.class, out.get(0));
buffer.writeBytes("<msg/>".getBytes());
decoder.decode(null, buffer, out);
assertEquals(2, out.size());
- assertThat(out.get(1), instanceOf(NetconfMessage.class));
+ assertInstanceOf(NetconfMessage.class, out.get(1));
}
}
package org.opendaylight.netconf.nettyutil.handler;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.xml.transform.Transformer;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
-public class ThreadLocalTransformersTest {
+class ThreadLocalTransformersTest {
private ExecutorService executorService;
- @Before
- public void setUp() throws Exception {
+ @BeforeEach
+ void setUp() {
executorService = Executors.newSingleThreadExecutor();
}
@Test
- public void testGetDefaultTransformer() throws Exception {
+ void testGetDefaultTransformer() throws Exception {
final Transformer t1 = ThreadLocalTransformers.getDefaultTransformer();
final Transformer t2 = ThreadLocalTransformers.getDefaultTransformer();
- Assert.assertSame(t1, t2);
+ assertSame(t1, t2);
final Future<Transformer> future = executorService.submit(ThreadLocalTransformers::getDefaultTransformer);
- Assert.assertNotSame(t1, future.get());
+ assertNotSame(t1, future.get());
}
@Test
- public void testGetPrettyTransformer() throws Exception {
+ void testGetPrettyTransformer() throws Exception {
final Transformer t1 = ThreadLocalTransformers.getPrettyTransformer();
final Transformer t2 = ThreadLocalTransformers.getPrettyTransformer();
- Assert.assertSame(t1, t2);
+ assertSame(t1, t2);
final Future<Transformer> future = executorService.submit(ThreadLocalTransformers::getPrettyTransformer);
- Assert.assertNotSame(t1, future.get());
+ assertNotSame(t1, future.get());
}
- @After
- public void tearDown() throws Exception {
+ @AfterEach
+ void tearDown() {
executorService.shutdown();
}
-}
\ No newline at end of file
+}
package org.opendaylight.netconf.nettyutil.handler.exi;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
-import java.util.Arrays;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
+import java.util.stream.Stream;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import org.opendaylight.netconf.api.xml.XmlElement;
import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.netconf.shaded.exificient.core.CodingMode;
-import org.opendaylight.netconf.shaded.exificient.core.EXIFactory;
import org.opendaylight.netconf.shaded.exificient.core.FidelityOptions;
-@RunWith(Parameterized.class)
-public class EXIParametersTest {
+class EXIParametersTest {
- @Parameterized.Parameters
- public static Iterable<Object[]> data() throws Exception {
- final String noChangeXml =
- "<start-exi xmlns=\"urn:ietf:params:xml:ns:netconf:exi:1.0\">\n"
- + "<alignment>bit-packed</alignment>\n"
- + "</start-exi>\n";
+ @ParameterizedTest
+ @MethodSource("getData")
+ void testFromXmlElement(final String sourceXml, final CodingMode coding, final FidelityOptions fidelity)
+ throws Exception {
+ final var opts =
+ EXIParameters.fromXmlElement(
+ XmlElement.fromDomElement(
+ XmlUtil.readXmlToElement(sourceXml)));
+ final var factory = opts.getFactory();
+ assertEquals(fidelity, factory.getFidelityOptions());
+ assertEquals(coding, factory.getCodingMode());
+ }
+
+ static Stream<Arguments> getData() throws Exception {
+ final var noChangeXml =
+ "<start-exi xmlns=\"urn:ietf:params:xml:ns:netconf:exi:1.0\">\n"
+ + "<alignment>bit-packed</alignment>\n"
+ + "</start-exi>\n";
- final String fullOptionsXml =
- "<start-exi xmlns=\"urn:ietf:params:xml:ns:netconf:exi:1.0\">\n"
- + "<alignment>byte-aligned</alignment>\n"
- + "<fidelity>\n"
- + "<comments/>\n"
- + "<dtd/>\n"
- + "<lexical-values/>\n"
- + "<pis/>\n"
- + "<prefixes/>\n"
- + "</fidelity>\n"
- + "</start-exi>\n";
- final FidelityOptions fullOptions = FidelityOptions.createDefault();
+ final var fullOptionsXml =
+ "<start-exi xmlns=\"urn:ietf:params:xml:ns:netconf:exi:1.0\">\n"
+ + "<alignment>byte-aligned</alignment>\n"
+ + "<fidelity>\n"
+ + "<comments/>\n"
+ + "<dtd/>\n"
+ + "<lexical-values/>\n"
+ + "<pis/>\n"
+ + "<prefixes/>\n"
+ + "</fidelity>\n"
+ + "</start-exi>\n";
+
+ final var fullOptions = FidelityOptions.createDefault();
fullOptions.setFidelity(FidelityOptions.FEATURE_LEXICAL_VALUE, true);
fullOptions.setFidelity(FidelityOptions.FEATURE_DTD, true);
fullOptions.setFidelity(FidelityOptions.FEATURE_COMMENT, true);
fullOptions.setFidelity(FidelityOptions.FEATURE_PREFIX, true);
fullOptions.setFidelity(FidelityOptions.FEATURE_PI, true);
- return Arrays.asList(new Object[][]{
- {noChangeXml, CodingMode.BIT_PACKED, FidelityOptions.createDefault()},
- {fullOptionsXml, CodingMode.BYTE_PACKED, fullOptions},
- });
- }
-
- private final String sourceXml;
- private final CodingMode coding;
- private final FidelityOptions fidelity;
-
- public EXIParametersTest(final String sourceXml, final CodingMode coding, final FidelityOptions fidelity) {
- this.sourceXml = sourceXml;
- this.coding = coding;
- this.fidelity = fidelity;
- }
-
- @Test
- public void testFromXmlElement() throws Exception {
- final EXIParameters opts =
- EXIParameters.fromXmlElement(
- XmlElement.fromDomElement(
- XmlUtil.readXmlToElement(sourceXml)));
-
- final EXIFactory factory = opts.getFactory();
- assertEquals(fidelity, factory.getFidelityOptions());
- assertEquals(coding, factory.getCodingMode());
+ return Stream.of(
+ Arguments.of(noChangeXml, CodingMode.BIT_PACKED, FidelityOptions.createDefault()),
+ Arguments.of(fullOptionsXml, CodingMode.BYTE_PACKED, fullOptions)
+ );
}
}
*/
package org.opendaylight.netconf.nettyutil.handler.exi;
-import static org.junit.Assert.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertFalse;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.opendaylight.netconf.shaded.exificient.core.CodingMode;
import org.opendaylight.netconf.shaded.exificient.core.FidelityOptions;
import org.xmlunit.builder.DiffBuilder;
-public class NetconfStartExiMessageTest {
+class NetconfStartExiMessageTest {
@Test
- public void testCreateEmpty() {
+ void testCreateEmpty() {
assertCreate("""
<rpc message-id="id" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<start-exi xmlns="urn:ietf:params:xml:ns:netconf:exi:1.0">
}
@Test
- public void testCreateFull() throws Exception {
+ void testCreateFull() throws Exception {
final var fullOptions = FidelityOptions.createDefault();
fullOptions.setFidelity(FidelityOptions.FEATURE_LEXICAL_VALUE, true);
fullOptions.setFidelity(FidelityOptions.FEATURE_DTD, true);
.ignoreWhitespace()
.checkForIdentical()
.build();
- assertFalse(diff.toString(), diff.hasDifferences());
+ assertFalse(diff.hasDifferences(), diff.toString());
}
-}
\ No newline at end of file
+}