2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.instar.node;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNull;
30 import static org.mockito.Matchers.anyObject;
31 import static org.mockito.Mockito.when;
32 import static org.mockito.Matchers.eq;
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Matchers.anyString;
35 import static org.mockito.Mockito.doReturn;
36 import java.io.InputStream;
38 import java.security.NoSuchAlgorithmException;
39 import java.util.HashMap;
40 import java.util.Properties;
41 import javax.net.ssl.SSLContext;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mockito;
46 import org.onap.appc.instar.dme2client.Dme2Client;
47 import org.onap.appc.instar.utils.InstarClientConstant;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51 import static org.powermock.api.mockito.PowerMockito.mockStatic;
52 import static javax.ws.rs.core.Response.Status.FORBIDDEN;
53 import static javax.ws.rs.core.Response.Status.OK;
54 import static javax.ws.rs.core.Response.ResponseBuilder;
56 import org.powermock.reflect.Whitebox;
57 import javax.ws.rs.client.Client;
58 import javax.ws.rs.client.WebTarget;
59 import javax.ws.rs.client.Invocation;
60 import javax.ws.rs.core.Response;
61 import javax.ws.rs.client.Entity;
62 import javax.ws.rs.client.ClientBuilder;
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({InstarClientConstant.class, SSLContext.class, Client.class,ClientBuilder.class})
66 public class TestDme2Client {
68 private static final String ANY = "notNullString";
69 private Dme2Client dme2;
70 private InputStream inputStream;
71 private SSLContext sslContext;
72 private Properties properties;
73 private Client client;
74 private WebTarget webResource;
75 private Response clientResponse;
76 private ClientBuilder clientBuilder;
78 private Invocation.Builder builder;
82 public void setUp() throws Exception {
83 inputStream = Mockito.mock(InputStream.class);
85 sslContext = PowerMockito.mock(SSLContext.class);
87 client = Mockito.mock(Client.class);
88 builder = Mockito.mock(Invocation.Builder.class);
89 clientResponse = Mockito.mock(Response.class);
90 webResource = Mockito.mock(WebTarget.class);
91 clientBuilder = Mockito.mock(ClientBuilder.class);
93 HashMap<String, String> data = new HashMap<String, String>();
94 data.put("subtext", "value");
97 mockStatic(InstarClientConstant.class);
98 PowerMockito.when(InstarClientConstant.getEnvironmentVariable("SDNC_CONFIG_DIR"))
102 PowerMockito.when(InstarClientConstant.getInputStream("test/outbound.properties"))
103 .thenReturn(inputStream);
105 mockStatic(SSLContext.class);
106 PowerMockito.when(SSLContext.getInstance("SSL")).thenReturn(sslContext);
108 mockStatic(ClientBuilder.class);
110 PowerMockito.when(ClientBuilder.newBuilder()).thenReturn(clientBuilder);
111 doReturn(clientBuilder).when(clientBuilder).sslContext(any());
112 doReturn(clientBuilder).when(clientBuilder).hostnameVerifier(any());
114 PowerMockito.when(clientBuilder.build()).thenReturn(client);
116 PowerMockito.when(client.target(any(URI.class))).thenReturn(webResource);
119 PowerMockito.when(webResource.request(eq("Content-Type"),anyString())).thenReturn(builder);
120 PowerMockito.when(webResource.request(anyString())).thenReturn(builder);
122 PowerMockito.when(builder.get(eq(Response.class))).thenReturn(clientResponse);
124 properties = Mockito.mock(Properties.class);
125 dme2 = new Dme2Client("opt", "subtext", data);
127 Whitebox.setInternalState(dme2, "properties", properties);
128 when(properties.getProperty("MechID")).thenReturn("123");
129 when(properties.getProperty("MechPass")).thenReturn("password");
133 public void testSendtoInstarGet() throws Exception {
134 PowerMockito.when(webResource.request("application/json")).thenReturn(builder);
135 PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn("Get Success");
136 when(properties.getProperty(eq("getIpAddressByVnf_method"))).thenReturn("GET");
138 assertEquals("Get Success", dme2.send());
142 public void testSendtoInstarPut() throws Exception {
144 PowerMockito.when(builder.put(any(Entity.class),eq(Response.class))).thenReturn(clientResponse);
146 PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn("Put Success");
148 when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("PUT");
149 assertEquals("Put Success", dme2.send());
153 public void testSendtoInstarPost() throws Exception {
154 ResponseBuilder responseBuilder = clientResponse.ok();
155 responseBuilder.encoding("Post Success").build();
156 PowerMockito.when(builder.post(any(Entity.class),eq(Response.class))).thenReturn(clientResponse);
157 PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn("Post Success");
158 when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("POST");
159 assertEquals("Post Success", dme2.send());
163 public void testSendtoInstarDelete() throws Exception {
164 ResponseBuilder responseBuilder = Response.ok();
165 PowerMockito.when(webResource.request(anyString()).delete(eq(Response.class))).thenReturn(clientResponse);
166 PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn("Delete Success");
167 when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("DELETE");
168 assertNull(dme2.send());
172 public void testSendtoInstarException() throws Exception {
173 PowerMockito.when(SSLContext.getInstance("SSL")).thenThrow(new NoSuchAlgorithmException());
174 when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("DELETE");
175 assertNull(dme2.send());
179 public void testSendtoInstarMaskNotNull() throws Exception {
180 Whitebox.setInternalState(dme2, "mask", "0.0.0.0/1");
181 PowerMockito.when(webResource.request("application/json")).thenReturn(builder);
182 PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn(null);
183 when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET");
184 assertNull(dme2.send());
188 public void testSendtoInstarIpNotNull() throws Exception {
189 Whitebox.setInternalState(dme2, "ipAddress", "0.0.0.0");
190 PowerMockito.when(webResource.request("application/json")).thenReturn(builder);
191 PowerMockito.when(clientResponse.readEntity(String.class)).thenReturn(null);
192 when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET");
193 assertNull(dme2.send());