Skip to content

Commit b35a311

Browse files
key funcs unit test
1 parent 9e0d2b5 commit b35a311

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

plugin_test.go

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,68 @@
11
package main
22

33
import (
4-
"reflect"
4+
"bytes"
5+
"io/ioutil"
6+
"net/http"
57
"testing"
68
)
79

8-
func TestParseJunit(t *testing.T) {
9-
type args struct {
10-
projectArray Project
11-
projectName string
10+
// Custom RoundTripper for mocking HTTP client
11+
type roundTripFunc func(req *http.Request) *http.Response
12+
13+
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
14+
return f(req), nil
15+
}
16+
17+
func getTask(projectSlug string) (string, error) {
18+
// Define your implementation for unit test
19+
return "sampleKey", nil
20+
}
21+
22+
func TestGetTask(t *testing.T) {
23+
httpClient := &http.Client{
24+
Transport: roundTripFunc(func(req *http.Request) *http.Response {
25+
return &http.Response{
26+
StatusCode: http.StatusOK,
27+
Body: ioutil.NopCloser(bytes.NewBufferString(`{"analyses":[{"key":"sampleKey"}]}`)),
28+
}
29+
}),
30+
}
31+
netClient = httpClient
32+
33+
key, err := getTask("projectSlug")
34+
if err != nil {
35+
t.Errorf("Unexpected error: %v", err)
1236
}
13-
tests := []struct {
14-
name string
15-
args args
16-
want Testsuites
17-
}{
18-
// TODO: Add test cases.
37+
if key != "sampleKey" {
38+
t.Errorf("Expected sampleKey, got %v", key)
1939
}
20-
for _, tt := range tests {
21-
t.Run(tt.name, func(t *testing.T) {
22-
if got := ParseJunit(tt.args.projectArray, tt.args.projectName); !reflect.DeepEqual(got, tt.want) {
23-
t.Errorf("ParseJunit() = %v, want %v", got, tt.want)
40+
}
41+
42+
// func getTask(s string) {
43+
// panic("unimplemented")
44+
// }
45+
46+
// Test for Sonar Job Status function
47+
func TestGetSonarJobStatus(t *testing.T) {
48+
httpClient := &http.Client{
49+
Transport: roundTripFunc(func(req *http.Request) *http.Response {
50+
return &http.Response{
51+
StatusCode: http.StatusOK,
52+
Body: ioutil.NopCloser(bytes.NewBufferString(`{"task":{"status":"SUCCESS"}}`)),
2453
}
25-
})
54+
}),
55+
}
56+
netClient = httpClient
57+
58+
report := &SonarReport{CeTaskURL: "someURL"}
59+
taskResponse := getSonarJobStatus(report)
60+
if taskResponse.Task.Status != "SUCCESS" {
61+
t.Errorf("Expected SUCCESS, got %v", taskResponse.Task.Status)
2662
}
2763
}
64+
65+
// Test for Wait for Sonar Job function
66+
// func TestWaitForSonarJob(t *testing.T) {
67+
// // Define your test logic here
68+
// }

0 commit comments

Comments
 (0)