-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmetaData.test.js
More file actions
320 lines (263 loc) · 11.5 KB
/
metaData.test.js
File metadata and controls
320 lines (263 loc) · 11.5 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const cds = require("@sap/cds");
const { compile: openapi } = require("@cap-js/openapi");
const { compile: asyncapi } = require("@cap-js/asyncapi");
const { getMetadata } = require("../../lib/index");
const cdsc = require("@sap/cds-compiler/lib/main");
jest.mock("@cap-js/openapi", () => ({
compile: jest.fn(),
}));
jest.mock("@cap-js/asyncapi", () => ({
compile: jest.fn(),
}));
describe("metaData", () => {
beforeEach(() => {
jest.clearAllMocks();
});
afterEach(() => {
jest.clearAllMocks();
});
test("getMetadata should return openapi content for a given URL", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.oas3.json";
const expectedResponse = {
contentType: "application/json",
response: "Openapi content",
};
openapi.mockImplementation(() => {
return "Openapi content";
});
const result = await getMetadata(url);
expect(result).toEqual(expectedResponse);
});
test("getMetadata should raise error when get openapi failed", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.oas3.json";
openapi.mockImplementation(() => {
throw new Error("OpenApi error");
});
try {
await getMetadata(url);
} catch (error) {
expect(error.message).toBe("OpenApi error");
}
});
test("getMetadata should return asyncapi content for a given URL", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:eventResource:AdminService:v1/AdminService.asyncapi2.json";
const expectedResponse = {
contentType: "application/json",
response: "Asyncapi content",
};
asyncapi.mockImplementation(() => {
return "Asyncapi content";
});
const result = await getMetadata(url);
expect(result).toEqual(expectedResponse);
});
test("getMetadata should raise error when get asyncapi failed", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:eventResource:AdminService:v1/AdminService.asyncapi2.json";
asyncapi.mockImplementation(() => {
throw new Error("AsyncApi error");
});
try {
await getMetadata(url);
} catch (error) {
expect(error.message).toBe("AsyncApi error");
}
});
test("getMetadata should return csn content for a given URL", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.csn.json";
const expectedResponse = {
contentType: "application/json",
response: "Csn content",
};
jest.spyOn(cdsc.for, "effective").mockImplementation(() => {
return expectedResponse.response;
});
const result = await getMetadata(url, "Csn content");
expect(result).toEqual(expectedResponse);
});
test("getMetadata should raise error when get csn failed", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.csn.json";
jest.spyOn(cdsc.for, "effective").mockImplementation(() => {
throw new Error("Csn error");
});
try {
await getMetadata(url, "");
} catch (error) {
expect(error.message).toContain("Csn error");
}
});
test("getMetadata should return edmx content for a given URL", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:CinemaService:v1/CinemaService.edmx";
const expectedResponse = {
contentType: "application/xml",
response: "Edmx content",
};
jest.spyOn(cds, "compile").mockImplementation(() => {
return {
to: {
edmx: () => "Edmx content",
},
};
});
const result = await getMetadata(url);
expect(result).toEqual(expectedResponse);
});
test("getMetadata should raise error when get edmx failed", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:CinemaService:v1/CinemaService.edmx";
openapi.mockImplementation(() => {
throw new Error("Edmx error");
});
try {
await getMetadata(url);
} catch (error) {
expect(error.message).toBe("Edmx error");
}
});
test("getMetadata should return mcp content for a given URL", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.mcp.json";
const mockMcpServerCard = {
$schema: "https://example.com/mcp-server-card.schema.json",
name: "sap.cds.service/admin",
title: "AdminService",
version: "1.0.0",
description: "Admin service",
tools: [],
};
const expectedResponse = {
contentType: "application/json",
response: JSON.stringify(mockMcpServerCard),
};
jest.spyOn(cds, "compile").mockImplementation(() => ({
to: {
mcp: () => JSON.stringify(mockMcpServerCard),
},
}));
const result = await getMetadata(url);
expect(result).toEqual(expectedResponse);
});
test("getMetadata should return GraphQL SDL content with text/plain content type", async () => {
const url =
"/ord/v1/customer.sample:apiResource:sap.capire.incidents.GraphQLService:v1/sap.capire.incidents.GraphQLService.graphql";
const result = await getMetadata(url);
expect(result.contentType).toBe("text/plain");
expect(typeof result.response).toBe("string");
expect(result.response).toContain("type Query");
});
test("getMetadata should raise error when GraphQL SDL compilation fails", async () => {
const url =
"/ord/v1/customer.sample:apiResource:sap.capire.incidents.GraphQLService:v1/sap.capire.incidents.GraphQLService.graphql";
// Mock cds.linked to return a model that causes generateSchema4 to fail
jest.spyOn(cds, "linked").mockImplementation(() => {
throw new Error("GraphQL schema error");
});
try {
await getMetadata(url);
} catch (error) {
expect(error.message).toBe("GraphQL schema error");
}
});
test("getMetadata should raise error when get mcp failed", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:AdminService:v1/AdminService.mcp.json";
jest.spyOn(cds, "compile").mockImplementation(() => ({
to: {
mcp: () => {
throw new Error("MCP error");
},
},
}));
try {
await getMetadata(url);
} catch (error) {
expect(error.message).toBe("MCP error");
}
});
test("getMetadata should handle missing service name in URL", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource::v1/Service.oas3.json";
openapi.mockImplementation(() => "Mock content");
const result = await getMetadata(url);
expect(result).toEqual({
contentType: "application/json",
response: "Mock content",
});
});
test("getMetadata should handle unknown file extension", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:TestService:v1/TestService.unknown";
try {
await getMetadata(url);
} catch (error) {
expect(error.message).toContain("Unsupported format");
}
});
test("getMetadata should return correct content type for asyncapi", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:eventResource:TestService:v1/TestService.asyncapi2.json";
asyncapi.mockImplementation(() => ({ test: "asyncapi content" }));
const result = await getMetadata(url);
expect(result.contentType).toBe("application/json");
expect(result.response).toEqual({ test: "asyncapi content" });
});
test("getMetadata should handle edmx compilation with correct content type", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:TestService:v1/TestService.edmx";
jest.spyOn(cds, "compile").mockImplementation(() => ({
to: {
edmx: () => "<edmx>content</edmx>",
},
}));
const result = await getMetadata(url);
expect(result.contentType).toBe("application/xml");
expect(result.response).toBe("<edmx>content</edmx>");
});
test("getMetadata should handle complex service names with dots", async () => {
const url =
"/ord/v1/sap.test.cdsrc.sample:apiResource:my.complex.ServiceName:v1/my.complex.ServiceName.oas3.json";
openapi.mockImplementation(() => "Complex service content");
const result = await getMetadata(url);
expect(result.contentType).toBe("application/json");
expect(result.response).toBe("Complex service content");
});
test("getMetadata should handle version variations in URL", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:TestService:v2/TestService.csn.json";
jest.spyOn(cdsc.for, "effective").mockImplementation(() => "Version 2 CSN");
const result = await getMetadata(url, "input csn");
expect(result.contentType).toBe("application/json");
expect(result.response).toBe("Version 2 CSN");
});
test("getMetadata should pass compile options from cds.env", async () => {
const url = "/ord/v1/sap.test.cdsrc.sample:apiResource:TestService:v1/TestService.oas3.json";
const compileOptions = { openapi: { "openapi:path": "my_path" } };
cds.env["ord"] = { compileOptions };
openapi.mockImplementation((csn, options) => {
expect(options["openapi:path"]).toBe("my_path");
return "Content with compile options";
});
const result = await getMetadata(url);
expect(result.contentType).toBe("application/json");
expect(result.response).toBe("Content with compile options");
});
describe("@OpenAPI.servers annotation", () => {
const url = "/ord/v1/ns:apiResource:TestService:v1/TestService.oas3.json";
let capturedOptions;
beforeEach(() => {
capturedOptions = null;
openapi.mockImplementation((csn, options) => {
capturedOptions = options;
return "content";
});
});
test("should pass servers from annotation to openapi compiler", async () => {
const servers = [{ url: "https://api.example.com", description: "Production" }];
const mockCsn = {
definitions: { TestService: { "@OpenAPI.servers": servers } },
};
await getMetadata(url, mockCsn);
expect(capturedOptions["openapi:servers"]).toBe(JSON.stringify(servers));
});
test.each([
["missing", {}],
["empty array", { "@OpenAPI.servers": [] }],
["not an array", { "@OpenAPI.servers": "invalid" }],
])("should not set servers when annotation is %s", async (_, serviceDef) => {
const mockCsn = { definitions: { TestService: serviceDef } };
await getMetadata(url, mockCsn);
expect(capturedOptions["openapi:servers"]).toBeUndefined();
});
});
});