diff options
Diffstat (limited to 'workflow/workflow-bdd/stepDefinitions')
9 files changed, 1065 insertions, 0 deletions
diff --git a/workflow/workflow-bdd/stepDefinitions/Collaboration_Steps.js b/workflow/workflow-bdd/stepDefinitions/Collaboration_Steps.js new file mode 100644 index 00000000..c4de7583 --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/Collaboration_Steps.js @@ -0,0 +1,113 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ +const {Then, When, Given} = require('cucumber'); +const assert = require('assert'); +const util = require('./Utils.js'); + +/** + * @module Collaboration + * @description Adds the user with the given user ID as a contributor on the item + * @exampleFile Example_Collaboration.feature + * @step I want to add user {string} as a contributor to this Item + **/ +When('I want to add user {string} as a contributor to this Item', function(string) { + let path = '/items/' + this.context.item.id + '/permissions/Contributor'; + let inputData = {removedUsersIds:[],addedUsersIds:[string]}; + return util.request(this.context, 'PUT', path, inputData); +}); + +/** + * @module Collaboration + * @description Adds the user with the given user ID as a contributor on the item + * @exampleFile Example_Collaboration.feature + * @step I want to remove user {string} as a contributor to this Item + **/ +When('I want to remove user {string} as a contributor to this Item', function(string) { + let path = '/items/' + this.context.item.id + '/permissions/Contributor'; + let inputData = {removedUsersIds:[string],addedUsersIds:[]}; + return util.request(this.context, 'PUT', path, inputData); +}); + +/** + * @module Collaboration + * @description Changes the owner to the given User ID + * @exampleFile Example_Collaboration.feature + * @step I want to change the owner to user {string} on this Item + **/ +When('I want to change the owner to user {string} on this Item', function(string) { + let path = '/items/' + this.context.item.id + '/permissions/Owner'; + let inputData = {removedUsersIds:[],addedUsersIds:[string]}; + return util.request(this.context, 'PUT', path, inputData); +}); + + +/** + * @module Collaboration + * @description Checks the role for a user on the item by User id and Role can be either: Contributor/Owner + * @exampleFile Example_Collaboration.feature + * @step I want check user {string} has role {string} on this Item + **/ +When('I want to check user {string} has role {string} on this Item', function(string, string2) { + let path = '/items/' + this.context.item.id + '/permissions'; + return util.request(this.context, 'GET', path).then(results => { + for (i in results.data.results) { + if (results.data.results[i].userId === string) { + assert.equal(string2.toLowerCase(), results.data.results[i].permission.toLowerCase()); + return; + } + } + assert.fail('User not found'); + }); +}); + +/** + * @module Collaboration + * @description Checks the user wth this Id has no permissions on this item + * @exampleFile Example_Collaboration.feature + * @step I want check user {string} has rno permissions on this Item + **/ +When('I want to check user {string} has no permissions on this Item', function(string) { + let path = '/items/' + this.context.item.id + '/permissions'; + return util.request(this.context, 'GET', path).then(results => { + for (i in results.data.results) { + if (results.data.results[i].userId === string) { + assert.fail('Found', null, 'User should not have permissions'); + return; + } + } + }); +}); + +/** + * @module Collaboration + * @description Gets the permissions for the Item + * @exampleFile Example_Collaboration.feature + * @step I want to get the permissions for this Item + **/ +When('I want to get the permissions for this Item', function() { + let path = '/items/' + this.context.item.id + '/permissions'; + return util.request(this.context, 'GET', path); +}); + +/** + * @module Collaboration + * @description Changes the user for the Rest calls + * @exampleFile Example_Collaboration.feature + * @step I want to set the user to {string} + **/ +When('I want to set the user to {string}', function(string) { + this.context.headers['onboarding'].USER_ID = string; +}); diff --git a/workflow/workflow-bdd/stepDefinitions/General_Steps.js b/workflow/workflow-bdd/stepDefinitions/General_Steps.js new file mode 100644 index 00000000..0550e415 --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/General_Steps.js @@ -0,0 +1,321 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ +const {Then, When, Given} = require('cucumber'); +const assert = require('assert'); +const _ = require('lodash'); +const normalizeNewline = require('normalize-newline'); +require('node-zip'); +YAML = require('yamljs'); +const fs = require('fs'); +const util = require('./Utils.js'); + +function getPath(path, context) { + let compiled = _.template(path); + return compiled(context); +} + +/** + * @module ContextData + * @description Use with "Given". Use ONLY for local testing when you know the value of the Item you want to use + * instead of creating a new one. + * @step Item {string} and version Id {string} + **/ +Given('Item {string} and version Id {string}', function (string, string2) { + this.context.item.id = string; + this.context.item.versionId = string2; +}); +/** + * @module ContextData + * @exampleFile Example_ResponseData_CheckAndManipulation.feature + * @description Response Data::<br> + * """<br> + * {jsonObject}<br> + * """<br> + * @step Use with "Given". Use ONLY for local testing, creates a response data object + **/ +Given('Response Data:', function (docString) { + this.context.responseData = JSON.parse(docString); +}); + +/** + * @module ContextData + * @description Copy a property from the response data to context Item data, example: item.componentId + * @step I want to save on the context for {string} property {string} with value {string} + **/ +Then('I want to save on the context for {string} property {string} with value {string}', function(string, string1, string2) { + assert.equal(_.includes(['Item'], string), true); + let val = _.get(this.context.responseData, string2); + _.set(this.context, string1, val); +}); +/** + * @module ContextData + * @description Copy a property from the response data to saved data on the context. Example: save newly generated IDs. Response data value can be from a path, xample: results[0].id + * @exampleFile Example_Rest_Calls.feature + * @step I want to save to property {string} from response data path {string} + **/ +Then('I want to copy to property {string} from response data path {string}', function(string, string2) { + let val = _.get(this.context.responseData, string2); + _.set(this.context, string, val); +}); +/** + * @module ContextData + * @description This will set the value of a saved property on the context + * @exampleFile Example_Rest_Calls.feature + * @step I want to set property {string} to value {string} + **/ +Then('I want to set property {string} to value {string}', function(string, string2) { + _.set(this.context, string, string2); +}); + +/** + * @module ResponseData + * @description Will check the output data for a property and a value. property can be a path (example: results[0].id) + * @exampleFile Example_ResponseData_CheckAndManipulation.feature + * @step I want to check property {string} for value {string} + **/ +Then('I want to check property {string} for value {string}', function(string, string2) { + assert.equal(_.get(this.context.responseData, string), string2); +}); +/** + * @module ResponseData + * @description Will check the output data for a property and a value. property can be a path + * (example: results[0].id). Supports comparison to a long String by allowing a line break + * @exampleFile VirtualMachineInterfaceValidationHeatResourceMissingProperties.feature + * @step I want to check property {string} for value {string} + **/ + +Then('I want to check property {string} for value:', function(string, docString) { + assert.equal(_.get(this.context.responseData, string), docString.trim()); +}); +/** + * @module ResponseData + * @description Will check the output data for a property and a integer. property can be a path (example: results[0].id) + * @exampleFile Example_ResponseData_CheckAndManipulation.feature + * @step I want to check property {string} for value {int} + **/ +Then('I want to check property {string} for value {int}', function(string, int) { + assert.equal(_.get(this.context.responseData, string), int); +}); +/** + * @module ResponseData + * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id) + * @exampleFile Example_ResponseData_CheckAndManipulation.feature + * @step I want to check property {string} to be "True/False" + **/ +Then('I want to check property {string} to be {word}', function(string, string2) { + assert.equal(_.get(this.context.responseData, string), string2.toLowerCase() == "true"); +}); +/** + * @module ResponseData + * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id) + * @exampleFile Example_ResponseData_CheckAndManipulation.feature + * @step I want to check property {string} to have length {int} + **/ +Then('I want to check property {string} to have length {int}', function(string, intLength) { + let arrayProp = _.get(this.context.responseData, string); + assert.equal(arrayProp.length, intLength); +}); +/** + * @module ResponseData + * @description Will check the output data for a property and make sure it exists + * @exampleFile Example_ResponseData_CheckAndManipulation.feature + * @step I want to check property {string} exists + **/ +Then('I want to check property {string} exists', function(string) { + assert.equal(_.has(this.context.responseData, string), true); +}); +/** + * @module ResponseData + * @description Will check the output data for a property and make sure it does not exist + * @exampleFile Example_ResponseData_CheckAndManipulation.feature + * @step I want to check property {string} does not exist + **/ +Then('I want to check property {string} does not exist', function(string) { + assert.equal(_.has(this.context.responseData, string), false); +}); + +/** +* @module ContextData +* @description Use during development to see what is on the context + * @exampleFile Example_ResponseData_CheckAndManipulation.feature +* @step I want to print context data +**/ +Then('I want to print the context data', function() { + console.log('------------ context ---------------'); + console.log(JSON.stringify(this.context, null, 2)); + console.log('--------------------------------------'); +}); +/** + * @module ContextData + * @description Set this in order to check that the following Rest call will not have response code 200 + * @exampleFile Example_Rest_Calls.feature + * @step I want the following to fail + **/ +Then('I want the following to fail', function() { + this.context.shouldFail = true; +}); + +/** + * @module ContextData + * @description Set this in order to check that the following Rest call will not have response code 200 + * @exampleFile Example_Rest_Calls.feature + * @step I want the following to fail + **/ +Then('I want the following to fail with response status code {int}', function(int) { + this.context.shouldFail = true; + this.context.responseStatusCode = int; +}); + +/** + * @module ContextData + * @description Set this in order to check that the following Rest call will have the error code on the return data + * @exampleFile Example_VSP.feature + * @step I want the following to fail with error code {string} + **/ +Then('I want the following to fail with error code {string}', function(string) { + this.context.shouldFail = true; + this.context.errorCode = string; +}); + + +/** + * @module ContextData + * @description Set this in order to check that the following Rest call will have the error message on the return data + * @exampleFile DeleteVLMCertified.feature + * @step I want the following to fail with error message {string} + **/ +Then('I want the following to fail with error message {string}', function(string) { + this.context.shouldFail = true; + let errorMessage = getPath(string, this.context); + this.context.errorMessage = errorMessage; +}); + +/** + * @module ZipData + * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison). + * @exampleFile Example_VSP.feature + * @step I want to compare the content of the entry {string} in the zip {string} with file {string} + **/ +Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) { + let zipFile = fs.readFileSync(string2, 'binary'); + let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); + let fileData = zip.files[string]._data; + let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'}); + assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData)); +}); + +/** + * @module ZipData + * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output + * @exampleFile Example_VSP.feature + * @step I want to load the yaml content of the entry {string} in the zip {string} to context + **/ +Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) { + let zipFile = fs.readFileSync(string2, 'binary'); + let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); + let fileData = zip.files[string]._data; + let nativeObject = YAML.parse(fileData); + this.context.responseData = nativeObject; + callback(); +}); + + +/** + * @module ZipData + * @description Loads the json from zip file onto the context responseData for running check son the output + * @exampleFile Example_VSP.feature + * @step I want to load the json content of the entry {string} in the zip {string} to context + **/ +When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) { + let zipFile = fs.readFileSync(string2, 'binary'); + let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); + let str = zip.files[string]._data; + this.context.responseData = JSON.parse(str); + callback(); +}); + +/** + * @module ResponseData + * @description Check that the result list doesn't contain an element with property x which has value + * equals to saved property y + * @exampleFile ListItemsFilters.feature + * @step I want to check that element in the response list with {string} equals to value of saved property {string} does not exist + **/ +Then('I want to check that element in the response list with {string} equals to value of saved property {string} does not exist', function (propertyPath, valueProperty) { + const results = this.context.responseData.results; + assert.equal(results.find(result => this.context[valueProperty] === _.get(result, propertyPath)), undefined); +}); + +/** + * @module ResponseData + * @description Check that the result list contains an element with property x which has value + * equals to saved property y + * @exampleFile ListItemsFilters.feature + * @step I want to check that element in the response list with {string} equals to value of saved property {string} exists + **/ +Then('I want to check that element in the response list with {string} equals to value of saved property {string} exists', function(propertyPath, valueProperty) { + const results = this.context.responseData.results; + assert.notEqual(results.find(result => this.context[valueProperty] === _.get(result, propertyPath)), undefined); +}); + + +Then('I want to check that property {string} in the response equals to value of saved property {string}', function(propertyPath, valueProperty) { + const results = this.context.responseData; + assert.equal(results[propertyPath],this.context[valueProperty]); +}); + +/** + * @module ResponseData + * @description Check that the itemId from context exits in result of responseData + * exampleFile ArchiveItem.feature + * step I want to check that item exits in response + **/ +Then('I want to check that item exits in response', function() { + + const id = this.context.item.id; + const results = this.context.responseData.results; + var testResult = false; + + for(var i=0; i< results.length; i++){ + if ( id == results[i].id){ + testResult = true; + } + } + + assert.equal(testResult,true); +}); + + +/** + * @module ResponseData + * @description Check that the itemId from context does NOT exits in result of responseData + * exampleFile ArchiveItem.feature + * step I want to check that item does not exits in response + **/ +Then('I want to check that item does not exits in response', function() { + + const id = this.context.item.id; + const results = this.context.responseData.results; + var testResult = false; + + for(var i=0; i< results.length; i++){ + if ( id == results[i].id){ + testResult = true; + } + } + + assert.equal(testResult,false); +});
\ No newline at end of file diff --git a/workflow/workflow-bdd/stepDefinitions/InputData_steps.js b/workflow/workflow-bdd/stepDefinitions/InputData_steps.js new file mode 100644 index 00000000..73695d0e --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/InputData_steps.js @@ -0,0 +1,83 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ +const {Then, When, Given} = require('cucumber'); +const assert = require('assert'); +const _ = require('lodash'); +const fs = require('fs'); +const util = require('./Utils.js'); + +/** + * @module InputData + * @description creates an ampty input data object + * @exampleFile Example_Rest_Calls.feature, Example_VLM.feature + * @step I want to create input data + **/ +When('I want to create input data', function () { + this.context.inputData = {}; +}); + +/** + * @module InputData + * @exampleFile Example_Heat.feature + * @description I want to set the input data to:<br> + * """<br> + * {jsonObject}<br> + * """<br> + * @step creates an input data element with the given json object + **/ +When('I want to set the input data to:', function (docString) { + this.context.inputData = JSON.parse(docString); +}); + +/** + * @module InputData + * @description creates an input data object from the json in the given file + * @exampleFile Example_Rest_Calls.feature + * @step I want to set the input data to file {string} + **/ +When('I want to set the input data to file {string}', function (string) { + this.context.inputData = util.getJSONFromFile(string); +}); + +/** + * @module InputData + * @description sets the property on the input data to the given value + * @exampleFile Example_Rest_Calls.feature, Example_VLM.feature + * @step I want to update the input property {string} with value {string} + **/ +Then('I want to update the input property {string} with value {string}', function(string, string2) { + _.set(this.context.inputData, string, string2); +}); + +/** + * @module InputData + * @description removes a property from the input data object + * @exampleFile Example_Rest_Calls.feature + * @step I want to remove {string} from the input data + **/ +Then('I want to remove {string} from the input data', function(string) { + delete this.context.inputData[string]; +}); + +/** + * @module InputData + * @description sets the input data property to a random value + * @exampleFile Example_Rest_Calls.feature + * @step I want to update the input property {string} with a random value + **/ +Then('I want to update the input property {string} with a random value', function(string) { + _.set(this.context.inputData, string, util.random()); +}); diff --git a/workflow/workflow-bdd/stepDefinitions/InterfaceOperationSteps.js b/workflow/workflow-bdd/stepDefinitions/InterfaceOperationSteps.js new file mode 100644 index 00000000..4c00debc --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/InterfaceOperationSteps.js @@ -0,0 +1,118 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ +const {Then, When} = require('cucumber'); +const assert = require('assert'); +const util = require('./Utils.js'); + + +When('I want to create a VF', function() { + let inputData = util.getJSONFromFile('resources/json/operation/createVF_From_Catalog.json'); + + inputData.name = util.random(); + inputData.tags[0] = util.random(); + + let path = '/catalog/resources'; + return util.request(this.context, 'POST', path, inputData, false, 'vf').then(result => { + this.context.item = {uniqueId : result.data.uniqueId, id : result.data.inputs[0].uniqueId, name : result.data.inputs[0].name}; + this.context.vf = {uniqueId : result.data.uniqueId, id : result.data.inputs[0].uniqueId, name : result.data.inputs[0].name}; +}); +}); + +function makeType() { + var text = ""; + var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + for (var i = 0; i < 5; i++) + text += possible.charAt(Math.floor(Math.random() * possible.length)); + + return text; +} + +When('I want to create an Operation', function() { + let inputData = util.getJSONFromFile('resources/json/operation/createOperation.json'); + //let path = '/catalog/resources/f3dc81bb-85e9-4dfd-bd1b-37f5dc5e5534/interfaceOperations'; + let path = '/catalog/resources/' + this.context.vf.uniqueId +'/interfaceOperations'; + + inputData.interfaceOperations.create.operationType = makeType(); + inputData.interfaceOperations.create.inputParams.listToscaDataDefinition[0].paramName = util.random(); + inputData.interfaceOperations.create.inputParams.listToscaDataDefinition[0].paramId = this.context.vf.id; + + return util.request(this.context, 'POST', path, inputData, false, 'vf').then(result => { + this.context.item = {uniqueId : result.data.uniqueId, operationType : result.data.operationType}; + this.context.operation = {uniqueId : result.data.uniqueId, operationType : result.data.operationType}; +}); +}); + + + +When('I want to list Operations', function () { + let path = '/catalog/resources/' + this.context.vf.uniqueId + '/filteredDataByParams?include=interfaces'; + return util.request(this.context, 'GET', path, null, false, 'vf').then((result)=> { +}); +}); + + +When('I want to get an Operation by Id', function () { + let path = '/catalog/resources/' + this.context.vf.uniqueId + '/interfaceOperations/' + this.context.operation.uniqueId; + return util.request(this.context, 'GET', path, null, false, 'vf').then((result)=> { + this.context.item ={uniqueId : result.data.uniqueId, operationType: result.data.operationType}; + this.context.operation = {uniqueId : result.data.uniqueId, operationType : result.data.operationType}; +}); +}); + + +When('I want to update an Operation', function () { + let inputData = util.getJSONFromFile('resources/json/operation/updateOperation.json'); + let path = '/catalog/resources/' + this.context.vf.uniqueId + '/interfaceOperations/'; + inputData.interfaceOperations.create.uniqueId = this.context.operation.uniqueId; + inputData.interfaceOperations.create.operationType = this.context.operation.operationType; + inputData.interfaceOperations.create.inputParams.listToscaDataDefinition[0].paramName = util.random(); + inputData.interfaceOperations.create.inputParams.listToscaDataDefinition[0].paramId = this.context.vf.id; + return util.request(this.context, 'PUT', path, inputData, false, 'vf').then((result)=> { + this.context.item ={uniqueId : result.data.uniqueId, operationType: result.data.operationType}; + this.context.operation = {uniqueId : result.data.uniqueId, operationType : result.data.operationType}; +}); +}); + + +When('I want to delete an Operation', function() { + let path = '/catalog/resources/' + this.context.vf.uniqueId + '/interfaceOperations/' + this.context.operation.uniqueId; + return util.request(this.context, 'DELETE', path, null, false, 'vf'); +}); + + +When('I want to checkin this VF', function () { + let path = '/catalog/resources/' + this.context.vf.uniqueId + '/lifecycleState/CHECKIN' ; + let inputData = {userRemarks: 'checkin'}; + return util.request(this.context, 'POST', path, inputData, false, 'vf').then((result)=> { + this.context.item ={uniqueId : result.data.uniqueId}; + this.context.vf = {uniqueId : result.data.uniqueId}; +}); +}); + + +Then('I want to submit this VF', function () { + let path = '/catalog/resources/' + this.context.vf.uniqueId + '/lifecycleState/certificationRequest' ; + let inputData = {userRemarks: 'submit'}; + return util.request(this.context, 'POST', path, inputData, false, 'vf').then((result)=> { + this.context.item ={uniqueId : result.data.uniqueId}; + this.context.vf = {uniqueId : result.data.uniqueId}; +}); +}); + + + + diff --git a/workflow/workflow-bdd/stepDefinitions/Item_steps.js b/workflow/workflow-bdd/stepDefinitions/Item_steps.js new file mode 100644 index 00000000..3ff7f20f --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/Item_steps.js @@ -0,0 +1,91 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ +const {Then, When} = require('cucumber'); +const assert = require('assert'); +const util = require('./Utils.js'); +/** + * @module Item + * @description uses item id and version id from context + * @exampleFile Example_VSP.feature, Example_VLM.feature + * @step I want to make sure this Item has status {string} + **/ +Then('I want to make sure this Item has status {string}', function (string) { + let path = '/items/' + this.context.item.id + '/versions'; + return util.request(this.context, 'GET', path).then(result => { + assert.equal(result.data.results[0].id, this.context.item.versionId); + assert.equal(result.data.results[0].status, string); + }); +}); +/** + * @module Item + * @description uses item id and version id from context + * @exampleFile Example_VSP.feature, Example_VLM.feature + * @step I want to commit this Item + **/ +Then('I want to commit this Item', function () { + let path = '/items/' + this.context.item.id + '/versions/' + this.context.item.versionId + '/actions'; + let inputData = {action: 'Commit', commitRequest: {message: '00Behave'}}; + return util.request(this.context, 'PUT', path, inputData); +}); +/** + * @module Item + * @description creates a new major version. item id and version id from context + * @exampleFile Example_VLM.feature + * @step I want to create a new version for this Item + **/ +Then('I want to create a new version for this Item', function () { + let path = '/items/' + this.context.item.id + '/versions/' + this.context.item.versionId; + let inputData = {description: 'Behave Version', creationMethod: 'major'}; + return util.request(this.context, 'POST', path, inputData).then(result => { + assert.equal(result.data.status, 'Draft'); + }); +}); +/** + * @module Item + * @description reverts to a revision with a given saved property. Should be set from the revision list first + * @exampleFile Example_VLM.feature + * @step I want to commit this Item + **/ +Then('I want to revert this Item to the revision with the value from saved property {string}', function (string) { + let path = '/items/' + this.context.item.id + '/versions/' + this.context.item.versionId + '/actions'; + let inputData = {action: 'Revert', revisionRequest: {revisionId: this.context[string]}}; + return util.request(this.context, 'PUT', path, inputData); +}); + + +/** + * @module Item + * @exampleFile ArchiveItem.feature + * @step I want to archive this item + **/ +Then('I want to archive this item', function() { + let path = '/items/' + this.context.item.id + '/actions' + let inputData = {action: 'ARCHIVE'}; + return util.request(this.context, 'PUT', path, inputData); +}); + + +/** + * @module Item + * @exampleFile ArchiveItem.feature + * @step I want to restore this item + **/ +Then('I want to restore this item', function() { + let path = '/items/' + this.context.item.id + '/actions' + let inputData = {action: 'RESTORE'}; + return util.request(this.context, 'PUT', path, inputData); +}); + diff --git a/workflow/workflow-bdd/stepDefinitions/REST_Steps.js b/workflow/workflow-bdd/stepDefinitions/REST_Steps.js new file mode 100644 index 00000000..c3844124 --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/REST_Steps.js @@ -0,0 +1,80 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ +const {When} = require('cucumber'); +const _ = require('lodash'); +const util = require('./Utils.js'); +_.templateSettings.interpolate = /{([\s\S]+?)}/g; + +function getPath(path, context) { + let compiled = _.template(path); + return compiled(context); +} +/** + * @module Rest_Calls + * @description makes a GET request to the given path (path is appended after the "onboarding-api/v1.0" prefix)<br> + * @exampleFile Example_Rest_Calls.feature + * @step I want to get path {string} + **/ +When('I want to get path {string}', function(string) { + let path = getPath(string, this.context); + return util.request(this.context, 'GET', path); +}); + +/** + * @module Rest_Calls + * @description makes a DELETE request to the given path and appends the saved property (path is appended after the "onboarding-api/v1.0" prefix)<br> + * @exampleFile Example_Rest_Calls.feature + * @step I want to delete for path {string} with the value from saved property {string} + **/ +When('I want to delete for path {string} with the value from saved property {string}', function(string, string2) { + let path = getPath(string, this.context); + path += '/' + this.context[string2]; + return util.request(this.context, 'DELETE', path); +}); + +/** + * @module Rest_Calls + * @description makes a DELETE request to the given path and appends the saved property (path is appended after the "onboarding-api/v1.0" prefix)<br> + * @exampleFile Example_Rest_Calls.feature + * @step I want to delete for path {string} with the value from saved property {string} + **/ +When('I want to delete for path {string}', function (string) { + let path = getPath(string, this.context); + //path += '/' + this.context[string2]; + return util.request(this.context, 'DELETE', path); +}); + +/** + * @module Rest_Calls + * @description makes a PUT request to the given path and sends the input data from the context (path is appended after the "onboarding-api/v1.0" prefix)<br> + * @exampleFile Example_Rest_Calls.feature + * @step I want to update for path {string} with the input data from the context + **/ +When('I want to update for path {string} with the input data from the context', function(string) { + let path = getPath(string, this.context); + return util.request(this.context, 'PUT', path, this.context.inputData); +}); + +/** + * @module Rest_Calls + * @description makes a POST request to the given path and sends the input data from the context (path is appended after the "onboarding-api/v1.0" prefix)<br> + * @exampleFile Example_Rest_Calls.feature + * @step I want to create for path {string} with the input data from the context + **/ +When('I want to create for path {string} with the input data from the context', function(string) { + let path = getPath(string, this.context); + return util.request(this.context, 'POST', path, this.context.inputData); +}); diff --git a/workflow/workflow-bdd/stepDefinitions/Utils.js b/workflow/workflow-bdd/stepDefinitions/Utils.js new file mode 100644 index 00000000..6f8a7a5c --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/Utils.js @@ -0,0 +1,153 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ +const request = require('request'); +const fs = require('fs'); +require('node-zip'); + +function _request(context, method, path, data, isBinary=false, type='onboarding') { + let server = context.getUrlForType(type); + + let options = { + method: method, + url: server + path, + headers: context.headers[type] + }; + console.log('--> Calling REST ' + options.method +' url: ' + options.url); + + return new Promise(function (resolve, reject) { + if (method === 'POST' || method === 'PUT') { + if (isBinary) { + var formData = { + upload: fs.createReadStream(data), + }; + options.formData = formData; + } else { + options.json = data; + } + } + request(options, function (err, result, data) { + context.inputData = null; + if (err) { + console.error('Request URL: ' + result.request.uri.href); + console.error('Request Method: ' + result.request.method); + console.error('Response Status Code: ' +result.statusCode); + console.log(err); + reject(err); + } else { + let isExpected = (context.shouldFail) ? (result.statusCode != 200 && result.statusCode != 201) : (result.statusCode == 200 || result.statusCode == 201); + if (!isExpected) { + console.error('Request URL: ' + result.request.uri.href); + console.error('Request Method: ' + result.request.method); + console.error('Response Status Code: ' +result.statusCode); + console.error(result.body); + reject('Status Code was ' + result.statusCode); + } + if (context.shouldFail && context.responseStatusCode) { + if (result.statusCode !== context.responseStatusCode) { + reject('Response Status Code was ' + result.statusCode + ' instead of ' + context.responseStatusCode); + } + } + if (context.shouldFail && context.errorCode) { + if (typeof data === 'string' && data) { + data = JSON.parse(data); + } + let errorCode = data.errorCode; + let contextErrorCode = context.errorCode; + context.errorCode = null; + if (errorCode !== contextErrorCode) { + reject('Error Code was ' + errorCode + ' instead of ' + contextErrorCode); + } + } + if (context.shouldFail && context.errorMessage) { + if (typeof data === 'string' && data) { + data = JSON.parse(data); + } + let errorMessage = data.message; + let contextErrorMessage = context.errorMessage; + context.errorMessage = null; + if (errorMessage !== contextErrorMessage) { + reject('Error Message was ' + errorMessage + ' instead of ' + contextErrorMessage); + } + } + if (context.shouldFail) { + context.shouldFail = false; + resolve({statusCode: result.statusCode, data: {}}); + return; + } + if (method === 'GET' && isBinary) { + // downloading (NetworkPackage) files + return ({ + blob: blobUtil.createBlob([data], {type: 'text/plain'}), + headers: result.headers + }); + } else { + if (typeof data === 'string' && data) { + data = JSON.parse(data); + } + context.responseData = data; + context.inputData = data; + resolve({statusCode: result.statusCode, data: data}); + } + } + }); + }); +} + +function download(context, path, filePath, callback, type='onboarding') { + let server = context.getUrlForType(type); + let options = { + method: 'GET', + url: server + path, + headers: context.headers[type] + }; + console.log('--> Calling REST download url: ' + options.url); + + var file = fs.createWriteStream(filePath); + var r = request(options).pipe(file); + r.on('error', function (err) { + console.log(err); + callback(err); + }); + r.on('finish', function () { + file.close(); + let zipFile = fs.readFileSync(filePath, 'binary'); + let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); + if (zip.files['MANIFEST.json']) { + let manifestData = zip.files['MANIFEST.json']._data; + manifestData = manifestData.replace(/\\n/g, ''); + context.responseData = JSON.parse(manifestData); + } + callback(); + }); + +}; + +function _random() { + let d = new Date(); + return d.getTime().toString().split('').reverse().join(''); +} + +function _getJSONFromFile(file) { + return JSON.parse(fs.readFileSync(file, 'utf8')); +} + + +module.exports = { + request: _request, + random : _random, + getJSONFromFile: _getJSONFromFile, + download: download +}; diff --git a/workflow/workflow-bdd/stepDefinitions/Workflow_Steps.js b/workflow/workflow-bdd/stepDefinitions/Workflow_Steps.js new file mode 100644 index 00000000..59c95d89 --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/Workflow_Steps.js @@ -0,0 +1,19 @@ +/** + * @module WORKFLOW + * @description Creates a new WORKFLOW with a random name and saves the id and versionId on the context item object and the context vlm object<br> + * Input data will be taken from the 'resources/json/createWorkflow.json' file. + * @step I want to create a Workflow + **/ + +const {Then, When, Given} = require('cucumber'); +const assert = require('assert'); +const util = require('./Utils.js'); + +When('I want to create a Workflow', function() { + let inputData = util.getJSONFromFile('resources/json/createWorkflow.json'); + inputData.name = util.random(); + let path = '/workflows'; + return util.request(this.context, 'POST', path, inputData).then(result => { + this.context.item ={id : result.data.id}; +}); +});
\ No newline at end of file diff --git a/workflow/workflow-bdd/stepDefinitions/world.js b/workflow/workflow-bdd/stepDefinitions/world.js new file mode 100644 index 00000000..cf749e66 --- /dev/null +++ b/workflow/workflow-bdd/stepDefinitions/world.js @@ -0,0 +1,87 @@ +/* + * Copyright © 2016-2017 European Support Limited + * + * 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. + */ +const { setWorldConstructor } = require('cucumber'); +const _ = require('lodash'); + +let config = require('../config.json'); +let localConfig = {}; +try { + localConfig = require('../devConfig.json'); +} catch (e) { + try { + localConfig = require('../jenkinsConfig.json'); + } catch (e) { + console.error("no env configuration was found!"); + } +} + +config = _.merge(config, localConfig); +var {setDefaultTimeout} = require('cucumber'); + + +/** + * @module Context + * @description Context that is used per feature file and can be accessed as 'this.context' in all steps.<Br> + *<Br> + * Contains the following items:<br> + * <li>this.context.server <ul>REST server and onboarding prefix including version. set either in configuration file or from the command line or SERVER environment variable</ul> + * <li>this.context <ul>Object with properties that were saved in the steps.</ul> + * <li>this.context.inputdata <ul><b>Automatically updated with the last responseData from the Rest call</b><br>Object with properties that were prepares in the steps.</ul> + * <li>this.context.responseData <ul>Response from the last REST call.</ul> + **/ +class CustomWorld { + constructor(options) { + this.context = {}; + this.context.headers = {}; + let typeName; + for (typeName in config) { + this.context.headers[typeName] = {}; + if (config[typeName].user) { + this.context.headers[typeName]['USER_ID'] = config[typeName].user; + } + } + + this.context.item = {id: null, versionId: null, componentId: null}; + + this.context.shouldFail = false; + this.context.errorCode = null; + this.context.inputData = null; + this.context.responseData = null; + + this.context.defaultServerType = 'workflow'; + + this.config = config; + + let context = this.context; + this.context.getUrlForType = (function(type) { + var _server = context.server; + var _config = config; + return function(type) { + let typeData = _config["workflow"]; + let _url = _config.protocol + '://' + + typeData.server + ':' + + typeData.port + '/' + + typeData.prefix; + return _url; + } + })(); + + setDefaultTimeout(60 * 1000); + } +} + + +setWorldConstructor(CustomWorld); |