Skip to content

Commit

Permalink
Revert to the old version of get devices by segment (filter by segmen…
Browse files Browse the repository at this point in the history
…t query does not give the same result as getting device from a segment)
  • Loading branch information
Gregoire Weber committed Feb 16, 2017
1 parent d0c31e0 commit fb453f2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
17 changes: 17 additions & 0 deletions clients/Barracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,23 @@ class Barracks {
});
}

getSegmentDevices(token, segmentId) {
return new Promise(resolve => {
logger.debug('Getting devices for segment:', segmentId);
const stream = new PageableStream();
resolve(stream);
this.client.retrieveAllPages(stream, 'getSegmentDevices', {
headers: {
'x-auth-token': token
},
pathVariables: {
segmentId
}
},
'devices');
});
}

getDevices(token, query) {
return new Promise(resolve => {
logger.debug('Getting devices with query:', query);
Expand Down
28 changes: 28 additions & 0 deletions clients/Barracks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,34 @@ describe('Barracks', () => {
});
});

describe('#getSegmentDevices()', () => {

it('should return a stream object and deleguate to the client', done => {
// Given
const segmentId = 'aSegment';
const options = {
headers: { 'x-auth-token': token },
pathVariables: { segmentId }
}
barracks.client.retrieveAllPages = sinon.spy();

// When / Then
barracks.getSegmentDevices(token, segmentId).then(result => {
expect(result).to.be.instanceOf(PageableStream);
expect(barracks.client.retrieveAllPages).to.have.been.calledOnce;
expect(barracks.client.retrieveAllPages).to.have.been.calledWithExactly(
new PageableStream(),
'getSegmentDevices',
options,
'devices'
);
done();
}).catch(err => {
done(err);
});
});
});

describe('#getDevices()', () => {

it('should return a stream object and deleguate to the client when no query given', done => {
Expand Down
41 changes: 24 additions & 17 deletions commands/DevicesCommand.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
const BarracksCommand = require('./BarracksCommand');

function getQuery(token, program, barracks) {
function getAllDevicesFromFilter(token, barracks, filterName) {
return new Promise((resolve, reject) => {
let promise;
if (program.segment) {
promise = barracks.getSegmentByName(token, program.segment);
} else if (program.filter) {
promise = barracks.getFilterByName(token, program.filter);
} else {
promise = Promise.resolve({});
}
barracks.getFilterByName(token, filterName).then(filter => {
return barracks.getDevices(token, filter.query);
}).then(resultStream => {
resolve(resultStream);
}).catch(err => {
reject(err);
});
});
}

promise.then(filterObject => {
resolve(filterObject.query);
function getAllDevicesFromSegment(token, barracks, segmentName) {
return new Promise((resolve, reject) => {
barracks.getSegmentByName(token, segmentName).then(segment => {
return barracks.getSegmentDevices(token, segment.id);
}).then(resultStream => {
resolve(resultStream);
}).catch(err => {
reject(err);
});
Expand Down Expand Up @@ -42,12 +47,14 @@ class DevicesCommand extends BarracksCommand {
}

execute(program) {
let token;
return this.getAuthenticationToken().then(authToken => {
token = authToken;
return getQuery(token, program, this.barracks);
}).then(query => {
return this.barracks.getDevices(token, query);
return this.getAuthenticationToken().then(token => {
if (program.segment) {
return getAllDevicesFromSegment(token, this.barracks, program.segment);
} else if (program.filter) {
return getAllDevicesFromFilter(token, this.barracks, program.filter);
} else {
return this.barracks.getDevices(token);
}
});
}
}
Expand Down
14 changes: 7 additions & 7 deletions commands/DevicesCommand.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('DevicesCommand', () => {
const error = 'request failed';
devicesCommand.getAuthenticationToken = sinon.stub().returns(Promise.resolve(token));
devicesCommand.barracks.getSegmentByName = sinon.stub().returns(Promise.resolve(segment));
devicesCommand.barracks.getDevices = sinon.stub().returns(Promise.reject(error));
devicesCommand.barracks.getSegmentDevices = sinon.stub().returns(Promise.reject(error));

// when / Then
devicesCommand.execute(program).then(result => {
Expand All @@ -180,8 +180,8 @@ describe('DevicesCommand', () => {
expect(devicesCommand.getAuthenticationToken).to.have.been.calledWithExactly();
expect(devicesCommand.barracks.getSegmentByName).to.have.been.calledOnce;
expect(devicesCommand.barracks.getSegmentByName).to.have.been.calledWithExactly(token, segmentName);
expect(devicesCommand.barracks.getDevices).to.have.been.calledOnce;
expect(devicesCommand.barracks.getDevices).to.have.been.calledWithExactly(token, query);
expect(devicesCommand.barracks.getSegmentDevices).to.have.been.calledOnce;
expect(devicesCommand.barracks.getSegmentDevices).to.have.been.calledWithExactly(token, segmentId);
done();
});
});
Expand All @@ -191,7 +191,7 @@ describe('DevicesCommand', () => {
const program = programWithValidSegment;
devicesCommand.getAuthenticationToken = sinon.stub().returns(Promise.resolve(token));
devicesCommand.barracks.getSegmentByName = sinon.stub().returns(Promise.resolve(segment));
devicesCommand.barracks.getDevices = sinon.stub().returns(Promise.resolve(new PageableStream()));
devicesCommand.barracks.getSegmentDevices = sinon.stub().returns(Promise.resolve(new PageableStream()));

// when / Then
devicesCommand.execute(program).then(result => {
Expand All @@ -200,8 +200,8 @@ describe('DevicesCommand', () => {
expect(devicesCommand.getAuthenticationToken).to.have.been.calledWithExactly();
expect(devicesCommand.barracks.getSegmentByName).to.have.been.calledOnce;
expect(devicesCommand.barracks.getSegmentByName).to.have.been.calledWithExactly(token, segmentName);
expect(devicesCommand.barracks.getDevices).to.have.been.calledOnce;
expect(devicesCommand.barracks.getDevices).to.have.been.calledWithExactly(token, query);
expect(devicesCommand.barracks.getSegmentDevices).to.have.been.calledOnce;
expect(devicesCommand.barracks.getSegmentDevices).to.have.been.calledWithExactly(token, segmentId);
done();
}).catch(err => {
done(err);
Expand All @@ -220,7 +220,7 @@ describe('DevicesCommand', () => {
expect(devicesCommand.getAuthenticationToken).to.have.been.calledOnce;
expect(devicesCommand.getAuthenticationToken).to.have.been.calledWithExactly();
expect(devicesCommand.barracks.getDevices).to.have.been.calledOnce;
expect(devicesCommand.barracks.getDevices).to.have.been.calledWithExactly(token, undefined);
expect(devicesCommand.barracks.getDevices).to.have.been.calledWithExactly(token);
done();
}).catch(err => {
done(err);
Expand Down
4 changes: 4 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module.exports = {
method: 'GET',
path: '/api/member/segments/:id'
},
getSegmentDevices: {
method: 'GET',
path: '/api/member/segments/:segmentId/devices'
},
getFilters: {
method: 'GET',
path: '/api/member/filters/'
Expand Down

0 comments on commit fb453f2

Please sign in to comment.