Skip to content

Commit

Permalink
Merge pull request #2024 from pubudu538/sem-tests
Browse files Browse the repository at this point in the history
Add integration and unit tests for the Semantic versioning feature
  • Loading branch information
pubudu538 authored Feb 29, 2024
2 parents 07525ad + ea9675b commit 3a6804d
Show file tree
Hide file tree
Showing 9 changed files with 1,176 additions and 1 deletion.
684 changes: 684 additions & 0 deletions adapter/internal/discovery/xds/semantic_versioning_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion adapter/pkg/semanticversion/semantic_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (baseVersion SemVersion) Compare(version SemVersion) bool {

// Compare patch version
if baseVersion.Patch != nil && version.Patch != nil {
return *baseVersion.Patch < *version.Patch
return *baseVersion.Patch <= *version.Patch
} else if baseVersion.Patch != nil {
return false
} else if version.Patch != nil {
Expand Down
186 changes: 186 additions & 0 deletions adapter/pkg/semanticversion/semantic_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* 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 semanticversion

import (
"errors"
"testing"
)

func TestSemVersionCompare(t *testing.T) {
tests := []struct {
name string
baseVersion SemVersion
compareVersion SemVersion
expected bool
}{
{
name: "Same versions",
baseVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(3)},
compareVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(3)},
expected: true,
},
{
name: "Base version major is greater",
baseVersion: SemVersion{Major: 2, Minor: 1, Patch: PtrInt(3)},
compareVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(3)},
expected: false,
},
{
name: "Base version minor is greater",
baseVersion: SemVersion{Major: 1, Minor: 3, Patch: PtrInt(3)},
compareVersion: SemVersion{Major: 1, Minor: 4, Patch: PtrInt(3)},
expected: true,
},
{
name: "Base version patch is greater",
baseVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(4)},
compareVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(3)},
expected: false,
},
{
name: "Compare version major is greater",
baseVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(3)},
compareVersion: SemVersion{Major: 2, Minor: 2, Patch: PtrInt(3)},
expected: true,
},
{
name: "Compare version minor is greater",
baseVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(3)},
compareVersion: SemVersion{Major: 1, Minor: 3, Patch: PtrInt(3)},
expected: true,
},
{
name: "Compare version patch is greater",
baseVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(3)},
compareVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(4)},
expected: true,
},
{
name: "Base version patch is nil",
baseVersion: SemVersion{Major: 1, Minor: 2},
compareVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(4)},
expected: true,
},
{
name: "Compare version patch is nil",
baseVersion: SemVersion{Major: 1, Minor: 2, Patch: PtrInt(3)},
compareVersion: SemVersion{Major: 1, Minor: 2},
expected: false,
},
{
name: "Both patch versions are nil",
baseVersion: SemVersion{Major: 1, Minor: 2},
compareVersion: SemVersion{Major: 1, Minor: 2},
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.baseVersion.Compare(tt.compareVersion)
if result != tt.expected {
t.Errorf("Expected %v, but got %v", tt.expected, result)
}
})
}
}

func TestValidateAndGetVersionComponents(t *testing.T) {
tests := []struct {
name string
version string
apiName string
expectedResult *SemVersion
expectedError error
}{
{
name: "Valid version format",
version: "v1.2.3",
apiName: "TestAPI",
expectedResult: &SemVersion{Version: "v1.2.3", Major: 1, Minor: 2, Patch: PtrInt(3)},
expectedError: nil,
},
{
name: "Valid version format without patch",
version: "v1.2",
apiName: "TestAPI",
expectedResult: &SemVersion{Version: "v1.2", Major: 1, Minor: 2, Patch: nil},
expectedError: nil,
},
{
name: "Invalid version format - missing 'v' prefix",
version: "1.2.3",
apiName: "TestAPI",
expectedResult: nil,
expectedError: errors.New("Invalid version: 1.2.3 for API: TestAPI. API version should be in the format x.y.z, x.y, vx.y.z or vx.y where x,y,z are non-negative integers and v is version prefix"),
},
{
name: "Invalid version format - negative major version",
version: "v-1.2.3",
apiName: "TestAPI",
expectedResult: nil,
expectedError: errors.New("invalid version format"),
},
{
name: "Invalid version format - negative minor version",
version: "v1.-2.3",
apiName: "TestAPI",
expectedResult: nil,
expectedError: errors.New("invalid version format"),
},
{
name: "Invalid version format - patch version not an integer",
version: "v1.2.three",
apiName: "TestAPI",
expectedResult: nil,
expectedError: errors.New("invalid version format"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := ValidateAndGetVersionComponents(tt.version, tt.apiName)

// Check for errors
if (err != nil && tt.expectedError == nil) || (err == nil && tt.expectedError != nil) || (err != nil && tt.expectedError != nil && err.Error() != tt.expectedError.Error()) {
t.Errorf("Unexpected error. Expected: %v, Got: %v", tt.expectedError, err)
}

// Check for nil results
if result == nil && tt.expectedResult != nil {
t.Errorf("Unexpected nil result")
} else if result != nil && tt.expectedResult == nil {
t.Errorf("Unexpected non-nil result")
}

// Check for result equality
if result != nil && tt.expectedResult != nil {
if result.Version != tt.expectedResult.Version || result.Major != tt.expectedResult.Major || result.Minor != tt.expectedResult.Minor || (result.Patch != nil && (*result.Patch != *tt.expectedResult.Patch)) {
t.Errorf("Unexpected result. Expected: %v, Got: %v", tt.expectedResult, result)
}
}
})
}

}

// PtrInt returns a pointer to an integer value
func PtrInt(i int) *int {
return &i
}
76 changes: 76 additions & 0 deletions test/cucumber-tests/CRs/artifacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,61 @@ data:
"body": "{\n \"keys\":[\n {\n \"kty\":\"RSA\",\n \"n\":\"m0YNpM5MVYToWZMZ9wL4KQOygvG0f6y0dw4wZ02T4C3SxiC1zEBCZLh2clj7bncyA3EV2bFrTIBNeq-1pFEfbNDMZB88Jcg0S9QyYujr6GM0AqLA7WjZQ6lLxLpeQdEQroEZI-c8rnGmzU8Qb25aiPbRf6Vh7vFYGQz5FnZ8E0LcEMYQ-4KPMkAqnMon1UKWDkqszTY5a-DGMAi5w7imKzXaU4qiEKVKIcezv9nLUVC5Od0T4FkUQi462ZA9SoHx1HNhcVAj8Nf9TG_C65GbsMMFJVcRXwZR99cVzVxVqEtxGlK7Qr0woYKQ3S5kHZPRFcMFXI6WHhEQXqyOMBdUfQ\",\n \"e\":\"AQAB\",\n \"alg\":\"RS256\",\n \"kid\":\"123-456\",\n \"use\":\"sig\"\n }\n ]\n}"
}
}
sem-versioning.json: |
{"mappings": [
{
"request": {
"method": "GET",
"url": "/sem-api/v1.0/employee"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\n \"version\":\"v1.0\" \n}"
}
},
{
"request": {
"method": "GET",
"url": "/sem-api/v1.1/employee"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\n \"version\":\"v1.1\" \n}"
}
},
{
"request": {
"method": "GET",
"url": "/sem-api/v1.5/employee"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\n \"version\":\"v1.5\" \n}"
}
},
{
"request": {
"method": "GET",
"url": "/sem-api/v2.1/employee"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\n \"version\":\"v2.1\" \n}"
}
}
]}
---
kind: TokenIssuer
apiVersion: dp.wso2.com/v1alpha1
Expand Down Expand Up @@ -818,3 +873,24 @@ data:
y5Oi4A4+id+xO0XnHIkkqCfPtFzxl3hwytcy8EqISynzzHWNJ8bFZIYX4tgX+PLq
u0/ITEw=
-----END CERTIFICATE-----
---
apiVersion: cp.wso2.com/v1alpha2
kind: Subscription
metadata:
name: semantic-versioning-subscription
namespace: apk-integration-test
spec:
organization: "default"
subscriptionStatus: "ACTIVE"
api:
name: "Semantic Versioning API"
version: "v\\d+(\\.\\d+)?"
---
apiVersion: cp.wso2.com/v1alpha2
kind: ApplicationMapping
metadata:
name: semantic-versioning-app-mapping
namespace: apk-integration-test
spec:
applicationRef: 583e4146-7ef5-11ee-b962-0242ac120003
subscriptionRef: semantic-versioning-subscription
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: "Semantic Versioning API"
basePath: "/sem-api"
version: "v1.0"
id: "sem-api-v1-0"
type: "REST"
defaultVersion: false
subscriptionValidation: true
endpointConfigurations:
production:
endpoint: "http://dynamic-backend-service:8080/sem-api/v1.0"
operations:
- target: "/employee"
verb: "GET"
secured: true
scopes: []
- target: "/employee"
verb: "POST"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "PUT"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "DELETE"
secured: true
scopes: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: "Semantic Versioning API"
basePath: "/sem-api"
version: "v1.1"
id: "sem-api-v1-1"
type: "REST"
defaultVersion: false
subscriptionValidation: true
endpointConfigurations:
production:
endpoint: "http://dynamic-backend-service:8080/sem-api/v1.1"
operations:
- target: "/employee"
verb: "GET"
secured: true
scopes: []
- target: "/employee"
verb: "POST"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "PUT"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "DELETE"
secured: true
scopes: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: "Semantic Versioning API"
basePath: "/sem-api"
version: "v1.5"
id: "sem-api-v1-5"
type: "REST"
defaultVersion: false
subscriptionValidation: true
endpointConfigurations:
production:
endpoint: "http://dynamic-backend-service:8080/sem-api/v1.5"
operations:
- target: "/employee"
verb: "GET"
secured: true
scopes: []
- target: "/employee"
verb: "POST"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "PUT"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "DELETE"
secured: true
scopes: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: "Semantic Versioning API"
basePath: "/sem-api"
version: "v2.1"
id: "sem-api-v2-1"
type: "REST"
defaultVersion: false
endpointConfigurations:
production:
endpoint: "http://dynamic-backend-service:8080/sem-api/v2.1"
operations:
- target: "/employee"
verb: "GET"
secured: true
scopes: []
- target: "/employee"
verb: "POST"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "PUT"
secured: true
scopes: []
- target: "/employee/{employeeId}"
verb: "DELETE"
secured: true
scopes: []
Loading

0 comments on commit 3a6804d

Please sign in to comment.