### Question:
ChainHeadPrivateNonceProvider implements PrivateNonceProvider { @Override public long getNonce(final Address sender, final Bytes32 privacyGroupId) { final BlockHeader chainHeadHeader = blockchain.getChainHeadHeader(); final Hash stateRoot = privateStateRootResolver.resolveLastStateRoot(privacyGroupId, chainHeadHeader.getHash()); return privateWorldStateArchive .get(stateRoot) .map( privateWorldState -> { final Account account = privateWorldState.get(sender); return account == null ? 0L : account.getNonce(); }) .orElse(Account.DEFAULT_NONCE); } ChainHeadPrivateNonceProvider(
      final Blockchain blockchain,
      final PrivateStateRootResolver privateStateRootResolver,
      final WorldStateArchive privateWorldStateArchive); @Override long getNonce(final Address sender, final Bytes32 privacyGroupId);  }### Answer:
@Test public void determineNonceForPrivacyGroupRequestWhenPrivateStateDoesNotExist() { when(privateStateRootResolver.resolveLastStateRoot(any(Bytes32.class), any(Hash.class))) .thenReturn(Hash.ZERO); when(privateWorldStateArchive.get(any(Hash.class))).thenReturn(Optional.empty()); final long nonce = privateNonceProvider.getNonce(ADDRESS, PRIVACY_GROUP_ID); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); }
@Test public void determineNonceForPrivacyGroupRequestWhenAccountExists() { when(account.getNonce()).thenReturn(4L); when(worldState.get(any(Address.class))).thenReturn(account); when(privateStateRootResolver.resolveLastStateRoot(any(Bytes32.class), any(Hash.class))) .thenReturn(Hash.ZERO); when(privateWorldStateArchive.get(any(Hash.class))).thenReturn(Optional.of(worldState)); final long nonce = privateNonceProvider.getNonce(ADDRESS, PRIVACY_GROUP_ID); assertThat(nonce).isEqualTo(4L); }
@Test public void determineNonceForPrivacyGroupRequestWhenAccountDoesNotExist() { when(privateStateRootResolver.resolveLastStateRoot(any(Bytes32.class), any(Hash.class))) .thenReturn(Hash.ZERO); when(privateWorldStateArchive.get(any(Hash.class))).thenReturn(Optional.of(worldState)); when(account.getNonce()).thenReturn(4L); final long nonce = privateNonceProvider.getNonce(ADDRESS, PRIVACY_GROUP_ID); assertThat(nonce).isEqualTo(Account.DEFAULT_NONCE); verifyNoInteractions(account); }