Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add js tests for wiring connections #236

Merged
merged 3 commits into from
Oct 13, 2016
Merged
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
4 changes: 3 additions & 1 deletion src/GruntFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ var WirecloudFiles = [
'wirecloud/platform/static/js/wirecloud/wiring/WidgetSourceEndpoint.js',
'wirecloud/platform/static/js/wirecloud/wiring/WidgetTargetEndpoint.js',
'wirecloud/platform/static/js/wirecloud/wiring/OperatorSourceEndpoint.js',
'wirecloud/platform/static/js/wirecloud/wiring/OperatorTargetEndpoint.js'
'wirecloud/platform/static/js/wirecloud/wiring/OperatorTargetEndpoint.js',
'wirecloud/platform/static/js/wirecloud/wiring/Connection.js',
'wirecloud/platform/static/js/wirecloud/wiring/ConnectionLogManager.js'
];

module.exports = function (grunt) {
Expand Down
318 changes: 318 additions & 0 deletions src/js_tests/wirecloud/ConnectionSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
/*
* Copyright (c) 2016 CoNWeT Lab., Universidad Politécnica de Madrid
*
* This file is part of Wirecloud Platform.
*
* Wirecloud Platform is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Wirecloud is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Wirecloud Platform. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

/* jshint jasmine:true */
/* globals Wirecloud */


(function (ns) {

"use strict";

describe("Connection", function () {
var operatorModel, widgetModel, endpointDesc1, endpointDesc2, sourceEndpoint, targetEndpoint, wiringEngine;

beforeEach(function () {

endpointDesc1 = {
name: "source",
description: "description",
label: "title",
friendcode: "a"
};

endpointDesc2 = {
name: "target",
description: "description",
label: "title",
friendcode: "a"
};

operatorModel = {
id: "1",
volatile: false,
meta: {
type: 'operator'
}
};

widgetModel = {
id: "1",
volatile: false,
meta: {
type: 'widget'
}
};

wiringEngine = {
logManager: new Wirecloud.LogManager()
};

sourceEndpoint = new ns.WidgetSourceEndpoint(widgetModel, endpointDesc1);
targetEndpoint = new ns.OperatorTargetEndpoint(operatorModel, endpointDesc2);
});

describe("new Connection(wiringEngine, sourceEndpoint, targetEndpoint, [options])", function () {

it("should create a new instance with no options", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

expect(connection instanceof ns.Connection).toBe(true);
expect(connection.source).toBe(sourceEndpoint);
expect(connection.target).toBe(targetEndpoint);
expect(connection.readonly).toBe(false);
expect(connection.volatile).toBe(false);
expect(connection.id).toEqual([sourceEndpoint.id, targetEndpoint.id].join("//"));
});

it("should create a new instance with options.readonly = true", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint, {
readonly: true
});

expect(connection.readonly).toBe(true);
});
});

describe("toJSON()", function () {

it("should parse to JSON", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

expect(connection.toJSON()).toEqual({
readonly: false,
source: sourceEndpoint.toJSON(),
target: targetEndpoint.toJSON()
});
});
});

describe("equals(value)", function () {

it("should be equals to the same connection", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

expect(connection.equals(connection)).toBe(true);
});

it("should be equals to another connection with same endpoints", function () {
var connection1 = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);
var connection2 = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

expect(connection1.equals(connection2)).toBe(true);
});

it("should not be equals to another connection with different endpoints", function () {
var endpointDesc3 = {
name: "target-test",
description: "description",
label: "title",
friendcode: "a"
};
var targetEndpoint2 = new ns.OperatorTargetEndpoint(operatorModel, endpointDesc3);

var connection1 = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);
var connection2 = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint2);

expect(connection1.equals(connection2)).toBe(false);
});
});

describe("establish()", function () {

it("should connect two endpoints properly", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

var callback = jasmine.createSpy("callback");

connection.addEventListener("establish", callback);

expect(connection.establish()).toBe(connection);
expect(connection.established).toBe(true);
expect(callback.calls.count()).toEqual(1);
});

it("should do nothing if the connection is already established", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

var callback = jasmine.createSpy("callback");

connection.addEventListener("establish", callback);
connection.establish();

expect(connection.establish()).toBe(connection);
expect(connection.established).toBe(true);
expect(callback.calls.count()).toEqual(1);
});

it("should do nothing if the source is a ghost endpoint", function () {
var missingSourceEndpoint = new ns.GhostSourceEndpoint(widgetModel, "missing");
var connection = new ns.Connection(wiringEngine, missingSourceEndpoint, targetEndpoint);

var callback = jasmine.createSpy("callback");

connection.addEventListener("establish", callback);

expect(connection.establish()).toBe(connection);
expect(connection.established).toBe(false);
expect(callback.calls.count()).toEqual(0);
});

it("should do nothing if the target is a ghost endpoint", function () {
var missingTargetEndpoint = new ns.GhostTargetEndpoint(operatorModel, "missing");
var connection = new ns.Connection(wiringEngine, sourceEndpoint, missingTargetEndpoint);

var callback = jasmine.createSpy("callback");

connection.addEventListener("establish", callback);

expect(connection.establish()).toBe(connection);
expect(connection.established).toBe(false);
expect(callback.calls.count()).toEqual(0);
});
});

describe("detach()", function () {

it("should disconnect two endpoints properly", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

var callback = jasmine.createSpy("callback");

connection.addEventListener("detach", callback);
connection.establish();

expect(connection.detach()).toBe(connection);
expect(connection.established).toBe(false);
expect(callback.calls.count()).toEqual(1);
});

it("should do nothing if the connection is not established", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

var callback = jasmine.createSpy("callback");

connection.addEventListener("detach", callback);

expect(connection.detach()).toBe(connection);

expect(callback.calls.count()).toEqual(0);
});
});

describe("remove()", function () {
Copy link
Member

Choose a reason for hiding this comment

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

Missing tests removing a disconnected connection


it("should remove if the connection is established", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

var callback1 = jasmine.createSpy("callback1");
var callback2 = jasmine.createSpy("callback2");

connection.addEventListener("detach", callback1);
connection.addEventListener("remove", callback2);
connection.establish();

expect(connection.remove()).toBe(connection);
expect(connection.established).toBe(false);
expect(callback1.calls.count()).toEqual(1);
expect(callback2.calls.count()).toEqual(1);
});

it("should do nothing if the connection is not established", function () {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);

var callback1 = jasmine.createSpy("callback1");
var callback2 = jasmine.createSpy("callback2");

connection.addEventListener("detach", callback1);
connection.addEventListener("remove", callback2);

expect(connection.remove()).toBe(connection);
expect(connection.established).toBe(false);
expect(callback1.calls.count()).toEqual(0);
expect(callback2.calls.count()).toEqual(1);
});
});

describe("updateEndpoint(endpoint)", function () {

it("should change the missing source-endpoint", function () {
var missingSourceEndpoint = new ns.GhostSourceEndpoint(widgetModel, "missing");
var connection = new ns.Connection(wiringEngine, missingSourceEndpoint, targetEndpoint);

var callback = jasmine.createSpy("callback");

connection.addEventListener("establish", callback);

expect(connection.updateEndpoint(sourceEndpoint)).toBe(connection);
expect(connection.established).toBe(true);
expect(connection.source).toEqual(sourceEndpoint);
expect(callback.calls.count()).toEqual(1);
});

it("should change the missing target-endpoint", function () {
var missingTargetEndpoint = new ns.GhostTargetEndpoint(operatorModel, "missing");
var connection = new ns.Connection(wiringEngine, sourceEndpoint, missingTargetEndpoint);

var callback = jasmine.createSpy("callback");

connection.addEventListener("establish", callback);

expect(connection.updateEndpoint(targetEndpoint)).toBe(connection);
expect(connection.established).toBe(true);
expect(connection.target).toEqual(targetEndpoint);
Copy link
Member

Choose a reason for hiding this comment

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

updateEndpoint should support method chaining. Change this line to:

expect(connection.updateEndpoint(targetEndpoint)).toBe(connection);

expect(callback.calls.count()).toEqual(1);
});

describe("throws a TypeError if endpoint is not valid", function () {

var base_test = function base_test(value) {
var connection = new ns.Connection(wiringEngine, sourceEndpoint, targetEndpoint);
var callback = jasmine.createSpy("callback");

connection.addEventListener("establish", callback);

expect(function () {
connection.updateEndpoint(value);
}).toThrowError(TypeError);
expect(callback.calls.count()).toEqual(0);
};

it("null", function () {
base_test(null);
});

it("number", function () {
base_test(0);
});

it("string", function () {
base_test("test");
});

it("object", function () {
base_test({});
});
});
});
});

})(Wirecloud.wiring);
Loading