use of org.web3j.protocol.core.Response in project quorum-acceptance-tests by ConsenSys.
the class PluginSecurity method invokeMultipleWithExpectation.
@Step("`<clientId>` sends a batch `<apis>` to `<node>` and expect: <table>")
public void invokeMultipleWithExpectation(String clientId, String apis, QuorumNetworkProperty.Node node, Table table) {
String token = mustHaveValue(DataStoreFactory.getScenarioDataStore(), clientId, String.class);
Context.storeAccessToken(token);
BatchRequest.Collector collector = BatchRequest.Collector.create();
Arrays.stream(apis.split(",")).map(String::trim).forEach(n -> collector.add(n, Collections.emptyList()));
Map<String, String> expect = table.getTableRows().stream().collect(Collectors.toMap(r -> StringUtils.removeEnd(StringUtils.removeStart(r.getCell("callApi"), "`"), "`"), r -> r.getCell("expectation")));
rpcService.call(node, collector).blockingForEach(batchResponse -> {
assertThat(batchResponse.getResponses()).as("Number of returned responses").hasSize(collector.size());
for (ObjectResponse res : batchResponse.getResponses()) {
assertThat(res.getId()).as("Response must have id").isNotZero();
Request r = collector.getByID(res.getId());
String policy = Optional.ofNullable(expect.get(r.getMethod())).orElseThrow(() -> new IllegalStateException("no such method in expectation table: " + r.getMethod()));
boolean expectAuthorized = "success".equalsIgnoreCase(policy);
String description = policy + ": " + r.getMethod() + "@" + node.getName();
if (expectAuthorized) {
assertThat(Optional.ofNullable(res.getError()).orElse(new Response.Error()).getMessage()).as(description).isNullOrEmpty();
}
assertThat(res.hasError()).as(description).isNotEqualTo(expectAuthorized);
if (res.hasError()) {
assertThat(res.getError().getMessage()).as(description).endsWith(policy);
}
}
});
}
use of org.web3j.protocol.core.Response in project quorum-acceptance-tests by ConsenSys.
the class ContractService method readSimpleContractValue.
/**
* Need to use EthCall to manipulate the error as webj3 doesn't
*
* @param node
* @param contractAddress
* @return
*/
public Observable<BigInteger> readSimpleContractValue(Node node, String contractAddress) {
Quorum client = connectionFactory().getConnection(node);
Function function = new Function(FUNC_GET, Arrays.<Type>asList(), Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {
}));
return client.ethCoinbase().flowable().toObservable().map(Response::getResult).flatMap(address -> {
Request<?, EthCall> req = client.ethCall(Transaction.createEthCallTransaction(address, contractAddress, FunctionEncoder.encode(function)), DefaultBlockParameterName.LATEST);
return req.flowable().toObservable();
}).map(ec -> {
if (ec.hasError()) {
throw new ContractCallException(ec.getError().getMessage());
}
List<Type> values = FunctionReturnDecoder.decode(ec.getValue(), function.getOutputParameters());
Type result;
if (!values.isEmpty()) {
result = values.get(0);
} else {
throw new ContractCallException("Empty value (0x) returned from contract");
}
Object value = result.getValue();
if (BigInteger.class.isAssignableFrom(value.getClass())) {
return (BigInteger) value;
} else {
throw new ContractCallException("Unable to convert response: " + value + " to expected type: " + BigInteger.class.getSimpleName());
}
});
}
use of org.web3j.protocol.core.Response in project teku by ConsenSys.
the class Web3jEth1ProviderTest method shouldThrowRejectedRequestExceptionWhenInfuraRejectsRequestWithTooManyLogs.
@Test
void shouldThrowRejectedRequestExceptionWhenInfuraRejectsRequestWithTooManyLogs() {
final EthFilter filter = new EthFilter();
final EthLog response = new EthLog();
response.setError(new Response.Error(-32005, "query returned more than 10000 results"));
when(web3.ethGetLogs(filter)).thenReturn(request1);
when(request1.sendAsync()).thenReturn(SafeFuture.completedFuture(response));
final SafeFuture<List<LogResult<?>>> result = provider.ethGetLogs(filter);
SafeFutureAssert.assertThatSafeFuture(result).isCompletedExceptionallyWith(RejectedRequestException.class);
}
use of org.web3j.protocol.core.Response in project web3j by web3j.
the class Service method sendBatch.
@Override
public BatchResponse sendBatch(BatchRequest batchRequest) throws IOException {
if (batchRequest.getRequests().isEmpty()) {
return new BatchResponse(Collections.emptyList(), Collections.emptyList());
}
String payload = objectMapper.writeValueAsString(batchRequest.getRequests());
try (InputStream result = performIO(payload)) {
if (result != null) {
ArrayNode nodes = (ArrayNode) objectMapper.readTree(result);
List<Response<?>> responses = new ArrayList<>(nodes.size());
for (int i = 0; i < nodes.size(); i++) {
Request<?, ? extends Response<?>> request = batchRequest.getRequests().get(i);
Response<?> response = objectMapper.treeToValue(nodes.get(i), request.getResponseType());
responses.add(response);
}
return new BatchResponse(batchRequest.getRequests(), responses);
} else {
return null;
}
}
}
use of org.web3j.protocol.core.Response in project web3j by web3j.
the class RawResponseDeserializer method deserialize.
@Override
public Response deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
Response deserializedResponse = (Response) defaultDeserializer.deserialize(jp, ctxt);
deserializedResponse.setRawResponse(getRawResponse(jp));
return deserializedResponse;
}
Aggregations