### Question:
ValidatorsUtil { @SuppressWarnings("DoNotReturnNullOptionals") public static Optional<Integer> getValidatorIndex(BeaconState state, BLSPublicKey publicKey) { final Integer validatorIndex = BeaconStateCache.getTransitionCaches(state) .getValidatorIndex() .get( publicKey, key -> { SSZList<Validator> validators = state.getValidators(); for (int i = 0; i < validators.size(); i++) { final Validator validator = validators.get(i); if (validator.getPubkey().equals(publicKey)) { return i; } } return null; }); return Optional.ofNullable(validatorIndex) .filter(index -> index < state.getValidators().size()); }  static boolean is_active_validator(Validator validator, UInt64 epoch); static boolean is_eligible_for_activation_queue(Validator validator); static boolean is_eligible_for_activation(BeaconState state, Validator validator); static Optional<BLSPublicKey> getValidatorPubKey(
      BeaconState state, UInt64 validatorIndex); static List<Integer> get_active_validator_indices(BeaconState state, UInt64 epoch); @SuppressWarnings("DoNotReturnNullOptionals") static Optional<Integer> getValidatorIndex(BeaconState state, BLSPublicKey publicKey); static void decrease_balance(MutableBeaconState state, int index, UInt64 delta); static void increase_balance(MutableBeaconState state, int index, UInt64 delta); static boolean is_slashable_validator(Validator validator, UInt64 epoch);  }### Answer:
@Test void getValidatorIndex_shouldReturnValidatorIndex() { final BeaconState state = dataStructureUtil.randomBeaconState(); assertThat(state.getValidators()).hasSizeGreaterThan(5); for (int i = 0; i < 5; i++) { final Validator validator = state.getValidators().get(i); assertThat(ValidatorsUtil.getValidatorIndex(state, validator.getPubkey())).contains(i); } }
@Test public void getValidatorIndex_shouldReturnEmptyWhenValidatorNotFound() { final BeaconState state = dataStructureUtil.randomBeaconState(); final Optional<Integer> index = ValidatorsUtil.getValidatorIndex(state, dataStructureUtil.randomPublicKey()); assertThat(index).isEmpty(); }
@Test public void getValidatorIndex_shouldReturnEmptyWhenValidatorInCacheButNotState() { final BeaconState state = dataStructureUtil.randomBeaconState(); final Validator validator = dataStructureUtil.randomValidator(); final BeaconState nextState = state.updated(s -> s.getValidators().add(validator)); assertThat(ValidatorsUtil.getValidatorIndex(nextState, validator.getPubkey())) .contains(nextState.getValidators().size() - 1); assertThat(ValidatorsUtil.getValidatorIndex(state, validator.getPubkey())).isEmpty(); }
@Test public void getValidatorIndex_shouldNotCacheValidatorMissing() { final BeaconState state = dataStructureUtil.randomBeaconState(); final Validator validator = dataStructureUtil.randomValidator(); assertThat(ValidatorsUtil.getValidatorIndex(state, validator.getPubkey())).isEmpty(); final BeaconState nextState = state.updated(s -> s.getValidators().add(validator)); assertThat(ValidatorsUtil.getValidatorIndex(nextState, validator.getPubkey())) .contains(nextState.getValidators().size() - 1); }