Search in sources :

Example 1 with ObjectResponse

use of com.quorum.gauge.ext.ObjectResponse 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);
            }
        }
    });
}
Also used : AbstractSpecImplementation(com.quorum.gauge.core.AbstractSpecImplementation) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Step(com.thoughtworks.gauge.Step) Logger(org.slf4j.Logger) ObjectResponse(com.quorum.gauge.ext.ObjectResponse) QuorumNode(com.quorum.gauge.common.QuorumNode) BatchRequest(com.quorum.gauge.ext.BatchRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) QuorumNetworkProperty(com.quorum.gauge.common.QuorumNetworkProperty) LoggerFactory(org.slf4j.LoggerFactory) Response(org.web3j.protocol.core.Response) Collectors(java.util.stream.Collectors) Request(org.web3j.protocol.core.Request) Stream(java.util.stream.Stream) Context(com.quorum.gauge.common.Context) Service(org.springframework.stereotype.Service) Table(com.thoughtworks.gauge.Table) DataStoreFactory(com.thoughtworks.gauge.datastore.DataStoreFactory) ObjectResponse(com.quorum.gauge.ext.ObjectResponse) Response(org.web3j.protocol.core.Response) BatchRequest(com.quorum.gauge.ext.BatchRequest) BatchRequest(com.quorum.gauge.ext.BatchRequest) Request(org.web3j.protocol.core.Request) ObjectResponse(com.quorum.gauge.ext.ObjectResponse) Step(com.thoughtworks.gauge.Step)

Example 2 with ObjectResponse

use of com.quorum.gauge.ext.ObjectResponse in project quorum-acceptance-tests by ConsenSys.

the class PluginSecurity method invokeMultiple.

@Step("`<clientId>` is responded with <policy> when trying to: <table>")
public void invokeMultiple(String clientId, String policy, Table table) {
    boolean expectAuthorized = "success".equalsIgnoreCase(policy);
    String token = mustHaveValue(DataStoreFactory.getScenarioDataStore(), clientId, String.class);
    Context.storeAccessToken(token);
    Map<String, QuorumNetworkProperty.Node> nodeMap = networkProperty.getNodes();
    table.getTableRows().stream().map(r -> new ApiCall(r.getCell("callApi"), r.getCell("targetNode"))).onClose(Context::removeAccessToken).forEach(a -> {
        if (a.isBatch) {
            rpcService.call(nodeMap.get(a.node), a.collector).blockingForEach(batchResponse -> {
                assertThat(batchResponse.getResponses()).as("Number of returned responses").hasSize(a.collector.size());
                for (ObjectResponse res : batchResponse.getResponses()) {
                    assertThat(res.getId()).as("Response must have id").isNotZero();
                    Request r = a.collector.getByID(res.getId());
                    String description = policy + ": " + r.getMethod() + "@" + a.node;
                    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);
                    }
                }
            });
        } else {
            rpcService.call(nodeMap.get(a.node), a.name, Collections.emptyList(), ObjectResponse.class).blockingForEach(res -> {
                assertThat(res.getId()).as("Response must have ID").isNotZero();
                String description = policy + ": " + a.name + "@" + a.node;
                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);
                }
            });
        }
    });
}
Also used : ObjectResponse(com.quorum.gauge.ext.ObjectResponse) Response(org.web3j.protocol.core.Response) QuorumNode(com.quorum.gauge.common.QuorumNode) BatchRequest(com.quorum.gauge.ext.BatchRequest) Request(org.web3j.protocol.core.Request) ObjectResponse(com.quorum.gauge.ext.ObjectResponse) Step(com.thoughtworks.gauge.Step)

Aggregations

QuorumNode (com.quorum.gauge.common.QuorumNode)2 BatchRequest (com.quorum.gauge.ext.BatchRequest)2 ObjectResponse (com.quorum.gauge.ext.ObjectResponse)2 Step (com.thoughtworks.gauge.Step)2 Request (org.web3j.protocol.core.Request)2 Response (org.web3j.protocol.core.Response)2 Context (com.quorum.gauge.common.Context)1 QuorumNetworkProperty (com.quorum.gauge.common.QuorumNetworkProperty)1 AbstractSpecImplementation (com.quorum.gauge.core.AbstractSpecImplementation)1 Table (com.thoughtworks.gauge.Table)1 DataStoreFactory (com.thoughtworks.gauge.datastore.DataStoreFactory)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StringUtils (org.apache.commons.lang.StringUtils)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Service (org.springframework.stereotype.Service)1