|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/jfrog/jfrog-cli-core/v2/utils/config" |
| 6 | + "go.uber.org/mock/gomock" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/jfrog/jfrog-cli-application/application/http/mocks" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSystemService_Ping(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + mockResponse *http.Response |
| 18 | + mockBody []byte |
| 19 | + mockError error |
| 20 | + expectedError error |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "Ping successful", |
| 24 | + mockResponse: &http.Response{ |
| 25 | + StatusCode: 200, |
| 26 | + }, |
| 27 | + mockBody: []byte("pong"), |
| 28 | + mockError: nil, |
| 29 | + expectedError: nil, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Ping failed with non-200 status code", |
| 33 | + mockResponse: &http.Response{ |
| 34 | + StatusCode: 500, |
| 35 | + }, |
| 36 | + mockBody: []byte(""), |
| 37 | + mockError: nil, |
| 38 | + expectedError: errors.New("failed to create app version. Status code: 500"), |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "Ping failed with error", |
| 42 | + mockResponse: nil, |
| 43 | + mockBody: nil, |
| 44 | + mockError: errors.New("http error"), |
| 45 | + expectedError: errors.New("http error"), |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + for _, tt := range tests { |
| 50 | + t.Run(tt.name, func(t *testing.T) { |
| 51 | + ctrl := gomock.NewController(t) |
| 52 | + defer ctrl.Finish() |
| 53 | + |
| 54 | + mockHttpClient := mock_http.NewMockAppHttpClient(ctrl) |
| 55 | + mockHttpClient.EXPECT().Get("/v1/system/ping"). |
| 56 | + Return(tt.mockResponse, tt.mockBody, tt.mockError) |
| 57 | + |
| 58 | + ctx := &Context{ |
| 59 | + ServerDetails: &config.ServerDetails{}, |
| 60 | + } |
| 61 | + |
| 62 | + ss := NewSystemService() |
| 63 | + err := ss.Ping(ctx) |
| 64 | + |
| 65 | + if tt.expectedError != nil { |
| 66 | + assert.EqualError(t, err, tt.expectedError.Error()) |
| 67 | + } else { |
| 68 | + assert.NoError(t, err) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
0 commit comments