summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration
diff options
context:
space:
mode:
Diffstat (limited to 'adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration')
-rw-r--r--adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/AbstractTopicsDbMigratorTest.java66
-rw-r--r--adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/TopicDbMigratorV3Test.java67
-rw-r--r--adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/TopicsDbHelperV2.java98
3 files changed, 231 insertions, 0 deletions
diff --git a/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/AbstractTopicsDbMigratorTest.java b/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/AbstractTopicsDbMigratorTest.java
new file mode 100644
index 0000000000..0f4317024c
--- /dev/null
+++ b/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/AbstractTopicsDbMigratorTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.data.topics.migration;
+
+import static org.junit.Assert.assertThrows;
+
+import android.database.sqlite.SQLiteDatabase;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/** Unit tests for {@link com.android.adservices.data.topics.migration.AbstractTopicsDbMigrator} */
+public class AbstractTopicsDbMigratorTest {
+ @Mock private SQLiteDatabase mDb;
+
+ @Before
+ public void setup() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void testPerformMigration_onUpgrade() {
+ // Test targetVersion is newer than newVersion on upgrading
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ initMigrator(/* targetVersion */ 3)
+ .performMigration(mDb, /* oldVersion */ 1, /* newVersion */ 2));
+
+ // Test targetVersion is not newer than oldVersion on upgrading
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ initMigrator(/* targetVersion */ 1)
+ .performMigration(mDb, /* oldVersion */ 1, /* newVersion */ 2));
+
+ // Test to perform on Upgrading
+ initMigrator(/* targetVersion */ 2)
+ .performMigration(mDb, /* oldVersion */ 1, /* newVersion */ 2);
+ }
+
+ // Initialize the abstractTopicsDbMigrator with a target version. Use isPerformed to indicate
+ // when performMigration is invoked.
+ private AbstractTopicsDbMigrator initMigrator(int targetVersion) {
+ return new AbstractTopicsDbMigrator(targetVersion) {
+ @Override
+ public void performMigration(SQLiteDatabase db) {}
+ };
+ }
+}
diff --git a/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/TopicDbMigratorV3Test.java b/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/TopicDbMigratorV3Test.java
new file mode 100644
index 0000000000..a6e535631d
--- /dev/null
+++ b/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/TopicDbMigratorV3Test.java
@@ -0,0 +1,67 @@
+/*
+ * 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.data.topics.migration;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.adservices.data.DbTestUtil;
+import com.android.adservices.data.topics.TopicsTables;
+
+import org.junit.Test;
+
+/** Unit tests for {@link com.android.adservices.data.topics.migration.TopicDbMigratorV3} */
+public class TopicDbMigratorV3Test {
+ private static final Context sContext = ApplicationProvider.getApplicationContext();
+ // The database is created with V2 and will migrate to V3.
+ private final TopicsDbHelperV2 mTopicsDbHelper = TopicsDbHelperV2.getInstance(sContext);
+
+ @Test
+ public void testDbMigrationFromV2ToV3() {
+ SQLiteDatabase db = mTopicsDbHelper.getWritableDatabase();
+
+ // TopicContributors table doesn't exist in V2
+ assertThat(
+ DbTestUtil.doesTableExistAndColumnCountMatch(
+ db,
+ TopicsTables.TopicContributorsContract.TABLE,
+ /* number Of Columns */ 4))
+ .isFalse();
+
+ // Use transaction here so that the changes in the performMigration is committed.
+ // In normal DB upgrade, the DB will commit the change automatically.
+ db.beginTransaction();
+ // Upgrade the db V3 by using TopicDbMigratorV3
+ new TopicDbMigratorV3().performMigration(db);
+ // Commit the schema change
+ db.setTransactionSuccessful();
+ db.endTransaction();
+
+ // TopicContributors table exists in V3
+ db = mTopicsDbHelper.getReadableDatabase();
+ assertThat(
+ DbTestUtil.doesTableExistAndColumnCountMatch(
+ db,
+ TopicsTables.TopicContributorsContract.TABLE,
+ /* number Of Columns */ 4))
+ .isTrue();
+ }
+}
diff --git a/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/TopicsDbHelperV2.java b/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/TopicsDbHelperV2.java
new file mode 100644
index 0000000000..58fcabb44f
--- /dev/null
+++ b/adservices/tests/unittest/service-core/src/com/android/adservices/data/topics/migration/TopicsDbHelperV2.java
@@ -0,0 +1,98 @@
+/*
+ * 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.data.topics.migration;
+
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_APP_CLASSIFICATION_TOPICS;
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_APP_USAGE_HISTORY;
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_BLOCKED_TOPICS;
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_CALLER_CAN_LEARN_TOPICS;
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_EPOCH_ORIGIN;
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_RETURNED_TOPIC;
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_TOPICS_TAXONOMY;
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_TOP_TOPICS;
+import static com.android.adservices.data.topics.TopicsTables.CREATE_TABLE_USAGE_HISTORY;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.annotation.NonNull;
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+
+import com.android.adservices.data.DbHelper;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Class to get the state of Topics' database on version 2 for test purpose. V2 doesn't include
+ * TopicContributors Table.
+ */
+public class TopicsDbHelperV2 extends DbHelper {
+ private static final int CURRENT_DATABASE_VERSION = 2;
+ // TODO(b/255964885): Consolidate DB Migrator Class across Rubidium
+ private static final String DATABASE_NAME_TOPICS_MIGRATION = "adservices_topics_migration.db";
+ private static TopicsDbHelperV2 sSingleton = null;
+
+ TopicsDbHelperV2(Context context, String dbName, int dbVersion) {
+ super(context, dbName, dbVersion);
+ }
+
+ /** Returns an instance of the DbHelper given a context. */
+ @NonNull
+ public static TopicsDbHelperV2 getInstance(@NonNull Context context) {
+ synchronized (TopicsDbHelperV2.class) {
+ if (sSingleton == null) {
+ clearDatabase(context);
+ sSingleton =
+ new TopicsDbHelperV2(
+ context, DATABASE_NAME_TOPICS_MIGRATION, CURRENT_DATABASE_VERSION);
+ }
+ return sSingleton;
+ }
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ for (String sql : TOPICS_CREATE_STATEMENT_V2) {
+ db.execSQL(sql);
+ }
+ }
+
+ // Clear the database. Ensure there is no stale database with a different version existed.
+ private static void clearDatabase(@NonNull Context context) {
+ File databaseFile = context.getDatabasePath(DATABASE_NAME_TOPICS_MIGRATION);
+ if (databaseFile.exists()) {
+ assertThat(databaseFile.delete()).isTrue();
+ }
+ }
+
+ // This will create tables for DB V2.
+ private static final List<String> TOPICS_CREATE_STATEMENT_V2 =
+ Collections.unmodifiableList(
+ Arrays.asList(
+ CREATE_TABLE_TOPICS_TAXONOMY,
+ CREATE_TABLE_APP_CLASSIFICATION_TOPICS,
+ CREATE_TABLE_TOP_TOPICS,
+ CREATE_TABLE_RETURNED_TOPIC,
+ CREATE_TABLE_USAGE_HISTORY,
+ CREATE_TABLE_APP_USAGE_HISTORY,
+ CREATE_TABLE_CALLER_CAN_LEARN_TOPICS,
+ CREATE_TABLE_BLOCKED_TOPICS,
+ CREATE_TABLE_EPOCH_ORIGIN));
+}