summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/com/android/adservices/service/topics/classifier/ModelManagerTest.java
blob: e21304c395b61964c607de49109ebf293307be54 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * Copyright (C) 2022 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 com.android.adservices.service.topics.classifier;

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

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;

import com.android.adservices.service.FlagsFactory;
import com.android.dx.mockito.inline.extended.ExtendedMockito;

import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.mobiledatadownload.ClientConfigProto.ClientFile;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoSession;
import org.mockito.quality.Strictness;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/** Model Manager Test {@link ModelManager}. */
public class ModelManagerTest {

    private static final Context sContext = ApplicationProvider.getApplicationContext();
    private ImmutableList<Integer> mProductionLabels;
    private ImmutableMap<String, ImmutableMap<String, String>> mProductionClassifierAssetsMetadata;
    private ImmutableMap<String, ImmutableMap<String, String>> mTestClassifierAssetsMetadata;
    private ModelManager mTestModelManager;
    private ModelManager mProductionModelManager;
    private static final String TEST_LABELS_FILE_PATH = "classifier/labels_test_topics.txt";
    private static final String TEST_APPS_FILE_PATH = "classifier/precomputed_test_app_list.csv";
    private static final String TEST_CLASSIFIER_ASSETS_METADATA_FILE_PATH =
            "classifier/classifier_test_assets_metadata.json";
    private static final String PRODUCTION_LABELS_FILE_PATH = "classifier/labels_topics.txt";
    private static final String PRODUCTION_APPS_FILE_PATH = "classifier/precomputed_app_list.csv";
    private static final String PRODUCTION_CLASSIFIER_ASSETS_METADATA_FILE_PATH =
            "classifier/classifier_assets_metadata.json";
    private static final String MODEL_FILE_PATH = "classifier/model.tflite";

    @Mock SynchronousFileStorage mMockFileStorage;
    @Mock Map<String, ClientFile> mMockDownloadedFiles;
    private MockitoSession mMockitoSession = null;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mMockitoSession =
                ExtendedMockito.mockitoSession()
                        .spyStatic(FlagsFactory.class)
                        .initMocks(this)
                        .strictness(Strictness.WARN)
                        .startMocking();
    }

    @After
    public void tearDown() {
        if (mMockitoSession != null) {
            mMockitoSession.finishMocking();
        }
    }

    @Test
    public void testGetInstance() {
        ExtendedMockito.doReturn(FlagsFactory.getFlagsForTest()).when(FlagsFactory::getFlags);
        ModelManager firstInstance = ModelManager.getInstance(sContext);
        ModelManager secondInstance = ModelManager.getInstance(sContext);

        assertThat(firstInstance).isNotNull();
        assertThat(secondInstance).isNotNull();
        assertThat(firstInstance).isEqualTo(secondInstance);
    }

    @Test
    public void testRetrieveModel_bundled() throws IOException {
        mProductionModelManager =
                new ModelManager(
                        sContext,
                        PRODUCTION_LABELS_FILE_PATH,
                        PRODUCTION_APPS_FILE_PATH,
                        PRODUCTION_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);

        ByteBuffer byteBuffer = mProductionModelManager.retrieveModel();
        // Check byteBuffer capacity greater than 0 when retrieveModel() finds bundled TFLite model
        // and loads file as a ByteBuffer.
        assertThat(byteBuffer.capacity()).isGreaterThan(0);
    }

    @Test
    public void testRetrieveLabels_bundled_successfulRead() {
        // Test the labels list in production assets
        // Check size of list.
        // The labels_topics.txt contains 446 topics.
        mProductionModelManager =
                new ModelManager(
                        sContext,
                        PRODUCTION_LABELS_FILE_PATH,
                        PRODUCTION_APPS_FILE_PATH,
                        PRODUCTION_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);

        mProductionLabels = mProductionModelManager.retrieveLabels();
        assertThat(mProductionLabels.size()).isEqualTo(446);

        // Check some labels.
        assertThat(mProductionLabels).containsAtLeast(10010, 10200, 10270, 10432);
    }

    @Test
    public void testRetrieveLabels_downloaded_successfulRead() throws IOException {
        // Mock a MDD FileGroup and FileStorage
        when(mMockFileStorage.open(any(), any()))
                .thenReturn(sContext.getAssets().open(PRODUCTION_LABELS_FILE_PATH));

        // Test the labels list in MDD downloaded label.
        // Check size of list.
        // The labels_topics.txt contains 446 topics.
        mProductionModelManager =
                new ModelManager(
                        sContext,
                        PRODUCTION_LABELS_FILE_PATH,
                        PRODUCTION_APPS_FILE_PATH,
                        PRODUCTION_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);
        mProductionLabels = mProductionModelManager.retrieveLabels();
        assertThat(mProductionLabels.size()).isEqualTo(446);

        // Check some labels.
        assertThat(mProductionLabels).containsAtLeast(10010, 10200, 10270, 10432);
    }

    @Test
    public void testRetrieveLabels_bundled_emptyListReturnedOnException() {
        mProductionModelManager =
                new ModelManager(
                        sContext,
                        "WrongFilePath",
                        "WrongFilePath",
                        "WrongFilePath",
                        "WrongFilePath",
                        mMockFileStorage,
                        mMockDownloadedFiles);

        mProductionLabels = mProductionModelManager.retrieveLabels();
        ImmutableList<Integer> labels = mProductionModelManager.retrieveLabels();
        // Check empty list returned.
        assertThat(labels).isEmpty();
    }

    @Test
    public void testRetrieveLabels_downloaded_emptyListReturnedOnException() throws IOException {
        // Mock a MDD FileGroup and FileStorage
        when(mMockFileStorage.open(any(), any())).thenReturn(FileInputStream.nullInputStream());
        mProductionModelManager =
                new ModelManager(
                        sContext,
                        "WrongFilePath",
                        "WrongFilePath",
                        "WrongFilePath",
                        "WrongFilePath",
                        mMockFileStorage,
                        mMockDownloadedFiles);

        mProductionLabels = mProductionModelManager.retrieveLabels();
        ImmutableList<Integer> labels = mProductionModelManager.retrieveLabels();
        // Check empty list returned.
        assertThat(labels).isEmpty();
    }

    @Test
    public void testLoadedAppTopics_bundled() {
        mTestModelManager =
                new ModelManager(
                        sContext,
                        TEST_LABELS_FILE_PATH,
                        TEST_APPS_FILE_PATH,
                        TEST_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);

        Map<String, List<Integer>> appTopic = mTestModelManager.retrieveAppClassificationTopics();
        // Check size of map
        // The app topics file contains 10000 apps + 11 sample apps + 2 test valid topics' apps
        // + 1 end2end test app + 1 cts test app + 1 empty app.
        assertThat(appTopic.size()).isEqualTo(10016);

        // Check android messaging, chrome and a sample app topics in map
        // The topicId of "com.google.android.apps.messaging" in
        // assets/precomputed_test_app_list.csv is 10281, 10280
        List<Integer> androidMessagingTopics = Arrays.asList(10281, 10280);
        assertThat(appTopic.get("com.google.android.apps.messaging"))
                .isEqualTo(androidMessagingTopics);

        // The topicId of "com.android.chrome" in assets/precomputed_test_app_list.csv
        // is 10185
        List<Integer> chromeTopics = Arrays.asList(10185);
        assertThat(appTopic.get("com.android.chrome")).isEqualTo(chromeTopics);

        // The topicIds of "com.example.adservices.samples.topics.sampleapp" in
        // assets/precomputed_test_app_list.csv are 10222, 10223, 10116, 10243, 10254
        String sampleAppPrefix = "com.example.adservices.samples.topics.sampleapp";
        List<Integer> sampleAppTopics = Arrays.asList(10222, 10223, 10116, 10243, 10254);
        assertThat(appTopic.get(sampleAppPrefix)).isEqualTo(sampleAppTopics);

        // The topicIds of "com.example.adservices.samples.topics.sampleapp4" in
        // assets/precomputed_test_app_list.csv are 10253, 10146, 10227, 10390, 10413
        List<Integer> sampleApp4Topics = Arrays.asList(10253, 10146, 10227, 10390, 10413);
        assertThat(appTopic.get(sampleAppPrefix + "4")).isEqualTo(sampleApp4Topics);

        // Check if all sample apps have 5 unique topics
        for (int appIndex = 1; appIndex <= 10; appIndex++) {
            assertThat(new HashSet<>(appTopic.get(sampleAppPrefix + appIndex)).size()).isEqualTo(5);
        }

        // Verify that the topics from the file are valid:
        // the valid topic is one of the topic in the labels file.
        // The invalid topics will not be loaded in the app topics map.
        String validTestAppPrefix = "com.example.adservices.valid.topics.testapp";

        // The valid topicIds of "com.example.adservices.valid.topics.testapp1" in
        // assets/precomputed_test_app_list.csv are 10147, 10253, 10254
        List<Integer> validTestApp1Topics = Arrays.asList(10147, 10253, 10254);
        assertThat(appTopic.get(validTestAppPrefix + "1")).isEqualTo(validTestApp1Topics);

        // The valid topicIds of "com.example.adservices.valid.topics.testapp2" in
        // assets/precomputed_test_app_list.csv are 143, 15
        List<Integer> validTestApp2Topics = Arrays.asList(10253, 10254);
        assertThat(appTopic.get(validTestAppPrefix + "2")).isEqualTo(validTestApp2Topics);
    }

    @Test
    public void testAppsWithOnlyEmptyTopics() {
        mTestModelManager =
                new ModelManager(
                        sContext,
                        TEST_LABELS_FILE_PATH,
                        TEST_APPS_FILE_PATH,
                        TEST_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);
        // Load precomputed labels from the test source `assets/precomputed_test_app_list.csv`
        Map<String, List<Integer>> appTopic = mTestModelManager.retrieveAppClassificationTopics();

        // The app com.emptytopics has empty topic in `assets/precomputed_test_app_list.csv`
        String emptyTopicsAppId = "com.emptytopics";

        // Verify the topic list of this app is empty.
        assertThat(appTopic.get(emptyTopicsAppId)).isEmpty();

        // Check app com.google.chromeremotedesktop has empty topic
        // in `assets/precomputed_test_app_list.csv`
        String chromeRemoteDesktopAppId = "com.google.chromeremotedesktop";

        // Verify the topic list of this app is empty.
        assertThat(appTopic.get(chromeRemoteDesktopAppId)).isEmpty();
    }

    @Test
    public void testGetTestClassifierAssetsMetadata_correctFormat() {
        mTestModelManager =
                new ModelManager(
                        sContext,
                        TEST_LABELS_FILE_PATH,
                        TEST_APPS_FILE_PATH,
                        TEST_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);
        // There should contain 6 assets and 1 property in classifier_test_assets_metadata.json.
        // The asset without "asset_name" or "property" will not be stored in the map.
        mTestClassifierAssetsMetadata = mTestModelManager.retrieveClassifierAssetsMetadata();
        assertThat(mTestClassifierAssetsMetadata).hasSize(7);

        // The property of metadata with correct format should contain 3 attributions:
        // "taxonomy_type", "taxonomy_version", "updated_date".
        // The key name of property is "version_info"
        assertThat(mTestClassifierAssetsMetadata.get("version_info")).hasSize(3);
        assertThat(mTestClassifierAssetsMetadata.get("version_info").keySet()).containsExactly(
                "taxonomy_type", "taxonomy_version", "updated_date");

        // The property "version_info" should have attribution "taxonomy_version"
        // and its value should be "12".
        assertThat(mTestClassifierAssetsMetadata.get("version_info").get("taxonomy_version"))
                .isEqualTo("12");

        // The property "version_info" should have attribution "taxonomy_type"
        // and its value should be "chrome_and_mobile_taxonomy".
        assertThat(mTestClassifierAssetsMetadata.get("version_info").get("taxonomy_type"))
                .isEqualTo("chrome_and_mobile_taxonomy");

        // The metadata of 1 asset with correct format should contain 4 attributions:
        // "asset_version", "path", "checksum", "updated_date".
        // Check if "labels_topics" asset has the correct format.
        assertThat(mTestClassifierAssetsMetadata.get("labels_topics")).hasSize(4);
        assertThat(mTestClassifierAssetsMetadata.get("labels_topics").keySet()).containsExactly(
                "asset_version", "path", "checksum", "updated_date");

        // The asset "labels_topics" should have attribution "asset_version" and its value should be
        // "34"
        assertThat(mTestClassifierAssetsMetadata.get("labels_topics").get("asset_version"))
                .isEqualTo("34");

        // The asset "labels_topics" should have attribution "path" and its value should be
        // "assets/classifier/labels_test_topics.txt"
        assertThat(mTestClassifierAssetsMetadata.get("labels_topics").get("path"))
                .isEqualTo("assets/classifier/labels_test_topics.txt");

        // The asset "labels_topics" should have attribution "updated_date" and its value should be
        // "2022-07-29"
        assertThat(mTestClassifierAssetsMetadata.get("labels_topics").get("updated_date"))
                .isEqualTo("2022-07-29");

        // There should contain 4 metadata attributions in asset "topic_id_to_name"
        assertThat(mTestClassifierAssetsMetadata.get("topic_id_to_name")).hasSize(4);

        // The asset "topic_id_to_name" should have attribution "path" and its value should be
        // "assets/classifier/topic_id_to_name.csv"
        assertThat(mTestClassifierAssetsMetadata.get("topic_id_to_name").get("path"))
                .isEqualTo("assets/classifier/topic_id_to_name.csv");

        // The asset "precomputed_app_list" should have attribution "checksum" and
        // its value should be "6c4fa0e24cf67c0e830d05196f2b8e66824ca0ebf6ade3229cdd3dedf63cbb96"
        assertThat(mTestClassifierAssetsMetadata.get("precomputed_app_list").get("checksum"))
                .isEqualTo("6c4fa0e24cf67c0e830d05196f2b8e66824ca0ebf6ade3229cdd3dedf63cbb96");
    }

    @Test
    public void testGetProductionClassifierAssetsMetadata_bundled_correctFormat() {
        mProductionModelManager =
                new ModelManager(
                        sContext,
                        PRODUCTION_LABELS_FILE_PATH,
                        PRODUCTION_APPS_FILE_PATH,
                        PRODUCTION_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);

        mProductionClassifierAssetsMetadata =
                mProductionModelManager.retrieveClassifierAssetsMetadata();
        // There should contain 4 assets and 1 property in classifier_assets_metadata.json.
        assertThat(mProductionClassifierAssetsMetadata).hasSize(5);

        // The property of metadata in production metadata should contain 4 attributions:
        // "taxonomy_type", "taxonomy_version", "updated_date".
        // The key name of property is "version_info"
        assertThat(mProductionClassifierAssetsMetadata.get("version_info")).hasSize(3);
        assertThat(mProductionClassifierAssetsMetadata.get("version_info").keySet())
                .containsExactly("taxonomy_type", "taxonomy_version", "updated_date");

        // The property "version_info" should have attribution "taxonomy_version"
        // and its value should be "2".
        assertThat(mProductionClassifierAssetsMetadata.get("version_info").get("taxonomy_version"))
                .isEqualTo("2");

        // The property "version_info" should have attribution "taxonomy_type"
        // and its value should be "chrome_and_mobile_taxonomy".
        assertThat(mProductionClassifierAssetsMetadata.get("version_info").get("taxonomy_type"))
                .isEqualTo("chrome_and_mobile_taxonomy");

        // The metadata of 1 asset in production metadata should contain 4 attributions:
        // "asset_version", "path", "checksum", "updated_date".
        // Check if "labels_topics" asset has the correct format.
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics")).hasSize(4);
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics").keySet())
                .containsExactly("asset_version", "path", "checksum", "updated_date");

        // The asset "labels_topics" should have attribution "asset_version" and its value should be
        // "2"
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics").get("asset_version"))
                .isEqualTo("2");

        // The asset "labels_topics" should have attribution "path" and its value should be
        // "assets/classifier/labels_topics.txt"
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics").get("path"))
                .isEqualTo("assets/classifier/labels_topics.txt");

        // The asset "labels_topics" should have attribution "updated_date" and its value should be
        // "2022-07-29"
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics").get("updated_date"))
                .isEqualTo("2022-07-29");

        // There should contain 5 metadata attributions in asset "topic_id_to_name"
        assertThat(mProductionClassifierAssetsMetadata.get("topic_id_to_name")).hasSize(4);

        // The asset "topic_id_to_name" should have attribution "path" and its value should be
        // "assets/classifier/topic_id_to_name.csv"
        assertThat(mProductionClassifierAssetsMetadata.get("topic_id_to_name").get("path"))
                .isEqualTo("assets/classifier/topic_id_to_name.csv");

        // The asset "precomputed_app_list" should have attribution "checksum" and
        // its value should be "780c4edecfe703bdddd57bd84ea7860bba2577f66f2d759558cfe9be3186bece"
        assertThat(mProductionClassifierAssetsMetadata.get("precomputed_app_list").get("checksum"))
                .isEqualTo("780c4edecfe703bdddd57bd84ea7860bba2577f66f2d759558cfe9be3186bece");
    }

    @Test
    public void testGetProductionClassifierAssetsMetadata_downloaded_correctFormat()
            throws IOException {
        // Mock a MDD FileGroup and FileStorage
        when(mMockFileStorage.open(any(), any()))
                .thenReturn(
                        sContext.getAssets().open(PRODUCTION_CLASSIFIER_ASSETS_METADATA_FILE_PATH));
        mProductionModelManager =
                new ModelManager(
                        sContext,
                        PRODUCTION_LABELS_FILE_PATH,
                        PRODUCTION_APPS_FILE_PATH,
                        PRODUCTION_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);

        mProductionClassifierAssetsMetadata =
                mProductionModelManager.retrieveClassifierAssetsMetadata();
        // There should contain 4 assets and 1 property in classifier_assets_metadata.json.
        assertThat(mProductionClassifierAssetsMetadata).hasSize(5);

        // The property of metadata in production metadata should contain 4 attributions:
        // "taxonomy_type", "taxonomy_version", "updated_date".
        // The key name of property is "version_info"
        assertThat(mProductionClassifierAssetsMetadata.get("version_info")).hasSize(3);
        assertThat(mProductionClassifierAssetsMetadata.get("version_info").keySet())
                .containsExactly("taxonomy_type", "taxonomy_version", "updated_date");

        // The property "version_info" should have attribution "taxonomy_version"
        // and its value should be "2".
        assertThat(mProductionClassifierAssetsMetadata.get("version_info").get("taxonomy_version"))
                .isEqualTo("2");

        // The property "version_info" should have attribution "taxonomy_type"
        // and its value should be "chrome_and_mobile_taxonomy".
        assertThat(mProductionClassifierAssetsMetadata.get("version_info").get("taxonomy_type"))
                .isEqualTo("chrome_and_mobile_taxonomy");

        // The metadata of 1 asset in production metadata should contain 4 attributions:
        // "asset_version", "path", "checksum", "updated_date".
        // Check if "labels_topics" asset has the correct format.
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics")).hasSize(4);
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics").keySet())
                .containsExactly("asset_version", "path", "checksum", "updated_date");

        // The asset "labels_topics" should have attribution "asset_version" and its value should be
        // "2"
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics").get("asset_version"))
                .isEqualTo("2");

        // The asset "labels_topics" should have attribution "path" and its value should be
        // "assets/classifier/labels_topics.txt"
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics").get("path"))
                .isEqualTo("assets/classifier/labels_topics.txt");

        // The asset "labels_topics" should have attribution "updated_date" and its value should be
        // "2022-07-29"
        assertThat(mProductionClassifierAssetsMetadata.get("labels_topics").get("updated_date"))
                .isEqualTo("2022-07-29");

        // There should contain 5 metadata attributions in asset "topic_id_to_name"
        assertThat(mProductionClassifierAssetsMetadata.get("topic_id_to_name")).hasSize(4);

        // The asset "topic_id_to_name" should have attribution "path" and its value should be
        // "assets/classifier/topic_id_to_name.csv"
        assertThat(mProductionClassifierAssetsMetadata.get("topic_id_to_name").get("path"))
                .isEqualTo("assets/classifier/topic_id_to_name.csv");

        // The asset "precomputed_app_list" should have attribution "checksum" and
        // its value should be "780c4edecfe703bdddd57bd84ea7860bba2577f66f2d759558cfe9be3186bece"
        assertThat(mProductionClassifierAssetsMetadata.get("precomputed_app_list").get("checksum"))
                .isEqualTo("780c4edecfe703bdddd57bd84ea7860bba2577f66f2d759558cfe9be3186bece");
    }

    @Test
    public void testGetTestClassifierAssetsMetadata_wrongFormat() {
        mTestModelManager =
                new ModelManager(
                        sContext,
                        TEST_LABELS_FILE_PATH,
                        TEST_APPS_FILE_PATH,
                        TEST_CLASSIFIER_ASSETS_METADATA_FILE_PATH,
                        MODEL_FILE_PATH,
                        mMockFileStorage,
                        mMockDownloadedFiles);

        mTestClassifierAssetsMetadata = mTestModelManager.retrieveClassifierAssetsMetadata();
        // There should contain 1 metadata attributions in asset "test_asset1",
        // because it doesn't have "checksum" and "updated_date"
        mTestClassifierAssetsMetadata = mTestModelManager.retrieveClassifierAssetsMetadata();
        assertThat(mTestClassifierAssetsMetadata.get("test_asset1")).hasSize(1);

        // The asset "test_asset1" should have attribution "path" and its value should be
        // "assets/classifier/test1"
        assertThat(mTestClassifierAssetsMetadata.get("test_asset1").get("path"))
                .isEqualTo("assets/classifier/test1");

        // There should contain 4 metadata attributions in asset "test_asset2",
        // because "redundant_field1" and "redundant_field2" are not correct attributions.
        assertThat(mTestClassifierAssetsMetadata.get("test_asset2")).hasSize(4);

        // The asset "test_asset2" should have attribution "path" and its value should be
        // "assets/classifier/test2"
        assertThat(mTestClassifierAssetsMetadata.get("test_asset2").get("path"))
                .isEqualTo("assets/classifier/test2");

        // The asset "test_asset2" shouldn't have redundant attribution "redundant_field1"
        assertThat(mTestClassifierAssetsMetadata.get("test_asset2"))
                .doesNotContainKey("redundant_field1");
    }
}