Skip to content

Commit

Permalink
fix: implement validation for both element templates versions
Browse files Browse the repository at this point in the history
test: verify features across both element template versions
  • Loading branch information
nikku committed Dec 9, 2024
1 parent 1852016 commit 07b580e
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 39 deletions.
44 changes: 35 additions & 9 deletions src/cloud-element-templates/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import {

import semverCompare from 'semver-compare';

import {
validRange as isSemverRangeValid
} from 'semver';

import {
validateZeebe as validateAgainstSchema,
getZeebeSchemaPackage as getTemplateSchemaPackage,
getZeebeSchemaVersion as getTemplateSchemaVersion
} from '@bpmn-io/element-templates-validator';
import { forEach } from 'min-dash';

const SUPPORTED_SCHEMA_VERSION = getTemplateSchemaVersion();
const SUPPORTED_SCHEMA_PACKAGE = getTemplateSchemaPackage();
Expand All @@ -31,8 +36,6 @@ export class Validator extends BaseValidator {
* @return {Error} validation error, if any
*/
_validateTemplate(template) {
let err;

const id = template.id,
version = template.version || '_',
schema = template.$schema,
Expand Down Expand Up @@ -78,25 +81,48 @@ export class Validator extends BaseValidator {
}

// (5) JSON schema compliance
const validationResult = validateAgainstSchema(template);
const schemaValidationResult = validateAgainstSchema(template);

const {
errors,
errors: schemaErrors,
valid
} = validationResult;
} = schemaValidationResult;

if (!valid) {
err = new Error('invalid template');

filteredSchemaErrors(errors).forEach((error) => {
filteredSchemaErrors(schemaErrors).forEach((error) => {
this._logError(error.message, template);
});

return new Error('invalid template');
}

return err;
// (6) engines validation
const enginesError = this._validateEngines(template);

if (enginesError) {
return enginesError;
}

return null;
}

isSchemaValid(schema) {
return schema && schema.includes(SUPPORTED_SCHEMA_PACKAGE);
}

_validateEngines(template) {

let err;

forEach(template.engines, (rangeStr, engine) => {

if (!isSemverRangeValid(rangeStr)) {
err = this._logError(new Error(
`Engine <${engine}> specifies invalid semver range <${rangeStr}>`
), template);
}
});

return err;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"id": "example.engines.test.multiple",
"name": "<engines> Test - Multiple",
"description": "does not match <desktopModeler>, if explicit <desktopModeler>=1> is provided",
"description": "does not match if { desktopModeler: >=1 } is provided",
"version": 2,
"engines": {
"camunda": "^8.6",
Expand All @@ -17,7 +17,7 @@
{
"id": "example.engines.test.multiple",
"name": "<engines> Test - Multiple",
"description": "matches when compatible <camunda> and/or <webModeler> run-time is indicated, or properties are not provided",
"description": "matches if { camunda: ^8.6, webModeler: ^4.1 } engine is indicated, or properties are not provided",
"version": 1,
"engines": {
"camunda": "^8.6",
Expand All @@ -32,7 +32,7 @@
{
"id": "example.engines.test.basic",
"name": "<engines> Test - Basic",
"description": "matches when compatible <camunda> run-time is indicated, or property is not provided",
"description": "matches if { camunda: ^8.6 }, or if no <camunda> engine is provided",
"version": 3,
"engines": {
"camunda": "^8.6"
Expand All @@ -45,7 +45,7 @@
{
"id": "example.engines.test.basic",
"name": "<engines> Test - Basic",
"description": "matches when compatible <camunda> run-time is indicated, or property is not provided",
"description": "matches if { camunda: ^8.5 }, or if no <camunda> engine is provided",
"version": 2,
"engines": {
"camunda": "^8.5"
Expand All @@ -72,7 +72,7 @@
"description": "specifies broken semver range",
"version": 1,
"engines": {
"camunda": "-foobar"
"camunda": "invalid-version"
},
"appliesTo": [
"bpmn:Task"
Expand Down
25 changes: 23 additions & 2 deletions test/spec/cloud-element-templates/ElementTemplates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import messageTemplates from './ElementTemplates.message-templates.json';
import enginesTemplates from './ElementTemplates.engines-templates.json';

import templates from './fixtures/simple';
import falsyVersionTemplate from './fixtures/falsy-version';
import complexTemplates from './fixtures/complex';
import integrationTemplates from './fixtures/integration';
import { findExtensions, findExtension } from 'src/cloud-element-templates/Helper';
Expand Down Expand Up @@ -728,6 +729,26 @@ describe('provider/cloud-element-templates - ElementTemplates', function() {

describe('set', function() {

it('should set templates', inject(function(elementTemplates) {

// when
elementTemplates.set(templates.slice(0, 3));

// then
expect(elementTemplates.getAll()).to.have.length(3);
}));


it('should not ignore version set to 0', inject(function(elementTemplates) {

// when
elementTemplates.set(falsyVersionTemplate);

// then
expect(elementTemplates.get(falsyVersionTemplate[0].id, 0)).to.exist;
}));


it('should emit <elementTemplates.changed> event', inject(function(elementTemplates, eventBus) {

// given
Expand All @@ -739,7 +760,7 @@ describe('provider/cloud-element-templates - ElementTemplates', function() {
elementTemplates.set(templates);

// then
expect(spy).to.have.been.called;
expect(spy).to.have.been.calledOnce;
}));

});
Expand All @@ -758,7 +779,7 @@ describe('provider/cloud-element-templates - ElementTemplates', function() {
elementTemplates.setEngines({});

// then
expect(spy).to.have.been.called;
expect(spy).to.have.been.calledOnce;
}));

});
Expand Down
37 changes: 37 additions & 0 deletions test/spec/cloud-element-templates/Validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,4 +586,41 @@ describe('provider/cloud-element-templates - Validator', function() {
});


describe('engines validation', function() {

it('should accept template with valid semver range', function() {

// given
const templates = new Validator(moddle);

const templateDescriptor = require('./fixtures/engines');

// when
templates.addAll(templateDescriptor);

// then
expect(errors(templates)).to.be.empty;

expect(valid(templates)).to.have.length(templateDescriptor.length);
});


it('should reject template with invalid semver range', function() {

// given
const templates = new Validator(moddle);

const templateDescriptor = require('./fixtures/engines-invalid');

// when
templates.addAll(templateDescriptor);

// then
expect(errors(templates)).to.contain('Engine <camunda> specifies invalid semver range <invalid-version>');

expect(valid(templates)).to.be.empty;
});

});

});
16 changes: 16 additions & 0 deletions test/spec/cloud-element-templates/fixtures/engines-invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "example.engines.test.broken",
"name": "<engines> Test - broken Semver range",
"description": "specifies broken semver range",
"version": 1,
"engines": {
"camunda": "invalid-version"
},
"appliesTo": [
"bpmn:Task"
],
"properties": []
}
]
23 changes: 4 additions & 19 deletions test/spec/cloud-element-templates/fixtures/engines.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "example.engines.test.multiple",
"name": "<engines> Test - Multiple",
"description": "does not match <desktopModeler>, if explicit <desktopModeler>=1> is provided",
"description": "does not match if { desktopModeler: >=1 } is provided",
"version": 2,
"engines": {
"camunda": "^8.6",
Expand All @@ -19,7 +19,7 @@
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "example.engines.test.multiple",
"name": "<engines> Test - Multiple",
"description": "matches when compatible <camunda> and/or <webModeler> run-time is indicated, or properties are not provided",
"description": "matches if { camunda: ^8.6, webModeler: ^4.1 } engine is indicated, or properties are not provided",
"version": 1,
"engines": {
"camunda": "^8.6",
Expand All @@ -35,7 +35,7 @@
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "example.engines.test.basic",
"name": "<engines> Test - Basic",
"description": "matches when compatible <camunda> run-time is indicated, or property is not provided",
"description": "matches if { camunda: ^8.6 }, or if no <camunda> engine is provided",
"version": 3,
"engines": {
"camunda": "^8.6"
Expand All @@ -49,7 +49,7 @@
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "example.engines.test.basic",
"name": "<engines> Test - Basic",
"description": "matches when compatible <camunda> run-time is indicated, or property is not provided",
"description": "matches if { camunda: ^8.5 }, or if no <camunda> engine is provided",
"version": 2,
"engines": {
"camunda": "^8.5"
Expand All @@ -71,21 +71,6 @@
"properties": []
},

{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "example.engines.test.broken",
"name": "<engines> Test - broken Semver range",
"description": "specifies broken semver range",
"version": 1,
"engines": {
"camunda": "-foobar"
},
"appliesTo": [
"bpmn:Task"
],
"properties": []
},

{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "example.engines.test.incompatible",
Expand Down
10 changes: 10 additions & 0 deletions test/spec/cloud-element-templates/fixtures/falsy-version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "foo",
"name":"Foo 1",
"version": 0,
"appliesTo": [ "bpmn:Task" ],
"properties": []
}
]
34 changes: 34 additions & 0 deletions test/spec/element-templates/ElementTemplates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,40 @@ describe('provider/element-templates - ElementTemplates', function() {
expect(elementTemplates.get(falsyVersionTemplate[0].id, 0)).to.exist;
}));


it('should emit <elementTemplates.changed> event', inject(function(elementTemplates, eventBus) {

// given
const spy = sinon.spy();

eventBus.on('elementTemplates.changed', spy);

// when
elementTemplates.set(templates);

// then
expect(spy).to.have.been.calledOnce;
}));

});


describe('setEngines', function() {

it('should emit event', inject(function(elementTemplates, eventBus) {

// given
const spy = sinon.spy();

eventBus.on('elementTemplates.engines.changed', spy);

// when
elementTemplates.setEngines({});

// then
expect(spy).to.have.been.calledOnce;
}));

});


Expand Down
1 change: 1 addition & 0 deletions test/spec/element-templates/Validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ describe('provider/element-templates - Validator', function() {

});


describe('engines validation', function() {

it('should accept template with valid semver range', function() {
Expand Down
1 change: 0 additions & 1 deletion test/spec/element-templates/fixtures/engines-invalid.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"id": "example.engines.test.basic",
"name": "<engines> Test - Basic",
"description": "basic template with invalid engines",
Expand Down
Loading

0 comments on commit 07b580e

Please sign in to comment.