From c9a9447a5630046a88685a3c7837be43b5ea9909 Mon Sep 17 00:00:00 2001 From: Oleksandr Mordyk Date: Thu, 31 Oct 2024 03:14:55 -0700 Subject: [PATCH 1/4] issue 607: Rework unit-test for version - Increased the number of test cases to cover corner cases. - Fixed minor issues found during refactoring. Signed-off-by: Oleksandr Mordyk --- .../exchangeapi/VersionSuite.scala | 55 -------------- .../route/version/TestVersion.scala | 74 +++++++++++++++++++ .../route/version/TestVersionRange.scala | 63 ++++++++++++++++ 3 files changed, 137 insertions(+), 55 deletions(-) delete mode 100644 src/test/scala/org/openhorizon/exchangeapi/VersionSuite.scala create mode 100644 src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala create mode 100644 src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersionRange.scala diff --git a/src/test/scala/org/openhorizon/exchangeapi/VersionSuite.scala b/src/test/scala/org/openhorizon/exchangeapi/VersionSuite.scala deleted file mode 100644 index 195345d3..00000000 --- a/src/test/scala/org/openhorizon/exchangeapi/VersionSuite.scala +++ /dev/null @@ -1,55 +0,0 @@ -package org.openhorizon.exchangeapi - -import org.scalatest.funsuite.AnyFunSuite -import org.junit.runner.RunWith -import org.scalatestplus.junit.JUnitRunner -import org.openhorizon.exchangeapi._ -import org.openhorizon.exchangeapi.utility.{Version, VersionRange} - -/** - * Tests for the Version and VersionRange case classes - */ -@RunWith(classOf[JUnitRunner]) -class VersionSuite extends AnyFunSuite { - test("Version tests") { - assert(Version("1.2.3").isValid) - assert(Version("infinity").isValid) - assert(Version("Infinity").isValid) - assert(Version("INFINITY").isValid) - assert(!Version("1.2.3.4").isValid) - assert(!Version("x").isValid) - assert(Version("1.2.3").toString === "1.2.3") - assert(Version("1.0.0") === Version("1")) - assert(Version("1.2.3") != Version("1.3.2")) - assert(Version("2.2.3") > Version("1.3.2")) - assert(!(Version("1.2.3") > Version("1.3.2"))) - assert(Version("infinity") > Version("1.3.2")) - assert(!(Version("1.2.3") > Version("INFINITY"))) - assert(Version("1.2.3") >= Version("1.2.3")) - assert(Version("1.3.3") >= Version("1.2.3")) - assert(!(Version("1.2.2") >= Version("1.2.3"))) - } - - test("VersionRange tests") { - assert(VersionRange("1").toString === "[1.0.0,infinity)") - assert(!VersionRange("1,x").isValid) - assert(VersionRange("1,infinity]").isValid) - assert(VersionRange("1,INFINITY]").isValid) - assert(VersionRange("1").isValid) - assert(Version("1.2") in VersionRange("1")) - assert(Version("1.2") notIn VersionRange(" 1, 1.1")) - assert(Version("1.2") in VersionRange("1.2")) - assert(Version("1.2") notIn VersionRange("(1.2")) - assert(Version("1.2.3") in VersionRange("1.2,1.2.3]")) - assert(Version("1.2.3") in VersionRange("1.2")) - assert(Version("1.2.3") in VersionRange("1.2,")) - assert(Version("1.2.3") notIn VersionRange("(1.2,1.2.3")) - assert(Version("1.2.3") notIn VersionRange("(1.2,1.2.3)")) - assert(Version("1.2.3") in VersionRange("1.2,infinity")) - assert(Version("1.2.3") in VersionRange("1.2,INFINITY")) - assert(Version("1.2.3") in VersionRange("1.2,1.4")) - assert(Version("1.2.3") in VersionRange("[1.0.0,2.0.0)")) - assert(Version("1.0.0") in VersionRange("[1.0.0,2.0.0)")) - assert(Version("2.0.0") notIn VersionRange("[1.0.0,2.0.0)")) - } -} \ No newline at end of file diff --git a/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala b/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala new file mode 100644 index 00000000..959af7c6 --- /dev/null +++ b/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala @@ -0,0 +1,74 @@ +//package org.openhorizon.exchangeapi.route.version + +package org.openhorizon.exchangeapi +import org.scalatest.funsuite.AnyFunSuite +import org.scalatest.matchers.should.Matchers +import org.junit.runner.RunWith +import org.scalatestplus.junit.JUnitRunner +import org.openhorizon.exchangeapi._ +import org.openhorizon.exchangeapi.utility.{Version, VersionRange} + +/** + * Tests for the Version + */ +@RunWith(classOf[JUnitRunner]) +class TestVersion extends AnyFunSuite with Matchers { + test("Version validity tests") { + // Valid versions + assert(Version("1.2.3").isValid) + assert(Version("1.0.0").isValid) + assert(Version("0.0.0").isValid) + assert(Version("infinity").isValid) + assert(Version("Infinity").isValid) + assert(Version("INFINITY").isValid) + + // Invalid versions + assert(!Version("1.2.3.4").isValid) // Too many segments + assert(!Version("x").isValid) // Non-numeric + assert(!Version("").isValid) // Empty string + assert(!Version("1.2.a").isValid) // Invalid character + assert(!Version("1..2").isValid) // Double dot + assert(!Version("1.2..3").isValid) // Double dot in the middle + assert(!Version("1.2.-3").isValid) // Negative number + assert(!Version("-1.2.3").isValid) // Negative number at start + assert(!Version("1.2.3-").isValid) // Hyphen at the end + } + + test("Version string representation") { + assert(Version("1.2.3").toString === "1.2.3") + assert(Version("infinity").toString === "infinity") + assert(Version("0.0.0").toString === "0.0.0") + } + + test("Version equality tests") { + assert(Version("1.0.0") === Version("1")) + assert(Version("1.2.3") === Version("1.2.3")) + assert(Version("1.2.3") != Version("1.3.2")) + assert(Version("0.0.0") === Version("0.0.0")) + } + + test("Version comparison tests") { + assert(Version("2.2.3") > Version("1.3.2")) + assert(Version("1.2.3") > Version("1.2.2")) + assert(Version("1.2.3") >= Version("1.2.3")) + assert(Version("1.3.3") >= Version("1.2.3")) + assert(!(Version("1.2.2") >= Version("1.2.3"))) + + assert(Version("infinity") > Version("1.3.2")) + assert(!(Version("1.2.3") > Version("INFINITY"))) + + // Testing with leading zeros + assert(Version("1.2.3") === Version("01.2.3")) + assert(Version("1.2.3") > Version("1.2.02")) + assert(Version("1.2.3") >= Version("1.02.3")) + } + + test("Edge cases and performance tests") { + // Check upper limits + assert(Version("999999999.999999999.999999999").isValid) + + // Check behavior with invalid but interesting formats + assert(Version("1.0.0").isValid) + assert(Version("1.0").isValid) + } +} \ No newline at end of file diff --git a/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersionRange.scala b/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersionRange.scala new file mode 100644 index 00000000..6e370448 --- /dev/null +++ b/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersionRange.scala @@ -0,0 +1,63 @@ +package org.openhorizon.exchangeapi +import org.scalatest.funsuite.AnyFunSuite +import org.scalatest.matchers.should.Matchers +import org.junit.runner.RunWith +import org.scalatestplus.junit.JUnitRunner +import org.openhorizon.exchangeapi._ +import org.openhorizon.exchangeapi.utility.{Version, VersionRange} + +/** + * Tests for the Version Range + */ +@RunWith(classOf[JUnitRunner]) +class TestVersionRange extends AnyFunSuite with Matchers { + test("VersionRange tests") { + // Basic string representation tests + assert(VersionRange("1").toString === "[1.0.0,infinity)") + assert(VersionRange("1,infinity]").toString === "[1.0.0,infinity]") + assert(VersionRange("1.2,2").toString === "[1.2.0,2.0.0)") + + // Validity tests + assert(!VersionRange("1,x").isValid) // Invalid due to non-numeric + assert(VersionRange("1,infinity]").isValid) // Valid range with infinity + assert(VersionRange("1,INFINITY]").isValid) // Case insensitivity + assert(VersionRange("1").isValid) // Single version as valid range + assert(VersionRange("1.0.0").isValid) // Valid single version + assert(VersionRange("1.2,2.0.0").isValid) // Valid range + + // Inclusion tests + assert(Version("1.2") in VersionRange("1")) // Included in range starting with 1 + assert(Version("1.2") notIn VersionRange("1, 1.1")) // Not included in this range + assert(Version("1.2") in VersionRange("1.2")) // Exact match + assert(Version("1.2") notIn VersionRange("(1.2")) // Not included due to exclusive start + assert(Version("1.2.3") in VersionRange("1.2,1.2.3]")) // Included in inclusive end range + assert(Version("1.2.3") in VersionRange("1.2")) // Included in single version range + assert(Version("1.2.3") in VersionRange("1.2,")) // Open-ended range + assert(Version("1.2.3") notIn VersionRange("(1.2,1.2.3")) // Exclusive lower bound + assert(Version("1.2.3") notIn VersionRange("(1.2,1.2.3)")) // Exclusive bounds + assert(Version("1.2.3") in VersionRange("1.2,infinity")) // Open-ended to infinity + assert(Version("1.2.3") in VersionRange("1.2,INFINITY")) // Case insensitivity for infinity + assert(Version("1.2.3") in VersionRange("1.2,1.4")) // Included in this range + assert(Version("1.2.3") in VersionRange("[1.0.0,2.0.0)")) // Within valid range + assert(Version("1.0.0") in VersionRange("[1.0.0,2.0.0)")) // Exact match + assert(Version("2.0.0") notIn VersionRange("[1.0.0,2.0.0)")) // Outside range + } + + test("Additional VersionRange edge cases") { + // Test for overlapping ranges + assert(Version("1.5") in VersionRange("1.0,1.6")) // In overlapping range + assert(Version("1.0") in VersionRange("[1.0,1.5)")) // Lower bound inclusive + assert(Version("1.5") in VersionRange("(1.0,2.0)")) // Upper bound exclusive + assert(Version("1.0") notIn VersionRange("(1.0,1.5)")) // Exclusive lower bound + + // Edge case with negative version numbers + assert(!VersionRange("-1.0,0").isValid) // Invalid range with negative version + } + + test("Invalid VersionRange formats") { + assert(!VersionRange("").isValid) // Empty string + assert(!VersionRange(",2").isValid) // Invalid start + assert(!VersionRange("1,2,3").isValid) // Too many elements + assert(!VersionRange("1.0,)2.0").isValid) // Unmatched parentheses and invalid character + } +} From 4203727a8d5b68b17ab1abdf2502f453b1564611 Mon Sep 17 00:00:00 2001 From: Oleksandr Mordyk Date: Thu, 31 Oct 2024 03:33:47 -0700 Subject: [PATCH 2/4] issue 607: Rework unit-test for version - Fix checking for empty version and invalid start. Signed-off-by: Oleksandr Mordyk --- .../org/openhorizon/exchangeapi/utility/VersionRange.scala | 7 ++++++- .../exchangeapi/route/version/TestVersion.scala | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/scala/org/openhorizon/exchangeapi/utility/VersionRange.scala b/src/main/scala/org/openhorizon/exchangeapi/utility/VersionRange.scala index 9222340d..0777e01e 100644 --- a/src/main/scala/org/openhorizon/exchangeapi/utility/VersionRange.scala +++ b/src/main/scala/org/openhorizon/exchangeapi/utility/VersionRange.scala @@ -34,7 +34,12 @@ final case class VersionRange(range: String) { case _ => (Version("x"), true) } - def isValid: Boolean = (floor.isValid && ceiling.isValid) + def isValid: Boolean = { + if (firstPart.trim.isEmpty || secondPart.trim.isEmpty || secondPart.trim.isEmpty) { + return false + } + floor.isValid && ceiling.isValid + } def includes(version: Version): Boolean = { if (floorInclusive) { diff --git a/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala b/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala index 959af7c6..aacf7cb8 100644 --- a/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala +++ b/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala @@ -71,4 +71,4 @@ class TestVersion extends AnyFunSuite with Matchers { assert(Version("1.0.0").isValid) assert(Version("1.0").isValid) } -} \ No newline at end of file +} From 97064c3ad6533bcec3a096bb23fd14f559ef7b3a Mon Sep 17 00:00:00 2001 From: Oleksandr Mordyk Date: Thu, 31 Oct 2024 04:06:34 -0700 Subject: [PATCH 3/4] issue 607: Update exchange-api version Signed-off-by: Oleksandr Mordyk --- CHANGELOG.md | 4 ++++ docs/openapi-3-developer.json | 2 +- docs/openapi-3-user.json | 2 +- src/main/resources/version.txt | 2 +- .../org/openhorizon/exchangeapi/utility/VersionRange.scala | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4da511a..8c2916b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). +## [2.125.2](https://github.com/open-horizon/exchange-api/pull/726) - 2024-10-31 +- issue 607: Rework unit-test for version. +- Increased the number of test cases to cover corner cases for Version. +- Fixed issues below that were found during refactoring test version suite. ## [2.125.1](https://github.com/open-horizon/exchange-api/pull/725) - 2024-10-22 - Issue 724: Version conflicts in library(pekko-http) dependencies diff --git a/docs/openapi-3-developer.json b/docs/openapi-3-developer.json index 18e3f59e..0b97b0dc 100644 --- a/docs/openapi-3-developer.json +++ b/docs/openapi-3-developer.json @@ -8,7 +8,7 @@ "name" : "Apache License Version 2.0", "url" : "https://www.apache.org/licenses/LICENSE-2.0" }, - "version" : "2.125.1" + "version" : "2.125.2" }, "externalDocs" : { "description" : "Open-horizon ExchangeAPI", diff --git a/docs/openapi-3-user.json b/docs/openapi-3-user.json index 6bafc98b..01b27f4c 100644 --- a/docs/openapi-3-user.json +++ b/docs/openapi-3-user.json @@ -8,7 +8,7 @@ "name" : "Apache License Version 2.0", "url" : "https://www.apache.org/licenses/LICENSE-2.0" }, - "version" : "2.125.1" + "version" : "2.125.2" }, "externalDocs" : { "description" : "Open-horizon ExchangeAPI", diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt index 0fe613ab..394b36fa 100644 --- a/src/main/resources/version.txt +++ b/src/main/resources/version.txt @@ -1 +1 @@ -2.125.1 +2.125.2 diff --git a/src/main/scala/org/openhorizon/exchangeapi/utility/VersionRange.scala b/src/main/scala/org/openhorizon/exchangeapi/utility/VersionRange.scala index 0777e01e..fe7e89b8 100644 --- a/src/main/scala/org/openhorizon/exchangeapi/utility/VersionRange.scala +++ b/src/main/scala/org/openhorizon/exchangeapi/utility/VersionRange.scala @@ -35,7 +35,7 @@ final case class VersionRange(range: String) { } def isValid: Boolean = { - if (firstPart.trim.isEmpty || secondPart.trim.isEmpty || secondPart.trim.isEmpty) { + if (firstPart.trim.isEmpty || secondPart.trim.isEmpty) { return false } floor.isValid && ceiling.isValid From ff50fb313bc83e3e315123a7b4c32f4005951c0f Mon Sep 17 00:00:00 2001 From: Oleksandr Mordyk Date: Thu, 31 Oct 2024 05:39:10 -0700 Subject: [PATCH 4/4] issue 607: The version test has been moved to the appropriate directory Signed-off-by: Oleksandr Mordyk --- .../exchangeapi/{route => utility}/version/TestVersion.scala | 0 .../exchangeapi/{route => utility}/version/TestVersionRange.scala | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/test/scala/org/openhorizon/exchangeapi/{route => utility}/version/TestVersion.scala (100%) rename src/test/scala/org/openhorizon/exchangeapi/{route => utility}/version/TestVersionRange.scala (100%) diff --git a/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala b/src/test/scala/org/openhorizon/exchangeapi/utility/version/TestVersion.scala similarity index 100% rename from src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersion.scala rename to src/test/scala/org/openhorizon/exchangeapi/utility/version/TestVersion.scala diff --git a/src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersionRange.scala b/src/test/scala/org/openhorizon/exchangeapi/utility/version/TestVersionRange.scala similarity index 100% rename from src/test/scala/org/openhorizon/exchangeapi/route/version/TestVersionRange.scala rename to src/test/scala/org/openhorizon/exchangeapi/utility/version/TestVersionRange.scala