1 /*
2  * Copyright (C) 2023 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 com.android.adservices.data.measurement.migration;
18 
19 import static com.android.adservices.common.DbTestUtil.doesTableExistAndColumnCountMatch;
20 import static com.android.adservices.common.DbTestUtil.getDbHelperForTest;
21 import static com.android.adservices.data.measurement.migration.MigrationTestHelper.populateDb;
22 
23 import static org.junit.Assert.assertTrue;
24 
25 import android.content.ContentValues;
26 import android.database.sqlite.SQLiteDatabase;
27 
28 import com.android.adservices.data.measurement.MeasurementDbHelper;
29 import com.android.adservices.data.measurement.MeasurementTables;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.junit.MockitoJUnitRunner;
34 
35 import java.util.ArrayList;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import java.util.Map;
39 
40 @RunWith(MockitoJUnitRunner.class)
41 public class MeasurementDbMigratorV17Test extends MeasurementDbMigratorTestBase {
42     @Test
performMigration_v16ToV17()43     public void performMigration_v16ToV17() {
44         // Setup
45         MeasurementDbHelper dbHelper =
46                 new MeasurementDbHelper(
47                         sContext,
48                         MEASUREMENT_DATABASE_NAME_FOR_MIGRATION,
49                         16,
50                         getDbHelperForTest());
51         SQLiteDatabase db = dbHelper.getWritableDatabase();
52 
53         Map<String, List<ContentValues>> dataV16 = createFakeDataDebugReportV16();
54         populateDb(db, dataV16);
55 
56         // Execution
57         getTestSubject().performMigration(db, 16, 17);
58 
59         assertTrue(
60                 doesTableExistAndColumnCountMatch(
61                         db, MeasurementTables.DebugReportContract.TABLE, 6));
62     }
63 
createFakeDataDebugReportV16()64     private Map<String, List<ContentValues>> createFakeDataDebugReportV16() {
65         Map<String, List<ContentValues>> tableRowsMap = new LinkedHashMap<>();
66         List<ContentValues> rows = new ArrayList<>();
67         rows.add(ContentValueFixtures.generateDebugReportContentValuesV16());
68         tableRowsMap.put(MeasurementTables.DebugReportContract.TABLE, rows);
69         return tableRowsMap;
70     }
71 
72     @Override
getTargetVersion()73     int getTargetVersion() {
74         return 17;
75     }
76 
77     @Override
getTestSubject()78     AbstractMeasurementDbMigrator getTestSubject() {
79         return new MeasurementDbMigratorV17();
80     }
81 }
82