### Question:
PrivateWorldStateReader { public Optional<Bytes> getContractCode( final String privacyGroupId, final Hash blockHash, final Address contractAddress) { final Hash latestStateRoot = privateStateRootResolver.resolveLastStateRoot( Bytes32.wrap(Bytes.fromBase64String(privacyGroupId)), blockHash); return privateWorldStateArchive .get(latestStateRoot) .flatMap(worldState -> Optional.ofNullable(worldState.get(contractAddress))) .flatMap(account -> Optional.ofNullable(account.getCode())); } PrivateWorldStateReader(
      final PrivateStateRootResolver privateStateRootResolver,
      final WorldStateArchive privateWorldStateArchive,
      final PrivateStateStorage privateStateStorage); Optional<Bytes> getContractCode(
      final String privacyGroupId, final Hash blockHash, final Address contractAddress); List<PrivateTransactionMetadata> getPrivateTransactionMetadataList(
      final String privacyGroupId, final Hash blockHash); Optional<PrivateTransactionReceipt> getPrivateTransactionReceipt(
      final Hash blockHash, final Hash transactionHash);  }### Answer:
@Test public void absentPrivateWorldStateReturnsEmpty() { final Bytes32 privacyGroupBytes = Bytes32.wrap(Bytes.fromBase64String(PRIVACY_GROUP_ID)); final Address contractAddress = Address.ZERO; when(privateStateRootResolver.resolveLastStateRoot(eq(privacyGroupBytes), eq(blockHash))) .thenReturn(stateRootHash); when(privateWorldStateArchive.get(eq(stateRootHash))).thenReturn(Optional.empty()); final Optional<Bytes> maybecontractCode = privateWorldStateReader.getContractCode(PRIVACY_GROUP_ID, blockHash, contractAddress); assertThat(maybecontractCode).isNotPresent(); }
@Test public void absentAccountReturnsEmpty() { final Bytes32 privacyGroupBytes = Bytes32.wrap(Bytes.fromBase64String(PRIVACY_GROUP_ID)); final Address contractAddress = Address.ZERO; when(privateStateRootResolver.resolveLastStateRoot(eq(privacyGroupBytes), eq(blockHash))) .thenReturn(stateRootHash); when(privateWorldStateArchive.get(eq(stateRootHash))) .thenReturn(Optional.of(privateWorldState)); when(privateWorldState.get(eq(contractAddress))).thenReturn(null); final Optional<Bytes> maybeContractCode = privateWorldStateReader.getContractCode(PRIVACY_GROUP_ID, blockHash, contractAddress); assertThat(maybeContractCode).isNotPresent(); }
@Test public void existingAccountWithEmptyCodeReturnsEmpty() { final Bytes32 privacyGroupBytes = Bytes32.wrap(Bytes.fromBase64String(PRIVACY_GROUP_ID)); final Address contractAddress = Address.ZERO; when(privateStateRootResolver.resolveLastStateRoot(eq(privacyGroupBytes), eq(blockHash))) .thenReturn(stateRootHash); when(privateWorldStateArchive.get(eq(stateRootHash))) .thenReturn(Optional.of(privateWorldState)); when(privateWorldState.get(eq(contractAddress))).thenReturn(contractAccount); when(contractAccount.getCode()).thenReturn(null); final Optional<Bytes> maybeContractCode = privateWorldStateReader.getContractCode(PRIVACY_GROUP_ID, blockHash, contractAddress); assertThat(maybeContractCode).isNotPresent(); }
@Test public void existingAccountWithCodeReturnsExpectedBytes() { when(privateStateRootResolver.resolveLastStateRoot(eq(PRIVACY_GROUP_ID_BYTES), eq(blockHash))) .thenReturn(stateRootHash); when(privateWorldStateArchive.get(eq(stateRootHash))) .thenReturn(Optional.of(privateWorldState)); when(privateWorldState.get(eq(contractAddress))).thenReturn(contractAccount); when(contractAccount.getCode()).thenReturn(contractCode); final Optional<Bytes> maybeContractCode = privateWorldStateReader.getContractCode(PRIVACY_GROUP_ID, blockHash, contractAddress); assertThat(maybeContractCode).isPresent().hasValue(contractCode); }