|
22 | 22 | import static org.junit.Assume.assumeTrue; |
23 | 23 |
|
24 | 24 | import com.google.cloud.firestore.CollectionReference; |
| 25 | +import com.google.cloud.firestore.DocumentSnapshot; |
| 26 | +import com.google.cloud.firestore.Firestore; |
25 | 27 | import com.google.cloud.firestore.GeoPoint; |
26 | 28 | import com.google.cloud.firestore.Pipeline; |
27 | 29 | import com.google.cloud.firestore.PipelineResult; |
| 30 | +import com.google.cloud.firestore.QuerySnapshot; |
28 | 31 | import com.google.cloud.firestore.WriteBatch; |
29 | 32 | import com.google.cloud.firestore.pipeline.stages.Search; |
30 | 33 | import java.util.Arrays; |
|
34 | 37 | import java.util.Objects; |
35 | 38 | import java.util.concurrent.TimeUnit; |
36 | 39 | import java.util.stream.Collectors; |
37 | | -import org.junit.Before; |
| 40 | +import org.junit.BeforeClass; |
38 | 41 | import org.junit.Test; |
39 | 42 | import org.junit.runner.RunWith; |
40 | 43 | import org.junit.runners.JUnit4; |
41 | 44 |
|
42 | 45 | @RunWith(JUnit4.class) |
43 | 46 | public class ITPipelineSearchTest extends ITBaseTest { |
44 | 47 |
|
45 | | - private final String COLLECTION_NAME = "TextSearchIntegrationTests"; |
46 | | - |
47 | | - private CollectionReference restaurantsCollection; |
| 48 | + private static final String COLLECTION_NAME = "TextSearchIntegrationTests"; |
48 | 49 |
|
49 | 50 | private static final Map<String, Map<String, Object>> restaurantDocs = new HashMap<>(); |
50 | 51 |
|
@@ -184,20 +185,41 @@ public void primeBackend() throws Exception { |
184 | 185 | // Disable priming as it uses Watch/Listen |
185 | 186 | } |
186 | 187 |
|
187 | | - @Before |
188 | | - public void setupRestaurantDocs() throws Exception { |
| 188 | + @BeforeClass |
| 189 | + public static void setupRestaurantDocs() throws Exception { |
189 | 190 | assumeTrue( |
190 | 191 | "This test suite only runs against the Enterprise edition in Nightly.", |
191 | 192 | getFirestoreEdition().equals(FirestoreEdition.ENTERPRISE) |
192 | 193 | && "NIGHTLY".equalsIgnoreCase(getTargetBackend())); |
193 | 194 |
|
194 | | - restaurantsCollection = firestore.collection(COLLECTION_NAME); |
195 | | - |
196 | | - WriteBatch batch = firestore.batch(); |
197 | | - for (Map.Entry<String, Map<String, Object>> entry : restaurantDocs.entrySet()) { |
198 | | - batch.set(restaurantsCollection.document(entry.getKey()), entry.getValue()); |
| 195 | + // Initialize a temporary Firestore instance for class-level setup. |
| 196 | + Firestore db = getOptionsBuilder().build().getService(); |
| 197 | + |
| 198 | + // Setup restaurant docs |
| 199 | + try { |
| 200 | + // Get the existing contents of the test collection |
| 201 | + CollectionReference collection = db.collection(COLLECTION_NAME); |
| 202 | + QuerySnapshot snapshot = collection.get().get(); |
| 203 | + |
| 204 | + // A batch will be used to update the test collection to the desired state |
| 205 | + WriteBatch batch = db.batch(); |
| 206 | + |
| 207 | + // Delete unexpected documents |
| 208 | + for (DocumentSnapshot doc : snapshot.getDocuments()) { |
| 209 | + if (!restaurantDocs.containsKey(doc.getId())) { |
| 210 | + batch.delete(doc.getReference()); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + // Add/overwrite expected documents |
| 215 | + for (Map.Entry<String, Map<String, Object>> entry : restaurantDocs.entrySet()) { |
| 216 | + batch.set(collection.document(entry.getKey()), entry.getValue()); |
| 217 | + } |
| 218 | + |
| 219 | + batch.commit().get(10, TimeUnit.SECONDS); |
| 220 | + } finally { |
| 221 | + db.close(); |
199 | 222 | } |
200 | | - batch.commit().get(10, TimeUnit.SECONDS); |
201 | 223 | } |
202 | 224 |
|
203 | 225 | private void assertResultIds(Pipeline.Snapshot snapshot, String... ids) { |
|
0 commit comments