1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/sonar/client_test.go
Christopher Fenner cb3fa7c293
feat(sonar): add output of measurements (#2218)
* add sonarqube measurements

* fetch measurements from API

* add api for fetching issue counts

* add debug outputs

* add further severities

* log number of issues

* report failure

* expose method to send request

* Fixed what was broken.

* add debug output

* wip

* correct opaque property

* push client handling to apiClient.go

* use correct API URL

* correct log outputs

* remove logging

* remove option validation

* extend search options

* restructure

* rename api client file

* simplify client usage

* simplify issue client

* write sonar values to influx

* extract issue service

* reorder imports

* add sonar integration test

* allow unknown fields

* add test case

* add test case

* remove

* fix

* Update http.go

* Apply suggestions from code review

* Update cmd/sonarExecuteScan.go

* rework test cases

* use explicit returns

* add task service

* add waitfortask

* fix typo

* remove fixme

* expose poll interval

* rename test cases

* add test cases

* use newAPIClient method

* use waitForTask

* rename services

* finalize code

* handle error

* move defer

* move types

* add test case

* use http.status...

* add test case

* expose api endpoint names

* extract api client

* adjust test cases

* Update integration-tests-pr.yaml

* Update integration-tests.yaml

* improve require message

* Update integration-tests-pr.yaml

* Update integration-tests-pr.yaml
2021-02-24 15:44:23 +01:00

50 lines
1.4 KiB
Go

package sonar
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestCreate(t *testing.T) {
testURL := "https://example.org/api/"
t.Run("", func(t *testing.T) {
// init
requester := Requester{
Host: testURL,
Username: mock.Anything,
}
// test
request, err := requester.create(http.MethodGet, mock.Anything, &IssuesSearchOption{P: "42"})
// assert
assert.NoError(t, err)
assert.Empty(t, request.URL.Opaque)
assert.Equal(t, http.MethodGet, request.Method)
assert.Equal(t, "https", request.URL.Scheme)
assert.Equal(t, "example.org", request.URL.Host)
assert.Equal(t, "/api/"+mock.Anything, request.URL.Path)
assert.Contains(t, request.Header.Get("Authorization"), "Basic ")
})
}
func TestNewAPIClient(t *testing.T) {
tests := []struct {
name string
host string
want string
}{
{name: mock.Anything, want: "https://example.org/api/", host: "https://example.org"},
{name: mock.Anything, want: "https://example.org/api/", host: "https://example.org/"},
{name: mock.Anything, want: "https://example.org/api/", host: "https://example.org/api"},
{name: mock.Anything, want: "https://example.org/api/", host: "https://example.org/api/"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewAPIClient(tt.host, mock.Anything, nil)
assert.Equal(t, tt.want, got.Host)
})
}
}