summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/server/index/account/AccountFieldTest.java
blob: 4d9cb76be72a86179630abee1dd5193ec181d819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) 2017 The Android Open Source Project
//
// 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 com.google.gerrit.server.index.account;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Streams;
import com.google.gerrit.entities.Account;
import com.google.gerrit.entities.RefNames;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.config.AllUsersNameProvider;
import com.google.gerrit.server.util.time.TimeUtil;
import java.util.List;
import org.eclipse.jgit.lib.ObjectId;
import org.junit.Test;

public class AccountFieldTest {
  @Test
  public void refStateFieldValues() throws Exception {
    AllUsersName allUsersName = new AllUsersName(AllUsersNameProvider.DEFAULT);
    Account.Builder account = Account.builder(Account.id(1), TimeUtil.now());
    String metaId = "0e39795bb25dc914118224995c53c5c36923a461";
    account.setMetaId(metaId);
    List<String> values =
        toStrings(AccountField.REF_STATE.get(AccountState.forAccount(account.build())));
    assertThat(values).hasSize(1);
    String expectedValue =
        allUsersName.get() + ":" + RefNames.refsUsers(account.id()) + ":" + metaId;
    assertThat(Iterables.getOnlyElement(values)).isEqualTo(expectedValue);
  }

  @Test
  public void externalIdStateFieldValues() throws Exception {
    Account.Id id = Account.id(1);
    Account account =
        Account.builder(id, TimeUtil.now())
            .setMetaId("1234567812345678123456781234567812345678")
            .build();
    ExternalId extId1 =
        ExternalId.create(
            ExternalId.Key.create(ExternalId.SCHEME_MAILTO, "foo.bar@example.com", false),
            id,
            "foo.bar@example.com",
            null,
            ObjectId.fromString("1b9a0cf038ea38a0ab08617c39aa8e28413a27ca"));
    ExternalId extId2 =
        ExternalId.create(
            ExternalId.Key.create(ExternalId.SCHEME_USERNAME, "foo", false),
            id,
            null,
            "secret",
            ObjectId.fromString("5b3a73dc9a668a5b89b5f049225261e3e3291d1a"));
    ExternalId extId3 =
        ExternalId.create(
            ExternalId.Key.create(ExternalId.SCHEME_USERNAME, "Bar", true),
            id,
            null,
            "secret",
            ObjectId.fromString("483ea804e84282e15ddcdd1d15a797eb4796a760"));
    List<String> values =
        toStrings(
            AccountField.EXTERNAL_ID_STATE.get(
                AccountState.forAccount(account, ImmutableSet.of(extId1, extId2, extId3))));
    String expectedValue1 = extId1.key().sha1().name() + ":" + extId1.blobId().name();
    String expectedValue2 = extId2.key().sha1().name() + ":" + extId2.blobId().name();
    String expectedValue3 = extId3.key().sha1().name() + ":" + extId3.blobId().name();
    assertThat(values).containsExactly(expectedValue1, expectedValue2, expectedValue3);
  }

  private List<String> toStrings(Iterable<byte[]> values) {
    return Streams.stream(values).map(v -> new String(v, UTF_8)).collect(toList());
  }
}