Skip to content

Commit b8163ce

Browse files
committed
Using enum for unmapped operations instead of strings
1 parent 7b3ae88 commit b8163ce

File tree

4 files changed

+127
-30
lines changed

4 files changed

+127
-30
lines changed

src/main/java/com/uid2/client/IdentityMapV3Response.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ private void populateIdentitiesForType(IdentityMapV3Input identityMapInput, Stri
3131
ApiIdentity apiIdentity = identities.get(i);
3232
List<String> inputDiis = identityMapInput.getInputDiis(identityType, i);
3333
for (String inputDii : inputDiis) {
34-
if (apiIdentity.error != null) {
35-
unmappedIdentities.put(inputDii, new UnmappedIdentity(apiIdentity));
36-
} else {
34+
if (apiIdentity.error == null) {
3735
mappedIdentities.put(inputDii, new MappedIdentity(apiIdentity));
36+
} else {
37+
unmappedIdentities.put(inputDii, new UnmappedIdentity(apiIdentity.error));
3838
}
3939
}
4040
}
@@ -99,19 +99,23 @@ public Instant getRefreshFrom() {
9999
}
100100

101101
static public class UnmappedIdentity {
102-
public UnmappedIdentity(String reason) {
103-
this.reason = reason;
102+
public UnmappedIdentity(String reason)
103+
{
104+
this.reason = UnmappedIdentityReason.fromString(reason);
105+
this.rawReason = reason;
104106
}
105107

106-
public UnmappedIdentity(ApiIdentity apiIdentity) {
107-
this(apiIdentity.error);
108+
public UnmappedIdentityReason getReason() {
109+
return reason;
108110
}
109111

110-
private final String reason;
111-
112-
public String getReason() {
113-
return reason;
112+
public String getRawReason() {
113+
return rawReason;
114114
}
115+
116+
private final UnmappedIdentityReason reason;
117+
118+
private final String rawReason;
115119
}
116120

117121
public HashMap<String, MappedIdentity> getMappedIdentities() {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.uid2.client;
2+
3+
4+
public enum UnmappedIdentityReason {
5+
OPTOUT,
6+
INVALID,
7+
UNKNOWN;
8+
9+
public static UnmappedIdentityReason fromString(String reason) {
10+
for (UnmappedIdentityReason knownReason : values()) {
11+
if (knownReason.name().equals(reason.toUpperCase())) {
12+
return knownReason;
13+
}
14+
}
15+
16+
return UNKNOWN;
17+
}
18+
}

src/test/java/com/uid2/client/IdentityMapV3IntegrationTests.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void identityMapEmails() {
3838
response.assertMapped(mappedEmail);
3939
response.assertMapped(optedOutEmail2);
4040

41-
response.assertUnmapped("OPTOUT", optedOutEmail);
41+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutEmail);
4242
}
4343

4444
@Test
@@ -55,7 +55,7 @@ public void identityMapNothingMapped() {
5555
IdentityMapV3Input identityMapInput = IdentityMapV3Input.fromEmails(Collections.singletonList(optedOutEmail));
5656
Response response = new Response(identityMapInput);
5757

58-
response.assertUnmapped("OPTOUT", optedOutEmail);
58+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutEmail);
5959
}
6060

6161

@@ -77,15 +77,15 @@ public void identityMapInvalidHashedEmail() {
7777

7878
Response response = new Response(identityMapInput);
7979

80-
response.assertUnmapped("INVALID", "this is not a hashed email");
80+
response.assertUnmapped(UnmappedIdentityReason.INVALID, "this is not a hashed email");
8181
}
8282

8383
@Test
8484
public void identityMapInvalidHashedPhone() {
8585
IdentityMapV3Input identityMapInput = IdentityMapV3Input.fromHashedPhones(Collections.singletonList("this is not a hashed phone"));
8686

8787
Response response = new Response(identityMapInput);
88-
response.assertUnmapped("INVALID", "this is not a hashed phone");
88+
response.assertUnmapped(UnmappedIdentityReason.INVALID, "this is not a hashed phone");
8989
}
9090

9191
@Test
@@ -100,7 +100,7 @@ public void identityMapHashedEmails() {
100100
response.assertMapped(hashedEmail1);
101101
response.assertMapped(hashedEmail2);
102102

103-
response.assertUnmapped("OPTOUT", hashedOptedOutEmail);
103+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, hashedOptedOutEmail);
104104
}
105105

106106
@Test
@@ -125,7 +125,7 @@ public void identityMapDuplicateHashedEmails() {
125125

126126
response.assertMapped(mappedEmailHash);
127127

128-
response.assertUnmapped("OPTOUT", optedOutEmailHash);
128+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutEmailHash);
129129
}
130130

131131
@Test
@@ -145,7 +145,7 @@ public void identityMapPhones() {
145145
response.assertMapped(mappedPhone);
146146
response.assertMapped(mappedPhone2);
147147

148-
response.assertUnmapped("OPTOUT", optedOutPhone);
148+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutPhone);
149149
}
150150

151151
@Test
@@ -160,7 +160,7 @@ public void identityMapHashedPhones() {
160160
response.assertMapped(hashedPhone1);
161161
response.assertMapped(hashedPhone2);
162162

163-
response.assertUnmapped("OPTOUT", hashedOptedOutPhone);
163+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, hashedOptedOutPhone);
164164
}
165165

166166
@Test
@@ -178,10 +178,10 @@ public void identityMapAllIdentityTypesInOneRequest() {
178178
response.assertMapped(mappedPhone);
179179
response.assertMapped(mappedPhoneHash);
180180

181-
response.assertUnmapped("OPTOUT", optedOutEmail);
182-
response.assertUnmapped("OPTOUT", optedOutEmailHash);
183-
response.assertUnmapped("OPTOUT", optedOutPhone);
184-
response.assertUnmapped("OPTOUT", optedOutPhoneHash);
181+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutEmail);
182+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutEmailHash);
183+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutPhone);
184+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutPhoneHash);
185185
}
186186

187187
@Test
@@ -198,8 +198,8 @@ public void identityMapAllIdentityTypesInOneRequestAddedOneByOne() {
198198
response.assertMapped(mappedEmail);
199199
response.assertMapped(mappedPhoneHash);
200200

201-
response.assertUnmapped("OPTOUT", optedOutPhone);
202-
response.assertUnmapped("OPTOUT", optedOutEmailHash);
201+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutPhone);
202+
response.assertUnmapped(UnmappedIdentityReason.OPTOUT, optedOutEmailHash);
203203
}
204204

205205

@@ -217,14 +217,14 @@ void assertMapped(String dii) {
217217
Instant aMinuteAgo = Instant.now().minusSeconds(60);
218218
assertTrue(mappedIdentity.getRefreshFrom().isAfter(aMinuteAgo));
219219

220-
IdentityMapV3Response.UnmappedIdentity unmappedIdentity = identityMapResponse.getUnmappedIdentities().get(dii);
221-
assertNull(unmappedIdentity);
220+
IdentityMapV3Response.UnmappedIdentity unmappedIdentityReason = identityMapResponse.getUnmappedIdentities().get(dii);
221+
assertNull(unmappedIdentityReason);
222222
}
223223

224-
void assertUnmapped(String reason, String dii) {
224+
void assertUnmapped(UnmappedIdentityReason reason, String dii) {
225225
HashMap<String, IdentityMapV3Response.UnmappedIdentity> unmappedIdentities = identityMapResponse.getUnmappedIdentities();
226-
IdentityMapV3Response.UnmappedIdentity dii2 = unmappedIdentities.get(dii);
227-
assertEquals(reason, dii2.getReason());
226+
IdentityMapV3Response.UnmappedIdentity unmappedIdentity = unmappedIdentities.get(dii);
227+
assertEquals(reason, unmappedIdentity.getReason());
228228

229229
IdentityMapV3Response.MappedIdentity mappedIdentity = identityMapResponse.getMappedIdentities().get(dii);
230230
assertNull(mappedIdentity);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.uid2.client;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.*;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class IdentityMapV3Tests {
11+
private final static String SOME_EMAIL = "[email protected]";
12+
13+
@Test
14+
void identityMapV3UnmappedIdentityReasonUnknown() {
15+
IdentityMapV3Input input = IdentityMapV3Input.fromEmails(Arrays.asList(SOME_EMAIL));
16+
17+
IdentityMapV3Response response = new IdentityMapV3Response(payloadJson("SOME_NEW_UNMAPPED_REASON"), input);
18+
19+
IdentityMapV3Response.UnmappedIdentity unmappedIdentity = response.getUnmappedIdentities().get(SOME_EMAIL);
20+
assertEquals(UnmappedIdentityReason.UNKNOWN, unmappedIdentity.getReason());
21+
assertEquals("SOME_NEW_UNMAPPED_REASON", unmappedIdentity.getRawReason());
22+
}
23+
24+
@Test
25+
void identityMapV3UnmappedIdentityReasonOptout() {
26+
IdentityMapV3Input input = IdentityMapV3Input.fromEmails(Arrays.asList(SOME_EMAIL));
27+
28+
IdentityMapV3Response response = new IdentityMapV3Response(payloadJson("OPTOUT"), input);
29+
30+
IdentityMapV3Response.UnmappedIdentity unmappedIdentity = response.getUnmappedIdentities().get(SOME_EMAIL);
31+
assertEquals(UnmappedIdentityReason.OPTOUT, unmappedIdentity.getReason());
32+
assertEquals("OPTOUT", unmappedIdentity.getRawReason());
33+
}
34+
35+
@Test
36+
void identityMapV3UnmappedIdentityReasonInvalid() {
37+
IdentityMapV3Input input = IdentityMapV3Input.fromEmails(Arrays.asList(SOME_EMAIL));
38+
39+
IdentityMapV3Response response = new IdentityMapV3Response(payloadJson("INVALID"), input);
40+
41+
IdentityMapV3Response.UnmappedIdentity unmappedIdentity = response.getUnmappedIdentities().get(SOME_EMAIL);
42+
assertEquals(UnmappedIdentityReason.INVALID, unmappedIdentity.getReason());
43+
assertEquals("INVALID", unmappedIdentity.getRawReason());
44+
}
45+
46+
@Test
47+
void identityMapV3UnmappedIdentityReasonCaseInsensitive() {
48+
IdentityMapV3Input input = IdentityMapV3Input.fromEmails(Arrays.asList(SOME_EMAIL));
49+
50+
IdentityMapV3Response.UnmappedIdentity lowercaseOptout =
51+
new IdentityMapV3Response(payloadJson("optout"), input).getUnmappedIdentities().get(SOME_EMAIL);
52+
assertEquals(UnmappedIdentityReason.OPTOUT, lowercaseOptout.getReason());
53+
assertEquals("optout", lowercaseOptout.getRawReason());
54+
55+
IdentityMapV3Response.UnmappedIdentity lowercaseInvalid =
56+
new IdentityMapV3Response(payloadJson("invalid"), input).getUnmappedIdentities().get(SOME_EMAIL);
57+
assertEquals(UnmappedIdentityReason.INVALID, lowercaseInvalid.getReason());
58+
assertEquals("invalid", lowercaseInvalid.getRawReason());
59+
60+
IdentityMapV3Response.UnmappedIdentity mixedOptout =
61+
new IdentityMapV3Response(payloadJson("OptOut"), input).getUnmappedIdentities().get(SOME_EMAIL);
62+
assertEquals(UnmappedIdentityReason.OPTOUT, mixedOptout.getReason());
63+
assertEquals("OptOut", mixedOptout.getRawReason());
64+
65+
IdentityMapV3Response.UnmappedIdentity mixedInvalid =
66+
new IdentityMapV3Response(payloadJson("InVaLiD"), input).getUnmappedIdentities().get(SOME_EMAIL);
67+
assertEquals(UnmappedIdentityReason.INVALID, mixedInvalid.getReason());
68+
assertEquals("InVaLiD", mixedInvalid.getRawReason());
69+
}
70+
71+
@NotNull
72+
private static String payloadJson(String reason) {
73+
return "{\"status\":\"success\",\"body\":{\"email_hash\":[{\"e\":\"" + reason + "\"}]}}";
74+
}
75+
}

0 commit comments

Comments
 (0)