2023-05-03 18:02:11 +02:00
//go:build unit
// +build unit
2020-08-04 17:52:28 +02:00
package abaputils
import (
2022-06-29 15:50:33 +02:00
"encoding/json"
2022-02-11 11:16:40 +02:00
"fmt"
2023-04-04 12:46:03 +02:00
"math"
2020-10-05 14:38:35 +02:00
"os"
2020-08-04 17:52:28 +02:00
"testing"
"github.com/stretchr/testify/assert"
)
2022-06-29 15:50:33 +02:00
var executionLogString string
func init ( ) {
2023-03-31 15:26:38 +02:00
executionLog := LogProtocolResults {
2023-04-04 12:46:03 +02:00
Count : fmt . Sprint ( math . Round ( numberOfEntriesPerPage * 1.5 ) ) ,
2023-03-31 15:26:38 +02:00
Results : [ ] LogProtocol {
{
ProtocolLine : 1 ,
OverviewIndex : 1 ,
Type : "LogEntry" ,
Description : "S" ,
Timestamp : "/Date(1644332299000+0000)/" ,
2022-06-29 15:50:33 +02:00
} ,
} ,
}
2023-03-31 15:26:38 +02:00
2022-06-29 15:50:33 +02:00
executionLogResponse , _ := json . Marshal ( executionLog )
executionLogString = string ( executionLogResponse )
}
2023-03-31 15:26:38 +02:00
2020-08-04 17:52:28 +02:00
func TestPollEntity ( t * testing . T ) {
t . Run ( "Test poll entity - success case" , func ( t * testing . T ) {
2022-02-11 11:16:40 +02:00
logResultSuccess := fmt . Sprintf ( ` { "d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Success", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } } ` )
2020-08-04 17:52:28 +02:00
client := & ClientMock {
BodyList : [ ] string {
2023-04-04 12:46:03 +02:00
` { "d" : ` + executionLogString + ` } ` ,
2022-06-29 15:50:33 +02:00
` { "d" : ` + executionLogString + ` } ` ,
2022-02-11 11:16:40 +02:00
logResultSuccess ,
2020-08-04 17:52:28 +02:00
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
2023-11-28 14:26:31 +02:00
` { "d" : { "status" : "Q" } } ` ,
` { } ` ,
2020-08-04 17:52:28 +02:00
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
con := ConnectionDetailsHTTP {
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
XCsrfToken : "MY_TOKEN" ,
}
2023-11-28 14:26:31 +02:00
2023-12-19 16:16:48 +02:00
swcManager := SoftwareComponentApiManager { Client : client , Force0510 : true }
2023-11-28 14:26:31 +02:00
repo := Repository { Name : "testRepo1" }
api , _ := swcManager . GetAPI ( con , repo )
status , _ := PollEntity ( api , 0 )
2020-08-04 17:52:28 +02:00
assert . Equal ( t , "S" , status )
2023-03-31 15:26:38 +02:00
assert . Equal ( t , 0 , len ( client . BodyList ) , "Not all requests were done" )
2020-08-04 17:52:28 +02:00
} )
t . Run ( "Test poll entity - error case" , func ( t * testing . T ) {
2022-02-11 11:16:40 +02:00
logResultError := fmt . Sprintf ( ` { "d": { "sc_name": "/DMO/SWC", "status": "S", "to_Log_Overview": { "results": [ { "log_index": 1, "log_name": "Main Import", "type_of_found_issues": "Error", "timestamp": "/Date(1644332299000+0000)/", "to_Log_Protocol": { "results": [ { "log_index": 1, "index_no": "1", "log_name": "", "type": "Info", "descr": "Main import", "timestamp": null, "criticality": 0 } ] } } ] } } } ` )
2020-08-04 17:52:28 +02:00
client := & ClientMock {
BodyList : [ ] string {
2023-04-04 12:46:03 +02:00
` { "d" : ` + executionLogString + ` } ` ,
2022-06-29 15:50:33 +02:00
` { "d" : ` + executionLogString + ` } ` ,
2022-02-11 11:16:40 +02:00
logResultError ,
2020-08-04 17:52:28 +02:00
` { "d" : { "status" : "E" } } ` ,
` { "d" : { "status" : "R" } } ` ,
2023-11-13 22:17:49 +02:00
` { "d" : { "status" : "Q" } } ` ,
2023-11-28 14:26:31 +02:00
` { } ` ,
2020-08-04 17:52:28 +02:00
} ,
Token : "myToken" ,
StatusCode : 200 ,
}
con := ConnectionDetailsHTTP {
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
XCsrfToken : "MY_TOKEN" ,
}
2023-11-28 14:26:31 +02:00
2023-12-19 16:16:48 +02:00
swcManager := SoftwareComponentApiManager { Client : client , Force0510 : true }
2023-11-28 14:26:31 +02:00
repo := Repository { Name : "testRepo1" }
api , _ := swcManager . GetAPI ( con , repo )
status , _ := PollEntity ( api , 0 )
2020-08-04 17:52:28 +02:00
assert . Equal ( t , "E" , status )
2023-03-31 15:26:38 +02:00
assert . Equal ( t , 0 , len ( client . BodyList ) , "Not all requests were done" )
2020-08-04 17:52:28 +02:00
} )
}
2020-10-05 14:38:35 +02:00
func TestGetRepositories ( t * testing . T ) {
t . Run ( "Get Repositories from config - success case" , func ( t * testing . T ) {
expectedRepositoryList := [ ] Repository { {
Name : "testRepo" ,
Branch : "testBranch" ,
} , {
Name : "testRepo2" ,
Branch : "testBranch2" ,
} , {
Name : "testRepo3" ,
Branch : "testBranch3" ,
} , {
Name : "testRepository" ,
Branch : "testBranch" ,
} , {
Name : "testRepository" ,
} }
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2020-10-05 14:38:35 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
defer func ( ) {
_ = os . Chdir ( oldCWD )
} ( )
manifestFileString := `
repositories :
- name : ' testRepo '
branch : ' testBranch '
- name : ' testRepo2 '
branch : ' testBranch2 '
- name : ' testRepo3 '
branch : ' testBranch3 ' `
2023-08-16 12:57:04 +02:00
err := os . WriteFile ( "repositoriesTest.yml" , [ ] byte ( manifestFileString ) , 0644 )
2020-10-05 14:38:35 +02:00
config := RepositoriesConfig {
BranchName : "testBranch" ,
RepositoryName : "testRepository" ,
RepositoryNames : [ ] string { "testRepository" } ,
Repositories : "repositoriesTest.yml" ,
}
2022-06-28 11:02:15 +02:00
repositories , err := GetRepositories ( & config , true )
2020-10-05 14:38:35 +02:00
assert . Equal ( t , expectedRepositoryList , repositories )
assert . NoError ( t , err )
} )
t . Run ( "Get Repositories from file config - failure case" , func ( t * testing . T ) {
expectedRepositoryList := [ ] Repository ( [ ] Repository { } )
expectedErrorMessage := "Error in config file repositoriesTest.yml, AddonDescriptor doesn't contain any repositories"
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2020-10-05 14:38:35 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
defer func ( ) {
_ = os . Chdir ( oldCWD )
} ( )
manifestFileString := `
repositories :
- repo : ' testRepo '
- repo : ' testRepo2 ' `
2023-08-16 12:57:04 +02:00
err := os . WriteFile ( "repositoriesTest.yml" , [ ] byte ( manifestFileString ) , 0644 )
2020-10-05 14:38:35 +02:00
config := RepositoriesConfig {
Repositories : "repositoriesTest.yml" ,
}
2022-06-28 11:02:15 +02:00
repositories , err := GetRepositories ( & config , false )
2020-10-05 14:38:35 +02:00
assert . Equal ( t , expectedRepositoryList , repositories )
assert . EqualError ( t , err , expectedErrorMessage )
} )
t . Run ( "Get Repositories from config - failure case" , func ( t * testing . T ) {
expectedRepositoryList := [ ] Repository ( [ ] Repository { } )
expectedErrorMessage := "Error in config file repositoriesTest.yml, AddonDescriptor doesn't contain any repositories"
2022-07-12 15:19:12 +02:00
dir := t . TempDir ( )
2020-10-05 14:38:35 +02:00
oldCWD , _ := os . Getwd ( )
_ = os . Chdir ( dir )
defer func ( ) {
_ = os . Chdir ( oldCWD )
} ( )
manifestFileString := `
repositories :
- repo : ' testRepo '
- repo : ' testRepo2 ' `
2023-08-16 12:57:04 +02:00
err := os . WriteFile ( "repositoriesTest.yml" , [ ] byte ( manifestFileString ) , 0644 )
2020-10-05 14:38:35 +02:00
config := RepositoriesConfig {
Repositories : "repositoriesTest.yml" ,
}
2022-06-28 11:02:15 +02:00
repositories , err := GetRepositories ( & config , false )
2020-10-05 14:38:35 +02:00
assert . Equal ( t , expectedRepositoryList , repositories )
assert . EqualError ( t , err , expectedErrorMessage )
} )
t . Run ( "Get Repositories from empty config - failure case" , func ( t * testing . T ) {
expectedRepositoryList := [ ] Repository ( [ ] Repository { } )
expectedErrorMessage := "Failed to read repository configuration: You have not specified any repository configuration. Please make sure that you have correctly specified it. For more information please read the User documentation"
config := RepositoriesConfig { }
2022-06-28 11:02:15 +02:00
repositories , err := GetRepositories ( & config , false )
2020-10-05 14:38:35 +02:00
assert . Equal ( t , expectedRepositoryList , repositories )
assert . EqualError ( t , err , expectedErrorMessage )
} )
}
2020-11-02 15:17:13 +02:00
2021-12-20 18:58:58 +02:00
func TestCreateLogStrings ( t * testing . T ) {
t . Run ( "Clone LogString Tag and Commit" , func ( t * testing . T ) {
repo := Repository {
Name : "/DMO/REPO" ,
Branch : "main" ,
CommitID : "1234567" ,
Tag : "myTag" ,
}
logString := repo . GetCloneLogString ( )
assert . Equal ( t , "repository / software component '/DMO/REPO', branch 'main', commit '1234567'" , logString , "Expected different string" )
} )
t . Run ( "Clone LogString Tag" , func ( t * testing . T ) {
repo := Repository {
Name : "/DMO/REPO" ,
Branch : "main" ,
Tag : "myTag" ,
}
logString := repo . GetCloneLogString ( )
assert . Equal ( t , "repository / software component '/DMO/REPO', branch 'main', tag 'myTag'" , logString , "Expected different string" )
} )
t . Run ( "Pull LogString Tag and Commit" , func ( t * testing . T ) {
repo := Repository {
Name : "/DMO/REPO" ,
Branch : "main" ,
CommitID : "1234567" ,
Tag : "myTag" ,
}
logString := repo . GetPullLogString ( )
assert . Equal ( t , "repository / software component '/DMO/REPO', commit '1234567'" , logString , "Expected different string" )
} )
t . Run ( "Pull LogString Tag" , func ( t * testing . T ) {
repo := Repository {
Name : "/DMO/REPO" ,
Branch : "main" ,
Tag : "myTag" ,
}
logString := repo . GetPullLogString ( )
assert . Equal ( t , "repository / software component '/DMO/REPO', tag 'myTag'" , logString , "Expected different string" )
} )
}
func TestCreateRequestBodies ( t * testing . T ) {
t . Run ( "Clone Body Tag and Commit" , func ( t * testing . T ) {
repo := Repository {
Name : "/DMO/REPO" ,
Branch : "main" ,
CommitID : "1234567" ,
Tag : "myTag" ,
}
body := repo . GetCloneRequestBody ( )
assert . Equal ( t , ` { "sc_name":"/DMO/REPO", "branch_name":"main", "commit_id":"1234567"} ` , body , "Expected different body" )
} )
t . Run ( "Clone Body Tag" , func ( t * testing . T ) {
repo := Repository {
Name : "/DMO/REPO" ,
Branch : "main" ,
Tag : "myTag" ,
}
body := repo . GetCloneRequestBody ( )
assert . Equal ( t , ` { "sc_name":"/DMO/REPO", "branch_name":"main", "tag_name":"myTag"} ` , body , "Expected different body" )
} )
t . Run ( "Pull Body Tag and Commit" , func ( t * testing . T ) {
repo := Repository {
Name : "/DMO/REPO" ,
Branch : "main" ,
CommitID : "1234567" ,
Tag : "myTag" ,
}
body := repo . GetPullRequestBody ( )
assert . Equal ( t , ` { "sc_name":"/DMO/REPO", "commit_id":"1234567"} ` , body , "Expected different body" )
2020-11-02 15:17:13 +02:00
} )
2021-12-20 18:58:58 +02:00
t . Run ( "Pull Body Tag" , func ( t * testing . T ) {
repo := Repository {
Name : "/DMO/REPO" ,
Branch : "main" ,
Tag : "myTag" ,
}
body := repo . GetPullRequestBody ( )
assert . Equal ( t , ` { "sc_name":"/DMO/REPO", "tag_name":"myTag"} ` , body , "Expected different body" )
2020-11-02 15:17:13 +02:00
} )
}