Skip to content

Commit

Permalink
(fix) Ability to serialize concepts to JSON. Issue #4078 (hyperledger…
Browse files Browse the repository at this point in the history
…-archives#4080)

Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman authored May 31, 2018
1 parent 6720cf7 commit d7d685a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/composer-admin/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

"serializer-constructor-factorynull": "Factory cannot be null",
"serializer-constructor-modelmanagernull": "ModelManager cannot be null",
"serializer-tojson-notcobject": "Serializer.toJSON only accepts instances of Resource.",
"serializer-tojson-notcobject": "Serializer.toJSON only accepts Concept, Event, Asset, Participant or Transaction.",

"util-securitycheck-novalidcontext": "A valid SecurityContext must be specified.",

Expand Down
4 changes: 2 additions & 2 deletions packages/composer-common/lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const EnumDeclaration = require('./introspect/enumdeclaration');
const Globalize = require('./globalize');
const JSONGenerator = require('./serializer/jsongenerator');
const JSONPopulator = require('./serializer/jsonpopulator');
const Resource = require('./model/resource');
const Typed = require('./model/typed');
const ResourceValidator = require('./serializer/resourcevalidator');
const TransactionDeclaration = require('./introspect/transactiondeclaration');
const TypedStack = require('./serializer/typedstack');
Expand Down Expand Up @@ -91,7 +91,7 @@ class Serializer {
*/
toJSON(resource, options) {
// correct instance type
if(!(resource instanceof Resource)) {
if(!(resource instanceof Typed)) {
throw new Error(Globalize.formatMessage('serializer-tojson-notcobject'));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/composer-common/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

"serializer-constructor-factorynull": "Factory cannot be null",
"serializer-constructor-modelmanagernull": "ModelManager cannot be null",
"serializer-tojson-notcobject": "Serializer.toJSON only accepts instances of Resource.",
"serializer-tojson-notcobject": "Serializer.toJSON only accepts Concept, Event, Asset, Participant or Transaction.",

"util-securitycheck-novalidcontext": "A valid SecurityContext must be specified.",

Expand Down
4 changes: 2 additions & 2 deletions packages/composer-common/test/composer/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,13 @@ describe('Globalization', function() {

it('check message in toJSON()', function() {
let formatter = Globalize.messageFormatter('serializer-tojson-notcobject');
formatter().should.equal('Serializer.toJSON only accepts instances of Resource.');
formatter().should.equal('Serializer.toJSON only accepts Concept, Event, Asset, Participant or Transaction.');

const modelManager = new ModelManager();
expect(function() {
let serializer = new Serializer(true, modelManager);
serializer.toJSON({});
}).to.throw(Error, 'Serializer.toJSON only accepts instances of Resource.');
}).to.throw(Error, 'Serializer.toJSON only accepts Concept, Event, Asset, Participant or Transaction.');
});
});

Expand Down
31 changes: 30 additions & 1 deletion packages/composer-common/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Factory = require('../lib/factory');
const ModelManager = require('../lib/modelmanager');
const Relationship = require('../lib/model/relationship');
const Resource = require('../lib/model/resource');
const Concept = require('../lib/model/concept');
const Serializer = require('../lib/serializer');
const TypeNotFoundException = require('../lib/typenotfoundexception');

Expand Down Expand Up @@ -55,6 +56,11 @@ describe('Serializer', () => {
o String newValue
}
concept Address {
o String city
o String country
}
event SampleEvent{
--> SampleAsset asset
o String newValue
Expand Down Expand Up @@ -90,7 +96,7 @@ describe('Serializer', () => {
it('should throw if resource not a Resource', () => {
(() => {
serializer.toJSON([{}]);
}).should.throw(/only accepts instances of Resource/);
}).should.throw(/only accepts/);
});

it('should throw if the class declaration cannot be found', () => {
Expand Down Expand Up @@ -173,6 +179,17 @@ describe('Serializer', () => {
}).should.throw(/missing required field/);
});

it('should serialize a concept', () => {
let address = factory.newConcept('org.acme.sample', 'Address');
address.city = 'Winchester';
address.country = 'UK';
const json = serializer.toJSON(address);
json.should.deep.equal({
$class: 'org.acme.sample.Address',
country: 'UK',
city: 'Winchester'
});
});
});

describe('#fromJSON', () => {
Expand Down Expand Up @@ -235,6 +252,18 @@ describe('Serializer', () => {
resource.newValue.should.equal('the value');
});

it('should deserialize a valid concept', () => {
let json = {
$class: 'org.acme.sample.Address',
city: 'Winchester',
country: 'UK'
};
let resource = serializer.fromJSON(json);
resource.should.be.an.instanceOf(Concept);
resource.city.should.equal('Winchester');
resource.country.should.equal('UK');
});

it('should throw validation errors if the validate flag is not specified', () => {
let json = {
$class: 'org.acme.sample.SampleAsset',
Expand Down

0 comments on commit d7d685a

Please sign in to comment.