diff options
author | Joss Armstrong <joss.armstrong@ericsson.com> | 2019-03-01 17:20:00 +0000 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2019-03-01 20:01:19 +0000 |
commit | de191b8ade7bd9abc0770ed4e332b10071026fdb (patch) | |
tree | a01b836451a31354c123ad353b90d2ec036aec04 | |
parent | 286805b229460c66a327af25f56fe29e1f5ebcd0 (diff) |
Test coverage for HttpClientTest
Added test coverage for untested class
Increased coverage from 0% to 89%
Issue-ID: APPC-1520
Change-Id: I113d515df3b6f6ef12565cf390f0bd2f2e8ac2f0
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
-rw-r--r-- | appc-core/appc-common-bundle/src/test/java/org/onap/appc/util/HttpClientTest.java | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/appc-core/appc-common-bundle/src/test/java/org/onap/appc/util/HttpClientTest.java b/appc-core/appc-common-bundle/src/test/java/org/onap/appc/util/HttpClientTest.java new file mode 100644 index 000000000..743e26596 --- /dev/null +++ b/appc-core/appc-common-bundle/src/test/java/org/onap/appc/util/HttpClientTest.java @@ -0,0 +1,100 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2019 Ericsson + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import java.io.IOException; +import java.util.Properties; +import org.apache.http.HttpEntity; +import org.apache.http.StatusLine; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.appc.configuration.Configuration; +import org.onap.appc.configuration.ConfigurationFactory; +import org.onap.appc.exceptions.APPCException; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ConfigurationFactory.class, HttpClients.class}) +public class HttpClientTest { + + private CloseableHttpClient client; + private CloseableHttpResponse httpResponse; + private StatusLine statusLine; + private HttpEntity httpEntity; + + + @Before + public void setup() throws ClientProtocolException, IOException { + Configuration configuration = Mockito.mock(Configuration.class); + Properties properties = new Properties(); + properties.put("username", "username"); + properties.put("password", "password"); + Mockito.when(configuration.getProperty("username")).thenReturn("username"); + Mockito.when(configuration.getProperty("password")).thenReturn("password"); + PowerMockito.mockStatic(ConfigurationFactory.class); + PowerMockito.when(ConfigurationFactory.getConfiguration()).thenReturn(configuration); + client = Mockito.mock(CloseableHttpClient.class); + PowerMockito.mockStatic(HttpClients.class); + HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class); + PowerMockito.when(HttpClients.custom()).thenReturn(httpClientBuilder); + PowerMockito.when(httpClientBuilder.build()).thenReturn(client); + httpResponse = Mockito.mock(CloseableHttpResponse.class); + statusLine = Mockito.mock(StatusLine.class); + Mockito.when(statusLine.getStatusCode()).thenReturn(300); + Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine); + httpEntity = Mockito.mock(HttpEntity.class); + Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity); + Mockito.when(client.execute(Mockito.any())).thenReturn(httpResponse); + } + + @Test + public void testPostMethod() throws APPCException { + assertEquals(300, httpClient.postMethod("http", "127.0.0.1", 22, "/path", "{}", "application/json")); + } + + @Test + public void testPutMethod() throws APPCException { + assertEquals(300, httpClient.putMethod("http", "127.0.0.1", 22, "/path", "{}", "application/json")); + } + + @Test + public void testGetMethod() throws APPCException { + assertNull(httpClient.getMethod("http", "127.0.0.1", 22, "/path", "application/json")); + } + + @Test + public void testDeleteMethod() throws APPCException { + assertEquals(300, httpClient.deleteMethod("http", "127.0.0.1", 22, "/path", "application/json")); + } +} |