|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "reflect" |
| 4 | + "bytes" |
| 5 | + "io/ioutil" |
| 6 | + "net/http" |
5 | 7 | "testing" |
6 | 8 | ) |
7 | 9 |
|
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) |
12 | 36 | } |
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) |
19 | 39 | } |
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"}}`)), |
24 | 53 | } |
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) |
26 | 62 | } |
27 | 63 | } |
| 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