@SuppressWarnings("unchecked") @Test public void testInstallChainCode() throws ClassNotFoundException, SQLException, InvalidArgumentException{ PowerMockito.mockStatic(SDKUtils.class); PowerMockito.mockStatic(HFClient.class); when(HFClient.createNewInstance()).thenReturn(mockClient); Channel mockChannel = mock(Channel.class); when(mockClient.newChannel(anyString())).thenReturn(mockChannel); InstallProposalRequest mockInstallProposalRequest = mock(InstallProposalRequest.class); when(mockClient.newInstallProposalRequest()).thenReturn(mockInstallProposalRequest); String configPath = "src/test/resources/blockchain-query"; Class.forName("com.impetus.fabric.jdbc.FabricDriver"); QueryBlock qb = new QueryBlock(configPath,"mychannel", null, null); qb.setChannel(); String chaincodeName ="chncodefunc"; String version = "1.0"; String chaincodePath = "hyperledger/fabric/examples/chaincode/go/chaincode_example02"; when(SDKUtils.getProposalConsistencySets(anyCollection())).thenReturn(new ArrayList<>()); String result = qb.installChaincode(chaincodeName, version, qb.getConf().getConfigPath(), chaincodePath); assert(result.equals("Chaincode installed successfully")); }
public String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); int numInstallProposal = 0; try { checkConfig(); InstallProposalRequest installProposalRequest = getInstallProposalRequest(chaincodeName, version, goPath, chaincodePath, userOrg); Set<Peer> peersFromOrg = userOrg.getPeers(); numInstallProposal = numInstallProposal + peersFromOrg.size(); responses = client.sendInstallProposal(installProposalRequest, peersFromOrg); for (ProposalResponse response : responses) { if (response.getStatus() == ProposalResponse.Status.SUCCESS) { logger.debug(String.format("Successful install proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); successful.add(response); } else { failed.add(response); } } if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Not enough endorsers for install :" + successful.size() + ". " + first.getMessage(); throw new BlkchnException(errMsg); } logger.info(String.format("Received %d install proposal responses. Successful+verified: %d . Failed: %d", numInstallProposal, successful.size(), failed.size())); return "Chaincode installed successfully"; } catch(Exception e) { String errMsg = "QueryBlock | installChaincode | " + e; logger.error(errMsg); throw new BlkchnException("Chaincode installation failed", e); } }
QueryBlock { public String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); int numInstallProposal = 0; try { checkConfig(); InstallProposalRequest installProposalRequest = getInstallProposalRequest(chaincodeName, version, goPath, chaincodePath, userOrg); Set<Peer> peersFromOrg = userOrg.getPeers(); numInstallProposal = numInstallProposal + peersFromOrg.size(); responses = client.sendInstallProposal(installProposalRequest, peersFromOrg); for (ProposalResponse response : responses) { if (response.getStatus() == ProposalResponse.Status.SUCCESS) { logger.debug(String.format("Successful install proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); successful.add(response); } else { failed.add(response); } } if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Not enough endorsers for install :" + successful.size() + ". " + first.getMessage(); throw new BlkchnException(errMsg); } logger.info(String.format("Received %d install proposal responses. Successful+verified: %d . Failed: %d", numInstallProposal, successful.size(), failed.size())); return "Chaincode installed successfully"; } catch(Exception e) { String errMsg = "QueryBlock | installChaincode | " + e; logger.error(errMsg); throw new BlkchnException("Chaincode installation failed", e); } } }
QueryBlock { public String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); int numInstallProposal = 0; try { checkConfig(); InstallProposalRequest installProposalRequest = getInstallProposalRequest(chaincodeName, version, goPath, chaincodePath, userOrg); Set<Peer> peersFromOrg = userOrg.getPeers(); numInstallProposal = numInstallProposal + peersFromOrg.size(); responses = client.sendInstallProposal(installProposalRequest, peersFromOrg); for (ProposalResponse response : responses) { if (response.getStatus() == ProposalResponse.Status.SUCCESS) { logger.debug(String.format("Successful install proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); successful.add(response); } else { failed.add(response); } } if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Not enough endorsers for install :" + successful.size() + ". " + first.getMessage(); throw new BlkchnException(errMsg); } logger.info(String.format("Received %d install proposal responses. Successful+verified: %d . Failed: %d", numInstallProposal, successful.size(), failed.size())); return "Chaincode installed successfully"; } catch(Exception e) { String errMsg = "QueryBlock | installChaincode | " + e; logger.error(errMsg); throw new BlkchnException("Chaincode installation failed", e); } } QueryBlock(String configPath, String channel, String username, String secret); }
QueryBlock { public String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); int numInstallProposal = 0; try { checkConfig(); InstallProposalRequest installProposalRequest = getInstallProposalRequest(chaincodeName, version, goPath, chaincodePath, userOrg); Set<Peer> peersFromOrg = userOrg.getPeers(); numInstallProposal = numInstallProposal + peersFromOrg.size(); responses = client.sendInstallProposal(installProposalRequest, peersFromOrg); for (ProposalResponse response : responses) { if (response.getStatus() == ProposalResponse.Status.SUCCESS) { logger.debug(String.format("Successful install proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); successful.add(response); } else { failed.add(response); } } if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Not enough endorsers for install :" + successful.size() + ". " + first.getMessage(); throw new BlkchnException(errMsg); } logger.info(String.format("Received %d install proposal responses. Successful+verified: %d . Failed: %d", numInstallProposal, successful.size(), failed.size())); return "Chaincode installed successfully"; } catch(Exception e) { String errMsg = "QueryBlock | installChaincode | " + e; logger.error(errMsg); throw new BlkchnException("Chaincode installation failed", e); } } QueryBlock(String configPath, String channel, String username, String secret); Config getConf(); String getAdminName(); void setAdminName(String adminName); String getChannelName(); void setChannelName(String channelName); void setChannel(); Channel getChannel(); void checkConfig(); synchronized void registerUser(String uname, String secret, String affiliation); synchronized HyperUser enroll(); Channel reconstructChannel(); Channel reconstructChannel(List<Peer> peers, HFClient client); String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath); String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath,
            String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers); DataFrame invokeChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs); String queryChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs); String upgradeChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath,
            String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers); Long getChannelHeight(); }
QueryBlock { public String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath) { Collection<ProposalResponse> responses; Collection<ProposalResponse> successful = new ArrayList<>(); Collection<ProposalResponse> failed = new ArrayList<>(); int numInstallProposal = 0; try { checkConfig(); InstallProposalRequest installProposalRequest = getInstallProposalRequest(chaincodeName, version, goPath, chaincodePath, userOrg); Set<Peer> peersFromOrg = userOrg.getPeers(); numInstallProposal = numInstallProposal + peersFromOrg.size(); responses = client.sendInstallProposal(installProposalRequest, peersFromOrg); for (ProposalResponse response : responses) { if (response.getStatus() == ProposalResponse.Status.SUCCESS) { logger.debug(String.format("Successful install proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName())); successful.add(response); } else { failed.add(response); } } if (failed.size() > 0) { ProposalResponse first = failed.iterator().next(); String errMsg = "Not enough endorsers for install :" + successful.size() + ". " + first.getMessage(); throw new BlkchnException(errMsg); } logger.info(String.format("Received %d install proposal responses. Successful+verified: %d . Failed: %d", numInstallProposal, successful.size(), failed.size())); return "Chaincode installed successfully"; } catch(Exception e) { String errMsg = "QueryBlock | installChaincode | " + e; logger.error(errMsg); throw new BlkchnException("Chaincode installation failed", e); } } QueryBlock(String configPath, String channel, String username, String secret); Config getConf(); String getAdminName(); void setAdminName(String adminName); String getChannelName(); void setChannelName(String channelName); void setChannel(); Channel getChannel(); void checkConfig(); synchronized void registerUser(String uname, String secret, String affiliation); synchronized HyperUser enroll(); Channel reconstructChannel(); Channel reconstructChannel(List<Peer> peers, HFClient client); String installChaincode(String chaincodeName, String version, String goPath, String chaincodePath); String instantiateChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath,
            String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers); DataFrame invokeChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs); String queryChaincode(String chaincodename, String chaincodeFunction, String[] chaincodeArgs); String upgradeChaincode(String chaincodeName, String chainCodeVersion, String chainCodePath,
            String chaincodeFunction, String[] chaincodeArgs, Endorsers endorsers); Long getChannelHeight();  }