Skip to content

Commit c0c1c5c

Browse files
committed
Fix tests and add more tests.
1 parent 90ee6a1 commit c0c1c5c

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

firebase-config/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigTest.java

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
import static org.mockito.ArgumentMatchers.eq;
3131
import static org.mockito.Matchers.any;
3232
import static org.mockito.Mockito.doAnswer;
33+
import static org.mockito.Mockito.doNothing;
34+
import static org.mockito.Mockito.doReturn;
3335
import static org.mockito.Mockito.doThrow;
3436
import static org.mockito.Mockito.never;
37+
import static org.mockito.Mockito.spy;
3538
import static org.mockito.Mockito.times;
3639
import static org.mockito.Mockito.verify;
3740
import static org.mockito.Mockito.when;
@@ -65,6 +68,7 @@
6568
import com.google.firebase.remoteconfig.internal.ConfigGetParameterHandler;
6669
import com.google.firebase.remoteconfig.internal.ConfigMetadataClient;
6770
import com.google.firebase.remoteconfig.internal.ConfigRealtimeHandler;
71+
import com.google.firebase.remoteconfig.internal.ConfigRealtimeHttpClient;
6872
import com.google.firebase.remoteconfig.internal.Personalization;
6973
import java.io.ByteArrayInputStream;
7074
import java.io.IOException;
@@ -140,6 +144,7 @@ public final class FirebaseRemoteConfigTest {
140144
@Mock private ConfigMetadataClient metadataClient;
141145

142146
@Mock private ConfigRealtimeHandler mockConfigRealtimeHandler;
147+
@Mock private ConfigAutoFetch mockConfigAutoFetch;
143148
@Mock private ConfigUpdateListenerRegistration mockRealtimeRegistration;
144149
@Mock private HttpURLConnection mockHttpURLConnection;
145150
@Mock private ConfigUpdateListener mockListener;
@@ -165,6 +170,7 @@ public final class FirebaseRemoteConfigTest {
165170
private FetchResponse realtimeFetchedContainerResponse;
166171
private ConfigContainer realtimeFetchedContainer;
167172
private ConfigAutoFetch configAutoFetch;
173+
private ConfigRealtimeHttpClient configRealtimeHttpClient;
168174

169175
private FetchResponse firstFetchedContainerResponse;
170176

@@ -269,6 +275,14 @@ public void setUp() throws Exception {
269275
listeners.add(mockListener);
270276
configAutoFetch =
271277
new ConfigAutoFetch(mockHttpURLConnection, mockFetchHandler, listeners, mockRetryListener);
278+
configRealtimeHttpClient =
279+
new ConfigRealtimeHttpClient(
280+
firebaseApp,
281+
mockFirebaseInstallations,
282+
mockFetchHandler,
283+
context,
284+
"firebase",
285+
listeners);
272286
}
273287

274288
@Test
@@ -1107,7 +1121,6 @@ public void realtime_client_removeListener_success() {
11071121

11081122
@Test
11091123
public void realtime_stream_listen_and_retry_success() throws Exception {
1110-
when(mockHttpURLConnection.getResponseCode()).thenReturn(200);
11111124
when(mockHttpURLConnection.getInputStream())
11121125
.thenReturn(
11131126
new ByteArrayInputStream(
@@ -1121,17 +1134,60 @@ public void realtime_stream_listen_and_retry_success() throws Exception {
11211134

11221135
@Test
11231136
public void realtime_stream_listen_fail() throws Exception {
1124-
when(mockHttpURLConnection.getResponseCode()).thenReturn(400);
1125-
when(mockHttpURLConnection.getInputStream())
1126-
.thenReturn(
1127-
new ByteArrayInputStream(
1128-
"{\\r\\n \\\"latestTemplateVersionNumber\\\": 1\\r\\n}"
1129-
.getBytes(StandardCharsets.UTF_8)));
1130-
when(mockFetchHandler.getTemplateVersionNumber()).thenReturn(1L);
1131-
when(mockFetchHandler.fetch(0)).thenReturn(Tasks.forResult(realtimeFetchedContainerResponse));
1137+
when(mockHttpURLConnection.getInputStream()).thenThrow(IOException.class);
11321138
configAutoFetch.listenForNotifications();
11331139

1134-
verify(mockListener).onError(any(FirebaseRemoteConfigRealtimeUpdateStreamException.class));
1140+
verify(mockListener).onError(any(FirebaseRemoteConfigRealtimeUpdateFetchException.class));
1141+
}
1142+
1143+
@Test
1144+
public void realtime_redirectStatusCode_noRetries() throws Exception {
1145+
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
1146+
doReturn(mockHttpURLConnection).when(configRealtimeHttpClientSpy).createRealtimeConnection();
1147+
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
1148+
when(mockHttpURLConnection.getResponseCode()).thenReturn(301);
1149+
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1150+
verify(configRealtimeHttpClientSpy, never()).startAutoFetch(any());
1151+
verify(configRealtimeHttpClientSpy, never()).retryHTTPConnection();
1152+
}
1153+
1154+
@Test
1155+
public void realtime_okStatusCode_startAutofetchAndRetries() throws Exception {
1156+
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
1157+
doReturn(mockHttpURLConnection).when(configRealtimeHttpClientSpy).createRealtimeConnection();
1158+
doReturn(mockConfigAutoFetch).when(configRealtimeHttpClientSpy).startAutoFetch(any());
1159+
doNothing().when(configRealtimeHttpClientSpy).retryHTTPConnection();
1160+
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
1161+
when(mockHttpURLConnection.getResponseCode()).thenReturn(200);
1162+
1163+
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1164+
verify(mockConfigAutoFetch).listenForNotifications();
1165+
verify(configRealtimeHttpClientSpy).retryHTTPConnection();
1166+
}
1167+
1168+
@Test
1169+
public void realtime_badGatewayStatusCode_noAutofetchButRetries() throws Exception {
1170+
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
1171+
doReturn(mockHttpURLConnection).when(configRealtimeHttpClientSpy).createRealtimeConnection();
1172+
doNothing().when(configRealtimeHttpClientSpy).retryHTTPConnection();
1173+
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
1174+
when(mockHttpURLConnection.getResponseCode()).thenReturn(502);
1175+
1176+
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1177+
verify(configRealtimeHttpClientSpy, never()).startAutoFetch(any());
1178+
verify(configRealtimeHttpClientSpy).retryHTTPConnection();
1179+
}
1180+
1181+
@Test
1182+
public void realtime_exceptionThrown_noAutofetchButRetries() throws Exception {
1183+
ConfigRealtimeHttpClient configRealtimeHttpClientSpy = spy(configRealtimeHttpClient);
1184+
doThrow(IOException.class).when(configRealtimeHttpClientSpy).createRealtimeConnection();
1185+
doNothing().when(configRealtimeHttpClientSpy).retryHTTPConnection();
1186+
doNothing().when(configRealtimeHttpClientSpy).closeRealtimeHttpStream();
1187+
1188+
configRealtimeHttpClientSpy.beginRealtimeHttpStream();
1189+
verify(configRealtimeHttpClientSpy, never()).startAutoFetch(any());
1190+
verify(configRealtimeHttpClientSpy).retryHTTPConnection();
11351191
}
11361192

11371193
@Test

0 commit comments

Comments
 (0)