Skip to content

Commit af5a5a1

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

File tree

4 files changed

+87
-41
lines changed

4 files changed

+87
-41
lines changed

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

Lines changed: 5 additions & 21 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, UnmappedIdentityReason.fromString(apiIdentity.error));
3838
}
3939
}
4040
}
@@ -98,31 +98,15 @@ public Instant getRefreshFrom() {
9898
}
9999
}
100100

101-
static public class UnmappedIdentity {
102-
public UnmappedIdentity(String reason) {
103-
this.reason = reason;
104-
}
105-
106-
public UnmappedIdentity(ApiIdentity apiIdentity) {
107-
this(apiIdentity.error);
108-
}
109-
110-
private final String reason;
111-
112-
public String getReason() {
113-
return reason;
114-
}
115-
}
116-
117101
public HashMap<String, MappedIdentity> getMappedIdentities() {
118102
return mappedIdentities;
119103
}
120104

121-
public HashMap<String, UnmappedIdentity> getUnmappedIdentities() {
105+
public HashMap<String, UnmappedIdentityReason> getUnmappedIdentities() {
122106
return unmappedIdentities;
123107
}
124108

125109
private final String status;
126110
private final HashMap<String, MappedIdentity> mappedIdentities = new HashMap<>();
127-
private final HashMap<String, UnmappedIdentity> unmappedIdentities = new HashMap<>();
111+
private final HashMap<String, UnmappedIdentityReason> unmappedIdentities = new HashMap<>();
128112
}
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)) {
12+
return knownReason;
13+
}
14+
}
15+
16+
return UNKNOWN;
17+
}
18+
}

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

Lines changed: 20 additions & 20 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+
UnmappedIdentityReason unmappedIdentityReason = identityMapResponse.getUnmappedIdentities().get(dii);
221+
assertNull(unmappedIdentityReason);
222222
}
223223

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

229229
IdentityMapV3Response.MappedIdentity mappedIdentity = identityMapResponse.getMappedIdentities().get(dii);
230230
assertNull(mappedIdentity);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 identityMapV3UnknownUnmappedIdentityReason() {
15+
IdentityMapV3Input input = IdentityMapV3Input.fromEmails(Arrays.asList(SOME_EMAIL));
16+
17+
IdentityMapV3Response response = new IdentityMapV3Response(payloadJson("SOME_NEW_UNMAPPED_REASON"), input);
18+
19+
assertEquals(UnmappedIdentityReason.UNKNOWN, response.getUnmappedIdentities().get(SOME_EMAIL));
20+
}
21+
22+
@Test
23+
void identityMapV3OptoutUnmappedIdentityReason() {
24+
IdentityMapV3Input input = IdentityMapV3Input.fromEmails(Arrays.asList(SOME_EMAIL));
25+
26+
IdentityMapV3Response response = new IdentityMapV3Response(payloadJson("OPTOUT"), input);
27+
28+
assertEquals(UnmappedIdentityReason.OPTOUT, response.getUnmappedIdentities().get(SOME_EMAIL));
29+
}
30+
31+
@Test
32+
void identityMapV3InvalidUnmappedIdentityReason() {
33+
IdentityMapV3Input input = IdentityMapV3Input.fromEmails(Arrays.asList(SOME_EMAIL));
34+
35+
IdentityMapV3Response response = new IdentityMapV3Response(payloadJson("INVALID"), input);
36+
37+
assertEquals(UnmappedIdentityReason.INVALID, response.getUnmappedIdentities().get(SOME_EMAIL));
38+
}
39+
40+
@NotNull
41+
private static String payloadJson(String reason) {
42+
return "{\"status\":\"success\",\"body\":{\"email_hash\":[{\"e\":\"" + reason + "\"}]}}";
43+
}
44+
}

0 commit comments

Comments
 (0)