Skip to content

Commit 3ab6f0d

Browse files
authored
SDK-2587 Add "optional" and "alternative_names" for share v1 (#516)
Updated in dynamic_sharing_service the policy wanted.attribute.builder and wanted.attribute to include the "optional" property and the "alternative_names"
1 parent 8feef9c commit 3ab6f0d

File tree

5 files changed

+166
-3
lines changed

5 files changed

+166
-3
lines changed

src/dynamic_sharing_service/policy/wanted.attribute.builder.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ module.exports = class WantedAttributeBuilder {
4242
return this;
4343
}
4444

45+
/**
46+
* @param {string} alternativeName
47+
*/
48+
withAlternativeName(alternativeName) {
49+
this.alternativeNames = [...(this.alternativeNames || []), alternativeName];
50+
return this;
51+
}
52+
53+
/**
54+
* @param {string[]} alternativeNames
55+
*/
56+
withAlternativeNames(alternativeNames) {
57+
this.alternativeNames = alternativeNames;
58+
return this;
59+
}
60+
61+
/**
62+
* @param {boolean} [optional=true]
63+
*/
64+
withOptional(optional = true) {
65+
this.optional = optional;
66+
return this;
67+
}
68+
4569
/**
4670
* @returns {WantedAttribute}
4771
*/
@@ -50,7 +74,9 @@ module.exports = class WantedAttributeBuilder {
5074
this.name,
5175
this.derivation,
5276
this.acceptSelfAsserted,
53-
this.constraints
77+
this.constraints,
78+
this.alternativeNames,
79+
this.optional
5480
);
5581
}
5682
};

src/dynamic_sharing_service/policy/wanted.attribute.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ module.exports = class WantedAttribute {
1414
* @param {string|null} derivation
1515
* @param {boolean|null} acceptSelfAsserted
1616
* @param {Constraints|null} constraints
17+
* @param {string[]|null} alternativeNames
18+
* @param {boolean|null} optional
1719
*/
18-
constructor(name, derivation = null, acceptSelfAsserted = null, constraints = null) {
20+
// eslint-disable-next-line max-len
21+
constructor(name, derivation = null, acceptSelfAsserted = null, constraints = null, alternativeNames = null, optional = null) {
1922
Validation.isString(name, 'name');
2023
Validation.notNullOrEmpty(name, 'name');
2124
/** @private */
@@ -38,6 +41,18 @@ module.exports = class WantedAttribute {
3841
}
3942
/** @private */
4043
this.constraints = constraints;
44+
45+
if (alternativeNames !== null) {
46+
Validation.isArrayOfStrings(alternativeNames, 'alternativeNames');
47+
}
48+
/** @private */
49+
this.alternativeNames = alternativeNames;
50+
51+
if (optional !== null) {
52+
Validation.isBoolean(optional, 'optional');
53+
}
54+
/** @private */
55+
this.optional = optional;
4156
}
4257

4358
/**
@@ -81,6 +96,26 @@ module.exports = class WantedAttribute {
8196
return this.acceptSelfAsserted;
8297
}
8398

99+
/**
100+
* Accept alternative names.
101+
*
102+
* These are names of attributes that can be used as fallback
103+
*
104+
* @returns {string[]}
105+
*/
106+
getAlternativeNames() {
107+
return this.alternativeNames;
108+
}
109+
110+
/**
111+
* Whether the attribute is wanted optionally
112+
*
113+
* @returns {boolean}
114+
*/
115+
getOptional() {
116+
return this.optional;
117+
}
118+
84119
/**
85120
* @returns {Object} data for JSON.stringify()
86121
*/
@@ -102,6 +137,14 @@ module.exports = class WantedAttribute {
102137
json.accept_self_asserted = this.getAcceptSelfAsserted();
103138
}
104139

140+
if (this.getAlternativeNames() !== null) {
141+
json.alternative_names = this.getAlternativeNames();
142+
}
143+
144+
if (this.getOptional() !== null) {
145+
json.optional = this.getOptional();
146+
}
147+
105148
return json;
106149
}
107150
};

tests/dynamic_sharing_service/policy/wanted.attribute.builder.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,64 @@ describe('WantedAttributeBuilder', () => {
127127
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
128128
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
129129
});
130+
131+
it('should build a wanted attribute with alternative name', () => {
132+
const wantedAttribute = new WantedAttributeBuilder()
133+
.withName(TEST_NAME)
134+
.withAlternativeName(`alt-${TEST_NAME}`)
135+
.build();
136+
137+
const expectedJson = JSON.stringify({
138+
name: TEST_NAME,
139+
optional: false,
140+
alternative_names: [`alt-${TEST_NAME}`],
141+
});
142+
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
143+
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
144+
});
145+
146+
it('should build a wanted attribute with alternative names', () => {
147+
const wantedAttribute = new WantedAttributeBuilder()
148+
.withName(TEST_NAME)
149+
.withAlternativeNames([`${TEST_NAME}-alt1`, `${TEST_NAME}-alt2`])
150+
.build();
151+
152+
const expectedJson = JSON.stringify({
153+
name: TEST_NAME,
154+
optional: false,
155+
alternative_names: [`${TEST_NAME}-alt1`, `${TEST_NAME}-alt2`],
156+
});
157+
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
158+
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
159+
});
160+
161+
it('should build a wanted attribute with alternative names using both methods', () => {
162+
const wantedAttribute = new WantedAttributeBuilder()
163+
.withName(TEST_NAME)
164+
.withAlternativeNames([`${TEST_NAME}-alt1`, `${TEST_NAME}-alt2`])
165+
.withAlternativeName(`${TEST_NAME}-extra`)
166+
.build();
167+
168+
const expectedJson = JSON.stringify({
169+
name: TEST_NAME,
170+
optional: false,
171+
alternative_names: [`${TEST_NAME}-alt1`, `${TEST_NAME}-alt2`, `${TEST_NAME}-extra`],
172+
});
173+
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
174+
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
175+
});
176+
177+
it('should build a wanted attribute with optional behaviour', () => {
178+
const wantedAttribute = new WantedAttributeBuilder()
179+
.withName(TEST_NAME)
180+
.withOptional(true)
181+
.build();
182+
183+
const expectedJson = JSON.stringify({
184+
name: TEST_NAME,
185+
optional: true,
186+
});
187+
expect(wantedAttribute).toBeInstanceOf(WantedAttribute);
188+
expect(JSON.stringify(wantedAttribute)).toBe(expectedJson);
189+
});
130190
});

types/src/dynamic_sharing_service/policy/wanted.attribute.builder.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ declare class WantedAttributeBuilder {
2222
*/
2323
withAcceptSelfAsserted(acceptSelfAsserted?: boolean): this;
2424
acceptSelfAsserted: boolean;
25+
/**
26+
* @param {string} alternativeName
27+
*/
28+
withAlternativeName(alternativeName: string): this;
29+
alternativeNames: any;
30+
/**
31+
* @param {string[]} alternativeNames
32+
*/
33+
withAlternativeNames(alternativeNames: string[]): this;
34+
/**
35+
* @param {boolean} [optional=true]
36+
*/
37+
withOptional(optional?: boolean): this;
38+
optional: boolean;
2539
/**
2640
* @returns {WantedAttribute}
2741
*/

types/src/dynamic_sharing_service/policy/wanted.attribute.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ declare class WantedAttribute {
55
* @param {string|null} derivation
66
* @param {boolean|null} acceptSelfAsserted
77
* @param {Constraints|null} constraints
8+
* @param {string[]|null} alternativeNames
9+
* @param {boolean|null} optional
810
*/
9-
constructor(name: string, derivation?: string | null, acceptSelfAsserted?: boolean | null, constraints?: Constraints | null);
11+
constructor(name: string, derivation?: string | null, acceptSelfAsserted?: boolean | null, constraints?: Constraints | null, alternativeNames?: string[] | null, optional?: boolean | null);
1012
/** @private */
1113
private name;
1214
/** @private */
@@ -15,6 +17,10 @@ declare class WantedAttribute {
1517
private acceptSelfAsserted;
1618
/** @private */
1719
private constraints;
20+
/** @private */
21+
private alternativeNames;
22+
/** @private */
23+
private optional;
1824
/**
1925
* Name identifying the WantedAttribute
2026
*
@@ -44,6 +50,20 @@ declare class WantedAttribute {
4450
* @returns {boolean}
4551
*/
4652
getAcceptSelfAsserted(): boolean;
53+
/**
54+
* Accept alternative names.
55+
*
56+
* These are names of attributes that can be used as fallback
57+
*
58+
* @returns {string[]}
59+
*/
60+
getAlternativeNames(): string[];
61+
/**
62+
* Whether the attribute is wanted optionally
63+
*
64+
* @returns {boolean}
65+
*/
66+
getOptional(): boolean;
4767
/**
4868
* @returns {Object} data for JSON.stringify()
4969
*/

0 commit comments

Comments
 (0)