Skip to content

Commit 4be3847

Browse files
committed
Adding custom assertThat with more readable assertions
1 parent 227bf3b commit 4be3847

File tree

2 files changed

+99
-38
lines changed

2 files changed

+99
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.installations.local;
16+
17+
import static com.google.common.truth.Truth.assertAbout;
18+
19+
import com.google.common.truth.FailureMetadata;
20+
import com.google.common.truth.Subject;
21+
import com.google.firebase.installations.local.PersistedFid.RegistrationStatus;
22+
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
23+
24+
public final class PersistedFidEntrySubject extends Subject {
25+
26+
// User-defined entry point
27+
public static PersistedFidEntrySubject assertThat(
28+
@NullableDecl PersistedFidEntry persistedFidEntry) {
29+
return assertAbout(PERSISTED_FID_ENTRY_SUBJECT_FACTORY).that(persistedFidEntry);
30+
}
31+
32+
// Static method for getting the subject factory (for use with assertAbout())
33+
public static Subject.Factory<PersistedFidEntrySubject, PersistedFidEntry> persistedFidEntry() {
34+
return PERSISTED_FID_ENTRY_SUBJECT_FACTORY;
35+
}
36+
37+
// Boiler-plate Subject.Factory for EmployeeSubject
38+
private static final Subject.Factory<PersistedFidEntrySubject, PersistedFidEntry>
39+
PERSISTED_FID_ENTRY_SUBJECT_FACTORY = PersistedFidEntrySubject::new;
40+
41+
private final PersistedFidEntry actual;
42+
43+
/**
44+
* Constructor for use by subclasses. If you want to create an instance of this class itself, call
45+
* {@link Subject#check(String, PersistedFidEntry ..) check(...)}{@code .that(actual)}.
46+
*
47+
* @param metadata
48+
* @param actual
49+
*/
50+
protected PersistedFidEntrySubject(
51+
FailureMetadata metadata, @NullableDecl PersistedFidEntry actual) {
52+
super(metadata, actual);
53+
this.actual = actual;
54+
}
55+
56+
// User-defined test assertion
57+
58+
public void hasFid(String fid) {
59+
check("getFirebaseInstallationId()").that(actual.getFirebaseInstallationId()).isEqualTo(fid);
60+
}
61+
62+
public void hasAuthToken(String authToken) {
63+
check("getAuthToken()").that(actual.getAuthToken()).isEqualTo(authToken);
64+
}
65+
66+
public void hasRefreshToken(String refreshToken) {
67+
check("getRefreshToken()").that(actual.getRefreshToken()).isEqualTo(refreshToken);
68+
}
69+
70+
public void hasCreationTimestamp(long creationTimestamp) {
71+
check("getTokenCreationEpochInSecs()")
72+
.that(actual.getTokenCreationEpochInSecs())
73+
.isEqualTo(creationTimestamp);
74+
}
75+
76+
public void hasTokenExpirationTimestamp(long tokenExpirationTimestamp) {
77+
check("getExpiresInSecs()").that(actual.getExpiresInSecs()).isEqualTo(tokenExpirationTimestamp);
78+
}
79+
80+
public void hasRegistrationStatus(RegistrationStatus registrationStatus) {
81+
check("getRegistrationStatus()")
82+
.that(actual.getRegistrationStatus())
83+
.isEqualTo(registrationStatus);
84+
}
85+
}

firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedFidTest.java

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.firebase.installations.local;
1616

17-
import static com.google.common.truth.Truth.assertWithMessage;
1817
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_1;
1918
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_2;
2019
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN;
@@ -23,13 +22,15 @@
2322
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_FID_1;
2423
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_REFRESH_TOKEN;
2524
import static com.google.firebase.installations.FisAndroidTestConstants.TEST_TOKEN_EXPIRATION_TIMESTAMP;
25+
import static com.google.firebase.installations.local.PersistedFidEntrySubject.assertThat;
2626
import static org.junit.Assert.assertNull;
2727
import static org.junit.Assert.assertTrue;
2828

2929
import androidx.test.core.app.ApplicationProvider;
3030
import androidx.test.runner.AndroidJUnit4;
3131
import com.google.firebase.FirebaseApp;
3232
import com.google.firebase.FirebaseOptions;
33+
import com.google.firebase.installations.local.PersistedFid.RegistrationStatus;
3334
import org.junit.After;
3435
import org.junit.Before;
3536
import org.junit.Test;
@@ -88,25 +89,12 @@ public void testUpdateAndReadPersistedFidEntry_successful() throws Exception {
8889
PersistedFidEntry entryValue = persistedFid0.readPersistedFidEntryValue();
8990

9091
// Validate insertion was successful
91-
assertWithMessage("Persisted Fid doesn't match")
92-
.that(entryValue.getFirebaseInstallationId())
93-
.isEqualTo(TEST_FID_1);
94-
assertWithMessage("Persisted Auth Token doesn't match")
95-
.that(entryValue.getAuthToken())
96-
.isEqualTo(TEST_AUTH_TOKEN);
97-
assertWithMessage("Persisted Refresh Token doesn't match")
98-
.that(entryValue.getRefreshToken())
99-
.isEqualTo(TEST_REFRESH_TOKEN);
100-
assertWithMessage("Persisted Registration Status doesn't match")
101-
.that(entryValue.getRegistrationStatus())
102-
.isEqualTo(PersistedFid.RegistrationStatus.UNREGISTERED);
103-
assertWithMessage("Persisted Token expiration timestamp doesn't match")
104-
.that(entryValue.getExpiresInSecs())
105-
.isEqualTo(TEST_TOKEN_EXPIRATION_TIMESTAMP);
106-
assertWithMessage("Persisted Creation time doesn't match")
107-
.that(entryValue.getTokenCreationEpochInSecs())
108-
.isEqualTo(TEST_CREATION_TIMESTAMP_1);
109-
assertNull(persistedFid1.readPersistedFidEntryValue());
92+
assertThat(entryValue).hasFid(TEST_FID_1);
93+
assertThat(entryValue).hasAuthToken(TEST_AUTH_TOKEN);
94+
assertThat(entryValue).hasRefreshToken(TEST_REFRESH_TOKEN);
95+
assertThat(entryValue).hasRegistrationStatus(RegistrationStatus.UNREGISTERED);
96+
assertThat(entryValue).hasTokenExpirationTimestamp(TEST_TOKEN_EXPIRATION_TIMESTAMP);
97+
assertThat(entryValue).hasCreationTimestamp(TEST_CREATION_TIMESTAMP_1);
11098

11199
// Update Persisted Fid Entry with Registered status in Shared Prefs
112100
assertTrue(
@@ -122,23 +110,11 @@ public void testUpdateAndReadPersistedFidEntry_successful() throws Exception {
122110
entryValue = persistedFid0.readPersistedFidEntryValue();
123111

124112
// Validate update was successful
125-
assertWithMessage("Persisted Fid doesn't match")
126-
.that(entryValue.getFirebaseInstallationId())
127-
.isEqualTo(TEST_FID_1);
128-
assertWithMessage("Persisted Auth Token doesn't match")
129-
.that(entryValue.getAuthToken())
130-
.isEqualTo(TEST_AUTH_TOKEN);
131-
assertWithMessage("Persisted Refresh Token doesn't match")
132-
.that(entryValue.getRefreshToken())
133-
.isEqualTo(TEST_REFRESH_TOKEN);
134-
assertWithMessage("Persisted Registration Status doesn't match")
135-
.that(entryValue.getRegistrationStatus())
136-
.isEqualTo(PersistedFid.RegistrationStatus.REGISTERED);
137-
assertWithMessage("Persisted Token expiration timestamp doesn't match")
138-
.that(entryValue.getExpiresInSecs())
139-
.isEqualTo(TEST_TOKEN_EXPIRATION_TIMESTAMP);
140-
assertWithMessage("Persisted Creation time doesn't match")
141-
.that(entryValue.getTokenCreationEpochInSecs())
142-
.isEqualTo(TEST_CREATION_TIMESTAMP_2);
113+
assertThat(entryValue).hasFid(TEST_FID_1);
114+
assertThat(entryValue).hasAuthToken(TEST_AUTH_TOKEN);
115+
assertThat(entryValue).hasRefreshToken(TEST_REFRESH_TOKEN);
116+
assertThat(entryValue).hasRegistrationStatus(RegistrationStatus.REGISTERED);
117+
assertThat(entryValue).hasTokenExpirationTimestamp(TEST_TOKEN_EXPIRATION_TIMESTAMP);
118+
assertThat(entryValue).hasCreationTimestamp(TEST_CREATION_TIMESTAMP_2);
143119
}
144120
}

0 commit comments

Comments
 (0)