From 6c07d06ef0587af86ecbea8b8b2f648f00497c18 Mon Sep 17 00:00:00 2001 From: rickr Date: Wed, 1 May 2019 15:03:20 -0400 Subject: [PATCH] FABJ-433 Fix return SD overrides. Return default implementation for SD peer orderer additions. PS 02Fix testcase error. Change-Id: Id9cf82210c3d63148694a3ef51feac6b7e5f0619 Signed-off-by: rickr --- .../org/hyperledger/fabric/sdk/Channel.java | 70 +++++++++++++++---- .../sdkintegration/ServiceDiscoveryIT.java | 25 +++++++ 2 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/hyperledger/fabric/sdk/Channel.java b/src/main/java/org/hyperledger/fabric/sdk/Channel.java index 97911ba6..833df0bf 100644 --- a/src/main/java/org/hyperledger/fabric/sdk/Channel.java +++ b/src/main/java/org/hyperledger/fabric/sdk/Channel.java @@ -217,10 +217,6 @@ public class Channel implements Serializable { } } - // public void clean(){ -// channelEventQue = null; -// } -// private Channel(String name, HFClient hfClient, Orderer orderer, ChannelConfiguration channelConfiguration, byte[][] signers) throws InvalidArgumentException, TransactionException { this(name, hfClient, false); @@ -1329,7 +1325,9 @@ public interface SDPeerAddition { transient SDPeerAddition sdPeerAddition = null; /** - * Set service discovery peer addition override. + * Set service discovery orderer addition override. + *

+ * Any service discovery properties {@link #setServiceDiscoveryProperties(Properties)} should be set before calling this. * * @param sdOrdererAddition * @return @@ -1340,10 +1338,32 @@ public SDOrdererAddition setSDOrdererAddition(SDOrdererAddition sdOrdererAdditio this.sdOrdererAddition = sdOrdererAddition; + if (null == ret) { + ret = new SDOrdererDefaultAddition(getServiceDiscoveryProperties()); + } + return ret; } + /** + * Get current service discovery orderer addition override. + *

+ * Any service discovery properties {@link #setServiceDiscoveryProperties(Properties)} should be set before calling this. + * + * @return SDOrdererAddition + */ + + public SDOrdererAddition getSDOrdererAddition() { + + if (null == sdOrdererAddition) { + sdOrdererAddition = new SDOrdererDefaultAddition(getServiceDiscoveryProperties()); + } + + return sdOrdererAddition; + + } + private static byte[] combineCerts(Collection... certCollections) throws IOException { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { for (Collection certCollection : certCollections) { @@ -1407,10 +1427,10 @@ public interface SDOrdererAddition { private Properties serviceDiscoveryProperties = new Properties(); - private static class SDOrdererDefaultAddition implements SDOrdererAddition { - private final Properties config; + public static class SDOrdererDefaultAddition implements SDOrdererAddition { + protected final Properties config; - SDOrdererDefaultAddition(Properties config) { + public SDOrdererDefaultAddition(Properties config) { this.config = config == null ? new Properties() : (Properties) config.clone(); } @@ -1464,10 +1484,10 @@ public Orderer addOrderer(SDOrdererAdditionInfo sdOrdererAdditionInfo) throws In } } - private static class SDOPeerDefaultAddition implements SDPeerAddition { - private final Properties config; + public static class SDOPeerDefaultAddition implements SDPeerAddition { + protected final Properties config; - SDOPeerDefaultAddition(Properties config) { + public SDOPeerDefaultAddition(Properties config) { this.config = config == null ? new Properties() : (Properties) config.clone(); } @@ -1538,6 +1558,8 @@ static Object findClientProp(Properties config, final String prop, final String /** * Set service discovery peer addition override. + *

+ * Any service discovery properties {@link #setServiceDiscoveryProperties(Properties)} should be set before calling this. * * @param sdPeerAddition * @return @@ -1548,10 +1570,32 @@ public SDPeerAddition setSDPeerAddition(SDPeerAddition sdPeerAddition) { this.sdPeerAddition = sdPeerAddition; + if (ret == null) { + ret = new SDOPeerDefaultAddition(getServiceDiscoveryProperties()); + } + return ret; } + /** + * Get current service discovery peer addition override. + *

+ * Any service discovery properties {@link #setServiceDiscoveryProperties(Properties)} should be set before calling this. + * + * @return SDOrdererAddition + */ + + public SDPeerAddition getSDPeerAddition() { + + if (null == sdPeerAddition) { + sdPeerAddition = new SDOPeerDefaultAddition(getServiceDiscoveryProperties()); + } + + return sdPeerAddition; + + } + /** * load the peer organizations CA certificates into the channel's trust store so that we * can verify signatures from peer messages @@ -3444,7 +3488,7 @@ public LifecycleApproveChaincodeDefinitionForMyOrgProposalResponse sendLifecycle * Approve chaincode to be run on this peer's organization. * * @param lifecycleApproveChaincodeDefinitionForMyOrgRequest the request see {@link LifecycleApproveChaincodeDefinitionForMyOrgRequest} - * @param peers to send the request to. + * @param peers to send the request to. * @return A {@link LifecycleApproveChaincodeDefinitionForMyOrgProposalResponse} * @throws ProposalException * @throws InvalidArgumentException @@ -3534,7 +3578,7 @@ public Collection s * Commit chaincode final approval to run on all organizations that have approved. * * @param lifecycleCommitChaincodeDefinitionRequest The request see {@link LifecycleCommitChaincodeDefinitionRequest} - * @param peers to send the request to. + * @param peers to send the request to. * @return A {@link LifecycleCommitChaincodeDefinitionProposalResponse} * @throws InvalidArgumentException * @throws ProposalException diff --git a/src/test/java/org/hyperledger/fabric/sdkintegration/ServiceDiscoveryIT.java b/src/test/java/org/hyperledger/fabric/sdkintegration/ServiceDiscoveryIT.java index 8e0d255c..972b5eff 100644 --- a/src/test/java/org/hyperledger/fabric/sdkintegration/ServiceDiscoveryIT.java +++ b/src/test/java/org/hyperledger/fabric/sdkintegration/ServiceDiscoveryIT.java @@ -17,6 +17,7 @@ package org.hyperledger.fabric.sdkintegration; import java.io.File; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.EnumSet; @@ -36,6 +37,8 @@ import org.hyperledger.fabric.sdk.ServiceDiscovery; import org.hyperledger.fabric.sdk.TransactionProposalRequest; import org.hyperledger.fabric.sdk.TransactionRequest; +import org.hyperledger.fabric.sdk.exception.InvalidArgumentException; +import org.hyperledger.fabric.sdk.exception.ServiceDiscoveryException; import org.hyperledger.fabric.sdk.exception.TransactionEventException; import org.hyperledger.fabric.sdk.security.CryptoSuite; import org.hyperledger.fabric.sdk.testutils.TestConfig; @@ -43,11 +46,13 @@ import static java.lang.String.format; import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNotNull; import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.fail; import static org.hyperledger.fabric.sdk.Channel.DiscoveryOptions.createDiscoveryOptions; import static org.hyperledger.fabric.sdk.Channel.PeerOptions.createPeerOptions; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; public class ServiceDiscoveryIT { private static final TestConfig testConfig = TestConfig.getConfig(); @@ -107,6 +112,25 @@ public void setup() throws Exception { foo.shutdown(false); foo = client.deSerializeChannel(bytes); + //You can override the default implementation but these just reuse the default. + Channel.SDPeerAddition sdPeerAddition = foo.getSDPeerAddition(); + assertNotNull(sdPeerAddition); + foo.setSDPeerAddition(sdPeerAddition); + assertSame(sdPeerAddition, foo.getSDPeerAddition()); + + // Just see if we can override and collect the orderers. + final ArrayList testCollectOrderers = new ArrayList(); + + Channel.SDOrdererAddition sdOrdererAddition = foo.setSDOrdererAddition(new Channel.SDOrdererDefaultAddition(sdprops) { + @Override + public Orderer addOrderer(Channel.SDOrdererAdditionInfo sdOrdererAdditionInfo) throws InvalidArgumentException, ServiceDiscoveryException { + Orderer ret = super.addOrderer(sdOrdererAdditionInfo); + testCollectOrderers.add(ret); // just see if our extended works. + return ret; + } + }); + assertNotNull(sdOrdererAddition); + foo.initialize(); // initialize the channel. Set expect = new HashSet<>(Arrays.asList(protocol + "//orderer.example.com:7050")); //discovered orderer @@ -114,6 +138,7 @@ public void setup() throws Exception { expect.remove(orderer.getUrl()); } assertTrue(expect.isEmpty()); + assertEquals(foo.getOrderers().size(), testCollectOrderers.size()); final Collection discoveredChaincodeNames = foo.getDiscoveredChaincodeNames();