Skip to content

Commit

Permalink
Merge pull request #726 from open-horizon/issue_607
Browse files Browse the repository at this point in the history
issue 607: Rework unit-test for VersionSuite
  • Loading branch information
naphelps authored Oct 31, 2024
2 parents 9e05b3f + ff50fb3 commit 02f87d8
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 59 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi-3-developer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi-3-user.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.125.1
2.125.2
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
return false
}
floor.isValid && ceiling.isValid
}

def includes(version: Version): Boolean = {
if (floorInclusive) {
Expand Down
55 changes: 0 additions & 55 deletions src/test/scala/org/openhorizon/exchangeapi/VersionSuite.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 02f87d8

Please sign in to comment.