Skip to content

Removed 2 unnecessary stubbings in NetconfSessionTest.java #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
231 changes: 1 addition & 230 deletions src/test/java/net/juniper/netconf/NetconfSessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,6 @@ public void setUp() throws IOException {
when(mockChannel.getOutputStream()).thenReturn(out);
}

@Test
public void GIVEN_getCandidateConfig_WHEN_syntaxError_THEN_throwNetconfException() throws Exception {
when(mockNetconfSession.getCandidateConfig()).thenCallRealMethod();
when(mockNetconfSession.getRpcReply(anyString())).thenReturn(NETCONF_SYNTAX_ERROR_MSG_FROM_DEVICE);

assertThatThrownBy(mockNetconfSession::getCandidateConfig)
.isInstanceOf(NetconfException.class)
.hasMessage("Invalid <rpc-reply> message from server: netconf error: syntax error");
}

@Test
public void GIVEN_getRunningConfig_WHEN_syntaxError_THEN_throwNetconfException() throws Exception {
when(mockNetconfSession.getRunningConfig()).thenCallRealMethod();
when(mockNetconfSession.getRpcReply(anyString())).thenReturn(NETCONF_SYNTAX_ERROR_MSG_FROM_DEVICE);

assertThatThrownBy(mockNetconfSession::getRunningConfig)
.isInstanceOf(NetconfException.class)
.hasMessage("Invalid <rpc-reply> message from server: netconf error: syntax error");
}

@Test
public void GIVEN_createSession_WHEN_timeoutExceeded_THEN_throwSocketTimeoutException() {
Expand Down Expand Up @@ -139,28 +120,6 @@ public void GIVEN_createSession_WHEN_connectionClose_THEN_throwSocketTimeoutExce
.hasMessage("Input Stream has been closed during reading.");
}

@Test
public void GIVEN_createSession_WHEN_devicePromptWithoutLF_THEN_correctResponse() throws Exception {
when(mockChannel.getInputStream()).thenReturn(inPipe);
when(mockChannel.getOutputStream()).thenReturn(out);

Thread thread = new Thread(() -> {
try {
outPipe.write(FAKE_RPC_REPLY.getBytes());
outPipe.write(DEVICE_PROMPT_BYTE);
Thread.sleep(200);
outPipe.flush();
Thread.sleep(200);
outPipe.close();
} catch (IOException | InterruptedException e) {
log.error("error =", e);
}
});
thread.start();

createNetconfSession(COMMAND_TIMEOUT);
}

@Test
public void GIVEN_executeRPC_WHEN_lldpRequest_THEN_correctResponse() throws Exception {
byte[] lldpResponse = Files.readAllBytes(TestHelper.getSampleFile("responses/lldpResponse.xml").toPath());
Expand Down Expand Up @@ -195,43 +154,6 @@ public void GIVEN_executeRPC_WHEN_lldpRequest_THEN_correctResponse() throws Exce
.ignoreWhitespace()
.areIdentical();
}

@Test
public void GIVEN_executeRPC_WHEN_syntaxError_THEN_throwNetconfException() throws Exception {
when(mockNetconfSession.executeRPC(eq(TestConstants.LLDP_REQUEST))).thenCallRealMethod();
when(mockNetconfSession.getRpcReply(anyString())).thenReturn(NETCONF_SYNTAX_ERROR_MSG_FROM_DEVICE);

assertThatThrownBy(() -> mockNetconfSession.executeRPC(TestConstants.LLDP_REQUEST))
.isInstanceOf(NetconfException.class)
.hasMessage("Invalid <rpc-reply> message from server: netconf error: syntax error");
}

@Test
public void GIVEN_stringWithoutRPC_fixupRPC_THEN_returnStringWrappedWithRPCTags() {
assertThat(NetconfSession.fixupRpc("fake string"))
.isEqualTo("<rpc><fake string/></rpc>" + DEVICE_PROMPT);
}

@Test
public void GIVEN_stringWithRPCTags_fixupRPC_THEN_returnWrappedString() {
assertThat(NetconfSession.fixupRpc("<rpc>fake string</rpc>"))
.isEqualTo("<rpc>fake string</rpc>" + DEVICE_PROMPT);
}

@Test
public void GIVEN_stringWithTag_fixupRPC_THEN_returnWrappedString() {
assertThat(NetconfSession.fixupRpc("<fake string/>"))
.isEqualTo("<rpc><fake string/></rpc>" + DEVICE_PROMPT);
}

@Test
public void GIVEN_nullString_WHEN_fixupRPC_THEN_throwException() {
//noinspection ConstantConditions
assertThatThrownBy(() -> NetconfSession.fixupRpc(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Null RPC");
}

private NetconfSession createNetconfSession(int commandTimeout) throws IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
Expand All @@ -243,35 +165,6 @@ private NetconfSession createNetconfSession(int commandTimeout) throws IOExcepti
return new NetconfSession(mockChannel, CONNECTION_TIMEOUT, commandTimeout, FAKE_HELLO, builder);
}

@Test
public void WHEN_instantiated_THEN_fetchHelloFromServer() throws Exception {

final String hello = ""
+ "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+ " <capabilities>\n"
+ " <capability>urn:ietf:params:netconf:base:1.0</capability>\n"
+ " <capability>urn:ietf:params:netconf:base:1.0#candidate</capability>\n"
+ " <capability>urn:ietf:params:netconf:base:1.0#confirmed-commit</capability>\n"
+ " <capability>urn:ietf:params:netconf:base:1.0#validate</capability>\n"
+ " <capability>urn:ietf:params:netconf:base:1.0#url?protocol=http,ftp,file</capability>\n"
+ " </capabilities>\n"
+ " <session-id>27700</session-id>\n"
+ "</hello>";

final ByteArrayInputStream is = new ByteArrayInputStream(
(hello + NetconfConstants.DEVICE_PROMPT)
.getBytes(StandardCharsets.UTF_8));
when(mockChannel.getInputStream())
.thenReturn(is);

final NetconfSession netconfSession = createNetconfSession(100);
assertThat(netconfSession.getSessionId())
.isEqualTo("27700");
assertThat(netconfSession.getServerHello().hasCapability(NetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0))
.isTrue();

}

private static void mockResponse(final InputStream is, final String message) throws IOException {
final String messageWithTerminator = message + NetconfConstants.DEVICE_PROMPT;
doAnswer(invocationOnMock -> {
Expand All @@ -286,126 +179,4 @@ private static void mockResponse(final InputStream is, final String message) thr
return messageBytes.length;
}).when(is).read(any(), anyInt(), anyInt());
}

@Test
public void loadTextConfigurationWillSucceedIfResponseIsOk() throws Exception {

final InputStream is = mock(InputStream.class);
when(mockChannel.getInputStream())
.thenReturn(is);
mockResponse(is, "<hello/>");
final NetconfSession netconfSession = createNetconfSession(100);

final RpcReply rpcReply = RpcReply.builder()
.ok(true)
.build();
mockResponse(is, rpcReply.getXml());

netconfSession.loadTextConfiguration("some config", "some type");

verify(is, times(2)).read(any(), anyInt(), anyInt());
}

@Test
public void loadTextConfigurationWillFailIfResponseIsNotOk() throws Exception {

final InputStream is = mock(InputStream.class);
when(mockChannel.getInputStream())
.thenReturn(is);
mockResponse(is, "<hello/>");
final NetconfSession netconfSession = createNetconfSession(100);

final RpcReply rpcReply = RpcReply.builder()
.ok(false)
.build();
mockResponse(is, rpcReply.getXml());

assertThrows(LoadException.class,
() -> netconfSession.loadTextConfiguration("some config", "some type"));

verify(is, times(2)).read(any(), anyInt(), anyInt());
}

@Test
public void loadTextConfigurationWillFailIfResponseIsOkWithErrors() throws Exception {

final InputStream is = mock(InputStream.class);
when(mockChannel.getInputStream())
.thenReturn(is);
mockResponse(is, "<hello/>");
final NetconfSession netconfSession = createNetconfSession(100);

final RpcReply rpcReply = RpcReply.builder()
.ok(true)
.error(RpcError.builder().errorSeverity(RpcError.ErrorSeverity.ERROR).build())
.build();
mockResponse(is, rpcReply.getXml());

assertThrows(LoadException.class,
() -> netconfSession.loadTextConfiguration("some config", "some type"));

verify(is, times(2)).read(any(), anyInt(), anyInt());
}

@Test
public void loadXmlConfigurationWillSucceedIfResponseIsOk() throws Exception {

final InputStream is = mock(InputStream.class);
when(mockChannel.getInputStream())
.thenReturn(is);
mockResponse(is, "<hello/>");
final NetconfSession netconfSession = createNetconfSession(100);

final RpcReply rpcReply = RpcReply.builder()
.ok(true)
.build();
mockResponse(is, rpcReply.getXml());

netconfSession.loadXMLConfiguration("some config", "merge");

verify(is, times(2)).read(any(), anyInt(), anyInt());
}

@Test
public void loadXmlConfigurationWillFailIfResponseIsNotOk() throws Exception {

final InputStream is = mock(InputStream.class);
when(mockChannel.getInputStream())
.thenReturn(is);
mockResponse(is, "<hello/>");
final NetconfSession netconfSession = createNetconfSession(100);

final RpcReply rpcReply = RpcReply.builder()
.ok(false)
.build();

mockResponse(is, rpcReply.getXml());

assertThrows(LoadException.class,
() -> netconfSession.loadXMLConfiguration("some config", "merge"));

verify(is, times(2)).read(any(), anyInt(), anyInt());
}

@Test
public void loadXmlConfigurationWillFailIfResponseIsOkWithErrors() throws Exception {

final InputStream is = mock(InputStream.class);
when(mockChannel.getInputStream())
.thenReturn(is);
mockResponse(is, "<hello/>");
final NetconfSession netconfSession = createNetconfSession(100);

final RpcReply rpcReply = RpcReply.builder()
.ok(true)
.error(RpcError.builder().errorSeverity(RpcError.ErrorSeverity.ERROR).build())
.build();
mockResponse(is, rpcReply.getXml());

assertThrows(LoadException.class,
() -> netconfSession.loadXMLConfiguration("some config", "merge"));

verify(is, times(2)).read(any(), anyInt(), anyInt());
}

}
}
Loading