google-nomulus/javatests/google/registry/testing/EppMetricSubject.java
weiminyu 7c64992c73 Cloned from CL 251456914 by 'g4 patch'.
Original change by cpovirk@cpovirk:rosie251284456-0055_Rosie:31511:citc on 2019/06/04 09:48:27.

Update to Truth 0.45, and address deprecations.

Renames may include:
- containsAllOf => containsAtLeast
- containsAllIn => containsAtLeastElementsIn
- isSameAs => isSameInstanceAs
- isOrdered => isInOrder
- isStrictlyOrdered => isInStrictOrder

The other major change is to change custom subjects to extend raw Subject instead of supplying type parameters. The type parameters are being removed from Subject. This CL will temporarily produce rawtypes warnings, which will go away when I remove the type parameters (as soon as this batch of CLs is submitted).

Some CLs in this batch also migrate calls away from actualAsString(). Its literal replacement is `"<" + actual + ">"` (unless an object overrides actualCustomStringRepresentation()), but usually I've made a larger change, such as switching from an old-style "Not true that..." failure message to one generated with the Fact API. In that case, the new code usually contains a direct reference to this.actual (a field that I occasionally had to create). Another larger change I sometimes made is to switch from a manual check-and-fail approach to instead use check(...). And sometimes I just remove a withMessage() call that's no longer necessary now that the code uses check(...), or I introduce a check(...) call. (An assertion made with check(...) automatically includes the actual value from the original subject, so there's no need to set it again with withMessage().)

Finally, there's one CL in this batch in which I migrate a Correspondence subclass to instead use Correspondence.from.

END_PUBLIC

If this is too many changes at once, let me know, and I can split it up and/or explain further. In the past, I've erred on the side of sending separate CLs for each change, which has required some owners to manually reapply each one, so now I'm trying this way.

Thanks again for your patience. There is an outside chance that this will be the last CL I send you before Truth 1.0 -- but certainly no promises :)

More information:
  Renames:
    []
    []
    []
  Removing type parameters: []
  Migration from old fail*(...) methods to new ones and to check(...): []
  Changes that replace assert*(...) with introduce check(...): []
  Correspondence subclass to Correspondence.from: []

Tested:
    TAP --sample ran all affected tests and none failed
    []

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=251921007
2019-06-07 11:46:44 -04:00

82 lines
2.9 KiB
Java

// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.testing;
import static com.google.common.truth.Fact.simpleFact;
import static com.google.common.truth.OptionalSubject.optionals;
import static com.google.common.truth.Truth.assertAbout;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import google.registry.model.eppoutput.Result.Code;
import google.registry.monitoring.whitebox.EppMetric;
import google.registry.testing.TruthChainer.And;
import java.util.Optional;
/** Utility methods for asserting things about {@link EppMetric} instances. */
public class EppMetricSubject extends Subject {
private final EppMetric actual;
public EppMetricSubject(FailureMetadata failureMetadata, EppMetric subject) {
super(failureMetadata, subject);
this.actual = subject;
}
public static EppMetricSubject assertThat(EppMetric subject) {
return assertAbout(SUBJECT_FACTORY).that(subject);
}
public And<EppMetricSubject> hasClientId(String clientId) {
return hasValue(clientId, actual.getClientId(), "getClientId()");
}
public And<EppMetricSubject> hasCommandName(String commandName) {
return hasValue(commandName, actual.getCommandName(), "getCommandName()");
}
public And<EppMetricSubject> hasStatus(Code status) {
return hasValue(status, actual.getStatus(), "getStatus()");
}
public And<EppMetricSubject> hasNoStatus() {
if (actual.getStatus().isPresent()) {
failWithActual(simpleFact("expected to have no status"));
}
return new And<>(this);
}
public And<EppMetricSubject> hasTld(String tld) {
return hasValue(tld, actual.getTld(), "getTld()");
}
public And<EppMetricSubject> hasNoTld() {
if (actual.getTld().isPresent()) {
failWithActual(simpleFact("expected to have no tld"));
}
return new And<>(this);
}
private <E> And<EppMetricSubject> hasValue(E expected, Optional<E> actual, String name) {
checkArgumentNotNull(expected, "Expected value cannot be null");
check(name).about(optionals()).that(actual).hasValue(expected);
return new And<>(this);
}
/** {@link Subject.Factory} for assertions about {@link EppMetric} objects. */
private static final Subject.Factory<EppMetricSubject, EppMetric> SUBJECT_FACTORY =
EppMetricSubject::new;
}