Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.extensions.common.testing; 16 : 17 : import static com.google.common.truth.Truth.assertAbout; 18 : import static java.util.Objects.requireNonNull; 19 : 20 : import com.google.common.truth.ComparableSubject; 21 : import com.google.common.truth.FailureMetadata; 22 : import com.google.common.truth.IntegerSubject; 23 : import com.google.common.truth.StringSubject; 24 : import com.google.common.truth.Subject; 25 : import com.google.gerrit.extensions.common.GitPerson; 26 : import java.sql.Timestamp; 27 : import org.eclipse.jgit.lib.PersonIdent; 28 : 29 : public class GitPersonSubject extends Subject { 30 : 31 : public static GitPersonSubject assertThat(GitPerson gitPerson) { 32 2 : return assertAbout(gitPersons()).that(gitPerson); 33 : } 34 : 35 : public static Factory<GitPersonSubject, GitPerson> gitPersons() { 36 4 : return GitPersonSubject::new; 37 : } 38 : 39 : private final GitPerson gitPerson; 40 : 41 : private GitPersonSubject(FailureMetadata failureMetadata, GitPerson gitPerson) { 42 4 : super(failureMetadata, gitPerson); 43 4 : this.gitPerson = gitPerson; 44 4 : } 45 : 46 : public StringSubject name() { 47 3 : isNotNull(); 48 3 : return check("name").that(gitPerson.name); 49 : } 50 : 51 : public StringSubject email() { 52 3 : isNotNull(); 53 3 : return check("email").that(gitPerson.email); 54 : } 55 : 56 : public ComparableSubject<Timestamp> date() { 57 1 : isNotNull(); 58 1 : return check("date").that(gitPerson.date); 59 : } 60 : 61 : public IntegerSubject tz() { 62 1 : isNotNull(); 63 1 : return check("tz").that(gitPerson.tz); 64 : } 65 : 66 : public void hasSameDateAs(GitPerson other) { 67 0 : requireNonNull(other, "'other' GitPerson must not be null"); 68 0 : isNotNull(); 69 0 : date().isEqualTo(other.date); 70 0 : tz().isEqualTo(other.tz); 71 0 : } 72 : 73 : // TODO(issue-15508): Migrate timestamp fields in *Info/*Input classes from type Timestamp to 74 : // Instant 75 : @SuppressWarnings("JdkObsolete") 76 : public void matches(PersonIdent ident) { 77 1 : isNotNull(); 78 1 : name().isEqualTo(ident.getName()); 79 1 : email().isEqualTo(ident.getEmailAddress()); 80 1 : check("roundedDate()") 81 1 : .that(gitPerson.date.getTime()) 82 1 : .isEqualTo(ident.getWhenAsInstant().toEpochMilli()); 83 1 : tz().isEqualTo(ident.getTimeZoneOffset()); 84 1 : } 85 : }