package org.hyperledger.indy.sdk.did;

import org.hyperledger.indy.sdk.IndyIntegrationTestWithPoolAndSingleWallet;
import org.hyperledger.indy.sdk.InvalidStateException;
import org.hyperledger.indy.sdk.ledger.Ledger;
import org.hyperledger.indy.sdk.utils.PoolUtils;
import org.junit.Test;

import java.util.concurrent.ExecutionException;

import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


public class GetEndpointForDidTest extends IndyIntegrationTestWithPoolAndSingleWallet {

	@Test
	public void testGetEndpointForDidWorks() throws Exception {
		Did.setEndpointForDid(wallet, DID, ENDPOINT, VERKEY).get();
		DidResults.EndpointForDidResult receivedEndpoint = Did.getEndpointForDid(wallet, pool, DID).get();
		assertEquals(ENDPOINT, receivedEndpoint.getAddress());
		assertEquals(VERKEY, receivedEndpoint.getTransportKey());
	}

	@Test(timeout = PoolUtils.TEST_TIMEOUT_FOR_REQUEST_ENSURE)
	public void testGetEndpointForDidWorksFromLedger() throws Exception {
		DidResults.CreateAndStoreMyDidResult trusteeDidResult = Did.createAndStoreMyDid(wallet, TRUSTEE_IDENTITY_JSON).get();
		String trusteeDid = trusteeDidResult.getDid();
		String trusteeVerkey = trusteeDidResult.getVerkey();

		String endpoint = String.format("{\"endpoint\":{\"ha\":\"%s\",\"verkey\":\"%s\"}}", ENDPOINT, trusteeVerkey);

		String attribRequest = Ledger.buildAttribRequest(trusteeDid, trusteeDid, null, endpoint, null).get();
		Ledger.signAndSubmitRequest(pool, wallet, trusteeDid, attribRequest).get();

		assertTrue(PoolUtils.retryCheck(
						() -> Did.getEndpointForDid(wallet, pool, trusteeDid).get().toString(),
						didEndpoint -> didEndpoint.equals(new DidResults.EndpointForDidResult(ENDPOINT, trusteeVerkey).toString())));
	}

	@Test
	public void testGetEndpointForDidWorksForUnknownDid() throws Exception {
		thrown.expect(ExecutionException.class);
		thrown.expectCause(isA(InvalidStateException.class));

		Did.getEndpointForDid(wallet, pool, DID).get();
	}
}
@@ -1603,15 +1603,80 @@
 %09%09%09%09
-%09%09
 () -%3E
+ %7B%0A%09%09%09%09%09String respEndpoint = null;%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09respEndpoint =
  Did
@@ -1740,31 +1740,97 @@
 ng()
-,
+;
 %0A%09%09%09%09%09
-%09didEndpoint -%3E 
+%7D catch (ExecutionException ignore) %7B%0A%09%09%09%09%09%7D%0A%09%09%09%09%09return respEndpoint;%0A%09%09%09%09%7D,%0A%09%09%09%09
 didE
@@ -1840,16 +1840,12 @@
 oint
-.equals(
+ -%3E 
 new 
@@ -1911,16 +1911,35 @@
 String()
+.equals(didEndpoint
 )));%0A%09%7D%0A

Fix retry logic for Java wrapper test testGetEndpointForDidWorksFromLedger.