Search in sources :

Example 1 with NetworkStore

use of com.quorum.tessera.discovery.NetworkStore in project tessera by ConsenSys.

the class DiscoveryHelperProviderTest method provider.

@Test
public void provider() {
    try (var mockedEnclave = mockStatic(Enclave.class);
        var mockedNetworkStore = mockStatic(NetworkStore.class)) {
        NetworkStore networkStore = mock(NetworkStore.class);
        mockedNetworkStore.when(NetworkStore::getInstance).thenReturn(networkStore);
        Enclave enclave = mock(Enclave.class);
        mockedEnclave.when(Enclave::create).thenReturn(enclave);
        DiscoveryHelper helper = DiscoveryHelperProvider.provider();
        assertThat(helper).isNotNull().isExactlyInstanceOf(DiscoveryHelperImpl.class);
        mockedEnclave.verify(Enclave::create);
        mockedEnclave.verifyNoMoreInteractions();
        mockedNetworkStore.verify(NetworkStore::getInstance);
        mockedNetworkStore.verifyNoMoreInteractions();
        verifyNoInteractions(networkStore);
        verifyNoInteractions(enclave);
    }
}
Also used : Enclave(com.quorum.tessera.enclave.Enclave) DiscoveryHelper(com.quorum.tessera.discovery.DiscoveryHelper) NetworkStore(com.quorum.tessera.discovery.NetworkStore) Test(org.junit.Test)

Example 2 with NetworkStore

use of com.quorum.tessera.discovery.NetworkStore in project tessera by ConsenSys.

the class DiscoveryHelperTest method buildCurrent.

@Test
public void buildCurrent() {
    final URI uri = URI.create("http://somedomain.com");
    when(runtimeContext.getP2pServerUri()).thenReturn(uri);
    final List<PublicKey> keys = IntStream.range(0, 5).mapToObj(i -> mock(PublicKey.class)).collect(Collectors.toList());
    final ActiveNode activeNode = ActiveNode.Builder.create().withUri(NodeUri.create(uri)).withKeys(keys).build();
    when(networkStore.getActiveNodes()).thenReturn(Stream.of(activeNode));
    NodeInfo result = discoveryHelper.buildCurrent();
    assertThat(result).isNotNull();
    assertThat(result.getUrl()).isEqualTo("http://somedomain.com/");
    assertThat(result.getRecipients()).hasSize(5);
    List<Recipient> recipients = List.copyOf(result.getRecipients());
    assertThat(recipients.stream().map(Recipient::getKey).collect(Collectors.toList())).containsExactlyInAnyOrderElementsOf(keys);
    verify(networkStore).getActiveNodes();
    verify(runtimeContext).getP2pServerUri();
    mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) KeyNotFoundException(com.quorum.tessera.encryption.KeyNotFoundException) PublicKey(com.quorum.tessera.encryption.PublicKey) DiscoveryHelper(com.quorum.tessera.discovery.DiscoveryHelper) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) Test(org.junit.Test) NodeUri(com.quorum.tessera.discovery.NodeUri) Collectors(java.util.stream.Collectors) Recipient(com.quorum.tessera.partyinfo.node.Recipient) Mockito(org.mockito.Mockito) ActiveNode(com.quorum.tessera.discovery.ActiveNode) NetworkStore(com.quorum.tessera.discovery.NetworkStore) List(java.util.List) MockedStatic(org.mockito.MockedStatic) Stream(java.util.stream.Stream) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) After(org.junit.After) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Enclave(com.quorum.tessera.enclave.Enclave) URI(java.net.URI) Before(org.junit.Before) PublicKey(com.quorum.tessera.encryption.PublicKey) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Recipient(com.quorum.tessera.partyinfo.node.Recipient) ActiveNode(com.quorum.tessera.discovery.ActiveNode) RuntimeContext(com.quorum.tessera.context.RuntimeContext) URI(java.net.URI) Test(org.junit.Test)

Example 3 with NetworkStore

use of com.quorum.tessera.discovery.NetworkStore in project tessera by ConsenSys.

the class DiscoveryHelperTest method buildAllNodeInfos.

@Test
public void buildAllNodeInfos() {
    when(runtimeContext.getP2pServerUri()).thenReturn(URI.create("http://own.com"));
    final ActiveNode node1 = ActiveNode.Builder.create().withUri(NodeUri.create("http://node1.com")).withKeys(List.of(PublicKey.from("key1".getBytes()))).withSupportedVersions(List.of("v1")).build();
    final ActiveNode node2 = ActiveNode.Builder.create().withUri(NodeUri.create("http://node2.com")).withKeys(List.of(PublicKey.from("key2".getBytes()))).withSupportedVersions(List.of("v2")).build();
    when(networkStore.getActiveNodes()).thenReturn(Stream.of(node1, node2));
    final Set<NodeInfo> nodeInfos = discoveryHelper.buildRemoteNodeInfos();
    assertThat(nodeInfos).hasSize(2);
    Set<ActiveNode> activeNodes = nodeInfos.stream().map(nodeInfo -> ActiveNode.Builder.create().withUri(NodeUri.create(nodeInfo.getUrl())).withKeys(nodeInfo.getRecipients().stream().map(Recipient::getKey).collect(Collectors.toSet())).withSupportedVersions(nodeInfo.supportedApiVersions()).build()).collect(Collectors.toSet());
    assertThat(activeNodes).containsExactlyInAnyOrder(node1, node2);
    verify(networkStore).getActiveNodes();
    verify(runtimeContext).getP2pServerUri();
    mockedRuntimeContext.verify(RuntimeContext::getInstance);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) KeyNotFoundException(com.quorum.tessera.encryption.KeyNotFoundException) PublicKey(com.quorum.tessera.encryption.PublicKey) DiscoveryHelper(com.quorum.tessera.discovery.DiscoveryHelper) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) Test(org.junit.Test) NodeUri(com.quorum.tessera.discovery.NodeUri) Collectors(java.util.stream.Collectors) Recipient(com.quorum.tessera.partyinfo.node.Recipient) Mockito(org.mockito.Mockito) ActiveNode(com.quorum.tessera.discovery.ActiveNode) NetworkStore(com.quorum.tessera.discovery.NetworkStore) List(java.util.List) MockedStatic(org.mockito.MockedStatic) Stream(java.util.stream.Stream) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) After(org.junit.After) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Enclave(com.quorum.tessera.enclave.Enclave) URI(java.net.URI) Before(org.junit.Before) NodeInfo(com.quorum.tessera.partyinfo.node.NodeInfo) Recipient(com.quorum.tessera.partyinfo.node.Recipient) ActiveNode(com.quorum.tessera.discovery.ActiveNode) RuntimeContext(com.quorum.tessera.context.RuntimeContext) Test(org.junit.Test)

Example 4 with NetworkStore

use of com.quorum.tessera.discovery.NetworkStore in project tessera by ConsenSys.

the class DiscoveryProvider method provider.

/**
 * @see java.util.ServiceLoader.Provider
 */
public static Discovery provider() {
    final DiscoveryHolder discoveryHolder = DiscoveryHolder.create();
    if (discoveryHolder.get().isPresent()) {
        return discoveryHolder.get().get();
    }
    final NetworkStore networkStore = NetworkStore.getInstance();
    final RuntimeContext runtimeContext = RuntimeContext.getInstance();
    final Discovery discovery;
    if (runtimeContext.isDisablePeerDiscovery()) {
        final Set<NodeUri> knownNodes = runtimeContext.getPeers().stream().map(NodeUri::create).collect(Collectors.toUnmodifiableSet());
        discovery = new DisabledAutoDiscovery(networkStore, knownNodes);
    } else {
        discovery = new AutoDiscovery(networkStore);
    }
    discoveryHolder.set(discovery);
    return discovery;
}
Also used : Discovery(com.quorum.tessera.discovery.Discovery) NetworkStore(com.quorum.tessera.discovery.NetworkStore) NodeUri(com.quorum.tessera.discovery.NodeUri) RuntimeContext(com.quorum.tessera.context.RuntimeContext)

Example 5 with NetworkStore

use of com.quorum.tessera.discovery.NetworkStore in project tessera by ConsenSys.

the class EnclaveKeySynchroniserProviderTest method provider.

@Test
public void provider() {
    try (var mockedEnclave = mockStatic(Enclave.class);
        var mockedNetworkStore = mockStatic(NetworkStore.class)) {
        NetworkStore networkStore = mock(NetworkStore.class);
        mockedNetworkStore.when(NetworkStore::getInstance).thenReturn(networkStore);
        Enclave enclave = mock(Enclave.class);
        mockedEnclave.when(Enclave::create).thenReturn(enclave);
        EnclaveKeySynchroniser enclaveKeySynchroniser = EnclaveKeySynchroniserProvider.provider();
        assertThat(enclaveKeySynchroniser).isNotNull().isExactlyInstanceOf(EnclaveKeySynchroniserImpl.class);
        mockedEnclave.verify(Enclave::create);
        mockedEnclave.verifyNoMoreInteractions();
        mockedNetworkStore.verify(NetworkStore::getInstance);
        mockedNetworkStore.verifyNoMoreInteractions();
        verifyNoInteractions(networkStore);
        verifyNoInteractions(enclave);
    }
}
Also used : Enclave(com.quorum.tessera.enclave.Enclave) NetworkStore(com.quorum.tessera.discovery.NetworkStore) EnclaveKeySynchroniser(com.quorum.tessera.discovery.EnclaveKeySynchroniser) Test(org.junit.Test)

Aggregations

NetworkStore (com.quorum.tessera.discovery.NetworkStore)8 Enclave (com.quorum.tessera.enclave.Enclave)7 Test (org.junit.Test)5 RuntimeContext (com.quorum.tessera.context.RuntimeContext)4 DiscoveryHelper (com.quorum.tessera.discovery.DiscoveryHelper)4 NodeUri (com.quorum.tessera.discovery.NodeUri)4 ActiveNode (com.quorum.tessera.discovery.ActiveNode)3 KeyNotFoundException (com.quorum.tessera.encryption.KeyNotFoundException)3 PublicKey (com.quorum.tessera.encryption.PublicKey)3 NodeInfo (com.quorum.tessera.partyinfo.node.NodeInfo)3 Recipient (com.quorum.tessera.partyinfo.node.Recipient)3 URI (java.net.URI)3 List (java.util.List)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3 Stream (java.util.stream.Stream)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)3 After (org.junit.After)3