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.stats; 18 19 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_API_CALLED; 20 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_API_CALLED__API_CLASS__FLEDGE; 21 22 import com.android.adservices.service.stats.ApiCallStats; 23 24 import org.mockito.ArgumentMatcher; 25 26 /** A FLEDGE-specific ApiCallStats matcher for use in Mockito verification. */ 27 public class FledgeApiCallStatsMatcher implements ArgumentMatcher<ApiCallStats> { 28 private final int mExpectedApiName; 29 private final int mExpectedResultCode; 30 31 /** Sets the expected result code and API name. */ FledgeApiCallStatsMatcher(int expectedApiName, int expectedResultCode)32 public FledgeApiCallStatsMatcher(int expectedApiName, int expectedResultCode) { 33 mExpectedApiName = expectedApiName; 34 mExpectedResultCode = expectedResultCode; 35 } 36 37 /** Matches on FLEDGE API stats: code, API class, API name, and result code. */ 38 @Override matches(ApiCallStats actualApiCallStats)39 public boolean matches(ApiCallStats actualApiCallStats) { 40 return actualApiCallStats.getCode() == AD_SERVICES_API_CALLED 41 && actualApiCallStats.getApiClass() == AD_SERVICES_API_CALLED__API_CLASS__FLEDGE 42 && actualApiCallStats.getApiName() == mExpectedApiName 43 && actualApiCallStats.getResultCode() == mExpectedResultCode; 44 } 45 46 /** Writes out expected fields to be matched. */ 47 @Override toString()48 public String toString() { 49 return String.format( 50 "ApiCallStats{code=%d, apiClass=%d, apiName=%d, resultCode=%d}", 51 AD_SERVICES_API_CALLED, 52 AD_SERVICES_API_CALLED__API_CLASS__FLEDGE, 53 mExpectedApiName, 54 mExpectedResultCode); 55 } 56 } 57