-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #435 from splitio/SDKS-7516
[SDKS-7516] Create FlagSetSanitizer to validate flag sets
- Loading branch information
Showing
5 changed files
with
156 additions
and
10 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
21 changes: 21 additions & 0 deletions
21
client/src/main/java/io/split/inputValidation/FlagSetsValidResult.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,21 @@ | ||
package io.split.inputValidation; | ||
|
||
import java.util.HashSet; | ||
|
||
public class FlagSetsValidResult { | ||
private final Boolean _valid; | ||
private final HashSet<String> _flagSets; | ||
|
||
public FlagSetsValidResult(Boolean valid, HashSet<String> flagSets) { | ||
_valid = valid; | ||
_flagSets = flagSets; | ||
} | ||
|
||
public Boolean getValid() { | ||
return _valid; | ||
} | ||
|
||
public HashSet<String> getFlagSets() { | ||
return _flagSets; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
client/src/main/java/io/split/inputValidation/FlagSetsValidator.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,49 @@ | ||
package io.split.inputValidation; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
public final class FlagSetsValidator { | ||
|
||
private static final String FLAG_SET_REGEX = "^[a-z0-9][_a-z0-9]{0,49}$"; | ||
private static final Logger _log = LoggerFactory.getLogger(FlagSetsValidator.class); | ||
|
||
private FlagSetsValidator() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
|
||
public static HashSet<String> cleanup(List<String> flagSets) { | ||
if (flagSets == null || flagSets.isEmpty()) { | ||
return new HashSet<>(); | ||
} | ||
HashSet<String> cleanFlagSets = new HashSet<>(); | ||
for (String flagSet: flagSets) { | ||
if(flagSet != flagSet.toLowerCase()) { | ||
_log.warn(String.format("Flag Set name %s should be all lowercase - converting string to lowercase", flagSet)); | ||
flagSet = flagSet.toLowerCase(); | ||
} | ||
if (flagSet.trim() != flagSet) { | ||
_log.warn(String.format("Flag Set name %s has extra whitespace, trimming", flagSet)); | ||
flagSet = flagSet.trim(); | ||
} | ||
if (!Pattern.matches(FLAG_SET_REGEX, flagSet)) { | ||
_log.warn(String.format("you passed %s, Flag Set must adhere to the regular expressions %s. This means an Flag Set must be " + | ||
"start with a letter, be in lowercase, alphanumeric and have a max length of 50 characters. %s was discarded.", | ||
flagSet, FLAG_SET_REGEX, flagSet)); | ||
continue; | ||
} | ||
cleanFlagSets.add(flagSet); | ||
} | ||
return cleanFlagSets; | ||
} | ||
|
||
public static FlagSetsValidResult areValid(List<String> flagSets) { | ||
HashSet<String> cleanFlagSets = cleanup(flagSets); | ||
|
||
return new FlagSetsValidResult(cleanFlagSets != null && cleanFlagSets.size() != 0, cleanFlagSets); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
client/src/test/java/io/split/client/utils/FlagSetsValidatorTest.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,76 @@ | ||
package io.split.client.utils; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
import static io.split.inputValidation.FlagSetsValidator.cleanup; | ||
import static io.split.inputValidation.FlagSetsValidator.areValid; | ||
|
||
public class FlagSetsValidatorTest { | ||
|
||
@Test | ||
public void testEmptyFlagSets() { | ||
List<String> flagSets = new ArrayList<>(); | ||
HashSet<String> cleanFlagSets = cleanup(flagSets); | ||
Assert.assertTrue(cleanFlagSets.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testUpperFlagSets() { | ||
List<String> flagSets = new ArrayList<>(); | ||
flagSets.add("Test1"); | ||
flagSets.add("TEST2"); | ||
HashSet<String> cleanFlagSets = cleanup(flagSets); | ||
Assert.assertTrue(cleanFlagSets.contains("test1")); | ||
Assert.assertTrue(cleanFlagSets.contains("test2")); | ||
} | ||
|
||
@Test | ||
public void testTrimFlagSets() { | ||
List<String> flagSets = new ArrayList<>(); | ||
flagSets.add(" test1"); | ||
flagSets.add(" test2 "); | ||
HashSet<String> cleanFlagSets = cleanup(flagSets); | ||
Assert.assertTrue(cleanFlagSets.contains("test1")); | ||
Assert.assertTrue(cleanFlagSets.contains("test2")); | ||
} | ||
|
||
@Test | ||
public void testRegexFlagSets() { | ||
List<String> flagSets = new ArrayList<>(); | ||
flagSets.add(" test1"); | ||
flagSets.add(" test-2 "); | ||
HashSet<String> cleanFlagSets = cleanup(flagSets); | ||
Assert.assertEquals(1, cleanFlagSets.size()); | ||
Assert.assertTrue(cleanFlagSets.contains("test1")); | ||
Assert.assertFalse(cleanFlagSets.contains("test-2")); | ||
} | ||
|
||
@Test | ||
public void testDuplicateFlagSets() { | ||
List<String> flagSets = new ArrayList<>(); | ||
flagSets.add(" test1"); | ||
flagSets.add(" test1 "); | ||
HashSet<String> cleanFlagSets = cleanup(flagSets); | ||
Assert.assertEquals(1, cleanFlagSets.size()); | ||
Assert.assertTrue(cleanFlagSets.contains("test1")); | ||
} | ||
|
||
@Test | ||
public void testIsValid(){ | ||
Assert.assertTrue(areValid(Arrays.asList(" test1 ")).getValid()); | ||
Assert.assertTrue(areValid(Arrays.asList("Test1 ")).getValid()); | ||
} | ||
|
||
@Test | ||
public void testIsNotValid(){ | ||
Assert.assertFalse(areValid(Arrays.asList(" test 1 ")).getValid()); | ||
Assert.assertFalse(areValid(Arrays.asList("")).getValid()); | ||
Assert.assertFalse(areValid(null).getValid()); | ||
} | ||
} |