Skip to content

Commit

Permalink
Make the list DIDs pipeline element discard results with errors and c…
Browse files Browse the repository at this point in the history
…over this by a test
  • Loading branch information
MytsV authored and maany committed Nov 26, 2024
1 parent a1508f1 commit 80d3321
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/lib/core/use-case/list-dids/pipeline-element-get-did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ export default class GetDIDsPipelineElement extends BaseStreamingPostProcessingP
};
}

transformResponseModel(responseModel: ListDIDsResponse, dto: DIDExtendedDTO): ListDIDsResponse {
transformResponseModel(responseModel: ListDIDsResponse, dto: DIDExtendedDTO): ListDIDsResponse | ListDIDsError {
if (dto.status === 'error')
return {
status: 'error',
name: '',
message: '',
error: '',
code: 500,
};
responseModel.bytes = dto.bytes;
responseModel.length = dto.length;
responseModel.did_type = dto.did_type;
Expand Down
124 changes: 119 additions & 5 deletions test/api/did/list-dids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import MockRucioServerFactory, { MockEndpoint } from 'test/fixtures/rucio-server
describe('DID API Tests', () => {
beforeEach(() => {
fetchMock.doMock();
});
afterEach(() => {
fetchMock.dontMock();
});

it('Should successfully stream DIDs', async () => {
const listDIDsEndpoint: MockEndpoint = {
url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dids/search`,
method: 'GET',
Expand Down Expand Up @@ -96,12 +102,7 @@ describe('DID API Tests', () => {
dataset2StatusEndpoint,
dataset3StatusEndpoint,
]);
});
afterEach(() => {
fetchMock.dontMock();
});

it('Should successfully stream DIDs', async () => {
const res = MockHttpStreamableResponseFactory.getMockResponse();
const listDIDsController = appContainer.get<BaseController<ListDIDsControllerParameters, ListDIDsRequest>>(CONTROLLERS.LIST_DIDS);
const controllerParams: ListDIDsControllerParameters = {
Expand Down Expand Up @@ -161,4 +162,117 @@ describe('DID API Tests', () => {
},
]);
});

it('Should output an error for DIDs which status cannot be retrieved', async () => {
fetchMock.doMock();

const listDIDsEndpoint: MockEndpoint = {
url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dids/search`,
method: 'GET',
includes: 'test/dids/search',
response: {
status: 200,
headers: {
'Content-Type': 'application/x-json-stream',
},
body: Readable.from(['"dataset1"\n', '"dataset2"\n', '"dataset3"\n'].join('\n')),
},
};

const dataset1StatusEndpoint: MockEndpoint = {
url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dataset1/status?dynamic_depth=FILE`,
method: 'GET',
response: {
status: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
scope: 'test',
name: 'dataset1',
type: 'DATASET',
account: 'root',
open: true,
monotonic: false,
expired_at: null,
length: 0,
bytes: 0,
}),
},
};

const dataset3StatusEndpoint: MockEndpoint = {
url: `${MockRucioServerFactory.RUCIO_HOST}/dids/test/dataset3/status?dynamic_depth=FILE`,
method: 'GET',
response: {
status: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
scope: 'test',
name: 'dataset3',
type: 'DATASET',
account: 'root',
open: true,
monotonic: false,
expired_at: null,
bytes: 456,
length: 789,
}),
},
};

MockRucioServerFactory.createMockRucioServer(true, [listDIDsEndpoint, dataset1StatusEndpoint, dataset3StatusEndpoint]);

const res = MockHttpStreamableResponseFactory.getMockResponse();
const listDIDsController = appContainer.get<BaseController<ListDIDsControllerParameters, ListDIDsRequest>>(CONTROLLERS.LIST_DIDS);
const controllerParams: ListDIDsControllerParameters = {
response: res as unknown as NextApiResponse,
rucioAuthToken: MockRucioServerFactory.VALID_RUCIO_TOKEN,
query: 'test:dataset1',
type: 'dataset',
};
await listDIDsController.execute(controllerParams);

const receivedData: any[] = [];
const onData = (data: any) => {
receivedData.push(JSON.parse(data));
};

const done = new Promise<void>((resolve, reject) => {
res.on('data', onData);
res.on('end', () => {
res.off('data', onData);
resolve();
});
res.on('error', err => {
res.off('data', onData);
reject(err);
});
});

await done;

expect(receivedData.length).toEqual(3);
expect(receivedData[0]).toEqual({
status: 'success',
name: 'dataset1',
scope: 'test',
did_type: 'Dataset',
bytes: 0,
length: 0,
open: true,
});
expect(receivedData[1].status).toEqual('error');
expect(receivedData[2]).toEqual({
status: 'success',
name: 'dataset3',
scope: 'test',
did_type: 'Dataset',
bytes: 456,
length: 789,
open: true,
});
});
});

0 comments on commit 80d3321

Please sign in to comment.