• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.logging;
18 
19 import static com.android.adservices.common.logging.ErrorLogUtilCall.None;
20 import static com.android.adservices.common.logging.annotations.ExpectErrorLogUtilCall.ANNOTATION_NAME;
21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer;
22 
23 import static org.mockito.ArgumentMatchers.anyInt;
24 
25 import android.util.Log;
26 
27 import com.android.adservices.common.logging.annotations.ExpectErrorLogUtilCall;
28 import com.android.adservices.common.logging.annotations.ExpectErrorLogUtilCalls;
29 import com.android.adservices.common.logging.annotations.SetErrorLogUtilDefaultParams;
30 import com.android.adservices.errorlogging.ErrorLogUtil;
31 import com.android.adservices.shared.testing.AbstractLogVerifier;
32 import com.android.adservices.shared.testing.TestHelper;
33 
34 import com.google.common.collect.ImmutableList;
35 import com.google.common.collect.ImmutableSet;
36 
37 import org.junit.runner.Description;
38 
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Set;
42 import java.util.stream.Collectors;
43 
44 /** Log verifier for {@code ErrorLogUtil.e(int, int)} invocations. */
45 public final class AdServicesErrorLogUtilVerifier extends AbstractLogVerifier<ErrorLogUtilCall> {
46     @Override
mockLogCalls()47     protected void mockLogCalls() {
48         // Mock ErrorLogUtil.e(int, int) calls and capture logging arguments.
49         doAnswer(
50                         invocation -> {
51                             recordActualCall(
52                                     new ErrorLogUtilCall(
53                                             None.class,
54                                             invocation.getArgument(0),
55                                             invocation.getArgument(1)));
56                             return null;
57                         })
58                 .when(() -> ErrorLogUtil.e(anyInt(), anyInt()));
59     }
60 
61     @Override
getExpectedLogCalls(Description description)62     public Set<ErrorLogUtilCall> getExpectedLogCalls(Description description) {
63         List<ExpectErrorLogUtilCall> annotations = getAnnotations(description);
64         SetErrorLogUtilDefaultParams defaultParams =
65                 TestHelper.getAnnotation(description, SetErrorLogUtilDefaultParams.class);
66 
67         if (annotations.isEmpty()) {
68             Log.v(mTag, "No @" + ANNOTATION_NAME + " found over test method.");
69             return ImmutableSet.of();
70         }
71 
72         Set<ErrorLogUtilCall> expectedCalls =
73                 annotations.stream()
74                         .peek(a -> validateTimes(a.times(), ANNOTATION_NAME))
75                         .map(a -> ErrorLogUtilCall.createFrom(a, defaultParams))
76                         .collect(Collectors.toSet());
77 
78         if (expectedCalls.size() != annotations.size()) {
79             throw new IllegalStateException(
80                     "Detected @"
81                             + ANNOTATION_NAME
82                             + " annotations representing the same "
83                             + "invocation! De-dupe by using times arg");
84         }
85 
86         return expectedCalls;
87     }
88 
89     @Override
getResolutionMessage()90     public String getResolutionMessage() {
91         return "Please make sure to use @"
92                 + ANNOTATION_NAME
93                 + "(..) "
94                 + "over test method to denote "
95                 + "all expected ErrorLogUtil.e(int, int) calls.";
96     }
97 
getAnnotations(Description description)98     private List<ExpectErrorLogUtilCall> getAnnotations(Description description) {
99         // Scan for multiple annotation container
100         ExpectErrorLogUtilCalls multiple = description.getAnnotation(ExpectErrorLogUtilCalls.class);
101         if (multiple != null) {
102             return Arrays.stream(multiple.value()).collect(Collectors.toList());
103         }
104 
105         // Scan for single annotation
106         ExpectErrorLogUtilCall single = description.getAnnotation(ExpectErrorLogUtilCall.class);
107         return single == null ? ImmutableList.of() : ImmutableList.of(single);
108     }
109 }
110