|
| 1 | +/* |
| 2 | + * Copyright 2025 The gRPC Authors |
| 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 io.grpc; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | +import static com.google.common.base.Preconditions.checkState; |
| 21 | + |
| 22 | +import org.mockito.ArgumentMatcher; |
| 23 | + |
| 24 | +/** |
| 25 | + * Mockito matcher for {@link Status}. |
| 26 | + */ |
| 27 | +public final class StatusMatcher implements ArgumentMatcher<Status> { |
| 28 | + public static StatusMatcher statusHasCode(ArgumentMatcher<Status.Code> codeMatcher) { |
| 29 | + return new StatusMatcher(codeMatcher, null); |
| 30 | + } |
| 31 | + |
| 32 | + public static StatusMatcher statusHasCode(Status.Code code) { |
| 33 | + return statusHasCode(new EqualsMatcher<>(code)); |
| 34 | + } |
| 35 | + |
| 36 | + private final ArgumentMatcher<Status.Code> codeMatcher; |
| 37 | + private final ArgumentMatcher<String> descriptionMatcher; |
| 38 | + |
| 39 | + private StatusMatcher( |
| 40 | + ArgumentMatcher<Status.Code> codeMatcher, |
| 41 | + ArgumentMatcher<String> descriptionMatcher) { |
| 42 | + this.codeMatcher = checkNotNull(codeMatcher, "codeMatcher"); |
| 43 | + this.descriptionMatcher = descriptionMatcher; |
| 44 | + } |
| 45 | + |
| 46 | + public StatusMatcher andDescription(ArgumentMatcher<String> descriptionMatcher) { |
| 47 | + checkState(this.descriptionMatcher == null, "Already has a description matcher"); |
| 48 | + return new StatusMatcher(codeMatcher, descriptionMatcher); |
| 49 | + } |
| 50 | + |
| 51 | + public StatusMatcher andDescription(String description) { |
| 52 | + return andDescription(new EqualsMatcher<>(description)); |
| 53 | + } |
| 54 | + |
| 55 | + public StatusMatcher andDescriptionContains(String substring) { |
| 56 | + return andDescription(new StringContainsMatcher(substring)); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean matches(Status status) { |
| 61 | + return status != null |
| 62 | + && codeMatcher.matches(status.getCode()) |
| 63 | + && (descriptionMatcher == null || descriptionMatcher.matches(status.getDescription())); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String toString() { |
| 68 | + StringBuilder sb = new StringBuilder(); |
| 69 | + sb.append("{code="); |
| 70 | + sb.append(codeMatcher); |
| 71 | + if (descriptionMatcher != null) { |
| 72 | + sb.append(", description="); |
| 73 | + sb.append(descriptionMatcher); |
| 74 | + } |
| 75 | + sb.append("}"); |
| 76 | + return sb.toString(); |
| 77 | + } |
| 78 | + |
| 79 | + // Use instead of lambda for better error message. |
| 80 | + static final class EqualsMatcher<T> implements ArgumentMatcher<T> { |
| 81 | + private final T obj; |
| 82 | + |
| 83 | + EqualsMatcher(T obj) { |
| 84 | + this.obj = checkNotNull(obj, "obj"); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean matches(Object other) { |
| 89 | + return obj.equals(other); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + return obj.toString(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + static final class StringContainsMatcher implements ArgumentMatcher<String> { |
| 99 | + private final String needle; |
| 100 | + |
| 101 | + StringContainsMatcher(String needle) { |
| 102 | + this.needle = checkNotNull(needle, "needle"); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean matches(String haystack) { |
| 107 | + if (haystack == null) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + return haystack.contains(needle); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toString() { |
| 115 | + return "contains " + needle; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments