@Test public void getAddress_testResponseFromStorageOrderNotCorrect_shouldReturnOrderedResponse() { List<Hash> addressesHashFromStorage1 = HashTestUtils.generateListOfRandomAddressHashes(15); List<Hash> addressesHashFromStorage2 = HashTestUtils.generateListOfRandomAddressHashes(15); List<Hash> addressesHashFromStorage = new ArrayList<>(); addressesHashFromStorage.addAll(addressesHashFromStorage1); addressesHashFromStorage.add(hashInRocks); addressesHashFromStorage.addAll(addressesHashFromStorage2); List<Hash> orderedList = new ArrayList<>(); orderedList.addAll(addressesHashFromStorage); GetHistoryAddressesRequest getHistoryAddressesRequestFromFullNode = new GetHistoryAddressesRequest(addressesHashFromStorage); setRequestDummySignerHashAndSignature(getHistoryAddressesRequestFromFullNode); List<Hash> addressesDataFromHistoryList = new ArrayList<>(); addressesDataFromHistoryList.addAll(addressesHashFromStorage1); addressesDataFromHistoryList.addAll(addressesHashFromStorage2); GetHistoryAddressesRequest getHistoryAddressesRequest = new GetHistoryAddressesRequest(addressesDataFromHistoryList); Map<Hash, AddressData> responseMap = new LinkedHashMap<>(); addressesHashFromStorage2.stream().forEach(hash -> responseMap.put(hash, new AddressData(hash))); addressesHashFromStorage1.stream().forEach(hash -> responseMap.put(hash, new AddressData(hash))); GetHistoryAddressesResponse unOrderedResponseFromStorage = new GetHistoryAddressesResponse(responseMap); setResponseDummySignerHashAndSignature(unOrderedResponseFromStorage); when(storageConnector.retrieveFromStorage(storageNodeUrl + "/addresses", getHistoryAddressesRequest, GetHistoryAddressesResponse.class)). thenReturn(ResponseEntity.status(HttpStatus.OK).body(unOrderedResponseFromStorage)); ResponseEntity<IResponse> responseFromGetAddresses = addressService.getAddresses(getHistoryAddressesRequestFromFullNode); Map<Hash, AddressData> orderedExpectedResponseMap = new LinkedHashMap<>(); addressesHashFromStorage1.stream().forEach(hash -> orderedExpectedResponseMap.put(hash, new AddressData(hash))); orderedExpectedResponseMap.put(hashInRocks, new AddressData(hashInRocks)); addressesHashFromStorage2.stream().forEach(hash -> orderedExpectedResponseMap.put(hash, new AddressData(hash))); GetHistoryAddressesResponse expectedResponse = new GetHistoryAddressesResponse(orderedExpectedResponseMap); ResponseEntity<IResponse> expectedOrderedResponseFromStorage = ResponseEntity.status(HttpStatus.OK).body(expectedResponse); Iterator<Map.Entry<Hash, AddressData>> responseIterator = ((GetHistoryAddressesResponse) expectedOrderedResponseFromStorage.getBody()).getAddressHashesToAddresses().entrySet().iterator(); int i = 0; while (responseIterator.hasNext()) { Map.Entry<Hash, AddressData> entry = responseIterator.next(); Assert.assertEquals(orderedList.get(i), entry.getKey()); i++; } Assert.assertEquals(HttpStatus.OK, responseFromGetAddresses.getStatusCode()); Assert.assertEquals(expectedResponse.getStatus(), ((GetHistoryAddressesResponse) responseFromGetAddresses.getBody()).getStatus()); }
public ResponseEntity<IResponse> getAddresses(GetHistoryAddressesRequest getHistoryAddressesRequest) { try { if (!getHistoryAddressesRequestCrypto.verifySignature(getHistoryAddressesRequest)) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new SerializableResponse(INVALID_SIGNATURE, STATUS_ERROR)); } List<Hash> addressesHashesToGetFromStorage = new ArrayList<>(getHistoryAddressesRequest.getAddressHashes()); HashMap<Hash, AddressData> addressToAddressDataFromDB = populateAndRemoveFoundAddresses(addressesHashesToGetFromStorage); Map<Hash, AddressData> getHistoryAddressesResponseMap; if (!addressesHashesToGetFromStorage.isEmpty()) { GetHistoryAddressesResponse getHistoryAddressesResponseFromStorageNode = getAddressesFromStorage(addressesHashesToGetFromStorage); Optional<ResponseEntity<IResponse>> responseValidationResult = validateStorageResponse(getHistoryAddressesResponseFromStorageNode); if (responseValidationResult.isPresent()) { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } else { Map<Hash, AddressData> addressHashesToAddressesFromStorage = getHistoryAddressesResponseFromStorageNode.getAddressHashesToAddresses(); getHistoryAddressesResponseMap = reorderHashResponses(getHistoryAddressesRequest.getAddressHashes(), addressToAddressDataFromDB, addressHashesToAddressesFromStorage); } } else { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } GetHistoryAddressesResponse getHistoryAddressesResponseToFullNode = new GetHistoryAddressesResponse(getHistoryAddressesResponseMap); getHistoryAddressesResponseCrypto.signMessage(getHistoryAddressesResponseToFullNode); return ResponseEntity. status(HttpStatus.OK). body(getHistoryAddressesResponseToFullNode); } catch (HttpClientErrorException | HttpServerErrorException e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(String.format(STORAGE_ADDRESS_ERROR, ((SerializableResponse) jacksonSerializer.deserialize(e.getResponseBodyAsByteArray())).getMessage()), STATUS_ERROR)); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(e.getMessage(), STATUS_ERROR)); } }
AddressService extends BaseNodeAddressService { public ResponseEntity<IResponse> getAddresses(GetHistoryAddressesRequest getHistoryAddressesRequest) { try { if (!getHistoryAddressesRequestCrypto.verifySignature(getHistoryAddressesRequest)) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new SerializableResponse(INVALID_SIGNATURE, STATUS_ERROR)); } List<Hash> addressesHashesToGetFromStorage = new ArrayList<>(getHistoryAddressesRequest.getAddressHashes()); HashMap<Hash, AddressData> addressToAddressDataFromDB = populateAndRemoveFoundAddresses(addressesHashesToGetFromStorage); Map<Hash, AddressData> getHistoryAddressesResponseMap; if (!addressesHashesToGetFromStorage.isEmpty()) { GetHistoryAddressesResponse getHistoryAddressesResponseFromStorageNode = getAddressesFromStorage(addressesHashesToGetFromStorage); Optional<ResponseEntity<IResponse>> responseValidationResult = validateStorageResponse(getHistoryAddressesResponseFromStorageNode); if (responseValidationResult.isPresent()) { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } else { Map<Hash, AddressData> addressHashesToAddressesFromStorage = getHistoryAddressesResponseFromStorageNode.getAddressHashesToAddresses(); getHistoryAddressesResponseMap = reorderHashResponses(getHistoryAddressesRequest.getAddressHashes(), addressToAddressDataFromDB, addressHashesToAddressesFromStorage); } } else { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } GetHistoryAddressesResponse getHistoryAddressesResponseToFullNode = new GetHistoryAddressesResponse(getHistoryAddressesResponseMap); getHistoryAddressesResponseCrypto.signMessage(getHistoryAddressesResponseToFullNode); return ResponseEntity. status(HttpStatus.OK). body(getHistoryAddressesResponseToFullNode); } catch (HttpClientErrorException | HttpServerErrorException e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(String.format(STORAGE_ADDRESS_ERROR, ((SerializableResponse) jacksonSerializer.deserialize(e.getResponseBodyAsByteArray())).getMessage()), STATUS_ERROR)); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(e.getMessage(), STATUS_ERROR)); } } }
AddressService extends BaseNodeAddressService { public ResponseEntity<IResponse> getAddresses(GetHistoryAddressesRequest getHistoryAddressesRequest) { try { if (!getHistoryAddressesRequestCrypto.verifySignature(getHistoryAddressesRequest)) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new SerializableResponse(INVALID_SIGNATURE, STATUS_ERROR)); } List<Hash> addressesHashesToGetFromStorage = new ArrayList<>(getHistoryAddressesRequest.getAddressHashes()); HashMap<Hash, AddressData> addressToAddressDataFromDB = populateAndRemoveFoundAddresses(addressesHashesToGetFromStorage); Map<Hash, AddressData> getHistoryAddressesResponseMap; if (!addressesHashesToGetFromStorage.isEmpty()) { GetHistoryAddressesResponse getHistoryAddressesResponseFromStorageNode = getAddressesFromStorage(addressesHashesToGetFromStorage); Optional<ResponseEntity<IResponse>> responseValidationResult = validateStorageResponse(getHistoryAddressesResponseFromStorageNode); if (responseValidationResult.isPresent()) { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } else { Map<Hash, AddressData> addressHashesToAddressesFromStorage = getHistoryAddressesResponseFromStorageNode.getAddressHashesToAddresses(); getHistoryAddressesResponseMap = reorderHashResponses(getHistoryAddressesRequest.getAddressHashes(), addressToAddressDataFromDB, addressHashesToAddressesFromStorage); } } else { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } GetHistoryAddressesResponse getHistoryAddressesResponseToFullNode = new GetHistoryAddressesResponse(getHistoryAddressesResponseMap); getHistoryAddressesResponseCrypto.signMessage(getHistoryAddressesResponseToFullNode); return ResponseEntity. status(HttpStatus.OK). body(getHistoryAddressesResponseToFullNode); } catch (HttpClientErrorException | HttpServerErrorException e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(String.format(STORAGE_ADDRESS_ERROR, ((SerializableResponse) jacksonSerializer.deserialize(e.getResponseBodyAsByteArray())).getMessage()), STATUS_ERROR)); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(e.getMessage(), STATUS_ERROR)); } }  }
AddressService extends BaseNodeAddressService { public ResponseEntity<IResponse> getAddresses(GetHistoryAddressesRequest getHistoryAddressesRequest) { try { if (!getHistoryAddressesRequestCrypto.verifySignature(getHistoryAddressesRequest)) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new SerializableResponse(INVALID_SIGNATURE, STATUS_ERROR)); } List<Hash> addressesHashesToGetFromStorage = new ArrayList<>(getHistoryAddressesRequest.getAddressHashes()); HashMap<Hash, AddressData> addressToAddressDataFromDB = populateAndRemoveFoundAddresses(addressesHashesToGetFromStorage); Map<Hash, AddressData> getHistoryAddressesResponseMap; if (!addressesHashesToGetFromStorage.isEmpty()) { GetHistoryAddressesResponse getHistoryAddressesResponseFromStorageNode = getAddressesFromStorage(addressesHashesToGetFromStorage); Optional<ResponseEntity<IResponse>> responseValidationResult = validateStorageResponse(getHistoryAddressesResponseFromStorageNode); if (responseValidationResult.isPresent()) { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } else { Map<Hash, AddressData> addressHashesToAddressesFromStorage = getHistoryAddressesResponseFromStorageNode.getAddressHashesToAddresses(); getHistoryAddressesResponseMap = reorderHashResponses(getHistoryAddressesRequest.getAddressHashes(), addressToAddressDataFromDB, addressHashesToAddressesFromStorage); } } else { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } GetHistoryAddressesResponse getHistoryAddressesResponseToFullNode = new GetHistoryAddressesResponse(getHistoryAddressesResponseMap); getHistoryAddressesResponseCrypto.signMessage(getHistoryAddressesResponseToFullNode); return ResponseEntity. status(HttpStatus.OK). body(getHistoryAddressesResponseToFullNode); } catch (HttpClientErrorException | HttpServerErrorException e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(String.format(STORAGE_ADDRESS_ERROR, ((SerializableResponse) jacksonSerializer.deserialize(e.getResponseBodyAsByteArray())).getMessage()), STATUS_ERROR)); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(e.getMessage(), STATUS_ERROR)); } }  ResponseEntity<IResponse> getAddresses(GetHistoryAddressesRequest getHistoryAddressesRequest); @Override boolean validateRequestedAddressHashExistsAndRelevant(RequestedAddressHashData requestedAddressHashData); }
AddressService extends BaseNodeAddressService { public ResponseEntity<IResponse> getAddresses(GetHistoryAddressesRequest getHistoryAddressesRequest) { try { if (!getHistoryAddressesRequestCrypto.verifySignature(getHistoryAddressesRequest)) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new SerializableResponse(INVALID_SIGNATURE, STATUS_ERROR)); } List<Hash> addressesHashesToGetFromStorage = new ArrayList<>(getHistoryAddressesRequest.getAddressHashes()); HashMap<Hash, AddressData> addressToAddressDataFromDB = populateAndRemoveFoundAddresses(addressesHashesToGetFromStorage); Map<Hash, AddressData> getHistoryAddressesResponseMap; if (!addressesHashesToGetFromStorage.isEmpty()) { GetHistoryAddressesResponse getHistoryAddressesResponseFromStorageNode = getAddressesFromStorage(addressesHashesToGetFromStorage); Optional<ResponseEntity<IResponse>> responseValidationResult = validateStorageResponse(getHistoryAddressesResponseFromStorageNode); if (responseValidationResult.isPresent()) { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } else { Map<Hash, AddressData> addressHashesToAddressesFromStorage = getHistoryAddressesResponseFromStorageNode.getAddressHashesToAddresses(); getHistoryAddressesResponseMap = reorderHashResponses(getHistoryAddressesRequest.getAddressHashes(), addressToAddressDataFromDB, addressHashesToAddressesFromStorage); } } else { getHistoryAddressesResponseMap = addressToAddressDataFromDB; } GetHistoryAddressesResponse getHistoryAddressesResponseToFullNode = new GetHistoryAddressesResponse(getHistoryAddressesResponseMap); getHistoryAddressesResponseCrypto.signMessage(getHistoryAddressesResponseToFullNode); return ResponseEntity. status(HttpStatus.OK). body(getHistoryAddressesResponseToFullNode); } catch (HttpClientErrorException | HttpServerErrorException e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(String.format(STORAGE_ADDRESS_ERROR, ((SerializableResponse) jacksonSerializer.deserialize(e.getResponseBodyAsByteArray())).getMessage()), STATUS_ERROR)); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new SerializableResponse(e.getMessage(), STATUS_ERROR)); } }  ResponseEntity<IResponse> getAddresses(GetHistoryAddressesRequest getHistoryAddressesRequest); @Override boolean validateRequestedAddressHashExistsAndRelevant(RequestedAddressHashData requestedAddressHashData);  }