forked from Azure/autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel-flatten.js
229 lines (219 loc) · 8.04 KB
/
model-flatten.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var express = require('express');
var router = express.Router();
var util = require('util');
var _ = require('underscore');
var utils = require('../util/utils');
var modelFlatten = function (coverage) {
router.get('/:type', function (req, res, next) {
if (req.params.type === 'array') {
coverage['getModelFlattenArray']++;
var result = [
{
id: '1',
location: 'Building 44',
name: 'Resource1',
properties: {
provisioningState: 'Succeeded',
provisioningStateValues: 'OK',
'p.name': 'Product1',
type: 'Flat'
},
tags: { tag1: 'value1', tag2: 'value3' },
type: 'Microsoft.Web/sites'
},
{
id: '2',
name: 'Resource2',
location: 'Building 44'
},
{
id: '3',
name: 'Resource3'
}
];
res.status(200).end(JSON.stringify(result));
} else if (req.params.type === 'dictionary') {
coverage['getModelFlattenDictionary']++;
var result = {
Product1: {
id: '1',
location: 'Building 44',
name: 'Resource1',
properties: {
provisioningState: 'Succeeded',
provisioningStateValues: 'OK',
'p.name': 'Product1',
type: 'Flat'
},
tags: { tag1: 'value1', tag2: 'value3' },
type: 'Microsoft.Web/sites'
},
Product2: {
id: '2',
name: 'Resource2',
location: 'Building 44'
},
Product3: {
id: '3',
name: 'Resource3'
}
};
res.status(200).end(JSON.stringify(result));
} else if (req.params.type === 'resourcecollection') {
coverage['getModelFlattenResourceCollection']++;
var result = {
dictionaryofresources: {
Product1: {
id: '1',
location: 'Building 44',
name: 'Resource1',
properties: {
provisioningState: 'Succeeded',
provisioningStateValues: 'OK',
'p.name': 'Product1',
type: 'Flat'
},
tags: { tag1: 'value1', tag2: 'value3' },
type: 'Microsoft.Web/sites'
},
Product2: {
id: '2',
name: 'Resource2',
location: 'Building 44'
},
Product3: {
id: '3',
name: 'Resource3'
}
},
arrayofresources: [
{
id: '4',
location: 'Building 44',
name: 'Resource4',
properties: {
provisioningState: 'Succeeded',
provisioningStateValues: 'OK',
'p.name': 'Product4',
type: 'Flat'
},
tags: { tag1: 'value1', tag2: 'value3' },
type: 'Microsoft.Web/sites'
},
{
id: '5',
name: 'Resource5',
location: 'Building 44'
},
{
id: '6',
name: 'Resource6'
}
],
productresource: {
id: '7',
name: 'Resource7',
location: 'Building 44'
}
};
res.status(200).end(JSON.stringify(result));
} else {
utils.send400(res, next, "Request path must contain 'array', 'dictionary' or 'resourcecollection'");
}
});
var arrayBody = '[{"location":"West US","tags":{"tag1":"value1","tag2":"value3"}},{"location":"Building 44"}]';
var dictionaryBody = '{"Resource1":{"location":"West US", "tags":{"tag1":"value1", "tag2":"value3"},"properties":{"p.name":"Product1","type":"Flat"}},' +
'"Resource2":{"location":"Building 44", "properties":{"p.name":"Product2","type":"Flat"}}}';
var resourceCollectionBody = '{"arrayofresources":[' +
'{"location":"West US", "tags":{"tag1":"value1", "tag2":"value3"}, "properties":{"p.name":"Product1","type":"Flat"}},' +
'{"location":"East US", "properties":{"p.name":"Product2","type":"Flat"}}],' +
'"dictionaryofresources":' + dictionaryBody + ',' +
'"productresource":{"location":"India", "properties":{"p.name":"Azure","type":"Flat"}}}';
var customFlattenBody = {
base_product_id: "123",
base_product_description: "product description",
details: {
max_product_display_name: 'max name',
max_product_capacity: "Large",
max_product_image: {
'@odata.value': "http://foo"
}
}
};
var customFlattenBodyWithInheritedProperty = {
base_product_id: "123",
base_product_description: "product description",
details: {
max_product_display_name: 'max name',
max_product_capacity: "Large",
max_product_image: {
'@odata.value': "http://foo",
'generic_value': "https://generic"
}
}
};
router.put('/:type', function (req, res, next) {
if (req.body) {
if (req.params.type === 'array') {
if (_.isEqual(req.body, JSON.parse(arrayBody))) {
coverage['putModelFlattenArray']++;
res.status(200).end();
} else {
utils.send400(res, next, "The received body '" + JSON.stringify(req.body) + "' did not match the expected body '" + JSON.stringify(arrayBody) + "'.");
}
} else if (req.params.type === 'dictionary') {
if (_.isEqual(req.body, JSON.parse(dictionaryBody))) {
coverage['putModelFlattenDictionary']++;
res.status(200).end();
} else {
utils.send400(res, next, "The received body '" + JSON.stringify(req.body) + "' did not match the expected body '" + JSON.stringify(dictionaryBody) + "'.");
}
} else if (req.params.type === 'resourcecollection') {
if (_.isEqual(req.body, JSON.parse(resourceCollectionBody))) {
coverage['putModelFlattenResourceCollection']++;
res.status(200).end();
} else {
utils.send400(res, next, "The received body '" + JSON.stringify(req.body) + "' did not match the expected body '" + JSON.stringify(resourceCollectionBody) + "'.");
}
} else if (req.params.type === 'customFlattening') {
if (_.isEqual(req.body, customFlattenBodyWithInheritedProperty)) {
coverage['putModelFlattenCustomBase']++;
res.status(200).end(JSON.stringify(customFlattenBodyWithInheritedProperty));
} else {
utils.send400(res, next, "The received body '" + JSON.stringify(req.body) + "' did not match the expected body '" + JSON.stringify(customFlattenBody) + "'.");
}
}
} else {
utils.send400(res, next, "Was expecting a body in the put request.");
}
});
router.post('/:type', function (req, res, next) {
if (req.body) {
if (req.params.type === 'customFlattening') {
if (_.isEqual(req.body, customFlattenBody)) {
coverage['postModelFlattenCustomParameter']++;
res.status(200).end(JSON.stringify(customFlattenBody));
} else {
utils.send400(res, next, "The received body '" + JSON.stringify(req.body) + "' did not match the expected body '" + JSON.stringify(customFlattenBody) + "'.");
}
}
} else {
utils.send400(res, next, "Was expecting a body in the put request.");
}
});
router.put('/customFlattening/parametergrouping/:name', function (req, res, next) {
if (req.body) {
if (_.isEqual(req.body, customFlattenBody) && req.params.name === 'groupproduct') {
coverage['putModelFlattenCustomGroupedParameter']++;
res.status(200).end(JSON.stringify(customFlattenBody));
} else {
utils.send400(res, next, "The received body '" + JSON.stringify(req.body) + "' did not match the expected body '" + JSON.stringify(customFlattenBody) +
"'. Or the path parameter name does not have the value 'groupproduct'");
}
} else {
utils.send400(res, next, "Was expecting a body in the put request.");
}
});
};
modelFlatten.prototype.router = router;
module.exports = modelFlatten;