Test coverage in netconf-oldconnector
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / test / java / org / onap / appc / adapter / netconf / odlconnector / NetconfClientRestconfImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Ericsson
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  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.adapter.netconf.odlconnector;
23
24 import static org.junit.Assert.assertEquals;
25 import java.util.Properties;
26 import org.apache.http.HttpStatus;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.onap.appc.exceptions.APPCException;
34 import org.onap.appc.util.httpClient;
35 import org.onap.appc.adapter.netconf.NetconfConnectionDetails;
36 import org.onap.appc.adapter.netconf.util.Constants;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
40 import org.powermock.reflect.Whitebox;
41
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest(httpClient.class)
44 public class NetconfClientRestconfImplTest {
45
46     @Rule
47     public ExpectedException expectedEx = ExpectedException.none();
48
49     @Before
50     public void setup() {
51         PowerMockito.mockStatic(httpClient.class);
52     }
53     @Test
54     public void testConfigureNullDetails() throws APPCException {
55         NetconfClientRestconfImpl client = new NetconfClientRestconfImpl();
56         expectedEx.expect(APPCException.class);
57         expectedEx.expectMessage("Invalid connection details - null value");
58         client.configure(null);
59     }
60
61     @Test
62     public void testConfigureNullProperties() throws APPCException {
63         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
64         expectedEx.expect(APPCException.class);
65         expectedEx.expectMessage("Invalid properties!");
66         Whitebox.setInternalState(client, "connectionDetails", Mockito.mock(NetconfConnectionDetails.class));
67         client.configure(null);
68     }
69
70     @Test
71     public void testConfigureWithError() throws APPCException {
72         PowerMockito.when(httpClient.putMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
73                 Constants.CONFIGURE_PATH + "null/yang-ext:mount/MODULE_NAME:NODE_NAME", null, "application/xml"))
74                 .thenReturn(HttpStatus.SC_ACCEPTED);
75         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
76         NetconfConnectionDetails details = new NetconfConnectionDetails();
77         Properties properties = new Properties();
78         properties.setProperty("module.name", "MODULE_NAME");
79         properties.setProperty("node.name", "NODE_NAME");
80         details.setAdditionalProperties(properties);
81         Whitebox.setInternalState(client, "connectionDetails", details);
82         expectedEx.expect(APPCException.class);
83         expectedEx.expectMessage("Error configuring node :NODE_NAME, of Module :MODULE_NAME, in device :null");
84         client.configure(null);
85     }
86
87     @Test
88     public void testConfigure4ArgWithError() throws APPCException {
89         PowerMockito.when(httpClient.putMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
90                 Constants.CONFIGURE_PATH + "null/yang-ext:mount/MODULE_NAME:NODE_NAME", null, "application/xml"))
91                 .thenReturn(HttpStatus.SC_ACCEPTED);
92         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
93         expectedEx.expect(APPCException.class);
94         expectedEx.expectMessage("Error configuring node :NODE_NAME, of Module :MODULE_NAME, in device :null");
95         client.configure(null, null, "MODULE_NAME", "NODE_NAME");
96     }
97
98     @Test
99     public void testConnect() throws APPCException {
100         PowerMockito.when(httpClient.putMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
101                 Constants.CONFIGURE_PATH + "null/yang-ext:mount/MODULE_NAME:NODE_NAME", null, "application/xml"))
102                 .thenReturn(HttpStatus.SC_ACCEPTED);
103         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
104         NetconfConnectionDetails details = new NetconfConnectionDetails();
105         Properties properties = new Properties();
106         properties.setProperty("module.name", "MODULE_NAME");
107         properties.setProperty("node.name", "NODE_NAME");
108         details.setAdditionalProperties(properties);
109         Whitebox.setInternalState(client, "connectionDetails", details);
110         expectedEx.expect(APPCException.class);
111         expectedEx.expectMessage("Error connecting device :null");
112         client.connect(details);
113     }
114
115     @Test
116     public void testConnectWithNullDetails() throws APPCException {
117         PowerMockito.when(httpClient.putMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
118                 Constants.CONFIGURE_PATH + "null/yang-ext:mount/MODULE_NAME:NODE_NAME", null, "application/xml"))
119                 .thenReturn(HttpStatus.SC_ACCEPTED);
120         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
121         expectedEx.expect(APPCException.class);
122         expectedEx.expectMessage("Invalid connection details - null value");
123         client.connect(null);
124     }
125
126     @Test
127     public void testDisconnectNullDetails() throws APPCException {
128         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
129         expectedEx.expect(APPCException.class);
130         expectedEx.expectMessage("Invalid connection details - null value");
131         client.disconnect();
132     }
133
134     @Test
135     public void testDisconnect() throws APPCException {
136         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
137         NetconfConnectionDetails details = new NetconfConnectionDetails();
138         Properties properties = new Properties();
139         properties.setProperty("module.name", "MODULE_NAME");
140         properties.setProperty("node.name", "NODE_NAME");
141         details.setAdditionalProperties(properties);
142         Whitebox.setInternalState(client, "connectionDetails", details);
143         expectedEx.expect(APPCException.class);
144         expectedEx.expectMessage("Disconnection of device null failed!");
145         client.disconnect();
146     }
147
148     @Test
149     public void testGetConfigurationNullDetails() throws APPCException {
150         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
151         expectedEx.expect(APPCException.class);
152         expectedEx.expectMessage("Invalid connection details - null value");
153         client.getConfiguration();
154     }
155
156     @Test
157     public void testGetConfigurationNullProperties() throws APPCException {
158         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
159         expectedEx.expect(APPCException.class);
160         expectedEx.expectMessage("Invalid properties!");
161         Whitebox.setInternalState(client, "connectionDetails", Mockito.mock(NetconfConnectionDetails.class));
162         client.getConfiguration();
163     }
164
165     @Test
166     public void testGetConfigurationWithError() throws APPCException {
167         PowerMockito.when(httpClient.putMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
168                 Constants.CONFIGURE_PATH + "null/yang-ext:mount/MODULE_NAME:NODE_NAME", null, "application/xml"))
169                 .thenReturn(HttpStatus.SC_ACCEPTED);
170         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
171         NetconfConnectionDetails details = new NetconfConnectionDetails();
172         Properties properties = new Properties();
173         properties.setProperty("module.name", "MODULE_NAME");
174         properties.setProperty("node.name", "NODE_NAME");
175         details.setAdditionalProperties(properties);
176         Whitebox.setInternalState(client, "connectionDetails", details);
177         expectedEx.expect(APPCException.class);
178         expectedEx.expectMessage("Error getting configuration of node :NODE_NAME, of Module :MODULE_NAME, in device :null");
179         client.getConfiguration();
180     }
181
182     @Test
183     public void testGetConfigurationSuccess() throws APPCException {
184         PowerMockito.when(httpClient.getMethod(Constants.PROTOCOL, Constants.CONTROLLER_IP, Constants.CONTROLLER_PORT,
185                 Constants.CONFIGURE_PATH + "null/yang-ext:mount/MODULE_NAME:NODE_NAME", "application/json"))
186                 .thenReturn("TEST");
187         NetconfClientRestconfImpl client = Mockito.spy(new NetconfClientRestconfImpl());
188         NetconfConnectionDetails details = new NetconfConnectionDetails();
189         Properties properties = new Properties();
190         properties.setProperty("module.name", "MODULE_NAME");
191         properties.setProperty("node.name", "NODE_NAME");
192         details.setAdditionalProperties(properties);
193         Whitebox.setInternalState(client, "connectionDetails", details);
194         assertEquals("TEST", client.getConfiguration());
195     }
196     
197     @Test
198     public void testCheckConnection() throws APPCException {
199         NetconfClientRestconfImpl client = new NetconfClientRestconfImpl();
200         assertEquals(false, client.checkConnection(null));
201
202     }
203 }