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 patch on connection creation #273

Merged
merged 3 commits into from
Apr 1, 2019
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
127 changes: 115 additions & 12 deletions src/js_tests/wirecloud/WiringSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,27 +289,130 @@

});

it("returns connection instances", function () {
var result = wiring.createConnection(
it("returns task instances", function (done) {
spyOn(Wirecloud.io, "makeRequest").and.callFake(function (url, options) {
return new Wirecloud.Task("Sending request", (resolve) => {resolve({status: 204});});
});

var p = wiring.createConnection(
wiring.operators[0].outputs.output,
wiring.operators[1].inputs.input
);

expect(result)
.toEqual(jasmine.any(Wirecloud.wiring.Connection));
expect(wiring.connections).toEqual([]);
expect(p).toEqual(jasmine.any(Wirecloud.Task));
p.then(
(connection) => {
expect(Wirecloud.io.makeRequest).toHaveBeenCalled();
expect(connection)
.toEqual(jasmine.any(Wirecloud.wiring.Connection));
expect(wiring.connections).toEqual([connection]);
done();
},
(error) => {
fail("error callback called");
}
);

});

it("append connection instances when using the commit option", function () {
var result = wiring.createConnection(
it("allows volatile connection creation", function (done) {
// Convert operator 2 into a volatile operator
wiring.operators[1].volatile = true;
spyOn(Wirecloud.io, "makeRequest");

var p = wiring.createConnection(
wiring.operators[0].outputs.output,
wiring.operators[1].inputs.input,
{commit: true}
wiring.operators[1].inputs.input
);

expect(result)
.toEqual(jasmine.any(Wirecloud.wiring.Connection));
expect(wiring.connections).toEqual([result]);
expect(p).toEqual(jasmine.any(Wirecloud.Task));
p.then(
(connection) => {
expect(Wirecloud.io.makeRequest).not.toHaveBeenCalled();
expect(connection)
.toEqual(jasmine.any(Wirecloud.wiring.Connection));
expect(wiring.connections).toEqual([connection]);
done();
},
(error) => {
fail("error callback called");
}
);
});

describe("calls reject on unexepected responses", () => {

var test = (status) => {
return (done) => {
spyOn(Wirecloud.io, "makeRequest").and.callFake((url, options) => {
return new Wirecloud.Task("Sending request", (resolve) => {
resolve({
status: status
});
});
});

var task = wiring.createConnection(
wiring.operators[0].outputs.output,
wiring.operators[1].inputs.input
);

expect(task).toEqual(jasmine.any(Wirecloud.Task));
task.catch((error) => {
done();
});
};
};

it("200", test(200));
it("201", test(201));
it("404", test(404));
it("422", test(422));

});

describe("calls reject on error responses", () => {

var test = (status, details) => {
return (done) => {
var description = "detailed error description";
spyOn(Wirecloud.io, "makeRequest").and.callFake((url, options) => {
return new Wirecloud.Task("Sending request", (resolve) => {
resolve({
status: status,
responseText: JSON.stringify({
description: description,
details: details
})
});
});
});

var task = wiring.createConnection(
wiring.operators[0].outputs.output,
wiring.operators[1].inputs.input
);

expect(task).toEqual(jasmine.any(Wirecloud.Task));
task.catch((error) => {
if (details == null) {
expect(error).toBe(description);
} else {
expect(error).toEqual({
description: description,
details: details
});
}

done();
});
};
};

it("401", test(401));
it("403", test(403));
it("500", test(500));

});

});
Expand Down
47 changes: 37 additions & 10 deletions src/wirecloud/platform/static/js/wirecloud/Wiring.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,46 @@
* @returns {Wirecloud.Wiring.Connection}
*/
createConnection: function createConnection(source, target, options) {
options = utils.merge({
commit: false
}, options);

var connection = new Wirecloud.wiring.Connection(this, source, target, options);
const connection = new Wirecloud.wiring.Connection(this, source, target, options);
const priv = privates.get(this);

// Create the conection on the server
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 (see js_tests/wirecloud/WiringSpec.js for more details).
  • This code make requests for persisting volatile connections when it shouldn't.
  • The commit option must disappear and the code should assume commit === true.
  • The createConnection method should return Promises.

if (!connection.volatile) {
var requestContent = [{
op: "add",
path: "/connections/-",
value: connection
}];
return Wirecloud.io.makeRequest(Wirecloud.URLs.WIRING_ENTRY.evaluate({
workspace_id: this.workspace.id,
}), {
method: 'PATCH',
contentType: 'application/json-patch+json',
requestHeaders: {'Accept': 'application/json'},
postBody: JSON.stringify(requestContent)
}).then((response) => {
if ([204, 401, 403, 404, 500].indexOf(response.status) === -1) {
return Promise.reject(utils.gettext("Unexpected response from server"));
} else if ([401, 403, 404, 500].indexOf(response.status) !== -1) {
return Promise.reject(Wirecloud.GlobalLogManager.parseErrorResponse(response));
}

if (options.commit) {
connection.addEventListener('remove', privates.get(this).on_removeconnection);
connection.establish();
privates.get(this).connections.push(connection);
}
connection.addEventListener('remove', priv.on_removeconnection);
connection.establish();
priv.connections.push(connection);

return Promise.resolve(connection);
});
} else {
return new Wirecloud.Task("Creating connection", (resolve) => {
connection.addEventListener('remove', priv.on_removeconnection);
connection.establish();
priv.connections.push(connection);

return connection;
resolve(connection);
});
}
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@
if (this.established) {
return this;
}

establishConnection.call(this, wiringEngine.createConnection(source, target, {
wiringEngine.createConnection(source, target, {
readonly: readonly
}));
this.refresh();
}).then((connection) => {
establishConnection.call(this, connection);
this.refresh();
});

return this;
},
Expand Down