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.testing; 16 : 17 : import com.google.common.collect.ImmutableMap; 18 : import com.google.common.collect.Maps; 19 : import com.google.gerrit.entities.Account; 20 : import com.google.gerrit.server.account.AccountCache; 21 : import com.google.gerrit.server.account.AccountState; 22 : import com.google.gerrit.server.util.time.TimeUtil; 23 : import java.util.HashMap; 24 : import java.util.Map; 25 : import java.util.Optional; 26 : import java.util.Set; 27 : import org.eclipse.jgit.lib.ObjectId; 28 : 29 : /** Fake implementation of {@link AccountCache} for testing. */ 30 : public class FakeAccountCache implements AccountCache { 31 : private final Map<Account.Id, AccountState> byId; 32 : 33 1 : public FakeAccountCache() { 34 1 : byId = new HashMap<>(); 35 1 : } 36 : 37 : @Override 38 : public synchronized AccountState getEvenIfMissing(Account.Id accountId) { 39 1 : AccountState state = byId.get(accountId); 40 1 : if (state != null) { 41 1 : return state; 42 : } 43 0 : return newState( 44 0 : Account.builder(accountId, TimeUtil.now()) 45 0 : .setMetaId("1234567812345678123456781234567812345678") 46 0 : .build()); 47 : } 48 : 49 : @Override 50 : public synchronized Optional<AccountState> get(Account.Id accountId) { 51 0 : return Optional.ofNullable(byId.get(accountId)); 52 : } 53 : 54 : @Override 55 : public synchronized Map<Account.Id, AccountState> get(Set<Account.Id> accountIds) { 56 1 : return ImmutableMap.copyOf(Maps.filterKeys(byId, accountIds::contains)); 57 : } 58 : 59 : @Override 60 : public synchronized Optional<AccountState> getByUsername(String username) { 61 0 : throw new UnsupportedOperationException(); 62 : } 63 : 64 : @Override 65 : public AccountState getFromMetaId(Account.Id accountId, ObjectId metaId) { 66 0 : return get(accountId).get(); 67 : } 68 : 69 : public synchronized void put(Account account) { 70 1 : AccountState state = newState(account); 71 1 : byId.put(account.id(), state); 72 1 : } 73 : 74 : private static AccountState newState(Account account) { 75 1 : return AccountState.forAccount(account); 76 : } 77 : }