Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Commit

Permalink
java functional tests - code and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Leonor Quintais <[email protected]>
  • Loading branch information
lquintai committed Oct 28, 2019
1 parent 2314996 commit ddbdabd
Show file tree
Hide file tree
Showing 12 changed files with 1,416 additions and 119 deletions.
2 changes: 1 addition & 1 deletion cucumber/features/evaluate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ Feature: Evaluate transaction
Then the logger should have been called with 'SUCCESS', 'Successfully evaluated transaction' and 'Returned value from readConga: {"value":"Big Conga"}'
Examples:
| language | assetType | name | version |
| JavaScript | Conga | JavaScriptContract | 0.0.1 |
| Java | Conga | JavaContract | 0.0.1 |
| TypeScript | Conga | TypeScriptContract | 0.0.1 |
1 change: 1 addition & 0 deletions cucumber/features/fabric-environments.feature
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Feature: Fabric Environments
Examples:
| language | assetType | name | upgradedName | version |
| JavaScript | Conga | JavaScriptContract | JavaScriptContract@0.0.2 | 0.0.1 |
| Java | Conga | JavaContract | JavaContract@0.0.2 | 0.0.1 |


@otherFabric
Expand Down
8 changes: 4 additions & 4 deletions cucumber/features/fabric-gateways.feature
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ Feature: Fabric Gateways
And the package has been installed
And the contract has been instantiated with the transaction '' and args '', not using private data
When I generate a <testLanguage> functional test for a <contractLanguage> contract
Then a functional test file with the filename '<assetType>Contract-<contractName>@0.0.1.test.<fileExtension>' should exist and contain the correct contents
Then a functional test file with .<fileExtension> extension for the <testLanguage> contract <contractName> version <version> with assets <assetType> should exist and contain the correct contents
And the tests should be runnable
Examples:
| contractName | assetType | contractLanguage | testLanguage | fileExtension | version |
| JavaScriptContract | Conga | JavaScript | JavaScript | js | 0.0.1 |
| JavaScriptContract2 | Conga | JavaScript | TypeScript | ts | 0.0.1 |
| TypeScriptContract | Conga | TypeScript | JavaScript | js | 0.0.1 |
| TypeScriptContract2 | Conga | TypeScript | TypeScript | ts | 0.0.1 |

| JavaContract | Conga | Java | Java | java | 0.0.1 |

@otherFabric
Scenario Outline: Generating tests for a contract (other fabric)
Expand All @@ -108,9 +108,9 @@ Feature: Fabric Gateways
And the gateway 'myGateway' is created
And I'm connected to the 'myGateway' gateway without association
When I generate a <testLanguage> functional test for a <contractLanguage> contract
Then a functional test file with the filename '<assetType>Contract-<contractName>@0.0.1.test.<fileExtension>' should exist and contain the correct contents
Then a functional test file with .<fileExtension> extension for the <testLanguage> contract <contractName> version <version> with assets <assetType> should exist and contain the correct contents
And the tests should be runnable
Examples:
| contractName | assetType | contractLanguage | testLanguage | fileExtension | version |
| TypeScriptContract | Conga | TypeScript | JavaScript | js | 0.0.1 |
| JavaScriptContract | Conga | JavaScript | TypeScript | ts | 0.0.1 |
| JavaContract | Conga | Java | Java | java | 0.0.1 |
2 changes: 1 addition & 1 deletion cucumber/features/submit.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ Feature: Submit transaction
Examples:
| language | assetType | name | version |
| JavaScript | Conga | JavaScriptContract | 0.0.1 |
| TypeScript | Conga | TypeScriptContract | 0.0.1 |
| Java | Conga | JavaContract | 0.0.1 |
35 changes: 29 additions & 6 deletions cucumber/helpers/generatedTestsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,49 @@ export class GeneratedTestsHelper {
const packageJSONPath: string = path.join(contractDirectory, 'package.json');
this.userInputUtilHelper.findFilesStub.resolves([vscode.Uri.file(packageJSONPath)]);

if (language === 'Java') {
this.userInputUtilHelper.showConfirmationWarningMessageStub.resolves('Now');
this.userInputUtilHelper.showConfirmationWarningMessageStub.withArgs(`The last step might overwrite ${packageJSONPath.replace(/package.json$/, 'build.gradle')}. Do you wish to continue?`).resolves('yes');
}
await vscode.commands.executeCommand(ExtensionCommands.TEST_SMART_CONTRACT);
}

public async runSmartContractTests(name: string, testLanguage: string, contractAssetType: string): Promise<string> {
public async runSmartContractTests(name: string, testLanguage: string, contractAssetType: string): Promise<boolean> {
const contractDirectory: string = this.smartContractHelper.getContractDirectory(name, testLanguage);
let fileExtension: string;
if (testLanguage === 'JavaScript') {
fileExtension = 'js';
} else if (testLanguage === 'TypeScript') {
fileExtension = 'ts';
} else if (testLanguage === 'Java') {
fileExtension = 'java';
} else {
// If we get here then we're running a language not supported for test files
return;
}
let testCommand: string = `node_modules/.bin/mocha ${path.join(contractDirectory, 'functionalTests', contractAssetType)}Contract-${name}@0.0.1.test.${fileExtension} --grep="create${contractAssetType}"`;
if (testLanguage === 'TypeScript') {
testCommand += ` -r ts-node/register`;

let testResult: string;
let success: boolean = false;
let testCommand: string;
if (testLanguage === 'Java') {
const capsContractName: string = name[0].toUpperCase() + name.slice(1);
const capsAssetType: string = contractAssetType[0].toUpperCase() + contractAssetType.slice(1);
testCommand = `./gradlew test --tests org.example.${capsAssetType}Contract${capsContractName}_001Test*.submitCreate${capsAssetType}Test`;
testResult = await CommandUtil.sendCommand(testCommand, contractDirectory);
if (testResult.includes('BUILD SUCCESSFUL')) {
success = true;
}
} else {
testCommand = `node_modules/.bin/mocha ${path.join(contractDirectory, 'functionalTests', contractAssetType)}Contract-${name}@0.0.1.test.${fileExtension} --grep="create${contractAssetType}"`;
if (testLanguage === 'TypeScript') {
testCommand += ` -r ts-node/register`;
}
testResult = await CommandUtil.sendCommand(testCommand, contractDirectory);
if (testResult.includes('1 passing')) {
success = true;
}
}
const testResult: string = await CommandUtil.sendCommand(testCommand, contractDirectory);
return testResult;
return success;
}

private getWorkspaceFolder(name: string, contractDirectory: string): vscode.WorkspaceFolder {
Expand Down
15 changes: 0 additions & 15 deletions cucumber/hlfv1/connection.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@
}
}
},
"channels": {
"mychannel": {
"orderers": [
"orderer.example.com"
],
"peers": {
"peer0.org1.example.com": {}
}
}
},
"organizations": {
"Org1": {
"mspid": "Org1MSP",
Expand All @@ -33,11 +23,6 @@
]
}
},
"orderers": {
"orderer.example.com": {
"url": "grpc://localhost:7050"
}
},
"peers": {
"peer0.org1.example.com": {
"url": "grpc://localhost:7051"
Expand Down
84 changes: 53 additions & 31 deletions cucumber/steps/gateway.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,41 +120,63 @@ module.exports = function(): any {
* Then
*/

this.Then("a functional test file with the filename '{string}' should exist and contain the correct contents", this.timeout, async (fileName: string) => {
const filePath: string = path.join(this.contractDirectory, 'functionalTests', fileName);
const exists: boolean = await fs.pathExists(filePath);
exists.should.equal(true);

const testFileContentsBuffer: Buffer = await fs.readFile(filePath);
const testFileContents: string = testFileContentsBuffer.toString();
// Did it open?
const textEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors;
const openFileNameArray: string[] = [];
for (const textEditor of textEditors) {
openFileNameArray.push(textEditor.document.fileName);
}
openFileNameArray.includes(filePath).should.be.true;
// Get the smart contract metadata
const connection: IFabricClientConnection = FabricConnectionManager.instance().getConnection();
const smartContractTransactionsMap: Map<string, string[]> = await MetadataUtil.getTransactionNames(connection, this.contractName, 'mychannel');
let smartContractTransactionsArray: string[];
for (const name of smartContractTransactionsMap.keys()) {
smartContractTransactionsArray = smartContractTransactionsMap.get(name);
}
// Check the test file was populated properly
testFileContents.includes(this.contractName).should.be.true;
testFileContents.startsWith('/*').should.be.true;
testFileContents.includes('gateway.connect').should.be.true;
testFileContents.includes('submitTransaction').should.be.true;
testFileContents.includes(smartContractTransactionsArray[0]).should.be.true;
testFileContents.includes(smartContractTransactionsArray[1]).should.be.true;
testFileContents.includes(smartContractTransactionsArray[2]).should.be.true;
this.Then('a functional test file with .{string} extension for the {string} contract {string} version {string} with assets {string} should exist and contain the correct contents',
this.timeout,
async (fileExtension: string, testLanguage: string, contractName: string, version: string, assetType: string) => {
let functionalDirPath: string;
let capsContractName: string;
let fileName: string;
let filePath: string;
if (testLanguage === 'Java') {
const capsAssetType: string = assetType[0].toUpperCase() + assetType.slice(1);
const versionPlain: string = version.replace(/\./g, '');
capsContractName = contractName[0].toUpperCase() + contractName.slice(1);
fileName = `${capsAssetType}Contract${capsContractName}_${versionPlain}Test.java`;
functionalDirPath = path.join(this.contractDirectory, 'src', 'test', 'java', 'org', 'example');
} else {
fileName = `${assetType}Contract-${contractName}@${version}.test.${fileExtension}`;
functionalDirPath = path.join(this.contractDirectory, 'functionalTests');
}
filePath = path.join(functionalDirPath, fileName);
const exists: boolean = await fs.pathExists(filePath);
exists.should.equal(true);

const testFileContentsBuffer: Buffer = await fs.readFile(filePath);
const testFileContents: string = testFileContentsBuffer.toString();
// Did it open?
const textEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors;
const openFileNameArray: string[] = [];
for (const textEditor of textEditors) {
openFileNameArray.push(textEditor.document.fileName);
}
openFileNameArray.includes(filePath).should.be.true;

// Get the smart contract metadata
const connection: IFabricClientConnection = FabricConnectionManager.instance().getConnection();
const smartContractTransactionsMap: Map<string, string[]> = await MetadataUtil.getTransactionNames(connection, this.contractName, 'mychannel');
let smartContractTransactionsArray: string[];
for (const name of smartContractTransactionsMap.keys()) {
smartContractTransactionsArray = smartContractTransactionsMap.get(name);
}
// Check the test file was populated properly
if (testLanguage === 'Java') {
testFileContents.includes(capsContractName).should.be.true;
testFileContents.includes('builder.connect').should.be.true;
} else {
testFileContents.includes(this.contractName).should.be.true;
testFileContents.includes('gateway.connect').should.be.true;
}
testFileContents.startsWith('/*').should.be.true;
testFileContents.includes('submitTransaction').should.be.true;
testFileContents.includes(smartContractTransactionsArray[0]).should.be.true;
testFileContents.includes(smartContractTransactionsArray[1]).should.be.true;
testFileContents.includes(smartContractTransactionsArray[2]).should.be.true;
});

this.Then('the tests should be runnable', this.timeout, async () => {
if (this.contractLanguage === 'TypeScript') {
if (this.contractLanguage === 'TypeScript' || this.contractLanguage === 'Java') {
const testRunResult: string = await this.generatedTestsHelper.runSmartContractTests(this.contractName, this.testLanguage, this.contractAssetType);
testRunResult.includes('1 passing').should.be.true;
testRunResult.should.equal(true);
}
});

Expand Down
Loading

0 comments on commit ddbdabd

Please sign in to comment.