Skip to content

Commit 12fc246

Browse files
authored
Merge pull request #23 from GytisZ/add-allowed-purposes-shorthand
Add a method to get allowed purposed bits as an integer
2 parents 218a54c + bb2789c commit 12fc246

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/main/java/com/iab/gdpr/VendorConsent.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import com.iab.gdpr.exception.VendorConsentParseException;
1616
import com.iab.gdpr.util.ConsentStringParser;
1717

18+
import static com.iab.gdpr.GdprConstants.PURPOSES_OFFSET;
19+
import static com.iab.gdpr.GdprConstants.PURPOSES_SIZE;
20+
1821
/**
1922
* This class implements a builder and a factory method for the IAB consent as specified in
2023
* https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/
@@ -111,9 +114,9 @@ private VendorConsent(Builder builder) throws VendorConsentException {
111114
int i = 0;
112115
for (boolean purpose : allowedPurposes) {
113116
if (purpose) {
114-
bits.setBit(GdprConstants.PURPOSES_OFFSET + i++);
117+
bits.setBit(PURPOSES_OFFSET + i++);
115118
} else {
116-
bits.unsetBit(GdprConstants.PURPOSES_OFFSET + i++);
119+
bits.unsetBit(PURPOSES_OFFSET + i++);
117120
}
118121
}
119122

@@ -282,6 +285,14 @@ public List<Integer> getAllowedPurposes() {
282285

283286
}
284287

288+
/**
289+
*
290+
* @return an integer equivalent of allowed purpose id bits according to this consent string
291+
*/
292+
public int getAllowedPurposesBits() {
293+
return bits.getInt(PURPOSES_OFFSET, PURPOSES_SIZE);
294+
}
295+
285296
/**
286297
*
287298
* @return the vendor list version which was used in creating this consent string
@@ -424,7 +435,7 @@ public static class Builder {
424435
private int vendorListVersion;
425436
private int maxVendorId;
426437
private int vendorEncodingType;
427-
private List<Boolean> allowedPurposes = new ArrayList<>(GdprConstants.PURPOSES_SIZE);
438+
private List<Boolean> allowedPurposes = new ArrayList<>(PURPOSES_SIZE);
428439
// only used when bitfield is enabled
429440
private List<Integer> vendorsBitField;
430441
// only used when range entry is enabled
@@ -515,7 +526,7 @@ public Builder withVendorListVersion(int vendorListVersion) {
515526
*/
516527
public Builder withAllowedPurposes(List<Integer> allowedPurposes) {
517528
this.integerPurposes = allowedPurposes;
518-
for (int i = 0; i < GdprConstants.PURPOSES_SIZE; i++) {
529+
for (int i = 0; i < PURPOSES_SIZE; i++) {
519530
this.allowedPurposes.add(false);
520531
}
521532
for (int purpose : allowedPurposes) {

src/test/java/com/iab/gdpr/VendorConsentTest.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public void testBitField() {
6868
assertThat(consent.getConsentRecordCreated(), Matchers.is(Instant.ofEpochMilli(14924661858L * 100)));
6969
assertThat(consent.getConsentRecordLastUpdated(), Matchers.is(Instant.ofEpochMilli(15240021858L * 100)));
7070
assertThat(consent.getAllowedPurposes().size(), Matchers.is(5));
71+
assertThat(consent.getAllowedPurposesBits(), Matchers.is(6291482));
7172

7273
assertTrue(consent.isPurposeAllowed(2));
7374
assertFalse(consent.isPurposeAllowed(1));
@@ -94,6 +95,7 @@ public void testRangeEntry() {
9495
assertThat(consent.getConsentRecordCreated(), Matchers.is(Instant.ofEpochMilli(14924661858L * 100)));
9596
assertThat(consent.getConsentRecordLastUpdated(), Matchers.is(Instant.ofEpochMilli(15240021858L * 100)));
9697
assertThat(consent.getAllowedPurposes().size(), Matchers.is(8));
98+
assertThat(consent.getAllowedPurposesBits(), Matchers.is(2000001));
9799

98100
assertTrue(consent.isPurposeAllowed(4));
99101
assertFalse(consent.isPurposeAllowed(1));
@@ -208,30 +210,4 @@ public void testRangeEntryConsent() {
208210
assertFalse(consent.isVendorAllowed(3));
209211
assertTrue(consent.isVendorAllowed(27));
210212
}
211-
212-
@Test
213-
public void testLongRangeEntry() {
214-
String consentString = "BOOMzbgOOQww_AtABAFRAb-AAAsvOA3gACAAkABgArgBaAF0AMAA1gBuAH8AQQBSgCoAL8AYQBigDIAM0AaABpgDYAOYAdgA8AB6gD4AQoAiABFQCMAI6ASABIgCTAEqAJeATIBQQCiAKSAU4BVQCtAK-AWYBaQC2ALcAXMAvAC-gGAAYcAxQDGAGQAMsAZsA0ADTAGqANcAbMA4ADjAHKAOiAdQB1gDtgHgAeMA9AD2AHzAP4BAACBAEEAIbAREBEgCKQEXARhZeYA";
215-
VendorConsent consent = VendorConsent.fromBase64String(consentString);
216-
assertThat(consent.getCmpId(), Matchers.is(45));
217-
assertThat(consent.getCmpVersion(), Matchers.is(1));
218-
assertThat(consent.getConsentLanguage(), Matchers.is("FR"));
219-
assertThat(consent.getConsentRecordCreated(), Matchers.is(Instant.ofEpochMilli(15270622944L * 100)));
220-
assertThat(consent.getConsentRecordLastUpdated(), Matchers.is(Instant.ofEpochMilli(15271660607L * 100)));
221-
assertThat(consent.getAllowedPurposes().size(), Matchers.is(5)); assertTrue(consent.isPurposeAllowed(1));
222-
assertTrue(consent.isPurposeAllowed(2));
223-
assertTrue(consent.isPurposeAllowed(3));
224-
assertTrue(consent.isPurposeAllowed(4));
225-
assertTrue(consent.isPurposeAllowed(5));
226-
assertFalse(consent.isPurposeAllowed(6));
227-
assertFalse(consent.isPurposeAllowed(25));
228-
assertFalse(consent.isPurposeAllowed(0));
229-
assertTrue(consent.isVendorAllowed(1));
230-
assertFalse(consent.isVendorAllowed(5));
231-
assertTrue(consent.isVendorAllowed(45));
232-
assertFalse(consent.isVendorAllowed(47));
233-
assertFalse(consent.isVendorAllowed(146));
234-
assertTrue(consent.isVendorAllowed(147));
235-
assertThat(consent.getConsentString(), Matchers.is(consentString));
236-
}
237213
}

0 commit comments

Comments
 (0)