Skip to content

Commit

Permalink
Tests updated to get the code coverage; and defects fixed that it fou…
Browse files Browse the repository at this point in the history
  • Loading branch information
mbwhite authored and Simon Stone committed Jul 6, 2017
1 parent 8424d76 commit 9a0351f
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/composer-common/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",

Expand Down
13 changes: 13 additions & 0 deletions packages/composer-common/test/model/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});

});
});
106 changes: 106 additions & 0 deletions packages/composer-common/test/model/validatedconcept.js
Original file line number Diff line number Diff line change
@@ -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');
});

});

});

0 comments on commit 9a0351f

Please sign in to comment.