Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions packages/composer-common/lib/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,13 @@ class ModelManager {
throw new Error('Cannot add a model file with the reserved system namspace: ' + m.getNamespace() );
}

m.validate();
this.modelFiles[m.getNamespace()] = m;
if (!this.modelFiles[m.getNamespace()]) {
m.validate();
this.modelFiles[m.getNamespace()] = m;
} else {
throw new Error('namespace already exists');
}

return m;
}

Expand Down Expand Up @@ -221,14 +226,26 @@ class ModelManager {
if (m.isSystemModelFile()){
throw new Error('System namespace can not be updated');
}
this.modelFiles[m.getNamespace()] = m;
newModelFiles.push(m);

if (!this.modelFiles[m.getNamespace()]) {
this.modelFiles[m.getNamespace()] = m;
newModelFiles.push(m);
}
else {
throw new Error('namespace already exists');
}
} else {
if (modelFile.isSystemModelFile()){
throw new Error('System namespace can not be updated');
}
this.modelFiles[modelFile.getNamespace()] = modelFile;
newModelFiles.push(modelFile);

if (!this.modelFiles[modelFile.getNamespace()]) {
this.modelFiles[modelFile.getNamespace()] = modelFile;
newModelFiles.push(modelFile);
}
else {
throw new Error('namespace already exists');
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/composer-common/test/codegen/loopbackvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ describe('LoopbackVisitor', () => {
});

it('should use the model file of the referencing type to resolve enumeration types', () => {
modelManager.addModelFile(`
modelManager.updateModelFile(`
namespace org.acme.base
enum Enum {
o SOME_VALUE
Expand Down Expand Up @@ -1021,7 +1021,7 @@ describe('LoopbackVisitor', () => {
});

it('should use the model file of the referencing type to resolve other types', () => {
modelManager.addModelFile(`
modelManager.updateModelFile(`
namespace org.acme.base
asset MyInlineAsset identified by assetId {
o String assetId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('ClassDeclaration', () => {
const modelFile = new ModelFile(modelManager, modelDefinitions);
modelFiles.push(modelFile);
}
modelManager.addModelFiles(modelFiles, modelFileNames);
//modelManager.addModelFiles(modelFiles, modelFileNames);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove dead code

return modelFiles;
};

Expand Down
38 changes: 37 additions & 1 deletion packages/composer-common/test/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ describe('ModelManager', () => {
}).should.throw();
});

it('should return error for duplicate namespaces for a string', () => {
modelManager.addModelFile(modelBase);
let mf1 = sinon.createStubInstance(ModelFile);
mf1.getNamespace.returns('org.acme.base');
(() => {
modelManager.addModelFile(modelBase);
}).should.throw(/namespace already exists/);
});

it('should return error for duplicate namespaces from an object', () => {
modelManager.addModelFile(modelBase);
let mf1 = sinon.createStubInstance(ModelFile);
mf1.getNamespace.returns('org.acme.base');
(() => {
modelManager.addModelFile(mf1);
}).should.throw(/namespace already exists/);
});

});

describe('#addModelFiles', () => {
Expand Down Expand Up @@ -157,7 +175,7 @@ describe('ModelManager', () => {
it('should add to existing model files from strings', () => {
modelManager.addModelFile(modelBase);
modelManager.getModelFile('org.acme.base').getNamespace().should.equal('org.acme.base');
modelManager.addModelFiles([modelBase, concertoModel, farm2fork]);
modelManager.addModelFiles([concertoModel, farm2fork]);
modelManager.getModelFile('org.acme.base').getNamespace().should.equal('org.acme.base');
modelManager.getModelFile('org.acme').getNamespace().should.equal('org.acme');
});
Expand Down Expand Up @@ -215,6 +233,24 @@ describe('ModelManager', () => {
modelManager.addModelFiles([mockSystemModelFile]);
}).should.throw();
});

it('should return an error for duplicate namespace from strings', () => {
(() => {
modelManager.addModelFiles([concertoModel, modelBase, farm2fork, modelBase]);
}).should.throw(/namespace already exists/);
});

it('should return an error for duplicate namespace from objects', () => {
let mf1 = sinon.createStubInstance(ModelFile);
mf1.getNamespace.returns('org.doge');
let mf2 = sinon.createStubInstance(ModelFile);
mf2.getNamespace.returns('org.doge.base');
modelManager.addModelFiles([mf1,mf2]);
(() => {
modelManager.addModelFiles([mf1]);
}).should.throw(/namespace already exists/);
});

});

describe('#updateModelFile', () => {
Expand Down