Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4e7df1

Browse files
committed
add EpointChaincodeCommon Test
1 parent 51b85fe commit f4e7df1

File tree

6 files changed

+114
-74
lines changed

6 files changed

+114
-74
lines changed

src/main/java/ijarvis/intelliq/FabricCA/FabricCAApp.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import ijarvis.intelliq.Fabric.FabricApp;
44
import ijarvis.intelliq.Fabric.SampleUser;
5+
import ijarvis.intelliq.LedgerRecord;
56
import org.apache.log4j.Logger;
67
import org.hyperledger.fabric.sdk.*;
78
import org.hyperledger.fabric.sdk.exception.CryptoException;
89
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
10+
import org.hyperledger.fabric.sdk.exception.ProposalException;
911
import org.hyperledger.fabric.sdk.security.CryptoSuite;
1012

1113
import java.util.Collection;
@@ -14,25 +16,74 @@ public class FabricCAApp {
1416
private static Logger logger=Logger.getLogger(FabricCAApp.class);
1517
public static HFClient client=null;
1618
public static CryptoSuite cs = CryptoSuite.Factory.getCryptoSuite();
19+
public static ChaincodeID cid = ChaincodeID.newBuilder().setName(TestConfigure.CHAINCODENAME).setVersion(TestConfigure.CHAINCODEVERSION).build();
1720
public static void init(User CAUSER) throws CryptoException, InvalidArgumentException {
1821
client = HFClient.createNewInstance();
1922
client.setCryptoSuite(cs);
2023
client.setUserContext(CAUSER);
2124
}
25+
26+
/**
27+
* 调用链码添加KV结构
28+
*/
29+
public static void addKV(Channel channel, LedgerRecord record) throws ProposalException, InvalidArgumentException {
30+
QueryByChaincodeRequest req = client.newQueryProposalRequest();
31+
req.setChaincodeID(cid);
32+
req.setFcn("addkv");
33+
req.setArgs(record.toStringArray());
34+
Collection<ProposalResponse> resps = channel.queryByChaincode(req);
35+
for (ProposalResponse resp : resps) {
36+
String payload = new String(resp.getChaincodeActionResponsePayload());
37+
logger.debug("response: " + payload);
38+
}
39+
}
40+
/**
41+
* 调用链码更新
42+
*/
43+
public static void updateKV(Channel channel,LedgerRecord record) throws ProposalException, InvalidArgumentException {
44+
QueryByChaincodeRequest req = client.newQueryProposalRequest();
45+
req.setChaincodeID(cid);
46+
req.setFcn("updatekv");
47+
req.setArgs(record.toStringArray());
48+
Collection<ProposalResponse> resps = channel.queryByChaincode(req);
49+
for (ProposalResponse resp : resps) {
50+
String payload = new String(resp.getChaincodeActionResponsePayload());
51+
logger.debug("response: " + payload);
52+
}
53+
}
54+
55+
56+
57+
58+
2259
/*
2360
* 实现根绝给定的Key查询数据
2461
* */
25-
public static void queryFabcar(Channel channel, String key) throws Exception {
62+
public static void querykv(Channel channel, String key) throws Exception {
2663
QueryByChaincodeRequest req = client.newQueryProposalRequest();
27-
ChaincodeID cid = ChaincodeID.newBuilder().setName("epointchaincodezzk").setVersion("0.1").build();
2864
req.setChaincodeID(cid);
2965
req.setFcn("query");
3066
req.setArgs(new String[] { key });
3167
System.out.println("Querying for " + key);
3268
Collection<ProposalResponse> resps = channel.queryByChaincode(req);
3369
for (ProposalResponse resp : resps) {
3470
String payload = new String(resp.getChaincodeActionResponsePayload());
35-
System.out.println("response: " + payload);
71+
logger.debug("response: " + payload);
72+
}
73+
}
74+
75+
/**
76+
* 调用链码查询给定Key的历史值
77+
*/
78+
public static void queryhistory(Channel channel,String key) throws ProposalException, InvalidArgumentException {
79+
QueryByChaincodeRequest req = client.newQueryProposalRequest();
80+
req.setChaincodeID(cid);
81+
req.setFcn("queryhistory");
82+
req.setArgs(new String[]{key});
83+
Collection<ProposalResponse> resps = channel.queryByChaincode(req);
84+
for (ProposalResponse resp : resps) {
85+
String payload = new String(resp.getChaincodeActionResponsePayload());
86+
logger.debug("response: " + payload);
3687
}
3788
}
3889
}

src/main/java/ijarvis/intelliq/FabricCA/TestConfigure.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import java.util.HashMap;
1111

1212
public class TestConfigure {
13+
public static String CHAINCODENAME="epointchaincodecommon";
14+
public static String CHAINCODEVERSION="0.2";
15+
public static String CHANNLNAME="epointchannel";
1316
public static HashMap<String,SampleOrg> getConfigure() throws MalformedURLException, InvalidArgumentException {
1417
HashMap<String,SampleOrg> orgHashMap=new HashMap<>();
1518
SampleOrg org1=new SampleOrg("org1","Org1MSP");

src/main/java/ijarvis/intelliq/LedgerRecord.java

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,39 @@
44
* 账本数据结构,非常简单的一个数据Bean而已
55
*/
66
public class LedgerRecord {
7-
private String Perid;
8-
private String Legalname;
9-
private String Carddate;
10-
private String Cardaddr;
11-
private String Cardmonery;
12-
private String Companyaddr;
7+
private String Key;
8+
private String Value;
139

14-
public String getPerid() {
15-
return Perid;
10+
public String getKey() {
11+
return Key;
1612
}
1713

18-
public void setPerid(String perid) {
19-
Perid = perid;
14+
public void setKey(String key) {
15+
Key = key;
2016
}
2117

22-
public String getLegalname() {
23-
return Legalname;
18+
public String getValue() {
19+
return Value;
2420
}
2521

26-
public void setLegalname(String legalname) {
27-
Legalname = legalname;
22+
public void setValue(String value) {
23+
Value = value;
2824
}
2925

30-
public String getCarddate() {
31-
return Carddate;
32-
}
33-
34-
public void setCarddate(String carddate) {
35-
Carddate = carddate;
36-
}
37-
38-
public String getCardaddr() {
39-
return Cardaddr;
40-
}
41-
42-
public void setCardaddr(String cardaddr) {
43-
Cardaddr = cardaddr;
44-
}
45-
46-
public String getCardmonery() {
47-
return Cardmonery;
48-
}
49-
50-
public void setCardmonery(String cardmonery) {
51-
Cardmonery = cardmonery;
52-
}
53-
54-
public String getCompanyaddr() {
55-
return Companyaddr;
56-
}
57-
58-
public void setCompanyaddr(String companyaddr) {
59-
Companyaddr = companyaddr;
60-
}
61-
62-
public LedgerRecord(String perid, String legalname, String carddate, String cardaddr, String cardmonery, String companyaddr) {
63-
Perid = perid;
64-
Legalname = legalname;
65-
Carddate = carddate;
66-
Cardaddr = cardaddr;
67-
Cardmonery = cardmonery;
68-
Companyaddr = companyaddr;
26+
public LedgerRecord(String key, String value) {
27+
Key = key;
28+
Value = value;
6929
}
7030

7131
public String[] toStringArray() {
72-
return new String[] {this.getPerid(),this.getLegalname(),this.getCarddate(),this.getCardaddr(),this.getCardmonery(),this.getCompanyaddr()};
32+
return new String[] {this.getKey(),this.getValue()};
7333
}
7434

7535
@Override
7636
public String toString() {
7737
return "LedgerRecord{" +
78-
"Perid='" + Perid + '\'' +
79-
", Legalname='" + Legalname + '\'' +
80-
", Carddate='" + Carddate + '\'' +
81-
", Cardaddr='" + Cardaddr + '\'' +
82-
", Cardmonery='" + Cardmonery + '\'' +
83-
", Companyaddr='" + Companyaddr + '\'' +
38+
"Key='" + Key + '\'' +
39+
", Value='" + Value + '\'' +
8440
'}';
8541
}
8642
}

src/test/java/ijarvis/intelliq/Fabric/AppTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class AppTest {
2222
private static String CONNFIG_Peer0Org2="grpc://192.168.188.114:7051";
2323
private static String CONNFIG_Peer1Org2="grpc://192.168.188.115:7051";
2424
private static String CHANNELID="epointchannel";
25-
private static LedgerRecord PERSONINFO=new LedgerRecord("liudong","刘东","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
25+
private static LedgerRecord PERSONINFO=new LedgerRecord("liudong","刘东");
2626
@Before
2727
public void Setup() throws CryptoException, InvalidArgumentException {
2828
logger.debug("Fabric Test Init........");
@@ -54,7 +54,7 @@ public void TestEpointChainCodeQuery() throws Exception {
5454
channel.addPeer(FabricApp.client.newPeer("peer", CONNFIG_Peer0Org1));
5555
channel.addOrderer(FabricApp.client.newOrderer("orderer", CONNFIG_Orderer));
5656
channel.initialize();
57-
FabricApp.queryFabcar(channel, PERSONINFO.getPerid());
57+
FabricApp.queryFabcar(channel, PERSONINFO.getKey());
5858
}
5959

6060
/**
@@ -69,7 +69,7 @@ public void TestEpointChainCodeMutilInstert() throws Exception{
6969
channel.initialize();
7070
for (int i =0 ;i<10000;i++){
7171
String perid=UUID.randomUUID().toString();
72-
LedgerRecord tmp=new LedgerRecord(perid,"测试","2017-12-13","江苏张家港","3000","苏州");
72+
LedgerRecord tmp=new LedgerRecord(perid,"测试");
7373
FabricApp.instertFabcar(channel, tmp);
7474
}
7575
logger.debug("测试完成");

src/test/java/ijarvis/intelliq/FabricCA/FabricCATestUseCAServer.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
*/
2222
public class FabricCATestUseCAServer {
2323
private static Logger logger=Logger.getLogger(FabricCATestUseCAServer.class);
24-
private static String CHANNELID="epointchannel";
2524
HashMap<String,SampleOrg> orgHashMap=new HashMap<>();
26-
private static LedgerRecord PERSONINFO=new LedgerRecord("liuwenru","刘美丽","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
25+
private static LedgerRecord PERSONINFO=new LedgerRecord("liuwenru","刘文儒");
2726

2827
@Before
2928
public void Setup() throws EnrollmentException, InvalidArgumentException, CryptoException, org.hyperledger.fabric.sdk.exception.InvalidArgumentException, MalformedURLException {
@@ -37,12 +36,43 @@ public void Setup() throws EnrollmentException, InvalidArgumentException, Crypto
3736
FabricCAApp.init(user1);
3837
}
3938
@Test
40-
public void TestEpointChainCodeQuery() throws Exception {
41-
logger.debug("测试Fabric 查询功能");
42-
Channel channel = FabricCAApp.client.newChannel(CHANNELID);
39+
public void TestEpointChainCodeAddKV()throws Exception{
40+
logger.debug("链码测试........向链码中添加KV");
41+
Channel channel = FabricCAApp.client.newChannel(TestConfigure.CHANNLNAME);
4342
channel.addPeer(FabricCAApp.client.newPeer("peer", orgHashMap.get("org1").getPeerLocation("peer0org1")));
4443
channel.addOrderer(FabricCAApp.client.newOrderer("orderer", orgHashMap.get("org1").getOrdererLocation("orderer")));
4544
channel.initialize();
46-
FabricCAApp.queryFabcar(channel, PERSONINFO.getPerid());
45+
FabricCAApp.addKV(channel, PERSONINFO);
46+
47+
}
48+
@Test
49+
public void TestEpointChainCodeUpdate() throws Exception {
50+
logger.debug("链码测试........向链码中更新KV");
51+
LedgerRecord tmp=new LedgerRecord("liuwenru","刘美丽");
52+
Channel channel = FabricCAApp.client.newChannel(TestConfigure.CHANNLNAME);
53+
channel.addPeer(FabricCAApp.client.newPeer("peer", orgHashMap.get("org1").getPeerLocation("peer0org1")));
54+
channel.addOrderer(FabricCAApp.client.newOrderer("orderer", orgHashMap.get("org1").getOrdererLocation("orderer")));
55+
channel.initialize();
56+
FabricCAApp.updateKV(channel, tmp);
57+
}
58+
59+
@Test
60+
public void TestEpointChainCodeQuery() throws Exception{
61+
logger.debug("链码测试........向链码查询");
62+
Channel channel = FabricCAApp.client.newChannel(TestConfigure.CHANNLNAME);
63+
channel.addPeer(FabricCAApp.client.newPeer("peer", orgHashMap.get("org1").getPeerLocation("peer0org1")));
64+
channel.addOrderer(FabricCAApp.client.newOrderer("orderer", orgHashMap.get("org1").getOrdererLocation("orderer")));
65+
channel.initialize();
66+
FabricCAApp.querykv(channel, PERSONINFO.getKey());
67+
}
68+
69+
@Test
70+
public void TestEpointChainCodeQueryHistory() throws Exception{
71+
logger.debug("链码测试........向链码查询给定Key的历史值");
72+
Channel channel = FabricCAApp.client.newChannel(TestConfigure.CHANNLNAME);
73+
channel.addPeer(FabricCAApp.client.newPeer("peer", orgHashMap.get("org1").getPeerLocation("peer0org1")));
74+
channel.addOrderer(FabricCAApp.client.newOrderer("orderer", orgHashMap.get("org1").getOrdererLocation("orderer")));
75+
channel.initialize();
76+
FabricCAApp.queryhistory(channel, PERSONINFO.getKey());
4777
}
4878
}

src/test/java/ijarvis/intelliq/FabricCA/FabricCATestUseStatic.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class FabricCATestUseStatic {
1919
private static String CONNFIG_Peer1Org2="grpc://192.168.188.115:7051";
2020
private static String CHANNELID="epointchannel";
2121
private static String keypath="";
22-
private static LedgerRecord PERSONINFO=new LedgerRecord("liuwenru","刘东","2017-12-12","江苏省张家港市","10000","江苏省苏州市张家港市国泰新点");
22+
private static LedgerRecord PERSONINFO=new LedgerRecord("liuwenru","刘东");
2323
@Before
2424
public void Setup() throws CryptoException, InvalidArgumentException {
2525
logger.debug("Fabric Test Init........");
@@ -34,7 +34,7 @@ public void TestEpointChainCodeQuery() throws Exception {
3434
channel.addPeer(FabricApp.client.newPeer("peer", CONNFIG_Peer0Org1));
3535
channel.addOrderer(FabricApp.client.newOrderer("orderer", CONNFIG_Orderer));
3636
channel.initialize();
37-
FabricApp.queryFabcar(channel, PERSONINFO.getPerid());
37+
FabricApp.queryFabcar(channel, PERSONINFO.getKey());
3838
}
3939

4040

0 commit comments

Comments
 (0)