Skip to content

Commit e436837

Browse files
author
Varun Rathore
committed
added UTs for remoteConfig and client
1 parent bfffb3d commit e436837

File tree

2 files changed

+104
-7
lines changed

2 files changed

+104
-7
lines changed

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertTrue;
2323
import static org.junit.Assert.fail;
2424

25+
import com.google.firebase.testing.TestUtils;
2526
import com.google.firebase.ErrorCode;
2627
import com.google.firebase.FirebaseApp;
2728
import com.google.firebase.FirebaseOptions;
@@ -44,6 +45,10 @@ public class FirebaseRemoteConfigTest {
4445
private static final FirebaseRemoteConfigException TEST_EXCEPTION =
4546
new FirebaseRemoteConfigException(ErrorCode.INTERNAL, "Test error message");
4647

48+
private static final String MOCK_SERVER_TEMPLATE_RESPONSE = TestUtils
49+
.loadResource("getServerRemoteConfig.json");
50+
51+
4752
@After
4853
public void tearDown() {
4954
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
@@ -80,6 +85,19 @@ public void testDefaultRemoteConfigClient() {
8085
assertEquals(expectedUrl, ((FirebaseRemoteConfigClientImpl) client).getRemoteConfigUrl());
8186
}
8287

88+
@Test
89+
public void testDefaultServerRemoteConfigClient() {
90+
FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app");
91+
FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(app);
92+
93+
FirebaseRemoteConfigClient client = remoteConfig.getRemoteConfigClient();
94+
95+
assertTrue(client instanceof FirebaseRemoteConfigClientImpl);
96+
assertSame(client, remoteConfig.getRemoteConfigClient());
97+
String expectedUrl = "https://firebaseremoteconfig.googleapis.com/v1/projects/test-project/namespaces/firebase-server/serverRemoteConfig";
98+
assertEquals(expectedUrl, ((FirebaseRemoteConfigClientImpl) client).getServerRemoteConfigUrl());
99+
}
100+
83101
@Test
84102
public void testAppDelete() {
85103
FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app");
@@ -597,4 +615,76 @@ private FirebaseRemoteConfig getRemoteConfig(FirebaseRemoteConfigClient client)
597615
FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS);
598616
return new FirebaseRemoteConfig(app, client);
599617
}
618+
619+
620+
// Get Server template tests
621+
622+
@Test
623+
public void testGetServerTemplate() throws FirebaseRemoteConfigException {
624+
String TEST_SERVER_TEMPLATE = "{\n" +
625+
" \"etag\": \"etag-123456789012-1\",\n" +
626+
" \"parameters\": {},\n" +
627+
" \"serverConditions\": [],\n" +
628+
" \"parameterGroups\": {}\n" +
629+
"}";
630+
MockRemoteConfigClient client = MockRemoteConfigClient.fromServerTemplate(
631+
new ServerTemplateData().setETag(TEST_ETAG).toJSON());
632+
FirebaseRemoteConfig remoteConfig = getRemoteConfig(client);
633+
634+
ServerTemplate template = remoteConfig.getServerTemplate();
635+
String templateData = template.toJson();
636+
assertEquals(TEST_SERVER_TEMPLATE, templateData);
637+
}
638+
639+
@Test
640+
public void testGetServerTemplate1() throws FirebaseRemoteConfigException {
641+
MockRemoteConfigClient client = MockRemoteConfigClient.fromTemplate(
642+
new Template().setETag(TEST_ETAG));
643+
FirebaseRemoteConfig remoteConfig = getRemoteConfig(client);
644+
645+
Template template = remoteConfig.getTemplate();
646+
647+
assertEquals(TEST_ETAG, template.getETag());
648+
}
649+
650+
@Test
651+
public void testGetServerTemplateFailure() {
652+
MockRemoteConfigClient client = MockRemoteConfigClient.fromException(TEST_EXCEPTION);
653+
FirebaseRemoteConfig remoteConfig = getRemoteConfig(client);
654+
655+
try {
656+
remoteConfig.getServerTemplate();
657+
} catch (FirebaseRemoteConfigException e) {
658+
assertSame(TEST_EXCEPTION, e);
659+
}
660+
}
661+
662+
@Test
663+
public void testGetServerTemplateAsync() throws Exception {
664+
String TEST_SERVER_TEMPLATE = "{\n" +
665+
" \"etag\": \"etag-123456789012-1\",\n" +
666+
" \"parameters\": {},\n" +
667+
" \"serverConditions\": [],\n" +
668+
" \"parameterGroups\": {}\n" +
669+
"}";
670+
MockRemoteConfigClient client = MockRemoteConfigClient.fromServerTemplate(
671+
new ServerTemplateData().setETag(TEST_ETAG).toJSON());
672+
FirebaseRemoteConfig remoteConfig = getRemoteConfig(client);
673+
674+
ServerTemplate template = remoteConfig.getServerTemplateAsync().get();
675+
String templateData = template.toJson();
676+
assertEquals(TEST_SERVER_TEMPLATE, templateData);
677+
}
678+
679+
@Test
680+
public void testGetServerTemplateAsyncFailure() throws InterruptedException {
681+
MockRemoteConfigClient client = MockRemoteConfigClient.fromException(TEST_EXCEPTION);
682+
FirebaseRemoteConfig remoteConfig = getRemoteConfig(client);
683+
684+
try {
685+
remoteConfig.getServerTemplateAsync().get();
686+
} catch (ExecutionException e) {
687+
assertSame(TEST_EXCEPTION, e.getCause());
688+
}
689+
}
600690
}

src/test/java/com/google/firebase/remoteconfig/MockRemoteConfigClient.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,35 @@
2121
public class MockRemoteConfigClient implements FirebaseRemoteConfigClient{
2222

2323
private final Template resultTemplate;
24+
private final String resultServerTemplate;
2425
private final FirebaseRemoteConfigException exception;
2526
private final ListVersionsResponse listVersionsResponse;
2627

2728
private MockRemoteConfigClient(Template resultTemplate,
29+
String resultServerTemplate,
2830
ListVersionsResponse listVersionsResponse,
2931
FirebaseRemoteConfigException exception) {
3032
this.resultTemplate = resultTemplate;
33+
this.resultServerTemplate = resultServerTemplate;
3134
this.listVersionsResponse = listVersionsResponse;
3235
this.exception = exception;
3336
}
3437

3538
static MockRemoteConfigClient fromTemplate(Template resultTemplate) {
36-
return new MockRemoteConfigClient(resultTemplate, null, null);
39+
return new MockRemoteConfigClient(resultTemplate,null, null, null);
40+
}
41+
42+
static MockRemoteConfigClient fromServerTemplate(String resultTemplate) {
43+
return new MockRemoteConfigClient(null, resultTemplate,null, null);
3744
}
3845

3946
static MockRemoteConfigClient fromListVersionsResponse(
4047
ListVersionsResponse listVersionsResponse) {
41-
return new MockRemoteConfigClient(null, listVersionsResponse, null);
48+
return new MockRemoteConfigClient(null,null, listVersionsResponse, null);
4249
}
4350

4451
static MockRemoteConfigClient fromException(FirebaseRemoteConfigException exception) {
45-
return new MockRemoteConfigClient(null, null, exception);
52+
return new MockRemoteConfigClient(null,null, null, exception);
4653
}
4754

4855
@Override
@@ -54,19 +61,19 @@ public Template getTemplate() throws FirebaseRemoteConfigException {
5461
}
5562

5663
@Override
57-
public Template getTemplateAtVersion(String versionNumber) throws FirebaseRemoteConfigException {
64+
public String getServerTemplate() throws FirebaseRemoteConfigException {
5865
if (exception != null) {
5966
throw exception;
6067
}
61-
return resultTemplate;
68+
return resultServerTemplate;
6269
}
6370

6471
@Override
65-
public String getServerTemplate() throws FirebaseRemoteConfigException {
72+
public Template getTemplateAtVersion(String versionNumber) throws FirebaseRemoteConfigException {
6673
if (exception != null) {
6774
throw exception;
6875
}
69-
return resultTemplate.toString();
76+
return resultTemplate;
7077
}
7178

7279
@Override

0 commit comments

Comments
 (0)