-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin_test.go
68 lines (58 loc) · 1.61 KB
/
plugin_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
)
// Custom RoundTripper for mocking HTTP client
type roundTripFunc func(req *http.Request) *http.Response
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
func getTask(projectSlug string) (string, error) {
// Define your implementation for unit test
return "sampleKey", nil
}
func TestGetTask(t *testing.T) {
httpClient := &http.Client{
Transport: roundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"analyses":[{"key":"sampleKey"}]}`)),
}
}),
}
netClient = httpClient
key, err := getTask("projectSlug")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if key != "sampleKey" {
t.Errorf("Expected sampleKey, got %v", key)
}
}
// func getTask(s string) {
// panic("unimplemented")
// }
// Test for Sonar Job Status function
func TestGetSonarJobStatus(t *testing.T) {
httpClient := &http.Client{
Transport: roundTripFunc(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(`{"task":{"status":"SUCCESS"}}`)),
}
}),
}
netClient = httpClient
report := &SonarReport{CeTaskURL: "someURL"}
taskResponse := getSonarJobStatus(report)
if taskResponse.Task.Status != "SUCCESS" {
t.Errorf("Expected SUCCESS, got %v", taskResponse.Task.Status)
}
}
// Test for Wait for Sonar Job function
// func TestWaitForSonarJob(t *testing.T) {
// // Define your test logic here
// }