import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
import org.opendaylight.netconf.notifications.NetconfNotification;
-import org.opendaylight.netconf.util.messages.SendErrorExceptionUtil;
import org.opendaylight.netconf.util.messages.SubtreeFilter;
import org.opendaylight.yangtools.yang.common.ErrorSeverity;
import org.opendaylight.yangtools.yang.common.ErrorTag;
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
-package org.opendaylight.netconf.util.messages;
+package org.opendaylight.netconf.impl;
import com.google.common.base.Preconditions;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.Map;
import org.opendaylight.netconf.api.DocumentedException;
-import org.opendaylight.netconf.util.messages.SendErrorExceptionUtil;
+import org.opendaylight.netconf.impl.SendErrorExceptionUtil;
import org.opendaylight.yangtools.yang.common.ErrorSeverity;
import org.opendaylight.yangtools.yang.common.ErrorTag;
import org.opendaylight.yangtools.yang.common.ErrorType;
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
+package org.opendaylight.netconf.impl;
-package org.opendaylight.netconf.util.messages;
-
+import static java.util.Objects.requireNonNull;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.util.concurrent.GenericFutureListener;
+import java.io.IOException;
+import java.io.InputStream;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.netconf.api.DocumentedException;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.api.NetconfSession;
-import org.opendaylight.netconf.util.test.XmlFileLoader;
-import org.w3c.dom.Document;
+import org.opendaylight.netconf.api.xml.XmlUtil;
import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class SendErrorExceptionUtilTest {
+ private final DocumentedException exception = new DocumentedException("err");
+ @Mock
NetconfSession netconfSession;
+ @Mock
ChannelFuture channelFuture;
+ @Mock
Channel channel;
- private DocumentedException exception;
@Before
public void setUp() throws Exception {
- netconfSession = mock(NetconfSession.class);
- channelFuture = mock(ChannelFuture.class);
- channel = mock(Channel.class);
doReturn(channelFuture).when(netconfSession).sendMessage(any(NetconfMessage.class));
doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
doReturn(channelFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
- exception = new DocumentedException("err");
}
@Test
public void testSendErrorMessage1() throws Exception {
SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception);
- verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
- verify(netconfSession, times(1)).sendMessage(any(NetconfMessage.class));
+ verify(channelFuture).addListener(any(GenericFutureListener.class));
+ verify(netconfSession).sendMessage(any(NetconfMessage.class));
}
@Test
public void testSendErrorMessage2() throws Exception {
SendErrorExceptionUtil.sendErrorMessage(channel, exception);
- verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
+ verify(channelFuture).addListener(any(GenericFutureListener.class));
}
@Test
public void testSendErrorMessage3() throws Exception {
- Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc.xml");
- SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, new NetconfMessage(helloMessage));
- verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
+ SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, readMessage("rpc.xml"));
+ verify(channelFuture).addListener(any(GenericFutureListener.class));
}
@Test
public void testSendErrorMessage4() throws Exception {
- Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc_ns.xml");
- SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, new NetconfMessage(helloMessage));
+ SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, readMessage("rpc_ns.xml"));
final ArgumentCaptor<NetconfMessage> messageCaptor = ArgumentCaptor.forClass(NetconfMessage.class);
verify(netconfSession, times(1)).sendMessage(messageCaptor.capture());
final Element rpcReply = messageCaptor.getValue().getDocument().getDocumentElement();
assertEquals("Invalid value of message-id attribute in the reply message", "a",
rpcReply.getAttribute("message-id"));
}
+
+ private static NetconfMessage readMessage(final String name) throws IOException, SAXException {
+ try (InputStream resource =
+ requireNonNull(SendErrorExceptionUtilTest.class.getResourceAsStream("/messages/" + name))) {
+ return new NetconfMessage(XmlUtil.readXmlToDocument(resource));
+ }
+ }
}
import org.xml.sax.SAXException;
public class AbstractNetconfOperationTest {
-
- class NetconfOperationImpl extends AbstractNetconfOperation {
-
+ static class NetconfOperationImpl extends AbstractNetconfOperation {
public boolean handleRun;
- protected NetconfOperationImpl(String netconfSessionIdForReporting) {
+ protected NetconfOperationImpl(final String netconfSessionIdForReporting) {
super(netconfSessionIdForReporting);
this.handleRun = false;
}
}
@Override
- protected Element handle(Document document, XmlElement message,
- NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
+ protected Element handle(final Document document, final XmlElement message,
+ final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
this.handleRun = true;
try {
return XmlUtil.readXmlToElement("<element/>");
} catch (SAXException | IOException e) {
- throw new RuntimeException(e);
+ throw DocumentedException.wrap(e);
}
}
}
- private NetconfOperationImpl netconfOperation;
+ private final NetconfOperationImpl netconfOperation = new NetconfOperationImpl("str");
private NetconfOperationChainedExecution operation;
@Before
public void setUp() throws Exception {
- netconfOperation = new NetconfOperationImpl("str");
operation = mock(NetconfOperationChainedExecution.class);
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.util.messages;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.util.test.XmlFileLoader;
-import org.w3c.dom.Document;
public class NetconfMessageUtilTest {
@Test
public void testNetconfMessageUtil() throws Exception {
- Document okMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
- assertTrue(NetconfMessageUtil.isOKMessage(new NetconfMessage(okMessage)));
- assertFalse(NetconfMessageUtil.isErrorMessage(new NetconfMessage(okMessage)));
+ final NetconfMessage okMessage = new NetconfMessage(XmlFileLoader.xmlFileToDocument(
+ "netconfMessages/rpc-reply_ok.xml"));
+ assertTrue(NetconfMessageUtil.isOKMessage(okMessage));
+ assertFalse(NetconfMessageUtil.isErrorMessage(okMessage));
- Document errorMessage = XmlFileLoader
- .xmlFileToDocument("netconfMessages/communicationError/testClientSendsRpcReply_expectedResponse.xml");
- assertTrue(NetconfMessageUtil.isErrorMessage(new NetconfMessage(errorMessage)));
- assertFalse(NetconfMessageUtil.isOKMessage(new NetconfMessage(errorMessage)));
+ final NetconfMessage errorMessage = new NetconfMessage(XmlFileLoader.xmlFileToDocument(
+ "netconfMessages/communicationError/testClientSendsRpcReply_expectedResponse.xml"));
+ assertTrue(NetconfMessageUtil.isErrorMessage(errorMessage));
+ assertFalse(NetconfMessageUtil.isOKMessage(errorMessage));
- Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml");
- Collection<String> caps =
- NetconfMessageUtil.extractCapabilitiesFromHello(new NetconfMessage(helloMessage).getDocument());
+ final Collection<String> caps = NetconfMessageUtil.extractCapabilitiesFromHello(
+ XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml"));
assertTrue(caps.contains("urn:ietf:params:netconf:base:1.0"));
assertTrue(caps.contains("urn:ietf:params:netconf:base:1.1"));
}
import static java.util.Objects.requireNonNull;
-import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.ParserConfigurationException;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.api.xml.XmlUtil;
import org.w3c.dom.Document;
public final class XmlFileLoader {
private XmlFileLoader() {
-
+ // Hidden on purpose
}
public static NetconfMessage xmlFileToNetconfMessage(final String fileName) throws IOException, SAXException,
public static Document xmlFileToDocument(final String fileName) throws IOException, SAXException,
ParserConfigurationException {
- try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
- requireNonNull(resourceAsStream, fileName);
- final Document doc = XmlUtil.readXmlToDocument(resourceAsStream);
- return doc;
+ try (InputStream resource = getResourceAsStream(fileName)) {
+ return XmlUtil.readXmlToDocument(resource);
}
}
public static String fileToString(final String fileName) throws IOException {
- try (InputStream resourceAsStream = XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName)) {
- requireNonNull(resourceAsStream);
- return new ByteSource() {
- @Override
- public InputStream openStream() {
- return resourceAsStream;
- }
- }.asCharSource(StandardCharsets.UTF_8).read();
-
+ try (InputStream resource = getResourceAsStream(fileName)) {
+ return new String(resource.readAllBytes(), StandardCharsets.UTF_8);
}
}
- public static InputStream getResourceAsStream(final String fileName) {
- return XmlFileLoader.class.getClassLoader().getResourceAsStream(fileName);
+ public static @NonNull InputStream getResourceAsStream(final String fileName) {
+ final String resourceName = requireNonNull(fileName);
+ return requireNonNull(XmlFileLoader.class.getClassLoader().getResourceAsStream(resourceName), resourceName);
}
}