a0a15b99389c1c8a768079189e3ad3c189a35367
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.netconfsimulator.netconfcore.configuration;
22
23 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import com.tailf.jnc.Element;
31 import com.tailf.jnc.JNCException;
32 import com.tailf.jnc.NetconfSession;
33 import com.tailf.jnc.NodeSet;
34 import java.io.IOException;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.MockitoAnnotations;
40
41 class NetconfConfigurationReaderTest {
42
43     private static final String NETCONF_MODEL_PATH = "";
44     private static final String EXPECTED_STRING_XML = "<?xml version=\"1.0\"?>";
45     private NetconfConfigurationReader reader;
46
47     @Mock
48     private NetconfSession netconfSession;
49
50     @Mock
51     private NetconfSessionHelper netconfSessionHelper;
52
53     @Mock
54     private NodeSet nodeSet;
55
56     @Mock
57     private Element element;
58
59     @BeforeEach
60     void setUp() throws IOException, JNCException {
61         MockitoAnnotations.initMocks(this);
62         NetconfConnectionParams params = null;
63         Mockito.when(netconfSessionHelper.createNetconfSession(params)).thenReturn(netconfSession);
64         reader = new NetconfConfigurationReader(params, netconfSessionHelper);
65     }
66
67     @Test
68     void properlyReadXML() throws IOException, JNCException {
69         when(netconfSession.getConfig()).thenReturn(nodeSet);
70         when(nodeSet.toXMLString()).thenReturn(EXPECTED_STRING_XML);
71
72         String result = reader.getRunningConfig();
73
74         verify(netconfSession).getConfig();
75         verify(nodeSet).toXMLString();
76         assertThat(result).isEqualTo(EXPECTED_STRING_XML);
77     }
78
79     @Test
80     void shouldProperlyReadXmlByName() throws IOException, JNCException {
81         when(netconfSession.getConfig("/sample:test")).thenReturn(nodeSet);
82         when(nodeSet.first()).thenReturn(element);
83         when(element.toXMLString()).thenReturn(EXPECTED_STRING_XML);
84
85         String result = reader.getRunningConfig("/sample:test");
86
87         verify(netconfSession).getConfig("/sample:test");
88         verify(nodeSet, times(2)).first();
89         verify(element).toXMLString();
90
91         assertThat(result).isEqualTo(EXPECTED_STRING_XML);
92     }
93
94 }