Skip to content

Commit

Permalink
Validators for HexChar and UUID (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapil Rastogi authored Jun 8, 2020
1 parent 741e44b commit a3adf2f
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 0 deletions.
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();
}
}
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();
}
}
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)
}
}
}
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)
}
}
}

0 comments on commit a3adf2f

Please sign in to comment.