Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Allow generation of Identifier field in Playground if no regex is req…
Browse files Browse the repository at this point in the history
…uired

As discussed with Simon, removing the code that adds the identifier field name to the
auto generated data for relationships in Asset

Signed-off-by: Sam Smith <smithsj@uk.ibm.com>
  • Loading branch information
samjsmith committed Nov 1, 2017
1 parent f49b0a2 commit 1b976a2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
7 changes: 3 additions & 4 deletions packages/composer-common/lib/serializer/instancegenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,9 @@ class InstanceGenerator {
* @return {String} an ID.
*/
generateRandomId(classDeclaration) {
const prefix = classDeclaration.getIdentifierFieldName();
let index = Math.round(Math.random() * 9999).toString();
index = leftPad(index, 4, '0');
return `${prefix}:${index}`;
let id = Math.round(Math.random() * 9999).toString();
id = leftPad(id, 4, '0');
return id;
}

}
Expand Down
8 changes: 4 additions & 4 deletions packages/composer-common/test/serializer/instancegenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('InstanceGenerator', () => {
o String assetId
--> MyAsset theValue
}`);
resource.theValue.getIdentifier().should.match(/^assetId:\d{4}$/);
resource.theValue.getIdentifier().should.match(/^\d{4}$/);
});

it('should generate a default value for a relationship array property', () => {
Expand All @@ -280,7 +280,7 @@ describe('InstanceGenerator', () => {
--> MyAsset[] theValues
}`);
resource.theValues.should.be.a('Array').and.have.lengthOf(1);
resource.theValues[0].getIdentifier().should.match(/^assetId:\d{4}$/);
resource.theValues[0].getIdentifier().should.match(/^\d{4}$/);
});

it('should generate a default value for a resource property', () => {
Expand All @@ -293,7 +293,7 @@ describe('InstanceGenerator', () => {
o String assetId
o MyInnerAsset theValue
}`);
resource.theValue.getIdentifier().should.match(/^innerAssetId:\d{4}$/);
resource.theValue.getIdentifier().should.match(/^\d{4}$/);
resource.theValue.theValue.should.be.a('string');
});

Expand All @@ -308,7 +308,7 @@ describe('InstanceGenerator', () => {
o MyInnerAsset[] theValues
}`);
resource.theValues.should.be.a('Array').and.have.lengthOf(1);
resource.theValues[0].getIdentifier().should.match(/^innerAssetId:\d{4}$/);
resource.theValues[0].getIdentifier().should.match(/^\d{4}$/);
resource.theValues[0].theValue.should.be.a('string');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ClassDeclaration,
Factory,
ModelFile

} from 'composer-common';

import { BusinessNetworkConnection, AssetRegistry } from 'composer-client';
Expand Down Expand Up @@ -174,19 +175,26 @@ describe('ResourceComponent', () => {
describe('#generateResource', () => {
let mockClassDeclaration;
let mockModelFile;
let mockField;

beforeEach(() => {
mockField = {
getValidator: sinon.stub ().returns(null)
};

component['updateExistingJSON'] = sandbox.stub();
mockModelFile = sinon.createStubInstance(ModelFile);
mockModelFile.getName.returns('model.cto');
mockClassDeclaration = sinon.createStubInstance(ClassDeclaration);
mockClassDeclaration.getModelFile.returns(mockModelFile);
mockClassDeclaration.getName.returns('class.declaration');
mockClassDeclaration.getIdentifierFieldName.returns('resourceId');

mockClassDeclaration.getOwnProperty.returns(mockField);
});

it('should generate a valid resource with undefined', () => {

it('should generate a valid resource with an empty ID', () => {
mockField.getValidator.returns('/regex/');
mockClassDeclaration.getOwnProperty.returns(mockField);
mockSerializer.toJSON.returns({$class: 'com.org'});
mockSerializer.fromJSON.returns(mockResource);
mockResource.validate = sandbox.stub();
Expand Down Expand Up @@ -289,6 +297,37 @@ describe('ResourceComponent', () => {

});

it('should generate a valid resource with a random 4-digit ID', () => {
mockSerializer.toJSON.returns({$class: 'com.org'});
mockSerializer.fromJSON.returns(mockResource);
mockResource.validate = sandbox.stub();
component['resourceDeclaration'] = mockClassDeclaration;

// should start clean
should.not.exist(component['definitionError']);

// run method
component['generateResource']();

// should not result in definitionError
should.not.exist(component['definitionError']);

// resourceDefinition should be set as per serializer.toJSON output
component['resourceDefinition'].should.equal('{\n "$class": "com.org"\n}');

// We use the following internal calls
mockFactory.newResource.should.be.calledWith(undefined,
'class.declaration',
sinon.match(/[0-9]{4}/),
{
generate: 'empty',
includeOptionalFields: false,
disableValidation: true,
allowEmptyId: true
});
component.onDefinitionChanged.should.be.calledOn;
});

it('should set definitionError on serializer fail', () => {
component['resourceDeclaration'] = mockClassDeclaration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
ClassDeclaration,
AssetDeclaration,
ParticipantDeclaration,
TransactionDeclaration
TransactionDeclaration,
Field
} from 'composer-common';
import leftPad = require('left-pad');

Expand Down Expand Up @@ -103,15 +104,29 @@ export class ResourceComponent implements OnInit {
return (this.resource ? true : false);
}

/**
* Returns true if the Identifying field of the Class that is being created has
* a validator associated with it ie. its ID field must conform to a regex
*/
private idFieldHasRegex() {
// a non-null validator on an identifying field returns true
let idf: Field = this.resourceDeclaration.getOwnProperty(this.resourceDeclaration.getIdentifierFieldName());
return idf.getValidator() ? true : false;
}

/**
* Generate the json description of a resource
*/
private generateResource(withSampleData ?: boolean): void {
let businessNetworkDefinition = this.clientService.getBusinessNetwork();
let factory = businessNetworkDefinition.getFactory();
let idx = Math.round(Math.random() * 9999).toString();
idx = leftPad(idx, 4, '0');

let id = '';
if (!this.idFieldHasRegex()) {
let idx = Math.round(Math.random() * 9999).toString();
id = leftPad(idx, 4, '0');
}

try {
const generateParameters = {
generate: withSampleData ? 'sample' : 'empty',
Expand Down

0 comments on commit 1b976a2

Please sign in to comment.