Skip to content

Commit

Permalink
feat: translate cms configuration parameter URL based on type (#385, #…
Browse files Browse the repository at this point in the history
…1223)

* translate ICM shared file system reference to URL in configuration parameter based on parameter type ("ImageFileRef" and "FileRef") instead of naming convention

Co-authored-by: Johannes Metzner <jm@jometzner.de>
  • Loading branch information
tbouliere-datasolution and jometzner authored Aug 1, 2022
1 parent 5183ed4 commit 3957b14
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface ContentConfigurationParameterData {
definitionQualifiedName: string;
value: string | object;
type: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ describe('Content Configuration Parameter Mapper', () => {
key1: {
definitionQualifiedName: 'name1',
value: '1',
type: 'set-id:types.pagelet2-name',
},
key2: {
definitionQualifiedName: 'name2',
value: 'hello',
type: 'set-id:types.pagelet2-name',
},
key3: {
definitionQualifiedName: 'name3',
value: ['hello', 'world'],
type: 'set-id:types.pagelet2-name',
},
};

Expand All @@ -60,26 +63,105 @@ describe('Content Configuration Parameter Mapper', () => {
`);
});

describe('postProcessFileReferences', () => {
it.each([
['assets/pwa/pwa_home_teaser_1.jpg', 'assets/pwa/pwa_home_teaser_1.jpg', 'Image'],
[
'site:/pwa/pwa_home_teaser_1.jpg',
'http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg',
'Image',
],
[
'site:/pwa/pwa_home_teaser_1.jpg',
'http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg',
'ImageXS',
],
['site:/pwa/pwa_home_teaser_1.jpg', 'site:/pwa/pwa_home_teaser_1.jpg', 'Other'],
['site:/video/video.mp4', 'http://www.example.org/static/channel/-/site/de_DE/video/video.mp4', 'Video'],
['https://www.youtube.com/watch?v=ABCDEFG', 'https://www.youtube.com/watch?v=ABCDEFG', 'Video'],
])(`should transform %s to %s for key %s`, (input, expected, key) => {
expect(contentConfigurationParameterMapper.postProcessFileReferences({ [key]: input })).toEqual({
[key]: expected,
});
});
it('should handle bc_pmc:types.pagelet2-ImageFileRef', () => {
const input: { [name: string]: ContentConfigurationParameterData } = {
key1: {
definitionQualifiedName: 'name1',
value: 'assets/pwa/pwa_home_teaser_1.jpg',
type: 'bc_pmc:types.pagelet2-ImageFileRef',
},
key2: {
definitionQualifiedName: 'name2',
value: 'site:/pwa/pwa_home_teaser_1.jpg',
type: 'bc_pmc:types.pagelet2-ImageFileRef',
},
key3: {
definitionQualifiedName: 'name3',
value: 'http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg',
type: 'bc_pmc:types.pagelet2-ImageFileRef',
},
key4: {
definitionQualifiedName: 'name4',
value: 'https://www.youtube.com/watch?v=ABCDEFG',
type: 'bc_pmc:types.pagelet2-ImageFileRef',
},
key5: {
definitionQualifiedName: 'name5',
value: [
'assets/pwa/pwa_home_teaser_1.jpg',
'site:/pwa/pwa_home_teaser_1.jpg',
'http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg',
'https://www.youtube.com/watch?v=ABCDEFG',
],
type: 'bc_pmc:types.pagelet2-ImageFileRef',
},
};

const result = contentConfigurationParameterMapper.fromData(input);
expect(result).toMatchInlineSnapshot(`
Object {
"key1": "assets/pwa/pwa_home_teaser_1.jpg",
"key2": "http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg",
"key3": "http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg",
"key4": "https://www.youtube.com/watch?v=ABCDEFG",
"key5": Array [
"assets/pwa/pwa_home_teaser_1.jpg",
"http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg",
"http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg",
"https://www.youtube.com/watch?v=ABCDEFG",
],
}
`);
});

it('should handle bc_pmc:types.pagelet2-FileRef', () => {
const input: { [name: string]: ContentConfigurationParameterData } = {
key1: {
definitionQualifiedName: 'name1',
value: 'assets/pwa/pwa_home_teaser_1.jpg',
type: 'bc_pmc:types.pagelet2-FileRef',
},
key2: {
definitionQualifiedName: 'name2',
value: 'site:/pwa/pwa_home_teaser_1.jpg',
type: 'bc_pmc:types.pagelet2-FileRef',
},
key3: {
definitionQualifiedName: 'name3',
value: 'http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg',
type: 'bc_pmc:types.pagelet2-FileRef',
},
key4: {
definitionQualifiedName: 'name4',
value: 'https://www.youtube.com/watch?v=ABCDEFG',
type: 'bc_pmc:types.pagelet2-FileRef',
},
key5: {
definitionQualifiedName: 'name5',
value: [
'assets/pwa/pwa_home_teaser_1.jpg',
'site:/pwa/pwa_home_teaser_1.jpg',
'http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg',
'https://www.youtube.com/watch?v=ABCDEFG',
],
type: 'bc_pmc:types.pagelet2-FileRef',
},
};

const result = contentConfigurationParameterMapper.fromData(input);
expect(result).toMatchInlineSnapshot(`
Object {
"key1": "assets/pwa/pwa_home_teaser_1.jpg",
"key2": "http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg",
"key3": "http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg",
"key4": "https://www.youtube.com/watch?v=ABCDEFG",
"key5": Array [
"assets/pwa/pwa_home_teaser_1.jpg",
"http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg",
"http://www.example.org/static/channel/-/site/de_DE/pwa/pwa_home_teaser_1.jpg",
"https://www.youtube.com/watch?v=ABCDEFG",
],
}
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,42 @@ export class ContentConfigurationParameterMapper {

if (data) {
configurationParameters = Object.entries(data)
.map(([key, value]) => ({ [key]: value.value }))
.map(([key, value]) => ({ [key]: this.postProcessData(value) }))
.reduce((acc, val) => ({ ...acc, ...val }));
}

this.postProcessFileReferences(configurationParameters);

return configurationParameters;
}

private resolveStaticURL(value: string): string {
if (value.startsWith('http')) {
return value;
}

if (!value.includes(':/')) {
return value;
}

const split = value.split(':');

return encodeURI(`${this.staticURL}/${split[0]}/${this.lang}${split[1]}`);
}

/**
* TODO: make this dependant on the type of the configuration parameter once the CMS REST API provides this information
* for now filter all configuration parameters that start with 'Image' or 'Video'
* and where the value does not start with 'http' but contains ':/'
+ if the filter matches convert the CMS REST API value in a full server URL to the configured file
* TODO: Make this method use name-based plugin mechanism to delegate post processing of
* configuration parameter data to specific handler.
*/
postProcessFileReferences(data: ContentConfigurationParameters): ContentConfigurationParameters {
Object.keys(data)
.filter(
key =>
(key.startsWith('Image') || key.startsWith('Video')) &&
data[key] &&
!data[key].toString().startsWith('http') &&
data[key].toString().includes(':/')
)
.forEach(key => {
const split = data[key].toString().split(':');
data[key] = encodeURI(`${this.staticURL}/${split[0]}/${this.lang}${split[1]}`);
});
return data;
private postProcessData(data: ContentConfigurationParameterData): string | object | number {
switch (data.type) {
case 'bc_pmc:types.pagelet2-ImageFileRef':
case 'bc_pmc:types.pagelet2-FileRef':
if (Array.isArray(data.value)) {
return data.value.map(x => this.resolveStaticURL(x));
} else {
return this.resolveStaticURL(data.value.toString());
}
default:
return data.value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('Content Pagelet Entry Point Mapper', () => {
key1: {
definitionQualifiedName: 'fq',
value: 'hallo 1',
type: 'set-id:types.pagelet2-name',
},
},
slots: {
Expand All @@ -56,6 +57,7 @@ describe('Content Pagelet Entry Point Mapper', () => {
key11: {
definitionQualifiedName: 'fq',
value: 'test',
type: 'set-id:types.pagelet2-name',
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('Content Pagelet Mapper', () => {
key: {
definitionQualifiedName: 'quali',
value: 'test',
type: 'set-id:types.pagelet2-name',
},
},
};
Expand All @@ -53,6 +54,7 @@ describe('Content Pagelet Mapper', () => {
key: {
definitionQualifiedName: 'quali',
value: 'test',
type: 'set-id:types.pagelet2-name',
},
},
slots: {
Expand All @@ -78,6 +80,7 @@ describe('Content Pagelet Mapper', () => {
key: {
definitionQualifiedName: 'quali',
value: 'test',
type: 'set-id:types.pagelet2-name',
},
},
slots: {
Expand Down Expand Up @@ -106,6 +109,7 @@ describe('Content Pagelet Mapper', () => {
key1: {
definitionQualifiedName: 'fq',
value: 'test-key1',
type: 'set-id:types.pagelet2-name',
},
},
slots: {
Expand All @@ -122,6 +126,7 @@ describe('Content Pagelet Mapper', () => {
key3: {
definitionQualifiedName: 'fq',
value: '1',
type: 'set-id:types.pagelet2-name',
},
},
},
Expand All @@ -134,10 +139,12 @@ describe('Content Pagelet Mapper', () => {
key1: {
definitionQualifiedName: 'name1',
value: 'hallo',
type: 'set-id:types.pagelet2-name',
},
key2: {
definitionQualifiedName: 'name2',
value: ['hallo', 'welt'],
type: 'set-id:types.pagelet2-name',
},
},
},
Expand Down Expand Up @@ -166,6 +173,7 @@ describe('Content Pagelet Mapper', () => {
Image: {
value: 'inSPIRED-inTRONICS-b2c-responsive:/brands/adata.jpg',
definitionQualifiedName: 'app_sf_base_cm:component.common.image.pagelet2-Component-Image',
type: 'bc_pmc:types.pagelet2-ImageFileRef',
},
},
} as ContentPageletData;
Expand Down

0 comments on commit 3957b14

Please sign in to comment.