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

Issue 301 #302

Merged
merged 2 commits into from
Apr 19, 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
69 changes: 53 additions & 16 deletions lib/api_resources/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,64 @@ RootResource.prototype.list = function(env, next) {
return next(env);
}

// Filter only peers connecting to this server
peers = peers.filter(function(peer) {
return peer.direction === 'acceptor';
});

var href = '/servers/{{peerName}}';
var qs ='?' + querystring.stringify({ql: params.ql});
env.request.templateUrl = href + qs;

var httpServer = self.server.httpServer;

self.server.runtime.registry.find(params.ql, function(err, results) {
if (err) {
env.response.statusCode = 400;
env.response.body = {
class: ['query-error'],
properties: {
message: err.message
},
links: [
{ rel: ['self'], href: env.helpers.url.current() }
]
};
return next(env);
var results = [];
var query = self.server.runtime.ql(params.ql);
var keys = Object.keys(self.server.runtime._jsDevices);
var maxIndex = keys.length - 1;
var hasError = false;

if (maxIndex === -1) {
return done();
}

// Return all devices from local server
keys.forEach(function(key, i) {
var device = self.server.runtime._jsDevices[key];

if (hasError) {
return;
}

self.server.runtime.registry.match(query, device, function(err, match) {
if (err) {
env.response.statusCode = 400;
hasError = true;
env.response.body = {
class: ['query-error'],
properties: {
message: err.message
},
links: [
{ rel: ['self'], href: env.helpers.url.current() }
]
};
return next(env);
}

if (match) {
results.push(match);
}

if (i === maxIndex) {
done();
}
});
});

function done() {
var localEntities = (!!err || !results) ? [] : results.map(function(device){
var deviceOnRuntime = self.server.runtime._jsDevices[device.id];
var deviceOnRuntime = device;
if (deviceOnRuntime) {
var model = deviceOnRuntime;
var loader = {path: '/servers/' + encodeURI(self.server.id)}
Expand All @@ -76,7 +112,7 @@ RootResource.prototype.list = function(env, next) {
};
return entity;
}
}).filter(function(entity) { return entity !== null });
}).filter(function(entity) { return entity !== null || entity !== undefined; });

httpServer.proxyToPeers(peers, env, function(err, results, messageId) {
var entities = results.map(function(obj) {
Expand Down Expand Up @@ -128,7 +164,8 @@ RootResource.prototype.list = function(env, next) {
delete httpServer.clients[messageId];
next(env);
});
});
}// done

});
} else if (this.shouldProxy(server)) {
this.server.peerRegistry.get(server, function(err, peer) {
Expand Down
86 changes: 85 additions & 1 deletion test/test_query_api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var assert = require('assert');
var os = require('os');
var util = require('util');
var request = require('supertest');
var zetta = require('../');
var Query = require('calypso').Query;
Expand All @@ -10,6 +11,25 @@ var HttpDriver = require('./fixture/example_http_driver');
var Registry = require('./fixture/mem_registry');
var PeerRegistry = require('./fixture/mem_peer_registry');
var zettacluster = require('zetta-cluster');
var Scientist = require('zetta-scientist');
var Runtime = require('../zetta_runtime');
var Device = Runtime.Device;

function TestDriver() {
Device.call(this);
this.foo = 'fooData';
this.bar = 'barData';
this.id = '123456789';
}
util.inherits(TestDriver, Device);

TestDriver.prototype.init = function(config) {
config
.name('Test')
.type('testdriver')
.state('ready');
};


function getHttpServer(app) {
return app.httpServer.server;
Expand Down Expand Up @@ -308,9 +328,73 @@ describe('Zetta Query Api', function() {
hasLinkRel(body.links, 'http://rels.zettajs.io/query');
}))
.end(done);

});
});

describe('Non provisioned devices', function() {
beforeEach(function(done) {
machine = Scientist.create(TestDriver);
Scientist.init(machine);
reg.save(machine, function(err) {
assert.ok(!err);
app = zetta({ registry: reg, peerRegistry: peerRegistry })
.silent()
.use(Scout)
.name('local')
.expose('*')
._run();
done();
});
});

it('queries on /servers/<id> should return no results', function(done) {
request(getHttpServer(app))
.get('/servers/local?ql=where%20type%20=%20"testdriver"')
.expect(getBody(function(res, body) {
assert.equal(body.entities.length, 1);
body.entities.forEach(function(entity) {
assert(entity.links);
})
}))
.end(done);
})

it('queries on /?server=<server> should return no results', function(done) {
request(getHttpServer(app))
.get('/?ql=where%20type%20=%20"testdriver"&server=local')
.expect(getBody(function(res, body) {
assert.equal(body.entities.length, 1);
body.entities.forEach(function(entity) {
assert(entity.links);
})
}))
.end(done);
})

it('queries on /?server=* should return no results', function(done) {
request(getHttpServer(app))
.get('/?ql=where%20type%20=%20"testdriver"&server=*')
.expect(getBody(function(res, body) {
assert.equal(body.entities.length, 1);
body.entities.forEach(function(entity) {
assert(entity.links);
})
}))
.end(done);
})

it('queries on / should return no results', function(done) {
request(getHttpServer(app))
.get('/?ql=where%20type%20=%20"testdriver"')
.expect(getBody(function(res, body) {
assert.equal(body.entities.length, 1);
body.entities.forEach(function(entity) {
assert(entity.links);
})
}))
.end(done);
})
})

describe('queries on /servers/<id>', function() {
var app = null;
Expand Down