Skip to content

Commit 4c33372

Browse files
author
Bilal Al
committed
Fixing sonar coverage
1 parent 0060f98 commit 4c33372

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

client/src/test/java/io/split/client/HttpSegmentChangeFetcherTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,26 @@ public void testFetcherWithCDNBypassOption() throws IOException, URISyntaxExcept
116116
Assert.assertFalse(captured.get(1).getUri().toString().contains("till="));
117117
}
118118

119+
@Test(expected = IllegalStateException.class)
120+
public void testFetcherWithError() throws IOException, URISyntaxException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
121+
URI rootTarget = URI.create("https://api.split.io");
122+
123+
HttpEntity entityMock = Mockito.mock(HttpEntity.class);
124+
when(entityMock.getContent()).thenReturn(new StringBufferInputStream("{\"till\": 1}"));
125+
ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
126+
when(response.getCode()).thenReturn(400);
127+
when(response.getEntity()).thenReturn(entityMock);
128+
when(response.getHeaders()).thenReturn(new Header[0]);
129+
130+
ArgumentCaptor<ClassicHttpRequest> requestCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class);
131+
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
132+
SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null));
133+
134+
when(httpClientMock.execute(requestCaptor.capture())).thenReturn(TestHelper.classicResponseToCloseableMock(response));
135+
136+
Metrics.NoopMetrics metrics = new Metrics.NoopMetrics();
137+
HttpSegmentChangeFetcher fetcher = HttpSegmentChangeFetcher.create(splitHtpClient, rootTarget, Mockito.mock(TelemetryStorage.class));
138+
139+
fetcher.fetch("someSegment", -1, new FetchOptions.Builder().build());
140+
}
119141
}

client/src/test/java/io/split/client/HttpSplitChangeFetcherTest.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
import java.net.URI;
2929
import java.net.URISyntaxException;
3030
import java.nio.charset.StandardCharsets;
31-
import java.util.HashSet;
32-
import java.util.List;
33-
import java.util.Map;
34-
import java.util.Set;
31+
import java.sql.Array;
32+
import java.util.*;
33+
import java.util.stream.Collectors;
3534

3635
import static org.mockito.Mockito.when;
3736

@@ -143,4 +142,30 @@ public void testRandomNumberGeneration() throws URISyntaxException {
143142

144143
Assert.assertTrue(seen.size() >= (total * 0.9999));
145144
}
145+
146+
@Test(expected = IllegalStateException.class)
147+
public void testURLTooLong() throws IOException, URISyntaxException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
148+
URI rootTarget = URI.create("https://api.split.io");
149+
150+
HttpEntity entityMock = Mockito.mock(HttpEntity.class);
151+
when(entityMock.getContent()).thenReturn(new ByteArrayInputStream("{\"till\": 1}".getBytes(StandardCharsets.UTF_8)));
152+
ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
153+
when(response.getCode()).thenReturn(414);
154+
when(response.getEntity()).thenReturn(entityMock);
155+
when(response.getHeaders()).thenReturn(new Header[0]);
156+
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
157+
ArgumentCaptor<ClassicHttpRequest> requestCaptor = ArgumentCaptor.forClass(ClassicHttpRequest.class);
158+
when(httpClientMock.execute(requestCaptor.capture())).thenReturn(TestHelper.classicResponseToCloseableMock(response));
159+
160+
SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClientMock, new RequestDecorator(null));
161+
HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget, Mockito.mock(TelemetryRuntimeProducer.class));
162+
List<String> sets = new ArrayList<String>();
163+
for (Integer i=0; i<100; i++) {
164+
sets.add("set" + i.toString());
165+
}
166+
String result = sets.stream()
167+
.map(n -> String.valueOf(n))
168+
.collect(Collectors.joining(",", "", ""));
169+
fetcher.fetch(-1, new FetchOptions.Builder().flagSetsFilter(result).cacheControlHeaders(false).build());
170+
}
146171
}

client/src/test/java/io/split/client/events/EventsSenderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package io.split.client.events;
22

3+
import io.split.TestHelper;
34
import io.split.client.RequestDecorator;
5+
import io.split.client.dtos.Event;
46
import io.split.service.SplitHttpClient;
57
import io.split.service.SplitHttpClientImpl;
68
import io.split.telemetry.storage.TelemetryRuntimeProducer;
79
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
10+
import org.apache.hc.core5.http.HttpStatus;
11+
import org.jetbrains.annotations.NotNull;
812
import org.junit.Assert;
913
import org.junit.Test;
1014
import org.mockito.Mockito;
1115

16+
import java.io.IOException;
17+
import java.lang.reflect.InvocationTargetException;
1218
import java.net.URI;
1319
import java.net.URISyntaxException;
20+
import java.util.*;
1421

1522
public class EventsSenderTest {
1623

@@ -48,4 +55,13 @@ public void testCustomURLAppendingPathNoBackslash() throws URISyntaxException {
4855
EventsSender fetcher = EventsSender.create(SPLIT_HTTP_CLIENT, rootTarget, TELEMETRY_RUNTIME_CONSUMER);
4956
Assert.assertEquals("https://kubernetesturl.com/split/api/events/bulk", fetcher.getBulkEndpoint().toString());
5057
}
58+
@Test
59+
public void testHttpError() throws URISyntaxException, IOException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
60+
URI rootTarget = URI.create("https://kubernetesturl.com/split");
61+
CloseableHttpClient httpClient = TestHelper.mockHttpClient("", HttpStatus.SC_BAD_REQUEST);
62+
SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null));
63+
EventsSender sender = EventsSender.create(splitHtpClient, rootTarget, TELEMETRY_RUNTIME_CONSUMER);
64+
// should not raise exception
65+
sender.sendEvents(new ArrayList<>());
66+
}
5167
}

client/src/test/java/io/split/client/impressions/HttpImpressionsSenderTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.lang.reflect.InvocationTargetException;
2828
import java.net.URI;
2929
import java.net.URISyntaxException;
30+
import java.util.ArrayList;
3031
import java.util.Arrays;
3132
import java.util.HashMap;
3233
import java.util.List;
@@ -142,7 +143,7 @@ public void testImpressionBulksEndpoint() throws URISyntaxException, IOException
142143
KeyImpression.fromImpression(new Impression("k1", null, "t1", "on", 123L, "r1", 456L, null)),
143144
KeyImpression.fromImpression(new Impression("k2", null, "t1", "on", 123L, "r1", 456L, null)),
144145
KeyImpression.fromImpression(new Impression("k3", null, "t1", "on", 123L, "r1", 456L, null))
145-
)), new TestImpressions("t2", Arrays.asList(
146+
)), new TestImpressions("t2", Arrays.asList(
146147
KeyImpression.fromImpression(new Impression("k1", null, "t2", "on", 123L, "r1", 456L, null)),
147148
KeyImpression.fromImpression(new Impression("k2", null, "t2", "on", 123L, "r1", 456L, null)),
148149
KeyImpression.fromImpression(new Impression("k3", null, "t2", "on", 123L, "r1", 456L, null))
@@ -160,7 +161,8 @@ public void testImpressionBulksEndpoint() throws URISyntaxException, IOException
160161
HttpPost asPostRequest = (HttpPost) request;
161162
InputStreamReader reader = new InputStreamReader(asPostRequest.getEntity().getContent());
162163
Gson gson = new Gson();
163-
List<TestImpressions> payload = gson.fromJson(reader, new TypeToken<List<TestImpressions>>() { }.getType());
164+
List<TestImpressions> payload = gson.fromJson(reader, new TypeToken<List<TestImpressions>>() {
165+
}.getType());
164166
assertThat(payload.size(), is(equalTo(2)));
165167

166168
// Do the same flow for imrpessionsMode = debug
@@ -175,4 +177,15 @@ public void testImpressionBulksEndpoint() throws URISyntaxException, IOException
175177
assertThat(request.getHeaders().length, is(1));
176178
assertThat(request.getFirstHeader("SplitSDKImpressionsMode").getValue(), is(equalTo("DEBUG")));
177179
}
180+
181+
@Test
182+
public void testHttpError() throws URISyntaxException, IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
183+
URI rootTarget = URI.create("https://kubernetesturl.com/split");
184+
CloseableHttpClient httpClient = TestHelper.mockHttpClient("", HttpStatus.SC_BAD_REQUEST);
185+
SplitHttpClient splitHtpClient = SplitHttpClientImpl.create(httpClient, new RequestDecorator(null));
186+
HttpImpressionsSender sender = HttpImpressionsSender.create(splitHtpClient, rootTarget, ImpressionsManager.Mode.OPTIMIZED, TELEMETRY_STORAGE);
187+
// Should not raise exception
188+
sender.postImpressionsBulk(new ArrayList<>());
189+
sender.postCounters(new HashMap<>());
190+
}
178191
}

0 commit comments

Comments
 (0)