Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test with multiple aca-py agents #13

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/test/java/org/hyperledger/aries/LedgerClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright (c) 2020-2021 - for information on the respective copyright owner
* see the NOTICE file and/or the repository at
* https://github.com/hyperledger-labs/acapy-java-client
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.aries;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.Builder;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.apache.commons.lang3.StringUtils;

import org.hyperledger.aries.util.ledger.LedgerDIDCreate;
import org.hyperledger.aries.util.ledger.LedgerDIDResponse;

import javax.annotation.Nullable;
import java.io.IOException;
import java.lang.reflect.Type;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* ACA-PY client
*/
@Slf4j
@SuppressWarnings("unused")
public class LedgerClient extends BaseClient {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be useful to have this in the main package


public static final String LEDGER_GENESIS_TXN_URL = "/genesis";
public static final String LEDGER_DID_REGISTRATION_URL = "/register";
public static final String LEDGER_DID_ENDORSER_ROLE = "ENDORSER";

private final String url;

/**
* @param url The ledger URL without a path e.g. protocol://host:[port]
* @param client {@link OkHttpClient} if null a default client is created
*/
@Builder
public LedgerClient(@NonNull String url,
@Nullable OkHttpClient client) {
super(client);
this.url = StringUtils.trim(url);
}

// ----------------------------------------------------
// Register a new public DID on the ledger
// ----------------------------------------------------

/**
* Create a public DID
* @param didCreate {@link DIDCreate}
* @return {@link DID}
* @throws IOException if the request could not be executed due to cancellation, a connectivity problem or timeout.
*/
public Optional<LedgerDIDResponse> ledgerDidCreate(@NonNull LedgerDIDCreate didCreate) throws IOException {
Request req = buildPost(url + LEDGER_DID_REGISTRATION_URL, didCreate);
return call(req, LedgerDIDResponse.class);
}


// ----------------------------------------------------
// Internal
// ----------------------------------------------------

private Request buildPost(String u, Object body) {
return request(u)
.post(jsonBody(gson.toJson(body)))
.build();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code below

private Request buildPut(String u, Object body) {
return request(u)
.put(jsonBody(gson.toJson(body)))
.build();
}

private Request buildPatch(String u, Object body) {
return request(u)
.patch(jsonBody(gson.toJson(body)))
.build();
}

private Request buildGet(String u) {
return request(u)
.get()
.build();
}

private Request buildDelete(String u) {
return request(u)
.delete()
.build();
}

private Request.Builder request(String u) {
Request.Builder b = new Request.Builder()
.url(u);
return b;
}
}
86 changes: 86 additions & 0 deletions src/test/java/org/hyperledger/aries/MultiIntegrationTestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2020-2021 - for information on the respective copyright owner
* see the NOTICE file and/or the repository at
* https://github.com/hyperledger-labs/acapy-java-client
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.aries;

import org.junit.jupiter.api.BeforeEach;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
public abstract class MultiIntegrationTestBase {

private final Logger log = LoggerFactory.getLogger(MultiIntegrationTestBase.class);

public static final String ARIES_VERSION = "bcgovimages/aries-cloudagent:py36-1.16-1_0.7.2";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a single version for all tests so I would use the one from IntegrationTestBase

public static final Integer ARIES_ENDPOINT_PORT = 8030;
public static final Integer ARIES_ADMIN_PORT = 8031;
public static final Integer ARIES_ENDPOINT_PORT_2 = 8040;
public static final Integer ARIES_ADMIN_PORT_2 = 8041;

protected LedgerClient lc;
protected AriesClient ac;
protected AriesClient ac2;

Network network = Network.newNetwork();

@Container
protected GenericContainer<?> ariesContainer = new GenericContainer<>(ARIES_VERSION)
.withNetwork(network)
.withNetworkAliases("agent_1")
.withExposedPorts(ARIES_ADMIN_PORT)
.withCommand("start"
+ " -it http 0.0.0.0 " + ARIES_ENDPOINT_PORT
+ " -ot http"
+ " --admin 0.0.0.0 " + ARIES_ADMIN_PORT
+ " --admin-insecure-mode"
+ " --log-level debug"
+ " -e http://agent_1:" + ARIES_ENDPOINT_PORT
+ " --plugin aries_cloudagent.messaging.jsonld"
+ this.extraAgentArgs(1))
.waitingFor(Wait.defaultWaitStrategy())
.withLogConsumer(new Slf4jLogConsumer(log))
;

@Container
protected GenericContainer<?> ariesContainer2 = new GenericContainer<>(ARIES_VERSION)
.withExposedPorts(ARIES_ADMIN_PORT_2)
.withNetwork(network)
.withNetworkAliases("agent_2")
.withCommand("start"
+ " -it http 0.0.0.0 " + ARIES_ENDPOINT_PORT_2
+ " -ot http"
+ " --admin 0.0.0.0 " + ARIES_ADMIN_PORT_2
+ " --admin-insecure-mode"
+ " --log-level debug"
+ " -e http://agent_2:" + ARIES_ENDPOINT_PORT_2
+ " --plugin aries_cloudagent.messaging.jsonld"
+ this.extraAgentArgs(2))
.waitingFor(Wait.defaultWaitStrategy())
.withLogConsumer(new Slf4jLogConsumer(log))
;

protected String extraAgentArgs(int agentNum) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The api is a bit confusing, I would also make that method abstract.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I would split this up into two abstract methods like agentOneArgs() and agentTwoArgs, then you do not have to use switches all the time

return " --no-ledger";
}

@BeforeEach
void setup() {
ac = AriesClient.builder()
.url("http://" + ariesContainer.getHost() + ":" + ariesContainer.getMappedPort(ARIES_ADMIN_PORT))
.build();
ac2 = AriesClient.builder()
.url("http://" + ariesContainer2.getHost() + ":" + ariesContainer2.getMappedPort(ARIES_ADMIN_PORT_2))
.build();
}
}
Loading