Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Al committed Aug 13, 2024
1 parent fa8de3f commit 2234a3c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
6 changes: 5 additions & 1 deletion client/src/main/java/io/split/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
import pluggable.CustomStorageWrapper;

import java.io.IOException;
import java.util.*;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ThreadFactory;
import java.util.Locale;
import java.io.InputStream;

import static io.split.inputValidation.FlagSetsValidator.cleanup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public synchronized SplitHttpResponse post(URI uri, HttpEntity entity, Map<Strin
postHttpURLConnection = (HttpURLConnection) uri.toURL().openConnection();
return _post(postHttpURLConnection, entity, additionalHeaders);
} catch (Exception e) {
throw new IOException(String.format("Problem in http post operation: %s", e), e);
throw new IllegalStateException(String.format("Problem in http post operation: %s", e), e);
} finally {
try {
postHttpURLConnection.disconnect();
Expand Down Expand Up @@ -165,7 +165,7 @@ public SplitHttpResponse _post(HttpURLConnection postHttpURLConnection,
}
return new SplitHttpResponse(responseCode, statusMessage, "", getResponseHeaders(postHttpURLConnection));
} catch (Exception e) {
throw new IOException(String.format("Problem in http post operation: %s", e), e);
throw new IllegalStateException(String.format("Problem in http post operation: %s", e), e);
}
}

Expand Down Expand Up @@ -209,6 +209,6 @@ private Header[] getResponseHeaders(HttpURLConnection urlConnection) {
}
@Override
public void close() throws IOException {
// _client.close();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void testGetError() throws URISyntaxException, IOException {

Mockito.when(mockHttpURLConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
ByteArrayInputStream stubInputStream = new ByteArrayInputStream(Files.asCharSource(
new File("/Users/bilalal-shahwany/repos/java/kerberos/java-client/client/src/test/resources/split-change-special-characters.json"), Charsets.UTF_8).read().getBytes(Charsets.UTF_8));
new File("src/test/resources/split-change-special-characters.json"), Charsets.UTF_8).read().getBytes(Charsets.UTF_8));
when(mockHttpURLConnection.getInputStream()).thenReturn(stubInputStream);

SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
Expand All @@ -111,13 +111,16 @@ public void testGetError() throws URISyntaxException, IOException {
@Test(expected = IllegalStateException.class)
public void testException() throws URISyntaxException, InvocationTargetException, NoSuchMethodException,
IllegalAccessException, IOException {
URI rootTarget = URI.create("https://api.split.io/splitChanges?since=1234567");
CloseableHttpClient httpClientMock = TestHelper.mockHttpClient("split-change-special-characters.json",
HttpStatus.SC_INTERNAL_SERVER_ERROR);
HttpURLConnection mockHttpURLConnection = Mockito.mock(HttpURLConnection.class);
RequestDecorator decorator = null;

SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, decorator, "qwerty", metadata());
splitHtpClient.get(rootTarget,
Mockito.when(mockHttpURLConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
ByteArrayInputStream stubInputStream = new ByteArrayInputStream(Files.asCharSource(
new File("src/test/resources/split-change-special-characters.json"), Charsets.UTF_8).read().getBytes(Charsets.UTF_8));
when(mockHttpURLConnection.getInputStream()).thenReturn(stubInputStream);

SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._get(mockHttpURLConnection,
new FetchOptions.Builder().cacheControlHeaders(true).build(), null);
}

Expand Down Expand Up @@ -178,11 +181,13 @@ public void testPotParameters() throws URISyntaxException, IOException {
assertThat(connectionCaptor.getValue().getURL().toString(), is(equalTo(new URL("https://kubernetesturl.com/split/api/testImpressions/bulk").toString())));
}

@Test(expected = IOException.class)
public void testPosttException() throws URISyntaxException, IOException {
@Test
public void testPosttError() throws URISyntaxException, IOException {
HttpURLConnection mockHttpURLConnection = Mockito.mock(HttpURLConnection.class);
RequestDecorator decorator = new RequestDecorator(null);
ByteArrayOutputStream mockOs = Mockito.mock( ByteArrayOutputStream.class);
Mockito.when(mockHttpURLConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
when(mockHttpURLConnection.getOutputStream()).thenReturn(mockOs);

SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._post(mockHttpURLConnection,
Expand All @@ -191,6 +196,17 @@ public void testPosttException() throws URISyntaxException, IOException {
Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, (long) splitHttpResponse.statusCode());
}

@Test(expected = IllegalStateException.class)
public void testPosttException() throws URISyntaxException, IOException {
HttpURLConnection mockHttpURLConnection = Mockito.mock(HttpURLConnection.class);
RequestDecorator decorator = null;
Mockito.when(mockHttpURLConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);

SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(decorator, "qwerty", metadata());
SplitHttpResponse splitHttpResponse = splitHtpClientKerberos._post(mockHttpURLConnection,
Utils.toJsonEntity(Arrays.asList(new String[] { "A", "B", "C", "D" })), null);
}

private SDKMetadata metadata() {
return new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP");
}
Expand Down

0 comments on commit 2234a3c

Please sign in to comment.