Skip to content

Commit 43bc504

Browse files
author
Subba Rao
committed
Adding null tests, standardize returns Option[String]
1 parent 8034824 commit 43bc504

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

util-core/src/main/scala/com/indix/utils/core/MPN.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object MPN {
1515

1616
def isValidIdentifier(input: String): Boolean = {
1717
input match {
18-
case _ if input.length > MaxLen || input.length < MinLen => false
18+
case _ if StringUtils.isBlank(input) || input.length > MaxLen || input.length < MinLen => false
1919
case _ if input.count(c => TerminateChars.contains(c)) > 1 => false
2020
case _ if BlackListedMpns.contains(input.toLowerCase) => false
2121
case _ if isTitleCase(input) => false
@@ -31,13 +31,14 @@ object MPN {
3131
else words.forall(w => w == WordUtils.capitalizeFully(w) && !StringUtils.isNumeric(w))
3232
}
3333

34-
def standardizeMPN(input: String): String = {
34+
def standardizeMPN(input: String): Option[String] = {
3535
if (isValidIdentifier(input)) {
36-
input
36+
Some(input)
37+
} else if (StringUtils.isBlank(input)) {
38+
None
39+
} else if (input.indexWhere(c => TerminateChars.contains(c)) > 0) {
40+
Some(input.substring(0, input.indexWhere(c => TerminateChars.contains(c))))
3741
}
38-
else if (input.indexWhere(c => TerminateChars.contains(c)) > 0) {
39-
input.substring(0, input.indexWhere(c => TerminateChars.contains(c)))
40-
}
41-
else ""
42+
else None
4243
}
4344
}

util-core/src/test/scala/com/indix/utils/core/MPNSpec.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class MPNSpec extends FlatSpec with Matchers {
1717
}
1818

1919
it should "validate identifier" in {
20+
MPN.isValidIdentifier(null) should be (false)
2021
MPN.isValidIdentifier("") should be (false)
2122
MPN.isValidIdentifier("51") should be (false)
2223
MPN.isValidIdentifier("NA") should be (false)
@@ -31,10 +32,12 @@ class MPNSpec extends FlatSpec with Matchers {
3132
}
3233

3334
it should "standardize MPN" in {
34-
MPN.standardizeMPN("Does not apply") should be ("")
35-
MPN.standardizeMPN("PJS2V") should be ("PJS2V")
36-
MPN.standardizeMPN("105200010437-07-70% All Windows") should be ("105200010437-07-70")
37-
MPN.standardizeMPN("30634190, 30753839, 31253006") should be ("30634190")
35+
MPN.standardizeMPN(null) should be (None)
36+
MPN.standardizeMPN("Does not apply") should be (None)
37+
MPN.standardizeMPN("PJS2V") should be (Some("PJS2V"))
38+
39+
MPN.standardizeMPN("105200010437-07-70% All Windows") should be (Some("105200010437-07-70"))
40+
MPN.standardizeMPN("30634190, 30753839, 31253006") should be (Some("30634190"))
3841
}
3942

4043
}

0 commit comments

Comments
 (0)