Skip to content

Commit

Permalink
Merge pull request #103 from MaibornWolff/variableAsPayload_for_mqtt_…
Browse files Browse the repository at this point in the history
…publish

Variable as payload for mqtt publish
  • Loading branch information
sken authored Nov 16, 2020
2 parents 23e326f + f6c9fa7 commit 14a3d11
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@maibornwolff/alt-core-js",
"version": "1.18.1",
"version": "1.18.2",
"description": "Simple test framework supporting execution of different `scenarios` based on `action` templates. The framework supports definition of different action types in an *yaml* format, including e.g. which endpoints have to be called with which parameters as well as defining validation rules to be applied on the responses. It also supports detailed report creation of the test results.",
"main": "lib/index",
"types": "lib/index",
Expand Down
30 changes: 23 additions & 7 deletions src/model/MqttPublishAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class MqttPublishAction implements Action {

private readonly diagramConfiguration: DiagramConfiguration;

private readonly variableAsPayload?: string;

public constructor(
name: string,
desc = name,
Expand All @@ -57,6 +59,7 @@ class MqttPublishAction implements Action {
invokeEvenOnFail = actionDef.invokeEvenOnFail,
allowFailure = actionDef.allowFailure,
diagramConfiguration = actionDef.diagramConfiguration ?? {},
variableAsPayload = actionDef.variableAsPayload,
) {
this.name = name;
this.url = url;
Expand All @@ -71,6 +74,7 @@ class MqttPublishAction implements Action {
this.invokeEvenOnFail = invokeEvenOnFail;
this.allowFailure = allowFailure;
this.diagramConfiguration = diagramConfiguration;
this.variableAsPayload = variableAsPayload;
}

public static fromTemplate(
Expand All @@ -85,8 +89,8 @@ class MqttPublishAction implements Action {
}

public invoke(scenario: Scenario): ActionCallback {
const promise = new Promise(resolve => {
this.invokeAsync(scenario);
const promise = new Promise((resolve, reject) => {
this.invokeAsync(scenario, reject);
resolve();
});
return { promise, cancel: () => console.log('TODO') };
Expand All @@ -110,7 +114,7 @@ class MqttPublishAction implements Action {
];
}

private genrateJsonPayload(
private generateJsonPayload(
scenarioVariables: Map<string, any>,
ctx = {},
): [string, string] {
Expand All @@ -120,7 +124,7 @@ class MqttPublishAction implements Action {
return [payload, payload];
}

private invokeAsync(scenario: Scenario): void {
private invokeAsync(scenario: Scenario, reject): void {
const logDebug = (debugMessage: string): void => {
getLogger(scenario.name).debug(debugMessage, ctx);
};
Expand Down Expand Up @@ -155,9 +159,21 @@ class MqttPublishAction implements Action {
client.on('connect', () => {
logDebug(`MQTT connection to ${this.url} successfully opened`);

const [payload, dataString] = this.protoFile
? this.encodeProtoPayload(scenario.cache, ctx)
: this.genrateJsonPayload(scenario.cache, ctx);
let [payload, dataString]: [string | Buffer, string] = ['', ''];

if (this.variableAsPayload !== undefined) {
const body = scenario.cache.get(this.variableAsPayload);
if (typeof body === 'string' || body instanceof Buffer) {
[payload, dataString] = [
body,
JSON.stringify(body.toString()),
];
}
} else {
[payload, dataString] = this.protoFile
? this.encodeProtoPayload(scenario.cache, ctx)
: this.generateJsonPayload(scenario.cache, ctx);
}

const topic = injectEvalAndVarsToString(
this.topic,
Expand Down
9 changes: 9 additions & 0 deletions src/model/NodeJSAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { injectVariableAccessAndEvaluate } from '../variableInjection';

export interface NodeJSActionDefinition extends ActionDefinition {
variables?: { [key: string]: string };
data?: { [key: string]: string };
}

export function isValidNodeJSActionDefinition(
Expand Down Expand Up @@ -47,20 +48,24 @@ export class NodeJSAction implements Action {

private readonly variables: { [key: string]: string };

private readonly data: { [key: string]: string };

public constructor(
name: string,
{
description = name,
invokeEvenOnFail = false,
allowFailure = false,
variables,
data,
}: NodeJSActionDefinition,
) {
this.name = name;
this.description = description;
this.invokeEvenOnFail = invokeEvenOnFail;
this.allowFailure = allowFailure;
this.variables = variables || {};
this.data = data || {};
}

public static fromTemplate(
Expand All @@ -79,11 +84,15 @@ export class NodeJSAction implements Action {
? nodeJSDefinition.allowFailure
: template.allowFailure,
variables: nodeJSDefinition.variables || template.variables,
data: nodeJSDefinition.data || template.data,
});
}

invoke(scenario: Scenario): ActionCallback {
const scenarioVariables = scenario.cache;
Object.entries(this.data).forEach(([key, value]) => {
scenarioVariables.set(key, value);
});

return {
cancel: () => {},
Expand Down
3 changes: 3 additions & 0 deletions src/nodeGlobals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as protoParsing from './protoParsing';

export const nodeGlobals = {
Buffer,
__dirname,
Expand All @@ -14,4 +16,5 @@ export const nodeGlobals = {
setImmediate,
setInterval,
setTimeout,
...protoParsing,
};

0 comments on commit 14a3d11

Please sign in to comment.