1 /* 2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.app.appsearch.testutil; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import android.annotation.NonNull; 23 import android.app.appsearch.AppSearchBatchResult; 24 import android.app.appsearch.AppSearchSessionShim; 25 import android.app.appsearch.GenericDocument; 26 import android.app.appsearch.GetByDocumentIdRequest; 27 import android.app.appsearch.SearchResult; 28 import android.app.appsearch.SearchResultsShim; 29 30 import com.android.server.appsearch.external.localstorage.visibilitystore.VisibilityChecker; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 import java.util.Set; 35 import java.util.concurrent.Future; 36 37 /** 38 * Class with helper functions for testing for AppSearch. 39 * 40 * @hide 41 */ 42 public class AppSearchTestUtils { AppSearchTestUtils()43 private AppSearchTestUtils() {} 44 45 /** Checks batch result. */ 46 @NonNull checkIsBatchResultSuccess( @onNull Future<AppSearchBatchResult<K, V>> future)47 public static <K, V> AppSearchBatchResult<K, V> checkIsBatchResultSuccess( 48 @NonNull Future<AppSearchBatchResult<K, V>> future) throws Exception { 49 AppSearchBatchResult<K, V> result = future.get(); 50 assertWithMessage("AppSearchBatchResult not successful: " + result) 51 .that(result.isSuccess()) 52 .isTrue(); 53 return result; 54 } 55 56 /** Gets documents from ids. */ 57 @NonNull doGet( @onNull AppSearchSessionShim session, @NonNull String namespace, @NonNull String... ids)58 public static List<GenericDocument> doGet( 59 @NonNull AppSearchSessionShim session, 60 @NonNull String namespace, 61 @NonNull String... ids) 62 throws Exception { 63 AppSearchBatchResult<String, GenericDocument> result = 64 checkIsBatchResultSuccess( 65 session.getByDocumentIdAsync( 66 new GetByDocumentIdRequest.Builder(namespace).addIds(ids).build())); 67 assertThat(result.getSuccesses()).hasSize(ids.length); 68 assertThat(result.getFailures()).isEmpty(); 69 List<GenericDocument> list = new ArrayList<>(ids.length); 70 for (String id : ids) { 71 list.add(result.getSuccesses().get(id)); 72 } 73 return list; 74 } 75 76 /** Gets documents from {@link GetByDocumentIdRequest}. */ 77 @NonNull doGet( @onNull AppSearchSessionShim session, @NonNull GetByDocumentIdRequest request)78 public static List<GenericDocument> doGet( 79 @NonNull AppSearchSessionShim session, @NonNull GetByDocumentIdRequest request) 80 throws Exception { 81 AppSearchBatchResult<String, GenericDocument> result = 82 checkIsBatchResultSuccess(session.getByDocumentIdAsync(request)); 83 Set<String> ids = request.getIds(); 84 assertThat(result.getSuccesses()).hasSize(ids.size()); 85 assertThat(result.getFailures()).isEmpty(); 86 List<GenericDocument> list = new ArrayList<>(ids.size()); 87 for (String id : ids) { 88 list.add(result.getSuccesses().get(id)); 89 } 90 return list; 91 } 92 93 /** Extracts documents from {@link SearchResultsShim}. */ 94 @NonNull convertSearchResultsToDocuments( @onNull SearchResultsShim searchResults)95 public static List<GenericDocument> convertSearchResultsToDocuments( 96 @NonNull SearchResultsShim searchResults) throws Exception { 97 List<SearchResult> results = retrieveAllSearchResults(searchResults); 98 List<GenericDocument> documents = new ArrayList<>(results.size()); 99 for (SearchResult result : results) { 100 documents.add(result.getGenericDocument()); 101 } 102 return documents; 103 } 104 105 /** Extracts all {@link SearchResult} from {@link SearchResultsShim}. */ 106 @NonNull retrieveAllSearchResults( @onNull SearchResultsShim searchResults)107 public static List<SearchResult> retrieveAllSearchResults( 108 @NonNull SearchResultsShim searchResults) throws Exception { 109 List<SearchResult> page = searchResults.getNextPageAsync().get(); 110 List<SearchResult> results = new ArrayList<>(); 111 while (!page.isEmpty()) { 112 results.addAll(page); 113 page = searchResults.getNextPageAsync().get(); 114 } 115 return results; 116 } 117 118 /** 119 * Creates a mock {@link VisibilityChecker}. 120 * 121 * @param visiblePrefixedSchemas Schema types that are accessible to any caller. 122 * @return 123 */ 124 @NonNull createMockVisibilityChecker( @onNull Set<String> visiblePrefixedSchemas)125 public static VisibilityChecker createMockVisibilityChecker( 126 @NonNull Set<String> visiblePrefixedSchemas) { 127 return (callerAccess, packageName, prefixedSchema, visibilityStore) -> 128 visiblePrefixedSchemas.contains(prefixedSchema); 129 } 130 } 131