From d0d7a632f9e52c102d107756901626e2f49e724c Mon Sep 17 00:00:00 2001 From: Matthew B White Date: Thu, 6 Jul 2017 12:29:53 +0100 Subject: [PATCH] Tests updated to get the code coverage; and defects fixed that it found --- packages/composer-common/messages/en.json | 2 + .../test/model/relationship.js | 13 +++ .../test/model/validatedconcept.js | 106 ++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 packages/composer-common/test/model/validatedconcept.js diff --git a/packages/composer-common/messages/en.json b/packages/composer-common/messages/en.json index 534800e1e7..7046f0fbff 100644 --- a/packages/composer-common/messages/en.json +++ b/packages/composer-common/messages/en.json @@ -39,6 +39,8 @@ "factory-newinstance-missingidentifier": "Missing identifier for Type {type} in namespace {namespace}", "factory-newinstance-invalididentifier": "Invalid or missing identifier for Type {type} in namespace {namespace}", "factory-newinstance-abstracttype": "Cannot instantiate Abstract Type {type} in namespace {namespace}", + "factory-newrelationship-notregisteredwithmm" : "Cannot create relationship as namespace {namespace} is not known", + "factory-newinstance-typenotdeclaredinns" : "Cannot instantiate Type {type} in namespace {namespace}", "instancegenerator-newinstance-noconcreteclass": "No concrete extending type for {type}", diff --git a/packages/composer-common/test/model/relationship.js b/packages/composer-common/test/model/relationship.js index 8bbaa5ad19..b2144fcf1e 100644 --- a/packages/composer-common/test/model/relationship.js +++ b/packages/composer-common/test/model/relationship.js @@ -124,5 +124,18 @@ describe('Relationship', function () { rel.getType().should.equal('Person'); rel.getIdentifier().should.equal('123'); }); + + it('check invalid name space gets error', function() { + (function () { + Relationship.fromURI(modelManager, '123', 'org.acme.empty', 'Person' ); + }).should.throw(/Cannot create relationship as namespace org.acme.empty is not known/); + }); + + it('check that relationships can be created from a URI', function() { + (function () { + Relationship.fromURI(modelManager, 'resource:org.acme.l1.Unkown#123' ); + }).should.throw(/Cannot instantiate Type Unkown in namespace org.acme.l1/); + }); + }); }); diff --git a/packages/composer-common/test/model/validatedconcept.js b/packages/composer-common/test/model/validatedconcept.js new file mode 100644 index 0000000000..4fb6715100 --- /dev/null +++ b/packages/composer-common/test/model/validatedconcept.js @@ -0,0 +1,106 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const ModelManager = require('../../lib/modelmanager'); +const ValidatedConcept = require('../../lib/model/validatedconcept'); +const ResourceValidator = require('../../lib/serializer/resourcevalidator'); +const sinon = require('sinon'); +const chai = require('chai'); +chai.should(); +chai.use(require('chai-things')); + +describe('Validatedoncept', function () { + + const levelOneModel = `namespace org.acme.l1 + concept Person { + o String name + o String[] arrayName + } + asset Car identified by vin { + o String vin + o Person owner + } + `; + + let modelManager = null; + let mockResourceValidator; + + before(function () { + modelManager = new ModelManager(); + }); + + beforeEach(function () { + modelManager.addModelFile(levelOneModel); + mockResourceValidator = sinon.createStubInstance(ResourceValidator); + mockResourceValidator.visit.returns(null); + + }); + + afterEach(function () { + modelManager.clearModelFiles(); + }); + + describe('#getClassDeclaration', function() { + it('should throw with no ModelFile', function () { + const resource = new ValidatedConcept(modelManager, 'org.acme.l1', 'Person' ,mockResourceValidator); + const stub = sinon.stub(modelManager, 'getModelFile', function(){return null;}); + (function () { + resource.getClassDeclaration(); + }).should.throw(/No model for namespace org.acme.l1 is registered with the ModelManager/); + stub.restore(); + }); + }); + + describe('#setPropertyValue', () => { + it (' should accept valid property - value', function (){ + const resource = new ValidatedConcept(modelManager, 'org.acme.l1', 'Person' ,mockResourceValidator); + resource.setPropertyValue('name','Fred Bloggs'); + }); + it (' should throw error for invalid property name', function (){ + const resource = new ValidatedConcept(modelManager, 'org.acme.l1', 'Person' ,mockResourceValidator); + ( () => { + resource.setPropertyValue('namenamename','Fred Bloggs'); + }).should.throw(/Trying to set field namenamename which is not declared in the model/); + }); + it (' should throw error for array', function (){ + const resource = new ValidatedConcept(modelManager, 'org.acme.l1', 'Person' ,mockResourceValidator); + ( () => { + resource.addArrayValue('name',['Fred','Bloggs']); + }).should.throw(/Trying to add array item name which is not declared as an array in the model/); + }); + it (' correct path for adding an array', function (){ + const resource = new ValidatedConcept(modelManager, 'org.acme.l1', 'Person' ,mockResourceValidator); + resource.addArrayValue('arrayName',['Fred','Bloggs']); + }); + it (' should throw error for invalid property name', function (){ + const resource = new ValidatedConcept(modelManager, 'org.acme.l1', 'Person' ,mockResourceValidator); + (()=>{ + resource.addArrayValue('invalid','Fred'); + }).should.throw(/Trying to set field invalid which is not declared in the model/); + }); + it (' validate', function (){ + const resource = new ValidatedConcept(modelManager, 'org.acme.l1', 'Person' ,mockResourceValidator); + resource.validate(); + }); + it (' add two elements separately to an array property', function (){ + const resource = new ValidatedConcept(modelManager, 'org.acme.l1', 'Person' ,mockResourceValidator); + resource.addArrayValue('arrayName','Fred'); + resource.addArrayValue('arrayName','Bloggs'); + }); + + }); + +});