Skip to content

Commit

Permalink
Add site tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asloobq committed Oct 8, 2024
1 parent 0e2ece4 commit 7c0c971
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/uid2/client/KeyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


class KeyParser {
Expand Down Expand Up @@ -82,12 +84,12 @@ private static Site getSiteFromJson(JsonObject siteJson) {
return null;
}
JsonArray domainOrAppNamesJArray = siteJson.getAsJsonArray("domain_names");
List<String> domainOrAppNames = new ArrayList<>();
Set<String> domainOrAppNamesSet = new HashSet<>();
for (int i = 0; i < domainOrAppNamesJArray.size(); ++i) {
domainOrAppNames.add(domainOrAppNamesJArray.get(i).getAsString());
domainOrAppNamesSet.add(domainOrAppNamesJArray.get(i).getAsString());
}

return new Site(siteId, domainOrAppNames);
return new Site(siteId, domainOrAppNamesSet);
}

static private int getAsInt(JsonObject body, String memberName) {
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/uid2/client/Site.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package com.uid2.client;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Set;

@Getter
@AllArgsConstructor
public class Site {
private final int id;

private final Set<String> domainOrAppNames;

public Site(int id, List<String> domainOrAppNames) {
this.id = id;
this.domainOrAppNames = new HashSet<>(domainOrAppNames);
}

public boolean allowDomainOrAppName(String domainOrAppName) {
// Using streams because HashSet's contains() is case sensitive
return domainOrAppNames.stream().anyMatch(domainOrAppName::equalsIgnoreCase);
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/com/uid2/client/SiteTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.uid2.client;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Arrays;
import java.util.HashSet;

public class SiteTests {

private static Site site;

@BeforeAll
public static void setup() {
site = new Site(101, new HashSet<>(Arrays.asList(
"example.com",
"example.org",
"com.123.Game.App.android",
"123456789"
)));
}


@ParameterizedTest
@ValueSource(strings = {
"example.com",
"example.org",
"com.123.Game.App.android",
"123456789",
"EXAMPLE.COM",
"com.123.game.app.android",
})
public void testAllowDomainOrAppNameSuccess(String domainOrAppName) {
Assertions.assertTrue(site.allowDomainOrAppName(domainOrAppName));
}

@ParameterizedTest
@CsvSource({
"*",
"example",
"example*",
"example.net"
})
public void testAllowDomainOrAppNameFailure(String domainOrAppName) {
Assertions.assertFalse(site.allowDomainOrAppName(domainOrAppName));
}
}

0 comments on commit 7c0c971

Please sign in to comment.