TransactionTest
This commit is contained in:
parent
670acb078e
commit
153e046928
119
src/test/java/blockchain/query/ChaincodeTransactionTest.java
Normal file
119
src/test/java/blockchain/query/ChaincodeTransactionTest.java
Normal file
|
@ -0,0 +1,119 @@
|
|||
package blockchain.query;
|
||||
|
||||
import blockchain.client.ChannelClientWrapper;
|
||||
import blockchain.client.FabricClientWrapper;
|
||||
import blockchain.configuration.Config;
|
||||
import blockchain.user.UserContext;
|
||||
import blockchain.utility.Util;
|
||||
import org.apache.log4j.BasicConfigurator;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.hyperledger.fabric.sdk.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ChaincodeTransactionTest {
|
||||
private static Logger logger = Logger.getLogger(ChaincodeTransactionTest.class);
|
||||
@Test
|
||||
public void TestATransaction(){
|
||||
BasicConfigurator.configure();
|
||||
|
||||
//UserContext user = Util.readUserContext(Config.ORG1,"User-org1");
|
||||
UserContext user = Util.readUserContext(Config.ORG1,Config.ADMIN);
|
||||
|
||||
try{
|
||||
|
||||
String chaincode = Config.CHAINCODE_NAME;
|
||||
FabricClientWrapper fabricClientWrapper = new FabricClientWrapper(user);
|
||||
ChannelClientWrapper channelClientWrapper = fabricClientWrapper.createChannelClient(Config.CHANNEL_NAME);
|
||||
|
||||
Channel channel = channelClientWrapper.getChannel();
|
||||
Peer peer = fabricClientWrapper.getClient().newPeer(Config.ORG_PEER,Config.ORG_PEER_URL);
|
||||
EventHub eventHub = fabricClientWrapper.getClient().newEventHub("eventhub01", Config.ORG_PEER_EVENT_URL);
|
||||
Orderer orderer = fabricClientWrapper.getClient().newOrderer(Config.ORDERER_NAME,Config.ORDERER_URL);
|
||||
channel.addPeer(peer);
|
||||
channel.addEventHub(eventHub);
|
||||
channel.addOrderer(orderer);
|
||||
channel.initialize();
|
||||
|
||||
/*
|
||||
List<String> args = new ArrayList<>();
|
||||
args.add("b");
|
||||
args.add("a");
|
||||
args.add("5");
|
||||
|
||||
String accountFromKey = args.get(0);
|
||||
String accountToKey = args.get(1);
|
||||
int amount = Integer.parseInt(args.get(2));
|
||||
|
||||
System.out.println("args0 = "+accountFromKey+" -- args1 = "+accountToKey+" -- args2 = "+amount);
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
String[] args = {"b","a","5"};
|
||||
|
||||
Collection<ProposalResponse> responseQuery = channelClientWrapper.queryByChainCode(chaincode,"invoke",args);
|
||||
for(ProposalResponse presp : responseQuery){
|
||||
ChaincodeResponse.Status status = presp.getStatus();
|
||||
if(status.getStatus()!=200){
|
||||
throw new Exception(presp.getMessage());
|
||||
}
|
||||
String stringResponse = new String(presp.getChaincodeActionResponsePayload());
|
||||
logger.info("RESPONSE : "+stringResponse);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
TransactionProposalRequest tpr = fabricClientWrapper.getClient().newTransactionProposalRequest();
|
||||
ChaincodeID cid = ChaincodeID.newBuilder().setName(Config.CHAINCODE_NAME).build();
|
||||
tpr.setChaincodeID(cid);
|
||||
tpr.setFcn("invoke");
|
||||
tpr.setArgs(new String[]{"b","a","20"});
|
||||
|
||||
Collection<ProposalResponse> responses = channel.sendTransactionProposal(tpr);
|
||||
List<ProposalResponse> invalid = responses.stream().filter(r -> r.isInvalid()).collect(Collectors.toList());
|
||||
if (!invalid.isEmpty()) {
|
||||
invalid.forEach(response -> {
|
||||
logger.error(response.getMessage());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
BlockEvent.TransactionEvent event = channel.sendTransaction(responses).get();
|
||||
|
||||
event.getTransactionID();
|
||||
/*
|
||||
BlockEvent.TransactionEvent event = sendTransaction(fabricClientWrapper.getClient(), channel).get(60, TimeUnit.SECONDS);
|
||||
if (event.isValid()) {
|
||||
logger.info("Transacion tx: " + event.getTransactionID() + " is completed.");
|
||||
} else {
|
||||
logger.error("Transaction tx: " + event.getTransactionID() + " is invalid.");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
//String[] args = {"a","b","5"};
|
||||
//logger.info("LENGHT : "+args.length);
|
||||
//Collection<ProposalResponse> responseQuery = channelClientWrapper.queryByChainCode(chaincode,"invoke",args);
|
||||
//for(ProposalResponse presp : responseQuery){
|
||||
//ChaincodeResponse.Status status = presp.getStatus();
|
||||
//if(status.getStatus()!=200){
|
||||
// throw new Exception(presp.getMessage());
|
||||
//}
|
||||
//String stringResponse = new String(presp.getChaincodeActionResponsePayload());
|
||||
//logger.info("RESPONSE : "+stringResponse);
|
||||
//}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,8 +21,8 @@ public class QueryTest {
|
|||
public void TestAQueryChannels() {
|
||||
BasicConfigurator.configure();
|
||||
|
||||
//UserContext admin = Util.readUserContext(Config.ORG1,Config.ADMIN);
|
||||
UserContext user = Util.readUserContext(Config.ORG1,"User-org1");
|
||||
UserContext user = Util.readUserContext(Config.ORG1,Config.ADMIN);
|
||||
//UserContext user = Util.readUserContext(Config.ORG1,"User-org1");
|
||||
|
||||
try{
|
||||
|
||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in a new issue