Skip to content

Commit 6dc4f51

Browse files
committed
Backstage plugin test cases
Signed-off-by: Ali Ok <aliok@redhat.com>
1 parent d129b82 commit 6dc4f51

File tree

2 files changed

+210
-3
lines changed

2 files changed

+210
-3
lines changed

backstage/plugins/knative-event-mesh-backend/src/providers/knativeEventMeshProvider.test.ts

Lines changed: 208 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {getVoidLogger} from '@backstage/backend-common';
22
import {ApiEntity, ComponentEntity} from "@backstage/catalog-model";
33

4-
import {Broker, EventType, KnativeEventMeshProvider} from "./knativeEventMeshProvider";
4+
import {Broker, EventType, KnativeEventMeshProvider, Source, Subscribable} from "./knativeEventMeshProvider";
55
import {KnativeEventMeshProviderConfig} from "./types";
66

77
describe('KnativeEventMeshProvider', () => {
@@ -222,4 +222,211 @@ describe('KnativeEventMeshProvider', () => {
222222

223223
}
224224
});
225+
226+
describe('buildSubscribableEntity', () => {
227+
const providerConfig:KnativeEventMeshProviderConfig = {
228+
id: 'test',
229+
baseUrl: 'http://example.com',
230+
schedule: undefined,
231+
};
232+
233+
const provider = new KnativeEventMeshProvider(providerConfig, logger, (null as any));
234+
235+
type TestCase = {
236+
name:string;
237+
input:Subscribable;
238+
expected:ComponentEntity;
239+
};
240+
241+
const testCases:TestCase[] = [
242+
{
243+
name: 'minimal information',
244+
input: {
245+
name: 'test',
246+
namespace: 'test',
247+
uid: 'test',
248+
kind: 'test-kind',
249+
group: 'test-group',
250+
},
251+
expected: {
252+
apiVersion: 'backstage.io/v1alpha1',
253+
kind: 'Component',
254+
metadata: {
255+
name: 'test',
256+
namespace: 'test',
257+
annotations: {
258+
"backstage.io/managed-by-location": "url:http://example.com",
259+
"backstage.io/managed-by-origin-location": "url:http://example.com",
260+
},
261+
labels: {},
262+
tags: [],
263+
},
264+
spec: {
265+
type: 'test-group:test-kind',
266+
owner: 'knative',
267+
system: 'knative-event-mesh',
268+
lifecycle: 'test',
269+
providesApis: [],
270+
},
271+
},
272+
},
273+
{
274+
name: 'all information',
275+
input: {
276+
name: 'test-subscribable',
277+
namespace: 'test-ns',
278+
uid: 'test-uid',
279+
labels: {
280+
"test-label": "test-label-value",
281+
},
282+
annotations: {
283+
"test-annotation": "test-annotation-value",
284+
},
285+
providedEventTypes: [
286+
"test-ns/test-type",
287+
],
288+
kind: "test-kind",
289+
group: "test-group",
290+
},
291+
expected: {
292+
apiVersion: 'backstage.io/v1alpha1',
293+
kind: 'Component',
294+
metadata: {
295+
name: 'test-subscribable',
296+
namespace: 'test-ns',
297+
annotations: {
298+
"test-annotation": "test-annotation-value",
299+
"backstage.io/managed-by-location": "url:http://example.com",
300+
"backstage.io/managed-by-origin-location": "url:http://example.com",
301+
},
302+
labels: {
303+
"test-label": "test-label-value",
304+
},
305+
tags: [],
306+
},
307+
spec: {
308+
type: 'test-group:test-kind',
309+
owner: 'knative',
310+
system: 'knative-event-mesh',
311+
lifecycle: 'test',
312+
providesApis: [
313+
"api:test-ns/test-type"
314+
],
315+
},
316+
},
317+
}
318+
];
319+
320+
for (const testCase of testCases) {
321+
test(`Name: ${testCase.name}`, async () => {
322+
const result = provider.buildSubscribableEntity(testCase.input);
323+
expect(result).toEqual(testCase.expected);
324+
});
325+
326+
}
327+
});
328+
329+
describe('buildSourceEntity', () => {
330+
const providerConfig:KnativeEventMeshProviderConfig = {
331+
id: 'test',
332+
baseUrl: 'http://example.com',
333+
schedule: undefined,
334+
};
335+
336+
const provider = new KnativeEventMeshProvider(providerConfig, logger, (null as any));
337+
338+
type TestCase = {
339+
name:string;
340+
input:Source;
341+
expected:ComponentEntity;
342+
};
343+
344+
const testCases:TestCase[] = [
345+
{
346+
name: 'minimal information',
347+
input: {
348+
name: 'test',
349+
namespace: 'test',
350+
uid: 'test',
351+
group: 'test-group',
352+
kind: 'test-kind',
353+
},
354+
expected: {
355+
apiVersion: 'backstage.io/v1alpha1',
356+
kind: 'Component',
357+
metadata: {
358+
name: 'test',
359+
namespace: 'test',
360+
annotations: {
361+
"backstage.io/managed-by-location": "url:http://example.com",
362+
"backstage.io/managed-by-origin-location": "url:http://example.com",
363+
},
364+
labels: {},
365+
tags: [],
366+
},
367+
spec: {
368+
type: 'test-group:test-kind',
369+
owner: 'knative',
370+
system: 'knative-event-mesh',
371+
lifecycle: 'test',
372+
providesApis: [],
373+
},
374+
},
375+
},
376+
{
377+
name: 'all information',
378+
input: {
379+
name: 'test-source',
380+
namespace: 'test-ns',
381+
uid: 'test-uid',
382+
labels: {
383+
"test-label": "test-label-value",
384+
},
385+
annotations: {
386+
"test-annotation": "test-annotation-value",
387+
},
388+
providedEventTypes: [
389+
"test-ns/test-type",
390+
],
391+
group: "test-group",
392+
kind: "test-kind",
393+
},
394+
expected: {
395+
apiVersion: 'backstage.io/v1alpha1',
396+
kind: 'Component',
397+
metadata: {
398+
name: 'test-source',
399+
namespace: 'test-ns',
400+
annotations: {
401+
"test-annotation": "test-annotation-value",
402+
"backstage.io/managed-by-location": "url:http://example.com",
403+
"backstage.io/managed-by-origin-location": "url:http://example.com",
404+
},
405+
labels: {
406+
"test-label": "test-label-value",
407+
},
408+
tags: [],
409+
},
410+
spec: {
411+
type: 'test-group:test-kind',
412+
owner: 'knative',
413+
system: 'knative-event-mesh',
414+
lifecycle: 'test',
415+
providesApis: [
416+
"api:test-ns/test-type"
417+
],
418+
},
419+
},
420+
}
421+
];
422+
423+
for (const testCase of testCases) {
424+
test(`Name: ${testCase.name}`, async () => {
425+
const result = provider.buildSourceEntity(testCase.input);
426+
expect(result).toEqual(testCase.expected);
427+
});
428+
429+
}
430+
});
431+
225432
});

backstage/plugins/knative-event-mesh-backend/src/providers/knativeEventMeshProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ export class KnativeEventMeshProvider implements EntityProvider {
304304
}
305305
}
306306

307-
private buildSubscribableEntity(subscribable:Subscribable) {
307+
buildSubscribableEntity(subscribable:Subscribable) {
308308
const annotations = subscribable.annotations ?? {} as Record<string, string>;
309309
annotations[ANNOTATION_ORIGIN_LOCATION] = annotations[ANNOTATION_LOCATION] = `url:${this.baseUrl}`;
310310

@@ -329,7 +329,7 @@ export class KnativeEventMeshProvider implements EntityProvider {
329329
}
330330
}
331331

332-
private buildSourceEntity(source:Source) {
332+
buildSourceEntity(source:Source) {
333333
const annotations = source.annotations ?? {} as Record<string, string>;
334334
annotations[ANNOTATION_ORIGIN_LOCATION] = annotations[ANNOTATION_LOCATION] = `url:${this.baseUrl}`;
335335

0 commit comments

Comments
 (0)