-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validators for HexChar and UUID (#4)
- Loading branch information
Kapil Rastogi
authored
Jun 8, 2020
1 parent
741e44b
commit a3adf2f
Showing
4 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/com/expedia/www/haystack/zipkin/validators/HexCharValidator.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,46 @@ | ||
/* | ||
* Copyright 2020 Expedia Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expedia.www.haystack.zipkin.validators; | ||
|
||
import org.apache.commons.lang3.Validate; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Class to validate 128 / 64 bit HexChar value | ||
*/ | ||
public class HexCharValidator { | ||
private static final String HEXCHAR_128_BIT_PATTERN = "([A-Fa-f0-9]{32})"; | ||
private static final String HEXCHAR_64_BIT_PATTERN = "([A-Fa-f0-9]{16})"; | ||
|
||
public static boolean isValid128BitHexChar(String hexChar) { | ||
Validate.notNull(hexChar, "The given value cannot be null"); | ||
|
||
Pattern pattern = Pattern.compile(HEXCHAR_128_BIT_PATTERN); | ||
Matcher matcher = pattern.matcher(hexChar); | ||
return matcher.matches(); | ||
} | ||
|
||
public static boolean isValid64BitHexChar(String value) { | ||
Validate.notNull(value, "The given value cannot be null"); | ||
|
||
Pattern pattern = Pattern.compile(HEXCHAR_64_BIT_PATTERN); | ||
Matcher matcher = pattern.matcher(value); | ||
return matcher.matches(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/expedia/www/haystack/zipkin/validators/UUIDValidator.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,37 @@ | ||
/* | ||
* Copyright 2020 Expedia Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expedia.www.haystack.zipkin.validators; | ||
|
||
import org.apache.commons.lang3.Validate; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Class to validate a UUID value | ||
*/ | ||
public class UUIDValidator { | ||
private static final String UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}"; | ||
|
||
public static boolean isValidUUID(String uuid) { | ||
Validate.notNull(uuid, "The uuid cannot be null"); | ||
|
||
Pattern pattern = Pattern.compile(UUID_PATTERN); | ||
Matcher matcher = pattern.matcher(uuid); | ||
return matcher.matches(); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/test/scala/com/expedia/www/haystack/zipkin/validators/HexCharValidatorSpec.scala
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,98 @@ | ||
/* | ||
* Copyright 2020 Expedia Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expedia.www.haystack.zipkin.validators | ||
|
||
import unit.UnitTestSpec | ||
|
||
class HexCharValidatorSpec extends UnitTestSpec { | ||
|
||
"HexCharValidator for 128 bit" should { | ||
"validate a 128 bit valid hex char id" in { | ||
|
||
Given("a string of 128 bit hex char value") | ||
val hexCharID = "fee92dfd87cb430fa30922dc540270b5" | ||
|
||
When("HexChar validator is called") | ||
val isValidHexChar = HexCharValidator.isValid128BitHexChar(hexCharID) | ||
|
||
Then("true should be returned") | ||
isValidHexChar should equal(true) | ||
} | ||
|
||
"not validate a 128 bit non hex char id" in { | ||
|
||
Given("an invalid string of 128 bit non hex char value") | ||
val hexCharID = "See92dfd87cb430fa30922dc540270b5" | ||
|
||
When("HexChar validator is called") | ||
val isValidHexChar = HexCharValidator.isValid128BitHexChar(hexCharID) | ||
|
||
Then("false should be returned") | ||
isValidHexChar should equal(false) | ||
} | ||
|
||
"not validate an invalid length 128 bit valid hex char id" in { | ||
|
||
Given("an invalid string of 128 bit non hex char value") | ||
val hexCharID = "afee92dfd87cb430fa30922dc540270b5" | ||
|
||
When("HexChar validator is called") | ||
val isValidHexChar = HexCharValidator.isValid128BitHexChar(hexCharID) | ||
|
||
Then("false should be returned") | ||
isValidHexChar should equal(false) | ||
} | ||
} | ||
|
||
"HexCharValidator for 64 bit" should { | ||
"validate a 64 bit valid hex char id" in { | ||
|
||
Given("a string of 64 bit hex char value") | ||
val hexCharID = "fee92dfd87cb4301" | ||
|
||
When("HexChar validator is called") | ||
val isValidHexChar = HexCharValidator.isValid64BitHexChar(hexCharID) | ||
|
||
Then("true should be returned") | ||
isValidHexChar should equal(true) | ||
} | ||
|
||
"not validate a 64 bit non hex char id" in { | ||
|
||
Given("an invalid string of 64 bit non hex char value") | ||
val hexCharID = "See92dfd87cb4301" | ||
|
||
When("HexChar validator is called") | ||
val isValidHexChar = HexCharValidator.isValid64BitHexChar(hexCharID) | ||
|
||
Then("false should be returned") | ||
isValidHexChar should equal(false) | ||
} | ||
|
||
"not validate an invalid length 64 bit valid hex char id" in { | ||
|
||
Given("an invalid string of 64 bit non hex char value") | ||
val hexCharID = "fee92dfd87cb43012" | ||
|
||
When("HexChar validator is called") | ||
val isValidHexChar = HexCharValidator.isValid64BitHexChar(hexCharID) | ||
|
||
Then("false should be returned") | ||
isValidHexChar should equal(false) | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/test/scala/com/expedia/www/haystack/zipkin/validators/UUIDValidatorSpec.scala
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,60 @@ | ||
/* | ||
* Copyright 2020 Expedia Group. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.expedia.www.haystack.zipkin.validators | ||
|
||
import unit.UnitTestSpec | ||
|
||
class UUIDValidatorSpec extends UnitTestSpec { | ||
|
||
"UUIDValidator" should { | ||
"validate a 128 bit valid hex char id" in { | ||
|
||
Given("a string UUID value") | ||
val uuid = "fee92dfd-87cb-430f-a309-22dc540270b5" | ||
|
||
When("UUID validator is called") | ||
val isValid = UUIDValidator.isValidUUID(uuid) | ||
|
||
Then("true should be returned") | ||
isValid should equal(true) | ||
} | ||
|
||
"not validate an invalid UUID" in { | ||
|
||
Given("an invalid string of UUID") | ||
val uuid = "See92dfd-87cb-430f-a309-22dc540270b5" | ||
|
||
When("UUID validator is called") | ||
val isValid = UUIDValidator.isValidUUID(uuid) | ||
|
||
Then("false should be returned") | ||
isValid should equal(false) | ||
} | ||
|
||
"not validate an invalid length UUID" in { | ||
|
||
Given("an invalid string of 128 bit non hex char value") | ||
val uuid = "Afee92dfd-87cb-430f-a309-22dc540270b5" | ||
|
||
When("UUID validator is called") | ||
val isValid = UUIDValidator.isValidUUID(uuid) | ||
|
||
Then("false should be returned") | ||
isValid should equal(false) | ||
} | ||
} | ||
} |