DB changes for persisting encoder logics
Bug: 294899887
Test: Added unit tests
Change-Id: Ie4e1ac3adf7c471988de2a7204a4884fbe141e84
diff --git a/adservices/service-core/java/com/android/adservices/data/signals/DBEncoderLogic.java b/adservices/service-core/java/com/android/adservices/data/signals/DBEncoderLogic.java
new file mode 100644
index 0000000..4800955
--- /dev/null
+++ b/adservices/service-core/java/com/android/adservices/data/signals/DBEncoderLogic.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2023 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.signals;
+
+import android.adservices.common.AdTechIdentifier;
+
+import androidx.annotation.NonNull;
+import androidx.room.ColumnInfo;
+import androidx.room.Entity;
+import androidx.room.PrimaryKey;
+
+import com.google.auto.value.AutoValue;
+
+import java.time.Instant;
+
+/**
+ * Represents an entry for encoder logic for a buyer, the actual logic will be persisted on the
+ * device as flat files. This DB entry is meant for record keeping of file-system storage.
+ */
+@AutoValue
+@AutoValue.CopyAnnotations
+@Entity(tableName = DBEncoderLogic.TABLE_NAME, inheritSuperIndices = true)
+public abstract class DBEncoderLogic {
+
+ public static final String TABLE_NAME = "encoder_logics";
+
+ /** The ad-tech buyer who owns the logic */
+ @AutoValue.CopyAnnotations
+ @ColumnInfo(name = "buyer")
+ @PrimaryKey
+ @NonNull
+ public abstract AdTechIdentifier getBuyer();
+
+ /** The version provided by ad-tech when this encoding logic was downloaded */
+ @AutoValue.CopyAnnotations
+ @ColumnInfo(name = "version")
+ public abstract int getVersion();
+
+ /** The time at which this entry for encoding logic was persisted */
+ @AutoValue.CopyAnnotations
+ @ColumnInfo(name = "creation_time", index = true)
+ @NonNull
+ public abstract Instant getCreationTime();
+
+ /**
+ * @return an instance of {@link DBEncoderLogic}
+ */
+ public static DBEncoderLogic create(
+ @NonNull AdTechIdentifier buyer, int version, @NonNull Instant creationTime) {
+ return builder().setBuyer(buyer).setVersion(version).setCreationTime(creationTime).build();
+ }
+
+ /**
+ * @return a builder for creating a {@link DBEncoderLogic}
+ */
+ public static DBEncoderLogic.Builder builder() {
+ return new AutoValue_DBEncoderLogic.Builder();
+ }
+
+ /** Provides a builder to create an instance of {@link DBEncoderLogic} */
+ @AutoValue.Builder
+ public abstract static class Builder {
+
+ /** For more details see {@link #getBuyer()} */
+ public abstract Builder setBuyer(@NonNull AdTechIdentifier value);
+
+ /** For more details see {@link #getVersion()} */
+ public abstract Builder setVersion(int value);
+
+ /** For more details see {@link #getCreationTime()} */
+ public abstract Builder setCreationTime(@NonNull Instant value);
+
+ /**
+ * @return an instance of {@link DBEncoderLogic}
+ */
+ public abstract DBEncoderLogic build();
+ }
+}
diff --git a/adservices/tests/unittest/service-core/src/com/android/adservices/data/signals/DBEncoderLogicTest.java b/adservices/tests/unittest/service-core/src/com/android/adservices/data/signals/DBEncoderLogicTest.java
new file mode 100644
index 0000000..f2df8df
--- /dev/null
+++ b/adservices/tests/unittest/service-core/src/com/android/adservices/data/signals/DBEncoderLogicTest.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2023 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.signals;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertThrows;
+
+import android.adservices.common.AdTechIdentifier;
+import android.adservices.common.CommonFixture;
+
+import org.junit.Test;
+
+import java.time.Instant;
+
+public class DBEncoderLogicTest {
+
+ @Test
+ public void testCreateEncodingLogic() {
+ AdTechIdentifier buyer = CommonFixture.VALID_BUYER_1;
+ int version = 1;
+ Instant time = CommonFixture.FIXED_NOW;
+
+ DBEncoderLogic logicEntry = DBEncoderLogic.create(buyer, version, time);
+ assertEquals(buyer, logicEntry.getBuyer());
+ assertEquals(version, (int) logicEntry.getVersion());
+ assertEquals(time, logicEntry.getCreationTime());
+ }
+
+ @Test
+ public void testBuildEncodingLogic() {
+ AdTechIdentifier buyer = CommonFixture.VALID_BUYER_1;
+ int version = 1;
+ Instant time = CommonFixture.FIXED_NOW;
+
+ DBEncoderLogic logicEntry =
+ DBEncoderLogic.builder()
+ .setBuyer(buyer)
+ .setVersion(version)
+ .setCreationTime(time)
+ .build();
+ assertEquals(buyer, logicEntry.getBuyer());
+ assertEquals(version, (int) logicEntry.getVersion());
+ assertEquals(time, logicEntry.getCreationTime());
+ }
+
+ @Test
+ public void testNullFails() {
+ assertThrows(
+ IllegalStateException.class,
+ () -> {
+ DBEncoderLogic.builder().build();
+ });
+ }
+
+ @Test
+ public void testEquals() {
+ AdTechIdentifier buyer = CommonFixture.VALID_BUYER_1;
+ int version = 1;
+ Instant time = CommonFixture.FIXED_NOW;
+
+ DBEncoderLogic logicEntry1 =
+ DBEncoderLogic.builder()
+ .setBuyer(buyer)
+ .setVersion(version)
+ .setCreationTime(time)
+ .build();
+ DBEncoderLogic logicEntry2 =
+ DBEncoderLogic.builder()
+ .setBuyer(buyer)
+ .setVersion(version)
+ .setCreationTime(time)
+ .build();
+
+ assertEquals(logicEntry1, logicEntry2);
+ }
+
+ @Test
+ public void testNotEquals() {
+ AdTechIdentifier buyer = CommonFixture.VALID_BUYER_1;
+ int version = 1;
+ Instant time = CommonFixture.FIXED_NOW;
+
+ DBEncoderLogic logicEntry1 =
+ DBEncoderLogic.builder()
+ .setBuyer(buyer)
+ .setVersion(version)
+ .setCreationTime(time)
+ .build();
+ DBEncoderLogic logicEntry2 =
+ DBEncoderLogic.builder()
+ .setBuyer(buyer)
+ .setVersion(version + 1)
+ .setCreationTime(time)
+ .build();
+
+ assertNotEquals(logicEntry1, logicEntry2);
+ }
+
+ @Test
+ public void testHashCode() {
+ AdTechIdentifier buyer = CommonFixture.VALID_BUYER_1;
+ int version = 1;
+ Instant time = CommonFixture.FIXED_NOW;
+
+ DBEncoderLogic logicEntry1 =
+ DBEncoderLogic.builder()
+ .setBuyer(buyer)
+ .setVersion(version)
+ .setCreationTime(time)
+ .build();
+ DBEncoderLogic logicEntry2 =
+ DBEncoderLogic.builder()
+ .setBuyer(buyer)
+ .setVersion(version)
+ .setCreationTime(time)
+ .build();
+
+ assertEquals(logicEntry1.hashCode(), logicEntry2.hashCode());
+ }
+}