-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/io/split/android/client/validators/FlagSetsValidatorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.split.android.client.validators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.TreeSet; | ||
|
||
import io.split.android.client.utils.logger.Logger; | ||
|
||
public class FlagSetsValidatorImpl implements SplitFilterValidator { | ||
|
||
private static final String FLAG_SET_REGEX = "^[a-z][_a-z0-9]{0,49}$"; | ||
|
||
/** | ||
* Validates the flag sets and returns a list of | ||
* de-duplicated and alphanumerically ordered valid flag sets. | ||
* | ||
* @param values list of flag sets | ||
* @return list of unique alphanumerically ordered valid flag sets | ||
*/ | ||
@Override | ||
public List<String> cleanup(List<String> values) { | ||
if (values == null || values.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
TreeSet<String> cleanedUpSets = new TreeSet<>(); | ||
for (String set : values) { | ||
if (set == null || set.isEmpty()) { | ||
continue; | ||
} | ||
|
||
if (set.startsWith(" ") || set.endsWith(" ")) { | ||
Logger.w("SDK config: Flag Set name " + set + " has extra whitespace, trimming"); | ||
set = set.trim(); | ||
} | ||
|
||
if (set.matches(FLAG_SET_REGEX)) { | ||
cleanedUpSets.add(set); | ||
} else { | ||
Logger.w("SDK config: you passed "+ set +", Flag Set must adhere to the regular expressions "+ FLAG_SET_REGEX +". This means a Flag Set must be start with a letter, be in lowercase, alphanumeric and have a max length of 50 characters. "+ set +" was discarded."); | ||
} | ||
} | ||
|
||
return new ArrayList<>(cleanedUpSets); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/io/split/android/client/validators/SplitFilterValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.split.android.client.validators; | ||
|
||
import java.util.List; | ||
|
||
public interface SplitFilterValidator { | ||
|
||
List<String> cleanup(List<String> values); | ||
} |
84 changes: 84 additions & 0 deletions
84
src/test/java/io/split/android/client/validators/FlagSetsValidatorImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.split.android.client.validators; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class FlagSetsValidatorImplTest { | ||
|
||
private final FlagSetsValidatorImpl mValidator = new FlagSetsValidatorImpl(); | ||
|
||
@Test | ||
public void nullInputReturnsEmptyList() { | ||
List<String> result = mValidator.cleanup(null); | ||
assertTrue(result.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void emptyInputReturnsEmptyList() { | ||
List<String> result = mValidator.cleanup(Collections.emptyList()); | ||
assertTrue(result.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void duplicatedInputValuesAreRemoved() { | ||
List<String> result = mValidator.cleanup(Arrays.asList("set1", "set1")); | ||
assertEquals(1, result.size()); | ||
assertTrue(result.contains("set1")); | ||
} | ||
|
||
@Test | ||
public void valuesAreSortedAlphanumerically() { | ||
List<String> result = mValidator.cleanup(Arrays.asList("set2", "set1", "set_1")); | ||
assertEquals(3, result.size()); | ||
assertEquals("set1", result.get(0)); | ||
assertEquals("set2", result.get(1)); | ||
assertEquals("set_1", result.get(2)); | ||
} | ||
|
||
@Test | ||
public void invalidValuesAreRemoved() { | ||
List<String> result = mValidator.cleanup(Arrays.asList("set1", "set2", "set_1", "set-1", "set 1", "set 2")); | ||
assertEquals(3, result.size()); | ||
assertEquals("set1", result.get(0)); | ||
assertEquals("set2", result.get(1)); | ||
assertEquals("set_1", result.get(2)); | ||
} | ||
|
||
@Test | ||
public void setWithMoreThan50CharsIsRemoved() { | ||
String longSet = "abcdfghijklmnopqrstuvwxyz1234567890abcdfghijklmnopq"; | ||
List<String> result = mValidator.cleanup(Arrays.asList("set1", longSet)); | ||
assertEquals(51, longSet.length()); | ||
assertEquals(1, result.size()); | ||
assertEquals("set1", result.get(0)); | ||
} | ||
|
||
@Test | ||
public void setWithLessThanOneCharIsOrEmptyRemoved() { | ||
List<String> result = mValidator.cleanup(Arrays.asList("set1", "", " ")); | ||
assertEquals(1, result.size()); | ||
assertEquals("set1", result.get(0)); | ||
} | ||
|
||
@Test | ||
public void nullSetIsRemoved() { | ||
List<String> result = mValidator.cleanup(Arrays.asList("set1", null)); | ||
assertEquals(1, result.size()); | ||
assertEquals("set1", result.get(0)); | ||
} | ||
|
||
@Test | ||
public void setWithExtraWhitespaceIsTrimmed() { | ||
List<String> result = mValidator.cleanup(Arrays.asList("set1 ", " set2", "set3", "set 4")); | ||
assertEquals(3, result.size()); | ||
assertEquals("set1", result.get(0)); | ||
assertEquals("set2", result.get(1)); | ||
assertEquals("set3", result.get(2)); | ||
} | ||
} |