aboutsummaryrefslogtreecommitdiff
path: root/java/tests/instrumentation/src/androidx/appsearch/smoketest/AndroidXSmokeTest.java
blob: 98b1b25abf3091bede9b6cb5f3f0cd026f5933f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.appsearch.smoketest;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;

import androidx.appsearch.app.AppSearchSchema;
import androidx.appsearch.app.AppSearchSchema.PropertyConfig;
import androidx.appsearch.app.AppSearchSchema.StringPropertyConfig;
import androidx.appsearch.app.AppSearchSession;
import androidx.appsearch.app.GenericDocument;
import androidx.appsearch.app.PutDocumentsRequest;
import androidx.appsearch.app.SearchResult;
import androidx.appsearch.app.SearchResults;
import androidx.appsearch.app.SearchSpec;
import androidx.appsearch.app.SetSchemaRequest;
import androidx.appsearch.localstorage.LocalStorage;
import androidx.appsearch.localstorage.LocalStorage.SearchContext;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

@RunWith(AndroidJUnit4.class)
public class AndroidXSmokeTest {
    private AppSearchSession appSearch;

    @Before
    public void setUp() throws Exception {
        appSearch =
                LocalStorage.createSearchSession(
                                new SearchContext.Builder(
                                                ApplicationProvider.getApplicationContext(),
                                                "database")
                                        .build())
                        .get();
        // Remove all data before test
        appSearch.setSchema(new SetSchemaRequest.Builder().setForceOverride(true).build()).get();
    }

    @Test
    public void smokeTest() throws Exception {
        AppSearchSchema schema =
                new AppSearchSchema.Builder("testType")
                        .addProperty(
                                new StringPropertyConfig.Builder("prop")
                                        .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
                                        .setIndexingType(
                                                StringPropertyConfig.INDEXING_TYPE_PREFIXES)
                                        .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
                                        .build())
                        .build();
        appSearch.setSchema(new SetSchemaRequest.Builder().addSchemas(schema).build()).get();
    }

    @Test
    public void smokeTestAnnotationProcessor() throws Exception {
        appSearch
                .setSchema(
                        new SetSchemaRequest.Builder()
                                .addDocumentClasses(TestDocument.class)
                                .build())
                .get();

        TestDocument input = new TestDocument("namespace", "id1", "avocado");
        appSearch
                .put(new PutDocumentsRequest.Builder().addDocuments(input).build())
                .get()
                .checkSuccess();
        SearchResults results =
                appSearch.search(
                        "av",
                        new SearchSpec.Builder()
                                .setTermMatch(SearchSpec.TERM_MATCH_PREFIX)
                                .build());
        List<SearchResult> page = results.getNextPage().get();
        assertThat(page).hasSize(1);
        SearchResult result = page.get(0);
        assertThat(results.getNextPage().get()).isEmpty();

        GenericDocument genericOutput = result.getGenericDocument();
        assertEquals("id1", genericOutput.getId());
        assertEquals("avocado", genericOutput.getPropertyString("body"));
        TestDocument output = genericOutput.toDocumentClass(TestDocument.class);
        assertEquals("id1", output.getId());
        assertEquals("avocado", output.getBody());
    }
}