Xiaoyu Jin | 33b6580 | 2022-03-07 13:50:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
Xiaoyu Jin | 2dbc067 | 2022-04-13 15:42:33 +0000 | [diff] [blame] | 17 | package com.android.server.appsearch.contactsindexer; |
Xiaoyu Jin | 33b6580 | 2022-03-07 13:50:15 +0000 | [diff] [blame] | 18 | |
| 19 | import android.net.Uri; |
| 20 | |
| 21 | import com.android.server.appsearch.contactsindexer.PersonBuilderHelper; |
| 22 | import com.android.server.appsearch.contactsindexer.appsearchtypes.ContactPoint; |
| 23 | import com.android.server.appsearch.contactsindexer.appsearchtypes.Person; |
| 24 | |
| 25 | |
| 26 | import static com.google.common.truth.Truth.assertThat; |
| 27 | |
| 28 | import org.junit.Test; |
| 29 | |
| 30 | import java.util.Arrays; |
| 31 | import java.util.List; |
| 32 | |
| 33 | public class PersonBuilderHelperTest { |
| 34 | @Test |
| 35 | public void testFingerprintGeneration_creationTimestampAndFingerprintNotIncluded() { |
| 36 | long creationTimestamp = 12345L; |
| 37 | long creationTimestamp2 = 12346L; |
| 38 | String namespace = "namespace"; |
| 39 | String id = "id"; |
| 40 | String name = "name"; |
| 41 | String givenName = "givenName"; |
| 42 | String middleName = "middleName"; |
| 43 | String lastName = "lastName"; |
| 44 | Uri externalUri = Uri.parse("http://external.com"); |
| 45 | Uri imageUri = Uri.parse("http://image.com"); |
| 46 | byte[] fingerprint = "Hello world!".getBytes(); |
| 47 | byte[] fingerprint2 = "Hello world!!!".getBytes(); |
| 48 | List<String> additionalNames = Arrays.asList("name1", "name2"); |
| 49 | List<String> affiliations = Arrays.asList("Org1", "Org2", "Org3"); |
| 50 | List<String> relations = Arrays.asList("relation1", "relation2"); |
| 51 | boolean isImportant = true; |
| 52 | boolean isBot = true; |
| 53 | ContactPoint contact1 = new ContactPoint.Builder(namespace, id + "1", "Home") |
| 54 | .addAddress("addr1") |
| 55 | .addPhone("phone1") |
| 56 | .addEmail("email1") |
| 57 | .addAppId("appId1") |
| 58 | .build(); |
| 59 | ContactPoint contact2 = new ContactPoint.Builder(namespace, id + "2", "Work") |
| 60 | .addAddress("addr2") |
| 61 | .addPhone("phone2") |
| 62 | .addEmail("email2") |
| 63 | .addAppId("appId2") |
| 64 | .build(); |
| 65 | Person.Builder personBuilder = new Person.Builder(namespace, id, name) |
| 66 | .setGivenName(givenName) |
| 67 | .setMiddleName(middleName) |
| 68 | .setFamilyName(lastName) |
| 69 | .setExternalUri(externalUri) |
| 70 | .setImageUri(imageUri) |
| 71 | .addAdditionalName(Person.TYPE_NICKNAME, additionalNames.get(0)) |
| 72 | .addAdditionalName(Person.TYPE_PHONETIC_NAME, additionalNames.get(1)) |
| 73 | .addAffiliation(affiliations.get(0)) |
| 74 | .addAffiliation(affiliations.get(1)) |
| 75 | .addAffiliation(affiliations.get(2)) |
| 76 | .addRelation(relations.get(0)) |
| 77 | .addRelation(relations.get(1)) |
| 78 | .setIsImportant(isImportant) |
| 79 | .setIsBot(isBot) |
| 80 | .setFingerprint(fingerprint) |
| 81 | .addContactPoint(contact1) |
| 82 | .addContactPoint(contact2); |
| 83 | |
| 84 | Person person = new PersonBuilderHelper(id, personBuilder).setCreationTimestampMillis( |
| 85 | creationTimestamp).buildPerson(); |
| 86 | Person personSame = new PersonBuilderHelper(id, personBuilder).setCreationTimestampMillis( |
| 87 | creationTimestamp).buildPerson(); |
| 88 | // same content except for creationTimestamp and fingerprint. |
| 89 | Person personWithDifferentTsAndFingerprint = new PersonBuilderHelper(id, |
| 90 | personBuilder.setFingerprint( |
| 91 | fingerprint2)).setCreationTimestampMillis(creationTimestamp2).buildPerson(); |
| 92 | |
| 93 | // Fingerprint is not empty. |
| 94 | assertThat(person.getFingerprint()).hasLength(16); |
| 95 | // Fingerprint would be reset by the generated value |
| 96 | assertThat(person.getFingerprint()).isNotEqualTo(fingerprint); |
| 97 | assertThat(personWithDifferentTsAndFingerprint.getFingerprint()).isNotEqualTo(fingerprint2); |
| 98 | assertThat(person.getFingerprint()).isEqualTo(personSame.getFingerprint()); |
| 99 | assertThat(person.getFingerprint()).isEqualTo( |
| 100 | personWithDifferentTsAndFingerprint.getFingerprint()); |
| 101 | assertThat(person.getCreationTimestampMillis()).isEqualTo(creationTimestamp); |
| 102 | assertThat(personSame.getCreationTimestampMillis()).isEqualTo(creationTimestamp); |
| 103 | assertThat(personWithDifferentTsAndFingerprint.getCreationTimestampMillis()).isEqualTo( |
| 104 | creationTimestamp2); |
| 105 | } |
| 106 | |
| 107 | // Test the fingerprinting function. |
| 108 | @Test |
| 109 | public void testFingerprintGeneration_forEachPropertyType_string() { |
| 110 | long creationTimestamp = 12345L; |
| 111 | String namespace = "namespace"; |
| 112 | String id = "id"; |
| 113 | String name = "name"; |
| 114 | String givenName = "givenName"; |
| 115 | Uri externalUri = Uri.parse("http://external.com"); |
| 116 | Uri externalUriDiff = Uri.parse("http://external2.com"); |
| 117 | Person.Builder personBuilder = new Person.Builder(namespace, id, name) |
| 118 | .setCreationTimestampMillis(creationTimestamp) |
| 119 | .setGivenName(givenName) |
| 120 | .setExternalUri(externalUri); |
| 121 | |
| 122 | Person person = new PersonBuilderHelper(id, personBuilder).setCreationTimestampMillis( |
| 123 | 0).buildPerson(); |
| 124 | Person personSame = new PersonBuilderHelper(id, personBuilder).setCreationTimestampMillis( |
| 125 | 0).buildPerson(); |
| 126 | Person personNameDiff = new PersonBuilderHelper(id, |
| 127 | personBuilder.setGivenName(name + "diff")).setCreationTimestampMillis( |
| 128 | 0).buildPerson(); |
| 129 | Person personUriDiff = new PersonBuilderHelper(id, |
| 130 | personBuilder.setExternalUri(externalUriDiff)).setCreationTimestampMillis( |
| 131 | 0).buildPerson(); |
| 132 | |
| 133 | assertThat(person.getFingerprint()).isEqualTo(personSame.getFingerprint()); |
| 134 | assertThat(person.getFingerprint()).isNotEqualTo(personNameDiff.getFingerprint()); |
| 135 | assertThat(person.getFingerprint()).isNotEqualTo(personUriDiff.getFingerprint()); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testFingerprintGeneration_forEachPropertyType_boolean() { |
| 140 | long creationTimestamp = 12345L; |
| 141 | String namespace = "namespace"; |
| 142 | String id = "id"; |
| 143 | String name = "name"; |
| 144 | Person.Builder personBuilder = new Person.Builder(namespace, id, name) |
| 145 | .setIsBot(true); |
| 146 | |
| 147 | Person personIsBotTrue = new PersonBuilderHelper(id, |
| 148 | personBuilder).setCreationTimestampMillis(creationTimestamp).buildPerson(); |
| 149 | Person personIsBotFalse = new PersonBuilderHelper(id, |
| 150 | personBuilder.setIsBot(false)).setCreationTimestampMillis( |
| 151 | creationTimestamp).buildPerson(); |
| 152 | |
| 153 | assertThat(personIsBotTrue.getFingerprint()).isNotEqualTo( |
| 154 | personIsBotFalse.getFingerprint()); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testFingerprintGeneration_forEachPropertyType_stringArray() { |
| 159 | String namespace = "namespace"; |
| 160 | String id = "id"; |
| 161 | String name = "name"; |
| 162 | List<String> additionalNames = Arrays.asList("name1", "name2"); |
| 163 | List<String> additionalNames2 = Arrays.asList("name1", "name3"); |
| 164 | Person.Builder personBuilder = new Person.Builder(namespace, id, name) |
| 165 | .addAdditionalName(Person.TYPE_NICKNAME, additionalNames.get(0)) |
| 166 | .addAdditionalName(Person.TYPE_PHONETIC_NAME, additionalNames.get(1)); |
| 167 | Person.Builder personBuilder2 = new Person.Builder(namespace, id, name) |
| 168 | .addAdditionalName(Person.TYPE_NICKNAME, additionalNames2.get(0)) |
| 169 | .addAdditionalName(Person.TYPE_PHONETIC_NAME, additionalNames2.get(1)); |
| 170 | // one additionalName type is different from personBuilder above. |
| 171 | Person.Builder personBuilder3 = new Person.Builder(namespace, id, name) |
| 172 | .addAdditionalName(Person.TYPE_UNKNOWN, additionalNames.get(0)) |
| 173 | .addAdditionalName(Person.TYPE_PHONETIC_NAME, additionalNames.get(1)); |
| 174 | |
| 175 | Person person = new PersonBuilderHelper(id, personBuilder).setCreationTimestampMillis( |
| 176 | 0).buildPerson(); |
| 177 | Person person2 = new PersonBuilderHelper(id, personBuilder2).setCreationTimestampMillis( |
| 178 | 0).buildPerson(); |
| 179 | Person person3 = new PersonBuilderHelper(id, personBuilder3).setCreationTimestampMillis( |
| 180 | 0).buildPerson(); |
| 181 | |
| 182 | assertThat(person.getFingerprint()).isNotEqualTo(person2.getFingerprint()); |
| 183 | assertThat(person.getFingerprint()).isNotEqualTo(person3.getFingerprint()); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void testFingerprintGeneration_forEachPropertyType_DocumentArray() { |
| 188 | long creationTimestamp = 12345L; |
| 189 | String namespace = "namespace"; |
| 190 | String id = "id"; |
| 191 | String name = "name"; |
| 192 | ContactPoint contact1 = new ContactPoint.Builder(namespace, id + "1", "Home") |
| 193 | .addAddress("addr1") |
| 194 | .addPhone("phone1") |
| 195 | .addEmail("email1") |
| 196 | .addAppId("appId1") |
| 197 | .build(); |
| 198 | ContactPoint contact2 = new ContactPoint.Builder(namespace, id + "2", "Work") |
| 199 | .addAddress("addr2") |
| 200 | .addPhone("phone2") |
| 201 | .addEmail("email2") |
| 202 | .addAppId("appId2") |
| 203 | .build(); |
| 204 | ContactPoint contact2diff = new ContactPoint.Builder(namespace, id + "2", "Work") |
| 205 | .addAddress("addr2diff") |
| 206 | .addPhone("phone2") |
| 207 | .addEmail("email2") |
| 208 | .addAppId("appId2") |
| 209 | .build(); |
| 210 | Person.Builder personBuilder = new Person.Builder(namespace, id, name) |
| 211 | .setCreationTimestampMillis(creationTimestamp) |
| 212 | .addContactPoint(contact1) |
| 213 | .addContactPoint(contact2); |
| 214 | Person.Builder personBuilder2 = new Person.Builder(namespace, id, name) |
| 215 | .setCreationTimestampMillis(creationTimestamp) |
| 216 | .addContactPoint(contact1) |
| 217 | .addContactPoint(contact2diff); |
| 218 | |
| 219 | Person person = new PersonBuilderHelper(id, personBuilder).setCreationTimestampMillis( |
| 220 | 0).setCreationTimestampMillis(0).buildPerson(); |
| 221 | Person person2 = new PersonBuilderHelper(id, personBuilder2).setCreationTimestampMillis( |
| 222 | 0).setCreationTimestampMillis(0).buildPerson(); |
| 223 | |
| 224 | // Fingerprint would be reset by the generated value |
| 225 | assertThat(person.getFingerprint()).isNotEqualTo(person2.getFingerprint()); |
| 226 | } |
| 227 | |
| 228 | @Test |
| 229 | public void testFingerprintGeneration_sameValueForDifferentProperties_differentFingerprint() { |
| 230 | String namespace = "namespace"; |
| 231 | String id = "id"; |
| 232 | String name = "same"; |
| 233 | Person.Builder personBuilder = new Person.Builder(namespace, id, name).setGivenName(name); |
| 234 | Person.Builder personBuilder2 = new Person.Builder(namespace, id, name).setFamilyName(name); |
| 235 | |
| 236 | Person person = new PersonBuilderHelper(id, personBuilder).setCreationTimestampMillis( |
| 237 | 0).buildPerson(); |
| 238 | Person person2 = new PersonBuilderHelper(id, personBuilder2).setCreationTimestampMillis( |
| 239 | 0).buildPerson(); |
| 240 | |
| 241 | assertThat(person.getFingerprint()).isNotEqualTo(person2.getFingerprint()); |
| 242 | } |
Xiaoyu Jin | 06b1412 | 2022-04-07 14:53:25 +0000 | [diff] [blame] | 243 | |
| 244 | @Test |
| 245 | public void testScore_setCorrectly() { |
| 246 | String namespace = "namespace"; |
| 247 | String id = "id"; |
| 248 | String name = "name"; |
| 249 | List<String> additionalNames = Arrays.asList("name1", "name2"); |
| 250 | Person.Builder personBuilder = new Person.Builder(namespace, id, name) |
| 251 | .addAdditionalName(Person.TYPE_NICKNAME, additionalNames.get(0)) |
| 252 | .addAdditionalName(Person.TYPE_PHONETIC_NAME, additionalNames.get(1)); |
| 253 | ContactPoint contact1 = new ContactPoint.Builder(namespace, id + "1", "Home") |
| 254 | .addAddress("addr1") |
| 255 | .addPhone("phone1") |
| 256 | .addEmail("email1") |
| 257 | .addAppId("appId1") |
| 258 | .build(); |
| 259 | ContactPoint contact2 = new ContactPoint.Builder(namespace, id + "2", "Work") |
| 260 | .addAddress("addr2") |
| 261 | .addPhone("phone2") |
| 262 | .addEmail("email2") |
| 263 | .addAppId("appId2") |
| 264 | .build(); |
| 265 | ContactPoint contact3 = new ContactPoint.Builder(namespace, id + "3", "Other") |
| 266 | .addAddress("addr3") |
| 267 | .addPhone("phone3") |
| 268 | .addEmail("email3") |
| 269 | .addAppId("appId3") |
| 270 | .build(); |
| 271 | personBuilder.addContactPoint(contact1) |
| 272 | .addContactPoint(contact2) |
| 273 | .addContactPoint(contact3); |
| 274 | |
| 275 | Person person = new PersonBuilderHelper(id, personBuilder).setCreationTimestampMillis( |
| 276 | 0).setCreationTimestampMillis(0).buildPerson(); |
| 277 | |
| 278 | // Score should be set as base(1) + # of contactPoints + # of additionalNames. |
| 279 | assertThat(person.getScore()).isEqualTo(6); |
| 280 | } |
Xiaoyu Jin | 33b6580 | 2022-03-07 13:50:15 +0000 | [diff] [blame] | 281 | } |