Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/trit 582 implement sdk compatibility for custom sorts #108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions src/lib/ContentClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import * as V1_SINGLE_RESULT from './content/coordinators/__fixtures__/v1/SINGLE
import * as V2_SINGLE_RESULT from './content/coordinators/__fixtures__/v2/SINGLE_RESULT.json';
import * as NO_RESULTS from './content/coordinators/__fixtures__/v2/filterBy/NO_RESULTS.json';
import * as MULTI_LAYER_RESPONSE from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESPONSE.json';
import * as MULTI_LAYER_RESPONSE_ALT_SORT from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESPONSE_ALT_SORT.json';
import * as MULTI_LAYER_RESPONSE_DESC from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESPONSE_DESC.json';
import * as MULTI_LAYER_RESULT_DESC from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESULT_DESC.json';
import * as MULTI_LAYER_RESULT_FILTERED from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESULT_FILTERED.json';
import * as MULTI_LAYER_RESULT_FILTERED_AND_MUTATED from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESULT_FILTER_AND_MUTATE.json';
import * as MULTI_LAYER_RESULT_MUTATED from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESULT_MUTATED.json';
import * as MULTI_LAYER_RESULT from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESULT.json';
import * as MULTI_LAYER_RESULT_ALT_SORT from './content/coordinators/__fixtures__/v2/hierarchies/MULTI_LAYER_RESULT_ALT_SORT.json';
import * as ROOT from './content/coordinators/__fixtures__/v2/hierarchies/ROOT.json';

import { ContentClientConfigV1 } from './config/ContentClientConfigV1';
Expand Down Expand Up @@ -477,6 +481,104 @@ describe('ContentClient', () => {
expect(response).to.deep.eq(JSON.parse(JSON.stringify(expectedContent)));
});

it(`getByHierarchy should sort correctly`, async () => {
const urlBuilder = new HierarchyURLBuilder();

const mocks = new MockAdapter(null);

const expectedBody: DefaultContentBody = {
_meta: new ContentMeta(MULTI_LAYER_RESULT_DESC.content._meta),
propertyName1: MULTI_LAYER_RESULT_DESC.content.propertyName1,
};

const expectedContent: HierarchyContentItem<DefaultContentBody> = {
content: expectedBody,
children: MULTI_LAYER_RESULT_DESC.children as any,
};

const cd2RunConfig = {
name: 'cdv2',
hubName: 'hub',
type: 'cdn',
baseUrl: 'https://hub.cdn.content.amplience.net',
config: { hubName: 'hub' },
} as ContentClientConfigV2;

mocks
.onGet(
'https://hub.cdn.content.amplience.net/content/id/90d6fa96-6ce0-4332-b995-4e6c50b1e233?depth=all&format=inlined'
)
.reply(200, ROOT);

mocks
.onGet(
cd2RunConfig.baseUrl +
urlBuilder.buildUrl({
rootId: ROOT.content._meta.deliveryId,
sortOrder: 'DESC',
})
)
.reply(200, MULTI_LAYER_RESPONSE_DESC);

const mergedConfig = { adaptor: mocks.adapter(), ...cd2RunConfig };

const client = new ContentClient(mergedConfig);
const response = await client.getByHierarchy({
rootId: ROOT.content._meta.deliveryId,
sortOrder: 'DESC',
});
expect(response).to.deep.eq(JSON.parse(JSON.stringify(expectedContent)));
});

it(`getByHierarchy should apply custom sorts`, async () => {
const urlBuilder = new HierarchyURLBuilder();

const mocks = new MockAdapter(null);

const expectedBody: DefaultContentBody = {
_meta: new ContentMeta(MULTI_LAYER_RESULT_ALT_SORT.content._meta),
propertyName1: MULTI_LAYER_RESULT_ALT_SORT.content.propertyName1,
};

const expectedContent: HierarchyContentItem<DefaultContentBody> = {
content: expectedBody,
children: MULTI_LAYER_RESULT_ALT_SORT.children as any,
};

const cd2RunConfig = {
name: 'cdv2',
hubName: 'hub',
type: 'cdn',
baseUrl: 'https://hub.cdn.content.amplience.net',
config: { hubName: 'hub' },
} as ContentClientConfigV2;

mocks
.onGet(
'https://hub.cdn.content.amplience.net/content/id/90d6fa96-6ce0-4332-b995-4e6c50b1e233?depth=all&format=inlined'
)
.reply(200, ROOT);

mocks
.onGet(
cd2RunConfig.baseUrl +
urlBuilder.buildUrl({
rootId: ROOT.content._meta.deliveryId,
sortKey: '_meta/deliveryId',
})
)
.reply(200, MULTI_LAYER_RESPONSE_ALT_SORT);

const mergedConfig = { adaptor: mocks.adapter(), ...cd2RunConfig };

const client = new ContentClient(mergedConfig);
const response = await client.getByHierarchy({
rootId: ROOT.content._meta.deliveryId,
sortKey: '_meta/deliveryId',
});
expect(response).to.deep.eq(JSON.parse(JSON.stringify(expectedContent)));
});

it(`getByHierarchy should apply filter when building a tree with a filter`, async () => {
const urlBuilder = new HierarchyURLBuilder();

Expand Down
8 changes: 8 additions & 0 deletions src/lib/ContentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ export class ContentClient implements GetContentItemById, GetContentItemByKey {
rootId: rootItem.body._meta.deliveryId,
maximumDepth: requestParameters.maximumDepth,
maximumPageSize: requestParameters.maximumPageSize,
sortOrder: requestParameters.sortOrder,
sortKey: requestParameters.sortKey,
},
rootItem
);
Expand All @@ -318,6 +320,8 @@ export class ContentClient implements GetContentItemById, GetContentItemByKey {
rootId: rootItem.body._meta.deliveryId,
maximumDepth: requestParameters.maximumDepth,
maximumPageSize: requestParameters.maximumPageSize,
sortOrder: requestParameters.sortOrder,
sortKey: requestParameters.sortKey,
},
rootItem
);
Expand All @@ -340,6 +344,8 @@ export class ContentClient implements GetContentItemById, GetContentItemByKey {
rootId: rootItem.body._meta.deliveryId,
maximumDepth: requestParameters.maximumDepth,
maximumPageSize: requestParameters.maximumPageSize,
sortOrder: requestParameters.sortOrder,
sortKey: requestParameters.sortKey,
},
rootItem
);
Expand All @@ -364,6 +370,8 @@ export class ContentClient implements GetContentItemById, GetContentItemByKey {
rootId: rootItem.body._meta.deliveryId,
maximumDepth: requestParameters.maximumDepth,
maximumPageSize: requestParameters.maximumPageSize,
sortOrder: requestParameters.sortOrder,
sortKey: requestParameters.sortKey,
},
rootItem
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ describe('getByHierarchies', () => {
'/content/hierarchies/descendants/id/testId?hierarchyDepth=3&maxPageSize=5&pageCursor=LEKEY'
);
});

it('Url should be constructed properly without max depth', () => {
const hierachyURL: HierarchyURLBuilder = new HierarchyURLBuilder();
const request: HierarchyRequest = {
Expand Down Expand Up @@ -118,6 +117,28 @@ describe('getByHierarchies', () => {
'/content/hierarchies/descendants/id/testId?pageCursor=LEKEY'
);
});
it('Url should be constructed properly with sort key', () => {
const hierachyURL: HierarchyURLBuilder = new HierarchyURLBuilder();
const request: HierarchyRequest = {
rootId: 'testId',
sortKey: '_meta/JSON/ID',
};
const result = hierachyURL.buildUrl(request);
expect(result).to.deep.eq(
'/content/hierarchies/descendants/id/testId?sortByKey=_meta%2FJSON%2FID'
);
});
it('Url should be constructed properly with sort order', () => {
const hierachyURL: HierarchyURLBuilder = new HierarchyURLBuilder();
const request: HierarchyRequest = {
rootId: 'testId',
sortOrder: 'DESC',
};
const result = hierachyURL.buildUrl(request);
expect(result).to.deep.eq(
'/content/hierarchies/descendants/id/testId?sortByOrder=DESC'
);
});
});

describe('Should correctly retrieve and build flat hierarchies', () => {
Expand Down
9 changes: 8 additions & 1 deletion src/lib/content/coordinators/GetByHierarchy/UrlBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export class HierarchyURLBuilder {
static MAXIMUM_DEPTH_PARAM = 'hierarchyDepth';
static MAXIMUM_PAGE_SIZE_PARAM = 'maxPageSize';
static LAST_EVALUATED_PARAM = 'pageCursor';

static SORT_KEY_PARAM = 'sortByKey';
static SORT_ORDER_PARAM = 'sortByOrder';
buildUrl(request: HierarchyRequest): string {
const params: string[][] = [];

Expand All @@ -28,6 +29,12 @@ export class HierarchyURLBuilder {
request.pageCursor.toString(),
]);
}
if (request.sortKey !== undefined) {
params.push([HierarchyURLBuilder.SORT_KEY_PARAM, request.sortKey]);
}
if (request.sortOrder !== undefined) {
params.push([HierarchyURLBuilder.SORT_ORDER_PARAM, request.sortOrder]);
}
if (params.length > 0) {
return `${HierarchyURLBuilder.HIERARCHY_URL}${
request.rootId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"responses": [
{
"content": {
"_meta": {
"name": "C",
"schema": "https://hierarchies.com",
"hierarchy": {
"parentId": "1ebab07d-acd8-4e19-a614-ec632cdf95d5",
"root": false
},
"deliveryId": "68676cda-5ab2-4a5e-bcfb-58c5cf3eb8ed"
},
"propertyName1": "C"
}
},
{
"content": {
"_meta": {
"name": "A",
"schema": "https://hierarchies.com",
"hierarchy": {
"parentId": "90d6fa96-6ce0-4332-b995-4e6c50b1e233",
"root": false
},
"deliveryId": "1ebab07d-acd8-4e19-a614-ec632cdf95d5"
},
"propertyName1": "A"
}
}
],
"page": {
"responseCount": 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"responses": [
{
"content": {
"_meta": {
"name": "C",
"schema": "https://hierarchies.com",
"hierarchy": {
"parentId": "1ebab07d-acd8-4e19-a614-ec632cdf95d5",
"root": false
},
"deliveryId": "68676cda-5ab2-4a5e-bcfb-58c5cf3eb8ed"
},
"propertyName1": "C"
}
},
{
"content": {
"_meta": {
"name": "B",
"schema": "https://hierarchies.com",
"hierarchy": {
"parentId": "1ebab07d-acd8-4e19-a614-ec632cdf95d5",
"root": false
},
"deliveryId": "4db37251-0c86-4f45-ad8a-583ebf6efc80"
},
"propertyName1": "B"
}
},
{
"content": {
"_meta": {
"name": "A",
"schema": "https://hierarchies.com",
"hierarchy": {
"parentId": "90d6fa96-6ce0-4332-b995-4e6c50b1e233",
"root": false
},
"deliveryId": "1ebab07d-acd8-4e19-a614-ec632cdf95d5"
},
"propertyName1": "A"
}
}
],
"page": {
"responseCount": 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"content": {
"_meta": {
"name": "Root",
"schema": "https://hierarchies.com",
"hierarchy": {
"root": true
},
"deliveryId": "90d6fa96-6ce0-4332-b995-4e6c50b1e233"
},
"propertyName1": "Root"
},
"children": [
{
"content": {
"_meta": {
"name": "A",
"schema": "https://hierarchies.com",
"hierarchy": {
"parentId": "90d6fa96-6ce0-4332-b995-4e6c50b1e233",
"root": false
},
"deliveryId": "1ebab07d-acd8-4e19-a614-ec632cdf95d5"
},
"propertyName1": "A"
},
"children": [

{
"content": {
"_meta": {
"name": "C",
"schema": "https://hierarchies.com",
"hierarchy": {
"parentId": "1ebab07d-acd8-4e19-a614-ec632cdf95d5",
"root": false
},
"deliveryId": "68676cda-5ab2-4a5e-bcfb-58c5cf3eb8ed"
},
"propertyName1": "C"
},
"children": []
}
]
}
]
}
Loading
Loading