Change parent version to snapshot
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / test / java / org / onap / appc / instar / node / TestDme2Client.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.instar.node;
27
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 java.io.InputStream;
33 import java.net.URI;
34 import java.security.NoSuchAlgorithmException;
35 import java.util.HashMap;
36 import java.util.Properties;
37 import javax.net.ssl.SSLContext;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.appc.instar.dme2client.Dme2Client;
43 import org.onap.appc.instar.utils.InstarClientConstant;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.powermock.reflect.Whitebox;
48 import com.sun.jersey.api.client.Client;
49 import com.sun.jersey.api.client.ClientResponse;
50 import com.sun.jersey.api.client.WebResource;
51 import com.sun.jersey.api.client.WebResource.Builder;
52
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({InstarClientConstant.class, SSLContext.class, Client.class})
55 public class TestDme2Client {
56
57   private Dme2Client dme2;
58   private InputStream inputStream;
59   private SSLContext sslContext;
60   private Properties properties;
61   private Client client;
62   private WebResource webResource;
63   private Builder builder;
64   private ClientResponse clientResponse;
65
66   @Before
67   public void setUp() throws Exception {
68     inputStream = Mockito.mock(InputStream.class);
69     sslContext = PowerMockito.mock(SSLContext.class);
70     client = Mockito.mock(Client.class);
71     builder = Mockito.mock(Builder.class);
72     clientResponse = Mockito.mock(ClientResponse.class);
73     webResource = Mockito.mock(WebResource.class);
74     HashMap<String, String> data = new HashMap<String, String>();
75     data.put("subtext", "value");
76     PowerMockito.mockStatic(InstarClientConstant.class);
77     PowerMockito.mockStatic(SSLContext.class);
78     PowerMockito.mockStatic(Client.class);
79     PowerMockito.when(InstarClientConstant.getEnvironmentVariable("SDNC_CONFIG_DIR"))
80         .thenReturn("test");
81     PowerMockito.when(InstarClientConstant.getInputStream("test/outbound.properties"))
82         .thenReturn(inputStream);
83     PowerMockito.when(SSLContext.getInstance("SSL")).thenReturn(sslContext);
84     PowerMockito.when(Client.create(anyObject())).thenReturn(client);
85     PowerMockito.when(client.resource(new URI("nullnullnullvalue"))).thenReturn(webResource);
86
87     PowerMockito.when(builder.get(ClientResponse.class)).thenReturn(clientResponse);
88     properties = Mockito.mock(Properties.class);
89     dme2 = new Dme2Client("opt", "subtext", data);
90     Whitebox.setInternalState(dme2, "properties", properties);
91     when(properties.getProperty("MechID")).thenReturn("123");
92     when(properties.getProperty("MechPass")).thenReturn("password");
93   }
94
95   @Test
96   public void testSendtoInstarGet() throws Exception {
97     PowerMockito.when(webResource.accept("application/json")).thenReturn(builder);
98     PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Get Success");
99     when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET");
100     assertEquals("Get Success", dme2.send());
101   }
102
103   @Test
104   public void testSendtoInstarPut() throws Exception {
105     PowerMockito.when(webResource.type("application/json")).thenReturn(builder);
106     PowerMockito.when(builder.put(ClientResponse.class, "")).thenReturn(clientResponse);
107     PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Put Success");
108     when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("PUT");
109     assertEquals("Put Success", dme2.send());
110   }
111
112   @Test
113   public void testSendtoInstarPost() throws Exception {
114     PowerMockito.when(webResource.type("application/json")).thenReturn(builder);
115     PowerMockito.when(builder.post(ClientResponse.class, "")).thenReturn(clientResponse);
116     PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Post Success");
117     when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("POST");
118     assertEquals("Post Success", dme2.send());
119   }
120
121   @Test
122   public void testSendtoInstarDelete() throws Exception {
123     PowerMockito.when(webResource.delete(ClientResponse.class)).thenReturn(clientResponse);
124     PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Delete Success");
125     when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("DELETE");
126     assertEquals("Delete Success", dme2.send());
127   }
128
129   @Test
130   public void testSendtoInstarException() throws Exception {
131     PowerMockito.when(SSLContext.getInstance("SSL")).thenThrow(new NoSuchAlgorithmException());
132     when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("DELETE");
133     assertNull(dme2.send());
134   }
135
136   @Test
137   public void testSendtoInstarMaskNotNull() throws Exception {
138     Whitebox.setInternalState(dme2, "mask", "0.0.0.0/1");
139     PowerMockito.when(webResource.accept("application/json")).thenReturn(builder);
140     PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Get Success");
141     when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET");
142     assertNull(dme2.send());
143   }
144
145   @Test
146   public void testSendtoInstarIpNotNull() throws Exception {
147     Whitebox.setInternalState(dme2, "ipAddress", "0.0.0.0");
148     PowerMockito.when(webResource.accept("application/json")).thenReturn(builder);
149     PowerMockito.when(clientResponse.getEntity(String.class)).thenReturn("Get Success");
150     when(properties.getProperty("getIpAddressByVnf_method")).thenReturn("GET");
151     assertNull(dme2.send());
152   }
153 }