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.createSearchSession( 52 new SearchContext.Builder( 53 ApplicationProvider.getApplicationContext(), 54 "database") 55 .build()) 56 .get(); 57 // Remove all data before test 58 appSearch.setSchema(new SetSchemaRequest.Builder().setForceOverride(true).build()).get(); 59 } 60 61 @Test smokeTest()62 public void smokeTest() throws Exception { 63 AppSearchSchema schema = 64 new AppSearchSchema.Builder("testType") 65 .addProperty( 66 new StringPropertyConfig.Builder("prop") 67 .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL) 68 .setIndexingType( 69 StringPropertyConfig.INDEXING_TYPE_PREFIXES) 70 .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN) 71 .build()) 72 .build(); 73 appSearch.setSchema(new SetSchemaRequest.Builder().addSchemas(schema).build()).get(); 74 } 75 76 @Test smokeTestAnnotationProcessor()77 public void smokeTestAnnotationProcessor() throws Exception { 78 appSearch 79 .setSchema( 80 new SetSchemaRequest.Builder() 81 .addDocumentClasses(TestDocument.class) 82 .build()) 83 .get(); 84 85 TestDocument input = new TestDocument("namespace", "id1", "avocado"); 86 appSearch 87 .put(new PutDocumentsRequest.Builder().addDocuments(input).build()) 88 .get() 89 .checkSuccess(); 90 SearchResults results = 91 appSearch.search( 92 "av", 93 new SearchSpec.Builder() 94 .setTermMatch(SearchSpec.TERM_MATCH_PREFIX) 95 .build()); 96 List<SearchResult> page = results.getNextPage().get(); 97 assertThat(page).hasSize(1); 98 SearchResult result = page.get(0); 99 assertThat(results.getNextPage().get()).isEmpty(); 100 101 GenericDocument genericOutput = result.getGenericDocument(); 102 assertEquals("id1", genericOutput.getId()); 103 assertEquals("avocado", genericOutput.getPropertyString("body")); 104 TestDocument output = genericOutput.toDocumentClass(TestDocument.class); 105 assertEquals("id1", output.getId()); 106 assertEquals("avocado", output.getBody()); 107 } 108 } 109