UNPKG

6.09 kBJavaScriptView Raw
1var assert = require('assert');
2var request = require('supertest');
3var zetta = require('../');
4var rels = require('zetta-rels');
5var Scout = require('./fixture/example_scout');
6var Driver = require('./fixture/example_driver');
7var Registry = require('./fixture/mem_registry');
8var PeerRegistry = require('./fixture/mem_peer_registry');
9
10function getHttpServer(app) {
11 return app.httpServer.server;
12}
13
14function getBody(fn) {
15 return function(res) {
16 try {
17 if(res.text) {
18 var body = JSON.parse(res.text);
19 } else {
20 var body = '';
21 }
22 } catch(err) {
23 throw new Error('Failed to parse json body');
24 }
25
26 fn(res, body);
27 }
28}
29
30describe('Metadata API', function() {
31 var reg = null;
32 var peerRegistry = null;
33 var app = null;
34 var url = null;
35 var device = null;
36
37 beforeEach(function(done) {
38 reg = new Registry();
39 peerRegistry = new PeerRegistry();
40
41 app = zetta({ registry: reg, peerRegistry: peerRegistry })
42 .silent()
43 .use(Driver)
44 .name('local')
45 ._run(function() {
46 device = app.runtime._jsDevices[Object.keys(app.runtime._jsDevices)[0]];
47 url = '/servers/' + app._name + '/devices/' + device.id;
48 done();
49 });
50 });
51
52 it('should contain a metadata class', function(done) {
53 request(getHttpServer(app))
54 .get('/servers/local/meta')
55 .expect(getBody(function(res, body){
56 assert.deepEqual(body.class, ['metadata']);
57 }))
58 .end(done);
59 });
60
61 it('should contain a self link', function(done) {
62 request(getHttpServer(app))
63 .get('/servers/local/meta')
64 .expect(getBody(function(res, body){
65 assert.equal(body.links[0].rel[0], 'self');
66 }))
67 .end(done);
68 });
69
70 it('should contain a server link', function(done) {
71 request(getHttpServer(app))
72 .get('/servers/local/meta')
73 .expect(getBody(function(res, body){
74 assert.equal(body.links[1].rel[0], rels.server);
75 }))
76 .end(done);
77 });
78
79 it('should contain a monitor link', function(done) {
80 request(getHttpServer(app))
81 .get('/servers/local/meta')
82 .expect(getBody(function(res, body){
83 assert.equal(body.links[2].rel[0], 'monitor');
84 assert.notEqual(body.links[2].href.indexOf('topic=meta'), -1);
85 }))
86 .end(done);
87 });
88
89 describe('Type Sub-entity', function() {
90 it('should contain a type class', function(done) {
91 request(getHttpServer(app))
92 .get('/servers/local/meta')
93 .expect(getBody(function(res, body){
94 assert.deepEqual(body.entities[0].class, ['type']);
95 }))
96 .end(done);
97 });
98
99 it('should contain properties', function(done) {
100 request(getHttpServer(app))
101 .get('/servers/local/meta')
102 .expect(getBody(function(res, body){
103 assert(Object.keys(body.entities[0].properties.properties).length > 0);
104 }))
105 .end(done);
106 });
107
108 it('should contain streams', function(done) {
109 request(getHttpServer(app))
110 .get('/servers/local/meta')
111 .expect(getBody(function(res, body){
112 assert(body.entities[0].properties.streams.length > 0);
113 }))
114 .end(done);
115 });
116
117 it('should contain transitions', function(done) {
118 request(getHttpServer(app))
119 .get('/servers/local/meta')
120 .expect(getBody(function(res, body){
121 assert(body.entities[0].properties.transitions.length > 0);
122 }))
123 .end(done);
124 });
125
126 it('should contain a self link', function(done) {
127 request(getHttpServer(app))
128 .get('/servers/local/meta')
129 .expect(getBody(function(res, body){
130 assert.equal(body.entities[0].links[0].rel[0], 'self');
131 }))
132 .end(done);
133 });
134 });
135
136 describe('Type Sub-resource', function() {
137 it('should contain a type class', function(done) {
138 request(getHttpServer(app))
139 .get('/servers/local/meta/testdriver')
140 .expect(getBody(function(res, body){
141 assert.deepEqual(body.class, ['type']);
142 }))
143 .end(done);
144 });
145
146 it('should contain properties', function(done) {
147 request(getHttpServer(app))
148 .get('/servers/local/meta/testdriver')
149 .expect(getBody(function(res, body){
150 assert(Object.keys(body.properties.properties).length > 0);
151 }))
152 .end(done);
153 });
154
155 it('should contain streams', function(done) {
156 request(getHttpServer(app))
157 .get('/servers/local/meta/testdriver')
158 .expect(getBody(function(res, body){
159 assert(body.properties.streams.length > 0);
160 }))
161 .end(done);
162 });
163
164 it('should contain transitions', function(done) {
165 request(getHttpServer(app))
166 .get('/servers/local/meta/testdriver')
167 .expect(getBody(function(res, body){
168 assert(body.properties.transitions.length > 0);
169 }))
170 .end(done);
171 });
172
173 it('should contain a self link', function(done) {
174 request(getHttpServer(app))
175 .get('/servers/local/meta/testdriver')
176 .expect(getBody(function(res, body){
177 assert.equal(body.links[0].rel[0], 'self');
178 }))
179 .end(done);
180 });
181
182 it('should contain a collection link', function(done) {
183 request(getHttpServer(app))
184 .get('/servers/local/meta/testdriver')
185 .expect(getBody(function(res, body){
186 assert.equal(body.links[1].rel[0], 'collection');
187 assert.equal(body.links[1].rel[1], rels.metadata);
188 }))
189 .end(done);
190 });
191
192 it('should contain an instances link', function(done) {
193 request(getHttpServer(app))
194 .get('/servers/local/meta/testdriver')
195 .expect(getBody(function(res, body){
196 assert.equal(body.links[2].rel[0], rels.instances);
197 assert.equal(body.links[2].rel[1], 'describes');
198 }))
199 .end(done);
200 });
201 });
202});