|
| 1 | +/** |
| 2 | + * Unit tests for LDMClient composition pattern. |
| 3 | + * LDMClient receives an OSLCClient instance instead of extending it. |
| 4 | + */ |
| 5 | +import { jest } from '@jest/globals'; |
| 6 | + |
| 7 | +// Mock rdflib to avoid heavy dependency in unit tests |
| 8 | +jest.unstable_mockModule('rdflib', () => { |
| 9 | + const mockStore = { |
| 10 | + statementsMatching: jest.fn(() => []), |
| 11 | + }; |
| 12 | + return { |
| 13 | + graph: jest.fn(() => mockStore), |
| 14 | + parse: jest.fn(), |
| 15 | + default: { graph: jest.fn(() => mockStore), parse: jest.fn() }, |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +const { default: LDMClient } = await import('../LDMClient.js'); |
| 20 | + |
| 21 | +/** |
| 22 | + * Build a fake OSLCClient-like object with the properties LDMClient needs. |
| 23 | + */ |
| 24 | +function makeFakeOslcClient(overrides = {}) { |
| 25 | + return { |
| 26 | + userid: 'testuser', |
| 27 | + password: 'testpass', |
| 28 | + configuration_context: 'https://server/gc/configuration/1', |
| 29 | + client: { |
| 30 | + post: jest.fn().mockResolvedValue({ data: { queryResults: [] }, headers: {} }), |
| 31 | + get: jest.fn().mockResolvedValue({ data: '', headers: {} }), |
| 32 | + defaults: { headers: { common: {} } }, |
| 33 | + }, |
| 34 | + ...overrides, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +describe('LDMClient composition pattern', () => { |
| 39 | + describe('constructor', () => { |
| 40 | + it('accepts an OSLCClient-like object and ldmBaseUrl', () => { |
| 41 | + const oslcClient = makeFakeOslcClient(); |
| 42 | + const ldm = new LDMClient(oslcClient, 'https://server/ldm'); |
| 43 | + expect(ldm).toBeDefined(); |
| 44 | + expect(ldm.oslcClient).toBe(oslcClient); |
| 45 | + }); |
| 46 | + |
| 47 | + it('throws if ldmBaseUrl is missing', () => { |
| 48 | + const oslcClient = makeFakeOslcClient(); |
| 49 | + expect(() => new LDMClient(oslcClient)).toThrow('LDMServerBaseURL is required'); |
| 50 | + expect(() => new LDMClient(oslcClient, '')).toThrow('LDMServerBaseURL is required'); |
| 51 | + expect(() => new LDMClient(oslcClient, null)).toThrow('LDMServerBaseURL is required'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('normalizes trailing slash from ldmBaseUrl', () => { |
| 55 | + const oslcClient = makeFakeOslcClient(); |
| 56 | + const ldm = new LDMClient(oslcClient, 'https://server/ldm/'); |
| 57 | + expect(ldm.LDMServerBaseURL).toBe('https://server/ldm'); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('uses OSLCClient axios instance', () => { |
| 62 | + it('uses oslcClient.client for LQE requests', async () => { |
| 63 | + const oslcClient = makeFakeOslcClient(); |
| 64 | + oslcClient.client.post.mockResolvedValue({ |
| 65 | + data: { queryResults: [] }, |
| 66 | + headers: { 'content-type': 'application/json' }, |
| 67 | + }); |
| 68 | + |
| 69 | + const ldm = new LDMClient(oslcClient, 'https://server/lqe'); |
| 70 | + await ldm.getIncomingLinks(['https://server/rm/resources/1']); |
| 71 | + |
| 72 | + expect(oslcClient.client.post).toHaveBeenCalled(); |
| 73 | + const [url] = oslcClient.client.post.mock.calls[0]; |
| 74 | + expect(url).toBe('https://server/lqe/incoming-links'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('uses oslcClient.client for LDM requests', async () => { |
| 78 | + const oslcClient = makeFakeOslcClient(); |
| 79 | + oslcClient.client.post.mockResolvedValue({ |
| 80 | + data: '@prefix : <http://example.org/> .', |
| 81 | + headers: { 'content-type': 'text/turtle' }, |
| 82 | + }); |
| 83 | + |
| 84 | + const ldm = new LDMClient(oslcClient, 'https://server/ldm'); |
| 85 | + await ldm.getIncomingLinks(['https://server/rm/resources/1']); |
| 86 | + |
| 87 | + expect(oslcClient.client.post).toHaveBeenCalled(); |
| 88 | + const [url] = oslcClient.client.post.mock.calls[0]; |
| 89 | + expect(url).toBe('https://server/ldm/discover-links'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('getIncomingLinks', () => { |
| 94 | + it('calls the LQE endpoint when base URL contains /lqe', async () => { |
| 95 | + const oslcClient = makeFakeOslcClient(); |
| 96 | + oslcClient.client.post.mockResolvedValue({ |
| 97 | + data: { queryResults: [{ sourceUrl: 'https://s', linkType: 'https://lt', targetUrl: 'https://t' }] }, |
| 98 | + headers: { 'content-type': 'application/json' }, |
| 99 | + }); |
| 100 | + |
| 101 | + const ldm = new LDMClient(oslcClient, 'https://server/lqe'); |
| 102 | + const results = await ldm.getIncomingLinks(['https://server/rm/resources/1']); |
| 103 | + |
| 104 | + expect(results).toEqual([{ sourceURL: 'https://s', linkType: 'https://lt', targetURL: 'https://t' }]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('passes configuration_context from oslcClient when not overridden', async () => { |
| 108 | + const oslcClient = makeFakeOslcClient({ configuration_context: 'https://server/gc/config/42' }); |
| 109 | + oslcClient.client.post.mockResolvedValue({ |
| 110 | + data: { queryResults: [] }, |
| 111 | + headers: { 'content-type': 'application/json' }, |
| 112 | + }); |
| 113 | + |
| 114 | + const ldm = new LDMClient(oslcClient, 'https://server/lqe'); |
| 115 | + await ldm.getIncomingLinks(['https://server/rm/resources/1']); |
| 116 | + |
| 117 | + const [, body] = oslcClient.client.post.mock.calls[0]; |
| 118 | + expect(body).toContain('oslc_config.context=https'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('throws on empty targetResourceURLs', async () => { |
| 122 | + const ldm = new LDMClient(makeFakeOslcClient(), 'https://server/lqe'); |
| 123 | + await expect(ldm.getIncomingLinks([])).rejects.toThrow('targetResourceURLs must be a non-empty array'); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe('invert', () => { |
| 128 | + it('maps link types to their inverses', () => { |
| 129 | + const ldm = new LDMClient(makeFakeOslcClient(), 'https://server/lqe'); |
| 130 | + const result = ldm.invert([{ |
| 131 | + sourceURL: 'https://s', |
| 132 | + linkType: 'http://open-services.net/ns/rm#elaborates', |
| 133 | + targetURL: 'https://t', |
| 134 | + }]); |
| 135 | + |
| 136 | + expect(result).toEqual([{ |
| 137 | + targetURL: 'https://t', |
| 138 | + inverseLinkType: 'http://open-services.net/ns/rm#elaboratedBy', |
| 139 | + sourceURL: 'https://s', |
| 140 | + }]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('returns original link type when no inverse mapping exists', () => { |
| 144 | + const ldm = new LDMClient(makeFakeOslcClient(), 'https://server/lqe'); |
| 145 | + const result = ldm.invert([{ |
| 146 | + sourceURL: 'https://s', |
| 147 | + linkType: 'http://example.org/unknownLink', |
| 148 | + targetURL: 'https://t', |
| 149 | + }]); |
| 150 | + |
| 151 | + expect(result[0].inverseLinkType).toBe('http://example.org/unknownLink'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('handles symmetric link types', () => { |
| 155 | + const ldm = new LDMClient(makeFakeOslcClient(), 'https://server/lqe'); |
| 156 | + const result = ldm.invert([{ |
| 157 | + sourceURL: 'https://s', |
| 158 | + linkType: 'http://open-services.net/ns/core#related', |
| 159 | + targetURL: 'https://t', |
| 160 | + }]); |
| 161 | + |
| 162 | + expect(result[0].inverseLinkType).toBe('http://open-services.net/ns/core#related'); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments