Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,15 @@
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -209,7 +215,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.14</version>
<version>0.8.7</version>
<executions>
<execution>
<id>prepare-agent</id>
Expand Down Expand Up @@ -295,7 +301,7 @@
<plugin>
<groupId>org.sonatype.central</groupId>
<artifactId>central-publishing-maven-plugin</artifactId>
<version>0.10.0</version>
<version>0.4.0</version>
<extensions>true</extensions>
<configuration>
<publishingServerId>central</publishingServerId>
Expand Down
21 changes: 15 additions & 6 deletions src/test/java/com/skyflow/utils/HttpUtilityTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -24,10 +26,13 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;

@RunWith(MockitoJUnitRunner.class)
@RunWith(PowerMockRunner.class)
@PrepareForTest({URL.class, HttpURLConnection.class})
public class HttpUtilityTests {

private static String INVALID_EXCEPTION_THROWN = "Should not have thrown any exception";
@InjectMocks
HttpUtility httpUtility;
@Mock
OutputStream outputStream;
private String expected;
Expand Down Expand Up @@ -55,14 +60,15 @@ protected URLConnection openConnection(final URL arg0) throws IOException {
}

@Test
@PrepareForTest({URL.class, HttpURLConnection.class})
public void testSendRequest() {
try {
given(mockConnection.getRequestProperty("content-type")).willReturn("application/json");
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/json");
JsonObject params = new JsonObject();
params.addProperty("key", "value");
String response = HttpUtility.sendRequest("GET", url, params, headers);
String response = httpUtility.sendRequest("GET", url, params, headers);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
Expand All @@ -71,40 +77,43 @@ public void testSendRequest() {


@Test
@PrepareForTest({URL.class, HttpURLConnection.class})
public void testSendRequestFormData() {
try {
given(mockConnection.getRequestProperty("content-type")).willReturn("multipart/form-data");
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "multipart/form-data");
JsonObject params = new JsonObject();
params.addProperty("key", "value");
String response = HttpUtility.sendRequest("GET", url, params, headers);
String response = httpUtility.sendRequest("GET", url, params, headers);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}

@Test
@PrepareForTest({URL.class, HttpURLConnection.class})
public void testSendRequestFormURLEncoded() {
try {
given(mockConnection.getRequestProperty("content-type")).willReturn("application/x-www-form-urlencoded");
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/x-www-form-urlencoded");
JsonObject params = new JsonObject();
params.addProperty("key", "value");
String response = HttpUtility.sendRequest("GET", url, params, headers);
String response = httpUtility.sendRequest("GET", url, params, headers);
Assert.assertEquals(expected, response);
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
}

@Test
@PrepareForTest({URL.class, HttpURLConnection.class})
public void testSendRequestError() {
try {
given(mockConnection.getResponseCode()).willReturn(500);
String response = HttpUtility.sendRequest("GET", url, null, null);
String response = httpUtility.sendRequest("GET", url, null, null);
} catch (SkyflowException e) {
Assert.assertEquals(500, e.getHttpCode());
Assert.assertEquals(new Integer(123), e.getGrpcCode());
Expand Down
Loading