Skip to content

Commit c2434c7

Browse files
committed
Added tests
1 parent 684d9a9 commit c2434c7

File tree

14 files changed

+207
-49
lines changed

14 files changed

+207
-49
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ build-install:: build
6363
.PHONY: clean-mock
6464
clean-mock:
6565
@echo Cleaning generated mock files
66-
find . -path "*/mocks/*.go" -delete
66+
@find . -name "*_mock.go" -delete
6767

6868
.PHONY: generate-mock
6969
generate-mock: clean-mock

application/app/auth/app_details.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

application/app/context_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package app
2+
3+
import (
4+
mockservice "github.com/jfrog/jfrog-cli-application/application/service/mocks"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewAppContext(t *testing.T) {
11+
ctx := NewAppContext()
12+
assert.NotNil(t, ctx)
13+
assert.NotNil(t, ctx.GetVersionService())
14+
assert.NotNil(t, ctx.GetSystemService())
15+
}
16+
17+
func TestGetVersionService(t *testing.T) {
18+
mockVersionService := &mockservice.MockVersionService{}
19+
ctx := &context{
20+
versionService: mockVersionService,
21+
}
22+
assert.Equal(t, mockVersionService, ctx.GetVersionService())
23+
}
24+
25+
func TestGetSystemService(t *testing.T) {
26+
mockSystemService := &mockservice.MockSystemService{}
27+
ctx := &context{
28+
systemService: mockSystemService,
29+
}
30+
assert.Equal(t, mockSystemService, ctx.GetSystemService())
31+
}
32+
33+
func TestGetConfig(t *testing.T) {
34+
ctx := &context{}
35+
assert.Nil(t, ctx.GetConfig())
36+
}

application/commands/system/ping_cmd.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ type pingCommand struct {
1717
}
1818

1919
func (pc *pingCommand) Run() error {
20-
ctx := &service.Context{ServerDetails: pc.serverDetails}
20+
ctx, err := service.NewContext(*pc.serverDetails)
21+
if err != nil {
22+
return err
23+
}
24+
2125
return pc.systemService.Ping(ctx)
2226
}
2327

application/commands/version/create_app_version_cmd.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ type createVersionSpec struct {
2929
}
3030

3131
func (cv *createAppVersionCommand) Run() error {
32-
ctx := &service.Context{ServerDetails: cv.serverDetails}
32+
ctx, err := service.NewContext(*cv.serverDetails)
33+
if err != nil {
34+
return err
35+
}
36+
3337
return cv.versionService.CreateAppVersion(ctx, cv.requestPayload)
3438
}
3539

application/commands/version/promote_app_version_cmd.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ type promoteAppVersionCommand struct {
2121
}
2222

2323
func (pv *promoteAppVersionCommand) Run() error {
24-
ctx := &service.Context{ServerDetails: pv.serverDetails}
24+
ctx, err := service.NewContext(*pv.serverDetails)
25+
if err != nil {
26+
return err
27+
}
28+
2529
return pv.versionService.PromoteAppVersion(ctx, pv.requestPayload)
2630
}
2731

application/http/http_client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package http
22

3+
//go:generate ${PROJECT_DIR}/scripts/mockgen.sh ${GOFILE}
4+
35
import (
46
"encoding/json"
57
"fmt"

application/service/context.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
package service
22

3-
import coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
3+
import (
4+
"github.com/jfrog/jfrog-cli-application/application/http"
5+
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
6+
)
47

5-
type Context struct {
6-
ServerDetails *coreConfig.ServerDetails
8+
type Context interface {
9+
GetServerDetails() coreConfig.ServerDetails
10+
GetHttpClient() http.AppHttpClient
11+
}
12+
13+
type context struct {
14+
ServerDetails coreConfig.ServerDetails
15+
HttpClient http.AppHttpClient
16+
}
17+
18+
func (c *context) GetServerDetails() coreConfig.ServerDetails {
19+
return c.ServerDetails
20+
}
21+
22+
func (c *context) GetHttpClient() http.AppHttpClient {
23+
return c.HttpClient
24+
}
25+
26+
func NewContext(serverDetails coreConfig.ServerDetails) (Context, error) {
27+
httpClient, err := http.NewAppHttpClient(&serverDetails)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
return &context{ServerDetails: serverDetails, HttpClient: httpClient}, nil
733
}

application/service/system_service.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package service
22

3+
//go:generate ${PROJECT_DIR}/scripts/mockgen.sh ${GOFILE}
4+
35
import (
46
"fmt"
5-
6-
"github.com/jfrog/jfrog-cli-application/application/http"
77
)
88

99
type SystemService interface {
10-
Ping(ctx *Context) error
10+
Ping(ctx Context) error
1111
}
1212

1313
type systemService struct{}
@@ -16,13 +16,8 @@ func NewSystemService() SystemService {
1616
return &systemService{}
1717
}
1818

19-
func (ss *systemService) Ping(ctx *Context) error {
20-
httpClient, err := http.NewAppHttpClient(ctx.ServerDetails)
21-
if err != nil {
22-
return err
23-
}
24-
25-
response, body, err := httpClient.Get("/v1/system/ping")
19+
func (ss *systemService) Ping(ctx Context) error {
20+
response, body, err := ctx.GetHttpClient().Get("/v1/system/ping")
2621
if err != nil {
2722
return err
2823
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)