-
Notifications
You must be signed in to change notification settings - Fork 45
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
aarranz
merged 3 commits into
Wirecloud:develop
from
jpajuelo:feature/wiring-connections-tests
Oct 13, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () { | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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