Line data Source code
1 : // Copyright (C) 2013 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.acceptance; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.common.collect.ImmutableList; 21 : import com.google.common.collect.Streams; 22 : import com.google.common.net.InetAddresses; 23 : import com.google.gerrit.common.Nullable; 24 : import com.google.gerrit.entities.Account; 25 : import com.google.gerrit.entities.Address; 26 : import java.net.InetSocketAddress; 27 : import java.util.Arrays; 28 : import org.apache.http.client.utils.URIBuilder; 29 : import org.eclipse.jgit.lib.PersonIdent; 30 : 31 : @AutoValue 32 138 : public abstract class TestAccount { 33 : public static ImmutableList<Account.Id> ids(Iterable<TestAccount> accounts) { 34 12 : return Streams.stream(accounts).map(TestAccount::id).collect(toImmutableList()); 35 : } 36 : 37 : public static ImmutableList<String> names(Iterable<TestAccount> accounts) { 38 1 : return Streams.stream(accounts).map(TestAccount::fullName).collect(toImmutableList()); 39 : } 40 : 41 : public static ImmutableList<String> names(TestAccount... accounts) { 42 1 : return names(Arrays.asList(accounts)); 43 : } 44 : 45 : static TestAccount create( 46 : Account.Id id, 47 : @Nullable String username, 48 : @Nullable String email, 49 : @Nullable String fullName, 50 : @Nullable String displayName, 51 : @Nullable String httpPassword, 52 : ImmutableList<String> tags) { 53 138 : return new AutoValue_TestAccount( 54 : id, username, email, fullName, displayName, httpPassword, tags); 55 : } 56 : 57 : public abstract Account.Id id(); 58 : 59 : @Nullable 60 : public abstract String username(); 61 : 62 : @Nullable 63 : public abstract String email(); 64 : 65 : @Nullable 66 : public abstract String fullName(); 67 : 68 : @Nullable 69 : public abstract String displayName(); 70 : 71 : @Nullable 72 : public abstract String httpPassword(); 73 : 74 : public abstract ImmutableList<String> tags(); 75 : 76 : public PersonIdent newIdent() { 77 92 : return new PersonIdent(fullName(), email()); 78 : } 79 : 80 : public String getHttpUrl(GerritServer server) { 81 6 : InetSocketAddress addr = server.getHttpAddress(); 82 6 : return new URIBuilder() 83 6 : .setScheme("http") 84 6 : .setUserInfo(username(), httpPassword()) 85 6 : .setHost(InetAddresses.toUriString(addr.getAddress())) 86 6 : .setPort(addr.getPort()) 87 6 : .toString(); 88 : } 89 : 90 : public Address getNameEmail() { 91 : // Address is weird enough that it's safer and clearer to create a new instance in a 92 : // non-abstract method rather than, say, having an abstract emailAddress() as part of this 93 : // AutoValue class. Specifically: 94 : // * Email is not specified as @Nullable in Address, but it is nullable in this class. If this 95 : // is a problem, at least it's a problem only for users of TestAccount that actually call 96 : // emailAddress(). 97 : // * Address#equals only considers email, not name, whereas TestAccount#equals should include 98 : // name. 99 11 : return Address.create(fullName(), email()); 100 : } 101 : }