1 /* 2 * Copyright (C) 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 androidx.appsearch.smoketest; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertEquals; 22 23 import androidx.appsearch.app.AppSearchSchema; 24 import androidx.appsearch.app.AppSearchSchema.PropertyConfig; 25 import androidx.appsearch.app.AppSearchSchema.StringPropertyConfig; 26 import androidx.appsearch.app.AppSearchSession; 27 import androidx.appsearch.app.GenericDocument; 28 import androidx.appsearch.app.PutDocumentsRequest; 29 import androidx.appsearch.app.SearchResult; 30 import androidx.appsearch.app.SearchResults; 31 import androidx.appsearch.app.SearchSpec; 32 import androidx.appsearch.app.SetSchemaRequest; 33 import androidx.appsearch.localstorage.LocalStorage; 34 import androidx.appsearch.localstorage.LocalStorage.SearchContext; 35 import androidx.test.core.app.ApplicationProvider; 36 import androidx.test.ext.junit.runners.AndroidJUnit4; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 42 import java.util.List; 43 44 @RunWith(AndroidJUnit4.class) 45 public class AndroidXSmokeTest { 46 private AppSearchSession appSearch; 47 48 @Before setUp()49 public void setUp() throws Exception { 50 appSearch = 51 LocalStorage.createSearchSessionAsync( 52 new SearchContext.Builder( 53 ApplicationProvider.getApplicationContext(), 54 "database") 55 .build()) 56 .get(); 57 // Remove all data before test 58 appSearch.setSchemaAsync(new SetSchemaRequest.Builder().setForceOverride(true).build()) 59 .get(); 60 } 61 62 @Test smokeTest()63 public void smokeTest() throws Exception { 64 AppSearchSchema schema = 65 new AppSearchSchema.Builder("testType") 66 .addProperty( 67 new StringPropertyConfig.Builder("prop") 68 .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL) 69 .setIndexingType( 70 StringPropertyConfig.INDEXING_TYPE_PREFIXES) 71 .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN) 72 .build()) 73 .build(); 74 appSearch.setSchemaAsync(new SetSchemaRequest.Builder().addSchemas(schema).build()).get(); 75 } 76 77 @Test smokeTestAnnotationProcessor()78 public void smokeTestAnnotationProcessor() throws Exception { 79 appSearch 80 .setSchemaAsync( 81 new SetSchemaRequest.Builder() 82 .addDocumentClasses(TestDocument.class) 83 .build()) 84 .get(); 85 86 TestDocument input = new TestDocument("namespace", "id1", "avocado"); 87 appSearch 88 .putAsync(new PutDocumentsRequest.Builder().addDocuments(input).build()) 89 .get() 90 .checkSuccess(); 91 SearchResults results = 92 appSearch.search( 93 "av", 94 new SearchSpec.Builder() 95 .setTermMatch(SearchSpec.TERM_MATCH_PREFIX) 96 .build()); 97 List<SearchResult> page = results.getNextPageAsync().get(); 98 assertThat(page).hasSize(1); 99 SearchResult result = page.get(0); 100 assertThat(results.getNextPageAsync().get()).isEmpty(); 101 102 GenericDocument genericOutput = result.getGenericDocument(); 103 assertEquals("id1", genericOutput.getId()); 104 assertEquals("avocado", genericOutput.getPropertyString("body")); 105 TestDocument output = genericOutput.toDocumentClass(TestDocument.class); 106 assertEquals("id1", output.getId()); 107 assertEquals("avocado", output.getBody()); 108 } 109 } 110