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 
17 package com.android.adservices.common;
18 
19 import android.adservices.common.AdData;
20 import android.adservices.common.AdDataFixture;
21 import android.adservices.common.AdTechIdentifier;
22 import android.adservices.common.CommonFixture;
23 
24 import com.android.adservices.data.common.DBAdData;
25 
26 import java.util.List;
27 import java.util.stream.Collectors;
28 
29 public class DBAdDataFixture {
30     public static final DBAdData VALID_DB_AD_DATA_NO_FILTERS =
31             getValidDbAdDataNoFiltersBuilder().build();
32 
getValidDbAdDataBuilder()33     public static DBAdData.Builder getValidDbAdDataBuilder() {
34         AdData mirrorAdData =
35                 AdDataFixture.getValidFilterAdDataByBuyer(CommonFixture.VALID_BUYER_1, 0);
36         return getValidDbAdDataNoFiltersBuilder()
37                 .setAdCounterKeys(mirrorAdData.getAdCounterKeys())
38                 .setAdFilters(mirrorAdData.getAdFilters());
39     }
40 
getValidDbAdDataNoFiltersBuilder()41     public static DBAdData.Builder getValidDbAdDataNoFiltersBuilder() {
42         AdData mirrorAdData =
43                 AdDataFixture.getValidFilterAdDataByBuyer(CommonFixture.VALID_BUYER_1, 0);
44         return new DBAdData.Builder()
45                 .setRenderUri(mirrorAdData.getRenderUri())
46                 .setMetadata(mirrorAdData.getMetadata());
47     }
48 
getValidDbAdDataNoFiltersBuilder( AdTechIdentifier buyer, int sequenceNumber)49     public static DBAdData.Builder getValidDbAdDataNoFiltersBuilder(
50             AdTechIdentifier buyer, int sequenceNumber) {
51         AdData mirrorAdData = AdDataFixture.getValidFilterAdDataByBuyer(buyer, sequenceNumber);
52         return new DBAdData.Builder()
53                 .setRenderUri(mirrorAdData.getRenderUri())
54                 .setMetadata(mirrorAdData.getMetadata());
55     }
56 
getValidDbAdDataListByBuyer(AdTechIdentifier buyer)57     public static List<DBAdData> getValidDbAdDataListByBuyer(AdTechIdentifier buyer) {
58         return AdDataFixture.getValidFilterAdsByBuyer(buyer).stream()
59                 .map(DBAdDataFixture::convertAdDataToDBAdData)
60                 .collect(Collectors.toList());
61     }
62 
getValidDbAdDataListByBuyerWithAdRenderId(AdTechIdentifier buyer)63     public static List<DBAdData> getValidDbAdDataListByBuyerWithAdRenderId(AdTechIdentifier buyer) {
64         return AdDataFixture.getValidFilterAdsWithAdRenderIdByBuyer(buyer).stream()
65                 .map(DBAdDataFixture::convertAdDataToDBAdData)
66                 .collect(Collectors.toList());
67     }
68 
69     /** Returns a list DBAdData with invalid render IDs */
getValidDbAdDataListByBuyerWithInvalidAdRenderId( AdTechIdentifier buyer)70     public static List<DBAdData> getValidDbAdDataListByBuyerWithInvalidAdRenderId(
71             AdTechIdentifier buyer) {
72         return AdDataFixture.getValidFilterAdsWithInvalidAdRenderIdByBuyer(buyer).stream()
73                 .map(DBAdDataFixture::convertAdDataToDBAdData)
74                 .collect(Collectors.toList());
75     }
76 
getValidDbAdDataListByBuyerNoFilters(AdTechIdentifier buyer)77     public static List<DBAdData> getValidDbAdDataListByBuyerNoFilters(AdTechIdentifier buyer) {
78         return AdDataFixture.getValidAdsByBuyer(buyer).stream()
79                 .map(DBAdDataFixture::convertAdDataToDBAdData)
80                 .collect(Collectors.toList());
81     }
82 
getInvalidDbAdDataListByBuyer(AdTechIdentifier buyer)83     public static List<DBAdData> getInvalidDbAdDataListByBuyer(AdTechIdentifier buyer) {
84         return AdDataFixture.getInvalidAdsByBuyer(buyer).stream()
85                 .map(DBAdDataFixture::convertAdDataToDBAdData)
86                 .collect(Collectors.toList());
87     }
88 
convertAdDataToDBAdData(AdData adData)89     public static DBAdData convertAdDataToDBAdData(AdData adData) {
90         return new DBAdData(
91                 adData.getRenderUri(),
92                 adData.getMetadata(),
93                 adData.getAdCounterKeys(),
94                 adData.getAdFilters(),
95                 adData.getAdRenderId());
96     }
97 }
98