From 98cbd2e40a1e66f6926011fa495a39f107eeecef Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 1 Jul 2023 13:50:38 -0600 Subject: [PATCH 01/70] :art: Changed the client interface from Client to Connector on the Jira Banner service. --- jira/internal/announcement_banner_impl.go | 13 ++--- .../internal/announcement_banner_impl_test.go | 53 ++++++++----------- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/jira/internal/announcement_banner_impl.go b/jira/internal/announcement_banner_impl.go index cc3afb6f..81b9904f 100644 --- a/jira/internal/announcement_banner_impl.go +++ b/jira/internal/announcement_banner_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewAnnouncementBannerService(client service.Client, version string) *AnnouncementBannerService { +func NewAnnouncementBannerService(client service.Connector, version string) *AnnouncementBannerService { return &AnnouncementBannerService{ internalClient: &internalAnnouncementBannerImpl{c: client, version: version}, @@ -39,7 +39,7 @@ func (a *AnnouncementBannerService) Update(ctx context.Context, payload *model.A } type internalAnnouncementBannerImpl struct { - c service.Client + c service.Connector version string } @@ -47,7 +47,7 @@ func (i *internalAnnouncementBannerImpl) Get(ctx context.Context) (*model.Announ endpoint := fmt.Sprintf("rest/api/%v/announcementBanner", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -63,14 +63,9 @@ func (i *internalAnnouncementBannerImpl) Get(ctx context.Context) (*model.Announ func (i *internalAnnouncementBannerImpl) Update(ctx context.Context, payload *model.AnnouncementBannerPayloadScheme) (*model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/announcementBanner", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } diff --git a/jira/internal/announcement_banner_impl_test.go b/jira/internal/announcement_banner_impl_test.go index 066e45d5..61da4d66 100644 --- a/jira/internal/announcement_banner_impl_test.go +++ b/jira/internal/announcement_banner_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalAnnouncementBannerImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -38,12 +37,13 @@ func Test_internalAnnouncementBannerImpl_Get(t *testing.T) { }, fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/announcementBanner", + "", nil). Return(&http.Request{}, nil) @@ -63,12 +63,13 @@ func Test_internalAnnouncementBannerImpl_Get(t *testing.T) { }, fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/announcementBanner", + "", nil). Return(&http.Request{}, nil) @@ -88,12 +89,13 @@ func Test_internalAnnouncementBannerImpl_Get(t *testing.T) { ctx: context.Background(), }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/announcementBanner", + "", nil). Return(&http.Request{}, errors.New("unable to create the http request")) @@ -110,12 +112,13 @@ func Test_internalAnnouncementBannerImpl_Get(t *testing.T) { ctx: context.Background(), }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/announcementBanner", + "", nil). Return(&http.Request{}, nil) @@ -170,7 +173,7 @@ func Test_internalAnnouncementBannerImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -195,17 +198,14 @@ func Test_internalAnnouncementBannerImpl_Update(t *testing.T) { }, fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/announcementBanner", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -225,17 +225,14 @@ func Test_internalAnnouncementBannerImpl_Update(t *testing.T) { }, fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/announcementBanner", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -255,17 +252,14 @@ func Test_internalAnnouncementBannerImpl_Update(t *testing.T) { payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/announcementBanner", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("unable to create the http request")) fields.c = client @@ -282,17 +276,14 @@ func Test_internalAnnouncementBannerImpl_Update(t *testing.T) { payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/announcementBanner", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", From 265555dcb7643f53322a47450126530e2646ab07 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 1 Jul 2023 13:58:12 -0600 Subject: [PATCH 02/70] :art: Changed the client interface from Client to Connector on the Jira ApplicatonRole service. --- jira/internal/application_role_impl.go | 8 ++--- jira/internal/application_role_impl_test.go | 35 +++++++++++++-------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/jira/internal/application_role_impl.go b/jira/internal/application_role_impl.go index ad26955a..497c8862 100644 --- a/jira/internal/application_role_impl.go +++ b/jira/internal/application_role_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewApplicationRoleService(client service.Client, version string) (*ApplicationRoleService, error) { +func NewApplicationRoleService(client service.Connector, version string) (*ApplicationRoleService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -45,7 +45,7 @@ func (a *ApplicationRoleService) Get(ctx context.Context, key string) (*model.Ap } type internalApplicationRoleImpl struct { - c service.Client + c service.Connector version string } @@ -53,7 +53,7 @@ func (i *internalApplicationRoleImpl) Gets(ctx context.Context) ([]*model.Applic endpoint := fmt.Sprintf("rest/api/%v/applicationrole", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -75,7 +75,7 @@ func (i *internalApplicationRoleImpl) Get(ctx context.Context, key string) (*mod endpoint := fmt.Sprintf("rest/api/%v/applicationrole/%v", i.version, key) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/application_role_impl_test.go b/jira/internal/application_role_impl_test.go index 709e97c1..8d32971f 100644 --- a/jira/internal/application_role_impl_test.go +++ b/jira/internal/application_role_impl_test.go @@ -15,7 +15,7 @@ import ( func TestApplicationRoleService_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,12 +40,13 @@ func TestApplicationRoleService_Get(t *testing.T) { }, fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/applicationrole/jira-users", + "", nil). Return(&http.Request{}, nil) @@ -66,12 +67,13 @@ func TestApplicationRoleService_Get(t *testing.T) { }, fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/applicationrole/jira-users", + "", nil). Return(&http.Request{}, nil) @@ -92,7 +94,7 @@ func TestApplicationRoleService_Get(t *testing.T) { }, fields: fields{version: "2"}, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoApplicationRoleError, @@ -106,12 +108,13 @@ func TestApplicationRoleService_Get(t *testing.T) { key: "jira-users", }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/applicationrole/jira-users", + "", nil). Return(&http.Request{}, errors.New("unable to create the http request")) @@ -129,12 +132,13 @@ func TestApplicationRoleService_Get(t *testing.T) { key: "jira-users", }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/applicationrole/jira-users", + "", nil). Return(&http.Request{}, nil) @@ -184,7 +188,7 @@ func TestApplicationRoleService_Get(t *testing.T) { func TestApplicationRoleService_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -205,12 +209,13 @@ func TestApplicationRoleService_Gets(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/applicationrole", + "", nil). Return(&http.Request{}, nil) @@ -232,12 +237,13 @@ func TestApplicationRoleService_Gets(t *testing.T) { fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/applicationrole", + "", nil). Return(&http.Request{}, nil) @@ -259,12 +265,13 @@ func TestApplicationRoleService_Gets(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/applicationrole", + "", nil). Return(&http.Request{}, errors.New("unable to create the http request")) @@ -282,12 +289,13 @@ func TestApplicationRoleService_Gets(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/applicationrole", + "", nil). Return(&http.Request{}, nil) @@ -310,12 +318,13 @@ func TestApplicationRoleService_Gets(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/applicationrole", + "", nil). Return(&http.Request{}, nil) @@ -366,7 +375,7 @@ func TestApplicationRoleService_Gets(t *testing.T) { func TestNewApplicationRoleService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 68f1b726f21eeebbc81802d0d08f05bffdcb2bf9 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 1 Jul 2023 14:05:31 -0600 Subject: [PATCH 03/70] :art: Changed the client interface from Client to Connector on the Jira Attachment service. --- jira/internal/attachment_impl.go | 16 +++--- jira/internal/attachment_impl_test.go | 79 ++++++++++++++++----------- 2 files changed, 55 insertions(+), 40 deletions(-) diff --git a/jira/internal/attachment_impl.go b/jira/internal/attachment_impl.go index 92cac4fe..bbe3acd1 100644 --- a/jira/internal/attachment_impl.go +++ b/jira/internal/attachment_impl.go @@ -14,7 +14,7 @@ import ( "strings" ) -func NewIssueAttachmentService(client service.Client, version string) (*IssueAttachmentService, error) { +func NewIssueAttachmentService(client service.Connector, version string) (*IssueAttachmentService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -90,7 +90,7 @@ func (i *IssueAttachmentService) Download(ctx context.Context, attachmentID stri } type internalIssueAttachmentServiceImpl struct { - c service.Client + c service.Connector version string } @@ -111,7 +111,7 @@ func (i *internalIssueAttachmentServiceImpl) Download(ctx context.Context, attac endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, err } @@ -123,7 +123,7 @@ func (i *internalIssueAttachmentServiceImpl) Settings(ctx context.Context) (*mod endpoint := fmt.Sprintf("rest/api/%v/attachment/meta", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -145,7 +145,7 @@ func (i *internalIssueAttachmentServiceImpl) Metadata(ctx context.Context, attac endpoint := fmt.Sprintf("rest/api/%v/attachment/%v", i.version, attachmentId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -167,7 +167,7 @@ func (i *internalIssueAttachmentServiceImpl) Delete(ctx context.Context, attachm endpoint := fmt.Sprintf("rest/api/%v/attachment/%v", i.version, attachmentId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -183,7 +183,7 @@ func (i *internalIssueAttachmentServiceImpl) Human(ctx context.Context, attachme endpoint := fmt.Sprintf("rest/api/%v/attachment/%v/expand/human", i.version, attachmentId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -228,7 +228,7 @@ func (i *internalIssueAttachmentServiceImpl) Add(ctx context.Context, issueKeyOr writer.Close() - request, err := i.c.NewFormRequest(ctx, http.MethodPost, endpoint, writer.FormDataContentType(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, writer.FormDataContentType(), reader) if err != nil { return nil, nil, err } diff --git a/jira/internal/attachment_impl_test.go b/jira/internal/attachment_impl_test.go index 4a32279c..4f7784cc 100644 --- a/jira/internal/attachment_impl_test.go +++ b/jira/internal/attachment_impl_test.go @@ -18,7 +18,7 @@ import ( func Test_internalIssueAttachmentServiceImpl_Settings(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -42,12 +42,13 @@ func Test_internalIssueAttachmentServiceImpl_Settings(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/attachment/meta", + "", nil). Return(&http.Request{}, nil) @@ -69,12 +70,13 @@ func Test_internalIssueAttachmentServiceImpl_Settings(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/attachment/meta", + "", nil). Return(&http.Request{}, nil) @@ -96,12 +98,13 @@ func Test_internalIssueAttachmentServiceImpl_Settings(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/attachment/meta", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -147,7 +150,7 @@ func Test_internalIssueAttachmentServiceImpl_Settings(t *testing.T) { func Test_internalIssueAttachmentServiceImpl_Metadata(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -173,12 +176,13 @@ func Test_internalIssueAttachmentServiceImpl_Metadata(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/attachment/1110", + "", nil). Return(&http.Request{}, nil) @@ -200,12 +204,13 @@ func Test_internalIssueAttachmentServiceImpl_Metadata(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/attachment/1110", + "", nil). Return(&http.Request{}, nil) @@ -227,7 +232,7 @@ func Test_internalIssueAttachmentServiceImpl_Metadata(t *testing.T) { attachmentId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoAttachmentIDError, @@ -242,12 +247,13 @@ func Test_internalIssueAttachmentServiceImpl_Metadata(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/attachment/1110", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -293,7 +299,7 @@ func Test_internalIssueAttachmentServiceImpl_Metadata(t *testing.T) { func Test_internalIssueAttachmentServiceImpl_Human(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -319,12 +325,13 @@ func Test_internalIssueAttachmentServiceImpl_Human(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/attachment/1110/expand/human", + "", nil). Return(&http.Request{}, nil) @@ -346,12 +353,13 @@ func Test_internalIssueAttachmentServiceImpl_Human(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/attachment/1110/expand/human", + "", nil). Return(&http.Request{}, nil) @@ -373,7 +381,7 @@ func Test_internalIssueAttachmentServiceImpl_Human(t *testing.T) { attachmentId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoAttachmentIDError, @@ -388,12 +396,13 @@ func Test_internalIssueAttachmentServiceImpl_Human(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/attachment/1110/expand/human", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -439,7 +448,7 @@ func Test_internalIssueAttachmentServiceImpl_Human(t *testing.T) { func Test_internalIssueAttachmentServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -465,12 +474,13 @@ func Test_internalIssueAttachmentServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/attachment/1110", + "", nil). Return(&http.Request{}, nil) @@ -492,12 +502,13 @@ func Test_internalIssueAttachmentServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/attachment/1110", + "", nil). Return(&http.Request{}, nil) @@ -519,7 +530,7 @@ func Test_internalIssueAttachmentServiceImpl_Delete(t *testing.T) { attachmentId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoAttachmentIDError, @@ -534,12 +545,13 @@ func Test_internalIssueAttachmentServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/attachment/1110", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -594,7 +606,7 @@ func Test_internalIssueAttachmentServiceImpl_Add(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -623,9 +635,9 @@ func Test_internalIssueAttachmentServiceImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) - client.On("NewFormRequest", + client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/attachments", @@ -653,9 +665,9 @@ func Test_internalIssueAttachmentServiceImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) - client.On("NewFormRequest", + client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/attachments", @@ -722,9 +734,9 @@ func Test_internalIssueAttachmentServiceImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) - client.On("NewFormRequest", + client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/attachments", @@ -774,7 +786,7 @@ func Test_internalIssueAttachmentServiceImpl_Add(t *testing.T) { func Test_internalIssueAttachmentServiceImpl_Download(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -802,12 +814,13 @@ func Test_internalIssueAttachmentServiceImpl_Download(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/attachment/content/1110?redirect=false", + "", nil). Return(&http.Request{}, nil) @@ -830,12 +843,13 @@ func Test_internalIssueAttachmentServiceImpl_Download(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/attachment/content/1110", + "", nil). Return(&http.Request{}, nil) @@ -857,7 +871,7 @@ func Test_internalIssueAttachmentServiceImpl_Download(t *testing.T) { attachmentId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoAttachmentIDError, @@ -873,12 +887,13 @@ func Test_internalIssueAttachmentServiceImpl_Download(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/attachment/content/1110", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -923,7 +938,7 @@ func Test_internalIssueAttachmentServiceImpl_Download(t *testing.T) { func TestNewIssueAttachmentService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 7bf602746d7bbc0f09fdef71e72bf6f881cce99a Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 1 Jul 2023 14:19:01 -0600 Subject: [PATCH 04/70] :art: Migrated the client interface from Client to Connector on the Jira Audit service. --- jira/internal/audit_impl.go | 6 +++--- jira/internal/audit_impl_test.go | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/jira/internal/audit_impl.go b/jira/internal/audit_impl.go index 63aa9c5d..2ad065a1 100644 --- a/jira/internal/audit_impl.go +++ b/jira/internal/audit_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewAuditRecordService(client service.Client, version string) (*AuditRecordService, error) { +func NewAuditRecordService(client service.Connector, version string) (*AuditRecordService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -37,7 +37,7 @@ func (a *AuditRecordService) Get(ctx context.Context, options *model.AuditRecord } type internalAuditRecordImpl struct { - c service.Client + c service.Connector version string } @@ -70,7 +70,7 @@ func (i *internalAuditRecordImpl) Get(ctx context.Context, options *model.AuditR endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/audit_impl_test.go b/jira/internal/audit_impl_test.go index 8f872a16..aac678e2 100644 --- a/jira/internal/audit_impl_test.go +++ b/jira/internal/audit_impl_test.go @@ -15,7 +15,7 @@ import ( func Test_internalAuditRecordImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -48,12 +48,13 @@ func Test_internalAuditRecordImpl_Get(t *testing.T) { }, fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/auditing/record?=summary&from=2015-11-17&limit=1000&offset=2000&to=2019-11-17", + "", nil). Return(&http.Request{}, nil) @@ -80,12 +81,13 @@ func Test_internalAuditRecordImpl_Get(t *testing.T) { }, fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/auditing/record?=summary&from=2015-11-17&limit=1000&offset=2000&to=2019-11-17", + "", nil). Return(&http.Request{}, nil) @@ -112,12 +114,13 @@ func Test_internalAuditRecordImpl_Get(t *testing.T) { limit: 1000, }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/auditing/record?=summary&from=2015-11-17&limit=1000&offset=2000&to=2019-11-17", + "", nil). Return(&http.Request{}, errors.New("unable to create the http request")) @@ -141,12 +144,13 @@ func Test_internalAuditRecordImpl_Get(t *testing.T) { limit: 1000, }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/auditing/record?=summary&from=2015-11-17&limit=1000&offset=2000&to=2019-11-17", + "", nil). Return(&http.Request{}, nil) @@ -169,10 +173,10 @@ func Test_internalAuditRecordImpl_Get(t *testing.T) { testCase.on(&testCase.fields) } - service, err := NewAuditRecordService(testCase.fields.c, testCase.fields.version) + newAuditRecordService, err := NewAuditRecordService(testCase.fields.c, testCase.fields.version) assert.NoError(t, err) - gotResult, gotResponse, err := service.Get(testCase.args.ctx, testCase.args.options, testCase.args.offSet, + gotResult, gotResponse, err := newAuditRecordService.Get(testCase.args.ctx, testCase.args.options, testCase.args.offSet, testCase.args.limit) if testCase.wantErr { @@ -197,7 +201,7 @@ func Test_internalAuditRecordImpl_Get(t *testing.T) { func TestNewAuditRecordService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 8cfb075c88fee342b780d8b6cacc38f64fbeb677 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 1 Jul 2023 14:45:50 -0600 Subject: [PATCH 05/70] :art: Migrated the client interface from Client to Connector on the Jira Dashboard service. --- jira/internal/dashboard_impl.go | 40 +--- jira/internal/dashboard_impl_test.go | 275 ++++++++++++--------------- 2 files changed, 130 insertions(+), 185 deletions(-) diff --git a/jira/internal/dashboard_impl.go b/jira/internal/dashboard_impl.go index f1335bf8..614e29b8 100644 --- a/jira/internal/dashboard_impl.go +++ b/jira/internal/dashboard_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewDashboardService(client service.Client, version string) (*DashboardService, error) { +func NewDashboardService(client service.Connector, version string) (*DashboardService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -97,7 +97,7 @@ func (d *DashboardService) Update(ctx context.Context, dashboardId string, paylo } type internalDashboardImpl struct { - c service.Client + c service.Connector version string } @@ -113,7 +113,7 @@ func (i *internalDashboardImpl) Gets(ctx context.Context, startAt, maxResults in endpoint := fmt.Sprintf("rest/api/%v/dashboard?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -129,14 +129,9 @@ func (i *internalDashboardImpl) Gets(ctx context.Context, startAt, maxResults in func (i *internalDashboardImpl) Create(ctx context.Context, payload *model.DashboardPayloadScheme) (*model.DashboardScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/dashboard", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -181,7 +176,7 @@ func (i *internalDashboardImpl) Search(ctx context.Context, options *model.Dashb endpoint := fmt.Sprintf("rest/api/%v/dashboard/search?%s", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -203,7 +198,7 @@ func (i *internalDashboardImpl) Get(ctx context.Context, dashboardId string) (*m endpoint := fmt.Sprintf("rest/api/%v/dashboard/%v", i.version, dashboardId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -225,17 +220,12 @@ func (i *internalDashboardImpl) Delete(ctx context.Context, dashboardId string) endpoint := fmt.Sprintf("rest/api/%v/dashboard/%v", i.version, dashboardId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) - if err != nil { - return nil, err - } - - response, err := i.c.Call(request, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } - return response, nil + return i.c.Call(request, nil) } func (i *internalDashboardImpl) Copy(ctx context.Context, dashboardId string, payload *model.DashboardPayloadScheme) (*model.DashboardScheme, *model.ResponseScheme, error) { @@ -244,14 +234,9 @@ func (i *internalDashboardImpl) Copy(ctx context.Context, dashboardId string, pa return nil, nil, model.ErrNoDashboardIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/dashboard/%v/copy", i.version, dashboardId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -271,14 +256,9 @@ func (i *internalDashboardImpl) Update(ctx context.Context, dashboardId string, return nil, nil, model.ErrNoDashboardIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/dashboard/%v", i.version, dashboardId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/dashboard_impl_test.go b/jira/internal/dashboard_impl_test.go index 71d930a1..25908895 100644 --- a/jira/internal/dashboard_impl_test.go +++ b/jira/internal/dashboard_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -14,8 +13,23 @@ import ( func TestDashboardService_Copy(t *testing.T) { + payloadMocked := &model.DashboardPayloadScheme{ + Name: "Auditors dashboard", + Description: "A dashboard to help auditors identify sample of issues to check.", + SharePermissions: []*model.SharePermissionScheme{ + { + Type: "global", + }, + }, + EditPermissions: []*model.SharePermissionScheme{ + { + Type: "global", + }, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -39,21 +53,18 @@ func TestDashboardService_Copy(t *testing.T) { args: args{ ctx: context.Background(), dashboardId: "10001", - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/dashboard/10001/copy", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -72,21 +83,18 @@ func TestDashboardService_Copy(t *testing.T) { args: args{ ctx: context.Background(), dashboardId: "10001", - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/dashboard/10001/copy", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -105,58 +113,33 @@ func TestDashboardService_Copy(t *testing.T) { args: args{ ctx: context.Background(), dashboardId: "", - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoDashboardIDError, }, - { - name: "when the payload is not provided", - fields: fields{version: "2"}, - args: args{ - ctx: context.Background(), - dashboardId: "10001", - payload: &model.DashboardPayloadScheme{}, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), model.ErrNilPayloadError) - fields.c = client - - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "2"}, args: args{ ctx: context.Background(), dashboardId: "10001", - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/dashboard/10001/copy", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -199,8 +182,23 @@ func TestDashboardService_Copy(t *testing.T) { func TestDashboardService_Update(t *testing.T) { + payloadMocked := &model.DashboardPayloadScheme{ + Name: "Auditors dashboard", + Description: "A dashboard to help auditors identify sample of issues to check.", + SharePermissions: []*model.SharePermissionScheme{ + { + Type: "global", + }, + }, + EditPermissions: []*model.SharePermissionScheme{ + { + Type: "global", + }, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -224,21 +222,18 @@ func TestDashboardService_Update(t *testing.T) { args: args{ ctx: context.Background(), dashboardId: "10001", - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/dashboard/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -257,21 +252,18 @@ func TestDashboardService_Update(t *testing.T) { args: args{ ctx: context.Background(), dashboardId: "10001", - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/dashboard/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -293,55 +285,30 @@ func TestDashboardService_Update(t *testing.T) { payload: &model.DashboardPayloadScheme{}, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoDashboardIDError, }, - { - name: "when the payload is not provided", - fields: fields{version: "2"}, - args: args{ - ctx: context.Background(), - dashboardId: "10001", - payload: &model.DashboardPayloadScheme{}, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), model.ErrNilPayloadError) - fields.c = client - - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "2"}, args: args{ ctx: context.Background(), dashboardId: "10001", - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/dashboard/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -385,7 +352,7 @@ func TestDashboardService_Update(t *testing.T) { func TestDashboardService_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -414,12 +381,13 @@ func TestDashboardService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/dashboard?filter=favourite&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -443,12 +411,13 @@ func TestDashboardService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/dashboard?filter=favourite&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -472,12 +441,13 @@ func TestDashboardService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/dashboard?filter=favourite&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -521,8 +491,23 @@ func TestDashboardService_Gets(t *testing.T) { func TestDashboardService_Create(t *testing.T) { + payloadMocked := &model.DashboardPayloadScheme{ + Name: "Auditors dashboard", + Description: "A dashboard to help auditors identify sample of issues to check.", + SharePermissions: []*model.SharePermissionScheme{ + { + Type: "global", + }, + }, + EditPermissions: []*model.SharePermissionScheme{ + { + Type: "global", + }, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -544,21 +529,18 @@ func TestDashboardService_Create(t *testing.T) { fields: fields{version: "2"}, args: args{ ctx: context.Background(), - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/dashboard", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -576,21 +558,18 @@ func TestDashboardService_Create(t *testing.T) { fields: fields{version: "3"}, args: args{ ctx: context.Background(), - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/dashboard", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -603,47 +582,23 @@ func TestDashboardService_Create(t *testing.T) { }, }, - { - name: "when the payload is not provided", - fields: fields{version: "2"}, - args: args{ - ctx: context.Background(), - payload: &model.DashboardPayloadScheme{}, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), model.ErrNilPayloadError) - fields.c = client - - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "2"}, args: args{ ctx: context.Background(), - payload: &model.DashboardPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.DashboardPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/dashboard", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -687,7 +642,7 @@ func TestDashboardService_Create(t *testing.T) { func TestDashboardService_Search(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -722,12 +677,13 @@ func TestDashboardService_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/dashboard/search?accountId=owner-id&dashboardName=owner-id&expand=isWritable&groupname=owner-id&maxResults=0&orderBy=owner-id&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -758,12 +714,13 @@ func TestDashboardService_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/dashboard/search?accountId=owner-id&dashboardName=owner-id&expand=isWritable&groupname=owner-id&maxResults=0&orderBy=owner-id&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -794,12 +751,13 @@ func TestDashboardService_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/dashboard/search?accountId=owner-id&dashboardName=owner-id&expand=isWritable&groupname=owner-id&maxResults=0&orderBy=owner-id&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -826,12 +784,13 @@ func TestDashboardService_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/dashboard/search?accountId=owner-id&dashboardName=owner-id&expand=isWritable&groupname=owner-id&maxResults=0&orderBy=owner-id&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -882,7 +841,7 @@ func TestDashboardService_Search(t *testing.T) { func TestDashboardService_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -908,12 +867,13 @@ func TestDashboardService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/dashboard/10001", + "", nil). Return(&http.Request{}, nil) @@ -936,12 +896,13 @@ func TestDashboardService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/dashboard/10001", + "", nil). Return(&http.Request{}, nil) @@ -963,7 +924,7 @@ func TestDashboardService_Get(t *testing.T) { dashboardId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoDashboardIDError, @@ -978,12 +939,13 @@ func TestDashboardService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/dashboard/10001", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -1028,7 +990,7 @@ func TestDashboardService_Get(t *testing.T) { func TestDashboardService_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -1054,12 +1016,13 @@ func TestDashboardService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/dashboard/10001", + "", nil). Return(&http.Request{}, nil) @@ -1082,12 +1045,13 @@ func TestDashboardService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/dashboard/10001", + "", nil). Return(&http.Request{}, nil) @@ -1109,7 +1073,7 @@ func TestDashboardService_Delete(t *testing.T) { dashboardId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoDashboardIDError, @@ -1124,12 +1088,13 @@ func TestDashboardService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/dashboard/10001", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -1173,7 +1138,7 @@ func TestDashboardService_Delete(t *testing.T) { func TestNewDashboardService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From c5cdb306b0c0b96cee0cc7159a8564bbaa0aaaf8 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 2 Jul 2023 02:06:29 -0600 Subject: [PATCH 06/70] :art: Migrated the client interface from Client to Connector on the Jira Filter service. --- jira/internal/filter_impl.go | 92 ++++++---- jira/internal/filter_impl_test.go | 271 +++++++++++++++--------------- 2 files changed, 194 insertions(+), 169 deletions(-) diff --git a/jira/internal/filter_impl.go b/jira/internal/filter_impl.go index 740984fb..60f8b729 100644 --- a/jira/internal/filter_impl.go +++ b/jira/internal/filter_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewFilterService(client service.Client, version string, share jira.FilterSharingConnector) (*FilterService, error) { +func NewFilterService(client service.Connector, version string, share jira.FilterSharingConnector) (*FilterService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -29,54 +29,92 @@ type FilterService struct { Share jira.FilterSharingConnector } +// Create creates a filter. The filter is shared according to the default share scope. +// +// The filter is not selected as a favorite. +// +// POST /rest/api/{2-3}/filter +// +// https://docs.go-atlassian.io/jira-software-cloud/filters#create-filter func (f *FilterService) Create(ctx context.Context, payload *model.FilterPayloadScheme) (*model.FilterScheme, *model.ResponseScheme, error) { return f.internalClient.Create(ctx, payload) } +// Favorite returns the visible favorite filters of the user. +// +// GET /rest/api/{2-3}/filter/favourite +// +// https://docs.go-atlassian.io/jira-software-cloud/filters#get-favorites func (f *FilterService) Favorite(ctx context.Context) ([]*model.FilterScheme, *model.ResponseScheme, error) { return f.internalClient.Favorite(ctx) } +// My returns the filters owned by the user. If includeFavourites is true, +// +// The user's visible favorite filters are also returned. +// GET /rest/api/{2-3}/filter/my +// +// https://docs.go-atlassian.io/jira-software-cloud/filters#get-my-filters func (f *FilterService) My(ctx context.Context, favorites bool, expand []string) ([]*model.FilterScheme, *model.ResponseScheme, error) { return f.internalClient.My(ctx, favorites, expand) } +// Search returns a paginated list of filters +// +// GET /rest/api/{2-3}/filter/search +// +// https://docs.go-atlassian.io/jira-software-cloud/filters#search-filters func (f *FilterService) Search(ctx context.Context, options *model.FilterSearchOptionScheme, startAt, maxResults int) (*model.FilterSearchPageScheme, *model.ResponseScheme, error) { return f.internalClient.Search(ctx, options, startAt, maxResults) } +// Get returns a filter. +// +// GET /rest/api/{2-3}/filter/{id} +// +// https://docs.go-atlassian.io/jira-software-cloud/filters#get-filter func (f *FilterService) Get(ctx context.Context, filterId int, expand []string) (*model.FilterScheme, *model.ResponseScheme, error) { return f.internalClient.Get(ctx, filterId, expand) } +// Update updates a filter. Use this operation to update a filter's name, description, JQL, or sharing. +// +// PUT /rest/api/{2-3}/filter/{id} +// +// https://docs.go-atlassian.io/jira-software-cloud/filters#update-filter func (f *FilterService) Update(ctx context.Context, filterId int, payload *model.FilterPayloadScheme) (*model.FilterScheme, *model.ResponseScheme, error) { return f.internalClient.Update(ctx, filterId, payload) } +// Delete a filter. +// +// DELETE /rest/api/{2-3}/filter/{id} +// +// https://docs.go-atlassian.io/jira-software-cloud/filters#delete-filter func (f *FilterService) Delete(ctx context.Context, filterId int) (*model.ResponseScheme, error) { return f.internalClient.Delete(ctx, filterId) } +// Change changes the owner of the filter. +// +// PUT /rest/api/{2-3}/filter/{id}/owner +// +// https://docs.go-atlassian.io/jira-software-cloud/filters#change-filter-owner func (f *FilterService) Change(ctx context.Context, filterId int, accountId string) (*model.ResponseScheme, error) { return f.internalClient.Change(ctx, filterId, accountId) } type internalFilterServiceImpl struct { - c service.Client + c service.Connector version string } func (i *internalFilterServiceImpl) Create(ctx context.Context, payload *model.FilterPayloadScheme) (*model.FilterScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/filter", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -91,9 +129,10 @@ func (i *internalFilterServiceImpl) Create(ctx context.Context, payload *model.F } func (i *internalFilterServiceImpl) Favorite(ctx context.Context) ([]*model.FilterScheme, *model.ResponseScheme, error) { + endpoint := fmt.Sprintf("rest/api/%v/filter/favourite", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -108,6 +147,7 @@ func (i *internalFilterServiceImpl) Favorite(ctx context.Context) ([]*model.Filt } func (i *internalFilterServiceImpl) My(ctx context.Context, favorites bool, expand []string) ([]*model.FilterScheme, *model.ResponseScheme, error) { + params := url.Values{} params.Add("includeFavourites", fmt.Sprintf("%v", favorites)) @@ -117,7 +157,7 @@ func (i *internalFilterServiceImpl) My(ctx context.Context, favorites bool, expa endpoint := fmt.Sprintf("rest/api/%v/filter/my?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -132,6 +172,7 @@ func (i *internalFilterServiceImpl) My(ctx context.Context, favorites bool, expa } func (i *internalFilterServiceImpl) Search(ctx context.Context, options *model.FilterSearchOptionScheme, startAt, maxResults int) (*model.FilterSearchPageScheme, *model.ResponseScheme, error) { + params := url.Values{} params.Add("startAt", strconv.Itoa(startAt)) params.Add("maxResults", strconv.Itoa(maxResults)) @@ -169,7 +210,7 @@ func (i *internalFilterServiceImpl) Search(ctx context.Context, options *model.F endpoint := fmt.Sprintf("rest/api/%v/filter/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -184,6 +225,7 @@ func (i *internalFilterServiceImpl) Search(ctx context.Context, options *model.F } func (i *internalFilterServiceImpl) Get(ctx context.Context, filterId int, expand []string) (*model.FilterScheme, *model.ResponseScheme, error) { + if filterId == 0 { return nil, nil, model.ErrNoFilterIDError } @@ -200,7 +242,7 @@ func (i *internalFilterServiceImpl) Get(ctx context.Context, filterId int, expan endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -217,17 +259,12 @@ func (i *internalFilterServiceImpl) Get(ctx context.Context, filterId int, expan func (i *internalFilterServiceImpl) Update(ctx context.Context, filterId int, payload *model.FilterPayloadScheme) (*model.FilterScheme, *model.ResponseScheme, error) { if filterId == 0 { - return nil, nil, model.ErrNoFieldIDError - } - - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err + return nil, nil, model.ErrNoFilterIDError } endpoint := fmt.Sprintf("rest/api/%v/filter/%v", i.version, filterId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -242,13 +279,14 @@ func (i *internalFilterServiceImpl) Update(ctx context.Context, filterId int, pa } func (i *internalFilterServiceImpl) Delete(ctx context.Context, filterId int) (*model.ResponseScheme, error) { + if filterId == 0 { return nil, model.ErrNoFilterIDError } endpoint := fmt.Sprintf("rest/api/%v/filter/%v", i.version, filterId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -262,6 +300,7 @@ func (i *internalFilterServiceImpl) Delete(ctx context.Context, filterId int) (* } func (i *internalFilterServiceImpl) Change(ctx context.Context, filterId int, accountId string) (*model.ResponseScheme, error) { + if filterId == 0 { return nil, model.ErrNoFilterIDError } @@ -270,20 +309,9 @@ func (i *internalFilterServiceImpl) Change(ctx context.Context, filterId int, ac return nil, model.ErrNoAccountIDError } - payload := struct { - AccountID string `json:"accountId"` - }{ - AccountID: accountId, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/filter/%v/owner", i.version, filterId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"accountId": accountId}) if err != nil { return nil, err } diff --git a/jira/internal/filter_impl_test.go b/jira/internal/filter_impl_test.go index 08f54d56..c13c7e62 100644 --- a/jira/internal/filter_impl_test.go +++ b/jira/internal/filter_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,8 +14,14 @@ import ( func TestFilterService_Create(t *testing.T) { + payloadMocked := &model.FilterPayloadScheme{ + Name: "All Open Bugs", + Description: "Lists all open bugs", + JQL: "type = Bug and resolution is empty", + } + type fields struct { - c service.Client + c service.Connector version string share jira.FilterSharingConnector } @@ -39,21 +44,18 @@ func TestFilterService_Create(t *testing.T) { fields: fields{version: "2"}, args: args{ ctx: context.Background(), - payload: &model.FilterPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FilterPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/filter", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -71,21 +73,18 @@ func TestFilterService_Create(t *testing.T) { fields: fields{version: "3"}, args: args{ ctx: context.Background(), - payload: &model.FilterPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FilterPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/filter", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -98,48 +97,23 @@ func TestFilterService_Create(t *testing.T) { }, }, - { - name: "when the payload is not provided", - fields: fields{version: "2"}, - args: args{ - ctx: context.Background(), - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.FilterPayloadScheme)(nil)). - Return(nil, model.ErrNilPayloadError) - - fields.c = client - - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "2"}, args: args{ ctx: context.Background(), - payload: &model.FilterPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FilterPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/filter", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -184,7 +158,7 @@ func TestFilterService_Create(t *testing.T) { func TestFilterService_Favorite(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string share jira.FilterSharingConnector } @@ -209,12 +183,13 @@ func TestFilterService_Favorite(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/favourite", + "", nil). Return(&http.Request{}, nil) @@ -236,12 +211,13 @@ func TestFilterService_Favorite(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/filter/favourite", + "", nil). Return(&http.Request{}, nil) @@ -263,12 +239,14 @@ func TestFilterService_Favorite(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/favourite", + "", + nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -314,7 +292,7 @@ func TestFilterService_Favorite(t *testing.T) { func TestFilterService_My(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string share jira.FilterSharingConnector } @@ -343,12 +321,13 @@ func TestFilterService_My(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/my?expand=subscriptions&includeFavourites=true", + "", nil). Return(&http.Request{}, nil) @@ -372,12 +351,13 @@ func TestFilterService_My(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/filter/my?expand=subscriptions&includeFavourites=true", + "", nil). Return(&http.Request{}, nil) @@ -401,12 +381,13 @@ func TestFilterService_My(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/my?expand=subscriptions&includeFavourites=true", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -452,7 +433,7 @@ func TestFilterService_My(t *testing.T) { func TestFilterService_Search(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string share jira.FilterSharingConnector } @@ -491,12 +472,13 @@ func TestFilterService_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/search?accountId=owner.accountId&expand=description%2CviewUrl&filterName=filterName&groupname=sharePermissions.group.groupId&id=10000&id=10001&maxResults=100&orderBy=description&projectId=100&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -529,12 +511,13 @@ func TestFilterService_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/filter/search?accountId=owner.accountId&expand=description%2CviewUrl&filterName=filterName&groupname=sharePermissions.group.groupId&id=10000&id=10001&maxResults=100&orderBy=description&projectId=100&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -567,12 +550,13 @@ func TestFilterService_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/search?accountId=owner.accountId&expand=description%2CviewUrl&filterName=filterName&groupname=sharePermissions.group.groupId&id=10000&id=10001&maxResults=100&orderBy=description&projectId=100&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -619,7 +603,7 @@ func TestFilterService_Search(t *testing.T) { func TestFilterService_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string share jira.FilterSharingConnector } @@ -638,6 +622,15 @@ func TestFilterService_Get(t *testing.T) { wantErr bool Err error }{ + { + name: "when filter id is not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.Background(), + }, + wantErr: true, + Err: model.ErrNoFilterIDError, + }, { name: "when the api version is v2", fields: fields{version: "2"}, @@ -648,12 +641,13 @@ func TestFilterService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/10001?expand=viewurl", + "", nil). Return(&http.Request{}, nil) @@ -677,12 +671,13 @@ func TestFilterService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/filter/10001?expand=viewurl", + "", nil). Return(&http.Request{}, nil) @@ -706,13 +701,14 @@ func TestFilterService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/10001?expand=viewurl", - nil). + + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -756,8 +752,12 @@ func TestFilterService_Get(t *testing.T) { func TestFilterService_Update(t *testing.T) { + payloadMocked := &model.FilterPayloadScheme{ + Name: "Filter updated!", + } + type fields struct { - c service.Client + c service.Connector version string share jira.FilterSharingConnector } @@ -776,27 +776,33 @@ func TestFilterService_Update(t *testing.T) { wantErr bool Err error }{ + { + name: "when filter id is not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.Background(), + }, + wantErr: true, + Err: model.ErrNoFilterIDError, + }, { name: "when the api version is v2", fields: fields{version: "2"}, args: args{ ctx: context.Background(), filterId: 10001, - payload: &model.FilterPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FilterPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/filter/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -815,21 +821,18 @@ func TestFilterService_Update(t *testing.T) { args: args{ ctx: context.Background(), filterId: 10001, - payload: &model.FilterPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FilterPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/filter/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -841,50 +844,24 @@ func TestFilterService_Update(t *testing.T) { }, }, - { - name: "when the payload is not provided", - fields: fields{version: "2"}, - args: args{ - ctx: context.Background(), - filterId: 10001, - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.FilterPayloadScheme)(nil)). - Return(nil, model.ErrNilPayloadError) - - fields.c = client - - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "2"}, args: args{ ctx: context.Background(), filterId: 10001, - payload: &model.FilterPayloadScheme{}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FilterPayloadScheme{}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/filter/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -928,7 +905,7 @@ func TestFilterService_Update(t *testing.T) { func TestFilterService_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string share jira.FilterSharingConnector } @@ -946,6 +923,16 @@ func TestFilterService_Delete(t *testing.T) { wantErr bool Err error }{ + + { + name: "when filter id is not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.Background(), + }, + wantErr: true, + Err: model.ErrNoFilterIDError, + }, { name: "when the api version is v2", fields: fields{version: "2"}, @@ -955,12 +942,13 @@ func TestFilterService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/filter/10001", + "", nil). Return(&http.Request{}, nil) @@ -983,12 +971,13 @@ func TestFilterService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/filter/10001", + "", nil). Return(&http.Request{}, nil) @@ -1011,12 +1000,13 @@ func TestFilterService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/filter/10001", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -1061,7 +1051,7 @@ func TestFilterService_Delete(t *testing.T) { func TestFilterService_Change(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string share jira.FilterSharingConnector } @@ -1090,19 +1080,14 @@ func TestFilterService_Change(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/filter/10001/owner", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, nil) client.On("Call", @@ -1125,19 +1110,14 @@ func TestFilterService_Change(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/filter/10001/owner", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, nil) client.On("Call", @@ -1150,6 +1130,28 @@ func TestFilterService_Change(t *testing.T) { }, }, + { + name: "when filter id is not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.Background(), + }, + wantErr: true, + Err: model.ErrNoFilterIDError, + }, + + { + name: "when accountID is not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.Background(), + filterId: 10002, + accountId: "", + }, + wantErr: true, + Err: model.ErrNoAccountIDError, + }, + { name: "when the http request cannot be created", fields: fields{version: "2"}, @@ -1160,19 +1162,14 @@ func TestFilterService_Change(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/filter/10001/owner", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1216,7 +1213,7 @@ func TestFilterService_Change(t *testing.T) { func Test_NewFilterService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From a7f4a16e69c1cf9396e609d3e9a83a0201c57e02 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 2 Jul 2023 14:20:47 -0600 Subject: [PATCH 07/70] :art: Migrated the client interface from Client to Connector on the Jira Filter Share service. --- jira/internal/filter_share_impl.go | 26 ++-- jira/internal/filter_share_impl_test.go | 164 +++++++++--------------- 2 files changed, 72 insertions(+), 118 deletions(-) diff --git a/jira/internal/filter_share_impl.go b/jira/internal/filter_share_impl.go index 51173b12..a0638732 100644 --- a/jira/internal/filter_share_impl.go +++ b/jira/internal/filter_share_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewFilterShareService(client service.Client, version string) (*FilterShareService, error) { +func NewFilterShareService(client service.Connector, version string) (*FilterShareService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -91,7 +91,7 @@ func (f *FilterShareService) Delete(ctx context.Context, filterId, permissionId } type internalFilterShareImpl struct { - c service.Client + c service.Connector version string } @@ -99,7 +99,7 @@ func (i *internalFilterShareImpl) Scope(ctx context.Context) (*model.ShareFilter endpoint := fmt.Sprintf("rest/api/%v/filter/defaultShareScope", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -115,14 +115,9 @@ func (i *internalFilterShareImpl) Scope(ctx context.Context) (*model.ShareFilter func (i *internalFilterShareImpl) SetScope(ctx context.Context, scope string) (*model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(&model.ShareFilterScopeScheme{Scope: scope}) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/filter/defaultShareScope", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", &model.ShareFilterScopeScheme{Scope: scope}) if err != nil { return nil, err } @@ -138,7 +133,7 @@ func (i *internalFilterShareImpl) Gets(ctx context.Context, filterId int) ([]*mo endpoint := fmt.Sprintf("rest/api/%v/filter/%v/permission", i.version, filterId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -158,14 +153,9 @@ func (i *internalFilterShareImpl) Add(ctx context.Context, filterId int, payload return nil, nil, model.ErrNoFilterIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/filter/%v/permission", i.version, filterId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -191,7 +181,7 @@ func (i *internalFilterShareImpl) Get(ctx context.Context, filterId, permissionI endpoint := fmt.Sprintf("rest/api/%v/filter/%v/permission/%v", i.version, filterId, permissionId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -217,7 +207,7 @@ func (i *internalFilterShareImpl) Delete(ctx context.Context, filterId, permissi endpoint := fmt.Sprintf("rest/api/%v/filter/%v/permission/%v", i.version, filterId, permissionId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/filter_share_impl_test.go b/jira/internal/filter_share_impl_test.go index f7efa28c..00a408db 100644 --- a/jira/internal/filter_share_impl_test.go +++ b/jira/internal/filter_share_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func TestFilterShareService_Scope(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -39,12 +38,13 @@ func TestFilterShareService_Scope(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/defaultShareScope", + "", nil). Return(&http.Request{}, nil) @@ -65,12 +65,13 @@ func TestFilterShareService_Scope(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/filter/defaultShareScope", + "", nil). Return(&http.Request{}, nil) @@ -91,12 +92,13 @@ func TestFilterShareService_Scope(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/defaultShareScope", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -140,7 +142,7 @@ func TestFilterShareService_Scope(t *testing.T) { func TestFilterShareService_SetScope(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -166,17 +168,14 @@ func TestFilterShareService_SetScope(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.ShareFilterScopeScheme{Scope: "PRIVATE"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/filter/defaultShareScope", - bytes.NewReader([]byte{})). + "", + &model.ShareFilterScopeScheme{Scope: "PRIVATE"}). Return(&http.Request{}, nil) client.On("Call", @@ -197,17 +196,14 @@ func TestFilterShareService_SetScope(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.ShareFilterScopeScheme{Scope: "PRIVATE"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/filter/defaultShareScope", - bytes.NewReader([]byte{})). + "", + &model.ShareFilterScopeScheme{Scope: "PRIVATE"}). Return(&http.Request{}, nil) client.On("Call", @@ -228,17 +224,14 @@ func TestFilterShareService_SetScope(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.ShareFilterScopeScheme{Scope: "PRIVATE"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/filter/defaultShareScope", - bytes.NewReader([]byte{})). + "", + &model.ShareFilterScopeScheme{Scope: "PRIVATE"}). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -279,7 +272,7 @@ func TestFilterShareService_SetScope(t *testing.T) { func TestFilterShareService_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -305,12 +298,13 @@ func TestFilterShareService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/10001/permission", + "", nil). Return(&http.Request{}, nil) @@ -332,12 +326,13 @@ func TestFilterShareService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/filter/10001/permission", + "", nil). Return(&http.Request{}, nil) @@ -358,7 +353,7 @@ func TestFilterShareService_Gets(t *testing.T) { filterId: 0, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoFilterIDError, @@ -373,12 +368,13 @@ func TestFilterShareService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/10001/permission", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -420,8 +416,13 @@ func TestFilterShareService_Gets(t *testing.T) { func TestFilterShareService_Add(t *testing.T) { + payloadMocked := &model.PermissionFilterPayloadScheme{ + Type: "group", + GroupName: "jira-administrators", + } + type fields struct { - c service.Client + c service.Connector version string } @@ -445,24 +446,18 @@ func TestFilterShareService_Add(t *testing.T) { args: args{ ctx: context.Background(), filterId: 10001, - payload: &model.PermissionFilterPayloadScheme{ - Type: "group", - GroupName: "jira-administrators", - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.PermissionFilterPayloadScheme{Type: "group", GroupName: "jira-administrators"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/filter/10001/permission", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -480,24 +475,18 @@ func TestFilterShareService_Add(t *testing.T) { args: args{ ctx: context.Background(), filterId: 10001, - payload: &model.PermissionFilterPayloadScheme{ - Type: "group", - GroupName: "jira-administrators", - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.PermissionFilterPayloadScheme{Type: "group", GroupName: "jira-administrators"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/filter/10001/permission", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -515,64 +504,33 @@ func TestFilterShareService_Add(t *testing.T) { args: args{ ctx: context.Background(), filterId: 0, - payload: &model.PermissionFilterPayloadScheme{ - Type: "group", - GroupName: "jira-administrators", - }, + payload: payloadMocked, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoFilterIDError, }, - { - name: "when the payload id not provided", - fields: fields{version: "2"}, - args: args{ - ctx: context.Background(), - filterId: 10001, - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.PermissionFilterPayloadScheme)(nil)). - Return(bytes.NewReader([]byte{}), model.ErrNilPayloadError) - - fields.c = client - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "2"}, args: args{ ctx: context.Background(), filterId: 10001, - payload: &model.PermissionFilterPayloadScheme{ - Type: "group", - GroupName: "jira-administrators", - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.PermissionFilterPayloadScheme{Type: "group", GroupName: "jira-administrators"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/filter/10001/permission", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -615,7 +573,7 @@ func TestFilterShareService_Add(t *testing.T) { func TestFilterShareService_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -642,12 +600,13 @@ func TestFilterShareService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/10001/permission/20", + "", nil). Return(&http.Request{}, nil) @@ -670,12 +629,13 @@ func TestFilterShareService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/filter/10001/permission/20", + "", nil). Return(&http.Request{}, nil) @@ -697,7 +657,7 @@ func TestFilterShareService_Get(t *testing.T) { permissionId: 20, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoFilterIDError, @@ -712,7 +672,7 @@ func TestFilterShareService_Get(t *testing.T) { permissionId: 0, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoPermissionGrantIDError, @@ -728,12 +688,13 @@ func TestFilterShareService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/filter/10001/permission/20", + "", nil). Return(&http.Request{}, errors.New("error, unable to creat the http request")) @@ -777,7 +738,7 @@ func TestFilterShareService_Get(t *testing.T) { func TestFilterShareService_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -804,12 +765,13 @@ func TestFilterShareService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/filter/10001/permission/20", + "", nil). Return(&http.Request{}, nil) @@ -832,12 +794,13 @@ func TestFilterShareService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/filter/10001/permission/20", + "", nil). Return(&http.Request{}, nil) @@ -859,7 +822,7 @@ func TestFilterShareService_Delete(t *testing.T) { permissionId: 20, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoFilterIDError, @@ -874,7 +837,7 @@ func TestFilterShareService_Delete(t *testing.T) { permissionId: 0, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoPermissionGrantIDError, @@ -890,12 +853,13 @@ func TestFilterShareService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/filter/10001/permission/20", + "", nil). Return(&http.Request{}, errors.New("error, unable to creat the http request")) @@ -937,7 +901,7 @@ func TestFilterShareService_Delete(t *testing.T) { func Test_NewFilterShareService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 7198b03ce268bd61ac1233be52cf533ea48d95c1 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 2 Jul 2023 14:39:32 -0600 Subject: [PATCH 08/70] :art: Migrated the client interface from Client to Connector on the Jira Group service. --- jira/internal/group_impl.go | 38 ++------- jira/internal/group_impl_test.go | 131 +++++++++++++------------------ 2 files changed, 64 insertions(+), 105 deletions(-) diff --git a/jira/internal/group_impl.go b/jira/internal/group_impl.go index be1465ea..52f0ef88 100644 --- a/jira/internal/group_impl.go +++ b/jira/internal/group_impl.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewGroupService(client service.Client, version string) (*GroupService, error) { +func NewGroupService(client service.Connector, version string) (*GroupService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -81,7 +81,7 @@ func (g *GroupService) Create(ctx context.Context, groupName string) (*model.Gro } type internalGroupServiceImpl struct { - c service.Client + c service.Connector version string } @@ -91,20 +91,9 @@ func (i *internalGroupServiceImpl) Create(ctx context.Context, groupName string) return nil, nil, model.ErrNoGroupNameError } - payload := struct { - Name string `json:"name"` - }{ - Name: groupName, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/group", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"name": groupName}) if err != nil { return nil, nil, err } @@ -129,7 +118,7 @@ func (i *internalGroupServiceImpl) Delete(ctx context.Context, groupName string) endpoint := fmt.Sprintf("rest/api/%v/group?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -156,7 +145,7 @@ func (i *internalGroupServiceImpl) Bulk(ctx context.Context, options *model.Grou endpoint := fmt.Sprintf("rest/api/%v/group/bulk?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -184,7 +173,7 @@ func (i *internalGroupServiceImpl) Members(ctx context.Context, groupName string endpoint := fmt.Sprintf("rest/api/%v/group/member?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -208,22 +197,11 @@ func (i *internalGroupServiceImpl) Add(ctx context.Context, groupName, accountId return nil, nil, model.ErrNoAccountIDError } - payload := struct { - AccountID string `json:"accountId"` - }{ - AccountID: accountId, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - params := url.Values{} params.Add("groupname", groupName) endpoint := fmt.Sprintf("rest/api/%v/group/user?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"accountId": accountId}) if err != nil { return nil, nil, err } @@ -252,7 +230,7 @@ func (i *internalGroupServiceImpl) Remove(ctx context.Context, groupName, accoun params.Add("accountId", accountId) endpoint := fmt.Sprintf("rest/api/%v/group/user?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/group_impl_test.go b/jira/internal/group_impl_test.go index 1773d8b2..24b925bc 100644 --- a/jira/internal/group_impl_test.go +++ b/jira/internal/group_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalGroupServiceImpl_Create(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -41,19 +40,14 @@ func Test_internalGroupServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - }{Name: "jira-users"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/group", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"name": "jira-users"}). Return(&http.Request{}, nil) client.On("Call", @@ -74,19 +68,14 @@ func Test_internalGroupServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - }{Name: "jira-users"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/group", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"name": "jira-users"}). Return(&http.Request{}, nil) client.On("Call", @@ -106,7 +95,7 @@ func Test_internalGroupServiceImpl_Create(t *testing.T) { groupName: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoGroupNameError, @@ -121,19 +110,14 @@ func Test_internalGroupServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - }{Name: "jira-users"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/group", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"name": "jira-users"}). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -176,7 +160,7 @@ func Test_internalGroupServiceImpl_Create(t *testing.T) { func Test_internalGroupServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -202,12 +186,13 @@ func Test_internalGroupServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/group?groupname=jira-users", + "", nil). Return(&http.Request{}, nil) @@ -229,12 +214,13 @@ func Test_internalGroupServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/group?groupname=jira-users", + "", nil). Return(&http.Request{}, nil) @@ -255,7 +241,7 @@ func Test_internalGroupServiceImpl_Delete(t *testing.T) { groupName: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoGroupNameError, @@ -270,12 +256,13 @@ func Test_internalGroupServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/group?groupname=jira-users", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -318,7 +305,7 @@ func Test_internalGroupServiceImpl_Delete(t *testing.T) { func Test_internalGroupServiceImpl_Remove(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -346,12 +333,13 @@ func Test_internalGroupServiceImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/group/user?accountId=account-id-sample&groupname=jira-users", + "", nil). Return(&http.Request{}, nil) @@ -374,12 +362,13 @@ func Test_internalGroupServiceImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/group/user?accountId=account-id-sample&groupname=jira-users", + "", nil). Return(&http.Request{}, nil) @@ -400,7 +389,7 @@ func Test_internalGroupServiceImpl_Remove(t *testing.T) { groupName: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoGroupNameError, @@ -415,7 +404,7 @@ func Test_internalGroupServiceImpl_Remove(t *testing.T) { accountId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoAccountIDError, @@ -431,12 +420,13 @@ func Test_internalGroupServiceImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/group/user?accountId=account-id-sample&groupname=jira-users", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -479,7 +469,7 @@ func Test_internalGroupServiceImpl_Remove(t *testing.T) { func Test_internalGroupServiceImpl_Add(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -507,19 +497,14 @@ func Test_internalGroupServiceImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/group/user?groupname=jira-users", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, nil) client.On("Call", @@ -541,19 +526,14 @@ func Test_internalGroupServiceImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/group/user?groupname=jira-users", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, nil) client.On("Call", @@ -573,7 +553,7 @@ func Test_internalGroupServiceImpl_Add(t *testing.T) { groupName: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoGroupNameError, @@ -588,7 +568,7 @@ func Test_internalGroupServiceImpl_Add(t *testing.T) { accountId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoAccountIDError, @@ -604,19 +584,14 @@ func Test_internalGroupServiceImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/group/user?groupname=jira-users", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -659,7 +634,7 @@ func Test_internalGroupServiceImpl_Add(t *testing.T) { func Test_internalGroupServiceImpl_Bulk(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -691,12 +666,13 @@ func Test_internalGroupServiceImpl_Bulk(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/group/bulk?groupId=1001&groupId=1002&groupName=jira-users&groupName=confluence-users&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -723,12 +699,13 @@ func Test_internalGroupServiceImpl_Bulk(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/group/bulk?groupId=1001&groupId=1002&groupName=jira-users&groupName=confluence-users&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -755,12 +732,13 @@ func Test_internalGroupServiceImpl_Bulk(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/group/bulk?groupId=1001&groupId=1002&groupName=jira-users&groupName=confluence-users&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -805,7 +783,7 @@ func Test_internalGroupServiceImpl_Bulk(t *testing.T) { func Test_internalGroupServiceImpl_Members(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -836,12 +814,13 @@ func Test_internalGroupServiceImpl_Members(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/group/member?groupname=jira-users&includeInactiveUsers=true&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -866,12 +845,13 @@ func Test_internalGroupServiceImpl_Members(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/group/member?groupname=jira-users&includeInactiveUsers=true&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -895,7 +875,7 @@ func Test_internalGroupServiceImpl_Members(t *testing.T) { maxResults: 50, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoGroupNameError, @@ -913,12 +893,13 @@ func Test_internalGroupServiceImpl_Members(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/group/member?groupname=jira-users&includeInactiveUsers=true&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -963,7 +944,7 @@ func Test_internalGroupServiceImpl_Members(t *testing.T) { func Test_NewGroupService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From d3486cfac9e81fcd8a94cba956523f8c648645b5 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Tue, 4 Jul 2023 12:15:26 -0600 Subject: [PATCH 09/70] :art: Migrated the client interface from Client to Connector on the Jira Issue service. --- jira/internal/issue_impl.go | 45 +- jira/internal/issue_impl_adf.go | 166 ++--- jira/internal/issue_impl_adf_test.go | 673 +++++++++----------- jira/internal/issue_impl_rich_text.go | 166 ++--- jira/internal/issue_impl_rich_text_test.go | 679 +++++++++------------ jira/internal/issue_impl_test.go | 2 +- pkg/infra/models/errors.go | 4 +- 7 files changed, 712 insertions(+), 1023 deletions(-) diff --git a/jira/internal/issue_impl.go b/jira/internal/issue_impl.go index 57f9305c..560a05e4 100644 --- a/jira/internal/issue_impl.go +++ b/jira/internal/issue_impl.go @@ -29,7 +29,7 @@ type IssueServices struct { WorklogRichText *WorklogRichTextService } -func NewIssueService(client service.Client, version string, services *IssueServices) (*IssueRichTextService, *IssueADFService, error) { +func NewIssueService(client service.Connector, version string, services *IssueServices) (*IssueRichTextService, *IssueADFService, error) { if version == "" { return nil, nil, model.ErrNoVersionProvided @@ -84,7 +84,12 @@ func NewIssueService(client service.Client, version string, services *IssueServi return richTextService, adfService, nil } -func deleteIssue(ctx context.Context, client service.Client, version, issueKeyOrId string, deleteSubTasks bool) (*model.ResponseScheme, error) { +// ------------------------------------------- +// These private functions are used on the Issue Services implementation, as that services is segmented in the ADF and Rich Text +// format, in order to avoid duplication, those function are injected on the ADF/Rich Text implementations. +// ------------------------------------------- + +func deleteIssue(ctx context.Context, client service.Connector, version, issueKeyOrId string, deleteSubTasks bool) (*model.ResponseScheme, error) { if issueKeyOrId == "" { return nil, model.ErrNoIssueKeyOrIDError @@ -95,7 +100,7 @@ func deleteIssue(ctx context.Context, client service.Client, version, issueKeyOr endpoint := fmt.Sprintf("rest/api/%v/issue/%v?%v", version, issueKeyOrId, params.Encode()) - request, err := client.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := client.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -103,30 +108,19 @@ func deleteIssue(ctx context.Context, client service.Client, version, issueKeyOr return client.Call(request, nil) } -func assignIssue(ctx context.Context, client service.Client, version, issueKeyOrId, accountId string) (*model.ResponseScheme, error) { +func assignIssue(ctx context.Context, client service.Connector, version, issueKeyOrID, accountID string) (*model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, model.ErrNoIssueKeyOrIDError } - if accountId == "" { + if accountID == "" { return nil, model.ErrNoAccountIDError } - payload := struct { - AccountID string `json:"accountId"` - }{ - AccountID: accountId, - } - - reader, err := client.TransformStructToReader(&payload) - if err != nil { - return nil, err - } - - endpoint := fmt.Sprintf("/rest/api/%v/issue/%v/assignee", version, issueKeyOrId) + endpoint := fmt.Sprintf("/rest/api/%v/issue/%v/assignee", version, issueKeyOrID) - request, err := client.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := client.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"accountId": accountID}) if err != nil { return nil, err } @@ -134,21 +128,16 @@ func assignIssue(ctx context.Context, client service.Client, version, issueKeyOr return client.Call(request, nil) } -func sendNotification(ctx context.Context, client service.Client, version, issueKeyOrId string, options *model.IssueNotifyOptionsScheme) ( +func sendNotification(ctx context.Context, client service.Connector, version, issueKeyOrId string, options *model.IssueNotifyOptionsScheme) ( *model.ResponseScheme, error) { if issueKeyOrId == "" { return nil, model.ErrNoIssueKeyOrIDError } - reader, err := client.TransformStructToReader(options) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/notify", version, issueKeyOrId) - request, err := client.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := client.NewRequest(ctx, http.MethodPost, endpoint, "", options) if err != nil { return nil, err } @@ -156,7 +145,7 @@ func sendNotification(ctx context.Context, client service.Client, version, issue return client.Call(request, nil) } -func getTransitions(ctx context.Context, client service.Client, version, issueKeyOrId string) (*model.IssueTransitionsScheme, *model.ResponseScheme, error) { +func getTransitions(ctx context.Context, client service.Connector, version, issueKeyOrId string) (*model.IssueTransitionsScheme, *model.ResponseScheme, error) { if issueKeyOrId == "" { return nil, nil, model.ErrNoIssueKeyOrIDError @@ -164,7 +153,7 @@ func getTransitions(ctx context.Context, client service.Client, version, issueKe endpoint := fmt.Sprintf("rest/api/%v/issue/%v/transitions", version, issueKeyOrId) - request, err := client.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := client.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/issue_impl_adf.go b/jira/internal/issue_impl_adf.go index b7c9a63c..0e43281f 100644 --- a/jira/internal/issue_impl_adf.go +++ b/jira/internal/issue_impl_adf.go @@ -2,13 +2,11 @@ package internal import ( "context" - "errors" "fmt" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" "github.com/ctreminiom/go-atlassian/service" "github.com/ctreminiom/go-atlassian/service/jira" "github.com/imdario/mergo" - "io" "net/http" "net/url" "strings" @@ -48,7 +46,7 @@ func (i *IssueADFService) Delete(ctx context.Context, issueKeyOrId string, delet // Assign assigns an issue to a user. // -// Use this operation when the calling user does not have the Edit Issues permission but has the +// # Use this operation when the calling user does not have the Edit Issues permission but has the // // Assign issue permission for the project that the issue is in. // @@ -127,7 +125,7 @@ func (i *IssueADFService) Get(ctx context.Context, issueKeyOrId string, fields, // // Edits an issue. A transition may be applied and issue properties updated as part of the edit. // -// The edits to the issue's fields are defined using update and fields +// # The edits to the issue's fields are defined using update and fields // // PUT /rest/api/{2-3}/issue/{issueIdOrKey} // @@ -148,7 +146,7 @@ func (i *IssueADFService) Move(ctx context.Context, issueKeyOrId, transitionId s } type internalIssueADFServiceImpl struct { - c service.Client + c service.Connector version string } @@ -170,36 +168,29 @@ func (i *internalIssueADFServiceImpl) Transitions(ctx context.Context, issueKeyO func (i *internalIssueADFServiceImpl) Create(ctx context.Context, payload *model.IssueScheme, customFields *model.CustomFields) (*model.IssueResponseScheme, *model.ResponseScheme, error) { - var reader io.Reader + var request *http.Request var err error + endpoint := fmt.Sprintf("rest/api/%v/issue", i.version) + if customFields != nil { - payloadUpdated, err := payload.MergeCustomFields(customFields) + payloadWithFields, err := payload.MergeCustomFields(customFields) if err != nil { return nil, nil, err } - reader, err = i.c.TransformStructToReader(payloadUpdated) + request, err = i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payloadWithFields) if err != nil { return nil, nil, err } - } else { - - reader, err = i.c.TransformStructToReader(payload) + request, err = i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } } - endpoint := fmt.Sprintf("rest/api/%v/issue", i.version) - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) - if err != nil { - return nil, nil, err - } - issue := new(model.IssueResponseScheme) response, err := i.c.Call(request, issue) if err != nil { @@ -212,8 +203,7 @@ func (i *internalIssueADFServiceImpl) Create(ctx context.Context, payload *model func (i *internalIssueADFServiceImpl) Creates(ctx context.Context, payload []*model.IssueBulkSchemeV3) (*model.IssueBulkResponseScheme, *model.ResponseScheme, error) { if len(payload) == 0 { - return nil, nil, errors.New("error, please provide a valid []*IssueBulkScheme slice of pointers") - // TODO: The errors when the bulk creates does not contains values needs to be parsed and moved to the model package + return nil, nil, model.ErrNoCreateIssuesError } var issuePayloads []map[string]interface{} @@ -231,17 +221,9 @@ func (i *internalIssueADFServiceImpl) Creates(ctx context.Context, payload []*mo issuePayloads = append(issuePayloads, issuePayload) } - var bulkPayload = map[string]interface{}{} - bulkPayload["issueUpdates"] = issuePayloads - - reader, err := i.c.TransformStructToReader(&bulkPayload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issue/bulk", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"issueUpdates": issuePayloads}) if err != nil { return nil, nil, err } @@ -278,7 +260,7 @@ func (i *internalIssueADFServiceImpl) Get(ctx context.Context, issueKeyOrId stri endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -302,22 +284,16 @@ func (i *internalIssueADFServiceImpl) Update(ctx context.Context, issueKeyOrId s params.Add("notifyUsers", fmt.Sprintf("%v", notify)) endpoint := fmt.Sprintf("rest/api/%v/issue/%v?%v", i.version, issueKeyOrId, params.Encode()) - var reader io.Reader - var err error - - // Executed when customfields and operations are not provided - if customFields == nil && operations == nil { + withCustomFields := customFields != nil + withOperations := operations != nil - reader, err = i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - } + var err error + payloadUpdated := make(map[string]interface{}) - // Executed when customfields and operation are provided - if customFields != nil && operations != nil { + // Executed when the customfields and operations are provided + if withCustomFields && withOperations { - payloadUpdated, err := payload.MergeCustomFields(customFields) + payloadUpdated, err = payload.MergeCustomFields(customFields) if err != nil { return nil, err } @@ -327,49 +303,45 @@ func (i *internalIssueADFServiceImpl) Update(ctx context.Context, issueKeyOrId s return nil, err } - if err := mergo.Map(&payloadUpdated, &payloadWithOperations, mergo.WithOverride); err != nil { - return nil, err - } - - reader, err = i.c.TransformStructToReader(&payloadUpdated) - if err != nil { + if err = mergo.Map(&payloadUpdated, &payloadWithOperations, mergo.WithOverride); err != nil { return nil, err } } - // Executed when customfields are provided but not the operations - if customFields != nil && operations == nil { + // Executed when only the customfields are provided, but not the operations + if withCustomFields && !withOperations { - payloadUpdated, err := payload.MergeCustomFields(customFields) + payloadUpdated, err = payload.MergeCustomFields(customFields) if err != nil { return nil, err } + } - reader, err = i.c.TransformStructToReader(&payloadUpdated) + // Executed when only the operations are provided, but not the customfields + if withOperations && !withCustomFields { + + payloadUpdated, err = payload.MergeOperations(operations) if err != nil { return nil, err } } - // Executed when operations are provided but not the customfields - if customFields == nil && operations != nil { + // After the payload transformation, validate if the shadowed payloadUpdated variable contains data + var request *http.Request + + if len(payloadUpdated) != 0 { - payloadUpdated, err := payload.MergeOperations(operations) + request, err = i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payloadUpdated) if err != nil { return nil, err } - - reader, err = i.c.TransformStructToReader(&payloadUpdated) + } else { + request, err = i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } } - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) - if err != nil { - return nil, err - } - return i.c.Call(request, nil) } @@ -386,83 +358,59 @@ func (i *internalIssueADFServiceImpl) Move(ctx context.Context, issueKeyOrId, tr payloadUpdated := make(map[string]interface{}) payloadUpdated["transition"] = map[string]interface{}{"id": transitionId} - var reader io.Reader - var err error + // Process logic only if the transition options are provided + if options != nil { - if options != nil && options.Fields != nil { + if options.Fields == nil { + return nil, model.ErrNoIssueSchemeError + } - // Executed when customfields and operation are provided - if options.CustomFields != nil && options.Operations != nil { + withCustomFields := options.CustomFields != nil + withOperations := options.Operations != nil - payloadWithCustomFields, err := options.Fields.MergeCustomFields(options.CustomFields) - if err != nil { - return nil, err - } + var err error - payloadWithOperations, err := options.Fields.MergeOperations(options.Operations) - if err != nil { - return nil, err - } - - if err := mergo.Map(&payloadWithCustomFields, &payloadWithOperations, mergo.WithOverride); err != nil { - return nil, err - } - - if err := mergo.Map(&payloadWithCustomFields, &payloadUpdated, mergo.WithOverride); err != nil { - return nil, err - } + // Executed when the customfields and operations are provided + if withCustomFields && withOperations { - reader, err = i.c.TransformStructToReader(&payloadWithCustomFields) + payloadUpdated, err = options.Fields.MergeCustomFields(options.CustomFields) if err != nil { return nil, err } - } - - // Executed when customfields are provided but not the operations - if options.CustomFields != nil && options.Operations == nil { - payloadWithCustomFields, err := options.Fields.MergeCustomFields(options.CustomFields) + payloadWithOperations, err := options.Fields.MergeOperations(options.Operations) if err != nil { return nil, err } - if err := mergo.Map(&payloadWithCustomFields, &payloadUpdated, mergo.WithOverride); err != nil { - return nil, err - } - - reader, err = i.c.TransformStructToReader(&payloadWithCustomFields) - if err != nil { + if err = mergo.Map(&payloadUpdated, &payloadWithOperations, mergo.WithOverride); err != nil { return nil, err } } - // Executed when operations are provided but not the customfields - if options.CustomFields == nil && options.Operations != nil { + // Executed when only the customfields are provided, but not the operations + if withCustomFields && !withOperations { - payloadWithOperations, err := options.Fields.MergeOperations(options.Operations) + payloadUpdated, err = options.Fields.MergeCustomFields(options.CustomFields) if err != nil { return nil, err } + } - if err := mergo.Map(&payloadWithOperations, &payloadUpdated, mergo.WithOverride); err != nil { - return nil, err - } + // Executed when only the operations are provided, but not the customfields + if withOperations && !withCustomFields { - reader, err = i.c.TransformStructToReader(&payloadWithOperations) + payloadUpdated, err = options.Fields.MergeOperations(options.Operations) if err != nil { return nil, err } } - } else { - reader, err = i.c.TransformStructToReader(&payloadUpdated) - if err != nil { - return nil, err - } + } endpoint := fmt.Sprintf("rest/api/%v/issue/%v/transitions", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payloadUpdated) if err != nil { return nil, err } diff --git a/jira/internal/issue_impl_adf_test.go b/jira/internal/issue_impl_adf_test.go index 52d5d4b3..dd0fbc4c 100644 --- a/jira/internal/issue_impl_adf_test.go +++ b/jira/internal/issue_impl_adf_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalIssueADFServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -43,12 +42,13 @@ func Test_internalIssueADFServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-1?deleteSubtasks=true", + "", nil). Return(&http.Request{}, nil) @@ -70,7 +70,7 @@ func Test_internalIssueADFServiceImpl_Delete(t *testing.T) { deleteSubTasks: true, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -86,12 +86,13 @@ func Test_internalIssueADFServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-1?deleteSubtasks=true", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -135,7 +136,7 @@ func Test_internalIssueADFServiceImpl_Delete(t *testing.T) { func Test_internalIssueADFServiceImpl_Assign(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -162,19 +163,14 @@ func Test_internalIssueADFServiceImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "/rest/api/3/issue/DUMMY-1/assignee", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, nil) client.On("Call", @@ -195,7 +191,7 @@ func Test_internalIssueADFServiceImpl_Assign(t *testing.T) { accountId: "account-id-sample", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -210,7 +206,7 @@ func Test_internalIssueADFServiceImpl_Assign(t *testing.T) { accountId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoAccountIDError, @@ -226,19 +222,14 @@ func Test_internalIssueADFServiceImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "/rest/api/3/issue/DUMMY-1/assignee", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -280,8 +271,17 @@ func Test_internalIssueADFServiceImpl_Assign(t *testing.T) { func Test_internalIssueADFServiceImpl_Notify(t *testing.T) { + optionsMocked := &model.IssueNotifyOptionsScheme{ + HTMLBody: "The latest test results for this ticket are now available.", + Subject: "SUBJECT EMAIL EXAMPLE", + To: &model.IssueNotifyToScheme{ + Reporter: true, + Assignee: true, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -305,35 +305,18 @@ func Test_internalIssueADFServiceImpl_Notify(t *testing.T) { args: args{ ctx: context.TODO(), issueKeyOrId: "DUMMY-1", - options: &model.IssueNotifyOptionsScheme{ - HTMLBody: "The latest test results for this ticket are now available.", - Subject: "SUBJECT EMAIL EXAMPLE", - To: &model.IssueNotifyToScheme{ - Reporter: true, - Assignee: true, - }, - }, + options: optionsMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.IssueNotifyOptionsScheme{ - HTMLBody: "The latest test results for this ticket are now available.", - Subject: "SUBJECT EMAIL EXAMPLE", - To: &model.IssueNotifyToScheme{ - Reporter: true, - Assignee: true, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/notify", - bytes.NewReader([]byte{})). + "", + optionsMocked). Return(&http.Request{}, nil) client.On("Call", @@ -353,7 +336,7 @@ func Test_internalIssueADFServiceImpl_Notify(t *testing.T) { issueKeyOrId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -365,35 +348,18 @@ func Test_internalIssueADFServiceImpl_Notify(t *testing.T) { args: args{ ctx: context.TODO(), issueKeyOrId: "DUMMY-1", - options: &model.IssueNotifyOptionsScheme{ - HTMLBody: "The latest test results for this ticket are now available.", - Subject: "SUBJECT EMAIL EXAMPLE", - To: &model.IssueNotifyToScheme{ - Reporter: true, - Assignee: true, - }, - }, + options: optionsMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.IssueNotifyOptionsScheme{ - HTMLBody: "The latest test results for this ticket are now available.", - Subject: "SUBJECT EMAIL EXAMPLE", - To: &model.IssueNotifyToScheme{ - Reporter: true, - Assignee: true, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/notify", - bytes.NewReader([]byte{})). + "", + optionsMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -436,7 +402,7 @@ func Test_internalIssueADFServiceImpl_Notify(t *testing.T) { func Test_internalIssueADFServiceImpl_Transitions(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -462,12 +428,13 @@ func Test_internalIssueADFServiceImpl_Transitions(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-1/transitions", + "", nil). Return(&http.Request{}, nil) @@ -488,7 +455,7 @@ func Test_internalIssueADFServiceImpl_Transitions(t *testing.T) { issueKeyOrId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -503,12 +470,13 @@ func Test_internalIssueADFServiceImpl_Transitions(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-1/transitions", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -552,21 +520,38 @@ func Test_internalIssueADFServiceImpl_Transitions(t *testing.T) { func Test_internalIssueADFServiceImpl_Create(t *testing.T) { - customFields := &model.CustomFields{} + payloadMocked := &model.IssueScheme{ + Fields: &model.IssueFieldsScheme{ + Summary: "New summary test", + Project: &model.ProjectScheme{ID: "10000"}, + IssueType: &model.IssueTypeScheme{Name: "Story"}, + }, + } + + customFieldsMocked := &model.CustomFields{} // Add a new custom field - err := customFields.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) + err := customFieldsMocked.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) if err != nil { t.Fatal(err) } - err = customFields.Number("customfield_10042", 1000.2222) + err = customFieldsMocked.Number("customfield_10042", 1000.2222) if err != nil { t.Fatal(err) } + expectedPayloadWithCustomFields := map[string]interface{}{ + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{"name": "jira-administrators"}, map[string]interface{}{"name": "jira-administrators-system"}}, + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -588,34 +573,20 @@ func Test_internalIssueADFServiceImpl_Create(t *testing.T) { name: "when the api version is v3", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - payload: &model.IssueScheme{ - Fields: &model.IssueFieldsScheme{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - customFields: customFields, + ctx: context.TODO(), + payload: payloadMocked, + customFields: customFieldsMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - map[string]interface{}{ - "fields": map[string]interface{}{"customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFields). Return(&http.Request{}, nil) client.On("Call", @@ -631,33 +602,20 @@ func Test_internalIssueADFServiceImpl_Create(t *testing.T) { name: "when the customfield are not provided", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - payload: &model.IssueScheme{ - Fields: &model.IssueFieldsScheme{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, + ctx: context.TODO(), + payload: payloadMocked, customFields: nil, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.IssueScheme{Fields: &model.IssueFieldsScheme{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -673,34 +631,20 @@ func Test_internalIssueADFServiceImpl_Create(t *testing.T) { name: "when the http request cannot be created", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - payload: &model.IssueScheme{ - Fields: &model.IssueFieldsScheme{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - customFields: customFields, + ctx: context.TODO(), + payload: payloadMocked, + customFields: customFieldsMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - map[string]interface{}{ - "fields": map[string]interface{}{"customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFields). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -743,21 +687,67 @@ func Test_internalIssueADFServiceImpl_Create(t *testing.T) { func Test_internalIssueADFServiceImpl_Creates(t *testing.T) { - customFields := &model.CustomFields{} + customFieldsMocked := &model.CustomFields{} - // Add a new custom field - err := customFields.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) + err := customFieldsMocked.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) if err != nil { t.Fatal(err) } - err = customFields.Number("customfield_10042", 1000.2222) + err = customFieldsMocked.Number("customfield_10042", 1000.2222) if err != nil { t.Fatal(err) } + payloadMocked := []*model.IssueBulkSchemeV3{ + { + Payload: &model.IssueScheme{ + Fields: &model.IssueFieldsScheme{ + Summary: "New summary test", + Project: &model.ProjectScheme{ID: "10000"}, + IssueType: &model.IssueTypeScheme{Name: "Story"}, + }, + }, + CustomFields: customFieldsMocked, + }, + + { + Payload: nil, + CustomFields: nil, + }, + + { + Payload: &model.IssueScheme{ + Fields: &model.IssueFieldsScheme{ + Summary: "New summary test #2", + Project: &model.ProjectScheme{ID: "10000"}, + IssueType: &model.IssueTypeScheme{Name: "Story"}, + }, + }, + CustomFields: customFieldsMocked, + }, + } + + expectedBulkWithCustomFieldsPayload := map[string]interface{}{ + + "issueUpdates": []map[string]interface{}{map[string]interface{}{ + + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{"name": "jira-administrators"}, map[string]interface{}{"name": "jira-administrators-system"}}, + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}}, map[string]interface{}{ + + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{"name": "jira-administrators"}, map[string]interface{}{"name": "jira-administrators-system"}}, + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test #2"}}}} + type fields struct { - c service.Client + c service.Connector version string } @@ -778,62 +768,19 @@ func Test_internalIssueADFServiceImpl_Creates(t *testing.T) { name: "when the api version is v3", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - payload: []*model.IssueBulkSchemeV3{ - { - Payload: &model.IssueScheme{ - Fields: &model.IssueFieldsScheme{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - CustomFields: customFields, - }, - - { - Payload: nil, - CustomFields: nil, - }, - - { - Payload: &model.IssueScheme{ - Fields: &model.IssueFieldsScheme{ - Summary: "New summary test #2", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - CustomFields: customFields, - }, - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{"issueUpdates": []map[string]interface{}{{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}}, - - {"fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test #2"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/bulk", - bytes.NewReader([]byte{})). + "", + expectedBulkWithCustomFieldsPayload). Return(&http.Request{}, nil) client.On("Call", @@ -853,72 +800,29 @@ func Test_internalIssueADFServiceImpl_Creates(t *testing.T) { payload: nil, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, - Err: errors.New("error, please provide a valid []*IssueBulkScheme slice of pointers"), + Err: model.ErrNoCreateIssuesError, }, { name: "when the http request cannot be created", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - payload: []*model.IssueBulkSchemeV3{ - { - Payload: &model.IssueScheme{ - Fields: &model.IssueFieldsScheme{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - CustomFields: customFields, - }, - - { - Payload: nil, - CustomFields: nil, - }, - - { - Payload: &model.IssueScheme{ - Fields: &model.IssueFieldsScheme{ - Summary: "New summary test #2", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - CustomFields: customFields, - }, - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{"issueUpdates": []map[string]interface{}{{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}}, - - {"fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test #2"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/bulk", - bytes.NewReader([]byte{})). + "", + expectedBulkWithCustomFieldsPayload). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -975,7 +879,7 @@ func Test_internalIssueADFServiceImpl_Get(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1004,12 +908,13 @@ func Test_internalIssueADFServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-1?expand=operations%2Cchangelogts&fields=summary%2Cstatus", + "", nil). Return(&http.Request{}, nil) @@ -1023,7 +928,7 @@ func Test_internalIssueADFServiceImpl_Get(t *testing.T) { }, { - name: "when the api version is v3", + name: "when the issue key or id is not provided", fields: fields{version: "3"}, args: args{ ctx: context.TODO(), @@ -1032,7 +937,7 @@ func Test_internalIssueADFServiceImpl_Get(t *testing.T) { expand: []string{"operations", "changelogts"}, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -1049,12 +954,13 @@ func Test_internalIssueADFServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-1?expand=operations%2Cchangelogts&fields=summary%2Cstatus", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -1099,28 +1005,68 @@ func Test_internalIssueADFServiceImpl_Get(t *testing.T) { func Test_internalIssueADFServiceImpl_Move(t *testing.T) { - customFields := &model.CustomFields{} + customFieldsMocked := &model.CustomFields{} - err := customFields.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) + err := customFieldsMocked.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) if err != nil { t.Fatal(err) } - err = customFields.Number("customfield_10042", 1000.2222) + err = customFieldsMocked.Number("customfield_10042", 1000.2222) if err != nil { t.Fatal(err) } - operations := &model.UpdateOperations{} - err = operations.AddArrayOperation("labels", map[string]string{ + operationsMocked := &model.UpdateOperations{} + err = operationsMocked.AddArrayOperation("labels", map[string]string{ "triaged": "remove", }) + + expectedPayloadWithCustomFieldsAndOperations := map[string]interface{}{ + + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{ + "name": "jira-administrators"}, map[string]interface{}{ + "name": "jira-administrators-system"}}, + + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}, + + "update": map[string]interface{}{ + "labels": []map[string]interface{}{map[string]interface{}{ + "remove": "triaged"}}}} + + expectedPayloadWithCustomfields := map[string]interface{}{ + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{ + "name": "jira-administrators"}, map[string]interface{}{ + "name": "jira-administrators-system"}}, + + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}} + + expectedPayloadWithOperations := map[string]interface{}{ + "fields": map[string]interface{}{ + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}, + + "update": map[string]interface{}{ + "labels": []map[string]interface{}{map[string]interface{}{ + "remove": "triaged"}}}} + + expectedPayloadWithNoOptions := map[string]interface{}{"transition": map[string]interface{}{"id": "10001"}} + if err != nil { t.Fatal(err) } type fields struct { - c service.Client + c service.Connector version string } @@ -1153,32 +1099,20 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, - Operations: operations, + CustomFields: customFieldsMocked, + Operations: operationsMocked, }, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}, - "transition": map[string]interface{}{"id": "10001"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFieldsAndOperations). Return(&http.Request{}, nil) client.On("Call", @@ -1190,6 +1124,22 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { }, }, + { + name: "when the options are provided and the fields are not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueKeyOrId: "DUMMY-1", + transitionId: "10001", + options: &model.IssueMoveOptionsV3{ + CustomFields: customFieldsMocked, + Operations: operationsMocked, + }, + }, + wantErr: true, + Err: model.ErrNoIssueSchemeError, + }, + { name: "when the operations are not provided", fields: fields{version: "3"}, @@ -1205,30 +1155,20 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, + CustomFields: customFieldsMocked, Operations: nil, }, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}, - "transition": map[string]interface{}{"id": "10001"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomfields). Return(&http.Request{}, nil) client.On("Call", @@ -1256,29 +1196,19 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { }, }, CustomFields: nil, - Operations: operations, + Operations: operationsMocked, }, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}, - "transition": map[string]interface{}{"id": "10001"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithOperations). Return(&http.Request{}, nil) client.On("Call", @@ -1301,18 +1231,14 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "transition": map[string]interface{}{"id": "10001"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithNoOptions). Return(&http.Request{}, nil) client.On("Call", @@ -1339,12 +1265,12 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, - Operations: operations, + CustomFields: customFieldsMocked, + Operations: operationsMocked, }, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -1365,12 +1291,12 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, - Operations: operations, + CustomFields: customFieldsMocked, + Operations: operationsMocked, }, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoTransitionIDError, @@ -1391,32 +1317,20 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, - Operations: operations, + CustomFields: customFieldsMocked, + Operations: operationsMocked, }, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}, - "transition": map[string]interface{}{"id": "10001"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFieldsAndOperations). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1459,14 +1373,14 @@ func Test_internalIssueADFServiceImpl_Move(t *testing.T) { func Test_internalIssueADFServiceImpl_Update(t *testing.T) { - customFields := &model.CustomFields{} + customFieldsMocked := &model.CustomFields{} - err := customFields.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) + err := customFieldsMocked.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) if err != nil { t.Fatal(err) } - err = customFields.Number("customfield_10042", 1000.2222) + err = customFieldsMocked.Number("customfield_10042", 1000.2222) if err != nil { t.Fatal(err) } @@ -1479,8 +1393,32 @@ func Test_internalIssueADFServiceImpl_Update(t *testing.T) { t.Fatal(err) } + expectedPayloadWithCustomFieldsAndOperations := map[string]interface{}{ + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{ + "name": "jira-administrators"}, map[string]interface{}{ + "name": "jira-administrators-system"}}, "summary": "New summary test"}, + "update": map[string]interface{}{ + "labels": []map[string]interface{}{map[string]interface{}{ + "remove": "triaged"}}}} + + expectedPayloadWithCustomfields := map[string]interface{}{ + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{ + "name": "jira-administrators"}, map[string]interface{}{ + "name": "jira-administrators-system"}}, + "summary": "New summary test"}} + + expectedPayloadWithOperations := map[string]interface{}{ + "fields": map[string]interface{}{ + "summary": "New summary test"}, + "update": map[string]interface{}{ + "labels": []map[string]interface{}{map[string]interface{}{"remove": "triaged"}}}} + type fields struct { - c service.Client + c service.Connector version string } @@ -1513,28 +1451,19 @@ func Test_internalIssueADFServiceImpl_Update(t *testing.T) { Summary: "New summary test", }, }, - customFields: customFields, + customFields: customFieldsMocked, operations: operations, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "summary": "New summary test"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFieldsAndOperations). Return(&http.Request{}, nil) client.On("Call", @@ -1558,11 +1487,11 @@ func Test_internalIssueADFServiceImpl_Update(t *testing.T) { Summary: "New summary test", }, }, - customFields: customFields, + customFields: customFieldsMocked, operations: operations, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -1580,28 +1509,19 @@ func Test_internalIssueADFServiceImpl_Update(t *testing.T) { Summary: "New summary test", }, }, - customFields: customFields, + customFields: customFieldsMocked, operations: operations, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "summary": "New summary test"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFieldsAndOperations). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1622,26 +1542,19 @@ func Test_internalIssueADFServiceImpl_Update(t *testing.T) { Summary: "New summary test", }, }, - customFields: customFields, + customFields: customFieldsMocked, operations: nil, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "summary": "New summary test"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomfields). Return(&http.Request{}, nil) client.On("Call", @@ -1670,21 +1583,14 @@ func Test_internalIssueADFServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "summary": "New summary test"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithOperations). Return(&http.Request{}, nil) client.On("Call", @@ -1713,21 +1619,18 @@ func Test_internalIssueADFServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) - client.On("TransformStructToReader", + client.On("NewRequest", + context.Background(), + http.MethodPut, + "rest/api/3/issue/DUMMY-1?notifyUsers=true", + "", &model.IssueScheme{ Fields: &model.IssueFieldsScheme{ Summary: "New summary test", }, }). - Return(bytes.NewReader([]byte{}), nil) - - client.On("NewRequest", - context.Background(), - http.MethodPut, - "rest/api/3/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). Return(&http.Request{}, nil) client.On("Call", diff --git a/jira/internal/issue_impl_rich_text.go b/jira/internal/issue_impl_rich_text.go index e31e21ab..68043b12 100644 --- a/jira/internal/issue_impl_rich_text.go +++ b/jira/internal/issue_impl_rich_text.go @@ -2,13 +2,11 @@ package internal import ( "context" - "errors" "fmt" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" "github.com/ctreminiom/go-atlassian/service" "github.com/ctreminiom/go-atlassian/service/jira" "github.com/imdario/mergo" - "io" "net/http" "net/url" "strings" @@ -48,7 +46,7 @@ func (i IssueRichTextService) Delete(ctx context.Context, issueKeyOrId string, d // Assign assigns an issue to a user. // -// Use this operation when the calling user does not have the Edit Issues permission but has the +// # Use this operation when the calling user does not have the Edit Issues permission but has the // // Assign issue permission for the project that the issue is in. // @@ -127,7 +125,7 @@ func (i IssueRichTextService) Get(ctx context.Context, issueKeyOrId string, fiel // // Edits an issue. A transition may be applied and issue properties updated as part of the edit. // -// The edits to the issue's fields are defined using update and fields +// # The edits to the issue's fields are defined using update and fields // // PUT /rest/api/{2-3}/issue/{issueIdOrKey} // @@ -148,7 +146,7 @@ func (i IssueRichTextService) Move(ctx context.Context, issueKeyOrId, transition } type internalRichTextServiceImpl struct { - c service.Client + c service.Connector version string } @@ -170,36 +168,29 @@ func (i *internalRichTextServiceImpl) Transitions(ctx context.Context, issueKeyO func (i *internalRichTextServiceImpl) Create(ctx context.Context, payload *model.IssueSchemeV2, customFields *model.CustomFields) (*model.IssueResponseScheme, *model.ResponseScheme, error) { - var reader io.Reader + var request *http.Request var err error + endpoint := fmt.Sprintf("rest/api/%v/issue", i.version) + if customFields != nil { - payloadUpdated, err := payload.MergeCustomFields(customFields) + payloadWithFields, err := payload.MergeCustomFields(customFields) if err != nil { return nil, nil, err } - reader, err = i.c.TransformStructToReader(payloadUpdated) + request, err = i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payloadWithFields) if err != nil { return nil, nil, err } - } else { - - reader, err = i.c.TransformStructToReader(payload) + request, err = i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } } - endpoint := fmt.Sprintf("rest/api/%v/issue", i.version) - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) - if err != nil { - return nil, nil, err - } - issue := new(model.IssueResponseScheme) response, err := i.c.Call(request, issue) if err != nil { @@ -212,8 +203,7 @@ func (i *internalRichTextServiceImpl) Create(ctx context.Context, payload *model func (i *internalRichTextServiceImpl) Creates(ctx context.Context, payload []*model.IssueBulkSchemeV2) (*model.IssueBulkResponseScheme, *model.ResponseScheme, error) { if len(payload) == 0 { - return nil, nil, errors.New("error, please provide a valid []*IssueBulkScheme slice of pointers") - // TODO: The errors when the bulk creates does not contains values needs to be parsed and moved to the model package + return nil, nil, model.ErrNoCreateIssuesError } var issuePayloads []map[string]interface{} @@ -231,17 +221,9 @@ func (i *internalRichTextServiceImpl) Creates(ctx context.Context, payload []*mo issuePayloads = append(issuePayloads, issuePayload) } - var bulkPayload = map[string]interface{}{} - bulkPayload["issueUpdates"] = issuePayloads - - reader, err := i.c.TransformStructToReader(&bulkPayload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issue/bulk", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"issueUpdates": issuePayloads}) if err != nil { return nil, nil, err } @@ -278,7 +260,7 @@ func (i *internalRichTextServiceImpl) Get(ctx context.Context, issueKeyOrId stri endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -302,22 +284,16 @@ func (i *internalRichTextServiceImpl) Update(ctx context.Context, issueKeyOrId s params.Add("notifyUsers", fmt.Sprintf("%v", notify)) endpoint := fmt.Sprintf("rest/api/%v/issue/%v?%v", i.version, issueKeyOrId, params.Encode()) - var reader io.Reader - var err error - - // Executed when customfields and operations are not provided - if customFields == nil && operations == nil { + withCustomFields := customFields != nil + withOperations := operations != nil - reader, err = i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - } + var err error + payloadUpdated := make(map[string]interface{}) - // Executed when customfields and operation are provided - if customFields != nil && operations != nil { + // Executed when the customfields and operations are provided + if withCustomFields && withOperations { - payloadUpdated, err := payload.MergeCustomFields(customFields) + payloadUpdated, err = payload.MergeCustomFields(customFields) if err != nil { return nil, err } @@ -327,49 +303,45 @@ func (i *internalRichTextServiceImpl) Update(ctx context.Context, issueKeyOrId s return nil, err } - if err := mergo.Map(&payloadUpdated, &payloadWithOperations, mergo.WithOverride); err != nil { - return nil, err - } - - reader, err = i.c.TransformStructToReader(&payloadUpdated) - if err != nil { + if err = mergo.Map(&payloadUpdated, &payloadWithOperations, mergo.WithOverride); err != nil { return nil, err } } - // Executed when customfields are provided but not the operations - if customFields != nil && operations == nil { + // Executed when only the customfields are provided, but not the operations + if withCustomFields && !withOperations { - payloadUpdated, err := payload.MergeCustomFields(customFields) + payloadUpdated, err = payload.MergeCustomFields(customFields) if err != nil { return nil, err } + } - reader, err = i.c.TransformStructToReader(&payloadUpdated) + // Executed when only the operations are provided, but not the customfields + if withOperations && !withCustomFields { + + payloadUpdated, err = payload.MergeOperations(operations) if err != nil { return nil, err } } - // Executed when operations are provided but not the customfields - if customFields == nil && operations != nil { + // After the payload transformation, validate if the shadowed payloadUpdated variable contains data + var request *http.Request + + if len(payloadUpdated) != 0 { - payloadUpdated, err := payload.MergeOperations(operations) + request, err = i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payloadUpdated) if err != nil { return nil, err } - - reader, err = i.c.TransformStructToReader(&payloadUpdated) + } else { + request, err = i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } } - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) - if err != nil { - return nil, err - } - return i.c.Call(request, nil) } @@ -386,83 +358,59 @@ func (i *internalRichTextServiceImpl) Move(ctx context.Context, issueKeyOrId, tr payloadUpdated := make(map[string]interface{}) payloadUpdated["transition"] = map[string]interface{}{"id": transitionId} - var reader io.Reader - var err error + // Process logic only if the transition options are provided + if options != nil { - if options != nil && options.Fields != nil { + if options.Fields == nil { + return nil, model.ErrNoIssueSchemeError + } - // Executed when customfields and operation are provided - if options.CustomFields != nil && options.Operations != nil { + withCustomFields := options.CustomFields != nil + withOperations := options.Operations != nil - payloadWithCustomFields, err := options.Fields.MergeCustomFields(options.CustomFields) - if err != nil { - return nil, err - } + var err error - payloadWithOperations, err := options.Fields.MergeOperations(options.Operations) - if err != nil { - return nil, err - } - - if err := mergo.Map(&payloadWithCustomFields, &payloadWithOperations, mergo.WithOverride); err != nil { - return nil, err - } - - if err := mergo.Map(&payloadWithCustomFields, &payloadUpdated, mergo.WithOverride); err != nil { - return nil, err - } + // Executed when the customfields and operations are provided + if withCustomFields && withOperations { - reader, err = i.c.TransformStructToReader(&payloadWithCustomFields) + payloadUpdated, err = options.Fields.MergeCustomFields(options.CustomFields) if err != nil { return nil, err } - } - - // Executed when customfields are provided but not the operations - if options.CustomFields != nil && options.Operations == nil { - payloadWithCustomFields, err := options.Fields.MergeCustomFields(options.CustomFields) + payloadWithOperations, err := options.Fields.MergeOperations(options.Operations) if err != nil { return nil, err } - if err := mergo.Map(&payloadWithCustomFields, &payloadUpdated, mergo.WithOverride); err != nil { - return nil, err - } - - reader, err = i.c.TransformStructToReader(&payloadWithCustomFields) - if err != nil { + if err = mergo.Map(&payloadUpdated, &payloadWithOperations, mergo.WithOverride); err != nil { return nil, err } } - // Executed when operations are provided but not the customfields - if options.CustomFields == nil && options.Operations != nil { + // Executed when only the customfields are provided, but not the operations + if withCustomFields && !withOperations { - payloadWithOperations, err := options.Fields.MergeOperations(options.Operations) + payloadUpdated, err = options.Fields.MergeCustomFields(options.CustomFields) if err != nil { return nil, err } + } - if err := mergo.Map(&payloadWithOperations, &payloadUpdated, mergo.WithOverride); err != nil { - return nil, err - } + // Executed when only the operations are provided, but not the customfields + if withOperations && !withCustomFields { - reader, err = i.c.TransformStructToReader(&payloadWithOperations) + payloadUpdated, err = options.Fields.MergeOperations(options.Operations) if err != nil { return nil, err } } - } else { - reader, err = i.c.TransformStructToReader(&payloadUpdated) - if err != nil { - return nil, err - } + } endpoint := fmt.Sprintf("rest/api/%v/issue/%v/transitions", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payloadUpdated) if err != nil { return nil, err } diff --git a/jira/internal/issue_impl_rich_text_test.go b/jira/internal/issue_impl_rich_text_test.go index 89b2a34f..fbb58441 100644 --- a/jira/internal/issue_impl_rich_text_test.go +++ b/jira/internal/issue_impl_rich_text_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalRichTextServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -43,12 +42,13 @@ func Test_internalRichTextServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/DUMMY-1?deleteSubtasks=true", + "", nil). Return(&http.Request{}, nil) @@ -70,7 +70,7 @@ func Test_internalRichTextServiceImpl_Delete(t *testing.T) { deleteSubTasks: true, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -86,12 +86,13 @@ func Test_internalRichTextServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/DUMMY-1?deleteSubtasks=true", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -135,7 +136,7 @@ func Test_internalRichTextServiceImpl_Delete(t *testing.T) { func Test_internalRichTextServiceImpl_Assign(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -153,7 +154,7 @@ func Test_internalRichTextServiceImpl_Assign(t *testing.T) { Err error }{ { - name: "when the api version is v2", + name: "when the api version is v3", fields: fields{version: "2"}, args: args{ ctx: context.TODO(), @@ -162,19 +163,14 @@ func Test_internalRichTextServiceImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "/rest/api/2/issue/DUMMY-1/assignee", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, nil) client.On("Call", @@ -195,7 +191,7 @@ func Test_internalRichTextServiceImpl_Assign(t *testing.T) { accountId: "account-id-sample", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -210,7 +206,7 @@ func Test_internalRichTextServiceImpl_Assign(t *testing.T) { accountId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoAccountIDError, @@ -226,19 +222,14 @@ func Test_internalRichTextServiceImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - AccountID string "json:\"accountId\"" - }{AccountID: "account-id-sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "/rest/api/2/issue/DUMMY-1/assignee", - bytes.NewReader([]byte{})). + "", + map[string]interface{}{"accountId": "account-id-sample"}). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -280,8 +271,17 @@ func Test_internalRichTextServiceImpl_Assign(t *testing.T) { func Test_internalRichTextServiceImpl_Notify(t *testing.T) { + optionsMocked := &model.IssueNotifyOptionsScheme{ + HTMLBody: "The latest test results for this ticket are now available.", + Subject: "SUBJECT EMAIL EXAMPLE", + To: &model.IssueNotifyToScheme{ + Reporter: true, + Assignee: true, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -305,35 +305,18 @@ func Test_internalRichTextServiceImpl_Notify(t *testing.T) { args: args{ ctx: context.TODO(), issueKeyOrId: "DUMMY-1", - options: &model.IssueNotifyOptionsScheme{ - HTMLBody: "The latest test results for this ticket are now available.", - Subject: "SUBJECT EMAIL EXAMPLE", - To: &model.IssueNotifyToScheme{ - Reporter: true, - Assignee: true, - }, - }, + options: optionsMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.IssueNotifyOptionsScheme{ - HTMLBody: "The latest test results for this ticket are now available.", - Subject: "SUBJECT EMAIL EXAMPLE", - To: &model.IssueNotifyToScheme{ - Reporter: true, - Assignee: true, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/notify", - bytes.NewReader([]byte{})). + "", + optionsMocked). Return(&http.Request{}, nil) client.On("Call", @@ -353,7 +336,7 @@ func Test_internalRichTextServiceImpl_Notify(t *testing.T) { issueKeyOrId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -365,35 +348,18 @@ func Test_internalRichTextServiceImpl_Notify(t *testing.T) { args: args{ ctx: context.TODO(), issueKeyOrId: "DUMMY-1", - options: &model.IssueNotifyOptionsScheme{ - HTMLBody: "The latest test results for this ticket are now available.", - Subject: "SUBJECT EMAIL EXAMPLE", - To: &model.IssueNotifyToScheme{ - Reporter: true, - Assignee: true, - }, - }, + options: optionsMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.IssueNotifyOptionsScheme{ - HTMLBody: "The latest test results for this ticket are now available.", - Subject: "SUBJECT EMAIL EXAMPLE", - To: &model.IssueNotifyToScheme{ - Reporter: true, - Assignee: true, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/notify", - bytes.NewReader([]byte{})). + "", + optionsMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -436,7 +402,7 @@ func Test_internalRichTextServiceImpl_Notify(t *testing.T) { func Test_internalRichTextServiceImpl_Transitions(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -462,12 +428,13 @@ func Test_internalRichTextServiceImpl_Transitions(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-1/transitions", + "", nil). Return(&http.Request{}, nil) @@ -488,7 +455,7 @@ func Test_internalRichTextServiceImpl_Transitions(t *testing.T) { issueKeyOrId: "", }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -503,12 +470,13 @@ func Test_internalRichTextServiceImpl_Transitions(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-1/transitions", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -552,21 +520,38 @@ func Test_internalRichTextServiceImpl_Transitions(t *testing.T) { func Test_internalRichTextServiceImpl_Create(t *testing.T) { - customFields := &model.CustomFields{} + payloadMocked := &model.IssueSchemeV2{ + Fields: &model.IssueFieldsSchemeV2{ + Summary: "New summary test", + Project: &model.ProjectScheme{ID: "10000"}, + IssueType: &model.IssueTypeScheme{Name: "Story"}, + }, + } + + customFieldsMocked := &model.CustomFields{} // Add a new custom field - err := customFields.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) + err := customFieldsMocked.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) if err != nil { t.Fatal(err) } - err = customFields.Number("customfield_10042", 1000.2222) + err = customFieldsMocked.Number("customfield_10042", 1000.2222) if err != nil { t.Fatal(err) } + expectedPayloadWithCustomFields := map[string]interface{}{ + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{"name": "jira-administrators"}, map[string]interface{}{"name": "jira-administrators-system"}}, + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -588,34 +573,20 @@ func Test_internalRichTextServiceImpl_Create(t *testing.T) { name: "when the api version is v2", fields: fields{version: "2"}, args: args{ - ctx: context.TODO(), - payload: &model.IssueSchemeV2{ - Fields: &model.IssueFieldsSchemeV2{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - customFields: customFields, + ctx: context.TODO(), + payload: payloadMocked, + customFields: customFieldsMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - map[string]interface{}{ - "fields": map[string]interface{}{"customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFields). Return(&http.Request{}, nil) client.On("Call", @@ -631,33 +602,20 @@ func Test_internalRichTextServiceImpl_Create(t *testing.T) { name: "when the customfield are not provided", fields: fields{version: "2"}, args: args{ - ctx: context.TODO(), - payload: &model.IssueSchemeV2{ - Fields: &model.IssueFieldsSchemeV2{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, + ctx: context.TODO(), + payload: payloadMocked, customFields: nil, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.IssueSchemeV2{Fields: &model.IssueFieldsSchemeV2{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -673,34 +631,20 @@ func Test_internalRichTextServiceImpl_Create(t *testing.T) { name: "when the http request cannot be created", fields: fields{version: "2"}, args: args{ - ctx: context.TODO(), - payload: &model.IssueSchemeV2{ - Fields: &model.IssueFieldsSchemeV2{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - customFields: customFields, + ctx: context.TODO(), + payload: payloadMocked, + customFields: customFieldsMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - map[string]interface{}{ - "fields": map[string]interface{}{"customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFields). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -743,21 +687,67 @@ func Test_internalRichTextServiceImpl_Create(t *testing.T) { func Test_internalRichTextServiceImpl_Creates(t *testing.T) { - customFields := &model.CustomFields{} + customFieldsMocked := &model.CustomFields{} - // Add a new custom field - err := customFields.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) + err := customFieldsMocked.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) if err != nil { t.Fatal(err) } - err = customFields.Number("customfield_10042", 1000.2222) + err = customFieldsMocked.Number("customfield_10042", 1000.2222) if err != nil { t.Fatal(err) } + payloadMocked := []*model.IssueBulkSchemeV2{ + { + Payload: &model.IssueSchemeV2{ + Fields: &model.IssueFieldsSchemeV2{ + Summary: "New summary test", + Project: &model.ProjectScheme{ID: "10000"}, + IssueType: &model.IssueTypeScheme{Name: "Story"}, + }, + }, + CustomFields: customFieldsMocked, + }, + + { + Payload: nil, + CustomFields: nil, + }, + + { + Payload: &model.IssueSchemeV2{ + Fields: &model.IssueFieldsSchemeV2{ + Summary: "New summary test #2", + Project: &model.ProjectScheme{ID: "10000"}, + IssueType: &model.IssueTypeScheme{Name: "Story"}, + }, + }, + CustomFields: customFieldsMocked, + }, + } + + expectedBulkWithCustomFieldsPayload := map[string]interface{}{ + + "issueUpdates": []map[string]interface{}{map[string]interface{}{ + + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{"name": "jira-administrators"}, map[string]interface{}{"name": "jira-administrators-system"}}, + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}}, map[string]interface{}{ + + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{"name": "jira-administrators"}, map[string]interface{}{"name": "jira-administrators-system"}}, + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test #2"}}}} + type fields struct { - c service.Client + c service.Connector version string } @@ -778,62 +768,19 @@ func Test_internalRichTextServiceImpl_Creates(t *testing.T) { name: "when the api version is v2", fields: fields{version: "2"}, args: args{ - ctx: context.TODO(), - payload: []*model.IssueBulkSchemeV2{ - { - Payload: &model.IssueSchemeV2{ - Fields: &model.IssueFieldsSchemeV2{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - CustomFields: customFields, - }, - - { - Payload: nil, - CustomFields: nil, - }, - - { - Payload: &model.IssueSchemeV2{ - Fields: &model.IssueFieldsSchemeV2{ - Summary: "New summary test #2", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - CustomFields: customFields, - }, - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{"issueUpdates": []map[string]interface{}{{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}}, - - {"fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test #2"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/bulk", - bytes.NewReader([]byte{})). + "", + expectedBulkWithCustomFieldsPayload). Return(&http.Request{}, nil) client.On("Call", @@ -853,72 +800,29 @@ func Test_internalRichTextServiceImpl_Creates(t *testing.T) { payload: nil, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, - Err: errors.New("error, please provide a valid []*IssueBulkScheme slice of pointers"), + Err: model.ErrNoCreateIssuesError, }, { name: "when the http request cannot be created", fields: fields{version: "2"}, args: args{ - ctx: context.TODO(), - payload: []*model.IssueBulkSchemeV2{ - { - Payload: &model.IssueSchemeV2{ - Fields: &model.IssueFieldsSchemeV2{ - Summary: "New summary test", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - CustomFields: customFields, - }, - - { - Payload: nil, - CustomFields: nil, - }, - - { - Payload: &model.IssueSchemeV2{ - Fields: &model.IssueFieldsSchemeV2{ - Summary: "New summary test #2", - Project: &model.ProjectScheme{ID: "10000"}, - IssueType: &model.IssueTypeScheme{Name: "Story"}, - }, - }, - CustomFields: customFields, - }, - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{"issueUpdates": []map[string]interface{}{{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}}, - - {"fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test #2"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/bulk", - bytes.NewReader([]byte{})). + "", + expectedBulkWithCustomFieldsPayload). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -975,7 +879,7 @@ func Test_internalRichTextServiceImpl_Get(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1004,12 +908,13 @@ func Test_internalRichTextServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-1?expand=operations%2Cchangelogts&fields=summary%2Cstatus", + "", nil). Return(&http.Request{}, nil) @@ -1032,7 +937,7 @@ func Test_internalRichTextServiceImpl_Get(t *testing.T) { expand: []string{"operations", "changelogts"}, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -1049,12 +954,13 @@ func Test_internalRichTextServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-1?expand=operations%2Cchangelogts&fields=summary%2Cstatus", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -1099,28 +1005,64 @@ func Test_internalRichTextServiceImpl_Get(t *testing.T) { func Test_internalRichTextServiceImpl_Move(t *testing.T) { - customFields := &model.CustomFields{} + customFieldsMocked := &model.CustomFields{} - err := customFields.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) + err := customFieldsMocked.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) if err != nil { t.Fatal(err) } - err = customFields.Number("customfield_10042", 1000.2222) + err = customFieldsMocked.Number("customfield_10042", 1000.2222) if err != nil { t.Fatal(err) } - operations := &model.UpdateOperations{} - err = operations.AddArrayOperation("labels", map[string]string{ + operationsMocked := &model.UpdateOperations{} + err = operationsMocked.AddArrayOperation("labels", map[string]string{ "triaged": "remove", }) - if err != nil { - t.Fatal(err) - } + + expectedPayloadWithCustomFieldsAndOperations := map[string]interface{}{ + + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{ + "name": "jira-administrators"}, map[string]interface{}{ + "name": "jira-administrators-system"}}, + + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}, + + "update": map[string]interface{}{ + "labels": []map[string]interface{}{map[string]interface{}{ + "remove": "triaged"}}}} + + expectedPayloadWithCustomfields := map[string]interface{}{ + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{ + "name": "jira-administrators"}, map[string]interface{}{ + "name": "jira-administrators-system"}}, + + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}} + + expectedPayloadWithOperations := map[string]interface{}{ + "fields": map[string]interface{}{ + "issuetype": map[string]interface{}{"name": "Story"}, + "project": map[string]interface{}{"id": "10000"}, + "summary": "New summary test"}, + + "update": map[string]interface{}{ + "labels": []map[string]interface{}{map[string]interface{}{ + "remove": "triaged"}}}} + + expectedPayloadWithNoOptions := map[string]interface{}{"transition": map[string]interface{}{"id": "10001"}} type fields struct { - c service.Client + c service.Connector version string } @@ -1139,7 +1081,7 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { Err error }{ { - name: "when the api version is v2", + name: "when the api version is v3", fields: fields{version: "2"}, args: args{ ctx: context.TODO(), @@ -1153,32 +1095,20 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, - Operations: operations, + CustomFields: customFieldsMocked, + Operations: operationsMocked, }, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}, - "transition": map[string]interface{}{"id": "10001"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFieldsAndOperations). Return(&http.Request{}, nil) client.On("Call", @@ -1190,6 +1120,22 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { }, }, + { + name: "when the options are provided and the fields are not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.TODO(), + issueKeyOrId: "DUMMY-1", + transitionId: "10001", + options: &model.IssueMoveOptionsV2{ + CustomFields: customFieldsMocked, + Operations: operationsMocked, + }, + }, + wantErr: true, + Err: model.ErrNoIssueSchemeError, + }, + { name: "when the operations are not provided", fields: fields{version: "2"}, @@ -1205,30 +1151,20 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, + CustomFields: customFieldsMocked, Operations: nil, }, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}, - "transition": map[string]interface{}{"id": "10001"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomfields). Return(&http.Request{}, nil) client.On("Call", @@ -1256,29 +1192,19 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { }, }, CustomFields: nil, - Operations: operations, + Operations: operationsMocked, }, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}, - "transition": map[string]interface{}{"id": "10001"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithOperations). Return(&http.Request{}, nil) client.On("Call", @@ -1301,18 +1227,14 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "transition": map[string]interface{}{"id": "10001"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithNoOptions). Return(&http.Request{}, nil) client.On("Call", @@ -1339,12 +1261,12 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, - Operations: operations, + CustomFields: customFieldsMocked, + Operations: operationsMocked, }, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -1365,12 +1287,12 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, - Operations: operations, + CustomFields: customFieldsMocked, + Operations: operationsMocked, }, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoTransitionIDError, @@ -1391,32 +1313,20 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { IssueType: &model.IssueTypeScheme{Name: "Story"}, }, }, - CustomFields: customFields, - Operations: operations, + CustomFields: customFieldsMocked, + Operations: operationsMocked, }, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "issuetype": map[string]interface{}{"name": "Story"}, - "project": map[string]interface{}{"id": "10000"}, - "summary": "New summary test"}, - "transition": map[string]interface{}{"id": "10001"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/transitions", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFieldsAndOperations). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1459,14 +1369,14 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { func Test_internalRichTextServiceImpl_Update(t *testing.T) { - customFields := &model.CustomFields{} + customFieldsMocked := &model.CustomFields{} - err := customFields.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) + err := customFieldsMocked.Groups("customfield_10052", []string{"jira-administrators", "jira-administrators-system"}) if err != nil { t.Fatal(err) } - err = customFields.Number("customfield_10042", 1000.2222) + err = customFieldsMocked.Number("customfield_10042", 1000.2222) if err != nil { t.Fatal(err) } @@ -1479,8 +1389,32 @@ func Test_internalRichTextServiceImpl_Update(t *testing.T) { t.Fatal(err) } + expectedPayloadWithCustomFieldsAndOperations := map[string]interface{}{ + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{ + "name": "jira-administrators"}, map[string]interface{}{ + "name": "jira-administrators-system"}}, "summary": "New summary test"}, + "update": map[string]interface{}{ + "labels": []map[string]interface{}{map[string]interface{}{ + "remove": "triaged"}}}} + + expectedPayloadWithCustomfields := map[string]interface{}{ + "fields": map[string]interface{}{ + "customfield_10042": 1000.2222, + "customfield_10052": []map[string]interface{}{map[string]interface{}{ + "name": "jira-administrators"}, map[string]interface{}{ + "name": "jira-administrators-system"}}, + "summary": "New summary test"}} + + expectedPayloadWithOperations := map[string]interface{}{ + "fields": map[string]interface{}{ + "summary": "New summary test"}, + "update": map[string]interface{}{ + "labels": []map[string]interface{}{map[string]interface{}{"remove": "triaged"}}}} + type fields struct { - c service.Client + c service.Connector version string } @@ -1502,7 +1436,7 @@ func Test_internalRichTextServiceImpl_Update(t *testing.T) { Err error }{ { - name: "when the api version is v2", + name: "when the api version is v3", fields: fields{version: "2"}, args: args{ ctx: context.TODO(), @@ -1513,28 +1447,19 @@ func Test_internalRichTextServiceImpl_Update(t *testing.T) { Summary: "New summary test", }, }, - customFields: customFields, + customFields: customFieldsMocked, operations: operations, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "summary": "New summary test"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFieldsAndOperations). Return(&http.Request{}, nil) client.On("Call", @@ -1558,11 +1483,11 @@ func Test_internalRichTextServiceImpl_Update(t *testing.T) { Summary: "New summary test", }, }, - customFields: customFields, + customFields: customFieldsMocked, operations: operations, }, on: func(fields *fields) { - fields.c = mocks.NewClient(t) + fields.c = mocks.NewConnector(t) }, wantErr: true, Err: model.ErrNoIssueKeyOrIDError, @@ -1580,28 +1505,19 @@ func Test_internalRichTextServiceImpl_Update(t *testing.T) { Summary: "New summary test", }, }, - customFields: customFields, + customFields: customFieldsMocked, operations: operations, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "summary": "New summary test"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomFieldsAndOperations). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1622,26 +1538,19 @@ func Test_internalRichTextServiceImpl_Update(t *testing.T) { Summary: "New summary test", }, }, - customFields: customFields, + customFields: customFieldsMocked, operations: nil, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "customfield_10042": 1000.2222, - "customfield_10052": []map[string]interface{}{{"name": "jira-administrators"}, {"name": "jira-administrators-system"}}, - "summary": "New summary test"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithCustomfields). Return(&http.Request{}, nil) client.On("Call", @@ -1670,21 +1579,14 @@ func Test_internalRichTextServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &map[string]interface{}{ - "fields": map[string]interface{}{ - "summary": "New summary test"}, - "update": map[string]interface{}{ - "labels": []map[string]interface{}{{"remove": "triaged"}}}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). + "", + expectedPayloadWithOperations). Return(&http.Request{}, nil) client.On("Call", @@ -1713,21 +1615,18 @@ func Test_internalRichTextServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) - client.On("TransformStructToReader", + client.On("NewRequest", + context.Background(), + http.MethodPut, + "rest/api/2/issue/DUMMY-1?notifyUsers=true", + "", &model.IssueSchemeV2{ Fields: &model.IssueFieldsSchemeV2{ Summary: "New summary test", }, }). - Return(bytes.NewReader([]byte{}), nil) - - client.On("NewRequest", - context.Background(), - http.MethodPut, - "rest/api/2/issue/DUMMY-1?notifyUsers=true", - bytes.NewReader([]byte{})). Return(&http.Request{}, nil) client.On("Call", diff --git a/jira/internal/issue_impl_test.go b/jira/internal/issue_impl_test.go index b9533673..c555f87f 100644 --- a/jira/internal/issue_impl_test.go +++ b/jira/internal/issue_impl_test.go @@ -10,7 +10,7 @@ import ( func TestNewIssueService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } diff --git a/pkg/infra/models/errors.go b/pkg/infra/models/errors.go index fdef5170..f5524871 100644 --- a/pkg/infra/models/errors.go +++ b/pkg/infra/models/errors.go @@ -71,7 +71,6 @@ var ( ErrNoIssueKeyOrIDError = errors.New("jira: no issue key/id set") ErrNoRemoteLinkIDError = errors.New("jira: no remote link id set") ErrNoRemoteLinkGlobalIDError = errors.New("jira: no global remote link id set") - ErrNoIssueSchemeError = errors.New("jira: no jira.IssueScheme set") ErrNoTransitionIDError = errors.New("jira: no transition id set") ErrNoAttachmentIDError = errors.New("jira: no attachment id set") ErrNoAttachmentNameError = errors.New("jira: no attachment filename set") @@ -156,4 +155,7 @@ var ( ErrNoAttachmentIdsError = errors.New("sm: no attachment id's set") ErrNoLabelsError = errors.New("sm: no label names set") ErrNoComponentsError = errors.New("sm: no components set") + + ErrNoCreateIssuesError = errors.New("jira: no issues payload set") + ErrNoIssueSchemeError = errors.New("jira: no issue instance set") ) From f8069487dca2e14f9e3b8fa11d1d5bf8a510bd34 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Tue, 4 Jul 2023 18:43:33 -0600 Subject: [PATCH 10/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Comment service. --- ...{issue_comment_impl.go => comment_impl.go} | 2 +- jira/internal/comment_impl_adf.go | 61 ++++++++--------- jira/internal/comment_impl_adf_test.go | 68 ++++++------------- jira/internal/comment_impl_rich_text.go | 45 ++++++------ jira/internal/comment_impl_rich_text_test.go | 68 ++++++------------- service/jira/comment.go | 4 +- 6 files changed, 95 insertions(+), 153 deletions(-) rename jira/internal/{issue_comment_impl.go => comment_impl.go} (81%) diff --git a/jira/internal/issue_comment_impl.go b/jira/internal/comment_impl.go similarity index 81% rename from jira/internal/issue_comment_impl.go rename to jira/internal/comment_impl.go index c9060256..54745e68 100644 --- a/jira/internal/issue_comment_impl.go +++ b/jira/internal/comment_impl.go @@ -5,7 +5,7 @@ import ( "github.com/ctreminiom/go-atlassian/service" ) -func NewCommentService(client service.Client, version string) (*CommentADFService, *CommentRichTextService, error) { +func NewCommentService(client service.Connector, version string) (*CommentADFService, *CommentRichTextService, error) { if version == "" { return nil, nil, model.ErrNoVersionProvided diff --git a/jira/internal/comment_impl_adf.go b/jira/internal/comment_impl_adf.go index cca0c866..bb377e39 100644 --- a/jira/internal/comment_impl_adf.go +++ b/jira/internal/comment_impl_adf.go @@ -21,8 +21,8 @@ type CommentADFService struct { // DELETE /rest/api/{2-3}/issue/{issueIdOrKey}/comment/{id} // // https://docs.go-atlassian.io/jira-software-cloud/issues/comments#delete-comment -func (c *CommentADFService) Delete(ctx context.Context, issueKeyOrId, commentId string) (*model.ResponseScheme, error) { - return c.internalClient.Delete(ctx, issueKeyOrId, commentId) +func (c *CommentADFService) Delete(ctx context.Context, issueKeyOrID, commentID string) (*model.ResponseScheme, error) { + return c.internalClient.Delete(ctx, issueKeyOrID, commentID) } // Gets returns all comments for an issue. @@ -30,17 +30,17 @@ func (c *CommentADFService) Delete(ctx context.Context, issueKeyOrId, commentId // GET /rest/api/{2-3}/issue/{issueIdOrKey}/comment // // https://docs.go-atlassian.io/jira-software-cloud/issues/comments#get-comments -func (c *CommentADFService) Gets(ctx context.Context, issueKeyOrId, orderBy string, expand []string, startAt, maxResults int) (*model.IssueCommentPageScheme, *model.ResponseScheme, error) { - return c.internalClient.Gets(ctx, issueKeyOrId, orderBy, expand, startAt, maxResults) +func (c *CommentADFService) Gets(ctx context.Context, issueKeyOrID, orderBy string, expand []string, startAt, maxResults int) (*model.IssueCommentPageScheme, *model.ResponseScheme, error) { + return c.internalClient.Gets(ctx, issueKeyOrID, orderBy, expand, startAt, maxResults) } // Get returns a comment. // // GET /rest/api/{2-3}/issue/{issueIdOrKey}/comment/{id} // -// TODO: The documentation needs to be created, raise a ticket here: https://github.com/ctreminiom/go-atlassian/issues -func (c *CommentADFService) Get(ctx context.Context, issueKeyOrId, commentId string) (*model.IssueCommentScheme, *model.ResponseScheme, error) { - return c.internalClient.Get(ctx, issueKeyOrId, commentId) +// https://docs.go-atlassian.io/jira-software-cloud/issues/comments#get-comment +func (c *CommentADFService) Get(ctx context.Context, issueKeyOrID, commentID string) (*model.IssueCommentScheme, *model.ResponseScheme, error) { + return c.internalClient.Get(ctx, issueKeyOrID, commentID) } // Add adds a comment to an issue. @@ -48,28 +48,28 @@ func (c *CommentADFService) Get(ctx context.Context, issueKeyOrId, commentId str // POST /rest/api/{2-3}/issue/{issueIdOrKey}/comment // // https://docs.go-atlassian.io/jira-software-cloud/issues/comments#add-comment -func (c *CommentADFService) Add(ctx context.Context, issueKeyOrId string, payload *model.CommentPayloadScheme, expand []string) (*model.IssueCommentScheme, *model.ResponseScheme, error) { - return c.internalClient.Add(ctx, issueKeyOrId, payload, expand) +func (c *CommentADFService) Add(ctx context.Context, issueKeyOrID string, payload *model.CommentPayloadScheme, expand []string) (*model.IssueCommentScheme, *model.ResponseScheme, error) { + return c.internalClient.Add(ctx, issueKeyOrID, payload, expand) } type internalAdfCommentImpl struct { - c service.Client + c service.Connector version string } -func (i *internalAdfCommentImpl) Delete(ctx context.Context, issueKeyOrId, commentId string) (*model.ResponseScheme, error) { +func (i *internalAdfCommentImpl) Delete(ctx context.Context, issueKeyOrID, commentID string) (*model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, model.ErrNoIssueKeyOrIDError } - if commentId == "" { + if commentID == "" { return nil, model.ErrNoCommentIDError } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment/%v", i.version, issueKeyOrId, commentId) + endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment/%v", i.version, issueKeyOrID, commentID) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -77,9 +77,9 @@ func (i *internalAdfCommentImpl) Delete(ctx context.Context, issueKeyOrId, comme return i.c.Call(request, nil) } -func (i *internalAdfCommentImpl) Gets(ctx context.Context, issueKeyOrId, orderBy string, expand []string, startAt, maxResults int) (*model.IssueCommentPageScheme, *model.ResponseScheme, error) { +func (i *internalAdfCommentImpl) Gets(ctx context.Context, issueKeyOrID, orderBy string, expand []string, startAt, maxResults int) (*model.IssueCommentPageScheme, *model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, nil, model.ErrNoIssueKeyOrIDError } @@ -95,9 +95,9 @@ func (i *internalAdfCommentImpl) Gets(ctx context.Context, issueKeyOrId, orderBy params.Add("orderBy", orderBy) } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment?%v", i.version, issueKeyOrId, params.Encode()) + endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment?%v", i.version, issueKeyOrID, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -111,19 +111,19 @@ func (i *internalAdfCommentImpl) Gets(ctx context.Context, issueKeyOrId, orderBy return comments, response, nil } -func (i *internalAdfCommentImpl) Get(ctx context.Context, issueKeyOrId, commentId string) (*model.IssueCommentScheme, *model.ResponseScheme, error) { +func (i *internalAdfCommentImpl) Get(ctx context.Context, issueKeyOrID, commentID string) (*model.IssueCommentScheme, *model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, nil, model.ErrNoIssueKeyOrIDError } - if commentId == "" { + if commentID == "" { return nil, nil, model.ErrNoCommentIDError } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment/%v", i.version, issueKeyOrId, commentId) + endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment/%v", i.version, issueKeyOrID, commentID) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -137,9 +137,9 @@ func (i *internalAdfCommentImpl) Get(ctx context.Context, issueKeyOrId, commentI return comment, response, nil } -func (i *internalAdfCommentImpl) Add(ctx context.Context, issueKeyOrId string, payload *model.CommentPayloadScheme, expand []string) (*model.IssueCommentScheme, *model.ResponseScheme, error) { +func (i *internalAdfCommentImpl) Add(ctx context.Context, issueKeyOrID string, payload *model.CommentPayloadScheme, expand []string) (*model.IssueCommentScheme, *model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, nil, model.ErrNoIssueKeyOrIDError } @@ -149,18 +149,13 @@ func (i *internalAdfCommentImpl) Add(ctx context.Context, issueKeyOrId string, p } var endpoint strings.Builder - endpoint.WriteString(fmt.Sprintf("rest/api/%v/issue/%v/comment", i.version, issueKeyOrId)) + endpoint.WriteString(fmt.Sprintf("rest/api/%v/issue/%v/comment", i.version, issueKeyOrID)) if params.Encode() != "" { endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/comment_impl_adf_test.go b/jira/internal/comment_impl_adf_test.go index 7fa996a7..8523b5b9 100644 --- a/jira/internal/comment_impl_adf_test.go +++ b/jira/internal/comment_impl_adf_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalAdfCommentImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -49,12 +48,13 @@ func Test_internalAdfCommentImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-1/comment?expand=renderedBody&maxResults=50&orderBy=id&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -98,12 +98,13 @@ func Test_internalAdfCommentImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-1/comment?expand=renderedBody&maxResults=50&orderBy=id&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -150,7 +151,7 @@ func Test_internalAdfCommentImpl_Gets(t *testing.T) { func Test_internalAdfCommentImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -177,12 +178,13 @@ func Test_internalAdfCommentImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-1/comment/10001", + "", nil). Return(&http.Request{}, nil) @@ -230,12 +232,13 @@ func Test_internalAdfCommentImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-1/comment/10001", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -281,7 +284,7 @@ func Test_internalAdfCommentImpl_Get(t *testing.T) { func Test_internalAdfCommentImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -308,12 +311,13 @@ func Test_internalAdfCommentImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-1/comment/10001", + "", nil). Return(&http.Request{}, nil) @@ -361,12 +365,13 @@ func Test_internalAdfCommentImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-1/comment/10001", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -450,7 +455,7 @@ func Test_internalAdfCommentImpl_Add(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -480,17 +485,14 @@ func Test_internalAdfCommentImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/comment?expand=body", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -518,29 +520,6 @@ func Test_internalAdfCommentImpl_Add(t *testing.T) { Err: model.ErrNoIssueKeyOrIDError, }, - { - name: "when the payload is not provided", - fields: fields{version: "3"}, - args: args{ - ctx: context.TODO(), - issueKeyOrId: "DUMMY-1", - payload: nil, - expand: []string{"body"}, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.CommentPayloadScheme)(nil)). - Return(bytes.NewReader([]byte{}), model.ErrNilPayloadError) - fields.c = client - - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -552,17 +531,14 @@ func Test_internalAdfCommentImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-1/comment?expand=body", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client diff --git a/jira/internal/comment_impl_rich_text.go b/jira/internal/comment_impl_rich_text.go index 09a37fb1..cfaba746 100644 --- a/jira/internal/comment_impl_rich_text.go +++ b/jira/internal/comment_impl_rich_text.go @@ -47,29 +47,29 @@ func (c *CommentRichTextService) Get(ctx context.Context, issueKeyOrId, commentI // // POST /rest/api/{2-3}/issue/{issueIdOrKey}/comment // -//https://docs.go-atlassian.io/jira-software-cloud/issues/comments#add-comment +// https://docs.go-atlassian.io/jira-software-cloud/issues/comments#add-comment func (c *CommentRichTextService) Add(ctx context.Context, issueKeyOrId string, payload *model.CommentPayloadSchemeV2, expand []string) (*model.IssueCommentSchemeV2, *model.ResponseScheme, error) { return c.internalClient.Add(ctx, issueKeyOrId, payload, expand) } type internalRichTextCommentImpl struct { - c service.Client + c service.Connector version string } -func (i *internalRichTextCommentImpl) Delete(ctx context.Context, issueKeyOrId, commentId string) (*model.ResponseScheme, error) { +func (i *internalRichTextCommentImpl) Delete(ctx context.Context, issueKeyOrID, commentID string) (*model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, model.ErrNoIssueKeyOrIDError } - if commentId == "" { + if commentID == "" { return nil, model.ErrNoCommentIDError } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment/%v", i.version, issueKeyOrId, commentId) + endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment/%v", i.version, issueKeyOrID, commentID) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -77,9 +77,9 @@ func (i *internalRichTextCommentImpl) Delete(ctx context.Context, issueKeyOrId, return i.c.Call(request, nil) } -func (i *internalRichTextCommentImpl) Gets(ctx context.Context, issueKeyOrId, orderBy string, expand []string, startAt, maxResults int) (*model.IssueCommentPageSchemeV2, *model.ResponseScheme, error) { +func (i *internalRichTextCommentImpl) Gets(ctx context.Context, issueKeyOrID, orderBy string, expand []string, startAt, maxResults int) (*model.IssueCommentPageSchemeV2, *model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, nil, model.ErrNoIssueKeyOrIDError } @@ -95,9 +95,9 @@ func (i *internalRichTextCommentImpl) Gets(ctx context.Context, issueKeyOrId, or params.Add("orderBy", orderBy) } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment?%v", i.version, issueKeyOrId, params.Encode()) + endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment?%v", i.version, issueKeyOrID, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -111,19 +111,19 @@ func (i *internalRichTextCommentImpl) Gets(ctx context.Context, issueKeyOrId, or return comments, response, nil } -func (i *internalRichTextCommentImpl) Get(ctx context.Context, issueKeyOrId, commentId string) (*model.IssueCommentSchemeV2, *model.ResponseScheme, error) { +func (i *internalRichTextCommentImpl) Get(ctx context.Context, issueKeyOrID, commentID string) (*model.IssueCommentSchemeV2, *model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, nil, model.ErrNoIssueKeyOrIDError } - if commentId == "" { + if commentID == "" { return nil, nil, model.ErrNoCommentIDError } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment/%v", i.version, issueKeyOrId, commentId) + endpoint := fmt.Sprintf("rest/api/%v/issue/%v/comment/%v", i.version, issueKeyOrID, commentID) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -137,9 +137,9 @@ func (i *internalRichTextCommentImpl) Get(ctx context.Context, issueKeyOrId, com return comment, response, nil } -func (i *internalRichTextCommentImpl) Add(ctx context.Context, issueKeyOrId string, payload *model.CommentPayloadSchemeV2, expand []string) (*model.IssueCommentSchemeV2, *model.ResponseScheme, error) { +func (i *internalRichTextCommentImpl) Add(ctx context.Context, issueKeyOrID string, payload *model.CommentPayloadSchemeV2, expand []string) (*model.IssueCommentSchemeV2, *model.ResponseScheme, error) { - if issueKeyOrId == "" { + if issueKeyOrID == "" { return nil, nil, model.ErrNoIssueKeyOrIDError } @@ -149,18 +149,13 @@ func (i *internalRichTextCommentImpl) Add(ctx context.Context, issueKeyOrId stri } var endpoint strings.Builder - endpoint.WriteString(fmt.Sprintf("rest/api/%v/issue/%v/comment", i.version, issueKeyOrId)) + endpoint.WriteString(fmt.Sprintf("rest/api/%v/issue/%v/comment", i.version, issueKeyOrID)) if params.Encode() != "" { endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/comment_impl_rich_text_test.go b/jira/internal/comment_impl_rich_text_test.go index dbd45187..26345b42 100644 --- a/jira/internal/comment_impl_rich_text_test.go +++ b/jira/internal/comment_impl_rich_text_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalRichTextCommentImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -49,12 +48,13 @@ func Test_internalRichTextCommentImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-1/comment?expand=renderedBody&maxResults=50&orderBy=id&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -98,12 +98,13 @@ func Test_internalRichTextCommentImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-1/comment?expand=renderedBody&maxResults=50&orderBy=id&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -150,7 +151,7 @@ func Test_internalRichTextCommentImpl_Gets(t *testing.T) { func Test_internalRichTextCommentImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -177,12 +178,13 @@ func Test_internalRichTextCommentImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-1/comment/10001", + "", nil). Return(&http.Request{}, nil) @@ -230,12 +232,13 @@ func Test_internalRichTextCommentImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-1/comment/10001", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -281,7 +284,7 @@ func Test_internalRichTextCommentImpl_Get(t *testing.T) { func Test_internalRichTextCommentImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -308,12 +311,13 @@ func Test_internalRichTextCommentImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/DUMMY-1/comment/10001", + "", nil). Return(&http.Request{}, nil) @@ -361,12 +365,13 @@ func Test_internalRichTextCommentImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/DUMMY-1/comment/10001", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -419,7 +424,7 @@ func Test_internalRichTextCommentImpl_Add(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -449,17 +454,14 @@ func Test_internalRichTextCommentImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/comment?expand=body", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -487,29 +489,6 @@ func Test_internalRichTextCommentImpl_Add(t *testing.T) { Err: model.ErrNoIssueKeyOrIDError, }, - { - name: "when the payload is not provided", - fields: fields{version: "2"}, - args: args{ - ctx: context.TODO(), - issueKeyOrId: "DUMMY-1", - payload: nil, - expand: []string{"body"}, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.CommentPayloadSchemeV2)(nil)). - Return(bytes.NewReader([]byte{}), model.ErrNilPayloadError) - fields.c = client - - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "2"}, @@ -521,17 +500,14 @@ func Test_internalRichTextCommentImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-1/comment?expand=body", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client diff --git a/service/jira/comment.go b/service/jira/comment.go index ef4271de..f3d9fbcb 100644 --- a/service/jira/comment.go +++ b/service/jira/comment.go @@ -19,7 +19,7 @@ type CommentRichTextConnector interface { // // GET /rest/api/{2-3}/issue/{issueIdOrKey}/comment/{id} // - // TODO: The documentation needs to be created, raise a ticket here: https://github.com/ctreminiom/go-atlassian/issues + // https://docs.go-atlassian.io/jira-software-cloud/issues/comments#get-comment Get(ctx context.Context, issueKeyOrId, commentId string) (*model.IssueCommentSchemeV2, *model.ResponseScheme, error) // Add adds a comment to an issue. @@ -44,7 +44,7 @@ type CommentADFConnector interface { // // GET /rest/api/{2-3}/issue/{issueIdOrKey}/comment/{id} // - // TODO: The documentation needs to be created, raise a ticket here: https://github.com/ctreminiom/go-atlassian/issues + // https://docs.go-atlassian.io/jira-software-cloud/issues/comments#get-comment Get(ctx context.Context, issueKeyOrId, commentId string) (*model.IssueCommentScheme, *model.ResponseScheme, error) // Add adds a comment to an issue. From c4eeb403be4e177ec4ce956603327e268576a8a8 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Wed, 5 Jul 2023 01:05:09 -0600 Subject: [PATCH 11/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Field service. --- jira/internal/field_impl.go | 17 ++-- jira/internal/field_impl_test.go | 139 +++++++++++-------------------- 2 files changed, 53 insertions(+), 103 deletions(-) diff --git a/jira/internal/field_impl.go b/jira/internal/field_impl.go index 35119125..73237a7d 100644 --- a/jira/internal/field_impl.go +++ b/jira/internal/field_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewIssueFieldService(client service.Client, version string, configuration *IssueFieldConfigService, context *IssueFieldContextService, +func NewIssueFieldService(client service.Connector, version string, configuration *IssueFieldConfigService, context *IssueFieldContextService, trash *IssueFieldTrashService) (*IssueFieldService, error) { if version == "" { @@ -82,7 +82,7 @@ func (i *IssueFieldService) Delete(ctx context.Context, fieldId string) (*model. } type internalIssueFieldServiceImpl struct { - c service.Client + c service.Connector version string } @@ -90,7 +90,7 @@ func (i *internalIssueFieldServiceImpl) Gets(ctx context.Context) ([]*model.Issu endpoint := fmt.Sprintf("rest/api/%v/field", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -106,14 +106,9 @@ func (i *internalIssueFieldServiceImpl) Gets(ctx context.Context) ([]*model.Issu func (i *internalIssueFieldServiceImpl) Create(ctx context.Context, payload *model.CustomFieldScheme) (*model.IssueFieldScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/field", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -158,7 +153,7 @@ func (i *internalIssueFieldServiceImpl) Search(ctx context.Context, options *mod endpoint := fmt.Sprintf("rest/api/%v/field/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -180,7 +175,7 @@ func (i *internalIssueFieldServiceImpl) Delete(ctx context.Context, fieldId stri endpoint := fmt.Sprintf("rest/api/%v/field/%v", i.version, fieldId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/field_impl_test.go b/jira/internal/field_impl_test.go index 52a429df..3e44d473 100644 --- a/jira/internal/field_impl_test.go +++ b/jira/internal/field_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalIssueFieldServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,12 +39,13 @@ func Test_internalIssueFieldServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field", + "", nil). Return(&http.Request{}, nil) @@ -69,12 +69,13 @@ func Test_internalIssueFieldServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field", + "", nil). Return(&http.Request{}, nil) @@ -98,12 +99,13 @@ func Test_internalIssueFieldServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -148,8 +150,15 @@ func Test_internalIssueFieldServiceImpl_Gets(t *testing.T) { func Test_internalIssueFieldServiceImpl_Create(t *testing.T) { + payloadMocked := &model.CustomFieldScheme{ + Name: "Alliance", + Description: "this is the alliance description field", + FieldType: "cascadingselect", + SearcherKey: "cascadingselectsearcher", + } + type fields struct { - c service.Client + c service.Connector version string } @@ -170,32 +179,19 @@ func Test_internalIssueFieldServiceImpl_Create(t *testing.T) { name: "when the api version is v3", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - payload: &model.CustomFieldScheme{ - Name: "Alliance", - Description: "this is the alliance description field", - FieldType: "cascadingselect", - SearcherKey: "cascadingselectsearcher", - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.CustomFieldScheme{ - Name: "Alliance", - Description: "this is the alliance description field", - FieldType: "cascadingselect", - SearcherKey: "cascadingselectsearcher", - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -214,32 +210,19 @@ func Test_internalIssueFieldServiceImpl_Create(t *testing.T) { name: "when the api version is v2", fields: fields{version: "2"}, args: args{ - ctx: context.TODO(), - payload: &model.CustomFieldScheme{ - Name: "Alliance", - Description: "this is the alliance description field", - FieldType: "cascadingselect", - SearcherKey: "cascadingselectsearcher", - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.CustomFieldScheme{ - Name: "Alliance", - Description: "this is the alliance description field", - FieldType: "cascadingselect", - SearcherKey: "cascadingselectsearcher", - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -254,58 +237,23 @@ func Test_internalIssueFieldServiceImpl_Create(t *testing.T) { Err: nil, }, - { - name: "when the payload is not provided", - fields: fields{version: "3"}, - args: args{ - ctx: context.TODO(), - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.CustomFieldScheme)(nil)). - Return(bytes.NewReader([]byte{}), model.ErrNilPayloadError) - - fields.c = client - - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - payload: &model.CustomFieldScheme{ - Name: "Alliance", - Description: "this is the alliance description field", - FieldType: "cascadingselect", - SearcherKey: "cascadingselectsearcher", - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.CustomFieldScheme{ - Name: "Alliance", - Description: "this is the alliance description field", - FieldType: "cascadingselect", - SearcherKey: "cascadingselectsearcher", - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -349,7 +297,7 @@ func Test_internalIssueFieldServiceImpl_Create(t *testing.T) { func Test_internalIssueFieldServiceImpl_Search(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -384,12 +332,13 @@ func Test_internalIssueFieldServiceImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/search?expand=screensCount%2ClastUsed&id=111%2C12222&maxResults=50&orderBy=lastUsed&query=query-sample&startAt=0&type=custom", + "", nil). Return(&http.Request{}, nil) @@ -422,12 +371,13 @@ func Test_internalIssueFieldServiceImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/search?expand=screensCount%2ClastUsed&id=111%2C12222&maxResults=50&orderBy=lastUsed&query=query-sample&startAt=0&type=custom", + "", nil). Return(&http.Request{}, nil) @@ -460,12 +410,13 @@ func Test_internalIssueFieldServiceImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/search?expand=screensCount%2ClastUsed&id=111%2C12222&maxResults=50&orderBy=lastUsed&query=query-sample&startAt=0&type=custom", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -484,12 +435,13 @@ func Test_internalIssueFieldServiceImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/search?maxResults=0&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -536,7 +488,7 @@ func Test_internalIssueFieldServiceImpl_Search(t *testing.T) { func Test_internalIssueFieldServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -562,12 +514,13 @@ func Test_internalIssueFieldServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/field/10005", + "", nil). Return(&http.Request{}, nil) @@ -592,12 +545,13 @@ func Test_internalIssueFieldServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/field/10005", + "", nil). Return(&http.Request{}, nil) @@ -633,12 +587,13 @@ func Test_internalIssueFieldServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/field/10005", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -684,7 +639,7 @@ func Test_internalIssueFieldServiceImpl_Delete(t *testing.T) { func Test_NewIssueFieldService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From a61d8266a38fb3007c2e835330d374d1d233a751 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Wed, 5 Jul 2023 01:16:31 -0600 Subject: [PATCH 12/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Field Configuration service. --- jira/internal/field_configuration_impl.go | 42 ++-- .../internal/field_configuration_impl_test.go | 188 +++++++++++------- 2 files changed, 126 insertions(+), 104 deletions(-) diff --git a/jira/internal/field_configuration_impl.go b/jira/internal/field_configuration_impl.go index 7e6b308a..f061bb5a 100644 --- a/jira/internal/field_configuration_impl.go +++ b/jira/internal/field_configuration_impl.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewIssueFieldConfigurationService(client service.Client, version string, item *IssueFieldConfigItemService, +func NewIssueFieldConfigurationService(client service.Connector, version string, item *IssueFieldConfigItemService, scheme *IssueFieldConfigSchemeService) (*IssueFieldConfigService, error) { if version == "" { @@ -75,7 +75,7 @@ func (i *IssueFieldConfigService) Delete(ctx context.Context, id int) (*model.Re } type internalIssueFieldConfigServiceImpl struct { - c service.Client + c service.Connector version string } @@ -92,7 +92,7 @@ func (i *internalIssueFieldConfigServiceImpl) Gets(ctx context.Context, ids []in endpoint := fmt.Sprintf("rest/api/%v/fieldconfiguration?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -112,22 +112,15 @@ func (i *internalIssueFieldConfigServiceImpl) Create(ctx context.Context, name, return nil, nil, model.ErrNoFieldConfigurationNameError } - payload := struct { - Name string `json:"name"` - Description string `json:"description,omitempty"` - }{ - Name: name, - Description: description, - } + endpoint := fmt.Sprintf("rest/api/%v/fieldconfiguration", i.version) - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } + payload := map[string]interface{}{"name": name} - endpoint := fmt.Sprintf("rest/api/%v/fieldconfiguration", i.version) + if description != "" { + payload["description"] = description + } - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -151,22 +144,15 @@ func (i *internalIssueFieldConfigServiceImpl) Update(ctx context.Context, id int return nil, model.ErrNoFieldConfigurationNameError } - payload := struct { - Name string `json:"name"` - Description string `json:"description,omitempty"` - }{ - Name: name, - Description: description, - } + payload := map[string]interface{}{"name": name} - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + if description != "" { + payload["description"] = description } endpoint := fmt.Sprintf("rest/api/%v/fieldconfiguration/%v", i.version, id) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -182,7 +168,7 @@ func (i *internalIssueFieldConfigServiceImpl) Delete(ctx context.Context, id int endpoint := fmt.Sprintf("rest/api/%v/fieldconfiguration/%v", i.version, id) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/field_configuration_impl_test.go b/jira/internal/field_configuration_impl_test.go index 36f5103b..3ce892a3 100644 --- a/jira/internal/field_configuration_impl_test.go +++ b/jira/internal/field_configuration_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalIssueFieldConfigServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -46,12 +45,13 @@ func Test_internalIssueFieldConfigServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfiguration?id=10000&id=100001&isDefault=false&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -79,12 +79,13 @@ func Test_internalIssueFieldConfigServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/fieldconfiguration?id=10000&id=100001&isDefault=false&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -112,12 +113,13 @@ func Test_internalIssueFieldConfigServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfiguration?id=10000&id=100001&isDefault=false&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http created")) @@ -163,8 +165,15 @@ func Test_internalIssueFieldConfigServiceImpl_Gets(t *testing.T) { func Test_internalIssueFieldConfigServiceImpl_Create(t *testing.T) { + payloadWithDescriptionMocked := map[string]interface{}{ + "description": "description sample", + "name": "DUMMY Field Configuration Scheme", + } + + payloadMocked := map[string]interface{}{"name": "DUMMY Field Configuration Scheme"} + type fields struct { - c service.Client + c service.Connector version string } @@ -191,22 +200,14 @@ func Test_internalIssueFieldConfigServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY Field Configuration Scheme", - Description: "description sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/fieldconfiguration", - bytes.NewReader([]byte{})). + "", + payloadWithDescriptionMocked). Return(&http.Request{}, nil) client.On("Call", @@ -231,22 +232,46 @@ func Test_internalIssueFieldConfigServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) + + client.On("NewRequest", + context.Background(), + http.MethodPost, + "rest/api/2/fieldconfiguration", + "", + payloadWithDescriptionMocked). + Return(&http.Request{}, nil) + + client.On("Call", + &http.Request{}, + &model.FieldConfigurationScheme{}). + Return(&model.ResponseScheme{}, nil) + + fields.c = client + + }, + wantErr: false, + Err: nil, + }, + + { + name: "when the description is not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.TODO(), + name: "DUMMY Field Configuration Scheme", + description: "", + }, + on: func(fields *fields) { - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY Field Configuration Scheme", - Description: "description sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/fieldconfiguration", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -283,22 +308,14 @@ func Test_internalIssueFieldConfigServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY Field Configuration Scheme", - Description: "description sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/fieldconfiguration", - bytes.NewReader([]byte{})). + "", + payloadWithDescriptionMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -342,8 +359,15 @@ func Test_internalIssueFieldConfigServiceImpl_Create(t *testing.T) { func Test_internalIssueFieldConfigServiceImpl_Update(t *testing.T) { + payloadWithDescriptionMocked := map[string]interface{}{ + "description": "description sample", + "name": "DUMMY Field Configuration Scheme", + } + + payloadMocked := map[string]interface{}{"name": "DUMMY Field Configuration Scheme"} + type fields struct { - c service.Client + c service.Connector version string } @@ -372,22 +396,14 @@ func Test_internalIssueFieldConfigServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY Field Configuration Scheme", - Description: "description sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/fieldconfiguration/1001", - bytes.NewReader([]byte{})). + "", + payloadWithDescriptionMocked). Return(&http.Request{}, nil) client.On("Call", @@ -413,22 +429,47 @@ func Test_internalIssueFieldConfigServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) + + client.On("NewRequest", + context.Background(), + http.MethodPut, + "rest/api/2/fieldconfiguration/1001", + "", + payloadWithDescriptionMocked). + Return(&http.Request{}, nil) + + client.On("Call", + &http.Request{}, + nil). + Return(&model.ResponseScheme{}, nil) - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY Field Configuration Scheme", - Description: "description sample"}). - Return(bytes.NewReader([]byte{}), nil) + fields.c = client + + }, + wantErr: false, + Err: nil, + }, + + { + name: "when the description is not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.TODO(), + id: 1001, + name: "DUMMY Field Configuration Scheme", + description: "", + }, + on: func(fields *fields) { + + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/fieldconfiguration/1001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -480,22 +521,14 @@ func Test_internalIssueFieldConfigServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY Field Configuration Scheme", - Description: "description sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/fieldconfiguration/1001", - bytes.NewReader([]byte{})). + "", + payloadWithDescriptionMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -540,7 +573,7 @@ func Test_internalIssueFieldConfigServiceImpl_Update(t *testing.T) { func Test_internalIssueFieldConfigServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -566,12 +599,13 @@ func Test_internalIssueFieldConfigServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/fieldconfiguration/1001", + "", nil). Return(&http.Request{}, nil) @@ -596,12 +630,13 @@ func Test_internalIssueFieldConfigServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/fieldconfiguration/1001", + "", nil). Return(&http.Request{}, nil) @@ -637,12 +672,13 @@ func Test_internalIssueFieldConfigServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/fieldconfiguration/1001", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -686,7 +722,7 @@ func Test_internalIssueFieldConfigServiceImpl_Delete(t *testing.T) { func TestNewIssueFieldConfigurationService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From f31942e686c7d9f12acb4cbabbd4ad79fd408469 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Wed, 5 Jul 2023 01:32:32 -0600 Subject: [PATCH 13/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Field Configuration Item service. --- .../internal/field_configuration_item_impl.go | 13 +- .../field_configuration_item_impl_test.go | 196 +++++------------- 2 files changed, 52 insertions(+), 157 deletions(-) diff --git a/jira/internal/field_configuration_item_impl.go b/jira/internal/field_configuration_item_impl.go index f26fcfd5..84ae6aa4 100644 --- a/jira/internal/field_configuration_item_impl.go +++ b/jira/internal/field_configuration_item_impl.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewIssueFieldConfigurationItemService(client service.Client, version string) (*IssueFieldConfigItemService, error) { +func NewIssueFieldConfigurationItemService(client service.Connector, version string) (*IssueFieldConfigItemService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -48,7 +48,7 @@ func (i *IssueFieldConfigItemService) Update(ctx context.Context, id int, payloa } type internalIssueFieldConfigItemServiceImpl struct { - c service.Client + c service.Connector version string } @@ -64,7 +64,7 @@ func (i *internalIssueFieldConfigItemServiceImpl) Gets(ctx context.Context, id, endpoint := fmt.Sprintf("rest/api/%v/fieldconfiguration/%v/fields?%v", i.version, id, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -84,14 +84,9 @@ func (i *internalIssueFieldConfigItemServiceImpl) Update(ctx context.Context, id return nil, model.ErrNoFieldConfigurationIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/fieldconfiguration/%v/fields", i.version, id) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } diff --git a/jira/internal/field_configuration_item_impl_test.go b/jira/internal/field_configuration_item_impl_test.go index ab7aba14..c2308f60 100644 --- a/jira/internal/field_configuration_item_impl_test.go +++ b/jira/internal/field_configuration_item_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalIssueFieldConfigItemServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -43,12 +42,13 @@ func Test_internalIssueFieldConfigItemServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfiguration/10001/fields?maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -87,12 +87,13 @@ func Test_internalIssueFieldConfigItemServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/fieldconfiguration/10001/fields?maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -119,12 +120,13 @@ func Test_internalIssueFieldConfigItemServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfiguration/10001/fields?maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http created")) @@ -170,8 +172,29 @@ func Test_internalIssueFieldConfigItemServiceImpl_Gets(t *testing.T) { func Test_internalIssueFieldConfigItemServiceImpl_Update(t *testing.T) { + payloadMocked := &model.UpdateFieldConfigurationItemPayloadScheme{ + FieldConfigurationItems: []*model.FieldConfigurationItemScheme{ + { + ID: "customfield_10012", + IsHidden: false, + Description: "The new description of this item.", + }, + { + ID: "customfield_10011", + IsRequired: true, + }, + { + ID: "customfield_10010", + IsHidden: false, + IsRequired: false, + Description: "Another new description.", + Renderer: "wiki-renderer", + }, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -193,61 +216,20 @@ func Test_internalIssueFieldConfigItemServiceImpl_Update(t *testing.T) { name: "when the api version is v3", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - id: 10001, - payload: &model.UpdateFieldConfigurationItemPayloadScheme{ - FieldConfigurationItems: []*model.FieldConfigurationItemScheme{ - { - ID: "customfield_10012", - IsHidden: false, - Description: "The new description of this item.", - }, - { - ID: "customfield_10011", - IsRequired: true, - }, - { - ID: "customfield_10010", - IsHidden: false, - IsRequired: false, - Description: "Another new description.", - Renderer: "wiki-renderer", - }, - }, - }, + ctx: context.TODO(), + id: 10001, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.UpdateFieldConfigurationItemPayloadScheme{ - FieldConfigurationItems: []*model.FieldConfigurationItemScheme{ - { - ID: "customfield_10012", - IsHidden: false, - Description: "The new description of this item.", - }, - { - ID: "customfield_10011", - IsRequired: true, - }, - { - ID: "customfield_10010", - IsHidden: false, - IsRequired: false, - Description: "Another new description.", - Renderer: "wiki-renderer", - }, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/fieldconfiguration/10001/fields", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -276,61 +258,20 @@ func Test_internalIssueFieldConfigItemServiceImpl_Update(t *testing.T) { name: "when the api version is v2", fields: fields{version: "2"}, args: args{ - ctx: context.TODO(), - id: 10001, - payload: &model.UpdateFieldConfigurationItemPayloadScheme{ - FieldConfigurationItems: []*model.FieldConfigurationItemScheme{ - { - ID: "customfield_10012", - IsHidden: false, - Description: "The new description of this item.", - }, - { - ID: "customfield_10011", - IsRequired: true, - }, - { - ID: "customfield_10010", - IsHidden: false, - IsRequired: false, - Description: "Another new description.", - Renderer: "wiki-renderer", - }, - }, - }, + ctx: context.TODO(), + id: 10001, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.UpdateFieldConfigurationItemPayloadScheme{ - FieldConfigurationItems: []*model.FieldConfigurationItemScheme{ - { - ID: "customfield_10012", - IsHidden: false, - Description: "The new description of this item.", - }, - { - ID: "customfield_10011", - IsRequired: true, - }, - { - ID: "customfield_10010", - IsHidden: false, - IsRequired: false, - Description: "Another new description.", - Renderer: "wiki-renderer", - }, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/fieldconfiguration/10001/fields", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -349,61 +290,20 @@ func Test_internalIssueFieldConfigItemServiceImpl_Update(t *testing.T) { name: "when the http request cannot be created", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - id: 10001, - payload: &model.UpdateFieldConfigurationItemPayloadScheme{ - FieldConfigurationItems: []*model.FieldConfigurationItemScheme{ - { - ID: "customfield_10012", - IsHidden: false, - Description: "The new description of this item.", - }, - { - ID: "customfield_10011", - IsRequired: true, - }, - { - ID: "customfield_10010", - IsHidden: false, - IsRequired: false, - Description: "Another new description.", - Renderer: "wiki-renderer", - }, - }, - }, + ctx: context.TODO(), + id: 10001, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.UpdateFieldConfigurationItemPayloadScheme{ - FieldConfigurationItems: []*model.FieldConfigurationItemScheme{ - { - ID: "customfield_10012", - IsHidden: false, - Description: "The new description of this item.", - }, - { - ID: "customfield_10011", - IsRequired: true, - }, - { - ID: "customfield_10010", - IsHidden: false, - IsRequired: false, - Description: "Another new description.", - Renderer: "wiki-renderer", - }, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/fieldconfiguration/10001/fields", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http created")) fields.c = client @@ -447,7 +347,7 @@ func Test_internalIssueFieldConfigItemServiceImpl_Update(t *testing.T) { func Test_NewIssueFieldConfigurationItemService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 9b5f12919174cbad99399c2f1c9013a9cdb1e889 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Fri, 7 Jul 2023 16:25:57 -0600 Subject: [PATCH 14/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Field Configuration Scheme service. --- jira/internal/field_configuration_scheme.go | 73 +-- .../field_configuration_scheme_test.go | 457 +++++++----------- 2 files changed, 200 insertions(+), 330 deletions(-) diff --git a/jira/internal/field_configuration_scheme.go b/jira/internal/field_configuration_scheme.go index e3990bd7..8591da0a 100644 --- a/jira/internal/field_configuration_scheme.go +++ b/jira/internal/field_configuration_scheme.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewIssueFieldConfigurationSchemeService(client service.Client, version string) (*IssueFieldConfigSchemeService, error) { +func NewIssueFieldConfigurationSchemeService(client service.Connector, version string) (*IssueFieldConfigSchemeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -130,7 +130,7 @@ func (i *IssueFieldConfigSchemeService) Unlink(ctx context.Context, schemeId int } type internalIssueFieldConfigSchemeServiceImpl struct { - c service.Client + c service.Connector version string } @@ -146,7 +146,7 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Gets(ctx context.Context, id endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -166,22 +166,15 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Create(ctx context.Context, return nil, nil, model.ErrNoFieldConfigurationSchemeNameError } - payload := struct { - Name string `json:"name"` - Description string `json:"description,omitempty"` - }{ - Name: name, - Description: description, - } + payload := map[string]interface{}{"name": name} - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err + if description != "" { + payload["description"] = description } endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -195,19 +188,19 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Create(ctx context.Context, return scheme, response, nil } -func (i *internalIssueFieldConfigSchemeServiceImpl) Mapping(ctx context.Context, fieldConfigIds []int, startAt, maxResults int) (*model.FieldConfigurationIssueTypeItemPageScheme, *model.ResponseScheme, error) { +func (i *internalIssueFieldConfigSchemeServiceImpl) Mapping(ctx context.Context, fieldConfigIDs []int, startAt, maxResults int) (*model.FieldConfigurationIssueTypeItemPageScheme, *model.ResponseScheme, error) { params := url.Values{} params.Add("startAt", strconv.Itoa(startAt)) params.Add("maxResults", strconv.Itoa(maxResults)) - for _, id := range fieldConfigIds { + for _, id := range fieldConfigIDs { params.Add("fieldConfigurationSchemeId", strconv.Itoa(id)) } endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme/mapping?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -233,7 +226,7 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Project(ctx context.Context, endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme/project?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -249,14 +242,9 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Project(ctx context.Context, func (i *internalIssueFieldConfigSchemeServiceImpl) Assign(ctx context.Context, payload *model.FieldConfigurationSchemeAssignPayload) (*model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme/project", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -274,22 +262,15 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Update(ctx context.Context, return nil, model.ErrNoFieldConfigurationSchemeNameError } - payload := struct { - Name string `json:"name"` - Description string `json:"description,omitempty"` - }{ - Name: name, - Description: description, - } + payload := map[string]interface{}{"name": name} - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + if description != "" { + payload["description"] = description } endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme/%v", i.version, schemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -305,7 +286,7 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Delete(ctx context.Context, endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme/%v", i.version, schemeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -319,14 +300,9 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Link(ctx context.Context, sc return nil, model.ErrNoFieldConfigurationSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme/%v/mapping", i.version, schemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -340,20 +316,13 @@ func (i *internalIssueFieldConfigSchemeServiceImpl) Unlink(ctx context.Context, return nil, model.ErrNoFieldConfigurationSchemeIDError } - payload := struct { - IssueTypeIds []string `json:"issueTypeIds"` - }{ - IssueTypeIds: issueTypeIDs, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + if len(issueTypeIDs) == 0 { + return nil, model.ErrNoIssueTypesError } endpoint := fmt.Sprintf("rest/api/%v/fieldconfigurationscheme/%v/mapping/delete", i.version, schemeId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"issueTypeIds": issueTypeIDs}) if err != nil { return nil, err } diff --git a/jira/internal/field_configuration_scheme_test.go b/jira/internal/field_configuration_scheme_test.go index d1c63c3a..400f3415 100644 --- a/jira/internal/field_configuration_scheme_test.go +++ b/jira/internal/field_configuration_scheme_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalIssueFieldConfigSchemeServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -44,12 +43,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfigurationscheme?id=10001&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -75,12 +75,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/fieldconfigurationscheme?id=10001&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -106,12 +107,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfigurationscheme?id=10001&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -156,8 +158,12 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Gets(t *testing.T) { func Test_internalIssueFieldConfigSchemeServiceImpl_Create(t *testing.T) { + payloadWithDescriptionMocked := map[string]interface{}{"description": "field scheme sample", "name": "field scheme sample"} + + payloadMocked := map[string]interface{}{"name": "field scheme sample"} + type fields struct { - c service.Client + c service.Connector version string } @@ -184,23 +190,14 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "field scheme sample", - Description: "field scheme sample"}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/fieldconfigurationscheme", - bytes.NewReader([]byte{})). + "", + payloadWithDescriptionMocked). Return(&http.Request{}, nil) client.On("Call", @@ -224,23 +221,45 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) + + client.On("NewRequest", + context.Background(), + http.MethodPost, + "rest/api/2/fieldconfigurationscheme", + "", + payloadWithDescriptionMocked). + Return(&http.Request{}, nil) + + client.On("Call", + &http.Request{}, + &model.FieldConfigurationSchemeScheme{}). + Return(&model.ResponseScheme{}, nil) + + fields.c = client + }, + wantErr: false, + Err: nil, + }, + + { + name: "when the description is not provided", + fields: fields{version: "2"}, + args: args{ + ctx: context.TODO(), + name: "field scheme sample", + description: "", + }, + on: func(fields *fields) { - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "field scheme sample", - Description: "field scheme sample"}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/fieldconfigurationscheme", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -264,23 +283,14 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "field scheme sample", - Description: "field scheme sample"}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/fieldconfigurationscheme", - bytes.NewReader([]byte{})). + "", + payloadWithDescriptionMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -325,7 +335,7 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Create(t *testing.T) { func Test_internalIssueFieldConfigSchemeServiceImpl_Mapping(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -354,12 +364,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfigurationscheme/mapping?fieldConfigurationSchemeId=10001&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -385,12 +396,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/fieldconfigurationscheme/mapping?fieldConfigurationSchemeId=10001&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -416,12 +428,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfigurationscheme/mapping?fieldConfigurationSchemeId=10001&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -467,7 +480,7 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Mapping(t *testing.T) { func Test_internalIssueFieldConfigSchemeServiceImpl_Project(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -496,12 +509,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Project(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfigurationscheme/project?maxResults=50&projectId=10001&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -527,12 +541,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Project(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/fieldconfigurationscheme/project?maxResults=50&projectId=10001&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -558,12 +573,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Project(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/fieldconfigurationscheme/project?maxResults=50&projectId=10001&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -608,8 +624,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Project(t *testing.T) { func Test_internalIssueFieldConfigSchemeServiceImpl_Assign(t *testing.T) { + payloadMocked := &model.FieldConfigurationSchemeAssignPayload{ + FieldConfigurationSchemeID: "10000", + ProjectID: "10000", + } + type fields struct { - c service.Client + c service.Connector version string } @@ -630,28 +651,19 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Assign(t *testing.T) { name: "when the api version is v3", fields: fields{version: "3"}, args: args{ - ctx: context.TODO(), - payload: &model.FieldConfigurationSchemeAssignPayload{ - FieldConfigurationSchemeID: "10000", - ProjectID: "10000", - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldConfigurationSchemeAssignPayload{ - FieldConfigurationSchemeID: "10000", - ProjectID: "10000", - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/fieldconfigurationscheme/project", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -669,28 +681,19 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Assign(t *testing.T) { name: "when the api version is v2", fields: fields{version: "2"}, args: args{ - ctx: context.TODO(), - payload: &model.FieldConfigurationSchemeAssignPayload{ - FieldConfigurationSchemeID: "10000", - ProjectID: "10000", - }, + ctx: context.TODO(), + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldConfigurationSchemeAssignPayload{ - FieldConfigurationSchemeID: "10000", - ProjectID: "10000", - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/fieldconfigurationscheme/project", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -703,33 +706,6 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Assign(t *testing.T) { wantErr: false, Err: nil, }, - - { - name: "when the payload is not provided", - fields: fields{version: "3"}, - args: args{ - ctx: context.TODO(), - payload: &model.FieldConfigurationSchemeAssignPayload{ - FieldConfigurationSchemeID: "10000", - ProjectID: "10000", - }, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldConfigurationSchemeAssignPayload{ - FieldConfigurationSchemeID: "10000", - ProjectID: "10000", - }). - Return(bytes.NewReader([]byte{}), model.ErrNonPayloadPointerError) - - fields.c = client - }, - wantErr: true, - Err: model.ErrNonPayloadPointerError, - }, } for _, testCase := range testCases { @@ -763,8 +739,12 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Assign(t *testing.T) { func Test_internalIssueFieldConfigSchemeServiceImpl_Update(t *testing.T) { + payloadMockedWithDescription := map[string]interface{}{"description": "description sample", "name": "name sample"} + + payloadMocked := map[string]interface{}{"name": "name sample"} + type fields struct { - c service.Client + c service.Connector version string } @@ -793,20 +773,46 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{Name: "name sample", Description: "description sample"}). - Return(bytes.NewReader([]byte{}), nil) + client.On("NewRequest", + context.Background(), + http.MethodPut, + "rest/api/3/fieldconfigurationscheme/10001", + "", + payloadMockedWithDescription). + Return(&http.Request{}, nil) + + client.On("Call", + &http.Request{}, + nil). + Return(&model.ResponseScheme{}, nil) + + fields.c = client + }, + wantErr: false, + Err: nil, + }, + + { + name: "when the description is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + schemeId: 10001, + name: "name sample", + description: "", + }, + on: func(fields *fields) { + + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/fieldconfigurationscheme/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -857,20 +863,14 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{Name: "name sample", Description: "description sample"}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/fieldconfigurationscheme/10001", - bytes.NewReader([]byte{})). + "", + payloadMockedWithDescription). Return(&http.Request{}, nil) client.On("Call", @@ -883,32 +883,6 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Update(t *testing.T) { wantErr: false, Err: nil, }, - - { - name: "when the payload is not provided", - fields: fields{version: "3"}, - args: args{ - ctx: context.TODO(), - schemeId: 10001, - name: "name sample", - description: "description sample", - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name\"" - Description string "json:\"description,omitempty\"" - }{Name: "name sample", Description: "description sample"}). - Return(bytes.NewReader([]byte{}), model.ErrNonPayloadPointerError) - - fields.c = client - }, - wantErr: true, - Err: model.ErrNonPayloadPointerError, - }, } for _, testCase := range testCases { @@ -943,7 +917,7 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Update(t *testing.T) { func Test_internalIssueFieldConfigSchemeServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -969,12 +943,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/fieldconfigurationscheme/10001", + "", nil). Return(&http.Request{}, nil) @@ -1009,12 +984,13 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/fieldconfigurationscheme/10001", + "", nil). Return(&http.Request{}, nil) @@ -1061,8 +1037,21 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Delete(t *testing.T) { func Test_internalIssueFieldConfigSchemeServiceImpl_Link(t *testing.T) { + payloadMocked := &model.FieldConfigurationToIssueTypeMappingPayloadScheme{ + Mappings: []*model.FieldConfigurationToIssueTypeMappingScheme{ + { + IssueTypeID: "default", + FieldConfigurationID: "10000", + }, + { + IssueTypeID: "10001", + FieldConfigurationID: "10002", + }, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -1086,43 +1075,18 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Link(t *testing.T) { args: args{ ctx: context.TODO(), schemeId: 10001, - payload: &model.FieldConfigurationToIssueTypeMappingPayloadScheme{ - Mappings: []*model.FieldConfigurationToIssueTypeMappingScheme{ - { - IssueTypeID: "default", - FieldConfigurationID: "10000", - }, - { - IssueTypeID: "10001", - FieldConfigurationID: "10002", - }, - }, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldConfigurationToIssueTypeMappingPayloadScheme{ - Mappings: []*model.FieldConfigurationToIssueTypeMappingScheme{ - { - IssueTypeID: "default", - FieldConfigurationID: "10000", - }, - { - IssueTypeID: "10001", - FieldConfigurationID: "10002", - }, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/fieldconfigurationscheme/10001/mapping", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1142,18 +1106,7 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Link(t *testing.T) { args: args{ ctx: context.TODO(), schemeId: 0, - payload: &model.FieldConfigurationToIssueTypeMappingPayloadScheme{ - Mappings: []*model.FieldConfigurationToIssueTypeMappingScheme{ - { - IssueTypeID: "default", - FieldConfigurationID: "10000", - }, - { - IssueTypeID: "10001", - FieldConfigurationID: "10002", - }, - }, - }, + payload: payloadMocked, }, wantErr: true, Err: model.ErrNoFieldConfigurationSchemeIDError, @@ -1165,43 +1118,18 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Link(t *testing.T) { args: args{ ctx: context.TODO(), schemeId: 10001, - payload: &model.FieldConfigurationToIssueTypeMappingPayloadScheme{ - Mappings: []*model.FieldConfigurationToIssueTypeMappingScheme{ - { - IssueTypeID: "default", - FieldConfigurationID: "10000", - }, - { - IssueTypeID: "10001", - FieldConfigurationID: "10002", - }, - }, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldConfigurationToIssueTypeMappingPayloadScheme{ - Mappings: []*model.FieldConfigurationToIssueTypeMappingScheme{ - { - IssueTypeID: "default", - FieldConfigurationID: "10000", - }, - { - IssueTypeID: "10001", - FieldConfigurationID: "10002", - }, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/fieldconfigurationscheme/10001/mapping", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1221,43 +1149,18 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Link(t *testing.T) { args: args{ ctx: context.TODO(), schemeId: 10001, - payload: &model.FieldConfigurationToIssueTypeMappingPayloadScheme{ - Mappings: []*model.FieldConfigurationToIssueTypeMappingScheme{ - { - IssueTypeID: "default", - FieldConfigurationID: "10000", - }, - { - IssueTypeID: "10001", - FieldConfigurationID: "10002", - }, - }, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldConfigurationToIssueTypeMappingPayloadScheme{ - Mappings: []*model.FieldConfigurationToIssueTypeMappingScheme{ - { - IssueTypeID: "default", - FieldConfigurationID: "10000", - }, - { - IssueTypeID: "10001", - FieldConfigurationID: "10002", - }, - }, - }). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/fieldconfigurationscheme/10001/mapping", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1298,8 +1201,10 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Link(t *testing.T) { func Test_internalIssueFieldConfigSchemeServiceImpl_Unlink(t *testing.T) { + payloadMocked := map[string]interface{}{"issueTypeIds": []string{"1001", "1002"}} + type fields struct { - c service.Client + c service.Connector version string } @@ -1327,19 +1232,14 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Unlink(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"1001", "1002"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/fieldconfigurationscheme/10001/mapping/delete", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1363,19 +1263,14 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Unlink(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"1001", "1002"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/fieldconfigurationscheme/10001/mapping/delete", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1400,6 +1295,17 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Unlink(t *testing.T) { Err: model.ErrNoFieldConfigurationSchemeIDError, }, + { + name: "when the issuetype id's are not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + schemeId: 1000, + }, + wantErr: true, + Err: model.ErrNoIssueTypesError, + }, + { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -1410,19 +1316,14 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Unlink(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"1001", "1002"}}). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/fieldconfigurationscheme/10001/mapping/delete", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1464,7 +1365,7 @@ func Test_internalIssueFieldConfigSchemeServiceImpl_Unlink(t *testing.T) { func Test_NewIssueFieldConfigurationSchemeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 33ab9ae30fbd96ecb48543e65dcf832f42bc09ea Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Fri, 7 Jul 2023 17:08:45 -0600 Subject: [PATCH 15/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Field Context Scheme service. --- jira/internal/field_context_impl.go | 103 ++--- jira/internal/field_context_impl_test.go | 536 ++++++++++------------- pkg/infra/models/errors.go | 1 + 3 files changed, 258 insertions(+), 382 deletions(-) diff --git a/jira/internal/field_context_impl.go b/jira/internal/field_context_impl.go index e71d3f22..2347e6d0 100644 --- a/jira/internal/field_context_impl.go +++ b/jira/internal/field_context_impl.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewIssueFieldContextService(client service.Client, version string, option *IssueFieldContextOptionService) (*IssueFieldContextService, error) { +func NewIssueFieldContextService(client service.Connector, version string, option *IssueFieldContextOptionService) (*IssueFieldContextService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -161,7 +161,7 @@ func (i *IssueFieldContextService) UnLink(ctx context.Context, fieldId string, c } type internalIssueFieldContextServiceImpl struct { - c service.Client + c service.Connector version string } @@ -186,7 +186,7 @@ func (i *internalIssueFieldContextServiceImpl) Gets(ctx context.Context, fieldId endpoint := fmt.Sprintf("rest/api/%v/field/%v/context?%v", i.version, fieldId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -206,14 +206,9 @@ func (i *internalIssueFieldContextServiceImpl) Create(ctx context.Context, field return nil, nil, model.ErrNoFieldIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/field/%v/context", i.version, fieldId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -243,7 +238,7 @@ func (i *internalIssueFieldContextServiceImpl) GetDefaultValues(ctx context.Cont endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/defaultValue?%s", i.version, fieldId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -263,14 +258,9 @@ func (i *internalIssueFieldContextServiceImpl) SetDefaultValue(ctx context.Conte return nil, model.ErrNoFieldIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/defaultValue", i.version, fieldId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -294,7 +284,7 @@ func (i *internalIssueFieldContextServiceImpl) IssueTypesContext(ctx context.Con endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/issuetypemapping?%v", i.version, fieldId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -324,7 +314,7 @@ func (i *internalIssueFieldContextServiceImpl) ProjectsContext(ctx context.Conte endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/projectmapping?%v", i.version, fieldId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -348,22 +338,15 @@ func (i *internalIssueFieldContextServiceImpl) Update(ctx context.Context, field return nil, model.ErrNoFieldContextIDError } - payload := struct { - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - }{ - Name: name, - Description: description, - } + payload := map[string]interface{}{"name": name} - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + if description != "" { + payload["description"] = description } endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -383,7 +366,7 @@ func (i *internalIssueFieldContextServiceImpl) Delete(ctx context.Context, field endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -401,20 +384,9 @@ func (i *internalIssueFieldContextServiceImpl) AddIssueTypes(ctx context.Context return nil, model.ErrNoIssueTypesError } - payload := struct { - IssueTypeIds []string `json:"issueTypeIds"` - }{ - IssueTypeIds: issueTypesIds, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/issuetype", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"issueTypeIds": issueTypesIds}) if err != nil { return nil, err } @@ -432,20 +404,9 @@ func (i *internalIssueFieldContextServiceImpl) RemoveIssueTypes(ctx context.Cont return nil, model.ErrNoIssueTypesError } - payload := struct { - IssueTypeIds []string `json:"issueTypeIds"` - }{ - IssueTypeIds: issueTypesIds, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/issuetype/remove", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"issueTypeIds": issueTypesIds}) if err != nil { return nil, err } @@ -459,24 +420,17 @@ func (i *internalIssueFieldContextServiceImpl) Link(ctx context.Context, fieldId return nil, model.ErrNoFieldIDError } - if len(projectIds) == 0 { - return nil, model.ErrNoIssueTypesError - } - - payload := struct { - ProjectIds []string `json:"projectIds"` - }{ - ProjectIds: projectIds, + if contextId == 0 { + return nil, model.ErrNoFieldContextIDError } - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + if len(projectIds) == 0 { + return nil, model.ErrNoProjectIDsError } endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/project", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"projectIds": projectIds}) if err != nil { return nil, err } @@ -490,24 +444,17 @@ func (i *internalIssueFieldContextServiceImpl) UnLink(ctx context.Context, field return nil, model.ErrNoFieldIDError } - if len(projectIds) == 0 { - return nil, model.ErrNoIssueTypesError - } - - payload := struct { - ProjectIds []string `json:"projectIds"` - }{ - ProjectIds: projectIds, + if contextId == 0 { + return nil, model.ErrNoFieldContextIDError } - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + if len(projectIds) == 0 { + return nil, model.ErrNoProjectIDsError } endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/project/remove", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"projectIds": projectIds}) if err != nil { return nil, err } diff --git a/jira/internal/field_context_impl_test.go b/jira/internal/field_context_impl_test.go index 347f396c..bcda6c83 100644 --- a/jira/internal/field_context_impl_test.go +++ b/jira/internal/field_context_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalIssueFieldContextServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -50,12 +49,13 @@ func Test_internalIssueFieldContextServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/custom_field_10002/context?contextId=10001&contextId=10002&isAnyIssueType=true&isGlobalContext=false&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -86,12 +86,13 @@ func Test_internalIssueFieldContextServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context?contextId=10001&contextId=10002&isAnyIssueType=true&isGlobalContext=false&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -140,12 +141,13 @@ func Test_internalIssueFieldContextServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context?contextId=10001&contextId=10002&isAnyIssueType=true&isGlobalContext=false&maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error")) @@ -190,8 +192,15 @@ func Test_internalIssueFieldContextServiceImpl_Gets(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_Create(t *testing.T) { + payloadMocked := &model.FieldContextPayloadScheme{ + IssueTypeIDs: []int{10010}, + ProjectIDs: nil, + Name: "Bug fields context", + Description: "A context used to define the custom field options for bugs.", + } + type fields struct { - c service.Client + c service.Connector version string } @@ -215,32 +224,18 @@ func Test_internalIssueFieldContextServiceImpl_Create(t *testing.T) { args: args{ ctx: context.TODO(), fieldId: "custom_field_10002", - payload: &model.FieldContextPayloadScheme{ - IssueTypeIDs: []int{10010}, - ProjectIDs: nil, - Name: "Bug fields context", - Description: "A context used to define the custom field options for bugs.", - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextPayloadScheme{ - IssueTypeIDs: []int{10010}, - ProjectIDs: nil, - Name: "Bug fields context", - Description: "A context used to define the custom field options for bugs.", - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/custom_field_10002/context", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -260,32 +255,18 @@ func Test_internalIssueFieldContextServiceImpl_Create(t *testing.T) { args: args{ ctx: context.TODO(), fieldId: "custom_field_10002", - payload: &model.FieldContextPayloadScheme{ - IssueTypeIDs: []int{10010}, - ProjectIDs: nil, - Name: "Bug fields context", - Description: "A context used to define the custom field options for bugs.", - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextPayloadScheme{ - IssueTypeIDs: []int{10010}, - ProjectIDs: nil, - Name: "Bug fields context", - Description: "A context used to define the custom field options for bugs.", - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/custom_field_10002/context", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -305,12 +286,7 @@ func Test_internalIssueFieldContextServiceImpl_Create(t *testing.T) { args: args{ ctx: context.TODO(), fieldId: "", - payload: &model.FieldContextPayloadScheme{ - IssueTypeIDs: []int{10010}, - ProjectIDs: nil, - Name: "Bug fields context", - Description: "A context used to define the custom field options for bugs.", - }, + payload: payloadMocked, }, wantErr: true, Err: model.ErrNoFieldIDError, @@ -322,32 +298,18 @@ func Test_internalIssueFieldContextServiceImpl_Create(t *testing.T) { args: args{ ctx: context.TODO(), fieldId: "custom_field_10002", - payload: &model.FieldContextPayloadScheme{ - IssueTypeIDs: []int{10010}, - ProjectIDs: nil, - Name: "Bug fields context", - Description: "A context used to define the custom field options for bugs.", - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextPayloadScheme{ - IssueTypeIDs: []int{10010}, - ProjectIDs: nil, - Name: "Bug fields context", - Description: "A context used to define the custom field options for bugs.", - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/custom_field_10002/context", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -391,7 +353,7 @@ func Test_internalIssueFieldContextServiceImpl_Create(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_GetDefaultValues(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -422,12 +384,13 @@ func Test_internalIssueFieldContextServiceImpl_GetDefaultValues(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/custom_field_10002/context/defaultValue?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -454,12 +417,13 @@ func Test_internalIssueFieldContextServiceImpl_GetDefaultValues(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context/defaultValue?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -497,12 +461,13 @@ func Test_internalIssueFieldContextServiceImpl_GetDefaultValues(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context/defaultValue?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -552,8 +517,18 @@ func Test_internalIssueFieldContextServiceImpl_GetDefaultValues(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_SetDefaultValue(t *testing.T) { + payloadMocked := &model.FieldContextDefaultPayloadScheme{ + DefaultValues: []*model.CustomFieldDefaultValueScheme{ + { + ContextID: "10128", + OptionID: "10022", + Type: "option.single", + }, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -577,38 +552,18 @@ func Test_internalIssueFieldContextServiceImpl_SetDefaultValue(t *testing.T) { args: args{ ctx: context.TODO(), fieldId: "custom_field_10002", - payload: &model.FieldContextDefaultPayloadScheme{ - DefaultValues: []*model.CustomFieldDefaultValueScheme{ - { - ContextID: "10128", - OptionID: "10022", - Type: "option.single", - }, - }, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextDefaultPayloadScheme{ - DefaultValues: []*model.CustomFieldDefaultValueScheme{ - { - ContextID: "10128", - OptionID: "10022", - Type: "option.single", - }, - }, - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/field/custom_field_10002/context/defaultValue", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -628,38 +583,18 @@ func Test_internalIssueFieldContextServiceImpl_SetDefaultValue(t *testing.T) { args: args{ ctx: context.TODO(), fieldId: "custom_field_10002", - payload: &model.FieldContextDefaultPayloadScheme{ - DefaultValues: []*model.CustomFieldDefaultValueScheme{ - { - ContextID: "10128", - OptionID: "10022", - Type: "option.single", - }, - }, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextDefaultPayloadScheme{ - DefaultValues: []*model.CustomFieldDefaultValueScheme{ - { - ContextID: "10128", - OptionID: "10022", - Type: "option.single", - }, - }, - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/defaultValue", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -690,38 +625,18 @@ func Test_internalIssueFieldContextServiceImpl_SetDefaultValue(t *testing.T) { args: args{ ctx: context.TODO(), fieldId: "custom_field_10002", - payload: &model.FieldContextDefaultPayloadScheme{ - DefaultValues: []*model.CustomFieldDefaultValueScheme{ - { - ContextID: "10128", - OptionID: "10022", - Type: "option.single", - }, - }, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextDefaultPayloadScheme{ - DefaultValues: []*model.CustomFieldDefaultValueScheme{ - { - ContextID: "10128", - OptionID: "10022", - Type: "option.single", - }, - }, - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/defaultValue", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -764,7 +679,7 @@ func Test_internalIssueFieldContextServiceImpl_SetDefaultValue(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_IssueTypesContext(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -795,12 +710,13 @@ func Test_internalIssueFieldContextServiceImpl_IssueTypesContext(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/custom_field_10002/context/issuetypemapping?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -827,12 +743,13 @@ func Test_internalIssueFieldContextServiceImpl_IssueTypesContext(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context/issuetypemapping?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -870,12 +787,13 @@ func Test_internalIssueFieldContextServiceImpl_IssueTypesContext(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context/issuetypemapping?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error")) @@ -921,7 +839,7 @@ func Test_internalIssueFieldContextServiceImpl_IssueTypesContext(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_ProjectsContext(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -952,12 +870,13 @@ func Test_internalIssueFieldContextServiceImpl_ProjectsContext(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/custom_field_10002/context/projectmapping?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -984,12 +903,13 @@ func Test_internalIssueFieldContextServiceImpl_ProjectsContext(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context/projectmapping?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -1027,12 +947,13 @@ func Test_internalIssueFieldContextServiceImpl_ProjectsContext(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context/projectmapping?contextId=10001&maxResults=50&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error")) @@ -1077,8 +998,12 @@ func Test_internalIssueFieldContextServiceImpl_ProjectsContext(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_Update(t *testing.T) { + payloadMockedWithDescription := map[string]interface{}{"description": "new customfield context description", "name": "DUMMY - customfield_10002 Context"} + + payloadMocked := map[string]interface{}{"name": "DUMMY - customfield_10002 Context"} + type fields struct { - c service.Client + c service.Connector version string } @@ -1109,23 +1034,47 @@ func Test_internalIssueFieldContextServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) + + client.On("NewRequest", + context.Background(), + http.MethodPut, + "rest/api/3/field/custom_field_10002/context/10001", + "", + payloadMockedWithDescription). + Return(&http.Request{}, nil) + + client.On("Call", + &http.Request{}, + nil). + Return(&model.ResponseScheme{}, nil) + + fields.c = client + }, + wantErr: false, + Err: nil, + }, + + { + name: "when the description is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "custom_field_10002", + contextId: 10001, + name: "DUMMY - customfield_10002 Context", + description: "", + }, + on: func(fields *fields) { - client.On("TransformStructToReader", - &struct { - Name string "json:\"name,omitempty\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY - customfield_10002 Context", - Description: "new customfield context description"}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/field/custom_field_10002/context/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1151,23 +1100,14 @@ func Test_internalIssueFieldContextServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name,omitempty\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY - customfield_10002 Context", - Description: "new customfield context description"}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/10001", - bytes.NewReader([]byte{})). + "", + payloadMockedWithDescription). Return(&http.Request{}, nil) client.On("Call", @@ -1204,23 +1144,14 @@ func Test_internalIssueFieldContextServiceImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - Name string "json:\"name,omitempty\"" - Description string "json:\"description,omitempty\"" - }{ - Name: "DUMMY - customfield_10002 Context", - Description: "new customfield context description"}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/10001", - bytes.NewReader([]byte{})). + "", + payloadMockedWithDescription). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -1264,7 +1195,7 @@ func Test_internalIssueFieldContextServiceImpl_Update(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -1292,12 +1223,13 @@ func Test_internalIssueFieldContextServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/field/custom_field_10002/context/10001", + "", nil). Return(&http.Request{}, nil) @@ -1322,12 +1254,13 @@ func Test_internalIssueFieldContextServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/field/custom_field_10002/context/10001", + "", nil). Return(&http.Request{}, nil) @@ -1363,12 +1296,13 @@ func Test_internalIssueFieldContextServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/field/custom_field_10002/context/10001", + "", nil). Return(&http.Request{}, errors.New("error")) @@ -1411,8 +1345,10 @@ func Test_internalIssueFieldContextServiceImpl_Delete(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_AddIssueTypes(t *testing.T) { + payloadMocked := map[string]interface{}{"issueTypeIds": []string{"4", "3"}} + type fields struct { - c service.Client + c service.Connector version string } @@ -1442,20 +1378,14 @@ func Test_internalIssueFieldContextServiceImpl_AddIssueTypes(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/field/custom_field_10002/context/10001/issuetype", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1480,20 +1410,14 @@ func Test_internalIssueFieldContextServiceImpl_AddIssueTypes(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/10001/issuetype", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1529,20 +1453,14 @@ func Test_internalIssueFieldContextServiceImpl_AddIssueTypes(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/10001/issuetype", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -1585,8 +1503,10 @@ func Test_internalIssueFieldContextServiceImpl_AddIssueTypes(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_RemoveIssueTypes(t *testing.T) { + payloadMocked := map[string]interface{}{"issueTypeIds": []string{"4", "3"}} + type fields struct { - c service.Client + c service.Connector version string } @@ -1616,20 +1536,14 @@ func Test_internalIssueFieldContextServiceImpl_RemoveIssueTypes(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/custom_field_10002/context/10001/issuetype/remove", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1654,20 +1568,14 @@ func Test_internalIssueFieldContextServiceImpl_RemoveIssueTypes(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/custom_field_10002/context/10001/issuetype/remove", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1692,6 +1600,18 @@ func Test_internalIssueFieldContextServiceImpl_RemoveIssueTypes(t *testing.T) { Err: model.ErrNoFieldIDError, }, + { + name: "when the issuetype id's are not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "custom_field_10002", + issueTypesIds: nil, + }, + wantErr: true, + Err: model.ErrNoIssueTypesError, + }, + { name: "when the http request cannot be created", fields: fields{version: "2"}, @@ -1703,20 +1623,14 @@ func Test_internalIssueFieldContextServiceImpl_RemoveIssueTypes(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/custom_field_10002/context/10001/issuetype/remove", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -1759,8 +1673,10 @@ func Test_internalIssueFieldContextServiceImpl_RemoveIssueTypes(t *testing.T) { func Test_internalIssueFieldContextServiceImpl_Link(t *testing.T) { + payloadMocked := map[string]interface{}{"projectIds": []string{"4", "3"}} + type fields struct { - c service.Client + c service.Connector version string } @@ -1790,20 +1706,14 @@ func Test_internalIssueFieldContextServiceImpl_Link(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - ProjectIds []string "json:\"projectIds\"" - }{ProjectIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/field/custom_field_10002/context/10001/project", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1828,20 +1738,14 @@ func Test_internalIssueFieldContextServiceImpl_Link(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - ProjectIds []string "json:\"projectIds\"" - }{ProjectIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/10001/project", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1866,6 +1770,29 @@ func Test_internalIssueFieldContextServiceImpl_Link(t *testing.T) { Err: model.ErrNoFieldIDError, }, + { + name: "when the context id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "custom_field_10002", + }, + wantErr: true, + Err: model.ErrNoFieldContextIDError, + }, + + { + name: "when the field id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "custom_field_10002", + contextId: 10001, + }, + wantErr: true, + Err: model.ErrNoProjectIDsError, + }, + { name: "when the http request cannot be created", fields: fields{version: "2"}, @@ -1877,20 +1804,14 @@ func Test_internalIssueFieldContextServiceImpl_Link(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - ProjectIds []string "json:\"projectIds\"" - }{ProjectIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/10001/project", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -1931,10 +1852,12 @@ func Test_internalIssueFieldContextServiceImpl_Link(t *testing.T) { } } -func Test_internalIssueFieldContextServiceImpl_UnLink(t *testing.T) { +func Test_internalIssueFieldContextServiceImpl_Unlink(t *testing.T) { + + payloadMocked := map[string]interface{}{"projectIds": []string{"4", "3"}} type fields struct { - c service.Client + c service.Connector version string } @@ -1964,20 +1887,14 @@ func Test_internalIssueFieldContextServiceImpl_UnLink(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - ProjectIds []string "json:\"projectIds\"" - }{ProjectIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/custom_field_10002/context/10001/project/remove", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -2002,20 +1919,14 @@ func Test_internalIssueFieldContextServiceImpl_UnLink(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - ProjectIds []string "json:\"projectIds\"" - }{ProjectIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/custom_field_10002/context/10001/project/remove", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -2040,6 +1951,29 @@ func Test_internalIssueFieldContextServiceImpl_UnLink(t *testing.T) { Err: model.ErrNoFieldIDError, }, + { + name: "when the context id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "custom_field_10002", + }, + wantErr: true, + Err: model.ErrNoFieldContextIDError, + }, + + { + name: "when the field id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "custom_field_10002", + contextId: 10001, + }, + wantErr: true, + Err: model.ErrNoProjectIDsError, + }, + { name: "when the http request cannot be created", fields: fields{version: "2"}, @@ -2051,20 +1985,14 @@ func Test_internalIssueFieldContextServiceImpl_UnLink(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &struct { - ProjectIds []string "json:\"projectIds\"" - }{ProjectIds: []string{"4", "3"}}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/custom_field_10002/context/10001/project/remove", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client diff --git a/pkg/infra/models/errors.go b/pkg/infra/models/errors.go index f5524871..2dd1d411 100644 --- a/pkg/infra/models/errors.go +++ b/pkg/infra/models/errors.go @@ -116,6 +116,7 @@ var ( ErrNoScreenTabNameError = errors.New("jira: no screen tab name set") ErrNoAccountSliceError = errors.New("jira: no account id's set") ErrNoProjectKeySliceError = errors.New("jira: no project key's set") + ErrNoProjectIDsError = errors.New("jira: no project id's set") ErrNoWorkflowIDError = errors.New("jira: no workflow id set") ErrNoWorkflowSchemeIDError = errors.New("jira: no workflow scheme id set") ErrNoScreenIDError = errors.New("jira: no screen id set") From 2451965900d7283039ee24dab1526f6d605dc24c Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 8 Jul 2023 22:29:21 -0600 Subject: [PATCH 16/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Field Context Option service. --- jira/internal/field_context_option_impl.go | 29 +- .../field_context_option_impl_test.go | 398 +++++++----------- 2 files changed, 162 insertions(+), 265 deletions(-) diff --git a/jira/internal/field_context_option_impl.go b/jira/internal/field_context_option_impl.go index 74aa0f1a..3ddb75b9 100644 --- a/jira/internal/field_context_option_impl.go +++ b/jira/internal/field_context_option_impl.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewIssueFieldContextOptionService(client service.Client, version string) (*IssueFieldContextOptionService, error) { +func NewIssueFieldContextOptionService(client service.Connector, version string) (*IssueFieldContextOptionService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -84,7 +84,7 @@ func (i *IssueFieldContextOptionService) Order(ctx context.Context, fieldId stri } type internalIssueFieldContextOptionServiceImpl struct { - c service.Client + c service.Connector version string } @@ -108,7 +108,7 @@ func (i *internalIssueFieldContextOptionServiceImpl) Gets(ctx context.Context, f endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/option?%v", i.version, fieldId, contextId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -128,14 +128,13 @@ func (i *internalIssueFieldContextOptionServiceImpl) Create(ctx context.Context, return nil, nil, model.ErrNoFieldIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err + if contextId == 0 { + return nil, nil, model.ErrNoFieldContextIDError } endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/option", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -155,14 +154,13 @@ func (i *internalIssueFieldContextOptionServiceImpl) Update(ctx context.Context, return nil, nil, model.ErrNoFieldIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err + if contextId == 0 { + return nil, nil, model.ErrNoFieldContextIDError } endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/option", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -192,7 +190,7 @@ func (i *internalIssueFieldContextOptionServiceImpl) Delete(ctx context.Context, endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/option/%v", i.version, fieldId, contextId, optionId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -210,14 +208,9 @@ func (i *internalIssueFieldContextOptionServiceImpl) Order(ctx context.Context, return nil, model.ErrNoFieldContextIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/field/%v/context/%v/option/move", i.version, fieldId, contextId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } diff --git a/jira/internal/field_context_option_impl_test.go b/jira/internal/field_context_option_impl_test.go index c126d75f..18162b84 100644 --- a/jira/internal/field_context_option_impl_test.go +++ b/jira/internal/field_context_option_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalIssueFieldContextOptionServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -51,12 +50,13 @@ func Test_internalIssueFieldContextOptionServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/custom_field_10002/context/10001/option?maxResults=50&onlyOptions=false&optionId=3022&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -87,12 +87,13 @@ func Test_internalIssueFieldContextOptionServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/custom_field_10002/context/10001/option?maxResults=50&onlyOptions=false&optionId=3022&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -141,12 +142,13 @@ func Test_internalIssueFieldContextOptionServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/custom_field_10002/context/10001/option?maxResults=50&onlyOptions=false&optionId=3022&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error")) @@ -191,8 +193,23 @@ func Test_internalIssueFieldContextOptionServiceImpl_Gets(t *testing.T) { func Test_internalIssueFieldContextOptionServiceImpl_Create(t *testing.T) { + payloadMocked := &model.FieldContextOptionListScheme{ + Options: []*model.CustomFieldContextOptionScheme{ + + // Single/Multiple Choice example + { + Value: "Option 2", + Disabled: false, + }, + { + Value: "Option 4", + Disabled: false, + }, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -218,46 +235,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Create(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/custom_field_10002/context/10001/option", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -278,46 +267,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Create(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/custom_field_10002/context/10001/option", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -343,6 +304,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Create(t *testing.T) { Err: model.ErrNoFieldIDError, }, + { + name: "when the context id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "customfield_1000", + contextId: 0, + }, + wantErr: true, + Err: model.ErrNoFieldContextIDError, + }, + { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -350,46 +323,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Create(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/custom_field_10002/context/10001/option", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -433,8 +378,23 @@ func Test_internalIssueFieldContextOptionServiceImpl_Create(t *testing.T) { func Test_internalIssueFieldContextOptionServiceImpl_Update(t *testing.T) { + payloadMocked := &model.FieldContextOptionListScheme{ + Options: []*model.CustomFieldContextOptionScheme{ + + // Single/Multiple Choice example + { + Value: "Option 2", + Disabled: false, + }, + { + Value: "Option 4", + Disabled: false, + }, + }, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -460,46 +420,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Update(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/field/custom_field_10002/context/10001/option", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -520,46 +452,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Update(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/10001/option", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -585,6 +489,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Update(t *testing.T) { Err: model.ErrNoFieldIDError, }, + { + name: "when the context id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "customfield_1000", + contextId: 0, + }, + wantErr: true, + Err: model.ErrNoFieldContextIDError, + }, + { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -592,46 +508,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Update(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.FieldContextOptionListScheme{ - Options: []*model.CustomFieldContextOptionScheme{ - - // Single/Multiple Choice example - { - Value: "Option 2", - Disabled: false, - }, - { - Value: "Option 4", - Disabled: false, - }, - }}, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/field/custom_field_10002/context/10001/option", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -676,7 +564,7 @@ func Test_internalIssueFieldContextOptionServiceImpl_Update(t *testing.T) { func Test_internalIssueFieldContextOptionServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -706,12 +594,13 @@ func Test_internalIssueFieldContextOptionServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/field/custom_field_10002/context/10001/option/1001", + "", nil). Return(&http.Request{}, nil) @@ -737,12 +626,13 @@ func Test_internalIssueFieldContextOptionServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/field/custom_field_10002/context/10001/option/1001", + "", nil). Return(&http.Request{}, nil) @@ -769,6 +659,32 @@ func Test_internalIssueFieldContextOptionServiceImpl_Delete(t *testing.T) { Err: model.ErrNoFieldIDError, }, + { + name: "when the context id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "customfield_1000", + contextId: 0, + }, + wantErr: true, + Err: model.ErrNoFieldContextIDError, + }, + + { + name: "when the option id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "customfield_1000", + contextId: 1000, + optionId: 0, + }, + + wantErr: true, + Err: model.ErrNoContextOptionIDError, + }, + { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -780,12 +696,13 @@ func Test_internalIssueFieldContextOptionServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/field/custom_field_10002/context/10001/option/1001", + "", nil). Return(&http.Request{}, errors.New("error")) @@ -829,8 +746,13 @@ func Test_internalIssueFieldContextOptionServiceImpl_Delete(t *testing.T) { func Test_internalIssueFieldContextOptionServiceImpl_Order(t *testing.T) { + payloadMocked := &model.OrderFieldOptionPayloadScheme{ + Position: "Last", + CustomFieldOptionIds: []string{"111"}, + } + type fields struct { - c service.Client + c service.Connector version string } @@ -856,28 +778,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Order(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.OrderFieldOptionPayloadScheme{ - Position: "Last", - CustomFieldOptionIds: []string{"111"}, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.OrderFieldOptionPayloadScheme{ - Position: "Last", - CustomFieldOptionIds: []string{"111"}, - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/field/custom_field_10002/context/10001/option/move", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -898,28 +810,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Order(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.OrderFieldOptionPayloadScheme{ - Position: "Last", - CustomFieldOptionIds: []string{"111"}, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.OrderFieldOptionPayloadScheme{ - Position: "Last", - CustomFieldOptionIds: []string{"111"}, - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/field/custom_field_10002/context/10001/option/move", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -945,6 +847,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Order(t *testing.T) { Err: model.ErrNoFieldIDError, }, + { + name: "when the context id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + fieldId: "customfield_1000", + contextId: 0, + }, + wantErr: true, + Err: model.ErrNoFieldContextIDError, + }, + { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -952,28 +866,18 @@ func Test_internalIssueFieldContextOptionServiceImpl_Order(t *testing.T) { ctx: context.TODO(), fieldId: "custom_field_10002", contextId: 10001, - payload: &model.OrderFieldOptionPayloadScheme{ - Position: "Last", - CustomFieldOptionIds: []string{"111"}, - }, + payload: payloadMocked, }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - &model.OrderFieldOptionPayloadScheme{ - Position: "Last", - CustomFieldOptionIds: []string{"111"}, - }, - ). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/field/custom_field_10002/context/10001/option/move", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error")) fields.c = client @@ -1017,7 +921,7 @@ func Test_internalIssueFieldContextOptionServiceImpl_Order(t *testing.T) { func Test_NewIssueFieldContextOptionService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 0bdd42ef0a847880304a0e3b0e03f8bc4699481a Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 8 Jul 2023 22:30:35 -0600 Subject: [PATCH 17/70] :memo: Updated the Field.Context.Option.Order documentation link. --- jira/internal/field_context_option_impl.go | 2 +- service/jira/fieldContext.go | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/jira/internal/field_context_option_impl.go b/jira/internal/field_context_option_impl.go index 3ddb75b9..12366050 100644 --- a/jira/internal/field_context_option_impl.go +++ b/jira/internal/field_context_option_impl.go @@ -78,7 +78,7 @@ func (i *IssueFieldContextOptionService) Delete(ctx context.Context, fieldId str // // PUT /rest/api/{2-3}/field/{fieldId}/context/{contextId}/option/move // -// TODO: The documentation needs to be created, raise a ticket here: https://github.com/ctreminiom/go-atlassian/issues +// https://docs.go-atlassian.io/jira-software-cloud/issues/fields/context/option#reorder-custom-field-options func (i *IssueFieldContextOptionService) Order(ctx context.Context, fieldId string, contextId int, payload *model.OrderFieldOptionPayloadScheme) (*model.ResponseScheme, error) { return i.internalClient.Order(ctx, fieldId, contextId, payload) } diff --git a/service/jira/fieldContext.go b/service/jira/fieldContext.go index 7745268d..963ae9fd 100644 --- a/service/jira/fieldContext.go +++ b/service/jira/fieldContext.go @@ -10,12 +10,12 @@ import ( // FieldContextConnector is the interface that wraps the Jira field context // // It contains the methods required to manipulate the field context associated with a Jira field, you can use to: -// 1. get, create, update, and delete custom field contexts. -// 2. get context to issue types and projects mappings. -// 3. get custom field contexts for projects and issue types. -// 4. assign custom field contexts to projects. -// 5. remove custom field contexts from projects. -// 6. add issue types to custom field contexts. +// 1. get, create, update, and delete custom field contexts. +// 2. get context to issue types and projects mappings. +// 3. get custom field contexts for projects and issue types. +// 4. assign custom field contexts to projects. +// 5. remove custom field contexts from projects. +// 6. add issue types to custom field contexts. type FieldContextConnector interface { // Gets returns a paginated list of contexts for a custom field. Contexts can be returned as follows: @@ -183,6 +183,6 @@ type FieldContextOptionConnector interface { // // PUT /rest/api/{2-3}/field/{fieldId}/context/{contextId}/option/move // - // TODO: The documentation needs to be created, raise a ticket here: https://github.com/ctreminiom/go-atlassian/issues + // https://docs.go-atlassian.io/jira-software-cloud/issues/fields/context/option#reorder-custom-field-options Order(ctx context.Context, fieldId string, contextId int, payload *model.OrderFieldOptionPayloadScheme) (*model.ResponseScheme, error) } From 815bc83af28f7dc775b1d514ccc9288f3d9e8db9 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 8 Jul 2023 22:35:39 -0600 Subject: [PATCH 18/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Field Trash service. --- jira/internal/field_trash_impl.go | 10 +++---- jira/internal/field_trash_impl_test.go | 38 ++++++++++++++++---------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/jira/internal/field_trash_impl.go b/jira/internal/field_trash_impl.go index 0d504b9a..d36d4cfa 100644 --- a/jira/internal/field_trash_impl.go +++ b/jira/internal/field_trash_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewIssueFieldTrashService(client service.Client, version string) (*IssueFieldTrashService, error) { +func NewIssueFieldTrashService(client service.Connector, version string) (*IssueFieldTrashService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -63,7 +63,7 @@ func (i *IssueFieldTrashService) Restore(ctx context.Context, id string) (*model } type internalFieldTrashServiceImpl struct { - c service.Client + c service.Connector version string } @@ -90,7 +90,7 @@ func (i *internalFieldTrashServiceImpl) Search(ctx context.Context, options *mod endpoint := fmt.Sprintf("rest/api/%v/field/search/trashed?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -112,7 +112,7 @@ func (i *internalFieldTrashServiceImpl) Move(ctx context.Context, id string) (*m endpoint := fmt.Sprintf("rest/api/%v/field/%v/trash", i.version, id) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, err } @@ -128,7 +128,7 @@ func (i *internalFieldTrashServiceImpl) Restore(ctx context.Context, id string) endpoint := fmt.Sprintf("rest/api/%v/field/%v/restore", i.version, id) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/field_trash_impl_test.go b/jira/internal/field_trash_impl_test.go index 80c22887..93f39c69 100644 --- a/jira/internal/field_trash_impl_test.go +++ b/jira/internal/field_trash_impl_test.go @@ -14,7 +14,7 @@ import ( func Test_internalFieldTrashServiceImpl_Search(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -47,12 +47,13 @@ func Test_internalFieldTrashServiceImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/search/trashed?id=111%2C12222&maxResults=50&orderBy=lastUsed&query=query-sample&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -83,12 +84,13 @@ func Test_internalFieldTrashServiceImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/search/trashed?id=111%2C12222&maxResults=50&orderBy=lastUsed&query=query-sample&startAt=0", + "", nil). Return(&http.Request{}, nil) @@ -119,12 +121,13 @@ func Test_internalFieldTrashServiceImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/search/trashed?id=111%2C12222&maxResults=50&orderBy=lastUsed&query=query-sample&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -143,12 +146,13 @@ func Test_internalFieldTrashServiceImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/search/trashed?maxResults=0&startAt=0", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -195,7 +199,7 @@ func Test_internalFieldTrashServiceImpl_Search(t *testing.T) { func Test_internalFieldTrashServiceImpl_Move(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -221,12 +225,13 @@ func Test_internalFieldTrashServiceImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/customfield_12000/trash", + "", nil). Return(&http.Request{}, nil) @@ -251,12 +256,13 @@ func Test_internalFieldTrashServiceImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/customfield_12000/trash", + "", nil). Return(&http.Request{}, nil) @@ -292,12 +298,13 @@ func Test_internalFieldTrashServiceImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/customfield_12000/trash", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -342,7 +349,7 @@ func Test_internalFieldTrashServiceImpl_Move(t *testing.T) { func Test_internalFieldTrashServiceImpl_Restore(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -368,12 +375,13 @@ func Test_internalFieldTrashServiceImpl_Restore(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/customfield_12000/restore", + "", nil). Return(&http.Request{}, nil) @@ -398,12 +406,13 @@ func Test_internalFieldTrashServiceImpl_Restore(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/field/customfield_12000/restore", + "", nil). Return(&http.Request{}, nil) @@ -439,12 +448,13 @@ func Test_internalFieldTrashServiceImpl_Restore(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/field/customfield_12000/restore", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -489,7 +499,7 @@ func Test_internalFieldTrashServiceImpl_Restore(t *testing.T) { func Test_NewIssueFieldTrashService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 9122d440b3030a595612924bb1061bc7f4784e8e Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 8 Jul 2023 22:41:38 -0600 Subject: [PATCH 19/70] :art: Migrated the client interface from Client to Connector on the Jira Label service. --- jira/internal/label_impl.go | 6 +++--- jira/internal/label_impl_test.go | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/jira/internal/label_impl.go b/jira/internal/label_impl.go index 06ac2153..78df9e08 100644 --- a/jira/internal/label_impl.go +++ b/jira/internal/label_impl.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewLabelService(client service.Client, version string) (*LabelService, error) { +func NewLabelService(client service.Connector, version string) (*LabelService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -36,7 +36,7 @@ func (i *LabelService) Gets(ctx context.Context, startAt, maxResults int) (*mode } type internalLabelServiceImpl struct { - c service.Client + c service.Connector version string } @@ -48,7 +48,7 @@ func (i *internalLabelServiceImpl) Gets(ctx context.Context, startAt, maxResults endpoint := fmt.Sprintf("rest/api/%v/label?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/label_impl_test.go b/jira/internal/label_impl_test.go index 685afd19..e3cd0a14 100644 --- a/jira/internal/label_impl_test.go +++ b/jira/internal/label_impl_test.go @@ -14,7 +14,7 @@ import ( func Test_internalLabelServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -41,12 +41,13 @@ func Test_internalLabelServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/label?maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -71,12 +72,13 @@ func Test_internalLabelServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/label?maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, nil) @@ -101,12 +103,13 @@ func Test_internalLabelServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/label?maxResults=50&startAt=50", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -151,7 +154,7 @@ func Test_internalLabelServiceImpl_Gets(t *testing.T) { func Test_NewLabelService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 980e9b948e8d94a1e55cc6319dd7291a17a9ffde Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 8 Jul 2023 22:50:02 -0600 Subject: [PATCH 20/70] :art: Migrated the client interface from Client to Connector on the Jira Link service. --- jira/internal/link_impl.go | 2 +- jira/internal/link_impl_adf.go | 17 ++--- jira/internal/link_impl_adf_test.go | 85 ++++++++++++++--------- jira/internal/link_impl_rich_text.go | 15 ++-- jira/internal/link_impl_rich_text_test.go | 85 ++++++++++++++--------- jira/internal/link_impl_test.go | 2 +- 6 files changed, 119 insertions(+), 87 deletions(-) diff --git a/jira/internal/link_impl.go b/jira/internal/link_impl.go index 5c1b7e67..bf51051c 100644 --- a/jira/internal/link_impl.go +++ b/jira/internal/link_impl.go @@ -5,7 +5,7 @@ import ( "github.com/ctreminiom/go-atlassian/service" ) -func NewLinkService(client service.Client, version string, type_ *LinkTypeService, remote *RemoteLinkService) (*LinkADFService, *LinkRichTextService, error) { +func NewLinkService(client service.Connector, version string, type_ *LinkTypeService, remote *RemoteLinkService) (*LinkADFService, *LinkRichTextService, error) { if version == "" { return nil, nil, model.ErrNoVersionProvided diff --git a/jira/internal/link_impl_adf.go b/jira/internal/link_impl_adf.go index 40165104..7dd24ad9 100644 --- a/jira/internal/link_impl_adf.go +++ b/jira/internal/link_impl_adf.go @@ -16,7 +16,7 @@ type LinkADFService struct { } type internalLinkADFServiceImpl struct { - c service.Client + c service.Connector version string } @@ -47,7 +47,7 @@ func (l *LinkADFService) Delete(ctx context.Context, linkId string) (*model.Resp // Create creates a link between two issues. Use this operation to indicate a relationship between two issues // -// and optionally add a comment to the from (outward) issue. +// and optionally add a comment to the form (outward) issue. // // To use this resource the site must have Issue Linking enabled. // @@ -64,7 +64,7 @@ func (i *internalLinkADFServiceImpl) Get(ctx context.Context, linkId string) (*m endpoint := fmt.Sprintf("rest/api/%v/issueLink/%v", i.version, linkId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -86,7 +86,7 @@ func (i *internalLinkADFServiceImpl) Gets(ctx context.Context, issueKeyOrId stri endpoint := fmt.Sprintf("rest/api/%v/issue/%v?fields=issuelinks", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -108,7 +108,7 @@ func (i *internalLinkADFServiceImpl) Delete(ctx context.Context, linkId string) endpoint := fmt.Sprintf("rest/api/%v/issueLink/%v", i.version, linkId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -118,14 +118,9 @@ func (i *internalLinkADFServiceImpl) Delete(ctx context.Context, linkId string) func (i *internalLinkADFServiceImpl) Create(ctx context.Context, payload *model.LinkPayloadSchemeV3) (*model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issueLink", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, err } diff --git a/jira/internal/link_impl_adf_test.go b/jira/internal/link_impl_adf_test.go index 84d00e14..df4eae3c 100644 --- a/jira/internal/link_impl_adf_test.go +++ b/jira/internal/link_impl_adf_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalLinkADFServiceImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -41,12 +40,13 @@ func Test_internalLinkADFServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issueLink/10002", + "", nil). Return(&http.Request{}, nil) @@ -70,12 +70,13 @@ func Test_internalLinkADFServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issueLink/10002", + "", nil). Return(&http.Request{}, nil) @@ -99,12 +100,13 @@ func Test_internalLinkADFServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issueLink/10002", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -113,6 +115,17 @@ func Test_internalLinkADFServiceImpl_Get(t *testing.T) { wantErr: true, Err: errors.New("error, unable to create the http request"), }, + + { + name: "when the issue link is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + linkId: "", + }, + wantErr: true, + Err: model.ErrNoTypeIDError, + }, } for _, testCase := range testCases { @@ -149,7 +162,7 @@ func Test_internalLinkADFServiceImpl_Get(t *testing.T) { func Test_internalLinkADFServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -175,12 +188,13 @@ func Test_internalLinkADFServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-4?fields=issuelinks", + "", nil). Return(&http.Request{}, nil) @@ -204,12 +218,13 @@ func Test_internalLinkADFServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-4?fields=issuelinks", + "", nil). Return(&http.Request{}, nil) @@ -244,12 +259,13 @@ func Test_internalLinkADFServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-4?fields=issuelinks", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -294,7 +310,7 @@ func Test_internalLinkADFServiceImpl_Gets(t *testing.T) { func Test_internalLinkADFServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -320,12 +336,13 @@ func Test_internalLinkADFServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issueLink/10002", + "", nil). Return(&http.Request{}, nil) @@ -349,12 +366,13 @@ func Test_internalLinkADFServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issueLink/10002", + "", nil). Return(&http.Request{}, nil) @@ -378,12 +396,13 @@ func Test_internalLinkADFServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issueLink/10002", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -392,6 +411,17 @@ func Test_internalLinkADFServiceImpl_Delete(t *testing.T) { wantErr: true, Err: errors.New("error, unable to create the http request"), }, + + { + name: "when the issue link is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + linkId: "", + }, + wantErr: true, + Err: model.ErrNoTypeIDError, + }, } for _, testCase := range testCases { @@ -464,7 +494,7 @@ func Test_internalLinkADFServiceImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -490,17 +520,14 @@ func Test_internalLinkADFServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issueLink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -523,17 +550,14 @@ func Test_internalLinkADFServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issueLink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -556,17 +580,14 @@ func Test_internalLinkADFServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issueLink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client diff --git a/jira/internal/link_impl_rich_text.go b/jira/internal/link_impl_rich_text.go index 649717ec..3b52fb87 100644 --- a/jira/internal/link_impl_rich_text.go +++ b/jira/internal/link_impl_rich_text.go @@ -16,7 +16,7 @@ type LinkRichTextService struct { } type internalLinkRichTextServiceImpl struct { - c service.Client + c service.Connector version string } @@ -64,7 +64,7 @@ func (i *internalLinkRichTextServiceImpl) Get(ctx context.Context, linkId string endpoint := fmt.Sprintf("rest/api/%v/issueLink/%v", i.version, linkId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -86,7 +86,7 @@ func (i *internalLinkRichTextServiceImpl) Gets(ctx context.Context, issueKeyOrId endpoint := fmt.Sprintf("rest/api/%v/issue/%v?fields=issuelinks", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -108,7 +108,7 @@ func (i *internalLinkRichTextServiceImpl) Delete(ctx context.Context, linkId str endpoint := fmt.Sprintf("rest/api/%v/issueLink/%v", i.version, linkId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -118,14 +118,9 @@ func (i *internalLinkRichTextServiceImpl) Delete(ctx context.Context, linkId str func (i *internalLinkRichTextServiceImpl) Create(ctx context.Context, payload *model.LinkPayloadSchemeV2) (*model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issueLink", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, err } diff --git a/jira/internal/link_impl_rich_text_test.go b/jira/internal/link_impl_rich_text_test.go index 9ed602ac..d444e93b 100644 --- a/jira/internal/link_impl_rich_text_test.go +++ b/jira/internal/link_impl_rich_text_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalLinkRichTextServiceImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -41,12 +40,13 @@ func Test_internalLinkRichTextServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issueLink/10002", + "", nil). Return(&http.Request{}, nil) @@ -70,12 +70,13 @@ func Test_internalLinkRichTextServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issueLink/10002", + "", nil). Return(&http.Request{}, nil) @@ -99,12 +100,13 @@ func Test_internalLinkRichTextServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issueLink/10002", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -113,6 +115,17 @@ func Test_internalLinkRichTextServiceImpl_Get(t *testing.T) { wantErr: true, Err: errors.New("error, unable to create the http request"), }, + + { + name: "when the issue link is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + linkId: "", + }, + wantErr: true, + Err: model.ErrNoTypeIDError, + }, } for _, testCase := range testCases { @@ -149,7 +162,7 @@ func Test_internalLinkRichTextServiceImpl_Get(t *testing.T) { func Test_internalLinkRichTextServiceImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -175,12 +188,13 @@ func Test_internalLinkRichTextServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-4?fields=issuelinks", + "", nil). Return(&http.Request{}, nil) @@ -204,12 +218,13 @@ func Test_internalLinkRichTextServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-4?fields=issuelinks", + "", nil). Return(&http.Request{}, nil) @@ -244,12 +259,13 @@ func Test_internalLinkRichTextServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-4?fields=issuelinks", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -294,7 +310,7 @@ func Test_internalLinkRichTextServiceImpl_Gets(t *testing.T) { func Test_internalLinkRichTextServiceImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -320,12 +336,13 @@ func Test_internalLinkRichTextServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issueLink/10002", + "", nil). Return(&http.Request{}, nil) @@ -349,12 +366,13 @@ func Test_internalLinkRichTextServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issueLink/10002", + "", nil). Return(&http.Request{}, nil) @@ -378,12 +396,13 @@ func Test_internalLinkRichTextServiceImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issueLink/10002", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -392,6 +411,17 @@ func Test_internalLinkRichTextServiceImpl_Delete(t *testing.T) { wantErr: true, Err: errors.New("error, unable to create the http request"), }, + + { + name: "when the issue link is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + linkId: "", + }, + wantErr: true, + Err: model.ErrNoTypeIDError, + }, } for _, testCase := range testCases { @@ -442,7 +472,7 @@ func Test_internalLinkRichTextServiceImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -468,17 +498,14 @@ func Test_internalLinkRichTextServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issueLink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -501,17 +528,14 @@ func Test_internalLinkRichTextServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issueLink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -534,17 +558,14 @@ func Test_internalLinkRichTextServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issueLink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client diff --git a/jira/internal/link_impl_test.go b/jira/internal/link_impl_test.go index 57fb2542..f35f23a9 100644 --- a/jira/internal/link_impl_test.go +++ b/jira/internal/link_impl_test.go @@ -10,7 +10,7 @@ import ( func Test_NewLinkService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 358c268933c22736cf1fb71b99191c19f01f3c46 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 8 Jul 2023 22:59:33 -0600 Subject: [PATCH 21/70] :art: Migrated the client interface from Client to Connector on the Jira Remote-Link service. --- jira/internal/remote_link_impl.go | 26 ++--- jira/internal/remote_link_impl_test.go | 133 ++++++++++++------------- 2 files changed, 70 insertions(+), 89 deletions(-) diff --git a/jira/internal/remote_link_impl.go b/jira/internal/remote_link_impl.go index 9e6b42d1..15c8947f 100644 --- a/jira/internal/remote_link_impl.go +++ b/jira/internal/remote_link_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewRemoteLinkService(client service.Client, version string) (*RemoteLinkService, error) { +func NewRemoteLinkService(client service.Connector, version string) (*RemoteLinkService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -97,7 +97,7 @@ func (r *RemoteLinkService) DeleteByGlobalId(ctx context.Context, issueKeyOrId, } type internalRemoteLinkImpl struct { - c service.Client + c service.Connector version string } @@ -117,7 +117,7 @@ func (i *internalRemoteLinkImpl) Gets(ctx context.Context, issueKeyOrId, globalI endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -143,7 +143,7 @@ func (i *internalRemoteLinkImpl) Get(ctx context.Context, issueKeyOrId, linkId s endpoint := fmt.Sprintf("rest/api/%v/issue/%v/remotelink/%v", i.version, issueKeyOrId, linkId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -163,14 +163,9 @@ func (i *internalRemoteLinkImpl) Create(ctx context.Context, issueKeyOrId string return nil, nil, model.ErrNoIssueKeyOrIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/remotelink", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -194,14 +189,9 @@ func (i *internalRemoteLinkImpl) Update(ctx context.Context, issueKeyOrId, linkI return nil, model.ErrNoRemoteLinkIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issue/%v/remotelink/%v", i.version, issueKeyOrId, linkId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -221,7 +211,7 @@ func (i *internalRemoteLinkImpl) DeleteById(ctx context.Context, issueKeyOrId, l endpoint := fmt.Sprintf("rest/api/%v/issue/%v/remotelink/%v", i.version, issueKeyOrId, linkId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -244,7 +234,7 @@ func (i *internalRemoteLinkImpl) DeleteByGlobalId(ctx context.Context, issueKeyO endpoint := fmt.Sprintf("rest/api/%v/issue/%v/remotelink?%v", i.version, issueKeyOrId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/remote_link_impl_test.go b/jira/internal/remote_link_impl_test.go index 0afe4614..0f542e74 100644 --- a/jira/internal/remote_link_impl_test.go +++ b/jira/internal/remote_link_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalRemoteLinkImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -38,12 +37,13 @@ func Test_internalRemoteLinkImpl_Gets(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/KP-23/remotelink?globalId=system%3Dhttp%3A%2F%2Fwww.mycompany.com%2Fsupport%26id%3D1", + "", nil). Return(&http.Request{}, nil) @@ -67,12 +67,13 @@ func Test_internalRemoteLinkImpl_Gets(t *testing.T) { fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/KP-23/remotelink?globalId=system%3Dhttp%3A%2F%2Fwww.mycompany.com%2Fsupport%26id%3D1", + "", nil). Return(&http.Request{}, nil) @@ -106,12 +107,13 @@ func Test_internalRemoteLinkImpl_Gets(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/KP-23/remotelink?globalId=system%3Dhttp%3A%2F%2Fwww.mycompany.com%2Fsupport%26id%3D1", + "", nil). Return(&http.Request{}, errors.New("unable to create the http request")) @@ -131,12 +133,13 @@ func Test_internalRemoteLinkImpl_Gets(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/KP-23/remotelink?globalId=system%3Dhttp%3A%2F%2Fwww.mycompany.com%2Fsupport%26id%3D1", + "", nil). Return(&http.Request{}, nil) @@ -190,7 +193,7 @@ func Test_internalRemoteLinkImpl_Gets(t *testing.T) { func Test_internalRemoteLinkImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -212,12 +215,13 @@ func Test_internalRemoteLinkImpl_Get(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/KP-23/remotelink/10001", + "", nil). Return(&http.Request{}, nil) @@ -241,12 +245,13 @@ func Test_internalRemoteLinkImpl_Get(t *testing.T) { fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/KP-23/remotelink/10001", + "", nil). Return(&http.Request{}, nil) @@ -291,12 +296,13 @@ func Test_internalRemoteLinkImpl_Get(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/KP-23/remotelink/10001", + "", nil). Return(&http.Request{}, errors.New("unable to create the http request")) @@ -316,12 +322,13 @@ func Test_internalRemoteLinkImpl_Get(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/KP-23/remotelink/10001", + "", nil). Return(&http.Request{}, nil) @@ -401,7 +408,7 @@ func Test_internalRemoteLinkImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -424,17 +431,14 @@ func Test_internalRemoteLinkImpl_Update(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/KP-23/remotelink/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -458,17 +462,14 @@ func Test_internalRemoteLinkImpl_Update(t *testing.T) { fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/KP-23/remotelink/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -513,17 +514,14 @@ func Test_internalRemoteLinkImpl_Update(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/KP-23/remotelink/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("unable to create the http request")) fields.c = client @@ -543,17 +541,14 @@ func Test_internalRemoteLinkImpl_Update(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/KP-23/remotelink/10001", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -633,7 +628,7 @@ func Test_internalRemoteLinkImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -656,17 +651,14 @@ func Test_internalRemoteLinkImpl_Create(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/KP-23/remotelink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -689,17 +681,14 @@ func Test_internalRemoteLinkImpl_Create(t *testing.T) { fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/KP-23/remotelink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -732,17 +721,14 @@ func Test_internalRemoteLinkImpl_Create(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/KP-23/remotelink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("unable to create the http request")) fields.c = client @@ -761,17 +747,14 @@ func Test_internalRemoteLinkImpl_Create(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/KP-23/remotelink", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -825,7 +808,7 @@ func Test_internalRemoteLinkImpl_Create(t *testing.T) { func Test_internalRemoteLinkImpl_DeleteById(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -847,12 +830,13 @@ func Test_internalRemoteLinkImpl_DeleteById(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/KP-23/remotelink/10001", + "", nil). Return(&http.Request{}, nil) @@ -876,12 +860,13 @@ func Test_internalRemoteLinkImpl_DeleteById(t *testing.T) { fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/KP-23/remotelink/10001", + "", nil). Return(&http.Request{}, nil) @@ -926,12 +911,13 @@ func Test_internalRemoteLinkImpl_DeleteById(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/KP-23/remotelink/10001", + "", nil). Return(&http.Request{}, errors.New("unable to create the http request")) @@ -951,12 +937,13 @@ func Test_internalRemoteLinkImpl_DeleteById(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/KP-23/remotelink/10001", + "", nil). Return(&http.Request{}, nil) @@ -1009,7 +996,7 @@ func Test_internalRemoteLinkImpl_DeleteById(t *testing.T) { func Test_internalRemoteLinkImpl_DeleteByGlobalId(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -1031,12 +1018,13 @@ func Test_internalRemoteLinkImpl_DeleteByGlobalId(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/KP-23/remotelink?globalId=system%3Dhttp%3A%2F%2Fwww.mycompany.com%2Fsupport%26id%3D1", + "", nil). Return(&http.Request{}, nil) @@ -1060,12 +1048,13 @@ func Test_internalRemoteLinkImpl_DeleteByGlobalId(t *testing.T) { fields: fields{version: "3"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/KP-23/remotelink?globalId=system%3Dhttp%3A%2F%2Fwww.mycompany.com%2Fsupport%26id%3D1", + "", nil). Return(&http.Request{}, nil) @@ -1110,12 +1099,13 @@ func Test_internalRemoteLinkImpl_DeleteByGlobalId(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/KP-23/remotelink?globalId=system%3Dhttp%3A%2F%2Fwww.mycompany.com%2Fsupport%26id%3D1", + "", nil). Return(&http.Request{}, errors.New("unable to create the http request")) @@ -1135,12 +1125,13 @@ func Test_internalRemoteLinkImpl_DeleteByGlobalId(t *testing.T) { fields: fields{version: "2"}, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/KP-23/remotelink?globalId=system%3Dhttp%3A%2F%2Fwww.mycompany.com%2Fsupport%26id%3D1", + "", nil). Return(&http.Request{}, nil) From f465fc1810fb8eb5520bae59e62bc4421709720d Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sat, 8 Jul 2023 23:13:04 -0600 Subject: [PATCH 22/70] :art: Migrated the client interface from Client to Connector on the Jira Link Type service. --- jira/internal/link_type_impl.go | 25 ++----- jira/internal/link_type_impl_test.go | 104 +++++++++++++-------------- 2 files changed, 59 insertions(+), 70 deletions(-) diff --git a/jira/internal/link_type_impl.go b/jira/internal/link_type_impl.go index 73273eb7..961e242b 100644 --- a/jira/internal/link_type_impl.go +++ b/jira/internal/link_type_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewLinkTypeService(client service.Client, version string) (*LinkTypeService, error) { +func NewLinkTypeService(client service.Connector, version string) (*LinkTypeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -35,7 +35,6 @@ func (l *LinkTypeService) Gets(ctx context.Context) (*model.IssueLinkTypeSearchS // Get returns an issue link type. // -// // GET /rest/api/{2-3}/issueLinkType/{issueLinkTypeId} // // https://docs.go-atlassian.io/jira-software-cloud/issues/link/types#get-issue-link-type @@ -75,7 +74,7 @@ func (l *LinkTypeService) Delete(ctx context.Context, issueLinkTypeId string) (* } type internalLinkTypeImpl struct { - c service.Client + c service.Connector version string } @@ -83,7 +82,7 @@ func (i *internalLinkTypeImpl) Gets(ctx context.Context) (*model.IssueLinkTypeSe endpoint := fmt.Sprintf("rest/api/%v/issueLinkType", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -105,7 +104,7 @@ func (i *internalLinkTypeImpl) Get(ctx context.Context, issueLinkTypeId string) endpoint := fmt.Sprintf("rest/api/%v/issueLinkType/%v", i.version, issueLinkTypeId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -121,14 +120,9 @@ func (i *internalLinkTypeImpl) Get(ctx context.Context, issueLinkTypeId string) func (i *internalLinkTypeImpl) Create(ctx context.Context, payload *model.LinkTypeScheme) (*model.LinkTypeScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issueLinkType", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -148,14 +142,9 @@ func (i *internalLinkTypeImpl) Update(ctx context.Context, issueLinkTypeId strin return nil, nil, model.ErrNoLinkTypeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issueLinkType/%v", i.version, issueLinkTypeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -177,7 +166,7 @@ func (i *internalLinkTypeImpl) Delete(ctx context.Context, issueLinkTypeId strin endpoint := fmt.Sprintf("rest/api/%v/issueLinkType/%v", i.version, issueLinkTypeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/link_type_impl_test.go b/jira/internal/link_type_impl_test.go index 77d3f8b4..35b3e969 100644 --- a/jira/internal/link_type_impl_test.go +++ b/jira/internal/link_type_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalLinkTypeImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -39,12 +38,13 @@ func Test_internalLinkTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issueLinkType", + "", nil). Return(&http.Request{}, nil) @@ -67,12 +67,13 @@ func Test_internalLinkTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issueLinkType", + "", nil). Return(&http.Request{}, nil) @@ -95,12 +96,13 @@ func Test_internalLinkTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issueLinkType", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -145,7 +147,7 @@ func Test_internalLinkTypeImpl_Gets(t *testing.T) { func Test_internalLinkTypeImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -171,12 +173,13 @@ func Test_internalLinkTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issueLinkType/1002", + "", nil). Return(&http.Request{}, nil) @@ -200,12 +203,13 @@ func Test_internalLinkTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issueLinkType/1002", + "", nil). Return(&http.Request{}, nil) @@ -240,12 +244,13 @@ func Test_internalLinkTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issueLinkType/1002", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -296,7 +301,7 @@ func Test_internalLinkTypeImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -314,6 +319,16 @@ func Test_internalLinkTypeImpl_Update(t *testing.T) { wantErr bool Err error }{ + { + name: "when the issue link type id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueLinkTypeId: "", + }, + wantErr: true, + Err: model.ErrNoLinkTypeIDError, + }, { name: "when the api version is v3", fields: fields{version: "3"}, @@ -324,17 +339,14 @@ func Test_internalLinkTypeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issueLinkType/1002", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -358,17 +370,14 @@ func Test_internalLinkTypeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issueLinkType/1002", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -392,17 +401,14 @@ func Test_internalLinkTypeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issueLinkType/1002", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -452,7 +458,7 @@ func Test_internalLinkTypeImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -478,17 +484,14 @@ func Test_internalLinkTypeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issueLinkType", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -511,17 +514,14 @@ func Test_internalLinkTypeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issueLinkType", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -544,17 +544,14 @@ func Test_internalLinkTypeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issueLinkType", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -598,7 +595,7 @@ func Test_internalLinkTypeImpl_Create(t *testing.T) { func Test_internalLinkTypeImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -624,12 +621,13 @@ func Test_internalLinkTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issueLinkType/1002", + "", nil). Return(&http.Request{}, nil) @@ -653,12 +651,13 @@ func Test_internalLinkTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issueLinkType/1002", + "", nil). Return(&http.Request{}, nil) @@ -693,12 +692,13 @@ func Test_internalLinkTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issueLinkType/1002", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -742,7 +742,7 @@ func Test_internalLinkTypeImpl_Delete(t *testing.T) { func Test_NewLinkTypeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From f30411d9851ea5b9000769f046dbce1c3e3afd60 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 01:20:10 -0600 Subject: [PATCH 23/70] :art: Migrated the client interface from Client to Connector on the Jira Metadata service. --- jira/internal/metadata_impl.go | 8 ++++---- jira/internal/metadata_impl_test.go | 24 +++++++++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/jira/internal/metadata_impl.go b/jira/internal/metadata_impl.go index af3630cc..33e7b9d4 100644 --- a/jira/internal/metadata_impl.go +++ b/jira/internal/metadata_impl.go @@ -11,7 +11,7 @@ import ( "net/url" ) -func NewMetadataService(client service.Client, version string) (*MetadataService, error) { +func NewMetadataService(client service.Connector, version string) (*MetadataService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -49,7 +49,7 @@ func (m *MetadataService) Create(ctx context.Context, opts *model.IssueMetadataC } type internalMetadataImpl struct { - c service.Client + c service.Connector version string } @@ -65,7 +65,7 @@ func (i *internalMetadataImpl) Get(ctx context.Context, issueKeyOrId string, ove endpoint := fmt.Sprintf("rest/api/%v/issue/%v/editmeta?%v", i.version, issueKeyOrId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return gjson.Result{}, nil, err } @@ -104,7 +104,7 @@ func (i *internalMetadataImpl) Create(ctx context.Context, opts *model.IssueMeta endpoint := fmt.Sprintf("rest/api/%v/issue/createmeta?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return gjson.Result{}, nil, err } diff --git a/jira/internal/metadata_impl_test.go b/jira/internal/metadata_impl_test.go index 7081c731..17f8a1d1 100644 --- a/jira/internal/metadata_impl_test.go +++ b/jira/internal/metadata_impl_test.go @@ -15,7 +15,7 @@ import ( func Test_internalMetadataImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -46,12 +46,13 @@ func Test_internalMetadataImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-4/editmeta?overrideEditableFlag=false&overrideScreenSecurity=true", + "", nil). Return(&http.Request{}, nil) @@ -77,12 +78,13 @@ func Test_internalMetadataImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-4/editmeta?overrideEditableFlag=false&overrideScreenSecurity=true", + "", nil). Return(&http.Request{}, nil) @@ -122,12 +124,13 @@ func Test_internalMetadataImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-4/editmeta?overrideEditableFlag=false&overrideScreenSecurity=true", + "", nil). Return(&http.Request{}, errors.New("error")) @@ -174,7 +177,7 @@ func Test_internalMetadataImpl_Get(t *testing.T) { func Test_internalMetadataImpl_Create(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -207,12 +210,13 @@ func Test_internalMetadataImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/createmeta?expand=projects.issuetypes.fields&issuetypeIds=1&issuetypeIds=2&issuetypeNames=Story&issuetypeNames=Bug&projectIds=1002&projectKeys=DUMMY", + "", nil). Return(&http.Request{}, nil) @@ -242,12 +246,13 @@ func Test_internalMetadataImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/createmeta?expand=projects.issuetypes.fields&issuetypeIds=1&issuetypeIds=2&issuetypeNames=Story&issuetypeNames=Bug&projectIds=1002&projectKeys=DUMMY", + "", nil). Return(&http.Request{}, nil) @@ -277,12 +282,13 @@ func Test_internalMetadataImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/createmeta?expand=projects.issuetypes.fields&issuetypeIds=1&issuetypeIds=2&issuetypeNames=Story&issuetypeNames=Bug&projectIds=1002&projectKeys=DUMMY", + "", nil). Return(&http.Request{}, errors.New("error")) @@ -328,7 +334,7 @@ func Test_internalMetadataImpl_Create(t *testing.T) { func Test_NewMetadataService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 5430b482c85248fdc8e35635b202982d7d168c70 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 01:21:10 -0600 Subject: [PATCH 24/70] :memo: Updated the Issue.Metadata.* methods documentation links. --- jira/internal/metadata_impl.go | 4 ++-- service/jira/metadata.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/jira/internal/metadata_impl.go b/jira/internal/metadata_impl.go index 33e7b9d4..4d95cbf9 100644 --- a/jira/internal/metadata_impl.go +++ b/jira/internal/metadata_impl.go @@ -32,7 +32,7 @@ type MetadataService struct { // // GET /rest/api/{2-3}/issue/{issueIdOrKey}/editmeta // -// TODO: the documentation needs to be created +// https://docs.go-atlassian.io/jira-software-cloud/issues/metadata#get-edit-issue-metadata func (m *MetadataService) Get(ctx context.Context, issueKeyOrId string, overrideScreenSecurity, overrideEditableFlag bool) (gjson.Result, *model.ResponseScheme, error) { return m.internalClient.Get(ctx, issueKeyOrId, overrideScreenSecurity, overrideEditableFlag) } @@ -43,7 +43,7 @@ func (m *MetadataService) Get(ctx context.Context, issueKeyOrId string, override // // GET /rest/api/{2-3}/issue/createmeta // -// TODO: the documentation needs to be created +// https://docs.go-atlassian.io/jira-software-cloud/issues/metadata#get-create-issue-metadata func (m *MetadataService) Create(ctx context.Context, opts *model.IssueMetadataCreateOptions) (gjson.Result, *model.ResponseScheme, error) { return m.internalClient.Create(ctx, opts) } diff --git a/service/jira/metadata.go b/service/jira/metadata.go index 8c083efa..2937ca42 100644 --- a/service/jira/metadata.go +++ b/service/jira/metadata.go @@ -14,7 +14,7 @@ type MetadataConnector interface { // // GET /rest/api/{2-3}/issue/{issueIdOrKey}/editmeta // - // TODO: the documentation needs to be created + // https://docs.go-atlassian.io/jira-software-cloud/issues/metadata#get-edit-issue-metadata Get(ctx context.Context, issueKeyOrId string, overrideScreenSecurity, overrideEditableFlag bool) (gjson.Result, *model.ResponseScheme, error) // Create returns details of projects, issue types within projects, and, when requested, @@ -23,6 +23,6 @@ type MetadataConnector interface { // // GET /rest/api/{2-3}/issue/createmeta // - // TODO: the documentation needs to be created + // https://docs.go-atlassian.io/jira-software-cloud/issues/metadata#get-create-issue-metadata Create(ctx context.Context, opts *model.IssueMetadataCreateOptions) (gjson.Result, *model.ResponseScheme, error) } From 351b711279ea1f88f19dda403eee1d05f99b2070 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 01:34:03 -0600 Subject: [PATCH 25/70] :art: Migrated the client interface from Client to Connector on the Jira Priority service. --- jira/internal/priority_impl.go | 8 ++++---- jira/internal/priority_impl_test.go | 30 ++++++++++++++--------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/jira/internal/priority_impl.go b/jira/internal/priority_impl.go index 9b0a26f0..50f45559 100644 --- a/jira/internal/priority_impl.go +++ b/jira/internal/priority_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewPriorityService(client service.Client, version string) (*PriorityService, error) { +func NewPriorityService(client service.Connector, version string) (*PriorityService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -43,7 +43,7 @@ func (p *PriorityService) Get(ctx context.Context, priorityId string) (*model.Pr } type internalPriorityImpl struct { - c service.Client + c service.Connector version string } @@ -51,7 +51,7 @@ func (i *internalPriorityImpl) Gets(ctx context.Context) ([]*model.PrioritySchem endpoint := fmt.Sprintf("rest/api/%v/priority", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -73,7 +73,7 @@ func (i *internalPriorityImpl) Get(ctx context.Context, priorityId string) (*mod endpoint := fmt.Sprintf("rest/api/%v/priority/%v", i.version, priorityId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/priority_impl_test.go b/jira/internal/priority_impl_test.go index 82ab7d18..e5fb208c 100644 --- a/jira/internal/priority_impl_test.go +++ b/jira/internal/priority_impl_test.go @@ -15,7 +15,7 @@ import ( func Test_internalPriorityImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -39,13 +39,13 @@ func Test_internalPriorityImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/priority", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -67,13 +67,13 @@ func Test_internalPriorityImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/priority", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -95,13 +95,13 @@ func Test_internalPriorityImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/priority", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -145,7 +145,7 @@ func Test_internalPriorityImpl_Gets(t *testing.T) { func Test_internalPriorityImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -171,13 +171,13 @@ func Test_internalPriorityImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/priority/2", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -200,13 +200,13 @@ func Test_internalPriorityImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/priority/2", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -239,13 +239,13 @@ func Test_internalPriorityImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/priority/2", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -289,7 +289,7 @@ func Test_internalPriorityImpl_Get(t *testing.T) { func Test_NewPriorityService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From f46c0fb1c59d1530c0e7e59fa6a89eabf0f55895 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 01:38:48 -0600 Subject: [PATCH 26/70] :art: Migrated the client interface from Client to Connector on the Jira Resolution service. --- jira/internal/resolution_impl.go | 9 ++++---- jira/internal/resolution_impl_test.go | 30 +++++++++++++-------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/jira/internal/resolution_impl.go b/jira/internal/resolution_impl.go index 17b833b4..86e0b741 100644 --- a/jira/internal/resolution_impl.go +++ b/jira/internal/resolution_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewResolutionService(client service.Client, version string) (*ResolutionService, error) { +func NewResolutionService(client service.Connector, version string) (*ResolutionService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -35,7 +35,6 @@ func (r *ResolutionService) Gets(ctx context.Context) ([]*model.ResolutionScheme // Get returns an issue resolution value. // -// // GET /rest/api/{2-3}/resolution/{id} // // https://docs.go-atlassian.io/jira-software-cloud/issues/resolutions#get-resolution @@ -44,7 +43,7 @@ func (r *ResolutionService) Get(ctx context.Context, resolutionId string) (*mode } type internalResolutionImpl struct { - c service.Client + c service.Connector version string } @@ -52,7 +51,7 @@ func (i *internalResolutionImpl) Gets(ctx context.Context) ([]*model.ResolutionS endpoint := fmt.Sprintf("rest/api/%v/resolution", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -74,7 +73,7 @@ func (i *internalResolutionImpl) Get(ctx context.Context, resolutionId string) ( endpoint := fmt.Sprintf("rest/api/%v/resolution/%v", i.version, resolutionId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/resolution_impl_test.go b/jira/internal/resolution_impl_test.go index c0aeb405..6c48fddc 100644 --- a/jira/internal/resolution_impl_test.go +++ b/jira/internal/resolution_impl_test.go @@ -15,7 +15,7 @@ import ( func Test_internalResolutionImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -39,13 +39,13 @@ func Test_internalResolutionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/resolution", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -67,13 +67,13 @@ func Test_internalResolutionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/resolution", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -95,13 +95,13 @@ func Test_internalResolutionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/resolution", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -145,7 +145,7 @@ func Test_internalResolutionImpl_Gets(t *testing.T) { func Test_internalResolutionImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -171,13 +171,13 @@ func Test_internalResolutionImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/resolution/2", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -200,13 +200,13 @@ func Test_internalResolutionImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/resolution/2", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -239,13 +239,13 @@ func Test_internalResolutionImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/resolution/2", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -288,7 +288,7 @@ func Test_internalResolutionImpl_Get(t *testing.T) { func Test_NewResolutionService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From c6f875c01b1bd571ba86b6b6837ac627b153671b Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 01:45:50 -0600 Subject: [PATCH 27/70] :art: Migrated the client interface from Client to Connector on the Jira Search service. --- jira/internal/search_impl.go | 2 +- jira/internal/search_impl_adf.go | 18 ++---- jira/internal/search_impl_adf_test.go | 69 +++++++-------------- jira/internal/search_impl_rich_text.go | 18 ++---- jira/internal/search_impl_rich_text_test.go | 69 +++++++-------------- jira/internal/search_impl_test.go | 2 +- 6 files changed, 54 insertions(+), 124 deletions(-) diff --git a/jira/internal/search_impl.go b/jira/internal/search_impl.go index 56e7bb34..7e80e548 100644 --- a/jira/internal/search_impl.go +++ b/jira/internal/search_impl.go @@ -5,7 +5,7 @@ import ( "github.com/ctreminiom/go-atlassian/service" ) -func NewSearchService(client service.Client, version string) (*SearchADFService, *SearchRichTextService, error) { +func NewSearchService(client service.Connector, version string) (*SearchADFService, *SearchRichTextService, error) { if version == "" { return nil, nil, model.ErrNoVersionProvided diff --git a/jira/internal/search_impl_adf.go b/jira/internal/search_impl_adf.go index fa034bed..fe696ac0 100644 --- a/jira/internal/search_impl_adf.go +++ b/jira/internal/search_impl_adf.go @@ -44,20 +44,15 @@ func (s *SearchADFService) Post(ctx context.Context, jql string, fields, expands } type internalSearchADFImpl struct { - c service.Client + c service.Connector version string } func (i *internalSearchADFImpl) Checks(ctx context.Context, payload *model.IssueSearchCheckPayloadScheme) (*model.IssueMatchesPageScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/jql/match", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -96,7 +91,7 @@ func (i *internalSearchADFImpl) Get(ctx context.Context, jql string, fields, exp endpoint := fmt.Sprintf("rest/api/%v/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -128,14 +123,9 @@ func (i *internalSearchADFImpl) Post(ctx context.Context, jql string, fields, ex ValidateQuery: validate, } - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/search", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/search_impl_adf_test.go b/jira/internal/search_impl_adf_test.go index 25037768..a5c0227a 100644 --- a/jira/internal/search_impl_adf_test.go +++ b/jira/internal/search_impl_adf_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -24,7 +23,7 @@ func Test_internalSearchADFImpl_Checks(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -50,17 +49,13 @@ func Test_internalSearchADFImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/jql/match", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -83,17 +78,13 @@ func Test_internalSearchADFImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/jql/match", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -116,17 +107,13 @@ func Test_internalSearchADFImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/jql/match", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -169,7 +156,7 @@ func Test_internalSearchADFImpl_Checks(t *testing.T) { func Test_internalSearchADFImpl_Post(t *testing.T) { - payloadMocked := &struct { + payloadMocked := struct { Expand []string "json:\"expand,omitempty\"" Jql string "json:\"jql,omitempty\"" MaxResults int "json:\"maxResults,omitempty\"" @@ -179,7 +166,7 @@ func Test_internalSearchADFImpl_Post(t *testing.T) { }{Expand: []string{"operations"}, Jql: "project = FOO", MaxResults: 100, Fields: []string{"status", "summary"}, StartAt: 50, ValidateQuery: "strict"} type fields struct { - c service.Client + c service.Connector version string } @@ -213,17 +200,13 @@ func Test_internalSearchADFImpl_Post(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/search", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -251,17 +234,13 @@ func Test_internalSearchADFImpl_Post(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/search", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -289,17 +268,13 @@ func Test_internalSearchADFImpl_Post(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/search", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -344,7 +319,7 @@ func Test_internalSearchADFImpl_Post(t *testing.T) { func Test_internalSearchADFImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -378,13 +353,13 @@ func Test_internalSearchADFImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/search?expand=operations&fields=status%2Csummary&jql=project+%3D+FOO&maxResults=100&startAt=50&validateQuery=strict", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -412,13 +387,13 @@ func Test_internalSearchADFImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/search?expand=operations&fields=status%2Csummary&jql=project+%3D+FOO&maxResults=100&startAt=50&validateQuery=strict", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -446,13 +421,13 @@ func Test_internalSearchADFImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/search?expand=operations&fields=status%2Csummary&jql=project+%3D+FOO&maxResults=100&startAt=50&validateQuery=strict", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client diff --git a/jira/internal/search_impl_rich_text.go b/jira/internal/search_impl_rich_text.go index 94516b20..b19ec92d 100644 --- a/jira/internal/search_impl_rich_text.go +++ b/jira/internal/search_impl_rich_text.go @@ -44,20 +44,15 @@ func (s *SearchRichTextService) Post(ctx context.Context, jql string, fields, ex } type internalSearchRichTextImpl struct { - c service.Client + c service.Connector version string } func (i *internalSearchRichTextImpl) Checks(ctx context.Context, payload *model.IssueSearchCheckPayloadScheme) (*model.IssueMatchesPageScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/jql/match", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -96,7 +91,7 @@ func (i *internalSearchRichTextImpl) Get(ctx context.Context, jql string, fields endpoint := fmt.Sprintf("rest/api/%v/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -128,14 +123,9 @@ func (i *internalSearchRichTextImpl) Post(ctx context.Context, jql string, field ValidateQuery: validate, } - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/search", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/search_impl_rich_text_test.go b/jira/internal/search_impl_rich_text_test.go index 519dced6..60c7147a 100644 --- a/jira/internal/search_impl_rich_text_test.go +++ b/jira/internal/search_impl_rich_text_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -24,7 +23,7 @@ func Test_internalSearchRichTextImpl_Checks(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -50,17 +49,13 @@ func Test_internalSearchRichTextImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/jql/match", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -83,17 +78,13 @@ func Test_internalSearchRichTextImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/jql/match", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -116,17 +107,13 @@ func Test_internalSearchRichTextImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/jql/match", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -169,7 +156,7 @@ func Test_internalSearchRichTextImpl_Checks(t *testing.T) { func Test_internalSearchRichTextImpl_Post(t *testing.T) { - payloadMocked := &struct { + payloadMocked := struct { Expand []string "json:\"expand,omitempty\"" Jql string "json:\"jql,omitempty\"" MaxResults int "json:\"maxResults,omitempty\"" @@ -179,7 +166,7 @@ func Test_internalSearchRichTextImpl_Post(t *testing.T) { }{Expand: []string{"operations"}, Jql: "project = FOO", MaxResults: 100, Fields: []string{"status", "summary"}, StartAt: 50, ValidateQuery: "strict"} type fields struct { - c service.Client + c service.Connector version string } @@ -213,17 +200,13 @@ func Test_internalSearchRichTextImpl_Post(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/search", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -251,17 +234,13 @@ func Test_internalSearchRichTextImpl_Post(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/search", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -289,17 +268,13 @@ func Test_internalSearchRichTextImpl_Post(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/search", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -344,7 +319,7 @@ func Test_internalSearchRichTextImpl_Post(t *testing.T) { func Test_internalSearchRichTextImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -378,13 +353,13 @@ func Test_internalSearchRichTextImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/search?expand=operations&fields=status%2Csummary&jql=project+%3D+FOO&maxResults=100&startAt=50&validateQuery=strict", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -412,13 +387,13 @@ func Test_internalSearchRichTextImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/search?expand=operations&fields=status%2Csummary&jql=project+%3D+FOO&maxResults=100&startAt=50&validateQuery=strict", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -446,13 +421,13 @@ func Test_internalSearchRichTextImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/search?expand=operations&fields=status%2Csummary&jql=project+%3D+FOO&maxResults=100&startAt=50&validateQuery=strict", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client diff --git a/jira/internal/search_impl_test.go b/jira/internal/search_impl_test.go index e8e0a63a..c18d4641 100644 --- a/jira/internal/search_impl_test.go +++ b/jira/internal/search_impl_test.go @@ -10,7 +10,7 @@ import ( func Test_NewSearchService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 7dfc018ca916a5e27083f3005ec223cb60ae584a Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 21:59:10 -0600 Subject: [PATCH 28/70] :art: Migrated the client interface from Client to Connector on the Jira IssueType service. --- jira/internal/type_impl.go | 26 +++----- jira/internal/type_impl_test.go | 111 +++++++++++++------------------- 2 files changed, 51 insertions(+), 86 deletions(-) diff --git a/jira/internal/type_impl.go b/jira/internal/type_impl.go index 4a912711..41a6d6f4 100644 --- a/jira/internal/type_impl.go +++ b/jira/internal/type_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewTypeService(client service.Client, version string, scheme *TypeSchemeService, screenScheme *TypeScreenSchemeService) ( +func NewTypeService(client service.Connector, version string, scheme *TypeSchemeService, screenScheme *TypeScreenSchemeService) ( *TypeService, error) { if version == "" { @@ -89,7 +89,7 @@ func (t *TypeService) Alternatives(ctx context.Context, issueTypeId string) ([]* } type internalTypeImpl struct { - c service.Client + c service.Connector version string } @@ -97,7 +97,7 @@ func (i *internalTypeImpl) Gets(ctx context.Context) ([]*model.IssueTypeScheme, endpoint := fmt.Sprintf("rest/api/%v/issuetype", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -113,14 +113,9 @@ func (i *internalTypeImpl) Gets(ctx context.Context) ([]*model.IssueTypeScheme, func (i *internalTypeImpl) Create(ctx context.Context, payload *model.IssueTypePayloadScheme) (*model.IssueTypeScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issuetype", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -142,7 +137,7 @@ func (i *internalTypeImpl) Get(ctx context.Context, issueTypeId string) (*model. endpoint := fmt.Sprintf("rest/api/%v/issuetype/%v", i.version, issueTypeId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -162,14 +157,9 @@ func (i *internalTypeImpl) Update(ctx context.Context, issueTypeId string, paylo return nil, nil, model.ErrNoIssueTypeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issuetype/%v", i.version, issueTypeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -191,7 +181,7 @@ func (i *internalTypeImpl) Delete(ctx context.Context, issueTypeId string) (*mod endpoint := fmt.Sprintf("rest/api/%v/issuetype/%v", i.version, issueTypeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -203,7 +193,7 @@ func (i *internalTypeImpl) Alternatives(ctx context.Context, issueTypeId string) endpoint := fmt.Sprintf("rest/api/%v/issuetype/%v/alternatives", i.version, issueTypeId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/type_impl_test.go b/jira/internal/type_impl_test.go index 6905c72d..3ed4ed7a 100644 --- a/jira/internal/type_impl_test.go +++ b/jira/internal/type_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalTypeImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,13 +39,13 @@ func Test_internalTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetype", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -68,13 +67,13 @@ func Test_internalTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetype", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -96,13 +95,13 @@ func Test_internalTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetype", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -146,7 +145,7 @@ func Test_internalTypeImpl_Gets(t *testing.T) { func Test_internalTypeImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -172,13 +171,13 @@ func Test_internalTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetype/8", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -201,13 +200,13 @@ func Test_internalTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetype/8", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -240,13 +239,13 @@ func Test_internalTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetype/8", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -290,7 +289,7 @@ func Test_internalTypeImpl_Get(t *testing.T) { func Test_internalTypeImpl_Alternatives(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -316,13 +315,13 @@ func Test_internalTypeImpl_Alternatives(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetype/8/alternatives", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -345,13 +344,13 @@ func Test_internalTypeImpl_Alternatives(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetype/8/alternatives", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -374,13 +373,13 @@ func Test_internalTypeImpl_Alternatives(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetype/8/alternatives", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -430,7 +429,7 @@ func Test_internalTypeImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -456,17 +455,13 @@ func Test_internalTypeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issuetype", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -489,17 +484,13 @@ func Test_internalTypeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issuetype", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -522,17 +513,13 @@ func Test_internalTypeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issuetype", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -582,7 +569,7 @@ func Test_internalTypeImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -610,17 +597,13 @@ func Test_internalTypeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetype/8", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -644,17 +627,13 @@ func Test_internalTypeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issuetype/8", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -678,17 +657,13 @@ func Test_internalTypeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetype/8", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -732,7 +707,7 @@ func Test_internalTypeImpl_Update(t *testing.T) { func Test_internalTypeImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -758,13 +733,13 @@ func Test_internalTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issuetype/8", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -787,13 +762,13 @@ func Test_internalTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issuetype/8", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -826,13 +801,13 @@ func Test_internalTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issuetype/8", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -875,7 +850,7 @@ func Test_internalTypeImpl_Delete(t *testing.T) { func Test_NewTypeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From dbb7793f39e446c27edeaf039680eee8514fed50 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 22:21:16 -0600 Subject: [PATCH 29/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Type Scheme service. --- jira/internal/type_scheme_impl.go | 58 ++------ jira/internal/type_scheme_impl_test.go | 186 +++++++++---------------- 2 files changed, 80 insertions(+), 164 deletions(-) diff --git a/jira/internal/type_scheme_impl.go b/jira/internal/type_scheme_impl.go index e996fe96..2687432d 100644 --- a/jira/internal/type_scheme_impl.go +++ b/jira/internal/type_scheme_impl.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewTypeSchemeService(client service.Client, version string) (*TypeSchemeService, error) { +func NewTypeSchemeService(client service.Connector, version string) (*TypeSchemeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -122,7 +122,7 @@ func (t *TypeSchemeService) Remove(ctx context.Context, issueTypeSchemeId, issue } type internalTypeSchemeImpl struct { - c service.Client + c service.Connector version string } @@ -138,7 +138,7 @@ func (i *internalTypeSchemeImpl) Gets(ctx context.Context, issueTypeSchemeIds [] endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -154,14 +154,9 @@ func (i *internalTypeSchemeImpl) Gets(ctx context.Context, issueTypeSchemeIds [] func (i *internalTypeSchemeImpl) Create(ctx context.Context, payload *model.IssueTypeSchemePayloadScheme) (*model.NewIssueTypeSchemeScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -187,7 +182,7 @@ func (i *internalTypeSchemeImpl) Items(ctx context.Context, issueTypeSchemeIds [ endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme/mapping?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -213,7 +208,7 @@ func (i *internalTypeSchemeImpl) Projects(ctx context.Context, projectIds []int, endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme/project?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -237,22 +232,13 @@ func (i *internalTypeSchemeImpl) Assign(ctx context.Context, issueTypeSchemeId, return nil, model.ErrNoProjectIDError } - payload := struct { - IssueTypeSchemeID string `json:"issueTypeSchemeId"` - ProjectID string `json:"projectId"` - }{ - IssueTypeSchemeID: issueTypeSchemeId, - ProjectID: projectId, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err - } + payload := map[string]interface{}{ + "issueTypeSchemeId": issueTypeSchemeId, + "projectId": projectId} endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme/project", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -266,14 +252,9 @@ func (i *internalTypeSchemeImpl) Update(ctx context.Context, issueTypeSchemeId i return nil, model.ErrNoIssueTypeSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme/%v", i.version, issueTypeSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -289,7 +270,7 @@ func (i *internalTypeSchemeImpl) Delete(ctx context.Context, issueTypeSchemeId i endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme/%v", i.version, issueTypeSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -308,20 +289,9 @@ func (i *internalTypeSchemeImpl) Append(ctx context.Context, issueTypeSchemeId i ids = append(ids, strconv.Itoa(issueTypeID)) } - payload := struct { - IssueTypeIds []string `json:"issueTypeIds"` - }{ - IssueTypeIds: ids, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme/%v/issuetype", i.version, issueTypeSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"issueTypeIds": ids}) if err != nil { return nil, err } @@ -341,7 +311,7 @@ func (i *internalTypeSchemeImpl) Remove(ctx context.Context, issueTypeSchemeId, endpoint := fmt.Sprintf("rest/api/%v/issuetypescheme/%v/issuetype/%v", i.version, issueTypeSchemeId, issueTypeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/type_scheme_impl_test.go b/jira/internal/type_scheme_impl_test.go index 6b8d2f21..b6a14dd0 100644 --- a/jira/internal/type_scheme_impl_test.go +++ b/jira/internal/type_scheme_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalTypeSchemeImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -44,13 +43,13 @@ func Test_internalTypeSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescheme?id=1001&id=1002&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -75,13 +74,13 @@ func Test_internalTypeSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetypescheme?id=1001&id=1002&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -106,13 +105,13 @@ func Test_internalTypeSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescheme?id=1001&id=1002&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -157,7 +156,7 @@ func Test_internalTypeSchemeImpl_Gets(t *testing.T) { func Test_internalTypeSchemeImpl_Items(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -186,13 +185,13 @@ func Test_internalTypeSchemeImpl_Items(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescheme/mapping?issueTypeSchemeId=1001&issueTypeSchemeId=1002&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -217,13 +216,13 @@ func Test_internalTypeSchemeImpl_Items(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetypescheme/mapping?issueTypeSchemeId=1001&issueTypeSchemeId=1002&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -248,13 +247,13 @@ func Test_internalTypeSchemeImpl_Items(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescheme/mapping?issueTypeSchemeId=1001&issueTypeSchemeId=1002&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -299,7 +298,7 @@ func Test_internalTypeSchemeImpl_Items(t *testing.T) { func Test_internalTypeSchemeImpl_Projects(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -328,13 +327,13 @@ func Test_internalTypeSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescheme/project?maxResults=100&projectId=1001&projectId=1002&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -359,13 +358,13 @@ func Test_internalTypeSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetypescheme/project?maxResults=100&projectId=1001&projectId=1002&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -390,13 +389,13 @@ func Test_internalTypeSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescheme/project?maxResults=100&projectId=1001&projectId=1002&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -448,7 +447,7 @@ func Test_internalTypeSchemeImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -474,17 +473,13 @@ func Test_internalTypeSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issuetypescheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -507,17 +502,13 @@ func Test_internalTypeSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issuetypescheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -540,17 +531,13 @@ func Test_internalTypeSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issuetypescheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -601,7 +588,7 @@ func Test_internalTypeSchemeImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -629,17 +616,13 @@ func Test_internalTypeSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -663,17 +646,13 @@ func Test_internalTypeSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issuetypescheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -708,17 +687,13 @@ func Test_internalTypeSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -761,12 +736,10 @@ func Test_internalTypeSchemeImpl_Update(t *testing.T) { func Test_internalTypeSchemeImpl_Append(t *testing.T) { - payloadMocked := &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"8", "10", "2"}} + payloadMocked := map[string]interface{}{"issueTypeIds": []string{"8", "10", "2"}} type fields struct { - c service.Client + c service.Connector version string } @@ -794,17 +767,13 @@ func Test_internalTypeSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescheme/10001/issuetype", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -828,17 +797,13 @@ func Test_internalTypeSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issuetypescheme/10001/issuetype", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -862,17 +827,13 @@ func Test_internalTypeSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescheme/10001/issuetype", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -916,7 +877,7 @@ func Test_internalTypeSchemeImpl_Append(t *testing.T) { func Test_internalTypeSchemeImpl_Remove(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -943,13 +904,13 @@ func Test_internalTypeSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issuetypescheme/10001/issuetype/9", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -973,13 +934,13 @@ func Test_internalTypeSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issuetypescheme/10001/issuetype/9", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1003,13 +964,13 @@ func Test_internalTypeSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issuetypescheme/10001/issuetype/9", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1052,7 +1013,7 @@ func Test_internalTypeSchemeImpl_Remove(t *testing.T) { func Test_internalTypeSchemeImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -1078,13 +1039,13 @@ func Test_internalTypeSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issuetypescheme/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1107,13 +1068,13 @@ func Test_internalTypeSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issuetypescheme/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1136,13 +1097,13 @@ func Test_internalTypeSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issuetypescheme/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1184,13 +1145,10 @@ func Test_internalTypeSchemeImpl_Delete(t *testing.T) { func Test_internalTypeSchemeImpl_Assign(t *testing.T) { - payloadMocked := &struct { - IssueTypeSchemeID string "json:\"issueTypeSchemeId\"" - ProjectID string "json:\"projectId\"" - }{IssueTypeSchemeID: "10001", ProjectID: "9"} + payloadMocked := map[string]interface{}{"issueTypeSchemeId": "10001", "projectId": "9"} type fields struct { - c service.Client + c service.Connector version string } @@ -1217,17 +1175,13 @@ func Test_internalTypeSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1251,17 +1205,13 @@ func Test_internalTypeSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issuetypescheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1307,17 +1257,13 @@ func Test_internalTypeSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1360,7 +1306,7 @@ func Test_internalTypeSchemeImpl_Assign(t *testing.T) { func Test_NewTypeSchemeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From eb5c2b012dce4c834850ca8cb3b13c9dcb1b24d2 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 22:51:38 -0600 Subject: [PATCH 30/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Type Screen Scheme service. --- jira/internal/type_screen_scheme_impl.go | 86 ++--- jira/internal/type_screen_scheme_impl_test.go | 351 +++++++++--------- 2 files changed, 206 insertions(+), 231 deletions(-) diff --git a/jira/internal/type_screen_scheme_impl.go b/jira/internal/type_screen_scheme_impl.go index b7c3b1fb..1d7e0d99 100644 --- a/jira/internal/type_screen_scheme_impl.go +++ b/jira/internal/type_screen_scheme_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewTypeScreenSchemeService(client service.Client, version string) (*TypeScreenSchemeService, error) { +func NewTypeScreenSchemeService(client service.Connector, version string) (*TypeScreenSchemeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -135,7 +135,7 @@ func (t *TypeScreenSchemeService) SchemesByProject(ctx context.Context, issueTyp } type internalTypeScreenSchemeImpl struct { - c service.Client + c service.Connector version string } @@ -166,7 +166,7 @@ func (i *internalTypeScreenSchemeImpl) Gets(ctx context.Context, options *model. endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -182,14 +182,9 @@ func (i *internalTypeScreenSchemeImpl) Gets(ctx context.Context, options *model. func (i *internalTypeScreenSchemeImpl) Create(ctx context.Context, payload *model.IssueTypeScreenSchemePayloadScheme) (*model.IssueTypeScreenScreenCreatedScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -213,22 +208,14 @@ func (i *internalTypeScreenSchemeImpl) Assign(ctx context.Context, issueTypeScre return nil, model.ErrNoProjectIDError } - payload := struct { - IssueTypeScreenSchemeID string `json:"issueTypeScreenSchemeId"` - ProjectID string `json:"projectId"` - }{ - IssueTypeScreenSchemeID: issueTypeScreenSchemeId, - ProjectID: projectId, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + payload := map[string]interface{}{ + "issueTypeScreenSchemeId": issueTypeScreenSchemeId, + "projectId": projectId, } endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/project", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -248,7 +235,7 @@ func (i *internalTypeScreenSchemeImpl) Projects(ctx context.Context, projectIds endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/project?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -274,7 +261,7 @@ func (i *internalTypeScreenSchemeImpl) Mapping(ctx context.Context, issueTypeScr endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/mapping?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -294,22 +281,15 @@ func (i *internalTypeScreenSchemeImpl) Update(ctx context.Context, issueTypeScre return nil, model.ErrNoIssueTypeScreenSchemeIDError } - payload := struct { - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - }{ - Name: name, - Description: description, - } + payload := map[string]interface{}{"name": name} - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + if description != "" { + payload["description"] = description } endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/%v", i.version, issueTypeScreenSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -325,7 +305,7 @@ func (i *internalTypeScreenSchemeImpl) Delete(ctx context.Context, issueTypeScre endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/%v", i.version, issueTypeScreenSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -339,14 +319,9 @@ func (i *internalTypeScreenSchemeImpl) Append(ctx context.Context, issueTypeScre return nil, model.ErrNoIssueTypeScreenSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/%v/mapping", i.version, issueTypeScreenSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -364,20 +339,9 @@ func (i *internalTypeScreenSchemeImpl) UpdateDefault(ctx context.Context, issueT return nil, model.ErrNoScreenSchemeIDError } - payload := struct { - ScreenSchemeID string `json:"screenSchemeId"` - }{ - ScreenSchemeID: screenSchemeId, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/%v/mapping/default", i.version, issueTypeScreenSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"screenSchemeId": screenSchemeId}) if err != nil { return nil, err } @@ -387,23 +351,17 @@ func (i *internalTypeScreenSchemeImpl) UpdateDefault(ctx context.Context, issueT func (i *internalTypeScreenSchemeImpl) Remove(ctx context.Context, issueTypeScreenSchemeId string, issueTypeIds []string) (*model.ResponseScheme, error) { - if len(issueTypeScreenSchemeId) == 0 { + if issueTypeScreenSchemeId == "" { return nil, model.ErrNoIssueTypeScreenSchemeIDError } - payload := struct { - IssueTypeIds []string `json:"issueTypeIds"` - }{ - IssueTypeIds: issueTypeIds, - } - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err + if len(issueTypeIds) == 0 { + return nil, model.ErrNoIssueTypesError } endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/%v/mapping/remove", i.version, issueTypeScreenSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"issueTypeIds": issueTypeIds}) if err != nil { return nil, err } @@ -419,7 +377,7 @@ func (i *internalTypeScreenSchemeImpl) SchemesByProject(ctx context.Context, iss endpoint := fmt.Sprintf("rest/api/%v/issuetypescreenscheme/%v/project?%v", i.version, issueTypeScreenSchemeId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/type_screen_scheme_impl_test.go b/jira/internal/type_screen_scheme_impl_test.go index b69c526c..a86e5b2b 100644 --- a/jira/internal/type_screen_scheme_impl_test.go +++ b/jira/internal/type_screen_scheme_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalTypeScreenSchemeImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -49,13 +48,13 @@ func Test_internalTypeScreenSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescreenscheme?=id&expand=expand&id=10001&id=10002&maxResults=100&queryString=query&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -85,13 +84,13 @@ func Test_internalTypeScreenSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetypescreenscheme?=id&expand=expand&id=10001&id=10002&maxResults=100&queryString=query&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -121,13 +120,13 @@ func Test_internalTypeScreenSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescreenscheme?=id&expand=expand&id=10001&id=10002&maxResults=100&queryString=query&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -172,7 +171,7 @@ func Test_internalTypeScreenSchemeImpl_Gets(t *testing.T) { func Test_internalTypeScreenSchemeImpl_Projects(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -201,13 +200,13 @@ func Test_internalTypeScreenSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescreenscheme/project?maxResults=100&projectId=29992&projectId=349383&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -232,13 +231,13 @@ func Test_internalTypeScreenSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetypescreenscheme/project?maxResults=100&projectId=29992&projectId=349383&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -263,13 +262,13 @@ func Test_internalTypeScreenSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescreenscheme/project?maxResults=100&projectId=29992&projectId=349383&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -314,7 +313,7 @@ func Test_internalTypeScreenSchemeImpl_Projects(t *testing.T) { func Test_internalTypeScreenSchemeImpl_Mapping(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -343,13 +342,13 @@ func Test_internalTypeScreenSchemeImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescreenscheme/mapping?issueTypeScreenSchemeId=29992&issueTypeScreenSchemeId=349383&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -374,13 +373,13 @@ func Test_internalTypeScreenSchemeImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetypescreenscheme/mapping?issueTypeScreenSchemeId=29992&issueTypeScreenSchemeId=349383&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -405,13 +404,13 @@ func Test_internalTypeScreenSchemeImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescreenscheme/mapping?issueTypeScreenSchemeId=29992&issueTypeScreenSchemeId=349383&maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -456,7 +455,7 @@ func Test_internalTypeScreenSchemeImpl_Mapping(t *testing.T) { func Test_internalTypeScreenSchemeImpl_SchemesByProject(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -485,13 +484,13 @@ func Test_internalTypeScreenSchemeImpl_SchemesByProject(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescreenscheme/29992/project?maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -516,13 +515,13 @@ func Test_internalTypeScreenSchemeImpl_SchemesByProject(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issuetypescreenscheme/29992/project?maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -547,13 +546,13 @@ func Test_internalTypeScreenSchemeImpl_SchemesByProject(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issuetypescreenscheme/29992/project?maxResults=100&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -597,13 +596,10 @@ func Test_internalTypeScreenSchemeImpl_SchemesByProject(t *testing.T) { func Test_internalTypeScreenSchemeImpl_Assign(t *testing.T) { - payloadMocked := &struct { - IssueTypeScreenSchemeID string "json:\"issueTypeScreenSchemeId\"" - ProjectID string "json:\"projectId\"" - }{IssueTypeScreenSchemeID: "20001", ProjectID: "848483"} + payloadMocked := map[string]interface{}{"issueTypeScreenSchemeId": "20001", "projectId": "848483"} type fields struct { - c service.Client + c service.Connector version string } @@ -630,17 +626,13 @@ func Test_internalTypeScreenSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescreenscheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -664,17 +656,13 @@ func Test_internalTypeScreenSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issuetypescreenscheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -688,6 +676,28 @@ func Test_internalTypeScreenSchemeImpl_Assign(t *testing.T) { Err: nil, }, + { + name: "when the issue type screen scheme id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "", + }, + wantErr: true, + Err: model.ErrNoIssueTypeScreenSchemeIDError, + }, + + { + name: "when the project id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "20001", + }, + wantErr: true, + Err: model.ErrNoProjectIDError, + }, + { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -698,17 +708,13 @@ func Test_internalTypeScreenSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescreenscheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -750,13 +756,10 @@ func Test_internalTypeScreenSchemeImpl_Assign(t *testing.T) { func Test_internalTypeScreenSchemeImpl_Update(t *testing.T) { - payloadMocked := &struct { - Name string "json:\"name,omitempty\"" - Description string "json:\"description,omitempty\"" - }{Name: "New issue type scheme name", Description: "New issue type scheme description"} + payloadMocked := map[string]interface{}{"description": "New issue type scheme description", "name": "New issue type scheme name"} type fields struct { - c service.Client + c service.Connector version string } @@ -773,6 +776,16 @@ func Test_internalTypeScreenSchemeImpl_Update(t *testing.T) { wantErr bool Err error }{ + { + name: "when the issue type screen scheme id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "", + }, + wantErr: true, + Err: model.ErrNoIssueTypeScreenSchemeIDError, + }, { name: "when the api version is v3", fields: fields{version: "3"}, @@ -784,17 +797,13 @@ func Test_internalTypeScreenSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescreenscheme/20001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -819,17 +828,13 @@ func Test_internalTypeScreenSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issuetypescreenscheme/20001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -854,17 +859,13 @@ func Test_internalTypeScreenSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescreenscheme/20001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -908,7 +909,7 @@ func Test_internalTypeScreenSchemeImpl_Update(t *testing.T) { func Test_internalTypeScreenSchemeImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -925,6 +926,16 @@ func Test_internalTypeScreenSchemeImpl_Delete(t *testing.T) { wantErr bool Err error }{ + { + name: "when the issue type screen scheme id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "", + }, + wantErr: true, + Err: model.ErrNoIssueTypeScreenSchemeIDError, + }, { name: "when the api version is v3", fields: fields{version: "3"}, @@ -934,13 +945,13 @@ func Test_internalTypeScreenSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issuetypescreenscheme/20001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -963,13 +974,13 @@ func Test_internalTypeScreenSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issuetypescreenscheme/20001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -992,13 +1003,13 @@ func Test_internalTypeScreenSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issuetypescreenscheme/20001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1041,12 +1052,20 @@ func Test_internalTypeScreenSchemeImpl_Delete(t *testing.T) { func Test_internalTypeScreenSchemeImpl_Append(t *testing.T) { payloadMocked := &model.IssueTypeScreenSchemePayloadScheme{ - Name: "", - IssueTypeMappings: nil, + IssueTypeMappings: []*model.IssueTypeScreenSchemeMappingPayloadScheme{ + { + IssueTypeID: "10000", + ScreenSchemeID: "10001", + }, + { + IssueTypeID: "10001", + ScreenSchemeID: "10002", + }, + }, } type fields struct { - c service.Client + c service.Connector version string } @@ -1064,6 +1083,16 @@ func Test_internalTypeScreenSchemeImpl_Append(t *testing.T) { wantErr bool Err error }{ + { + name: "when the issue type screen scheme id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "", + }, + wantErr: true, + Err: model.ErrNoIssueTypeScreenSchemeIDError, + }, { name: "when the api version is v3", fields: fields{version: "3"}, @@ -1074,17 +1103,13 @@ func Test_internalTypeScreenSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescreenscheme/20001/mapping", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1108,17 +1133,13 @@ func Test_internalTypeScreenSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issuetypescreenscheme/20001/mapping", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1142,17 +1163,13 @@ func Test_internalTypeScreenSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescreenscheme/20001/mapping", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1195,12 +1212,10 @@ func Test_internalTypeScreenSchemeImpl_Append(t *testing.T) { func Test_internalTypeScreenSchemeImpl_UpdateDefault(t *testing.T) { - payloadMocked := &struct { - ScreenSchemeID string "json:\"screenSchemeId\"" - }{ScreenSchemeID: "200202"} + payloadMocked := map[string]interface{}{"screenSchemeId": "200202"} type fields struct { - c service.Client + c service.Connector version string } @@ -1217,6 +1232,26 @@ func Test_internalTypeScreenSchemeImpl_UpdateDefault(t *testing.T) { wantErr bool Err error }{ + { + name: "when the issue type screen scheme id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "", + }, + wantErr: true, + Err: model.ErrNoIssueTypeScreenSchemeIDError, + }, + { + name: "when the screen scheme id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "20001", + }, + wantErr: true, + Err: model.ErrNoScreenSchemeIDError, + }, { name: "when the api version is v3", fields: fields{version: "3"}, @@ -1227,17 +1262,13 @@ func Test_internalTypeScreenSchemeImpl_UpdateDefault(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescreenscheme/20001/mapping/default", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1261,17 +1292,13 @@ func Test_internalTypeScreenSchemeImpl_UpdateDefault(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issuetypescreenscheme/20001/mapping/default", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1295,17 +1322,13 @@ func Test_internalTypeScreenSchemeImpl_UpdateDefault(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issuetypescreenscheme/20001/mapping/default", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1348,12 +1371,10 @@ func Test_internalTypeScreenSchemeImpl_UpdateDefault(t *testing.T) { func Test_internalTypeScreenSchemeImpl_Remove(t *testing.T) { - payloadMocked := &struct { - IssueTypeIds []string "json:\"issueTypeIds\"" - }{IssueTypeIds: []string{"9", "43"}} + payloadMocked := map[string]interface{}{"issueTypeIds": []string{"9", "43"}} type fields struct { - c service.Client + c service.Connector version string } @@ -1371,6 +1392,26 @@ func Test_internalTypeScreenSchemeImpl_Remove(t *testing.T) { wantErr bool Err error }{ + { + name: "when the issue type screen scheme id is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "", + }, + wantErr: true, + Err: model.ErrNoIssueTypeScreenSchemeIDError, + }, + { + name: "when the issue type id's are not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + issueTypeScreenSchemeId: "2201", + }, + wantErr: true, + Err: model.ErrNoIssueTypesError, + }, { name: "when the api version is v3", fields: fields{version: "3"}, @@ -1381,17 +1422,13 @@ func Test_internalTypeScreenSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issuetypescreenscheme/20001/mapping/remove", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1415,17 +1452,13 @@ func Test_internalTypeScreenSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issuetypescreenscheme/20001/mapping/remove", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1449,17 +1482,13 @@ func Test_internalTypeScreenSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issuetypescreenscheme/20001/mapping/remove", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1517,7 +1546,7 @@ func Test_internalTypeScreenSchemeImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1543,17 +1572,13 @@ func Test_internalTypeScreenSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issuetypescreenscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1576,17 +1601,13 @@ func Test_internalTypeScreenSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issuetypescreenscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1609,17 +1630,13 @@ func Test_internalTypeScreenSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issuetypescreenscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1663,7 +1680,7 @@ func Test_internalTypeScreenSchemeImpl_Create(t *testing.T) { func Test_NewTypeScreenSchemeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 07eab8fc431aa06b85ac99f52ef1771b7bdf6653 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 23:30:52 -0600 Subject: [PATCH 31/70] :memo: Updated the Issue.Type.ScreenScheme.SchemesByProject() method documentation link. --- jira/internal/type_screen_scheme_impl.go | 2 +- service/jira/type.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jira/internal/type_screen_scheme_impl.go b/jira/internal/type_screen_scheme_impl.go index 1d7e0d99..97fd3542 100644 --- a/jira/internal/type_screen_scheme_impl.go +++ b/jira/internal/type_screen_scheme_impl.go @@ -129,7 +129,7 @@ func (t *TypeScreenSchemeService) Remove(ctx context.Context, issueTypeScreenSch // // GET /rest/api/{2-3}/issuetypescreenscheme/{issueTypeScreenSchemeId}/project // -// Docs: TODO: the documentation needs to be created +// https://docs.go-atlassian.io/jira-software-cloud/issues/types/screen-scheme#get-issue-type-screen-scheme-projects func (t *TypeScreenSchemeService) SchemesByProject(ctx context.Context, issueTypeScreenSchemeId, startAt, maxResults int) (*model.IssueTypeScreenSchemeByProjectPageScheme, *model.ResponseScheme, error) { return t.internalClient.SchemesByProject(ctx, issueTypeScreenSchemeId, startAt, maxResults) } diff --git a/service/jira/type.go b/service/jira/type.go index 4b305f08..4f0fb3f1 100644 --- a/service/jira/type.go +++ b/service/jira/type.go @@ -220,6 +220,6 @@ type TypeScreenSchemeConnector interface { // // GET /rest/api/{2-3}/issuetypescreenscheme/{issueTypeScreenSchemeId}/project // - // Docs: TODO: the documentation needs to be created + // https://docs.go-atlassian.io/jira-software-cloud/issues/types/screen-scheme#get-issue-type-screen-scheme-projects SchemesByProject(ctx context.Context, issueTypeScreenSchemeId, startAt, maxResults int) (*model.IssueTypeScreenSchemeByProjectPageScheme, *model.ResponseScheme, error) } From 295f9ddccc789862c35babed695a0676a80817f0 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 23:34:51 -0600 Subject: [PATCH 32/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Vote service. --- jira/internal/vote_impl.go | 12 +++++------ jira/internal/vote_impl_test.go | 35 +++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/jira/internal/vote_impl.go b/jira/internal/vote_impl.go index 6550001a..ad34c57e 100644 --- a/jira/internal/vote_impl.go +++ b/jira/internal/vote_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewVoteService(client service.Client, version string) (*VoteService, error) { +func NewVoteService(client service.Connector, version string) (*VoteService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -26,7 +26,7 @@ type VoteService struct { // Gets returns details about the votes on an issue. // -// This operation requires allowing users to vote on issues option to be ON +// # This operation requires allowing users to vote on issues option to be ON // // GET /rest/api/{2-3}/issue/{issueIdOrKey}/votes // @@ -58,7 +58,7 @@ func (v *VoteService) Delete(ctx context.Context, issueKeyOrId string) (*model.R } type internalVoteImpl struct { - c service.Client + c service.Connector version string } @@ -70,7 +70,7 @@ func (i *internalVoteImpl) Gets(ctx context.Context, issueKeyOrId string) (*mode endpoint := fmt.Sprintf("rest/api/%v/issue/%v/votes", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -92,7 +92,7 @@ func (i *internalVoteImpl) Add(ctx context.Context, issueKeyOrId string) (*model endpoint := fmt.Sprintf("rest/api/%v/issue/%v/votes", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, err } @@ -108,7 +108,7 @@ func (i *internalVoteImpl) Delete(ctx context.Context, issueKeyOrId string) (*mo endpoint := fmt.Sprintf("rest/api/%v/issue/%v/votes", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/vote_impl_test.go b/jira/internal/vote_impl_test.go index 2ac4b091..a2906a11 100644 --- a/jira/internal/vote_impl_test.go +++ b/jira/internal/vote_impl_test.go @@ -14,7 +14,7 @@ import ( func Test_internalVoteImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,12 +40,13 @@ func Test_internalVoteImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, nil) @@ -69,12 +70,13 @@ func Test_internalVoteImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, nil) @@ -109,12 +111,13 @@ func Test_internalVoteImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -159,7 +162,7 @@ func Test_internalVoteImpl_Gets(t *testing.T) { func Test_internalVoteImpl_Add(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -185,12 +188,13 @@ func Test_internalVoteImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, nil) @@ -214,12 +218,13 @@ func Test_internalVoteImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, nil) @@ -254,12 +259,13 @@ func Test_internalVoteImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -303,7 +309,7 @@ func Test_internalVoteImpl_Add(t *testing.T) { func Test_internalVoteImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -329,12 +335,13 @@ func Test_internalVoteImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, nil) @@ -358,12 +365,13 @@ func Test_internalVoteImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, nil) @@ -398,12 +406,13 @@ func Test_internalVoteImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/votes", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -447,7 +456,7 @@ func Test_internalVoteImpl_Delete(t *testing.T) { func Test_NewVoteService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 02c1c3e6c3f6a17e26c8a378731866d978c91b8f Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 9 Jul 2023 23:40:00 -0600 Subject: [PATCH 33/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Watcher service. --- jira/internal/watcher_impl.go | 10 ++++----- jira/internal/watcher_impl_test.go | 35 +++++++++++++++++++----------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/jira/internal/watcher_impl.go b/jira/internal/watcher_impl.go index cf9bd036..749550e7 100644 --- a/jira/internal/watcher_impl.go +++ b/jira/internal/watcher_impl.go @@ -10,7 +10,7 @@ import ( "net/url" ) -func NewWatcherService(client service.Client, version string) (*WatcherService, error) { +func NewWatcherService(client service.Connector, version string) (*WatcherService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -55,7 +55,7 @@ func (w *WatcherService) Delete(ctx context.Context, issueKeyOrId, accountId str } type internalWatcherImpl struct { - c service.Client + c service.Connector version string } @@ -67,7 +67,7 @@ func (i *internalWatcherImpl) Gets(ctx context.Context, issueKeyOrId string) (*m endpoint := fmt.Sprintf("rest/api/%v/issue/%v/watchers", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -89,7 +89,7 @@ func (i *internalWatcherImpl) Add(ctx context.Context, issueKeyOrId string) (*mo endpoint := fmt.Sprintf("rest/api/%v/issue/%v/watchers", i.version, issueKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, err } @@ -112,7 +112,7 @@ func (i *internalWatcherImpl) Delete(ctx context.Context, issueKeyOrId, accountI endpoint := fmt.Sprintf("rest/api/%v/issue/%v/watchers?%v", i.version, issueKeyOrId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/watcher_impl_test.go b/jira/internal/watcher_impl_test.go index 18d7bcc1..49a8f6aa 100644 --- a/jira/internal/watcher_impl_test.go +++ b/jira/internal/watcher_impl_test.go @@ -14,7 +14,7 @@ import ( func Test_internalWatcherImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,12 +40,13 @@ func Test_internalWatcherImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/watchers", + "", nil). Return(&http.Request{}, nil) @@ -69,12 +70,13 @@ func Test_internalWatcherImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-5/watchers", + "", nil). Return(&http.Request{}, nil) @@ -109,12 +111,13 @@ func Test_internalWatcherImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/watchers", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -159,7 +162,7 @@ func Test_internalWatcherImpl_Gets(t *testing.T) { func Test_internalWatcherImpl_Add(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -185,12 +188,13 @@ func Test_internalWatcherImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-5/watchers", + "", nil). Return(&http.Request{}, nil) @@ -214,12 +218,13 @@ func Test_internalWatcherImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-5/watchers", + "", nil). Return(&http.Request{}, nil) @@ -254,12 +259,13 @@ func Test_internalWatcherImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-5/watchers", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -303,7 +309,7 @@ func Test_internalWatcherImpl_Add(t *testing.T) { func Test_internalWatcherImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -331,12 +337,13 @@ func Test_internalWatcherImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/watchers?accountId=dummy-account-id", + "", nil). Return(&http.Request{}, nil) @@ -361,12 +368,13 @@ func Test_internalWatcherImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/DUMMY-5/watchers?accountId=dummy-account-id", + "", nil). Return(&http.Request{}, nil) @@ -414,12 +422,13 @@ func Test_internalWatcherImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/watchers?accountId=dummy-account-id", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -463,7 +472,7 @@ func Test_internalWatcherImpl_Delete(t *testing.T) { func Test_NewWatcherService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 78a337feebaca2160dd6a84184b505cac46dec9b Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Mon, 10 Jul 2023 00:48:45 -0600 Subject: [PATCH 34/70] :art: Migrated the client interface from Client to Connector on the Jira Issue Worklog service. --- jira/internal/worklog_impl_adf.go | 41 +--- jira/internal/worklog_impl_adf_test.go | 204 +++++----------- jira/internal/worklog_impl_rich_text.go | 41 +--- jira/internal/worklog_impl_rich_text_test.go | 230 +++++-------------- 4 files changed, 138 insertions(+), 378 deletions(-) diff --git a/jira/internal/worklog_impl_adf.go b/jira/internal/worklog_impl_adf.go index e0dce529..b37d88b6 100644 --- a/jira/internal/worklog_impl_adf.go +++ b/jira/internal/worklog_impl_adf.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewWorklogADFService(client service.Client, version string) (*WorklogADFService, error) { +func NewWorklogADFService(client service.Connector, version string) (*WorklogADFService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -126,7 +126,7 @@ func (w *WorklogADFService) Update(ctx context.Context, issueKeyOrId, worklogId } type internalWorklogAdfImpl struct { - c service.Client + c service.Connector version string } @@ -148,18 +148,7 @@ func (i *internalWorklogAdfImpl) Gets(ctx context.Context, worklogIds []int, exp endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - payload := struct { - Ids []int `json:"ids"` - }{ - Ids: worklogIds, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), "", map[string]interface{}{"ids": worklogIds}) if err != nil { return nil, nil, err } @@ -195,7 +184,7 @@ func (i *internalWorklogAdfImpl) Get(ctx context.Context, issueKeyOrId, worklogI endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -229,7 +218,7 @@ func (i *internalWorklogAdfImpl) Issue(ctx context.Context, issueKeyOrId string, endpoint := fmt.Sprintf("rest/api/%v/issue/%v/worklog?%v", i.version, issueKeyOrId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -281,7 +270,7 @@ func (i *internalWorklogAdfImpl) Delete(ctx context.Context, issueKeyOrId, workl endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint.String(), "", nil) if err != nil { return nil, err } @@ -303,7 +292,7 @@ func (i *internalWorklogAdfImpl) Deleted(ctx context.Context, since int) (*model endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -335,7 +324,7 @@ func (i *internalWorklogAdfImpl) Updated(ctx context.Context, since int, expand endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -384,12 +373,7 @@ func (i *internalWorklogAdfImpl) Add(ctx context.Context, issueKeyOrID string, p endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), "", payload) if err != nil { return nil, nil, err } @@ -442,12 +426,7 @@ func (i *internalWorklogAdfImpl) Update(ctx context.Context, issueKeyOrId, workl endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint.String(), "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/worklog_impl_adf_test.go b/jira/internal/worklog_impl_adf_test.go index 275d08b9..f76089e8 100644 --- a/jira/internal/worklog_impl_adf_test.go +++ b/jira/internal/worklog_impl_adf_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,12 +14,10 @@ import ( func Test_internalWorklogAdfImpl_Gets(t *testing.T) { - payloadMocked := &struct { - Ids []int "json:\"ids\"" - }{Ids: []int{1, 2, 3, 4}} + payloadMocked := map[string]interface{}{"ids": []int{1, 2, 3, 4}} type fields struct { - c service.Client + c service.Connector version string } @@ -48,17 +45,13 @@ func Test_internalWorklogAdfImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/worklog/list?expand=properties", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -82,17 +75,13 @@ func Test_internalWorklogAdfImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/worklog/list?expand=properties", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -126,17 +115,13 @@ func Test_internalWorklogAdfImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/worklog/list?expand=properties", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -180,7 +165,7 @@ func Test_internalWorklogAdfImpl_Gets(t *testing.T) { func Test_internalWorklogAdfImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -209,13 +194,13 @@ func Test_internalWorklogAdfImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/worklog/493939?expand=properties", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -240,13 +225,13 @@ func Test_internalWorklogAdfImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-5/worklog/493939?expand=properties", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -292,13 +277,13 @@ func Test_internalWorklogAdfImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/worklog/493939?expand=properties", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -342,7 +327,7 @@ func Test_internalWorklogAdfImpl_Get(t *testing.T) { func Test_internalWorklogAdfImpl_Issue(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -374,13 +359,13 @@ func Test_internalWorklogAdfImpl_Issue(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/worklog?expand=properties&maxResults=50&startAt=0&startedAfter=1661101991", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -407,13 +392,13 @@ func Test_internalWorklogAdfImpl_Issue(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-5/worklog?expand=properties&maxResults=50&startAt=0&startedAfter=1661101991", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -450,13 +435,13 @@ func Test_internalWorklogAdfImpl_Issue(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/worklog?expand=properties&maxResults=50&startAt=0&startedAfter=1661101991", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -501,7 +486,7 @@ func Test_internalWorklogAdfImpl_Issue(t *testing.T) { func Test_internalWorklogAdfImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -537,13 +522,13 @@ func Test_internalWorklogAdfImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/worklog/h837372?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -575,13 +560,13 @@ func Test_internalWorklogAdfImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/DUMMY-5/worklog/h837372?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -605,13 +590,13 @@ func Test_internalWorklogAdfImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/worklog/h837372", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -656,13 +641,13 @@ func Test_internalWorklogAdfImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/worklog/h837372", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -706,7 +691,7 @@ func Test_internalWorklogAdfImpl_Delete(t *testing.T) { func Test_internalWorklogAdfImpl_Deleted(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -732,13 +717,13 @@ func Test_internalWorklogAdfImpl_Deleted(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/worklog/deleted?since=928281811", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -761,13 +746,13 @@ func Test_internalWorklogAdfImpl_Deleted(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/worklog/deleted?since=928281811", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -790,13 +775,13 @@ func Test_internalWorklogAdfImpl_Deleted(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/worklog/deleted?since=928281811", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -840,7 +825,7 @@ func Test_internalWorklogAdfImpl_Deleted(t *testing.T) { func Test_internalWorklogAdfImpl_Updated(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -868,13 +853,13 @@ func Test_internalWorklogAdfImpl_Updated(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/worklog/updated?expand=properties&since=928281811", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -898,13 +883,13 @@ func Test_internalWorklogAdfImpl_Updated(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/worklog/updated?expand=properties&since=928281811", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -928,13 +913,13 @@ func Test_internalWorklogAdfImpl_Updated(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/worklog/updated?expand=properties&since=928281811", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1004,7 +989,7 @@ func Test_internalWorklogAdfImpl_Add(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1041,17 +1026,13 @@ func Test_internalWorklogAdfImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-5/worklog?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1083,17 +1064,13 @@ func Test_internalWorklogAdfImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-5/worklog?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1107,28 +1084,6 @@ func Test_internalWorklogAdfImpl_Add(t *testing.T) { Err: nil, }, - { - name: "when the payload is not provided", - fields: fields{version: "3"}, - args: args{ - ctx: context.TODO(), - issueKeyOrID: "DUMMY-5", - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.WorklogADFPayloadScheme)(nil)). - Return(bytes.NewReader([]byte{}), model.ErrNonPayloadPointerError) - - fields.c = client - }, - wantErr: true, - Err: model.ErrNonPayloadPointerError, - }, - { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -1147,17 +1102,13 @@ func Test_internalWorklogAdfImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-5/worklog?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1228,7 +1179,7 @@ func Test_internalWorklogAdfImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1266,17 +1217,13 @@ func Test_internalWorklogAdfImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/DUMMY-5/worklog/3933828822?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1309,17 +1256,13 @@ func Test_internalWorklogAdfImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/DUMMY-5/worklog/3933828822?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1354,29 +1297,6 @@ func Test_internalWorklogAdfImpl_Update(t *testing.T) { Err: model.ErrNoWorklogIDError, }, - { - name: "when the payload is not provided", - fields: fields{version: "3"}, - args: args{ - ctx: context.TODO(), - issueKeyOrID: "DUMMY-5", - worklogId: "38388382", - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.WorklogADFPayloadScheme)(nil)). - Return(bytes.NewReader([]byte{}), model.ErrNonPayloadPointerError) - - fields.c = client - }, - wantErr: true, - Err: model.ErrNonPayloadPointerError, - }, - { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -1396,17 +1316,13 @@ func Test_internalWorklogAdfImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/DUMMY-5/worklog/3933828822?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1451,7 +1367,7 @@ func Test_internalWorklogAdfImpl_Update(t *testing.T) { func Test_NewWorklogADFService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } diff --git a/jira/internal/worklog_impl_rich_text.go b/jira/internal/worklog_impl_rich_text.go index 4df9eccb..8695cb2b 100644 --- a/jira/internal/worklog_impl_rich_text.go +++ b/jira/internal/worklog_impl_rich_text.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewWorklogRichTextService(client service.Client, version string) (*WorklogRichTextService, error) { +func NewWorklogRichTextService(client service.Connector, version string) (*WorklogRichTextService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -126,7 +126,7 @@ func (w *WorklogRichTextService) Update(ctx context.Context, issueKeyOrId, workl } type internalWorklogRichTextImpl struct { - c service.Client + c service.Connector version string } @@ -148,18 +148,7 @@ func (i *internalWorklogRichTextImpl) Gets(ctx context.Context, worklogIds []int endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - payload := struct { - Ids []int `json:"ids"` - }{ - Ids: worklogIds, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), "", map[string]interface{}{"ids": worklogIds}) if err != nil { return nil, nil, err } @@ -195,7 +184,7 @@ func (i *internalWorklogRichTextImpl) Get(ctx context.Context, issueKeyOrId, wor endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -229,7 +218,7 @@ func (i *internalWorklogRichTextImpl) Issue(ctx context.Context, issueKeyOrId st endpoint := fmt.Sprintf("rest/api/%v/issue/%v/worklog?%v", i.version, issueKeyOrId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -281,7 +270,7 @@ func (i *internalWorklogRichTextImpl) Delete(ctx context.Context, issueKeyOrId, endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint.String(), "", nil) if err != nil { return nil, err } @@ -303,7 +292,7 @@ func (i *internalWorklogRichTextImpl) Deleted(ctx context.Context, since int) (* endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -335,7 +324,7 @@ func (i *internalWorklogRichTextImpl) Updated(ctx context.Context, since int, ex endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -384,12 +373,7 @@ func (i *internalWorklogRichTextImpl) Add(ctx context.Context, issueKeyOrID stri endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), "", payload) if err != nil { return nil, nil, err } @@ -442,12 +426,7 @@ func (i *internalWorklogRichTextImpl) Update(ctx context.Context, issueKeyOrId, endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint.String(), "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/worklog_impl_rich_text_test.go b/jira/internal/worklog_impl_rich_text_test.go index 0e9c328c..b628b569 100644 --- a/jira/internal/worklog_impl_rich_text_test.go +++ b/jira/internal/worklog_impl_rich_text_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,12 +14,10 @@ import ( func Test_internalWorklogRichTextImpl_Gets(t *testing.T) { - payloadMocked := &struct { - Ids []int "json:\"ids\"" - }{Ids: []int{1, 2, 3, 4}} + payloadMocked := map[string]interface{}{"ids": []int{1, 2, 3, 4}} type fields struct { - c service.Client + c service.Connector version string } @@ -48,51 +45,13 @@ func Test_internalWorklogRichTextImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/worklog/list?expand=properties", - bytes.NewReader([]byte{})). - Return(&http.Request{}, nil) - - client.On("Call", - &http.Request{}, - mock.Anything). - Return(&model.ResponseScheme{}, nil) - - fields.c = client - }, - wantErr: false, - Err: nil, - }, - - { - name: "when the api version is v3", - fields: fields{version: "2"}, - args: args{ - ctx: context.TODO(), - worklogIds: []int{1, 2, 3, 4}, - expand: []string{"properties"}, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) - - client.On("NewRequest", - context.Background(), - http.MethodPost, - "rest/api/2/worklog/list?expand=properties", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -126,17 +85,13 @@ func Test_internalWorklogRichTextImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/worklog/list?expand=properties", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -180,7 +135,7 @@ func Test_internalWorklogRichTextImpl_Gets(t *testing.T) { func Test_internalWorklogRichTextImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -209,13 +164,13 @@ func Test_internalWorklogRichTextImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/worklog/493939?expand=properties", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -240,13 +195,13 @@ func Test_internalWorklogRichTextImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-5/worklog/493939?expand=properties", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -292,13 +247,13 @@ func Test_internalWorklogRichTextImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/worklog/493939?expand=properties", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -342,7 +297,7 @@ func Test_internalWorklogRichTextImpl_Get(t *testing.T) { func Test_internalWorklogRichTextImpl_Issue(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -374,13 +329,13 @@ func Test_internalWorklogRichTextImpl_Issue(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/worklog?expand=properties&maxResults=50&startAt=0&startedAfter=1661101991", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -407,13 +362,13 @@ func Test_internalWorklogRichTextImpl_Issue(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/issue/DUMMY-5/worklog?expand=properties&maxResults=50&startAt=0&startedAfter=1661101991", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -450,13 +405,13 @@ func Test_internalWorklogRichTextImpl_Issue(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/issue/DUMMY-5/worklog?expand=properties&maxResults=50&startAt=0&startedAfter=1661101991", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -501,7 +456,7 @@ func Test_internalWorklogRichTextImpl_Issue(t *testing.T) { func Test_internalWorklogRichTextImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -537,13 +492,13 @@ func Test_internalWorklogRichTextImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/worklog/h837372?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -575,13 +530,13 @@ func Test_internalWorklogRichTextImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/issue/DUMMY-5/worklog/h837372?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -605,13 +560,13 @@ func Test_internalWorklogRichTextImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/worklog/h837372", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -656,13 +611,13 @@ func Test_internalWorklogRichTextImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/issue/DUMMY-5/worklog/h837372", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -706,7 +661,7 @@ func Test_internalWorklogRichTextImpl_Delete(t *testing.T) { func Test_internalWorklogRichTextImpl_Deleted(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -732,13 +687,13 @@ func Test_internalWorklogRichTextImpl_Deleted(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/worklog/deleted?since=928281811", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -761,13 +716,13 @@ func Test_internalWorklogRichTextImpl_Deleted(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/worklog/deleted?since=928281811", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -790,13 +745,13 @@ func Test_internalWorklogRichTextImpl_Deleted(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/worklog/deleted?since=928281811", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -840,7 +795,7 @@ func Test_internalWorklogRichTextImpl_Deleted(t *testing.T) { func Test_internalWorklogRichTextImpl_Updated(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -868,13 +823,13 @@ func Test_internalWorklogRichTextImpl_Updated(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/worklog/updated?expand=properties&since=928281811", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -898,13 +853,13 @@ func Test_internalWorklogRichTextImpl_Updated(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/worklog/updated?expand=properties&since=928281811", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -928,13 +883,13 @@ func Test_internalWorklogRichTextImpl_Updated(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/worklog/updated?expand=properties&since=928281811", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -991,7 +946,7 @@ func Test_internalWorklogRichTextImpl_Add(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1028,17 +983,13 @@ func Test_internalWorklogRichTextImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-5/worklog?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1070,17 +1021,13 @@ func Test_internalWorklogRichTextImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/issue/DUMMY-5/worklog?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1094,28 +1041,6 @@ func Test_internalWorklogRichTextImpl_Add(t *testing.T) { Err: nil, }, - { - name: "when the payload is not provided", - fields: fields{version: "3"}, - args: args{ - ctx: context.TODO(), - issueKeyOrID: "DUMMY-5", - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.WorklogRichTextPayloadScheme)(nil)). - Return(bytes.NewReader([]byte{}), model.ErrNonPayloadPointerError) - - fields.c = client - }, - wantErr: true, - Err: model.ErrNonPayloadPointerError, - }, - { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -1134,17 +1059,13 @@ func Test_internalWorklogRichTextImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/issue/DUMMY-5/worklog?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1200,7 +1121,7 @@ func Test_internalWorklogRichTextImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1238,17 +1159,13 @@ func Test_internalWorklogRichTextImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/DUMMY-5/worklog/3933828822?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1281,17 +1198,13 @@ func Test_internalWorklogRichTextImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/issue/DUMMY-5/worklog/3933828822?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1326,29 +1239,6 @@ func Test_internalWorklogRichTextImpl_Update(t *testing.T) { Err: model.ErrNoWorklogIDError, }, - { - name: "when the payload is not provided", - fields: fields{version: "3"}, - args: args{ - ctx: context.TODO(), - issueKeyOrID: "DUMMY-5", - worklogId: "38388382", - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.WorklogRichTextPayloadScheme)(nil)). - Return(bytes.NewReader([]byte{}), model.ErrNonPayloadPointerError) - - fields.c = client - }, - wantErr: true, - Err: model.ErrNonPayloadPointerError, - }, - { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -1368,17 +1258,13 @@ func Test_internalWorklogRichTextImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/issue/DUMMY-5/worklog/3933828822?adjustEstimate=new&expand=properties&newEstimate=2d¬ifyUsers=true&overrideEditableFlag=true&reduceBy=manual", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1423,7 +1309,7 @@ func Test_internalWorklogRichTextImpl_Update(t *testing.T) { func Test_NewWorklogRichTextService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From bfeca274198ea3cbab9b4c4727b37f4f5731b721 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Wed, 12 Jul 2023 00:53:49 -0600 Subject: [PATCH 35/70] :art: Migrated the client interface from Client to Connector on the Jira Permission service. --- jira/internal/permission_impl.go | 26 +++----- jira/internal/permission_impl_test.go | 88 +++++++++++++-------------- pkg/infra/models/errors.go | 1 + 3 files changed, 49 insertions(+), 66 deletions(-) diff --git a/jira/internal/permission_impl.go b/jira/internal/permission_impl.go index 70dff9e9..aaca80a7 100644 --- a/jira/internal/permission_impl.go +++ b/jira/internal/permission_impl.go @@ -10,7 +10,7 @@ import ( "net/http" ) -func NewPermissionService(client service.Client, version string, scheme *PermissionSchemeService) (*PermissionService, error) { +func NewPermissionService(client service.Connector, version string, scheme *PermissionSchemeService) (*PermissionService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -55,7 +55,7 @@ func (p *PermissionService) Projects(ctx context.Context, permissions []string) } type internalPermissionImpl struct { - c service.Client + c service.Connector version string } @@ -63,7 +63,7 @@ func (i *internalPermissionImpl) Gets(ctx context.Context) ([]*model.PermissionS endpoint := fmt.Sprintf("rest/api/%v/permissions", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -98,14 +98,9 @@ func (i *internalPermissionImpl) Gets(ctx context.Context) ([]*model.PermissionS func (i *internalPermissionImpl) Check(ctx context.Context, payload *model.PermissionCheckPayload) (*model.PermissionGrantsScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/permissions/check", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -121,20 +116,13 @@ func (i *internalPermissionImpl) Check(ctx context.Context, payload *model.Permi func (i *internalPermissionImpl) Projects(ctx context.Context, permissions []string) (*model.PermittedProjectsScheme, *model.ResponseScheme, error) { - payload := struct { - Permissions []string `json:"permissions,omitempty"` - }{ - Permissions: permissions, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err + if len(permissions) == 0 { + return nil, nil, model.ErrNoPermissionKeysError } endpoint := fmt.Sprintf("rest/api/%v/permissions/project", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"permissions": permissions}) if err != nil { return nil, nil, err } diff --git a/jira/internal/permission_impl_test.go b/jira/internal/permission_impl_test.go index f87b62ac..4d538d20 100644 --- a/jira/internal/permission_impl_test.go +++ b/jira/internal/permission_impl_test.go @@ -30,7 +30,7 @@ func Test_internalPermissionImpl_Gets(t *testing.T) { `) type fields struct { - c service.Client + c service.Connector version string } @@ -54,12 +54,13 @@ func Test_internalPermissionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/permissions", + "", nil). Return(&http.Request{}, nil) @@ -82,12 +83,13 @@ func Test_internalPermissionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissions", + "", nil). Return(&http.Request{}, nil) @@ -110,12 +112,13 @@ func Test_internalPermissionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/permissions", + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) @@ -146,12 +149,13 @@ func Test_internalPermissionImpl_Gets(t *testing.T) { } `) - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/permissions", + "", nil). Return(&http.Request{}, nil) @@ -213,7 +217,7 @@ func Test_internalPermissionImpl_Checks(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -239,17 +243,14 @@ func Test_internalPermissionImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/permissions/check", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -272,17 +273,14 @@ func Test_internalPermissionImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/permissions/check", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -305,17 +303,14 @@ func Test_internalPermissionImpl_Checks(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/permissions/check", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -358,12 +353,10 @@ func Test_internalPermissionImpl_Checks(t *testing.T) { func Test_internalPermissionImpl_Projects(t *testing.T) { - payloadMocked := &struct { - Permissions []string "json:\"permissions,omitempty\"" - }{Permissions: []string{"EDIT_ISSUES", "CREATE_ISSUES"}} + payloadMocked := map[string]interface{}{"permissions": []string{"EDIT_ISSUES", "CREATE_ISSUES"}} type fields struct { - c service.Client + c service.Connector version string } @@ -389,17 +382,14 @@ func Test_internalPermissionImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/permissions/project", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -422,17 +412,14 @@ func Test_internalPermissionImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/permissions/project", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -455,17 +442,14 @@ func Test_internalPermissionImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/permissions/project", - bytes.NewReader([]byte{})). + "", + payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -473,6 +457,16 @@ func Test_internalPermissionImpl_Projects(t *testing.T) { wantErr: true, Err: errors.New("error, unable to create the http request"), }, + + { + name: "when the permission keys are not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + }, + wantErr: true, + Err: model.ErrNoPermissionKeysError, + }, } for _, testCase := range testCases { @@ -509,7 +503,7 @@ func Test_internalPermissionImpl_Projects(t *testing.T) { func Test_NewPermissionService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } diff --git a/pkg/infra/models/errors.go b/pkg/infra/models/errors.go index 2dd1d411..65662fd0 100644 --- a/pkg/infra/models/errors.go +++ b/pkg/infra/models/errors.go @@ -108,6 +108,7 @@ var ( ErrNpWorklogsError = errors.New("jira: no worklog's id set") ErrNoPermissionSchemeIDError = errors.New("jira: no permission scheme id set") ErrNoPermissionGrantIDError = errors.New("jira: no permission grant id set") + ErrNoPermissionKeysError = errors.New("jira: no permission keys set") ErrNoComponentIDError = errors.New("jira: no component id set") ErrProjectTypeKeyError = errors.New("jira: no project type key set") ErrNoProjectNameError = errors.New("jira: no project name set") From fd7008acd38332526500370796617b5ca536d7be Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Wed, 12 Jul 2023 00:58:33 -0600 Subject: [PATCH 36/70] :memo: Updated the documentation link on the Jira.Permission services --- jira/internal/permission_impl.go | 4 ++-- service/jira/permission.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/jira/internal/permission_impl.go b/jira/internal/permission_impl.go index aaca80a7..fe1a198e 100644 --- a/jira/internal/permission_impl.go +++ b/jira/internal/permission_impl.go @@ -31,7 +31,7 @@ type PermissionService struct { // // GET /rest/api/{2-3}/permissions // -// TODO: Add/Create documentation +// https://docs.go-atlassian.io/jira-software-cloud/permissions#get-my-permissions func (p *PermissionService) Gets(ctx context.Context) ([]*model.PermissionScheme, *model.ResponseScheme, error) { return p.internalClient.Gets(ctx) } @@ -49,7 +49,7 @@ func (p *PermissionService) Check(ctx context.Context, payload *model.Permission // // POST /rest/api/{2-3}/permissions/project // -// TODO: Add/Create documentation +// https://docs.go-atlassian.io/jira-software-cloud/permissions#get-permitted-projects func (p *PermissionService) Projects(ctx context.Context, permissions []string) (*model.PermittedProjectsScheme, *model.ResponseScheme, error) { return p.internalClient.Projects(ctx, permissions) } diff --git a/service/jira/permission.go b/service/jira/permission.go index 2521acb5..d1ce1925 100644 --- a/service/jira/permission.go +++ b/service/jira/permission.go @@ -11,7 +11,7 @@ type PermissionConnector interface { // // GET /rest/api/{2-3}/permissions // - // TODO: Add/Create documentation + // https://docs.go-atlassian.io/jira-software-cloud/permissions#get-my-permissions Gets(ctx context.Context) ([]*model.PermissionScheme, *model.ResponseScheme, error) // Check search the permissions linked to an accountID, then check if the user permissions. @@ -25,7 +25,7 @@ type PermissionConnector interface { // // POST /rest/api/{2-3}/permissions/project // - // TODO: Add/Create documentation + // https://docs.go-atlassian.io/jira-software-cloud/permissions#get-permitted-projects Projects(ctx context.Context, permissions []string) (*model.PermittedProjectsScheme, *model.ResponseScheme, error) } From 4206ac28bd3c281977aed4ab159739987cdb900a Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Wed, 12 Jul 2023 01:04:50 -0600 Subject: [PATCH 37/70] :art: Migrated the client interface from Client to Connector on the Jira Permission Scheme service. --- jira/internal/permission_scheme_impl.go | 24 ++--- jira/internal/permission_scheme_impl_test.go | 97 ++++++++------------ 2 files changed, 43 insertions(+), 78 deletions(-) diff --git a/jira/internal/permission_scheme_impl.go b/jira/internal/permission_scheme_impl.go index eba74cab..4bb3843c 100644 --- a/jira/internal/permission_scheme_impl.go +++ b/jira/internal/permission_scheme_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewPermissionSchemeService(client service.Client, version string, grant *PermissionSchemeGrantService) (*PermissionSchemeService, error) { +func NewPermissionSchemeService(client service.Connector, version string, grant *PermissionSchemeGrantService) (*PermissionSchemeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -83,7 +83,7 @@ func (p *PermissionSchemeService) Update(ctx context.Context, permissionSchemeId } type internalPermissionSchemeImpl struct { - c service.Client + c service.Connector version string } @@ -91,7 +91,7 @@ func (i *internalPermissionSchemeImpl) Gets(ctx context.Context) (*model.Permiss endpoint := fmt.Sprintf("rest/api/%v/permissionscheme", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -122,7 +122,7 @@ func (i *internalPermissionSchemeImpl) Get(ctx context.Context, permissionScheme endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -144,7 +144,7 @@ func (i *internalPermissionSchemeImpl) Delete(ctx context.Context, permissionSch endpoint := fmt.Sprintf("rest/api/%v/permissionscheme/%v", i.version, permissionSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -154,14 +154,9 @@ func (i *internalPermissionSchemeImpl) Delete(ctx context.Context, permissionSch func (i *internalPermissionSchemeImpl) Create(ctx context.Context, payload *model.PermissionSchemeScheme) (*model.PermissionSchemeScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/permissionscheme", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -181,14 +176,9 @@ func (i *internalPermissionSchemeImpl) Update(ctx context.Context, permissionSch return nil, nil, model.ErrNoPermissionSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/permissionscheme/%v", i.version, permissionSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/permission_scheme_impl_test.go b/jira/internal/permission_scheme_impl_test.go index dce9b571..554c7e97 100644 --- a/jira/internal/permission_scheme_impl_test.go +++ b/jira/internal/permission_scheme_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func TestPermissionSchemeService_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -39,13 +38,13 @@ func TestPermissionSchemeService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/permissionscheme", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -67,13 +66,13 @@ func TestPermissionSchemeService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissionscheme", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -95,13 +94,13 @@ func TestPermissionSchemeService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissionscheme", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -145,7 +144,7 @@ func TestPermissionSchemeService_Gets(t *testing.T) { func TestPermissionSchemeService_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -173,13 +172,13 @@ func TestPermissionSchemeService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/permissionscheme/10001?expand=all", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -203,13 +202,13 @@ func TestPermissionSchemeService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissionscheme/10001?expand=all", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -243,13 +242,13 @@ func TestPermissionSchemeService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissionscheme/10001?expand=all", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -293,7 +292,7 @@ func TestPermissionSchemeService_Get(t *testing.T) { func TestPermissionSchemeService_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -319,13 +318,13 @@ func TestPermissionSchemeService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/permissionscheme/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -348,13 +347,13 @@ func TestPermissionSchemeService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/permissionscheme/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -387,13 +386,13 @@ func TestPermissionSchemeService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/permissionscheme/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -457,7 +456,7 @@ func TestPermissionSchemeService_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -483,17 +482,13 @@ func TestPermissionSchemeService_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/permissionscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -516,17 +511,13 @@ func TestPermissionSchemeService_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/permissionscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -549,17 +540,13 @@ func TestPermissionSchemeService_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/permissionscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -624,7 +611,7 @@ func TestPermissionSchemeService_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -652,17 +639,13 @@ func TestPermissionSchemeService_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/permissionscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -686,17 +669,13 @@ func TestPermissionSchemeService_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/permissionscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -720,17 +699,13 @@ func TestPermissionSchemeService_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/permissionscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -774,7 +749,7 @@ func TestPermissionSchemeService_Update(t *testing.T) { func Test_NewPermissionSchemeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 192c1eb582f34e994b2e25253c38bf1b04c0dc30 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Wed, 12 Jul 2023 01:16:32 -0600 Subject: [PATCH 38/70] :art: Migrated the client interface from Client to Connector on the Jira Permission Scheme Grant service. --- jira/internal/permission_scheme_grant_impl.go | 17 ++--- .../permission_scheme_grant_impl_test.go | 71 ++++++++----------- 2 files changed, 35 insertions(+), 53 deletions(-) diff --git a/jira/internal/permission_scheme_grant_impl.go b/jira/internal/permission_scheme_grant_impl.go index 802cdb2f..47cd1dfd 100644 --- a/jira/internal/permission_scheme_grant_impl.go +++ b/jira/internal/permission_scheme_grant_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewPermissionSchemeGrantService(client service.Client, version string) (*PermissionSchemeGrantService, error) { +func NewPermissionSchemeGrantService(client service.Connector, version string) (*PermissionSchemeGrantService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -63,7 +63,7 @@ func (p *PermissionSchemeGrantService) Delete(ctx context.Context, permissionSch } type internalPermissionSchemeGrantImpl struct { - c service.Client + c service.Connector version string } @@ -73,14 +73,9 @@ func (i *internalPermissionSchemeGrantImpl) Create(ctx context.Context, permissi return nil, nil, model.ErrNoPermissionSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/permissionscheme/%v/permission", i.version, permissionSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -111,7 +106,7 @@ func (i *internalPermissionSchemeGrantImpl) Gets(ctx context.Context, permission endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -146,7 +141,7 @@ func (i *internalPermissionSchemeGrantImpl) Get(ctx context.Context, permissionS endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -172,7 +167,7 @@ func (i *internalPermissionSchemeGrantImpl) Delete(ctx context.Context, permissi endpoint := fmt.Sprintf("rest/api/%v/permissionscheme/%v/permission/%v", i.version, permissionSchemeId, permissionGrantId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/permission_scheme_grant_impl_test.go b/jira/internal/permission_scheme_grant_impl_test.go index 12d4fc18..16b16c07 100644 --- a/jira/internal/permission_scheme_grant_impl_test.go +++ b/jira/internal/permission_scheme_grant_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -23,7 +22,7 @@ func TestPermissionSchemeGrantService_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -51,17 +50,13 @@ func TestPermissionSchemeGrantService_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/permissionscheme/10001/permission", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -85,17 +80,13 @@ func TestPermissionSchemeGrantService_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/permissionscheme/10001/permission", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -129,17 +120,13 @@ func TestPermissionSchemeGrantService_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/permissionscheme/10001/permission", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -183,7 +170,7 @@ func TestPermissionSchemeGrantService_Create(t *testing.T) { func TestPermissionSchemeGrantService_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -211,13 +198,13 @@ func TestPermissionSchemeGrantService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/permissionscheme/10001/permission?expand=all", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -241,13 +228,13 @@ func TestPermissionSchemeGrantService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissionscheme/10001/permission?expand=all", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -281,13 +268,13 @@ func TestPermissionSchemeGrantService_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissionscheme/10001/permission?expand=all", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -331,7 +318,7 @@ func TestPermissionSchemeGrantService_Gets(t *testing.T) { func TestPermissionSchemeGrantService_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -361,13 +348,13 @@ func TestPermissionSchemeGrantService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/permissionscheme/10001/permission/30092?expand=all", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -392,13 +379,13 @@ func TestPermissionSchemeGrantService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissionscheme/10001/permission/30092?expand=all", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -444,13 +431,13 @@ func TestPermissionSchemeGrantService_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/permissionscheme/10001/permission/30092?expand=all", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -495,7 +482,7 @@ func TestPermissionSchemeGrantService_Get(t *testing.T) { func TestPermissionSchemeGrantService_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -523,13 +510,13 @@ func TestPermissionSchemeGrantService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/permissionscheme/10001/permission/30092", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -553,13 +540,13 @@ func TestPermissionSchemeGrantService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/permissionscheme/10001/permission/30092", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -604,13 +591,13 @@ func TestPermissionSchemeGrantService_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/permissionscheme/10001/permission/30092", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -653,7 +640,7 @@ func TestPermissionSchemeGrantService_Delete(t *testing.T) { func Test_NewPermissionSchemeGrantService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 89b91ba618e6a87d3dc9d5fc32122d27fcde3329 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 16 Jul 2023 03:33:08 -0600 Subject: [PATCH 39/70] :art: Migrated the client interface from Client to Connector on the Jira Project service. --- jira/internal/project_impl.go | 34 +++--- jira/internal/project_impl_test.go | 167 ++++++++++++----------------- 2 files changed, 83 insertions(+), 118 deletions(-) diff --git a/jira/internal/project_impl.go b/jira/internal/project_impl.go index c873db60..48a58771 100644 --- a/jira/internal/project_impl.go +++ b/jira/internal/project_impl.go @@ -24,7 +24,7 @@ type ProjectChildServices struct { Version *ProjectVersionService } -func NewProjectService(client service.Client, version string, subServices *ProjectChildServices) (*ProjectService, error) { +func NewProjectService(client service.Connector, version string, subServices *ProjectChildServices) (*ProjectService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -162,20 +162,15 @@ func (p *ProjectService) NotificationScheme(ctx context.Context, projectKeyOrId } type internalProjectImpl struct { - c service.Client + c service.Connector version string } func (i *internalProjectImpl) Create(ctx context.Context, payload *model.ProjectPayloadScheme) (*model.NewProjectCreatedScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/project", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -244,7 +239,7 @@ func (i *internalProjectImpl) Search(ctx context.Context, options *model.Project endpoint := fmt.Sprintf("rest/api/%v/project/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -275,7 +270,7 @@ func (i *internalProjectImpl) Get(ctx context.Context, projectKeyOrId string, ex endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -295,14 +290,9 @@ func (i *internalProjectImpl) Update(ctx context.Context, projectKeyOrId string, return nil, nil, model.ErrNoProjectIDOrKeyError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/project/%v", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -327,7 +317,7 @@ func (i *internalProjectImpl) Delete(ctx context.Context, projectKeyOrId string, endpoint := fmt.Sprintf("rest/api/%v/project/%v?%v", i.version, projectKeyOrId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -343,7 +333,7 @@ func (i *internalProjectImpl) DeleteAsynchronously(ctx context.Context, projectK endpoint := fmt.Sprintf("rest/api/%v/project/%v/delete", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -365,7 +355,7 @@ func (i *internalProjectImpl) Archive(ctx context.Context, projectKeyOrId string endpoint := fmt.Sprintf("rest/api/%v/project/%v/archive", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, err } @@ -381,7 +371,7 @@ func (i *internalProjectImpl) Restore(ctx context.Context, projectKeyOrId string endpoint := fmt.Sprintf("rest/api/%v/project/%v/restore", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -403,7 +393,7 @@ func (i *internalProjectImpl) Statuses(ctx context.Context, projectKeyOrId strin endpoint := fmt.Sprintf("rest/api/%v/project/%v/statuses", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -434,7 +424,7 @@ func (i *internalProjectImpl) NotificationScheme(ctx context.Context, projectKey endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/project_impl_test.go b/jira/internal/project_impl_test.go index 9f1aa191..bf544310 100644 --- a/jira/internal/project_impl_test.go +++ b/jira/internal/project_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -32,7 +31,7 @@ func Test_internalProjectImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -58,17 +57,13 @@ func Test_internalProjectImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -91,17 +86,13 @@ func Test_internalProjectImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -124,17 +115,13 @@ func Test_internalProjectImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -192,7 +179,7 @@ func Test_internalProjectImpl_Search(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -221,13 +208,13 @@ func Test_internalProjectImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/search?action=view&categoryId=48882&expand=description&id=10000&id=10001&keys=PA&keys=PB&maxResults=50&orderBy=category&properties=data.is.completed%3F&query=ADM&startAt=0&status=live%2Carchived&typeKey=business%2Cservice_desk", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -252,13 +239,13 @@ func Test_internalProjectImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/search?action=view&categoryId=48882&expand=description&id=10000&id=10001&keys=PA&keys=PB&maxResults=50&orderBy=category&properties=data.is.completed%3F&query=ADM&startAt=0&status=live%2Carchived&typeKey=business%2Cservice_desk", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -283,13 +270,13 @@ func Test_internalProjectImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/search?action=view&categoryId=48882&expand=description&id=10000&id=10001&keys=PA&keys=PB&maxResults=50&orderBy=category&properties=data.is.completed%3F&query=ADM&startAt=0&status=live%2Carchived&typeKey=business%2Cservice_desk", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -334,7 +321,7 @@ func Test_internalProjectImpl_Search(t *testing.T) { func Test_internalProjectImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -362,13 +349,13 @@ func Test_internalProjectImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/KP?expand=issueTypes%2Clead", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -392,13 +379,13 @@ func Test_internalProjectImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/KP?expand=issueTypes%2Clead", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -422,13 +409,13 @@ func Test_internalProjectImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/KP?expand=issueTypes%2Clead", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -487,7 +474,7 @@ func Test_internalProjectImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -515,17 +502,13 @@ func Test_internalProjectImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/project/KP", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -549,17 +532,13 @@ func Test_internalProjectImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/project/KP", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -593,17 +572,13 @@ func Test_internalProjectImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/project/KP", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -647,7 +622,7 @@ func Test_internalProjectImpl_Update(t *testing.T) { func Test_internalProjectImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -675,13 +650,13 @@ func Test_internalProjectImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/project/KP?enableUndo=true", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -705,13 +680,13 @@ func Test_internalProjectImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/project/KP?enableUndo=true", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -745,13 +720,13 @@ func Test_internalProjectImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/project/KP?enableUndo=true", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -794,7 +769,7 @@ func Test_internalProjectImpl_Delete(t *testing.T) { func Test_internalProjectImpl_DeleteAsynchronously(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -820,13 +795,13 @@ func Test_internalProjectImpl_DeleteAsynchronously(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project/KP/delete", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -849,13 +824,13 @@ func Test_internalProjectImpl_DeleteAsynchronously(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/project/KP/delete", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -888,13 +863,13 @@ func Test_internalProjectImpl_DeleteAsynchronously(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project/KP/delete", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -938,7 +913,7 @@ func Test_internalProjectImpl_DeleteAsynchronously(t *testing.T) { func Test_internalProjectImpl_Archive(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -964,13 +939,13 @@ func Test_internalProjectImpl_Archive(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project/KP/archive", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -993,13 +968,13 @@ func Test_internalProjectImpl_Archive(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/project/KP/archive", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1032,13 +1007,13 @@ func Test_internalProjectImpl_Archive(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project/KP/archive", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1081,7 +1056,7 @@ func Test_internalProjectImpl_Archive(t *testing.T) { func Test_internalProjectImpl_Restore(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -1107,13 +1082,13 @@ func Test_internalProjectImpl_Restore(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project/KP/restore", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1136,13 +1111,13 @@ func Test_internalProjectImpl_Restore(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/project/KP/restore", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1175,13 +1150,13 @@ func Test_internalProjectImpl_Restore(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project/KP/restore", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1225,7 +1200,7 @@ func Test_internalProjectImpl_Restore(t *testing.T) { func Test_internalProjectImpl_Statuses(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -1251,13 +1226,13 @@ func Test_internalProjectImpl_Statuses(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/KP/statuses", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1280,13 +1255,13 @@ func Test_internalProjectImpl_Statuses(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/KP/statuses", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1319,13 +1294,13 @@ func Test_internalProjectImpl_Statuses(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/KP/statuses", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1369,7 +1344,7 @@ func Test_internalProjectImpl_Statuses(t *testing.T) { func Test_internalProjectImpl_NotificationScheme(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -1397,13 +1372,13 @@ func Test_internalProjectImpl_NotificationScheme(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/KP/notificationscheme?expand=all%2CprojectRole", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1427,13 +1402,13 @@ func Test_internalProjectImpl_NotificationScheme(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/KP/notificationscheme?expand=all%2CprojectRole", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1457,13 +1432,13 @@ func Test_internalProjectImpl_NotificationScheme(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/KP/notificationscheme?expand=all%2CprojectRole", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1507,7 +1482,7 @@ func Test_internalProjectImpl_NotificationScheme(t *testing.T) { func Test_NewProjectService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From fe1213c98be6614836bf431c23d4d8b7667d23d2 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 16 Jul 2023 03:43:14 -0600 Subject: [PATCH 40/70] :art: Migrated the client interface from Client to Connector on the Jira Project Category service. --- jira/internal/project_category_impl.go | 24 ++--- jira/internal/project_category_impl_test.go | 97 ++++++++------------ jira/internal/project_component_impl_test.go | 2 +- 3 files changed, 44 insertions(+), 79 deletions(-) diff --git a/jira/internal/project_category_impl.go b/jira/internal/project_category_impl.go index 0c27f5c1..a2a625c6 100644 --- a/jira/internal/project_category_impl.go +++ b/jira/internal/project_category_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewProjectCategoryService(client service.Client, version string) (*ProjectCategoryService, error) { +func NewProjectCategoryService(client service.Connector, version string) (*ProjectCategoryService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -70,7 +70,7 @@ func (p *ProjectCategoryService) Delete(ctx context.Context, categoryId int) (*m } type internalProjectCategoryImpl struct { - c service.Client + c service.Connector version string } @@ -78,7 +78,7 @@ func (i *internalProjectCategoryImpl) Gets(ctx context.Context) ([]*model.Projec endpoint := fmt.Sprintf("rest/api/%v/projectCategory", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -100,7 +100,7 @@ func (i *internalProjectCategoryImpl) Get(ctx context.Context, categoryId int) ( endpoint := fmt.Sprintf("rest/api/%v/projectCategory/%v", i.version, categoryId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -116,14 +116,9 @@ func (i *internalProjectCategoryImpl) Get(ctx context.Context, categoryId int) ( func (i *internalProjectCategoryImpl) Create(ctx context.Context, payload *model.ProjectCategoryPayloadScheme) (*model.ProjectCategoryScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/projectCategory", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -143,14 +138,9 @@ func (i *internalProjectCategoryImpl) Update(ctx context.Context, categoryId int return nil, nil, model.ErrNoProjectCategoryIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/projectCategory/%v", i.version, categoryId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -172,7 +162,7 @@ func (i *internalProjectCategoryImpl) Delete(ctx context.Context, categoryId int endpoint := fmt.Sprintf("rest/api/%v/projectCategory/%v", i.version, categoryId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/project_category_impl_test.go b/jira/internal/project_category_impl_test.go index 79491f13..3a645f09 100644 --- a/jira/internal/project_category_impl_test.go +++ b/jira/internal/project_category_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalProjectCategoryImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,13 +39,13 @@ func Test_internalProjectCategoryImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectCategory", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -68,13 +67,13 @@ func Test_internalProjectCategoryImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/projectCategory", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -96,13 +95,13 @@ func Test_internalProjectCategoryImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectCategory", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -146,7 +145,7 @@ func Test_internalProjectCategoryImpl_Gets(t *testing.T) { func Test_internalProjectCategoryImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -172,13 +171,13 @@ func Test_internalProjectCategoryImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectCategory/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -201,13 +200,13 @@ func Test_internalProjectCategoryImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/projectCategory/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -240,13 +239,13 @@ func Test_internalProjectCategoryImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectCategory/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -295,7 +294,7 @@ func Test_internalProjectCategoryImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -321,17 +320,13 @@ func Test_internalProjectCategoryImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/projectCategory", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -354,17 +349,13 @@ func Test_internalProjectCategoryImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/projectCategory", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -387,17 +378,13 @@ func Test_internalProjectCategoryImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/projectCategory", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -446,7 +433,7 @@ func Test_internalProjectCategoryImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -474,17 +461,13 @@ func Test_internalProjectCategoryImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/projectCategory/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -508,17 +491,13 @@ func Test_internalProjectCategoryImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/projectCategory/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -552,17 +531,13 @@ func Test_internalProjectCategoryImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/projectCategory/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -606,7 +581,7 @@ func Test_internalProjectCategoryImpl_Update(t *testing.T) { func Test_internalProjectCategoryImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -632,13 +607,13 @@ func Test_internalProjectCategoryImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/projectCategory/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -661,13 +636,13 @@ func Test_internalProjectCategoryImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/projectCategory/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -700,13 +675,13 @@ func Test_internalProjectCategoryImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/projectCategory/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -749,7 +724,7 @@ func Test_internalProjectCategoryImpl_Delete(t *testing.T) { func Test_NewProjectCategoryService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } diff --git a/jira/internal/project_component_impl_test.go b/jira/internal/project_component_impl_test.go index 306189ca..aed04fc5 100644 --- a/jira/internal/project_component_impl_test.go +++ b/jira/internal/project_component_impl_test.go @@ -915,7 +915,7 @@ func Test_internalProjectComponentImpl_Delete(t *testing.T) { func Test_NewProjectComponentService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 95c3b0c67a381e175e0458a99a94a242892eb2a5 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 16 Jul 2023 03:49:31 -0600 Subject: [PATCH 41/70] :art: Migrated the client interface from Client to Connector on the Jira Project Component service. --- jira/internal/project_component_impl.go | 28 ++--- jira/internal/project_component_impl_test.go | 111 +++++++------------ 2 files changed, 52 insertions(+), 87 deletions(-) diff --git a/jira/internal/project_component_impl.go b/jira/internal/project_component_impl.go index baae61a9..4bd52ac3 100644 --- a/jira/internal/project_component_impl.go +++ b/jira/internal/project_component_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewProjectComponentService(client service.Client, version string) (*ProjectComponentService, error) { +func NewProjectComponentService(client service.Connector, version string) (*ProjectComponentService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -62,7 +62,7 @@ func (p *ProjectComponentService) Delete(ctx context.Context, componentId string // Update updates a component. // -// Any fields included in the request are overwritten +// # Any fields included in the request are overwritten // // PUT /rest/api/{2-3}/component/{id} // @@ -81,20 +81,15 @@ func (p *ProjectComponentService) Get(ctx context.Context, componentId string) ( } type internalProjectComponentImpl struct { - c service.Client + c service.Connector version string } func (i *internalProjectComponentImpl) Create(ctx context.Context, payload *model.ComponentPayloadScheme) (*model.ComponentScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/component", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -116,7 +111,7 @@ func (i *internalProjectComponentImpl) Gets(ctx context.Context, projectIdOrKey endpoint := fmt.Sprintf("rest/api/%v/project/%v/components", i.version, projectIdOrKey) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -138,7 +133,7 @@ func (i *internalProjectComponentImpl) Count(ctx context.Context, componentId st endpoint := fmt.Sprintf("rest/api/%v/component/%v/relatedIssueCounts", i.version, componentId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -160,7 +155,7 @@ func (i *internalProjectComponentImpl) Delete(ctx context.Context, componentId s endpoint := fmt.Sprintf("rest/api/%v/component/%v", i.version, componentId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -174,14 +169,9 @@ func (i *internalProjectComponentImpl) Update(ctx context.Context, componentId s return nil, nil, model.ErrNoComponentIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/component/%v", i.version, componentId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -203,7 +193,7 @@ func (i *internalProjectComponentImpl) Get(ctx context.Context, componentId stri endpoint := fmt.Sprintf("rest/api/%v/component/%v", i.version, componentId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/project_component_impl_test.go b/jira/internal/project_component_impl_test.go index aed04fc5..41b3192c 100644 --- a/jira/internal/project_component_impl_test.go +++ b/jira/internal/project_component_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -25,7 +24,7 @@ func Test_internalProjectComponentImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -51,17 +50,13 @@ func Test_internalProjectComponentImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/component", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -84,17 +79,13 @@ func Test_internalProjectComponentImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/component", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -117,17 +108,13 @@ func Test_internalProjectComponentImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/component", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -180,7 +167,7 @@ func Test_internalProjectComponentImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -208,17 +195,13 @@ func Test_internalProjectComponentImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/component/10393", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -242,17 +225,13 @@ func Test_internalProjectComponentImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/component/10393", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -286,17 +265,13 @@ func Test_internalProjectComponentImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/component/10393", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -340,7 +315,7 @@ func Test_internalProjectComponentImpl_Update(t *testing.T) { func Test_internalProjectComponentImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -366,13 +341,13 @@ func Test_internalProjectComponentImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/components", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -395,13 +370,13 @@ func Test_internalProjectComponentImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/components", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -434,13 +409,13 @@ func Test_internalProjectComponentImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/components", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -484,7 +459,7 @@ func Test_internalProjectComponentImpl_Gets(t *testing.T) { func Test_internalProjectComponentImpl_Count(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -510,13 +485,13 @@ func Test_internalProjectComponentImpl_Count(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/component/10001/relatedIssueCounts", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -539,13 +514,13 @@ func Test_internalProjectComponentImpl_Count(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/component/10001/relatedIssueCounts", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -578,13 +553,13 @@ func Test_internalProjectComponentImpl_Count(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/component/10001/relatedIssueCounts", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -628,7 +603,7 @@ func Test_internalProjectComponentImpl_Count(t *testing.T) { func Test_internalProjectComponentImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -654,13 +629,13 @@ func Test_internalProjectComponentImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/component/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -683,13 +658,13 @@ func Test_internalProjectComponentImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/component/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -722,13 +697,13 @@ func Test_internalProjectComponentImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/component/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -772,7 +747,7 @@ func Test_internalProjectComponentImpl_Get(t *testing.T) { func Test_internalProjectComponentImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -798,13 +773,13 @@ func Test_internalProjectComponentImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/component/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -827,13 +802,13 @@ func Test_internalProjectComponentImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/component/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -866,13 +841,13 @@ func Test_internalProjectComponentImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/component/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -946,7 +921,7 @@ func Test_NewProjectComponentService(t *testing.T) { } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - got, err := NewProjectCategoryService(testCase.args.client, testCase.args.version) + got, err := NewProjectComponentService(testCase.args.client, testCase.args.version) if testCase.wantErr { From a22e1a7756ce9f567182fa1d05764fddd7a2c511 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 16 Jul 2023 15:05:51 -0600 Subject: [PATCH 42/70] :art: Migrated the client interface from Client to Connector on the Jira Project Permission Scheme service. --- .../project_permission_scheme_impl.go | 21 ++----- .../project_permission_scheme_impl_test.go | 61 +++++++------------ 2 files changed, 28 insertions(+), 54 deletions(-) diff --git a/jira/internal/project_permission_scheme_impl.go b/jira/internal/project_permission_scheme_impl.go index 2a57173b..dd935fd0 100644 --- a/jira/internal/project_permission_scheme_impl.go +++ b/jira/internal/project_permission_scheme_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewProjectPermissionSchemeService(client service.Client, version string) (*ProjectPermissionSchemeService, error) { +func NewProjectPermissionSchemeService(client service.Connector, version string) (*ProjectPermissionSchemeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -54,7 +54,7 @@ func (p *ProjectPermissionSchemeService) SecurityLevels(ctx context.Context, pro } type internalProjectPermissionSchemeImpl struct { - c service.Client + c service.Connector version string } @@ -74,7 +74,7 @@ func (i *internalProjectPermissionSchemeImpl) Get(ctx context.Context, projectKe endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -94,20 +94,9 @@ func (i *internalProjectPermissionSchemeImpl) Assign(ctx context.Context, projec return nil, nil, model.ErrNoProjectIDOrKeyError } - payload := struct { - ID int `json:"id"` - }{ - ID: permissionSchemeId, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/project/%v/permissionscheme", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"id": permissionSchemeId}) if err != nil { return nil, nil, err } @@ -129,7 +118,7 @@ func (i *internalProjectPermissionSchemeImpl) SecurityLevels(ctx context.Context endpoint := fmt.Sprintf("rest/api/%v/project/%v/securitylevel", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/project_permission_scheme_impl_test.go b/jira/internal/project_permission_scheme_impl_test.go index 82ed2e64..8cbe816a 100644 --- a/jira/internal/project_permission_scheme_impl_test.go +++ b/jira/internal/project_permission_scheme_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalProjectPermissionSchemeImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -43,13 +42,13 @@ func Test_internalProjectPermissionSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/permissionscheme?expand=field%2Cgroup", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -73,13 +72,13 @@ func Test_internalProjectPermissionSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/permissionscheme?expand=field%2Cgroup", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -113,13 +112,13 @@ func Test_internalProjectPermissionSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/permissionscheme?expand=field%2Cgroup", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -163,7 +162,7 @@ func Test_internalProjectPermissionSchemeImpl_Get(t *testing.T) { func Test_internalProjectPermissionSchemeImpl_SecurityLevels(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -189,13 +188,13 @@ func Test_internalProjectPermissionSchemeImpl_SecurityLevels(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/securitylevel", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -218,13 +217,13 @@ func Test_internalProjectPermissionSchemeImpl_SecurityLevels(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/securitylevel", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -257,13 +256,13 @@ func Test_internalProjectPermissionSchemeImpl_SecurityLevels(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/securitylevel", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -306,12 +305,10 @@ func Test_internalProjectPermissionSchemeImpl_SecurityLevels(t *testing.T) { func Test_internalProjectPermissionSchemeImpl_Assign(t *testing.T) { - payloadMocked := &struct { - ID int "json:\"id\"" - }{ID: 10001} + payloadMocked := map[string]interface{}{"id": 10001} type fields struct { - c service.Client + c service.Connector version string } @@ -339,17 +336,13 @@ func Test_internalProjectPermissionSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/project/DUMMY/permissionscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -373,17 +366,13 @@ func Test_internalProjectPermissionSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/project/DUMMY/permissionscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -417,17 +406,13 @@ func Test_internalProjectPermissionSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/project/DUMMY/permissionscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -471,7 +456,7 @@ func Test_internalProjectPermissionSchemeImpl_Assign(t *testing.T) { func Test_NewProjectPermissionSchemeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 75adef9f7d744b8da6cf9309c2b716c1a17d6c3e Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 16 Jul 2023 15:11:45 -0600 Subject: [PATCH 43/70] :art: Migrated the client interface from Client to Connector on the Jira Project Notification Scheme service. --- jira/internal/notification_scheme_impl.go | 35 ++-- .../internal/notification_scheme_impl_test.go | 149 +++++++----------- 2 files changed, 66 insertions(+), 118 deletions(-) diff --git a/jira/internal/notification_scheme_impl.go b/jira/internal/notification_scheme_impl.go index e1621969..0ad18fdf 100644 --- a/jira/internal/notification_scheme_impl.go +++ b/jira/internal/notification_scheme_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewNotificationSchemeService(client service.Client, version string) (*NotificationSchemeService, error) { +func NewNotificationSchemeService(client service.Connector, version string) (*NotificationSchemeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -112,7 +112,7 @@ func (n *NotificationSchemeService) Remove(ctx context.Context, schemeID, notifi } type internalNotificationSchemeImpl struct { - c service.Client + c service.Connector version string } @@ -143,7 +143,7 @@ func (i *internalNotificationSchemeImpl) Search(ctx context.Context, options *mo endpoint := fmt.Sprintf("rest/api/%v/notificationscheme?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -159,14 +159,9 @@ func (i *internalNotificationSchemeImpl) Search(ctx context.Context, options *mo func (i *internalNotificationSchemeImpl) Create(ctx context.Context, payload *model.NotificationSchemePayloadScheme) (*model.NotificationSchemeCreatedPayload, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/notificationscheme", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -196,7 +191,7 @@ func (i *internalNotificationSchemeImpl) Projects(ctx context.Context, schemeIDs endpoint := fmt.Sprintf("rest/api/%v/notificationscheme/project?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -227,7 +222,7 @@ func (i *internalNotificationSchemeImpl) Get(ctx context.Context, schemeID strin endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -247,14 +242,9 @@ func (i *internalNotificationSchemeImpl) Update(ctx context.Context, schemeID st return nil, model.ErrNoNotificationSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/notificationscheme/%v", i.version, schemeID) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -268,14 +258,9 @@ func (i *internalNotificationSchemeImpl) Append(ctx context.Context, schemeID st return nil, model.ErrNoNotificationSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/notificationscheme/%v/notification", i.version, schemeID) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -291,7 +276,7 @@ func (i *internalNotificationSchemeImpl) Delete(ctx context.Context, schemeID st endpoint := fmt.Sprintf("rest/api/%v/notificationscheme/%v", i.version, schemeID) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -311,7 +296,7 @@ func (i *internalNotificationSchemeImpl) Remove(ctx context.Context, schemeID, n endpoint := fmt.Sprintf("rest/api/%v/notificationscheme/%v/notification/%v", i.version, schemeID, notificationID) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/notification_scheme_impl_test.go b/jira/internal/notification_scheme_impl_test.go index 5af5b247..892dd2b6 100644 --- a/jira/internal/notification_scheme_impl_test.go +++ b/jira/internal/notification_scheme_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalNotificationSchemeImpl_Search(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -49,13 +48,13 @@ func Test_internalNotificationSchemeImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/notificationscheme?expand=all&id=1000&id=1001&maxResults=50&onlyDefault=true&projectId=50001&projectId=50002&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -85,13 +84,13 @@ func Test_internalNotificationSchemeImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/notificationscheme?expand=all&id=1000&id=1001&maxResults=50&onlyDefault=true&projectId=50001&projectId=50002&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -121,13 +120,13 @@ func Test_internalNotificationSchemeImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/notificationscheme?expand=all&id=1000&id=1001&maxResults=50&onlyDefault=true&projectId=50001&projectId=50002&startAt=100", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -172,7 +171,7 @@ func Test_internalNotificationSchemeImpl_Search(t *testing.T) { func Test_internalNotificationSchemeImpl_Projects(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -202,13 +201,13 @@ func Test_internalNotificationSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/notificationscheme/project?maxResults=50¬ificationSchemeId=scheme-id&projectId=10001&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -234,13 +233,13 @@ func Test_internalNotificationSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/notificationscheme/project?maxResults=50¬ificationSchemeId=scheme-id&projectId=10001&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -266,13 +265,13 @@ func Test_internalNotificationSchemeImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/notificationscheme/project?maxResults=50¬ificationSchemeId=scheme-id&projectId=10001&startAt=100", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -318,7 +317,7 @@ func Test_internalNotificationSchemeImpl_Projects(t *testing.T) { func Test_internalNotificationSchemeImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -346,13 +345,13 @@ func Test_internalNotificationSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/notificationscheme/10001?expand=all", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -376,13 +375,13 @@ func Test_internalNotificationSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/notificationscheme/10001?expand=all", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -416,13 +415,13 @@ func Test_internalNotificationSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/notificationscheme/10001?expand=all", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -471,7 +470,7 @@ func Test_internalNotificationSchemeImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -499,17 +498,13 @@ func Test_internalNotificationSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/notificationscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -533,17 +528,13 @@ func Test_internalNotificationSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/notificationscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -577,17 +568,13 @@ func Test_internalNotificationSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/notificationscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -646,7 +633,7 @@ func Test_internalNotificationSchemeImpl_Append(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -674,17 +661,13 @@ func Test_internalNotificationSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/notificationscheme/10001/notification", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -708,17 +691,13 @@ func Test_internalNotificationSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/notificationscheme/10001/notification", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -752,17 +731,13 @@ func Test_internalNotificationSchemeImpl_Append(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/notificationscheme/10001/notification", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -805,7 +780,7 @@ func Test_internalNotificationSchemeImpl_Append(t *testing.T) { func Test_internalNotificationSchemeImpl_Remove(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -833,13 +808,13 @@ func Test_internalNotificationSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/notificationscheme/10001/notification/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -863,13 +838,13 @@ func Test_internalNotificationSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/notificationscheme/10001/notification/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -914,13 +889,13 @@ func Test_internalNotificationSchemeImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/notificationscheme/10001/notification/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -963,7 +938,7 @@ func Test_internalNotificationSchemeImpl_Remove(t *testing.T) { func Test_internalNotificationSchemeImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -989,13 +964,13 @@ func Test_internalNotificationSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/notificationscheme/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1018,13 +993,13 @@ func Test_internalNotificationSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/notificationscheme/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1057,13 +1032,13 @@ func Test_internalNotificationSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/notificationscheme/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1124,7 +1099,7 @@ func Test_internalNotificationSchemeImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1150,17 +1125,13 @@ func Test_internalNotificationSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/notificationscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1183,17 +1154,13 @@ func Test_internalNotificationSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/notificationscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1216,17 +1183,13 @@ func Test_internalNotificationSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/notificationscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client From bce61c6367fc95548984ff1914f76ce6b087844b Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Mon, 17 Jul 2023 00:12:54 -0600 Subject: [PATCH 44/70] :art: Migrated the client interface from Client to Connector on the Jira Project Role service. --- jira/internal/project_role_impl.go | 19 ++---- jira/internal/project_role_impl_test.go | 88 +++++++++++-------------- 2 files changed, 45 insertions(+), 62 deletions(-) diff --git a/jira/internal/project_role_impl.go b/jira/internal/project_role_impl.go index d934cdb9..2c1e60ea 100644 --- a/jira/internal/project_role_impl.go +++ b/jira/internal/project_role_impl.go @@ -13,7 +13,7 @@ import ( "strings" ) -func NewProjectRoleService(client service.Client, version string, actor *ProjectRoleActorService) (*ProjectRoleService, error) { +func NewProjectRoleService(client service.Connector, version string, actor *ProjectRoleActorService) (*ProjectRoleService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -76,7 +76,7 @@ func (p *ProjectRoleService) Create(ctx context.Context, payload *model.ProjectR } type internalProjectRoleImpl struct { - c service.Client + c service.Connector version string } @@ -88,7 +88,7 @@ func (i *internalProjectRoleImpl) Gets(ctx context.Context, projectKeyOrId strin endpoint := fmt.Sprintf("rest/api/%v/project/%v/role", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -133,7 +133,7 @@ func (i *internalProjectRoleImpl) Get(ctx context.Context, projectKeyOrId string endpoint := fmt.Sprintf("rest/api/%v/project/%v/role/%v", i.version, projectKeyOrId, roleId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -155,7 +155,7 @@ func (i *internalProjectRoleImpl) Details(ctx context.Context, projectKeyOrId st endpoint := fmt.Sprintf("rest/api/%v/project/%v/roledetails", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -173,7 +173,7 @@ func (i *internalProjectRoleImpl) Global(ctx context.Context) ([]*model.ProjectR endpoint := fmt.Sprintf("rest/api/%v/role", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -189,14 +189,9 @@ func (i *internalProjectRoleImpl) Global(ctx context.Context) ([]*model.ProjectR func (i *internalProjectRoleImpl) Create(ctx context.Context, payload *model.ProjectRolePayloadScheme) (*model.ProjectRoleScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/role", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/project_role_impl_test.go b/jira/internal/project_role_impl_test.go index e0d303a8..2b15c4e6 100644 --- a/jira/internal/project_role_impl_test.go +++ b/jira/internal/project_role_impl_test.go @@ -16,7 +16,7 @@ import ( func Test_internalProjectRoleImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -44,13 +44,13 @@ func Test_internalProjectRoleImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/role/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -74,13 +74,13 @@ func Test_internalProjectRoleImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/role/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -114,13 +114,13 @@ func Test_internalProjectRoleImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/role/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -174,7 +174,7 @@ func Test_internalProjectRoleImpl_Gets(t *testing.T) { }`) type fields struct { - c service.Client + c service.Connector version string } @@ -200,13 +200,13 @@ func Test_internalProjectRoleImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/role", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -229,13 +229,13 @@ func Test_internalProjectRoleImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/role", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -268,13 +268,13 @@ func Test_internalProjectRoleImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/role", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -292,13 +292,13 @@ func Test_internalProjectRoleImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/role", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -347,7 +347,7 @@ func Test_internalProjectRoleImpl_Gets(t *testing.T) { func Test_internalProjectRoleImpl_Details(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -373,13 +373,13 @@ func Test_internalProjectRoleImpl_Details(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/roledetails", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -402,13 +402,13 @@ func Test_internalProjectRoleImpl_Details(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/roledetails", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -441,13 +441,13 @@ func Test_internalProjectRoleImpl_Details(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/roledetails", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -491,7 +491,7 @@ func Test_internalProjectRoleImpl_Details(t *testing.T) { func Test_internalProjectRoleImpl_Global(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -515,13 +515,13 @@ func Test_internalProjectRoleImpl_Global(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/role", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -543,13 +543,13 @@ func Test_internalProjectRoleImpl_Global(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/role", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -571,13 +571,13 @@ func Test_internalProjectRoleImpl_Global(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/role", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -626,7 +626,7 @@ func Test_internalProjectRoleImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -652,17 +652,13 @@ func Test_internalProjectRoleImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/role", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -685,17 +681,13 @@ func Test_internalProjectRoleImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/role", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -718,17 +710,13 @@ func Test_internalProjectRoleImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/role", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -772,7 +760,7 @@ func Test_internalProjectRoleImpl_Create(t *testing.T) { func Test_NewProjectRoleService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From dfcac3eac46d4da29b099aab44b648be19bb23a5 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Thu, 20 Jul 2023 17:23:49 -0600 Subject: [PATCH 45/70] :art: Migrated the client interface from Client to Connector on the Jira Project Role Actor service. --- jira/internal/project_role_actor_impl.go | 21 ++------ jira/internal/project_role_actor_impl_test.go | 48 +++++++------------ 2 files changed, 20 insertions(+), 49 deletions(-) diff --git a/jira/internal/project_role_actor_impl.go b/jira/internal/project_role_actor_impl.go index a0a72dd2..74979a0b 100644 --- a/jira/internal/project_role_actor_impl.go +++ b/jira/internal/project_role_actor_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewProjectRoleActorService(client service.Client, version string) (*ProjectRoleActorService, error) { +func NewProjectRoleActorService(client service.Connector, version string) (*ProjectRoleActorService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -45,7 +45,7 @@ func (p *ProjectRoleActorService) Delete(ctx context.Context, projectKeyOrId str } type internalProjectRoleActorImpl struct { - c service.Client + c service.Connector version string } @@ -59,22 +59,9 @@ func (i *internalProjectRoleActorImpl) Add(ctx context.Context, projectKeyOrId s return nil, nil, model.ErrNoProjectRoleIDError } - payload := struct { - Group []string `json:"group,omitempty"` - Users []string `json:"user,omitempty"` - }{ - Group: groups, - Users: accountIds, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/project/%v/role/%v", i.version, projectKeyOrId, roleId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"group": groups, "user": accountIds}) if err != nil { return nil, nil, err } @@ -115,7 +102,7 @@ func (i *internalProjectRoleActorImpl) Delete(ctx context.Context, projectKeyOrI endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint.String(), "", nil) if err != nil { return nil, err } diff --git a/jira/internal/project_role_actor_impl_test.go b/jira/internal/project_role_actor_impl_test.go index 35458de4..90ce34fa 100644 --- a/jira/internal/project_role_actor_impl_test.go +++ b/jira/internal/project_role_actor_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -14,13 +13,10 @@ import ( func Test_internalProjectRoleActorImpl_Add(t *testing.T) { - payloadMocked := &struct { - Group []string "json:\"group,omitempty\"" - Users []string "json:\"user,omitempty\"" - }{Group: []string{"jira-users"}, Users: []string{"uuid"}} + payloadMocked := map[string]interface{}{"group": []string{"jira-users"}, "user": []string{"uuid"}} type fields struct { - c service.Client + c service.Connector version string } @@ -51,17 +47,13 @@ func Test_internalProjectRoleActorImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project/DUMMY/role/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -87,17 +79,13 @@ func Test_internalProjectRoleActorImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/project/DUMMY/role/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -144,17 +132,13 @@ func Test_internalProjectRoleActorImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/project/DUMMY/role/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -199,7 +183,7 @@ func Test_internalProjectRoleActorImpl_Add(t *testing.T) { func Test_internalProjectRoleActorImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -230,13 +214,13 @@ func Test_internalProjectRoleActorImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/project/DUMMY/role/10001?group=jira-users&user=uuid", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -262,13 +246,13 @@ func Test_internalProjectRoleActorImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/project/DUMMY/role/10001?group=jira-users&user=uuid", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -315,13 +299,13 @@ func Test_internalProjectRoleActorImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/project/DUMMY/role/10001?group=jira-users&user=uuid", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -365,7 +349,7 @@ func Test_internalProjectRoleActorImpl_Delete(t *testing.T) { func Test_NewProjectRoleActorService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From daa60fe54c4cf8dc066f59bf3c17541773ca4c7d Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Thu, 20 Jul 2023 17:32:20 -0600 Subject: [PATCH 46/70] :art: Migrated the client interface from Client to Connector on the Jira Project Type service. --- jira/internal/project_type_impl.go | 12 ++--- jira/internal/project_type_impl_test.go | 58 ++++++++++++------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/jira/internal/project_type_impl.go b/jira/internal/project_type_impl.go index a556e2f8..0589bebc 100644 --- a/jira/internal/project_type_impl.go +++ b/jira/internal/project_type_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewProjectTypeService(client service.Client, version string) (*ProjectTypeService, error) { +func NewProjectTypeService(client service.Connector, version string) (*ProjectTypeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -61,7 +61,7 @@ func (p *ProjectTypeService) Accessible(ctx context.Context, projectTypeKey stri } type internalProjectTypeImpl struct { - c service.Client + c service.Connector version string } @@ -69,7 +69,7 @@ func (i *internalProjectTypeImpl) Gets(ctx context.Context) ([]*model.ProjectTyp endpoint := fmt.Sprintf("rest/api/%v/project/type", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -87,7 +87,7 @@ func (i *internalProjectTypeImpl) Licensed(ctx context.Context) ([]*model.Projec endpoint := fmt.Sprintf("rest/api/%v/project/type/accessible", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -109,7 +109,7 @@ func (i *internalProjectTypeImpl) Get(ctx context.Context, projectTypeKey string endpoint := fmt.Sprintf("rest/api/%v/project/type/%v", i.version, projectTypeKey) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -131,7 +131,7 @@ func (i *internalProjectTypeImpl) Accessible(ctx context.Context, projectTypeKey endpoint := fmt.Sprintf("rest/api/%v/project/type/%v/accessible", i.version, projectTypeKey) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/project_type_impl_test.go b/jira/internal/project_type_impl_test.go index cc0e5d01..fbdaea0a 100644 --- a/jira/internal/project_type_impl_test.go +++ b/jira/internal/project_type_impl_test.go @@ -15,7 +15,7 @@ import ( func Test_internalProjectTypeImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -39,13 +39,13 @@ func Test_internalProjectTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/type", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -67,13 +67,13 @@ func Test_internalProjectTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/type", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -95,13 +95,13 @@ func Test_internalProjectTypeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/type", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -145,7 +145,7 @@ func Test_internalProjectTypeImpl_Gets(t *testing.T) { func Test_internalProjectTypeImpl_Licensed(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -169,13 +169,13 @@ func Test_internalProjectTypeImpl_Licensed(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/type/accessible", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -197,13 +197,13 @@ func Test_internalProjectTypeImpl_Licensed(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/type/accessible", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -225,13 +225,13 @@ func Test_internalProjectTypeImpl_Licensed(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/type/accessible", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -275,7 +275,7 @@ func Test_internalProjectTypeImpl_Licensed(t *testing.T) { func Test_internalProjectTypeImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -301,13 +301,13 @@ func Test_internalProjectTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/type/business", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -330,13 +330,13 @@ func Test_internalProjectTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/type/business", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -370,13 +370,13 @@ func Test_internalProjectTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/type/business", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -420,7 +420,7 @@ func Test_internalProjectTypeImpl_Get(t *testing.T) { func Test_internalProjectTypeImpl_Accessible(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -446,13 +446,13 @@ func Test_internalProjectTypeImpl_Accessible(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/type/business/accessible", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -475,13 +475,13 @@ func Test_internalProjectTypeImpl_Accessible(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/type/business/accessible", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -515,13 +515,13 @@ func Test_internalProjectTypeImpl_Accessible(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/type/business/accessible", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -565,7 +565,7 @@ func Test_internalProjectTypeImpl_Accessible(t *testing.T) { func Test_NewProjectTypeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From c03ef52d735a902e3f6e91065f2524aee45f5be1 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Thu, 20 Jul 2023 17:58:15 -0600 Subject: [PATCH 47/70] :art: Migrated the client interface from Client to Connector on the Jira Project Version service. --- jira/internal/project_version_impl.go | 32 ++--- jira/internal/project_version_impl_test.go | 139 +++++++++------------ 2 files changed, 68 insertions(+), 103 deletions(-) diff --git a/jira/internal/project_version_impl.go b/jira/internal/project_version_impl.go index 1c744b59..1a4c4e72 100644 --- a/jira/internal/project_version_impl.go +++ b/jira/internal/project_version_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewProjectVersionService(client service.Client, version string) (*ProjectVersionService, error) { +func NewProjectVersionService(client service.Connector, version string) (*ProjectVersionService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -78,7 +78,7 @@ func (p *ProjectVersionService) Update(ctx context.Context, versionId string, pa // Merge merges two project versions. // -// The merge is completed by deleting the version specified in id and replacing any occurrences of +// # The merge is completed by deleting the version specified in id and replacing any occurrences of // // its ID in fixVersion with the version ID specified in moveIssuesTo. // @@ -112,7 +112,7 @@ func (p *ProjectVersionService) UnresolvedIssueCount(ctx context.Context, versio } type internalProjectVersionImpl struct { - c service.Client + c service.Connector version string } @@ -124,7 +124,7 @@ func (i *internalProjectVersionImpl) Gets(ctx context.Context, projectKeyOrId st endpoint := fmt.Sprintf("rest/api/%v/project/%v/versions", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -169,7 +169,7 @@ func (i *internalProjectVersionImpl) Search(ctx context.Context, projectKeyOrId endpoint := fmt.Sprintf("rest/api/%v/project/%v/version?%v", i.version, projectKeyOrId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -185,14 +185,9 @@ func (i *internalProjectVersionImpl) Search(ctx context.Context, projectKeyOrId func (i *internalProjectVersionImpl) Create(ctx context.Context, payload *model.VersionPayloadScheme) (*model.VersionScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/version", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -223,7 +218,7 @@ func (i *internalProjectVersionImpl) Get(ctx context.Context, versionId string, endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -243,14 +238,9 @@ func (i *internalProjectVersionImpl) Update(ctx context.Context, versionId strin return nil, nil, model.ErrNoVersionIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/version/%v", i.version, versionId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -276,7 +266,7 @@ func (i *internalProjectVersionImpl) Merge(ctx context.Context, versionId, versi endpoint := fmt.Sprintf("rest/api/%v/version/%v/mergeto/%v", i.version, versionId, versionMoveIssuesTo) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", nil) if err != nil { return nil, err } @@ -292,7 +282,7 @@ func (i *internalProjectVersionImpl) RelatedIssueCounts(ctx context.Context, ver endpoint := fmt.Sprintf("rest/api/%v/version/%v/relatedIssueCounts", i.version, versionId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -314,7 +304,7 @@ func (i *internalProjectVersionImpl) UnresolvedIssueCount(ctx context.Context, v endpoint := fmt.Sprintf("rest/api/%v/version/%v/unresolvedIssueCount", i.version, versionId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/project_version_impl_test.go b/jira/internal/project_version_impl_test.go index 885babcc..0d6d253f 100644 --- a/jira/internal/project_version_impl_test.go +++ b/jira/internal/project_version_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalProjectVersionImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -42,13 +41,13 @@ func Test_internalProjectVersionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/versions", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -71,13 +70,13 @@ func Test_internalProjectVersionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/versions", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -110,13 +109,13 @@ func Test_internalProjectVersionImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/versions", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -160,7 +159,7 @@ func Test_internalProjectVersionImpl_Gets(t *testing.T) { func Test_internalProjectVersionImpl_Search(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -196,13 +195,13 @@ func Test_internalProjectVersionImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/version?expand=issues&maxResults=50&orderBy=id&query=release&startAt=50&status=released", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -233,13 +232,13 @@ func Test_internalProjectVersionImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/version?expand=issues&maxResults=50&orderBy=id&query=release&startAt=50&status=released", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -280,13 +279,13 @@ func Test_internalProjectVersionImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/version?expand=issues&maxResults=50&orderBy=id&query=release&startAt=50&status=released", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -331,7 +330,7 @@ func Test_internalProjectVersionImpl_Search(t *testing.T) { func Test_internalProjectVersionImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -359,13 +358,13 @@ func Test_internalProjectVersionImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/version/10391?expand=operations", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -389,13 +388,13 @@ func Test_internalProjectVersionImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/version/10391?expand=operations", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -429,13 +428,13 @@ func Test_internalProjectVersionImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/version/10391?expand=operations", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -480,7 +479,7 @@ func Test_internalProjectVersionImpl_Get(t *testing.T) { func Test_internalProjectVersionImpl_RelatedIssueCounts(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -506,13 +505,13 @@ func Test_internalProjectVersionImpl_RelatedIssueCounts(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/version/10391/relatedIssueCounts", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -535,13 +534,13 @@ func Test_internalProjectVersionImpl_RelatedIssueCounts(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/version/10391/relatedIssueCounts", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -574,13 +573,13 @@ func Test_internalProjectVersionImpl_RelatedIssueCounts(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/version/10391/relatedIssueCounts", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -624,7 +623,7 @@ func Test_internalProjectVersionImpl_RelatedIssueCounts(t *testing.T) { func Test_internalProjectVersionImpl_UnresolvedIssueCount(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -650,13 +649,13 @@ func Test_internalProjectVersionImpl_UnresolvedIssueCount(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/version/10391/unresolvedIssueCount", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -679,13 +678,13 @@ func Test_internalProjectVersionImpl_UnresolvedIssueCount(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/version/10391/unresolvedIssueCount", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -718,13 +717,13 @@ func Test_internalProjectVersionImpl_UnresolvedIssueCount(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/version/10391/unresolvedIssueCount", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -768,7 +767,7 @@ func Test_internalProjectVersionImpl_UnresolvedIssueCount(t *testing.T) { func Test_internalProjectVersionImpl_Merge(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -795,13 +794,13 @@ func Test_internalProjectVersionImpl_Merge(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/version/10391/mergeto/10392", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -825,13 +824,13 @@ func Test_internalProjectVersionImpl_Merge(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/version/10391/mergeto/10392", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -876,13 +875,13 @@ func Test_internalProjectVersionImpl_Merge(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/version/10391/mergeto/10392", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -935,7 +934,7 @@ func Test_internalProjectVersionImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -961,17 +960,13 @@ func Test_internalProjectVersionImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/version", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -994,17 +989,13 @@ func Test_internalProjectVersionImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/version", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1027,17 +1018,13 @@ func Test_internalProjectVersionImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/version", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1091,7 +1078,7 @@ func Test_internalProjectVersionImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -1119,17 +1106,13 @@ func Test_internalProjectVersionImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/version/10923", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1153,17 +1136,13 @@ func Test_internalProjectVersionImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/version/10923", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1197,17 +1176,13 @@ func Test_internalProjectVersionImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/version/10923", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1252,7 +1227,7 @@ func Test_internalProjectVersionImpl_Update(t *testing.T) { func Test_NewProjectVersionService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From e6c37e526c9d3938926236469731e4f99d348b97 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Fri, 21 Jul 2023 00:16:39 -0600 Subject: [PATCH 48/70] :art: Migrated the client interface from Client to Connector on the Jira Project Validator service. --- jira/internal/project_validator_impl.go | 10 ++--- jira/internal/project_validator_impl_test.go | 44 ++++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/jira/internal/project_validator_impl.go b/jira/internal/project_validator_impl.go index 25df1e38..8a4b2e45 100644 --- a/jira/internal/project_validator_impl.go +++ b/jira/internal/project_validator_impl.go @@ -10,7 +10,7 @@ import ( "net/url" ) -func NewProjectValidatorService(client service.Client, version string) (*ProjectValidatorService, error) { +func NewProjectValidatorService(client service.Connector, version string) (*ProjectValidatorService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -61,7 +61,7 @@ func (p *ProjectValidatorService) Name(ctx context.Context, name string) (string } type internalProjectValidatorImpl struct { - c service.Client + c service.Connector version string } @@ -76,7 +76,7 @@ func (i *internalProjectValidatorImpl) Validate(ctx context.Context, key string) endpoint := fmt.Sprintf("rest/api/%v/projectvalidate/key?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -101,7 +101,7 @@ func (i *internalProjectValidatorImpl) Key(ctx context.Context, key string) (str endpoint := fmt.Sprintf("rest/api/%v/projectvalidate/validProjectKey?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return "", nil, err } @@ -125,7 +125,7 @@ func (i *internalProjectValidatorImpl) Name(ctx context.Context, name string) (s endpoint := fmt.Sprintf("rest/api/%v/projectvalidate/validProjectName?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return "", nil, err } diff --git a/jira/internal/project_validator_impl_test.go b/jira/internal/project_validator_impl_test.go index b300d4dc..e7965074 100644 --- a/jira/internal/project_validator_impl_test.go +++ b/jira/internal/project_validator_impl_test.go @@ -14,7 +14,7 @@ import ( func Test_internalProjectValidatorImpl_Validate(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,13 +40,13 @@ func Test_internalProjectValidatorImpl_Validate(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectvalidate/key?key=DUMMY", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -69,13 +69,13 @@ func Test_internalProjectValidatorImpl_Validate(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/projectvalidate/key?key=DUMMY", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -108,13 +108,13 @@ func Test_internalProjectValidatorImpl_Validate(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectvalidate/key?key=DUMMY", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -158,7 +158,7 @@ func Test_internalProjectValidatorImpl_Validate(t *testing.T) { func Test_internalProjectValidatorImpl_Key(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -184,13 +184,13 @@ func Test_internalProjectValidatorImpl_Key(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectvalidate/validProjectKey?key=DUMMY", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -213,13 +213,13 @@ func Test_internalProjectValidatorImpl_Key(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/projectvalidate/validProjectKey?key=DUMMY", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -252,13 +252,13 @@ func Test_internalProjectValidatorImpl_Key(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectvalidate/validProjectKey?key=DUMMY", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -302,7 +302,7 @@ func Test_internalProjectValidatorImpl_Key(t *testing.T) { func Test_internalProjectValidatorImpl_Name(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -328,13 +328,13 @@ func Test_internalProjectValidatorImpl_Name(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectvalidate/validProjectName?name=DUMMY+Project", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -357,13 +357,13 @@ func Test_internalProjectValidatorImpl_Name(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/projectvalidate/validProjectName?name=DUMMY+Project", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -396,13 +396,13 @@ func Test_internalProjectValidatorImpl_Name(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/projectvalidate/validProjectName?name=DUMMY+Project", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -446,7 +446,7 @@ func Test_internalProjectValidatorImpl_Name(t *testing.T) { func Test_NewProjectValidatorService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 17ef4f44ad9a6c8400441514bf17ecff0ed4670a Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Fri, 21 Jul 2023 00:24:43 -0600 Subject: [PATCH 49/70] :art: Migrated the client interface from Client to Connector on the Jira Project Feature service. --- jira/internal/project_feature_impl.go | 19 +++---- jira/internal/project_feature_impl_test.go | 59 ++++++++++------------ pkg/infra/models/errors.go | 1 + 3 files changed, 36 insertions(+), 43 deletions(-) diff --git a/jira/internal/project_feature_impl.go b/jira/internal/project_feature_impl.go index ab927d30..58643b16 100644 --- a/jira/internal/project_feature_impl.go +++ b/jira/internal/project_feature_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewProjectFeatureService(client service.Client, version string) (*ProjectFeatureService, error) { +func NewProjectFeatureService(client service.Connector, version string) (*ProjectFeatureService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -43,7 +43,7 @@ func (p *ProjectFeatureService) Set(ctx context.Context, projectKeyOrId, feature } type internalProjectFeatureImpl struct { - c service.Client + c service.Connector version string } @@ -55,7 +55,7 @@ func (i *internalProjectFeatureImpl) Gets(ctx context.Context, projectKeyOrId st endpoint := fmt.Sprintf("rest/api/%v/project/%v/features", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -79,20 +79,15 @@ func (i *internalProjectFeatureImpl) Set(ctx context.Context, projectKeyOrId, fe return nil, nil, model.ErrNoProjectFeatureKeyError } - payload := struct { - State string `json:"state,omitempty"` - }{ - State: state, + if state == "" { + return nil, nil, model.ErrNoProjectFeatureStateError } - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } + payload := map[string]interface{}{"state": state} endpoint := fmt.Sprintf("rest/api/%v/project/%v/features/%v", i.version, projectKeyOrId, featureKey) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/project_feature_impl_test.go b/jira/internal/project_feature_impl_test.go index f33f7edc..1137e44a 100644 --- a/jira/internal/project_feature_impl_test.go +++ b/jira/internal/project_feature_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -14,12 +13,10 @@ import ( func Test_internalProjectFeatureImpl_Set(t *testing.T) { - payloadMocked := &struct { - State string "json:\"state,omitempty\"" - }{State: "ENABLED"} + payloadMocked := map[string]interface{}{"state": "ENABLED"} type fields struct { - c service.Client + c service.Connector version string } @@ -47,17 +44,13 @@ func Test_internalProjectFeatureImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/project/DUMMY/features/jsw.classic.roadmap", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -82,17 +75,13 @@ func Test_internalProjectFeatureImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/project/DUMMY/features/jsw.classic.roadmap", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -127,6 +116,18 @@ func Test_internalProjectFeatureImpl_Set(t *testing.T) { Err: model.ErrNoProjectFeatureKeyError, }, + { + name: "when the project feature state is not provided", + fields: fields{version: "3"}, + args: args{ + ctx: context.TODO(), + projectKeyOrId: "DUMMY", + featureKey: "jsw.classic.roadmap", + }, + wantErr: true, + Err: model.ErrNoProjectFeatureStateError, + }, + { name: "when the http request cannot be created", fields: fields{version: "3"}, @@ -138,17 +139,13 @@ func Test_internalProjectFeatureImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/project/DUMMY/features/jsw.classic.roadmap", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -193,7 +190,7 @@ func Test_internalProjectFeatureImpl_Set(t *testing.T) { func Test_internalProjectFeatureImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -219,13 +216,13 @@ func Test_internalProjectFeatureImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/features", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -248,13 +245,13 @@ func Test_internalProjectFeatureImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/features", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -287,13 +284,13 @@ func Test_internalProjectFeatureImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/features", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -337,7 +334,7 @@ func Test_internalProjectFeatureImpl_Gets(t *testing.T) { func Test_NewProjectFeatureService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } diff --git a/pkg/infra/models/errors.go b/pkg/infra/models/errors.go index 65662fd0..018d286d 100644 --- a/pkg/infra/models/errors.go +++ b/pkg/infra/models/errors.go @@ -82,6 +82,7 @@ var ( ErrNoProjectCategoryIDError = errors.New("jira: no project category id set") ErrNoPropertyKeyError = errors.New("jira: no property key set") ErrNoProjectFeatureKeyError = errors.New("jira: no project feature key set") + ErrNoProjectFeatureStateError = errors.New("jira: no project state key set") ErrNoFieldIDError = errors.New("jira: no field id set") ErrNoEditOperatorError = errors.New("jira: no update operation set") ErrNoOperatorError = errors.New("jira: no operation set") From d2323a9454ca30b379c1db0d9ae5b99c15cfa4b2 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Fri, 21 Jul 2023 00:31:09 -0600 Subject: [PATCH 50/70] :art: Migrated the client interface from Client to Connector on the Jira Property Feature service. --- jira/internal/project_property_impl.go | 17 ++--- jira/internal/project_property_impl_test.go | 71 +++++++++------------ 2 files changed, 35 insertions(+), 53 deletions(-) diff --git a/jira/internal/project_property_impl.go b/jira/internal/project_property_impl.go index bb1f2d20..7918d828 100644 --- a/jira/internal/project_property_impl.go +++ b/jira/internal/project_property_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewProjectPropertyService(client service.Client, version string) (*ProjectPropertyService, error) { +func NewProjectPropertyService(client service.Connector, version string) (*ProjectPropertyService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -67,7 +67,7 @@ func (p *ProjectPropertyService) Delete(ctx context.Context, projectKeyOrId, pro } type internalProjectPropertyImpl struct { - c service.Client + c service.Connector version string } @@ -79,7 +79,7 @@ func (i *internalProjectPropertyImpl) Gets(ctx context.Context, projectKeyOrId s endpoint := fmt.Sprintf("rest/api/%v/project/%v/properties", i.version, projectKeyOrId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -105,7 +105,7 @@ func (i *internalProjectPropertyImpl) Get(ctx context.Context, projectKeyOrId, p endpoint := fmt.Sprintf("rest/api/%v/project/%v/properties/%v", i.version, projectKeyOrId, propertyKey) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -129,14 +129,9 @@ func (i *internalProjectPropertyImpl) Set(ctx context.Context, projectKeyOrId, p return nil, model.ErrNoPropertyKeyError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/project/%v/properties/%v", i.version, projectKeyOrId, propertyKey) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -156,7 +151,7 @@ func (i *internalProjectPropertyImpl) Delete(ctx context.Context, projectKeyOrId endpoint := fmt.Sprintf("rest/api/%v/project/%v/properties/%v", i.version, projectKeyOrId, propertyKey) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/project_property_impl_test.go b/jira/internal/project_property_impl_test.go index bcdd7bc8..9e438625 100644 --- a/jira/internal/project_property_impl_test.go +++ b/jira/internal/project_property_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalProjectPropertyImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -41,13 +40,13 @@ func Test_internalProjectPropertyImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/properties", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -70,13 +69,13 @@ func Test_internalProjectPropertyImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/properties", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -109,13 +108,13 @@ func Test_internalProjectPropertyImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/properties", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -159,7 +158,7 @@ func Test_internalProjectPropertyImpl_Gets(t *testing.T) { func Test_internalProjectPropertyImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -187,13 +186,13 @@ func Test_internalProjectPropertyImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/properties/alliance", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -217,13 +216,13 @@ func Test_internalProjectPropertyImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/project/DUMMY/properties/alliance", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -268,13 +267,13 @@ func Test_internalProjectPropertyImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/project/DUMMY/properties/alliance", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -318,7 +317,7 @@ func Test_internalProjectPropertyImpl_Get(t *testing.T) { func Test_internalProjectPropertyImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -346,13 +345,13 @@ func Test_internalProjectPropertyImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/project/DUMMY/properties/alliance", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -376,13 +375,13 @@ func Test_internalProjectPropertyImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/project/DUMMY/properties/alliance", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -427,13 +426,13 @@ func Test_internalProjectPropertyImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/project/DUMMY/properties/alliance", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -481,7 +480,7 @@ func Test_internalProjectPropertyImpl_Set(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -511,17 +510,13 @@ func Test_internalProjectPropertyImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/project/DUMMY/properties/alliance", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -546,17 +541,13 @@ func Test_internalProjectPropertyImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/project/DUMMY/properties/alliance", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -602,17 +593,13 @@ func Test_internalProjectPropertyImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/project/DUMMY/properties/alliance", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -656,7 +643,7 @@ func Test_internalProjectPropertyImpl_Set(t *testing.T) { func Test_NewProjectPropertyService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 6dc98c1f41752ea325d846b2fc898a915dd93ec6 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Fri, 21 Jul 2023 00:53:03 -0600 Subject: [PATCH 51/70] :art: Migrated the client interface from Client to Connector on the Jira Screen service. --- jira/internal/screen_impl.go | 44 ++++------ jira/internal/screen_impl_test.go | 135 ++++++++++++------------------ 2 files changed, 67 insertions(+), 112 deletions(-) diff --git a/jira/internal/screen_impl.go b/jira/internal/screen_impl.go index 075fb4e1..ff38fcaa 100644 --- a/jira/internal/screen_impl.go +++ b/jira/internal/screen_impl.go @@ -11,7 +11,7 @@ import ( "strconv" ) -func NewScreenService(client service.Client, version string, scheme *ScreenSchemeService, tab *ScreenTabService) (*ScreenService, error) { +func NewScreenService(client service.Connector, version string, scheme *ScreenSchemeService, tab *ScreenTabService) (*ScreenService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -97,7 +97,7 @@ func (s *ScreenService) Available(ctx context.Context, screenId int) ([]*model.A } type internalScreenImpl struct { - c service.Client + c service.Connector version string } @@ -113,7 +113,7 @@ func (i *internalScreenImpl) Fields(ctx context.Context, fieldId string, startAt endpoint := fmt.Sprintf("rest/api/%v/field/%v/screens?%v", i.version, fieldId, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -154,7 +154,7 @@ func (i *internalScreenImpl) Gets(ctx context.Context, options *model.ScreenPara endpoint := fmt.Sprintf("rest/api/%v/screens?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -174,22 +174,15 @@ func (i *internalScreenImpl) Create(ctx context.Context, name, description strin return nil, nil, model.ErrNoScreenNameError } - payload := struct { - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - }{ - Name: name, - Description: description, - } + payload := map[string]interface{}{"name": name} - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err + if description != "" { + payload["description"] = description } endpoint := fmt.Sprintf("rest/api/%v/screens", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -211,7 +204,7 @@ func (i *internalScreenImpl) AddToDefault(ctx context.Context, fieldId string) ( endpoint := fmt.Sprintf("rest/api/%v/screens/addToDefault/%v", i.version, fieldId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, err } @@ -225,22 +218,15 @@ func (i *internalScreenImpl) Update(ctx context.Context, screenId int, name, des return nil, nil, model.ErrNoScreenIDError } - payload := struct { - Name string `json:"name,omitempty"` - Description string `json:"description,omitempty"` - }{ - Name: name, - Description: description, - } + payload := map[string]interface{}{"name": name} - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err + if description != "" { + payload["description"] = description } endpoint := fmt.Sprintf("rest/api/%v/screens/%v", i.version, screenId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -262,7 +248,7 @@ func (i *internalScreenImpl) Delete(ctx context.Context, screenId int) (*model.R endpoint := fmt.Sprintf("rest/api/%v/screens/%v", i.version, screenId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -278,7 +264,7 @@ func (i *internalScreenImpl) Available(ctx context.Context, screenId int) ([]*mo endpoint := fmt.Sprintf("rest/api/%v/screens/%v/availableFields", i.version, screenId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/screen_impl_test.go b/jira/internal/screen_impl_test.go index e8f3806e..5bc9f544 100644 --- a/jira/internal/screen_impl_test.go +++ b/jira/internal/screen_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalScreenImpl_Fields(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -45,13 +44,13 @@ func Test_internalScreenImpl_Fields(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/customfield_12000/screens?maxResults=50&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -76,13 +75,13 @@ func Test_internalScreenImpl_Fields(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/field/customfield_12000/screens?maxResults=50&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -117,13 +116,13 @@ func Test_internalScreenImpl_Fields(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/field/customfield_12000/screens?maxResults=50&startAt=100", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -168,7 +167,7 @@ func Test_internalScreenImpl_Fields(t *testing.T) { func Test_internalScreenImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -202,13 +201,13 @@ func Test_internalScreenImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screens?id=10002&id=10002&maxResults=50&orderBy=name&queryString=Default+DUMMY+Screen&scope=GLOBAL&scope=TEMPLATE&scope=PROJECT&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -238,13 +237,13 @@ func Test_internalScreenImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/screens?id=10002&id=10002&maxResults=50&orderBy=name&queryString=Default+DUMMY+Screen&scope=GLOBAL&scope=TEMPLATE&scope=PROJECT&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -274,13 +273,13 @@ func Test_internalScreenImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screens?id=10002&id=10002&maxResults=50&orderBy=name&queryString=Default+DUMMY+Screen&scope=GLOBAL&scope=TEMPLATE&scope=PROJECT&startAt=100", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -325,7 +324,7 @@ func Test_internalScreenImpl_Gets(t *testing.T) { func Test_internalScreenImpl_AddToDefault(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -351,13 +350,13 @@ func Test_internalScreenImpl_AddToDefault(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/addToDefault/customfield_12700", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -380,13 +379,13 @@ func Test_internalScreenImpl_AddToDefault(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/screens/addToDefault/customfield_12700", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -419,13 +418,13 @@ func Test_internalScreenImpl_AddToDefault(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/addToDefault/customfield_12700", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -468,7 +467,7 @@ func Test_internalScreenImpl_AddToDefault(t *testing.T) { func Test_internalScreenImpl_Available(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -494,13 +493,13 @@ func Test_internalScreenImpl_Available(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screens/10001/availableFields", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -523,13 +522,13 @@ func Test_internalScreenImpl_Available(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/screens/10001/availableFields", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -562,13 +561,13 @@ func Test_internalScreenImpl_Available(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screens/10001/availableFields", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -611,13 +610,10 @@ func Test_internalScreenImpl_Available(t *testing.T) { func Test_internalScreenImpl_Create(t *testing.T) { - payloadMocked := &struct { - Name string "json:\"name,omitempty\"" - Description string "json:\"description,omitempty\"" - }{Name: "Screen Name", Description: "Screen Description"} + payloadMocked := map[string]interface{}{"description": "Screen Description", "name": "Screen Name"} type fields struct { - c service.Client + c service.Connector version string } @@ -644,17 +640,13 @@ func Test_internalScreenImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -678,17 +670,13 @@ func Test_internalScreenImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/screens", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -722,17 +710,13 @@ func Test_internalScreenImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -775,13 +759,10 @@ func Test_internalScreenImpl_Create(t *testing.T) { func Test_internalScreenImpl_Update(t *testing.T) { - payloadMocked := &struct { - Name string "json:\"name,omitempty\"" - Description string "json:\"description,omitempty\"" - }{Name: "Screen Name", Description: "Screen Description"} + payloadMocked := map[string]interface{}{"description": "Screen Description", "name": "Screen Name"} type fields struct { - c service.Client + c service.Connector version string } @@ -810,17 +791,13 @@ func Test_internalScreenImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/screens/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -845,17 +822,13 @@ func Test_internalScreenImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/screens/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -890,17 +863,13 @@ func Test_internalScreenImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/screens/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -945,7 +914,7 @@ func Test_internalScreenImpl_Update(t *testing.T) { func Test_internalScreenImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -971,13 +940,13 @@ func Test_internalScreenImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/screens/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1000,13 +969,13 @@ func Test_internalScreenImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/screens/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1039,13 +1008,13 @@ func Test_internalScreenImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/screens/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1088,7 +1057,7 @@ func Test_internalScreenImpl_Delete(t *testing.T) { func Test_NewScreenService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 073556a824a4ad0657d37ce65923b32f7a605976 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 15:38:26 -0600 Subject: [PATCH 52/70] :art: Migrated the client interface from Client to Connector on the Jira Screen Tab service. --- jira/internal/screen_tab_impl.go | 36 ++------- jira/internal/screen_tab_impl_test.go | 105 ++++++++++---------------- 2 files changed, 45 insertions(+), 96 deletions(-) diff --git a/jira/internal/screen_tab_impl.go b/jira/internal/screen_tab_impl.go index 58d01163..999f716e 100644 --- a/jira/internal/screen_tab_impl.go +++ b/jira/internal/screen_tab_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewScreenTabService(client service.Client, version string, field *ScreenTabFieldService) (*ScreenTabService, error) { +func NewScreenTabService(client service.Connector, version string, field *ScreenTabFieldService) (*ScreenTabService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -74,7 +74,7 @@ func (s *ScreenTabService) Move(ctx context.Context, screenId, tabId, position i } type internalScreenTabImpl struct { - c service.Client + c service.Connector version string } @@ -95,7 +95,7 @@ func (i *internalScreenTabImpl) Gets(ctx context.Context, screenId int, projectK endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -119,20 +119,9 @@ func (i *internalScreenTabImpl) Create(ctx context.Context, screenId int, tabNam return nil, nil, model.ErrNoScreenTabNameError } - payload := struct { - Name string `json:"name"` - }{ - Name: tabName, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/screens/%v/tabs", i.version, screenId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"name": tabName}) if err != nil { return nil, nil, err } @@ -160,20 +149,9 @@ func (i *internalScreenTabImpl) Update(ctx context.Context, screenId, tabId int, return nil, nil, model.ErrNoScreenTabNameError } - payload := struct { - Name string `json:"name"` - }{ - Name: newTabName, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/screens/%v/tabs/%v", i.version, screenId, tabId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"name": newTabName}) if err != nil { return nil, nil, err } @@ -199,7 +177,7 @@ func (i *internalScreenTabImpl) Delete(ctx context.Context, screenId, tabId int) endpoint := fmt.Sprintf("rest/api/%v/screens/%v/tabs/%v", i.version, screenId, tabId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -219,7 +197,7 @@ func (i *internalScreenTabImpl) Move(ctx context.Context, screenId, tabId, posit endpoint := fmt.Sprintf("rest/api/%v/screens/%v/tabs/%v/move/%v", i.version, screenId, tabId, position) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/screen_tab_impl_test.go b/jira/internal/screen_tab_impl_test.go index d1e7f47b..4f558b2b 100644 --- a/jira/internal/screen_tab_impl_test.go +++ b/jira/internal/screen_tab_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalScreenTabImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -44,13 +43,13 @@ func Test_internalScreenTabImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screens/10002/tabs?projectKey=DUMMY", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -74,13 +73,13 @@ func Test_internalScreenTabImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/screens/10002/tabs?projectKey=DUMMY", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -114,13 +113,13 @@ func Test_internalScreenTabImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screens/10002/tabs?projectKey=DUMMY", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -163,12 +162,10 @@ func Test_internalScreenTabImpl_Gets(t *testing.T) { func Test_internalScreenTabImpl_Create(t *testing.T) { - payloadMocked := &struct { - Name string "json:\"name\"" - }{Name: "Time Tracking"} + payloadMocked := map[string]interface{}{"name": "Time Tracking"} type fields struct { - c service.Client + c service.Connector version string } @@ -196,17 +193,13 @@ func Test_internalScreenTabImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/10002/tabs", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -230,17 +223,13 @@ func Test_internalScreenTabImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/screens/10002/tabs", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -285,17 +274,13 @@ func Test_internalScreenTabImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/10002/tabs", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -338,12 +323,10 @@ func Test_internalScreenTabImpl_Create(t *testing.T) { func Test_internalScreenTabImpl_Update(t *testing.T) { - payloadMocked := &struct { - Name string "json:\"name\"" - }{Name: "Time Tracking"} + payloadMocked := map[string]interface{}{"name": "Time Tracking"} type fields struct { - c service.Client + c service.Connector version string } @@ -373,17 +356,13 @@ func Test_internalScreenTabImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/screens/10002/tabs/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -408,17 +387,13 @@ func Test_internalScreenTabImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/screens/10002/tabs/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -476,17 +451,13 @@ func Test_internalScreenTabImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/screens/10002/tabs/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -531,7 +502,7 @@ func Test_internalScreenTabImpl_Update(t *testing.T) { func Test_internalScreenTabImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -559,13 +530,13 @@ func Test_internalScreenTabImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/screens/10002/tabs/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -589,13 +560,13 @@ func Test_internalScreenTabImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/screens/10002/tabs/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -640,13 +611,13 @@ func Test_internalScreenTabImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/screens/10002/tabs/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -689,7 +660,7 @@ func Test_internalScreenTabImpl_Delete(t *testing.T) { func Test_internalScreenTabImpl_Move(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -719,13 +690,13 @@ func Test_internalScreenTabImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/10002/tabs/10001/move/1", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -750,13 +721,13 @@ func Test_internalScreenTabImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/screens/10002/tabs/10001/move/1", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -802,13 +773,13 @@ func Test_internalScreenTabImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/10002/tabs/10001/move/1", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -852,7 +823,7 @@ func Test_internalScreenTabImpl_Move(t *testing.T) { func Test_NewScreenTabService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 11b37a0c5f66e538c610ae470ffe83f9202f7842 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 15:54:29 -0600 Subject: [PATCH 53/70] :art: Migrated the client interface from Client to Connector on the Jira Screen Tab Field service. --- jira/internal/screen_tab_field_impl.go | 36 ++------ jira/internal/screen_tab_field_impl_test.go | 92 +++++++-------------- 2 files changed, 37 insertions(+), 91 deletions(-) diff --git a/jira/internal/screen_tab_field_impl.go b/jira/internal/screen_tab_field_impl.go index 932a1504..1f894e32 100644 --- a/jira/internal/screen_tab_field_impl.go +++ b/jira/internal/screen_tab_field_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewScreenTabFieldService(client service.Client, version string) (*ScreenTabFieldService, error) { +func NewScreenTabFieldService(client service.Connector, version string) (*ScreenTabFieldService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -63,7 +63,7 @@ func (s *ScreenTabFieldService) Move(ctx context.Context, screenId, tabId int, f } type internalScreenTabFieldImpl struct { - c service.Client + c service.Connector version string } @@ -79,7 +79,7 @@ func (i *internalScreenTabFieldImpl) Gets(ctx context.Context, screenId, tabId i endpoint := fmt.Sprintf("rest/api/%v/screens/%v/tabs/%v/fields", i.version, screenId, tabId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -107,20 +107,9 @@ func (i *internalScreenTabFieldImpl) Add(ctx context.Context, screenId, tabId in return nil, nil, model.ErrNoFieldIDError } - payload := struct { - FieldID string `json:"fieldId"` - }{ - FieldID: fieldId, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/screens/%v/tabs/%v/fields", i.version, screenId, tabId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"fieldId": fieldId}) if err != nil { return nil, nil, err } @@ -150,7 +139,7 @@ func (i *internalScreenTabFieldImpl) Remove(ctx context.Context, screenId, tabId endpoint := fmt.Sprintf("rest/api/%v/screens/%v/tabs/%v/fields/%v", i.version, screenId, tabId, fieldId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -172,22 +161,9 @@ func (i *internalScreenTabFieldImpl) Move(ctx context.Context, screenId, tabId i return nil, model.ErrNoFieldIDError } - payload := struct { - After string `json:"after,omitempty"` - Position string `json:"position,omitempty"` - }{ - After: after, - Position: position, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/screens/%v/tabs/%v/fields/%v/move", i.version, screenId, tabId, fieldId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"after": after, "position": position}) if err != nil { return nil, err } diff --git a/jira/internal/screen_tab_field_impl_test.go b/jira/internal/screen_tab_field_impl_test.go index 242be85d..a7ba6f07 100644 --- a/jira/internal/screen_tab_field_impl_test.go +++ b/jira/internal/screen_tab_field_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalScreenTabFieldImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -43,13 +42,13 @@ func Test_internalScreenTabFieldImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screens/10002/tabs/18272/fields", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -73,13 +72,13 @@ func Test_internalScreenTabFieldImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/screens/10002/tabs/18272/fields", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -124,13 +123,13 @@ func Test_internalScreenTabFieldImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screens/10002/tabs/18272/fields", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -173,12 +172,10 @@ func Test_internalScreenTabFieldImpl_Gets(t *testing.T) { func Test_internalScreenTabFieldImpl_Add(t *testing.T) { - payloadMocked := &struct { - FieldID string "json:\"fieldId\"" - }{FieldID: "customfield_10001"} + payloadMocked := map[string]interface{}{"fieldId": "customfield_10001"} type fields struct { - c service.Client + c service.Connector version string } @@ -207,17 +204,13 @@ func Test_internalScreenTabFieldImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/10002/tabs/18272/fields", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -242,17 +235,13 @@ func Test_internalScreenTabFieldImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/screens/10002/tabs/18272/fields", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -310,17 +299,13 @@ func Test_internalScreenTabFieldImpl_Add(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/10002/tabs/18272/fields", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -365,7 +350,7 @@ func Test_internalScreenTabFieldImpl_Add(t *testing.T) { func Test_internalScreenTabFieldImpl_Remove(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -394,13 +379,13 @@ func Test_internalScreenTabFieldImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/screens/10002/tabs/18272/fields/customfield_10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -425,13 +410,13 @@ func Test_internalScreenTabFieldImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/screens/10002/tabs/18272/fields/customfield_10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -489,13 +474,13 @@ func Test_internalScreenTabFieldImpl_Remove(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/screens/10002/tabs/18272/fields/customfield_10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -538,13 +523,10 @@ func Test_internalScreenTabFieldImpl_Remove(t *testing.T) { func Test_internalScreenTabFieldImpl_Move(t *testing.T) { - payloadMocked := &struct { - After string "json:\"after,omitempty\"" - Position string "json:\"position,omitempty\"" - }{After: "", Position: "First"} + payloadMocked := map[string]interface{}{"after": "", "position": "First"} type fields struct { - c service.Client + c service.Connector version string } @@ -575,17 +557,13 @@ func Test_internalScreenTabFieldImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/10002/tabs/18272/fields/customfield_10001/move", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -611,17 +589,13 @@ func Test_internalScreenTabFieldImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/screens/10002/tabs/18272/fields/customfield_10001/move", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -680,17 +654,13 @@ func Test_internalScreenTabFieldImpl_Move(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screens/10002/tabs/18272/fields/customfield_10001/move", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -734,7 +704,7 @@ func Test_internalScreenTabFieldImpl_Move(t *testing.T) { func Test_NewScreenTabFieldService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 5fee07dd340830a519dcce1fa63917242e17429b Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 16:03:13 -0600 Subject: [PATCH 54/70] :art: Migrated the client interface from Client to Connector on the Jira Screen Scheme service. --- jira/internal/screen_scheme_impl.go | 22 ++----- jira/internal/screen_scheme_impl_test.go | 83 +++++++++--------------- 2 files changed, 35 insertions(+), 70 deletions(-) diff --git a/jira/internal/screen_scheme_impl.go b/jira/internal/screen_scheme_impl.go index 646d2eb5..f1ca7c28 100644 --- a/jira/internal/screen_scheme_impl.go +++ b/jira/internal/screen_scheme_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewScreenSchemeService(client service.Client, version string) (*ScreenSchemeService, error) { +func NewScreenSchemeService(client service.Connector, version string) (*ScreenSchemeService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -68,7 +68,7 @@ func (s *ScreenSchemeService) Delete(ctx context.Context, screenSchemeId string) } type internalScreenSchemeImpl struct { - c service.Client + c service.Connector version string } @@ -99,7 +99,7 @@ func (i *internalScreenSchemeImpl) Gets(ctx context.Context, options *model.Scre endpoint := fmt.Sprintf("rest/api/%v/screenscheme?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -115,14 +115,9 @@ func (i *internalScreenSchemeImpl) Gets(ctx context.Context, options *model.Scre func (i *internalScreenSchemeImpl) Create(ctx context.Context, payload *model.ScreenSchemePayloadScheme) (*model.ScreenSchemeScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/screenscheme", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -142,14 +137,9 @@ func (i *internalScreenSchemeImpl) Update(ctx context.Context, screenSchemeId st return nil, model.ErrNoScreenSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/screenscheme/%v", i.version, screenSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -165,7 +155,7 @@ func (i *internalScreenSchemeImpl) Delete(ctx context.Context, screenSchemeId st endpoint := fmt.Sprintf("rest/api/%v/screenscheme/%v", i.version, screenSchemeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/screen_scheme_impl_test.go b/jira/internal/screen_scheme_impl_test.go index 245b62fa..597c4ed3 100644 --- a/jira/internal/screen_scheme_impl_test.go +++ b/jira/internal/screen_scheme_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalScreenSchemeImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -49,13 +48,13 @@ func Test_internalScreenSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screenscheme?=name&expand=issueTypeScreenSchemes&id=1292&id=38403&maxResults=0&queryString=DUMMY+Screen&startAt=0", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -85,13 +84,13 @@ func Test_internalScreenSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/screenscheme?=name&expand=issueTypeScreenSchemes&id=1292&id=38403&maxResults=0&queryString=DUMMY+Screen&startAt=0", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -121,13 +120,13 @@ func Test_internalScreenSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/screenscheme?=name&expand=issueTypeScreenSchemes&id=1292&id=38403&maxResults=0&queryString=DUMMY+Screen&startAt=0", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -182,7 +181,7 @@ func Test_internalScreenSchemeImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -208,17 +207,13 @@ func Test_internalScreenSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screenscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -241,17 +236,13 @@ func Test_internalScreenSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/screenscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -274,17 +265,13 @@ func Test_internalScreenSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/screenscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -338,7 +325,7 @@ func Test_internalScreenSchemeImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -366,17 +353,13 @@ func Test_internalScreenSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/screenscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -400,17 +383,13 @@ func Test_internalScreenSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/screenscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -444,17 +423,13 @@ func Test_internalScreenSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/screenscheme/10001", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -497,7 +472,7 @@ func Test_internalScreenSchemeImpl_Update(t *testing.T) { func Test_internalScreenSchemeImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -523,13 +498,13 @@ func Test_internalScreenSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/screenscheme/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -552,13 +527,13 @@ func Test_internalScreenSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/screenscheme/10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -591,13 +566,13 @@ func Test_internalScreenSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/screenscheme/10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -640,7 +615,7 @@ func Test_internalScreenSchemeImpl_Delete(t *testing.T) { func Test_NewScreenSchemeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 7161bc0f4a62d2506799fb14fa8ccac7e25482fd Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 19:53:46 -0600 Subject: [PATCH 55/70] :art: Migrated the client interface from Client to Connector on the Jira Server service. --- jira/internal/server_impl.go | 6 +++--- jira/internal/server_impl_test.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/jira/internal/server_impl.go b/jira/internal/server_impl.go index 412f0ecf..5d36f4f1 100644 --- a/jira/internal/server_impl.go +++ b/jira/internal/server_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewServerService(client service.Client, version string) (*ServerService, error) { +func NewServerService(client service.Connector, version string) (*ServerService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -34,7 +34,7 @@ func (s *ServerService) Info(ctx context.Context) (*model.ServerInformationSchem } type internalServerServiceImpl struct { - c service.Client + c service.Connector version string } @@ -42,7 +42,7 @@ func (i *internalServerServiceImpl) Info(ctx context.Context) (*model.ServerInfo endpoint := fmt.Sprintf("rest/api/%v/serverInfo", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/server_impl_test.go b/jira/internal/server_impl_test.go index baa0626f..369d8d54 100644 --- a/jira/internal/server_impl_test.go +++ b/jira/internal/server_impl_test.go @@ -14,7 +14,7 @@ import ( func Test_internalServerServiceImpl_Info(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -38,13 +38,13 @@ func Test_internalServerServiceImpl_Info(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/serverInfo", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -66,13 +66,13 @@ func Test_internalServerServiceImpl_Info(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/serverInfo", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -94,13 +94,13 @@ func Test_internalServerServiceImpl_Info(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/serverInfo", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -144,7 +144,7 @@ func Test_internalServerServiceImpl_Info(t *testing.T) { func Test_NewServerService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 1cd5b0096f741cd54e1f410c3269f3d6d7eeade5 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 20:11:36 -0600 Subject: [PATCH 56/70] :art: Migrated the client interface from Client to Connector on the Jira JQL service. --- jira/internal/jql_impl.go | 17 +++----------- jira/internal/jql_impl_test.go | 41 ++++++++++++++-------------------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/jira/internal/jql_impl.go b/jira/internal/jql_impl.go index 9f20462a..634a60f4 100644 --- a/jira/internal/jql_impl.go +++ b/jira/internal/jql_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewJQLService(client service.Client, version string) (*JQLService, error) { +func NewJQLService(client service.Connector, version string) (*JQLService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -38,7 +38,7 @@ func (j *JQLService) Parse(ctx context.Context, validationType string, JqlQuerie } type internalJQLServiceImpl struct { - c service.Client + c service.Connector version string } @@ -54,18 +54,7 @@ func (i *internalJQLServiceImpl) Parse(ctx context.Context, validationType strin endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - payload := struct { - Queries []string `json:"queries,omitempty"` - }{ - Queries: JqlQueries, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint.String(), "", map[string]interface{}{"queries": JqlQueries}) if err != nil { return nil, nil, err } diff --git a/jira/internal/jql_impl_test.go b/jira/internal/jql_impl_test.go index e9cdf850..7dd00910 100644 --- a/jira/internal/jql_impl_test.go +++ b/jira/internal/jql_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -14,12 +13,18 @@ import ( func Test_internalJQLServiceImpl_Parse(t *testing.T) { - payloadMocked := &struct { - Queries []string "json:\"queries,omitempty\"" - }{Queries: []string{"summary ~ test AND (labels in (urgent, blocker) OR lastCommentedBy = currentUser()) AND status CHANGED AFTER startOfMonth(-1M) ORDER BY updated DESC", "invalid query", "summary = test", "summary in test", "project = INVALID", "universe = 42"}} + payloadMocked := map[string]interface{}{ + "queries": []string{ + "summary ~ test AND (labels in (urgent, blocker) OR lastCommentedBy = currentUser()) AND status CHANGED AFTER startOfMonth(-1M) ORDER BY updated DESC", + "invalid query", + "summary = test", + "summary in test", + "project = INVALID", + "universe = 42", + }} type fields struct { - c service.Client + c service.Connector version string } @@ -53,17 +58,13 @@ func Test_internalJQLServiceImpl_Parse(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "/rest/api/3/jql/parse?validation=strict", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -94,17 +95,13 @@ func Test_internalJQLServiceImpl_Parse(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "/rest/api/2/jql/parse?validation=strict", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -135,17 +132,13 @@ func Test_internalJQLServiceImpl_Parse(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "/rest/api/3/jql/parse?validation=strict", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -191,7 +184,7 @@ func Test_internalJQLServiceImpl_Parse(t *testing.T) { func Test_NewJQLService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 6526f95acb1b15c132dcbf862c3db37d89ef87f6 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 20:17:20 -0600 Subject: [PATCH 57/70] :art: Migrated the client interface from Client to Connector on the Jira Task service. --- jira/internal/task_impl.go | 8 ++++---- jira/internal/task_impl_test.go | 30 +++++++++++++++--------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/jira/internal/task_impl.go b/jira/internal/task_impl.go index 705b1ece..5b32eee0 100644 --- a/jira/internal/task_impl.go +++ b/jira/internal/task_impl.go @@ -9,7 +9,7 @@ import ( "net/http" ) -func NewTaskService(client service.Client, version string) (*TaskService, error) { +func NewTaskService(client service.Connector, version string) (*TaskService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -49,7 +49,7 @@ func (t *TaskService) Cancel(ctx context.Context, taskId string) (*model.Respons } type internalTaskServiceImpl struct { - c service.Client + c service.Connector version string } @@ -61,7 +61,7 @@ func (i *internalTaskServiceImpl) Get(ctx context.Context, taskId string) (*mode endpoint := fmt.Sprintf("rest/api/%v/task/%v", i.version, taskId) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -82,7 +82,7 @@ func (i *internalTaskServiceImpl) Cancel(ctx context.Context, taskId string) (*m } endpoint := fmt.Sprintf("rest/api/%v/task/%v/cancel", i.version, taskId) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/task_impl_test.go b/jira/internal/task_impl_test.go index 848a43bf..6fa6ebec 100644 --- a/jira/internal/task_impl_test.go +++ b/jira/internal/task_impl_test.go @@ -14,7 +14,7 @@ import ( func Test_internalTaskServiceImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,13 +40,13 @@ func Test_internalTaskServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/task/uuid-sample", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -69,13 +69,13 @@ func Test_internalTaskServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/task/uuid-sample", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -108,13 +108,13 @@ func Test_internalTaskServiceImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/task/uuid-sample", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -158,7 +158,7 @@ func Test_internalTaskServiceImpl_Get(t *testing.T) { func Test_internalTaskServiceImpl_Cancel(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -184,13 +184,13 @@ func Test_internalTaskServiceImpl_Cancel(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/task/uuid-sample/cancel", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -213,13 +213,13 @@ func Test_internalTaskServiceImpl_Cancel(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/task/uuid-sample/cancel", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -252,13 +252,13 @@ func Test_internalTaskServiceImpl_Cancel(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/task/uuid-sample/cancel", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -301,7 +301,7 @@ func Test_internalTaskServiceImpl_Cancel(t *testing.T) { func Test_NewTaskService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From cd1f0565137aa3c85f4689e17e3a339421be6a7d Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 20:23:23 -0600 Subject: [PATCH 58/70] :art: Migrated the client interface from Client to Connector on the Jira User service. --- jira/internal/user_impl.go | 21 +++---- jira/internal/user_impl_test.go | 99 ++++++++++++++------------------- 2 files changed, 51 insertions(+), 69 deletions(-) diff --git a/jira/internal/user_impl.go b/jira/internal/user_impl.go index a95f2d5a..fe63ea84 100644 --- a/jira/internal/user_impl.go +++ b/jira/internal/user_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewUserService(client service.Client, version string, connector *UserSearchService) (*UserService, error) { +func NewUserService(client service.Connector, version string, connector *UserSearchService) (*UserService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -96,7 +96,7 @@ func (u *UserService) Gets(ctx context.Context, startAt, maxResults int) ([]*mod } type internalUserImpl struct { - c service.Client + c service.Connector version string } @@ -115,7 +115,7 @@ func (i *internalUserImpl) Get(ctx context.Context, accountId string, expand []s endpoint := fmt.Sprintf("rest/api/%v/user?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -131,14 +131,9 @@ func (i *internalUserImpl) Get(ctx context.Context, accountId string, expand []s func (i *internalUserImpl) Create(ctx context.Context, payload *model.UserPayloadScheme) (*model.UserScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/user", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -162,7 +157,7 @@ func (i *internalUserImpl) Delete(ctx context.Context, accountId string) (*model params.Add("accountId", accountId) endpoint := fmt.Sprintf("rest/api/%v/user?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -186,7 +181,7 @@ func (i *internalUserImpl) Find(ctx context.Context, accountIds []string, startA endpoint := fmt.Sprintf("rest/api/%v/user/bulk?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -210,7 +205,7 @@ func (i *internalUserImpl) Groups(ctx context.Context, accountId string) ([]*mod params.Add("accountId", accountId) endpoint := fmt.Sprintf("rest/api/%v/user/groups?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -232,7 +227,7 @@ func (i *internalUserImpl) Gets(ctx context.Context, startAt, maxResults int) ([ endpoint := fmt.Sprintf("rest/api/%v/users/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/user_impl_test.go b/jira/internal/user_impl_test.go index 08bdb762..053952ff 100644 --- a/jira/internal/user_impl_test.go +++ b/jira/internal/user_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalUserImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -44,13 +43,13 @@ func Test_internalUserImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user?accountId=uuid-sample&expand=groups%2CapplicationRoles", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -74,13 +73,13 @@ func Test_internalUserImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/user?accountId=uuid-sample&expand=groups%2CapplicationRoles", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -114,13 +113,13 @@ func Test_internalUserImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user?accountId=uuid-sample&expand=groups%2CapplicationRoles", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -165,7 +164,7 @@ func Test_internalUserImpl_Get(t *testing.T) { func Test_internalUserImpl_Find(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -194,13 +193,13 @@ func Test_internalUserImpl_Find(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/bulk?accountId=uuid-sample-1&accountId=uuid-sample-2&maxResults=25&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -225,13 +224,13 @@ func Test_internalUserImpl_Find(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/user/bulk?accountId=uuid-sample-1&accountId=uuid-sample-2&maxResults=25&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -266,13 +265,13 @@ func Test_internalUserImpl_Find(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/bulk?accountId=uuid-sample-1&accountId=uuid-sample-2&maxResults=25&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -317,7 +316,7 @@ func Test_internalUserImpl_Find(t *testing.T) { func Test_internalUserImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -343,13 +342,13 @@ func Test_internalUserImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/user?accountId=uuid-sample", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -372,13 +371,13 @@ func Test_internalUserImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/user?accountId=uuid-sample", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -411,13 +410,13 @@ func Test_internalUserImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/user?accountId=uuid-sample", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -460,7 +459,7 @@ func Test_internalUserImpl_Delete(t *testing.T) { func Test_internalUserImpl_Groups(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -486,13 +485,13 @@ func Test_internalUserImpl_Groups(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/groups?accountId=uuid-sample", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -515,13 +514,13 @@ func Test_internalUserImpl_Groups(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/user/groups?accountId=uuid-sample", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -554,13 +553,13 @@ func Test_internalUserImpl_Groups(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/groups?accountId=uuid-sample", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -604,7 +603,7 @@ func Test_internalUserImpl_Groups(t *testing.T) { func Test_internalUserImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -629,13 +628,13 @@ func Test_internalUserImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/users/search?maxResults=0&startAt=0", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -657,13 +656,13 @@ func Test_internalUserImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/users/search?maxResults=0&startAt=0", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -685,13 +684,13 @@ func Test_internalUserImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/users/search?maxResults=0&startAt=0", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -741,7 +740,7 @@ func Test_internalUserImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -767,17 +766,13 @@ func Test_internalUserImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/user", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -800,17 +795,13 @@ func Test_internalUserImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/user", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -833,17 +824,13 @@ func Test_internalUserImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/user", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -887,7 +874,7 @@ func Test_internalUserImpl_Create(t *testing.T) { func Test_NewUserService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From a17554175f1622c7c29fb1b4ed8d3dd4a778302a Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 20:31:39 -0600 Subject: [PATCH 59/70] :art: Migrated the client interface from Client to Connector on the Jira User Search service. --- jira/internal/user_search_impl.go | 10 +++--- jira/internal/user_search_impl_test.go | 46 +++++++++++++------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/jira/internal/user_search_impl.go b/jira/internal/user_search_impl.go index b8a671fb..bf929b28 100644 --- a/jira/internal/user_search_impl.go +++ b/jira/internal/user_search_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewUserSearchService(client service.Client, version string) (*UserSearchService, error) { +func NewUserSearchService(client service.Connector, version string) (*UserSearchService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -68,7 +68,7 @@ func (u *UserSearchService) Check(ctx context.Context, permission string, option } type internalUserSearchImpl struct { - c service.Client + c service.Connector version string } @@ -104,7 +104,7 @@ func (i *internalUserSearchImpl) Check(ctx context.Context, permission string, o endpoint := fmt.Sprintf("rest/api/%v/user/permission/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -138,7 +138,7 @@ func (i *internalUserSearchImpl) Projects(ctx context.Context, accountId string, endpoint := fmt.Sprintf("rest/api/%v/user/assignable/multiProjectSearch?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -168,7 +168,7 @@ func (i *internalUserSearchImpl) Do(ctx context.Context, accountId, query string endpoint := fmt.Sprintf("rest/api/%v/user/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/user_search_impl_test.go b/jira/internal/user_search_impl_test.go index 597212cd..81637e22 100644 --- a/jira/internal/user_search_impl_test.go +++ b/jira/internal/user_search_impl_test.go @@ -15,7 +15,7 @@ import ( func Test_internalUserSearchImpl_Projects(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -46,13 +46,13 @@ func Test_internalUserSearchImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/assignable/multiProjectSearch?accountId=uuid-sample&maxResults=50&projectKeys=DUMMY%2CKP&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -78,13 +78,13 @@ func Test_internalUserSearchImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/user/assignable/multiProjectSearch?accountId=uuid-sample&maxResults=50&projectKeys=DUMMY%2CKP&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -121,13 +121,13 @@ func Test_internalUserSearchImpl_Projects(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/assignable/multiProjectSearch?accountId=uuid-sample&maxResults=50&projectKeys=DUMMY%2CKP&startAt=100", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -172,7 +172,7 @@ func Test_internalUserSearchImpl_Projects(t *testing.T) { func Test_internalUserSearchImpl_Do(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -203,13 +203,13 @@ func Test_internalUserSearchImpl_Do(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/search?accountId=uuid-sample&maxResults=50&query=charles.smith%40example.com&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -235,13 +235,13 @@ func Test_internalUserSearchImpl_Do(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/user/search?accountId=uuid-sample&maxResults=50&query=charles.smith%40example.com&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -267,13 +267,13 @@ func Test_internalUserSearchImpl_Do(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/search?accountId=uuid-sample&maxResults=50&query=charles.smith%40example.com&startAt=100", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -318,7 +318,7 @@ func Test_internalUserSearchImpl_Do(t *testing.T) { func Test_internalUserSearchImpl_Check(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -354,13 +354,13 @@ func Test_internalUserSearchImpl_Check(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/permission/search?accountId=uuid-sample&issueKey=DFUMM-1&maxResults=50&permission=CREATE_ISSUES&projectKey=DUMMY&query=project+A&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -381,7 +381,7 @@ func Test_internalUserSearchImpl_Check(t *testing.T) { ctx: context.TODO(), }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) fields.c = client }, wantErr: true, @@ -405,13 +405,13 @@ func Test_internalUserSearchImpl_Check(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/user/permission/search?accountId=uuid-sample&issueKey=DFUMM-1&maxResults=50&permission=CREATE_ISSUES&projectKey=DUMMY&query=project+A&startAt=100", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -442,13 +442,13 @@ func Test_internalUserSearchImpl_Check(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/user/permission/search?accountId=uuid-sample&issueKey=DFUMM-1&maxResults=50&permission=CREATE_ISSUES&projectKey=DUMMY&query=project+A&startAt=100", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -493,7 +493,7 @@ func Test_internalUserSearchImpl_Check(t *testing.T) { func Test_NewUserSearchService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 3e5bce7aa7afd43337cf5ac16a62bcab498a1a57 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 20:42:46 -0600 Subject: [PATCH 60/70] :art: Migrated the client interface from Client to Connector on the Jira Workflow service. --- jira/internal/workflow_impl.go | 15 +++----- jira/internal/workflow_impl_test.go | 57 +++++++++++------------------ 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/jira/internal/workflow_impl.go b/jira/internal/workflow_impl.go index 3efe1a8c..e163c7f2 100644 --- a/jira/internal/workflow_impl.go +++ b/jira/internal/workflow_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewWorkflowService(client service.Client, version string, scheme *WorkflowSchemeService, status *WorkflowStatusService) (*WorkflowService, error) { +func NewWorkflowService(client service.Connector, version string, scheme *WorkflowSchemeService, status *WorkflowStatusService) (*WorkflowService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -74,20 +74,15 @@ func (w *WorkflowService) Delete(ctx context.Context, workflowId string) (*model } type internalWorkflowImpl struct { - c service.Client + c service.Connector version string } func (i *internalWorkflowImpl) Create(ctx context.Context, payload *model.WorkflowPayloadScheme) (*model.WorkflowCreatedResponseScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/workflow", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -129,7 +124,7 @@ func (i *internalWorkflowImpl) Gets(ctx context.Context, options *model.Workflow endpoint := fmt.Sprintf("rest/api/%v/workflow/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -151,7 +146,7 @@ func (i *internalWorkflowImpl) Delete(ctx context.Context, workflowId string) (* endpoint := fmt.Sprintf("rest/api/%v/workflow/%v", i.version, workflowId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } diff --git a/jira/internal/workflow_impl_test.go b/jira/internal/workflow_impl_test.go index 83311aff..1e5ee537 100644 --- a/jira/internal/workflow_impl_test.go +++ b/jira/internal/workflow_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalWorkflowImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -50,13 +49,13 @@ func Test_internalWorkflowImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflow/search?expand=transitions&isActive=true&maxResults=25&orderBy=name&queryString=workflow&startAt=50&workflowName=workflow-name", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -87,13 +86,13 @@ func Test_internalWorkflowImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/workflow/search?expand=transitions&isActive=true&maxResults=25&orderBy=name&queryString=workflow&startAt=50&workflowName=workflow-name", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -124,13 +123,13 @@ func Test_internalWorkflowImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflow/search?expand=transitions&isActive=true&maxResults=25&orderBy=name&queryString=workflow&startAt=50&workflowName=workflow-name", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -175,7 +174,7 @@ func Test_internalWorkflowImpl_Gets(t *testing.T) { func Test_internalWorkflowImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -201,13 +200,13 @@ func Test_internalWorkflowImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/workflow/2838382882", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -230,13 +229,13 @@ func Test_internalWorkflowImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/workflow/2838382882", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -269,13 +268,13 @@ func Test_internalWorkflowImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/workflow/2838382882", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -340,7 +339,7 @@ func Test_internalWorkflowImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -366,17 +365,13 @@ func Test_internalWorkflowImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/workflow", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -399,17 +394,13 @@ func Test_internalWorkflowImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/workflow", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -432,17 +423,13 @@ func Test_internalWorkflowImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/workflow", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -486,7 +473,7 @@ func Test_internalWorkflowImpl_Create(t *testing.T) { func Test_NewWorkflowService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 8040c97accbfb79f9b6b814dee568eab1d8b2e07 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 21:07:39 -0600 Subject: [PATCH 61/70] :art: Migrated the client interface from Client to Connector on the Jira Workflow Scheme service. --- jira/internal/workflow_scheme_impl.go | 41 ++---- jira/internal/workflow_scheme_impl_test.go | 142 ++++++++------------- 2 files changed, 60 insertions(+), 123 deletions(-) diff --git a/jira/internal/workflow_scheme_impl.go b/jira/internal/workflow_scheme_impl.go index 75d782b4..941d0e67 100644 --- a/jira/internal/workflow_scheme_impl.go +++ b/jira/internal/workflow_scheme_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewWorkflowSchemeService(client service.Client, version string, issueType *WorkflowSchemeIssueTypeService) *WorkflowSchemeService { +func NewWorkflowSchemeService(client service.Connector, version string, issueType *WorkflowSchemeIssueTypeService) *WorkflowSchemeService { return &WorkflowSchemeService{ internalClient: &internalWorkflowSchemeImpl{c: client, version: version}, @@ -107,7 +107,7 @@ func (w *WorkflowSchemeService) Assign(ctx context.Context, schemeId, projectId } type internalWorkflowSchemeImpl struct { - c service.Client + c service.Connector version string } @@ -119,7 +119,7 @@ func (i *internalWorkflowSchemeImpl) Gets(ctx context.Context, startAt, maxResul endpoint := fmt.Sprintf("rest/api/%v/workflowscheme?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -135,14 +135,9 @@ func (i *internalWorkflowSchemeImpl) Gets(ctx context.Context, startAt, maxResul func (i *internalWorkflowSchemeImpl) Create(ctx context.Context, payload *model.WorkflowSchemePayloadScheme) (*model.WorkflowSchemeScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/workflowscheme", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -173,7 +168,7 @@ func (i *internalWorkflowSchemeImpl) Get(ctx context.Context, schemeId int, retu endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -193,14 +188,9 @@ func (i *internalWorkflowSchemeImpl) Update(ctx context.Context, schemeId int, p return nil, nil, model.ErrNoWorkflowSchemeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/workflowscheme/%v", i.version, schemeId) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -222,7 +212,7 @@ func (i *internalWorkflowSchemeImpl) Delete(ctx context.Context, schemeId int) ( endpoint := fmt.Sprintf("rest/api/%v/workflowscheme/%v", i.version, schemeId) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -243,7 +233,7 @@ func (i *internalWorkflowSchemeImpl) Associations(ctx context.Context, projectId endpoint := fmt.Sprintf("rest/api/%v/workflowscheme/project?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -267,22 +257,9 @@ func (i *internalWorkflowSchemeImpl) Assign(ctx context.Context, schemeId, proje return nil, model.ErrNoProjectIDOrKeyError } - payload := struct { - WorkflowSchemeID string `json:"workflowSchemeId"` - ProjectID string `json:"projectId"` - }{ - WorkflowSchemeID: schemeId, - ProjectID: projectId, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/workflowscheme/project", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", map[string]interface{}{"workflowSchemeId": schemeId, "projectId": projectId}) if err != nil { return nil, err } diff --git a/jira/internal/workflow_scheme_impl_test.go b/jira/internal/workflow_scheme_impl_test.go index f09463fc..a74037af 100644 --- a/jira/internal/workflow_scheme_impl_test.go +++ b/jira/internal/workflow_scheme_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -15,7 +14,7 @@ import ( func Test_internalWorkflowSchemeImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -42,13 +41,13 @@ func Test_internalWorkflowSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme?maxResults=25&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -72,13 +71,13 @@ func Test_internalWorkflowSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/workflowscheme?maxResults=25&startAt=50", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -102,13 +101,13 @@ func Test_internalWorkflowSchemeImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme?maxResults=25&startAt=50", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -151,7 +150,7 @@ func Test_internalWorkflowSchemeImpl_Gets(t *testing.T) { func Test_internalWorkflowSchemeImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -179,13 +178,13 @@ func Test_internalWorkflowSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme/10002?returnDraftIfExists=true", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -209,13 +208,13 @@ func Test_internalWorkflowSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/workflowscheme/10002?returnDraftIfExists=true", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -249,13 +248,13 @@ func Test_internalWorkflowSchemeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme/10002?returnDraftIfExists=true", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -298,7 +297,7 @@ func Test_internalWorkflowSchemeImpl_Get(t *testing.T) { func Test_internalWorkflowSchemeImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -324,13 +323,13 @@ func Test_internalWorkflowSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/workflowscheme/10002", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -353,13 +352,13 @@ func Test_internalWorkflowSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/workflowscheme/10002", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -392,13 +391,13 @@ func Test_internalWorkflowSchemeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/workflowscheme/10002", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -440,7 +439,7 @@ func Test_internalWorkflowSchemeImpl_Delete(t *testing.T) { func Test_internalWorkflowSchemeImpl_Associations(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -466,13 +465,13 @@ func Test_internalWorkflowSchemeImpl_Associations(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme/project?projectId=1002&projectId=1003", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -495,13 +494,13 @@ func Test_internalWorkflowSchemeImpl_Associations(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/workflowscheme/project?projectId=1002&projectId=1003", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -534,13 +533,13 @@ func Test_internalWorkflowSchemeImpl_Associations(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme/project?projectId=1002&projectId=1003", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -582,13 +581,10 @@ func Test_internalWorkflowSchemeImpl_Associations(t *testing.T) { func Test_internalWorkflowSchemeImpl_Assign(t *testing.T) { - payloadMocked := &struct { - WorkflowSchemeID string "json:\"workflowSchemeId\"" - ProjectID string "json:\"projectId\"" - }{WorkflowSchemeID: "1004561", ProjectID: "4984838"} + payloadMocked := map[string]interface{}{"projectId": "4984838", "workflowSchemeId": "1004561"} type fields struct { - c service.Client + c service.Connector version string } @@ -615,17 +611,13 @@ func Test_internalWorkflowSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/workflowscheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -649,17 +641,13 @@ func Test_internalWorkflowSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/workflowscheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -704,17 +692,13 @@ func Test_internalWorkflowSchemeImpl_Assign(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/workflowscheme/project", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -765,7 +749,7 @@ func Test_internalWorkflowSchemeImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -793,17 +777,13 @@ func Test_internalWorkflowSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/workflowscheme/10002", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -827,17 +807,13 @@ func Test_internalWorkflowSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/workflowscheme/10002", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -871,17 +847,13 @@ func Test_internalWorkflowSchemeImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/workflowscheme/10002", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -934,7 +906,7 @@ func Test_internalWorkflowSchemeImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -960,17 +932,13 @@ func Test_internalWorkflowSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/workflowscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -993,17 +961,13 @@ func Test_internalWorkflowSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/workflowscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -1026,17 +990,13 @@ func Test_internalWorkflowSchemeImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/workflowscheme", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1079,7 +1039,7 @@ func Test_internalWorkflowSchemeImpl_Create(t *testing.T) { func Test_NewWorkflowSchemeService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From ba80719c9727cf7c2ced553894573c4e9a749584 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 21:11:46 -0600 Subject: [PATCH 62/70] :art: Migrated the client interface from Client to Connector on the Jira Workflow Scheme IssueType service. --- .../workflow_scheme_issue_type_impl.go | 17 ++--- .../workflow_scheme_issue_type_impl_test.go | 69 ++++++++----------- 2 files changed, 34 insertions(+), 52 deletions(-) diff --git a/jira/internal/workflow_scheme_issue_type_impl.go b/jira/internal/workflow_scheme_issue_type_impl.go index 5f767544..a08d6871 100644 --- a/jira/internal/workflow_scheme_issue_type_impl.go +++ b/jira/internal/workflow_scheme_issue_type_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewWorkflowSchemeIssueTypeService(client service.Client, version string) *WorkflowSchemeIssueTypeService { +func NewWorkflowSchemeIssueTypeService(client service.Connector, version string) *WorkflowSchemeIssueTypeService { return &WorkflowSchemeIssueTypeService{ internalClient: &internalWorkflowSchemeIssueTypeImpl{c: client, version: version}, @@ -75,7 +75,7 @@ func (w *WorkflowSchemeIssueTypeService) Mapping(ctx context.Context, schemeID i } type internalWorkflowSchemeIssueTypeImpl struct { - c service.Client + c service.Connector version string } @@ -99,7 +99,7 @@ func (i *internalWorkflowSchemeIssueTypeImpl) Get(ctx context.Context, schemeID endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -123,14 +123,9 @@ func (i *internalWorkflowSchemeIssueTypeImpl) Set(ctx context.Context, schemeID return nil, nil, model.ErrNoIssueTypeIDError } - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/workflowscheme/%v/issuetype/%v", i.version, schemeID, issueTypeID) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -164,7 +159,7 @@ func (i *internalWorkflowSchemeIssueTypeImpl) Delete(ctx context.Context, scheme endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -201,7 +196,7 @@ func (i *internalWorkflowSchemeIssueTypeImpl) Mapping(ctx context.Context, schem endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/workflow_scheme_issue_type_impl_test.go b/jira/internal/workflow_scheme_issue_type_impl_test.go index b337271f..5b507895 100644 --- a/jira/internal/workflow_scheme_issue_type_impl_test.go +++ b/jira/internal/workflow_scheme_issue_type_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalWorkflowSchemeIssueTypeImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -46,13 +45,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme/10002/issuetype/4?returnDraftIfExists=true", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -98,13 +97,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/workflowscheme/10002/issuetype/4?returnDraftIfExists=true", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -129,13 +128,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme/10002/issuetype/4?returnDraftIfExists=true", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -179,7 +178,7 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Get(t *testing.T) { func Test_internalWorkflowSchemeIssueTypeImpl_Mapping(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -209,13 +208,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme/10002/workflow?returnDraftIfExists=true&workflowName=jira+workflow+", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -250,13 +249,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/workflowscheme/10002/workflow?returnDraftIfExists=true&workflowName=jira+workflow+", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -281,13 +280,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Mapping(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/workflowscheme/10002/workflow?returnDraftIfExists=true&workflowName=jira+workflow+", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -331,7 +330,7 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Mapping(t *testing.T) { func Test_internalWorkflowSchemeIssueTypeImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -361,13 +360,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/workflowscheme/10002/issuetype/4?updateDraftIfNeeded=true", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -413,13 +412,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/workflowscheme/10002/issuetype/4?updateDraftIfNeeded=true", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -444,13 +443,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/workflowscheme/10002/issuetype/4?updateDraftIfNeeded=true", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -500,7 +499,7 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Set(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -530,17 +529,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/workflowscheme/10002/issuetype/4", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -586,17 +581,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/workflowscheme/10002/issuetype/4", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -621,17 +612,13 @@ func Test_internalWorkflowSchemeIssueTypeImpl_Set(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/workflowscheme/10002/issuetype/4", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client From 978c54257ad59bc025c1b3e3de4236383269ec63 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 21:21:48 -0600 Subject: [PATCH 63/70] :art: Migrated the client interface from Client to Connector on the Jira MySelf service. --- jira/internal/myself_impl.go | 6 +++--- jira/internal/myself_impl_test.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/jira/internal/myself_impl.go b/jira/internal/myself_impl.go index 05de81e5..b9a3564b 100644 --- a/jira/internal/myself_impl.go +++ b/jira/internal/myself_impl.go @@ -11,7 +11,7 @@ import ( "strings" ) -func NewMySelfService(client service.Client, version string) (*MySelfService, error) { +func NewMySelfService(client service.Connector, version string) (*MySelfService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -36,7 +36,7 @@ func (m *MySelfService) Details(ctx context.Context, expand []string) (*model.Us } type internalMySelfImpl struct { - c service.Client + c service.Connector version string } @@ -53,7 +53,7 @@ func (i *internalMySelfImpl) Details(ctx context.Context, expand []string) (*mod endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/myself_impl_test.go b/jira/internal/myself_impl_test.go index b73ce766..d5ef0307 100644 --- a/jira/internal/myself_impl_test.go +++ b/jira/internal/myself_impl_test.go @@ -14,7 +14,7 @@ import ( func Test_internalMySelfImpl_Details(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -40,13 +40,13 @@ func Test_internalMySelfImpl_Details(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/myself?expand=groups%2CapplicationRoles", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -69,13 +69,13 @@ func Test_internalMySelfImpl_Details(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/myself?expand=groups%2CapplicationRoles", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -98,13 +98,13 @@ func Test_internalMySelfImpl_Details(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/myself?expand=groups%2CapplicationRoles", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -148,7 +148,7 @@ func Test_internalMySelfImpl_Details(t *testing.T) { func Test_NewMySelfService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From ed3d96cb88a4428b59a59cedd5e61ff25545da36 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 21:27:48 -0600 Subject: [PATCH 64/70] :art: Migrated the client interface from Client to Connector on the Jira Workflow Status service. --- jira/internal/workflow_status_impl.go | 28 ++--- jira/internal/workflow_status_impl_test.go | 136 ++++++++------------- 2 files changed, 61 insertions(+), 103 deletions(-) diff --git a/jira/internal/workflow_status_impl.go b/jira/internal/workflow_status_impl.go index 92a99406..21ad596d 100644 --- a/jira/internal/workflow_status_impl.go +++ b/jira/internal/workflow_status_impl.go @@ -12,7 +12,7 @@ import ( "strings" ) -func NewWorkflowStatusService(client service.Client, version string) (*WorkflowStatusService, error) { +func NewWorkflowStatusService(client service.Connector, version string) (*WorkflowStatusService, error) { if version == "" { return nil, model.ErrNoVersionProvided @@ -99,7 +99,7 @@ func (w *WorkflowStatusService) Get(ctx context.Context, idOrName string) (*mode } type internalWorkflowStatusImpl struct { - c service.Client + c service.Connector version string } @@ -111,7 +111,7 @@ func (i *internalWorkflowStatusImpl) Get(ctx context.Context, idOrName string) ( endpoint := fmt.Sprintf("/rest/api/%v/status/%v", i.version, idOrName) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -129,7 +129,7 @@ func (i *internalWorkflowStatusImpl) Bulk(ctx context.Context) ([]*model.StatusD endpoint := fmt.Sprintf("/rest/api/%v/status", i.version) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } @@ -161,7 +161,7 @@ func (i *internalWorkflowStatusImpl) Gets(ctx context.Context, ids, expand []str endpoint.WriteString(fmt.Sprintf("?%v", params.Encode())) } - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), "", nil) if err != nil { return nil, nil, err } @@ -177,14 +177,9 @@ func (i *internalWorkflowStatusImpl) Gets(ctx context.Context, ids, expand []str func (i *internalWorkflowStatusImpl) Update(ctx context.Context, payload *model.WorkflowStatusPayloadScheme) (*model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, err - } - endpoint := fmt.Sprintf("rest/api/%v/statuses", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPut, endpoint, "", payload) if err != nil { return nil, err } @@ -194,11 +189,6 @@ func (i *internalWorkflowStatusImpl) Update(ctx context.Context, payload *model. func (i *internalWorkflowStatusImpl) Create(ctx context.Context, payload *model.WorkflowStatusPayloadScheme) ([]*model.WorkflowStatusDetailScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - if len(payload.Statuses) == 0 { return nil, nil, model.ErrNoWorkflowStatusesError } @@ -209,7 +199,7 @@ func (i *internalWorkflowStatusImpl) Create(ctx context.Context, payload *model. endpoint := fmt.Sprintf("rest/api/%v/statuses", i.version) - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } @@ -236,7 +226,7 @@ func (i *internalWorkflowStatusImpl) Delete(ctx context.Context, ids []string) ( endpoint := fmt.Sprintf("rest/api/%v/statuses?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil) if err != nil { return nil, err } @@ -271,7 +261,7 @@ func (i *internalWorkflowStatusImpl) Search(ctx context.Context, options *model. endpoint := fmt.Sprintf("rest/api/%v/statuses/search?%v", i.version, params.Encode()) - request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil) + request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil) if err != nil { return nil, nil, err } diff --git a/jira/internal/workflow_status_impl_test.go b/jira/internal/workflow_status_impl_test.go index d970d271..93ad085e 100644 --- a/jira/internal/workflow_status_impl_test.go +++ b/jira/internal/workflow_status_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -16,7 +15,7 @@ import ( func Test_internalWorkflowStatusImpl_Gets(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -43,13 +42,13 @@ func Test_internalWorkflowStatusImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/statuses?expand=usages&id=10000&id=10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -73,13 +72,13 @@ func Test_internalWorkflowStatusImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/statuses?expand=usages&id=10000&id=10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -103,13 +102,13 @@ func Test_internalWorkflowStatusImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/statuses?expand=usages&id=10000&id=10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -170,7 +169,7 @@ func Test_internalWorkflowStatusImpl_Update(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -196,17 +195,13 @@ func Test_internalWorkflowStatusImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/statuses", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -229,17 +224,13 @@ func Test_internalWorkflowStatusImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/2/statuses", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -262,17 +253,13 @@ func Test_internalWorkflowStatusImpl_Update(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPut, "rest/api/3/statuses", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -356,7 +343,7 @@ func Test_internalWorkflowStatusImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector version string } @@ -382,17 +369,13 @@ func Test_internalWorkflowStatusImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/statuses", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -415,16 +398,13 @@ func Test_internalWorkflowStatusImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/2/statuses", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -446,11 +426,7 @@ func Test_internalWorkflowStatusImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMockedWithOutStatuses). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) fields.c = client }, @@ -467,11 +443,7 @@ func Test_internalWorkflowStatusImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMockedWithOutScope). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) fields.c = client }, @@ -488,17 +460,13 @@ func Test_internalWorkflowStatusImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/api/3/statuses", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -542,7 +510,7 @@ func Test_internalWorkflowStatusImpl_Create(t *testing.T) { func Test_internalWorkflowStatusImpl_Delete(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -568,13 +536,13 @@ func Test_internalWorkflowStatusImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/statuses?id=10000&id=10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -597,13 +565,13 @@ func Test_internalWorkflowStatusImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/2/statuses?id=10000&id=10001", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -636,13 +604,13 @@ func Test_internalWorkflowStatusImpl_Delete(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodDelete, "rest/api/3/statuses?id=10000&id=10001", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -685,7 +653,7 @@ func Test_internalWorkflowStatusImpl_Delete(t *testing.T) { func Test_internalWorkflowStatusImpl_Search(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -719,13 +687,13 @@ func Test_internalWorkflowStatusImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/statuses/search?expand=usages&maxResults=50&projectId=8373772&searchString=UAT&startAt=0&statusCategory=IN_PROGRESS", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -755,13 +723,13 @@ func Test_internalWorkflowStatusImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/2/statuses/search?expand=usages&maxResults=50&projectId=8373772&searchString=UAT&startAt=0&statusCategory=IN_PROGRESS", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -791,13 +759,13 @@ func Test_internalWorkflowStatusImpl_Search(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "rest/api/3/statuses/search?expand=usages&maxResults=50&projectId=8373772&searchString=UAT&startAt=0&statusCategory=IN_PROGRESS", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -842,7 +810,7 @@ func Test_internalWorkflowStatusImpl_Search(t *testing.T) { func Test_internalWorkflowStatusImpl_Bulk(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -866,13 +834,13 @@ func Test_internalWorkflowStatusImpl_Bulk(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "/rest/api/3/status", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -894,13 +862,13 @@ func Test_internalWorkflowStatusImpl_Bulk(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "/rest/api/2/status", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -922,13 +890,13 @@ func Test_internalWorkflowStatusImpl_Bulk(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "/rest/api/3/status", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -972,7 +940,7 @@ func Test_internalWorkflowStatusImpl_Bulk(t *testing.T) { func Test_internalWorkflowStatusImpl_Get(t *testing.T) { type fields struct { - c service.Client + c service.Connector version string } @@ -998,13 +966,13 @@ func Test_internalWorkflowStatusImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "/rest/api/3/status/TODO", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1027,13 +995,13 @@ func Test_internalWorkflowStatusImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "/rest/api/2/status/TODO", - nil). + "", nil). Return(&http.Request{}, nil) client.On("Call", @@ -1056,13 +1024,13 @@ func Test_internalWorkflowStatusImpl_Get(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodGet, "/rest/api/3/status/TODO", - nil). + "", nil). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -1106,7 +1074,7 @@ func Test_internalWorkflowStatusImpl_Get(t *testing.T) { func Test_NewWorkflowStatusService(t *testing.T) { type args struct { - client service.Client + client service.Connector version string } From 4a4620fd9d4a0a918f608491df6466f6d863ff04 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 21:35:11 -0600 Subject: [PATCH 65/70] :art: Migrated the client interface from Client to Connector on the Jira Team service. --- jira/internal/team_impl.go | 24 ++-------- jira/internal/team_impl_test.go | 77 +++++++-------------------------- 2 files changed, 19 insertions(+), 82 deletions(-) diff --git a/jira/internal/team_impl.go b/jira/internal/team_impl.go index 86ce03b4..90f996cf 100644 --- a/jira/internal/team_impl.go +++ b/jira/internal/team_impl.go @@ -8,7 +8,7 @@ import ( "net/http" ) -func NewTeamService(client service.Client) *TeamService { +func NewTeamService(client service.Connector) *TeamService { return &TeamService{ internalClient: &internalTeamServiceImpl{c: client}, @@ -46,25 +46,14 @@ func (t *TeamService) Create(ctx context.Context, payload *model.JiraTeamCreateP } type internalTeamServiceImpl struct { - c service.Client + c service.Connector } func (i *internalTeamServiceImpl) Gets(ctx context.Context, maxResults int) (*model.JiraTeamPageScheme, *model.ResponseScheme, error) { - payload := struct { - MaxResults int `json:"maxResults"` - }{ - MaxResults: maxResults, - } - - reader, err := i.c.TransformStructToReader(&payload) - if err != nil { - return nil, nil, err - } - endpoint := "rest/teams/1.0/teams/find" - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", map[string]interface{}{"maxResults": maxResults}) if err != nil { return nil, nil, err } @@ -80,14 +69,9 @@ func (i *internalTeamServiceImpl) Gets(ctx context.Context, maxResults int) (*mo func (i *internalTeamServiceImpl) Create(ctx context.Context, payload *model.JiraTeamCreatePayloadScheme) (*model.JiraTeamCreateResponseScheme, *model.ResponseScheme, error) { - reader, err := i.c.TransformStructToReader(payload) - if err != nil { - return nil, nil, err - } - endpoint := "rest/teams/1.0/teams/create" - request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, reader) + request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload) if err != nil { return nil, nil, err } diff --git a/jira/internal/team_impl_test.go b/jira/internal/team_impl_test.go index 8eb8cc6b..557513b2 100644 --- a/jira/internal/team_impl_test.go +++ b/jira/internal/team_impl_test.go @@ -1,7 +1,6 @@ package internal import ( - "bytes" "context" "errors" model "github.com/ctreminiom/go-atlassian/pkg/infra/models" @@ -14,12 +13,10 @@ import ( func Test_internalTeamServiceImpl_Gets(t *testing.T) { - payloadMocked := &struct { - MaxResults int "json:\"maxResults\"" - }{MaxResults: 1000} + payloadMocked := map[string]interface{}{"maxResults": 1000} type fields struct { - c service.Client + c service.Connector } type args struct { @@ -43,17 +40,13 @@ func Test_internalTeamServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/teams/1.0/teams/find", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -75,17 +68,13 @@ func Test_internalTeamServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/teams/1.0/teams/find", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -102,17 +91,13 @@ func Test_internalTeamServiceImpl_Gets(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/teams/1.0/teams/find", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -169,7 +154,7 @@ func Test_internalTeamServiceImpl_Create(t *testing.T) { } type fields struct { - c service.Client + c service.Connector } type args struct { @@ -193,17 +178,13 @@ func Test_internalTeamServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/teams/1.0/teams/create", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", @@ -217,26 +198,6 @@ func Test_internalTeamServiceImpl_Create(t *testing.T) { Err: nil, }, - { - name: "when the payload is not provided", - args: args{ - ctx: context.TODO(), - payload: nil, - }, - on: func(fields *fields) { - - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - (*model.JiraTeamCreatePayloadScheme)(nil)). - Return(nil, model.ErrNilPayloadError) - - fields.c = client - }, - wantErr: true, - Err: model.ErrNilPayloadError, - }, - { name: "when the http request cannot be created", args: args{ @@ -245,17 +206,13 @@ func Test_internalTeamServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/teams/1.0/teams/create", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, errors.New("error, unable to create the http request")) fields.c = client @@ -272,17 +229,13 @@ func Test_internalTeamServiceImpl_Create(t *testing.T) { }, on: func(fields *fields) { - client := mocks.NewClient(t) - - client.On("TransformStructToReader", - payloadMocked). - Return(bytes.NewReader([]byte{}), nil) + client := mocks.NewConnector(t) client.On("NewRequest", context.Background(), http.MethodPost, "rest/teams/1.0/teams/create", - bytes.NewReader([]byte{})). + "", payloadMocked). Return(&http.Request{}, nil) client.On("Call", From 81cda3753efb9c089c33a181d20debb62d369a31 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 22:02:13 -0600 Subject: [PATCH 66/70] :art: Migrated the client interface from Client to Connector on the Jira v2/v3 modules. --- jira/internal/authentication_impl.go | 8 +- jira/internal/authentication_impl_test.go | 32 +- jira/v2/api_client_impl.go | 240 +++++---- jira/v2/api_client_impl_test.go | 574 +++++++++------------- jira/v3/api_client_impl.go | 239 +++++---- jira/v3/api_client_impl_test.go | 574 +++++++++------------- 6 files changed, 707 insertions(+), 960 deletions(-) diff --git a/jira/internal/authentication_impl.go b/jira/internal/authentication_impl.go index f8555352..2bbe9ab8 100644 --- a/jira/internal/authentication_impl.go +++ b/jira/internal/authentication_impl.go @@ -5,12 +5,12 @@ import ( "github.com/ctreminiom/go-atlassian/service/common" ) -func NewAuthenticationService(client service.Client) common.Authentication { +func NewAuthenticationService(client service.Connector) common.Authentication { return &AuthenticationService{c: client} } type AuthenticationService struct { - c service.Client + c service.Connector basicAuthProvided bool mail, token string @@ -26,9 +26,7 @@ func (a *AuthenticationService) SetBearerToken(token string) { func (a *AuthenticationService) GetBearerToken() string { return a.token } - -func (a *AuthenticationService) SetExperimentalFlag() { -} +func (a *AuthenticationService) SetExperimentalFlag() {} func (a *AuthenticationService) HasSetExperimentalFlag() bool { return false diff --git a/jira/internal/authentication_impl_test.go b/jira/internal/authentication_impl_test.go index 33b541ec..052f3466 100644 --- a/jira/internal/authentication_impl_test.go +++ b/jira/internal/authentication_impl_test.go @@ -12,7 +12,7 @@ import ( func TestAuthenticationService_GetBasicAuth(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -28,7 +28,7 @@ func TestAuthenticationService_GetBasicAuth(t *testing.T) { { name: "when the basic auth is already set", fields: fields{ - c: mocks.NewClient(t), + c: mocks.NewConnector(t), mail: "mail", token: "token", }, @@ -62,7 +62,7 @@ func TestAuthenticationService_GetBasicAuth(t *testing.T) { func TestAuthenticationService_GetUserAgent(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -77,7 +77,7 @@ func TestAuthenticationService_GetUserAgent(t *testing.T) { { name: "when the user agent is already set", fields: fields{ - c: mocks.NewClient(t), + c: mocks.NewConnector(t), agent: "firefox-09", }, want: "firefox-09", @@ -102,7 +102,7 @@ func TestAuthenticationService_GetUserAgent(t *testing.T) { func TestAuthenticationService_HasBasicAuth(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -117,7 +117,7 @@ func TestAuthenticationService_HasBasicAuth(t *testing.T) { { name: "when the params are correct", fields: fields{ - c: mocks.NewClient(t), + c: mocks.NewConnector(t), basicAuthProvided: true, }, want: true, @@ -143,7 +143,7 @@ func TestAuthenticationService_HasBasicAuth(t *testing.T) { func TestAuthenticationService_HasUserAgent(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -184,10 +184,10 @@ func TestAuthenticationService_HasUserAgent(t *testing.T) { func TestNewAuthenticationService(t *testing.T) { - clientMocked := mocks.NewClient(t) + clientMocked := mocks.NewConnector(t) type args struct { - client service.Client + client service.Connector } testCases := []struct { name string @@ -214,7 +214,7 @@ func TestNewAuthenticationService(t *testing.T) { func TestAuthenticationService_SetUserAgent(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -254,7 +254,7 @@ func TestAuthenticationService_SetUserAgent(t *testing.T) { func TestAuthenticationService_SetBasicAuth(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -297,7 +297,7 @@ func TestAuthenticationService_SetBasicAuth(t *testing.T) { func TestAuthenticationService_HasSetExperimentalFlag(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -335,7 +335,7 @@ func TestAuthenticationService_HasSetExperimentalFlag(t *testing.T) { func TestAuthenticationService_GetBearerToken(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -350,7 +350,7 @@ func TestAuthenticationService_GetBearerToken(t *testing.T) { { name: "when the parameters are correct", fields: fields{ - c: mocks.NewClient(t), + c: mocks.NewConnector(t), token: "token-sample", }, want: "token-sample", @@ -373,7 +373,7 @@ func TestAuthenticationService_GetBearerToken(t *testing.T) { func TestAuthenticationService_SetBearerToken(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string @@ -415,7 +415,7 @@ func TestAuthenticationService_SetBearerToken(t *testing.T) { func TestAuthenticationService_SetExperimentalFlag(t *testing.T) { type fields struct { - c service.Client + c service.Connector basicAuthProvided bool mail string token string diff --git a/jira/v2/api_client_impl.go b/jira/v2/api_client_impl.go index 68c4df74..e69386c4 100644 --- a/jira/v2/api_client_impl.go +++ b/jira/v2/api_client_impl.go @@ -10,171 +10,178 @@ import ( "io" "net/http" "net/url" - "reflect" "strings" ) +const ApiVersion = "2" + func New(httpClient common.HttpClient, site string) (*Client, error) { if httpClient == nil { httpClient = http.DefaultClient } + if site == "" { + return nil, models.ErrNoSiteError + } + if !strings.HasSuffix(site, "/") { site += "/" } - siteAsURL, err := url.Parse(site) + u, err := url.Parse(site) if err != nil { return nil, err } client := &Client{ HTTP: httpClient, - Site: siteAsURL, + Site: u, } - auditRecordService, err := internal.NewAuditRecordService(client, "2") + client.Auth = internal.NewAuthenticationService(client) + + auditRecordService, err := internal.NewAuditRecordService(client, ApiVersion) if err != nil { return nil, err } - applicationRoleService, err := internal.NewApplicationRoleService(client, "2") + applicationRoleService, err := internal.NewApplicationRoleService(client, ApiVersion) if err != nil { return nil, err } - dashboardService, err := internal.NewDashboardService(client, "2") + dashboardService, err := internal.NewDashboardService(client, ApiVersion) if err != nil { return nil, err } - filterShareService, err := internal.NewFilterShareService(client, "2") + filterShareService, err := internal.NewFilterShareService(client, ApiVersion) if err != nil { return nil, err } - filterService, err := internal.NewFilterService(client, "2", filterShareService) + filterService, err := internal.NewFilterService(client, ApiVersion, filterShareService) if err != nil { return nil, err } - groupService, err := internal.NewGroupService(client, "2") + groupService, err := internal.NewGroupService(client, ApiVersion) if err != nil { return nil, err } - issueAttachmentService, err := internal.NewIssueAttachmentService(client, "2") + issueAttachmentService, err := internal.NewIssueAttachmentService(client, ApiVersion) if err != nil { return nil, err } - _, commentService, err := internal.NewCommentService(client, "2") + _, commentService, err := internal.NewCommentService(client, ApiVersion) if err != nil { return nil, err } - fieldConfigurationItemService, err := internal.NewIssueFieldConfigurationItemService(client, "2") + fieldConfigurationItemService, err := internal.NewIssueFieldConfigurationItemService(client, ApiVersion) if err != nil { return nil, err } - fieldConfigurationSchemeService, err := internal.NewIssueFieldConfigurationSchemeService(client, "2") + fieldConfigurationSchemeService, err := internal.NewIssueFieldConfigurationSchemeService(client, ApiVersion) if err != nil { return nil, err } - fieldConfigService, err := internal.NewIssueFieldConfigurationService(client, "2", fieldConfigurationItemService, fieldConfigurationSchemeService) + fieldConfigService, err := internal.NewIssueFieldConfigurationService(client, ApiVersion, fieldConfigurationItemService, fieldConfigurationSchemeService) if err != nil { return nil, err } - optionService, err := internal.NewIssueFieldContextOptionService(client, "2") + optionService, err := internal.NewIssueFieldContextOptionService(client, ApiVersion) if err != nil { return nil, err } - fieldContextService, err := internal.NewIssueFieldContextService(client, "2", optionService) + fieldContextService, err := internal.NewIssueFieldContextService(client, ApiVersion, optionService) if err != nil { return nil, err } - fieldTrashService, err := internal.NewIssueFieldTrashService(client, "2") + fieldTrashService, err := internal.NewIssueFieldTrashService(client, ApiVersion) if err != nil { return nil, err } - issueFieldService, err := internal.NewIssueFieldService(client, "2", fieldConfigService, fieldContextService, fieldTrashService) + issueFieldService, err := internal.NewIssueFieldService(client, ApiVersion, fieldConfigService, fieldContextService, fieldTrashService) if err != nil { return nil, err } - label, err := internal.NewLabelService(client, "2") + label, err := internal.NewLabelService(client, ApiVersion) if err != nil { return nil, err } - linkType, err := internal.NewLinkTypeService(client, "2") + linkType, err := internal.NewLinkTypeService(client, ApiVersion) if err != nil { return nil, err } - remoteLink, err := internal.NewRemoteLinkService(client, "2") + remoteLink, err := internal.NewRemoteLinkService(client, ApiVersion) if err != nil { return nil, err } - _, link, err := internal.NewLinkService(client, "2", linkType, remoteLink) + _, link, err := internal.NewLinkService(client, ApiVersion, linkType, remoteLink) if err != nil { return nil, err } - metadata, err := internal.NewMetadataService(client, "2") + metadata, err := internal.NewMetadataService(client, ApiVersion) if err != nil { return nil, err } - priority, err := internal.NewPriorityService(client, "2") + priority, err := internal.NewPriorityService(client, ApiVersion) if err != nil { return nil, err } - resolution, err := internal.NewResolutionService(client, "2") + resolution, err := internal.NewResolutionService(client, ApiVersion) if err != nil { return nil, err } - _, search, err := internal.NewSearchService(client, "2") + _, search, err := internal.NewSearchService(client, ApiVersion) if err != nil { return nil, err } - typeScheme, err := internal.NewTypeSchemeService(client, "2") + typeScheme, err := internal.NewTypeSchemeService(client, ApiVersion) if err != nil { return nil, err } - issueTypeScreenScheme, err := internal.NewTypeScreenSchemeService(client, "2") + issueTypeScreenScheme, err := internal.NewTypeScreenSchemeService(client, ApiVersion) if err != nil { return nil, err } - type_, err := internal.NewTypeService(client, "2", typeScheme, issueTypeScreenScheme) + type_, err := internal.NewTypeService(client, ApiVersion, typeScheme, issueTypeScreenScheme) if err != nil { return nil, err } - vote, err := internal.NewVoteService(client, "2") + vote, err := internal.NewVoteService(client, ApiVersion) if err != nil { return nil, err } - watcher, err := internal.NewWatcherService(client, "2") + watcher, err := internal.NewWatcherService(client, ApiVersion) if err != nil { return nil, err } - worklog, err := internal.NewWorklogRichTextService(client, "2") + worklog, err := internal.NewWorklogRichTextService(client, ApiVersion) if err != nil { return nil, err } @@ -195,82 +202,82 @@ func New(httpClient common.HttpClient, site string) (*Client, error) { WorklogRichText: worklog, } - issueService, _, err := internal.NewIssueService(client, "2", issueServices) + issueService, _, err := internal.NewIssueService(client, ApiVersion, issueServices) if err != nil { return nil, err } - mySelf, err := internal.NewMySelfService(client, "2") + mySelf, err := internal.NewMySelfService(client, ApiVersion) if err != nil { return nil, err } - permissionSchemeGrant, err := internal.NewPermissionSchemeGrantService(client, "2") + permissionSchemeGrant, err := internal.NewPermissionSchemeGrantService(client, ApiVersion) if err != nil { return nil, err } - permissionScheme, err := internal.NewPermissionSchemeService(client, "2", permissionSchemeGrant) + permissionScheme, err := internal.NewPermissionSchemeService(client, ApiVersion, permissionSchemeGrant) if err != nil { return nil, err } - permission, err := internal.NewPermissionService(client, "2", permissionScheme) + permission, err := internal.NewPermissionService(client, ApiVersion, permissionScheme) if err != nil { return nil, err } - projectCategory, err := internal.NewProjectCategoryService(client, "2") + projectCategory, err := internal.NewProjectCategoryService(client, ApiVersion) if err != nil { return nil, err } - projectComponent, err := internal.NewProjectComponentService(client, "2") + projectComponent, err := internal.NewProjectComponentService(client, ApiVersion) if err != nil { return nil, err } - projectFeature, err := internal.NewProjectFeatureService(client, "2") + projectFeature, err := internal.NewProjectFeatureService(client, ApiVersion) if err != nil { return nil, err } - projectPermission, err := internal.NewProjectPermissionSchemeService(client, "2") + projectPermission, err := internal.NewProjectPermissionSchemeService(client, ApiVersion) if err != nil { return nil, err } - projectProperties, err := internal.NewProjectPropertyService(client, "2") + projectProperties, err := internal.NewProjectPropertyService(client, ApiVersion) if err != nil { return nil, err } - projectRoleActor, err := internal.NewProjectRoleActorService(client, "2") + projectRoleActor, err := internal.NewProjectRoleActorService(client, ApiVersion) if err != nil { return nil, err } - projectRole, err := internal.NewProjectRoleService(client, "2", projectRoleActor) + projectRole, err := internal.NewProjectRoleService(client, ApiVersion, projectRoleActor) if err != nil { return nil, err } - projectType, err := internal.NewProjectTypeService(client, "2") + projectType, err := internal.NewProjectTypeService(client, ApiVersion) if err != nil { return nil, err } - projectValidator, err := internal.NewProjectValidatorService(client, "2") + projectValidator, err := internal.NewProjectValidatorService(client, ApiVersion) if err != nil { return nil, err } - projectVersion, err := internal.NewProjectVersionService(client, "2") + projectVersion, err := internal.NewProjectVersionService(client, ApiVersion) if err != nil { return nil, err } - projectNotificationScheme, err := internal.NewNotificationSchemeService(client, "2") + projectNotificationScheme, err := internal.NewNotificationSchemeService(client, ApiVersion) if err != nil { return nil, err } @@ -287,67 +294,67 @@ func New(httpClient common.HttpClient, site string) (*Client, error) { Version: projectVersion, } - project, err := internal.NewProjectService(client, "2", projectSubService) + project, err := internal.NewProjectService(client, ApiVersion, projectSubService) if err != nil { return nil, err } - screenFieldTabField, err := internal.NewScreenTabFieldService(client, "2") + screenFieldTabField, err := internal.NewScreenTabFieldService(client, ApiVersion) if err != nil { return nil, err } - screenTab, err := internal.NewScreenTabService(client, "2", screenFieldTabField) + screenTab, err := internal.NewScreenTabService(client, ApiVersion, screenFieldTabField) if err != nil { return nil, err } - screenScheme, err := internal.NewScreenSchemeService(client, "2") + screenScheme, err := internal.NewScreenSchemeService(client, ApiVersion) if err != nil { return nil, err } - screen, err := internal.NewScreenService(client, "2", screenScheme, screenTab) + screen, err := internal.NewScreenService(client, ApiVersion, screenScheme, screenTab) if err != nil { return nil, err } - task, err := internal.NewTaskService(client, "2") + task, err := internal.NewTaskService(client, ApiVersion) if err != nil { return nil, err } - server, err := internal.NewServerService(client, "2") + server, err := internal.NewServerService(client, ApiVersion) if err != nil { return nil, err } - userSearch, err := internal.NewUserSearchService(client, "2") + userSearch, err := internal.NewUserSearchService(client, ApiVersion) if err != nil { return nil, err } - user, err := internal.NewUserService(client, "2", userSearch) + user, err := internal.NewUserService(client, ApiVersion, userSearch) if err != nil { return nil, err } workflowScheme := internal.NewWorkflowSchemeService( client, - "2", - internal.NewWorkflowSchemeIssueTypeService(client, "2")) + ApiVersion, + internal.NewWorkflowSchemeIssueTypeService(client, ApiVersion)) - workflowStatus, err := internal.NewWorkflowStatusService(client, "2") + workflowStatus, err := internal.NewWorkflowStatusService(client, ApiVersion) if err != nil { return nil, err } - workflow, err := internal.NewWorkflowService(client, "2", workflowScheme, workflowStatus) + workflow, err := internal.NewWorkflowService(client, ApiVersion, workflowScheme, workflowStatus) if err != nil { return nil, err } - jql, err := internal.NewJQLService(client, "2") + jql, err := internal.NewJQLService(client, ApiVersion) if err != nil { return nil, err } @@ -356,7 +363,7 @@ func New(httpClient common.HttpClient, site string) (*Client, error) { client.Permission = permission client.MySelf = mySelf client.Auth = internal.NewAuthenticationService(client) - client.Banner = internal.NewAnnouncementBannerService(client, "2") + client.Banner = internal.NewAnnouncementBannerService(client, ApiVersion) client.Role = applicationRoleService client.Dashboard = dashboardService client.Filter = filterService @@ -399,66 +406,49 @@ type Client struct { Team *internal.TeamService } -func (c *Client) NewFormRequest(ctx context.Context, method, apiEndpoint, contentType string, payload io.Reader) (*http.Request, error) { - - relativePath, err := url.Parse(apiEndpoint) - if err != nil { - return nil, err - } - - var endpoint = c.Site.ResolveReference(relativePath).String() +func (c *Client) NewRequest(ctx context.Context, method, urlStr, type_ string, body interface{}) (*http.Request, error) { - request, err := http.NewRequestWithContext(ctx, method, endpoint, payload) + rel, err := url.Parse(urlStr) if err != nil { return nil, err } - request.Header.Add("Content-Type", contentType) - request.Header.Add("Accept", "application/json") - request.Header.Set("X-Atlassian-Token", "no-check") + u := c.Site.ResolveReference(rel) - if c.Auth.HasBasicAuth() { - request.SetBasicAuth(c.Auth.GetBasicAuth()) - } - - if c.Auth.HasUserAgent() { - request.Header.Set("User-Agent", c.Auth.GetUserAgent()) + buf := new(bytes.Buffer) + if body != nil { + if err = json.NewEncoder(buf).Encode(body); err != nil { + return nil, err + } } - return request, nil -} - -func (c *Client) NewRequest(ctx context.Context, method, apiEndpoint string, payload io.Reader) (*http.Request, error) { - - relativePath, err := url.Parse(apiEndpoint) + req, err := http.NewRequestWithContext(ctx, method, u.String(), buf) if err != nil { return nil, err } - var endpoint = c.Site.ResolveReference(relativePath).String() + req.Header.Set("Accept", "application/json") - request, err := http.NewRequestWithContext(ctx, method, endpoint, payload) - if err != nil { - return nil, err + if body != nil { + req.Header.Set("Content-Type", "application/json") } - request.Header.Set("Accept", "application/json") - - if payload != nil { - request.Header.Set("Content-Type", "application/json") + if type_ != "" { + // When the type_ is provided, it means the request needs to be created to handle files + req.Header.Set("Content-Type", type_) + req.Header.Set("X-Atlassian-Token", "no-check") } if c.Auth.HasBasicAuth() { - request.SetBasicAuth(c.Auth.GetBasicAuth()) + req.SetBasicAuth(c.Auth.GetBasicAuth()) } if c.Auth.HasUserAgent() { - request.Header.Set("User-Agent", c.Auth.GetUserAgent()) + req.Header.Set("User-Agent", c.Auth.GetUserAgent()) } - return request, nil + return req, nil } - func (c *Client) Call(request *http.Request, structure interface{}) (*models.ResponseScheme, error) { response, err := c.HTTP.Do(request) @@ -466,12 +456,14 @@ func (c *Client) Call(request *http.Request, structure interface{}) (*models.Res return nil, err } - return c.TransformTheHTTPResponse(response, structure) + return c.processResponse(response, structure) } -func (c *Client) TransformTheHTTPResponse(response *http.Response, structure interface{}) (*models.ResponseScheme, error) { +func (c *Client) processResponse(response *http.Response, structure interface{}) (*models.ResponseScheme, error) { - responseTransformed := &models.ResponseScheme{ + defer response.Body.Close() + + res := &models.ResponseScheme{ Response: response, Code: response.StatusCode, Endpoint: response.Request.URL.String(), @@ -480,39 +472,39 @@ func (c *Client) TransformTheHTTPResponse(response *http.Response, structure int responseAsBytes, err := io.ReadAll(response.Body) if err != nil { - return responseTransformed, err + return res, err } - responseTransformed.Bytes.Write(responseAsBytes) + res.Bytes.Write(responseAsBytes) + + wasSuccess := response.StatusCode >= 200 && response.StatusCode < 300 - var wasSuccess = response.StatusCode >= 200 && response.StatusCode < 300 if !wasSuccess { - return responseTransformed, models.ErrInvalidStatusCodeError - } - if structure != nil { - if err = json.Unmarshal(responseAsBytes, &structure); err != nil { - return responseTransformed, err - } - } + switch response.StatusCode { - return responseTransformed, nil -} + case http.StatusNotFound: + return res, models.ErrNotFound -func (c *Client) TransformStructToReader(structure interface{}) (io.Reader, error) { + case http.StatusUnauthorized: + return res, models.ErrUnauthorized - if structure == nil { - return nil, models.ErrNilPayloadError - } + case http.StatusInternalServerError: + return res, models.ErrInternalError + + case http.StatusBadRequest: + return res, models.ErrBadRequestError - if reflect.ValueOf(structure).Type().Kind() == reflect.Struct { - return nil, models.ErrNonPayloadPointerError + default: + return res, models.ErrInvalidStatusCodeError + } } - structureAsBodyBytes, err := json.Marshal(structure) - if err != nil { - return nil, err + if structure != nil { + if err = json.Unmarshal(responseAsBytes, &structure); err != nil { + return res, err + } } - return bytes.NewReader(structureAsBodyBytes), nil + return res, nil } diff --git a/jira/v2/api_client_impl_test.go b/jira/v2/api_client_impl_test.go index 0d0f80c4..7db1d1d8 100644 --- a/jira/v2/api_client_impl_test.go +++ b/jira/v2/api_client_impl_test.go @@ -3,16 +3,13 @@ package v2 import ( "bytes" "context" - "encoding/json" "errors" "github.com/ctreminiom/go-atlassian/jira/internal" - "github.com/ctreminiom/go-atlassian/pkg/infra/models" + model "github.com/ctreminiom/go-atlassian/pkg/infra/models" "github.com/ctreminiom/go-atlassian/service/common" - "github.com/ctreminiom/go-atlassian/service/jira" "github.com/ctreminiom/go-atlassian/service/mocks" "github.com/stretchr/testify/assert" "io" - "io/ioutil" "net/http" "net/url" "strings" @@ -23,29 +20,54 @@ func TestClient_Call(t *testing.T) { expectedResponse := &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(strings.NewReader("Hello, world!")), + Body: io.NopCloser(strings.NewReader("Hello, world!")), Request: &http.Request{ Method: http.MethodGet, URL: &url.URL{}, }, } - nonExpectedResponse := &http.Response{ + badRequestResponse := &http.Response{ StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(strings.NewReader("Hello, world!")), + Body: io.NopCloser(strings.NewReader("Hello, world!")), Request: &http.Request{ Method: http.MethodGet, URL: &url.URL{}, }, } - type fields struct { - HTTP common.HttpClient - Site *url.URL - Authentication common.Authentication - ApplicationRole jira.AppRoleConnector + internalServerResponse := &http.Response{ + StatusCode: http.StatusInternalServerError, + Body: io.NopCloser(strings.NewReader("Hello, world!")), + Request: &http.Request{ + Method: http.MethodGet, + URL: &url.URL{}, + }, } + unauthorizedResponse := &http.Response{ + StatusCode: http.StatusUnauthorized, + Body: io.NopCloser(strings.NewReader("Hello, world!")), + Request: &http.Request{ + Method: http.MethodGet, + URL: &url.URL{}, + }, + } + + notFoundResponse := &http.Response{ + StatusCode: http.StatusNotFound, + Body: io.NopCloser(strings.NewReader("Hello, world!")), + Request: &http.Request{ + Method: http.MethodGet, + URL: &url.URL{}, + }, + } + + type fields struct { + HTTP common.HttpClient + Site *url.URL + Auth common.Authentication + } type args struct { request *http.Request structure interface{} @@ -54,9 +76,9 @@ func TestClient_Call(t *testing.T) { testCases := []struct { name string fields fields - on func(*fields) args args - want *models.ResponseScheme + on func(*fields) + want *model.ResponseScheme wantErr bool Err error }{ @@ -75,7 +97,7 @@ func TestClient_Call(t *testing.T) { request: nil, structure: nil, }, - want: &models.ResponseScheme{ + want: &model.ResponseScheme{ Response: expectedResponse, Code: http.StatusOK, Method: http.MethodGet, @@ -85,28 +107,38 @@ func TestClient_Call(t *testing.T) { }, { - name: "when the http callback cannot be executed", + name: "when the response status is a bad request", on: func(fields *fields) { client := mocks.NewHttpClient(t) client.On("Do", (*http.Request)(nil)). - Return(nil, errors.New("error, unable to execute the http call")) + Return(badRequestResponse, nil) fields.HTTP = client }, + args: args{ + request: nil, + structure: nil, + }, + want: &model.ResponseScheme{ + Response: badRequestResponse, + Code: http.StatusBadRequest, + Method: http.MethodGet, + Bytes: *bytes.NewBufferString("Hello, world!"), + }, wantErr: true, - Err: errors.New("error, unable to execute the http call"), + Err: model.ErrBadRequestError, }, { - name: "when the response status is not valid", + name: "when the response status is an internal service error", on: func(fields *fields) { client := mocks.NewHttpClient(t) client.On("Do", (*http.Request)(nil)). - Return(nonExpectedResponse, nil) + Return(internalServerResponse, nil) fields.HTTP = client }, @@ -114,19 +146,67 @@ func TestClient_Call(t *testing.T) { request: nil, structure: nil, }, - want: &models.ResponseScheme{ - Response: nonExpectedResponse, - Code: http.StatusBadRequest, + want: &model.ResponseScheme{ + Response: internalServerResponse, + Code: http.StatusInternalServerError, Method: http.MethodGet, Bytes: *bytes.NewBufferString("Hello, world!"), }, wantErr: true, - Err: models.ErrInvalidStatusCodeError, + Err: model.ErrInternalError, }, - } - for _, testCase := range testCases { + { + name: "when the response status is a not found", + on: func(fields *fields) { + + client := mocks.NewHttpClient(t) + + client.On("Do", (*http.Request)(nil)). + Return(notFoundResponse, nil) + + fields.HTTP = client + }, + args: args{ + request: nil, + structure: nil, + }, + want: &model.ResponseScheme{ + Response: notFoundResponse, + Code: http.StatusNotFound, + Method: http.MethodGet, + Bytes: *bytes.NewBufferString("Hello, world!"), + }, + wantErr: true, + Err: model.ErrNotFound, + }, + + { + name: "when the response status is unauthorized", + on: func(fields *fields) { + client := mocks.NewHttpClient(t) + + client.On("Do", (*http.Request)(nil)). + Return(unauthorizedResponse, nil) + + fields.HTTP = client + }, + args: args{ + request: nil, + structure: nil, + }, + want: &model.ResponseScheme{ + Response: unauthorizedResponse, + Code: http.StatusUnauthorized, + Method: http.MethodGet, + Bytes: *bytes.NewBufferString("Hello, world!"), + }, + wantErr: true, + Err: model.ErrUnauthorized, + }, + } + for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { if testCase.on != nil { @@ -136,7 +216,6 @@ func TestClient_Call(t *testing.T) { c := &Client{ HTTP: testCase.fields.HTTP, Site: testCase.fields.Site, - Auth: testCase.fields.Authentication, } got, err := c.Call(testCase.args.request, testCase.args.structure) @@ -153,61 +232,125 @@ func TestClient_Call(t *testing.T) { assert.NoError(t, err) assert.Equal(t, got, testCase.want) } + }) } } -func TestNewV2(t *testing.T) { +func TestClient_NewRequest(t *testing.T) { - mockClient, err := New(http.DefaultClient, "https://ctreminiom.atlassian.net") + authMocked := internal.NewAuthenticationService(nil) + authMocked.SetBasicAuth("mail", "token") + authMocked.SetUserAgent("firefox") + + siteAsURL, err := url.Parse("https://ctreminiom.atlassian.net") if err != nil { t.Fatal(err) } - mockClient.Auth.SetBasicAuth("test", "test") - mockClient.Auth.SetUserAgent("aaa") + requestMocked, err := http.NewRequestWithContext(context.TODO(), + http.MethodGet, + "https://ctreminiom.atlassian.net/rest/2/issue/attachment", + bytes.NewReader([]byte("Hello World")), + ) + + if err != nil { + t.Fatal(err) + } + + requestMocked.Header.Set("Accept", "application/json") + requestMocked.Header.Set("Content-Type", "application/json") - mockClient2, _ := New(nil, " https://zhidao.baidu.com/special/view?id=sd&preview=1") + type fields struct { + HTTP common.HttpClient + Auth common.Authentication + Site *url.URL + } type args struct { - httpClient common.HttpClient - site string + ctx context.Context + method string + urlStr string + type_ string + body interface{} } testCases := []struct { name string + fields fields args args - on func(*args) - want *Client + want *http.Request wantErr bool - Err error }{ { name: "when the parameters are correct", + fields: fields{ + HTTP: http.DefaultClient, + Auth: authMocked, + Site: siteAsURL, + }, args: args{ - httpClient: http.DefaultClient, - site: "https://ctreminiom.atlassian.net", + ctx: context.TODO(), + method: http.MethodGet, + urlStr: "rest/2/issue/attachment", + type_: "", + body: bytes.NewReader([]byte("Hello World")), }, - want: mockClient, + want: requestMocked, wantErr: false, }, { - name: "when the site url is not valid", + name: "when the url cannot be parsed", + fields: fields{ + HTTP: http.DefaultClient, + Auth: internal.NewAuthenticationService(nil), + Site: siteAsURL, + }, args: args{ - httpClient: http.DefaultClient, - site: " https://zhidao.baidu.com/special/view?id=sd&preview=1", + ctx: context.TODO(), + method: http.MethodGet, + urlStr: " https://zhidao.baidu.com/special/view?id=49105a24626975510000&preview=1", + body: bytes.NewReader([]byte("Hello World")), }, - want: mockClient2, + want: nil, + wantErr: true, + }, + + { + name: "when the request cannot be created", + fields: fields{ + HTTP: http.DefaultClient, + Auth: internal.NewAuthenticationService(nil), + Site: siteAsURL, + }, + args: args{ + ctx: nil, + method: http.MethodGet, + urlStr: "rest/2/issue/attachment", + body: bytes.NewReader([]byte("Hello World")), + }, + want: requestMocked, wantErr: true, - Err: errors.New("parse \" https://zhidao.baidu.com/special/view?id=sd&preview=1/\": first path segment in URL cannot contain colon"), }, } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - gotClient, err := New(testCase.args.httpClient, testCase.args.site) + c := &Client{ + HTTP: testCase.fields.HTTP, + Auth: testCase.fields.Auth, + Site: testCase.fields.Site, + } + + got, err := c.NewRequest( + testCase.args.ctx, + testCase.args.method, + testCase.args.urlStr, + testCase.args.type_, + testCase.args.body, + ) if testCase.wantErr { @@ -216,18 +359,16 @@ func TestNewV2(t *testing.T) { } assert.Error(t, err) - assert.EqualError(t, err, testCase.Err.Error()) - } else { assert.NoError(t, err) - assert.NotEqual(t, gotClient, nil) + assert.NotEqual(t, got, nil) } }) } } -func TestClient_TransformTheHTTPResponse(t *testing.T) { +func TestClient_processResponse(t *testing.T) { expectedJsonResponse := ` { @@ -239,7 +380,7 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { expectedResponse := &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(strings.NewReader(expectedJsonResponse)), + Body: io.NopCloser(strings.NewReader(expectedJsonResponse)), Request: &http.Request{ Method: http.MethodGet, URL: &url.URL{}, @@ -247,12 +388,10 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { } type fields struct { - HTTP common.HttpClient - Site *url.URL - Authentication common.Authentication - ApplicationRole jira.AppRoleConnector + HTTP common.HttpClient + Site *url.URL + Authentication common.Authentication } - type args struct { response *http.Response structure interface{} @@ -262,7 +401,7 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { name string fields fields args args - want *models.ResponseScheme + want *model.ResponseScheme wantErr bool Err error }{ @@ -271,9 +410,9 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { fields: fields{}, args: args{ response: expectedResponse, - structure: models.BoardScheme{}, + structure: model.BoardScheme{}, }, - want: &models.ResponseScheme{ + want: &model.ResponseScheme{ Response: expectedResponse, Code: http.StatusOK, Method: http.MethodGet, @@ -282,110 +421,16 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { wantErr: false, }, } - for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - c := &Client{ - HTTP: testCase.fields.HTTP, - Site: testCase.fields.Site, - Auth: testCase.fields.Authentication, - } - - got, err := c.TransformTheHTTPResponse(testCase.args.response, testCase.args.structure) - if testCase.wantErr { - - if err != nil { - t.Logf("error returned: %v", err.Error()) - } - - assert.Error(t, err) - assert.EqualError(t, err, testCase.Err.Error()) - - } else { - assert.NoError(t, err) - assert.NotEqual(t, got, nil) - } - }) - } -} - -func TestClient_TransformStructToReader(t *testing.T) { - - expectedBytes, err := json.Marshal(&models.BoardScheme{ - Name: "Board Sample", - Type: "Scrum", - }) - - if err != nil { - t.Fatal(err) - } - - type fields struct { - HTTP common.HttpClient - Site *url.URL - Authentication common.Authentication - ApplicationRole jira.AppRoleConnector - } - - type args struct { - structure interface{} - } - - testCases := []struct { - name string - fields fields - args args - want io.Reader - wantErr bool - Err error - }{ - { - name: "when the parameters are correct", - args: args{ - structure: &models.BoardScheme{ - Name: "Board Sample", - Type: "Scrum", - }, - }, - want: bytes.NewReader(expectedBytes), - wantErr: false, - }, - - { - name: "when the payload provided is not a pointer", - args: args{ - structure: models.BoardScheme{ - Name: "Board Sample", - Type: "Scrum", - }, - }, - want: bytes.NewReader(expectedBytes), - wantErr: true, - Err: models.ErrNonPayloadPointerError, - }, - - { - name: "when the payload is not provided", - args: args{ - structure: nil, - }, - want: bytes.NewReader(expectedBytes), - wantErr: true, - Err: models.ErrNilPayloadError, - }, - } - - for _, testCase := range testCases { - - t.Run(testCase.name, func(t *testing.T) { c := &Client{ HTTP: testCase.fields.HTTP, Site: testCase.fields.Site, Auth: testCase.fields.Authentication, } - got, err := c.TransformStructToReader(testCase.args.structure) + got, err := c.processResponse(testCase.args.response, testCase.args.structure) if testCase.wantErr { @@ -400,239 +445,74 @@ func TestClient_TransformStructToReader(t *testing.T) { assert.NoError(t, err) assert.NotEqual(t, got, nil) } - }) - } -} - -func TestClient_NewFormRequest(t *testing.T) { - - authMocked := internal.NewAuthenticationService(nil) - authMocked.SetBasicAuth("mail", "token") - authMocked.SetUserAgent("firefox") - - siteAsURL, err := url.Parse("https://ctreminiom.atlassian.net") - if err != nil { - t.Fatal(err) - } - - requestMocked, err := http.NewRequestWithContext(context.TODO(), - http.MethodGet, - "https://ctreminiom.atlassian.net/rest/2/issue/attachment", - bytes.NewReader([]byte("Hello World")), - ) - - if err != nil { - t.Fatal(err) - } - - requestMocked.Header.Add("Content-Type", "form-type-sample") - requestMocked.Header.Add("Accept", "application/json") - requestMocked.Header.Set("X-Atlassian-Token", "no-check") - - type fields struct { - HTTP common.HttpClient - Auth common.Authentication - Site *url.URL - } - type args struct { - ctx context.Context - method string - apiEndpoint string - contentType string - payload io.Reader - } - - testCases := []struct { - name string - fields fields - args args - want *http.Request - wantErr bool - }{ - { - name: "when the parameters are correct", - fields: fields{ - HTTP: http.DefaultClient, - Auth: authMocked, - Site: siteAsURL, - }, - args: args{ - ctx: context.TODO(), - method: http.MethodGet, - apiEndpoint: "rest/2/issue/attachment", - contentType: "form-type-sample", - payload: bytes.NewReader([]byte("Hello World")), - }, - want: requestMocked, - wantErr: false, - }, - - { - name: "when the url cannot be parsed", - fields: fields{ - HTTP: http.DefaultClient, - Auth: internal.NewAuthenticationService(nil), - Site: siteAsURL, - }, - args: args{ - ctx: context.TODO(), - method: http.MethodGet, - apiEndpoint: " https://zhidao.baidu.com/special/view?id=49105a24626975510000&preview=1", - contentType: "form-type-sample", - payload: bytes.NewReader([]byte("Hello World")), - }, - want: nil, - wantErr: true, - }, - - { - name: "when the request cannot be created", - fields: fields{ - HTTP: http.DefaultClient, - Auth: internal.NewAuthenticationService(nil), - Site: siteAsURL, - }, - args: args{ - ctx: nil, - method: http.MethodGet, - apiEndpoint: "rest/2/issue/attachment", - contentType: "form-type-sample", - payload: bytes.NewReader([]byte("Hello World")), - }, - want: requestMocked, - wantErr: true, - }, - } - for _, testCase := range testCases { - t.Run(testCase.name, func(t *testing.T) { - c := &Client{ - HTTP: testCase.fields.HTTP, - Auth: testCase.fields.Auth, - Site: testCase.fields.Site, - } - - got, err := c.NewFormRequest(testCase.args.ctx, testCase.args.method, testCase.args.apiEndpoint, testCase.args.contentType, testCase.args.payload) - - if testCase.wantErr { - - if err != nil { - t.Logf("error returned: %v", err.Error()) - } - - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.NotEqual(t, got, nil) - } }) } } -func TestClient_NewRequest(t *testing.T) { - - authMocked := internal.NewAuthenticationService(nil) - authMocked.SetBasicAuth("mail", "token") - authMocked.SetUserAgent("firefox") +func TestNew(t *testing.T) { - siteAsURL, err := url.Parse("https://ctreminiom.atlassian.net") + mockClient, err := New(http.DefaultClient, "https://ctreminiom.atlassian.net") if err != nil { t.Fatal(err) } - requestMocked, err := http.NewRequestWithContext(context.TODO(), - http.MethodGet, - "https://ctreminiom.atlassian.net/rest/2/issue/attachment", - bytes.NewReader([]byte("Hello World")), - ) + mockClient.Auth.SetBasicAuth("test", "test") + mockClient.Auth.SetUserAgent("aaa") - if err != nil { - t.Fatal(err) - } + invalidURLClientMocked, _ := New(nil, " https://zhidao.baidu.com/special/view?id=sd&preview=1") - requestMocked.Header.Set("Accept", "application/json") - requestMocked.Header.Set("Content-Type", "application/json") - - type fields struct { - HTTP common.HttpClient - Auth common.Authentication - Site *url.URL - } + noURLClientMocked, _ := New(nil, "") type args struct { - ctx context.Context - method string - apiEndpoint string - payload io.Reader + httpClient common.HttpClient + site string } testCases := []struct { name string - fields fields args args - want *http.Request + want *Client wantErr bool + Err error }{ + { name: "when the parameters are correct", - fields: fields{ - HTTP: http.DefaultClient, - Auth: authMocked, - Site: siteAsURL, - }, args: args{ - ctx: context.TODO(), - method: http.MethodGet, - apiEndpoint: "rest/2/issue/attachment", - payload: bytes.NewReader([]byte("Hello World")), + httpClient: http.DefaultClient, + site: "https://ctreminiom.atlassian.net", }, - want: requestMocked, + want: mockClient, wantErr: false, }, { - name: "when the url cannot be parsed", - fields: fields{ - HTTP: http.DefaultClient, - Auth: internal.NewAuthenticationService(nil), - Site: siteAsURL, - }, + name: "when the site url are not provided", args: args{ - ctx: context.TODO(), - method: http.MethodGet, - apiEndpoint: " https://zhidao.baidu.com/special/view?id=49105a24626975510000&preview=1", - payload: bytes.NewReader([]byte("Hello World")), + httpClient: http.DefaultClient, + site: "", }, - want: nil, + want: noURLClientMocked, wantErr: true, + Err: model.ErrNoSiteError, }, - { - name: "when the request cannot be created", - fields: fields{ - HTTP: http.DefaultClient, - Auth: internal.NewAuthenticationService(nil), - Site: siteAsURL, - }, + name: "when the site url is not valid", args: args{ - ctx: nil, - method: http.MethodGet, - apiEndpoint: "rest/2/issue/attachment", - payload: bytes.NewReader([]byte("Hello World")), + httpClient: http.DefaultClient, + site: " https://zhidao.baidu.com/special/view?id=sd&preview=1", }, - want: requestMocked, + want: invalidURLClientMocked, wantErr: true, + Err: errors.New("parse \" https://zhidao.baidu.com/special/view?id=sd&preview=1/\": first path segment in URL cannot contain colon"), }, } + for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - c := &Client{ - HTTP: testCase.fields.HTTP, - Auth: testCase.fields.Auth, - Site: testCase.fields.Site, - } - got, err := c.NewRequest(testCase.args.ctx, testCase.args.method, testCase.args.apiEndpoint, testCase.args.payload) + gotClient, err := New(testCase.args.httpClient, testCase.args.site) if testCase.wantErr { @@ -641,9 +521,11 @@ func TestClient_NewRequest(t *testing.T) { } assert.Error(t, err) + assert.EqualError(t, err, testCase.Err.Error()) + } else { assert.NoError(t, err) - assert.NotEqual(t, got, nil) + assert.NotEqual(t, gotClient, nil) } }) } diff --git a/jira/v3/api_client_impl.go b/jira/v3/api_client_impl.go index b8555c81..fb23984c 100644 --- a/jira/v3/api_client_impl.go +++ b/jira/v3/api_client_impl.go @@ -10,171 +10,178 @@ import ( "io" "net/http" "net/url" - "reflect" "strings" ) +const ApiVersion = "3" + func New(httpClient common.HttpClient, site string) (*Client, error) { if httpClient == nil { httpClient = http.DefaultClient } + if site == "" { + return nil, models.ErrNoSiteError + } + if !strings.HasSuffix(site, "/") { site += "/" } - siteAsURL, err := url.Parse(site) + u, err := url.Parse(site) if err != nil { return nil, err } client := &Client{ HTTP: httpClient, - Site: siteAsURL, + Site: u, } - auditRecord, err := internal.NewAuditRecordService(client, "3") + client.Auth = internal.NewAuthenticationService(client) + + auditRecord, err := internal.NewAuditRecordService(client, ApiVersion) if err != nil { return nil, err } - applicationRoleService, err := internal.NewApplicationRoleService(client, "3") + applicationRoleService, err := internal.NewApplicationRoleService(client, ApiVersion) if err != nil { return nil, err } - dashboardService, err := internal.NewDashboardService(client, "3") + dashboardService, err := internal.NewDashboardService(client, ApiVersion) if err != nil { return nil, err } - filterShareService, err := internal.NewFilterShareService(client, "3") + filterShareService, err := internal.NewFilterShareService(client, ApiVersion) if err != nil { return nil, err } - filterService, err := internal.NewFilterService(client, "3", filterShareService) + filterService, err := internal.NewFilterService(client, ApiVersion, filterShareService) if err != nil { return nil, err } - groupService, err := internal.NewGroupService(client, "3") + groupService, err := internal.NewGroupService(client, ApiVersion) if err != nil { return nil, err } - issueAttachmentService, err := internal.NewIssueAttachmentService(client, "3") + issueAttachmentService, err := internal.NewIssueAttachmentService(client, ApiVersion) if err != nil { return nil, err } - commentService, _, err := internal.NewCommentService(client, "3") + commentService, _, err := internal.NewCommentService(client, ApiVersion) if err != nil { return nil, err } - fieldConfigurationItemService, err := internal.NewIssueFieldConfigurationItemService(client, "3") + fieldConfigurationItemService, err := internal.NewIssueFieldConfigurationItemService(client, ApiVersion) if err != nil { return nil, err } - fieldConfigurationSchemeService, err := internal.NewIssueFieldConfigurationSchemeService(client, "3") + fieldConfigurationSchemeService, err := internal.NewIssueFieldConfigurationSchemeService(client, ApiVersion) if err != nil { return nil, err } - fieldConfigService, err := internal.NewIssueFieldConfigurationService(client, "3", fieldConfigurationItemService, fieldConfigurationSchemeService) + fieldConfigService, err := internal.NewIssueFieldConfigurationService(client, ApiVersion, fieldConfigurationItemService, fieldConfigurationSchemeService) if err != nil { return nil, err } - optionService, err := internal.NewIssueFieldContextOptionService(client, "3") + optionService, err := internal.NewIssueFieldContextOptionService(client, ApiVersion) if err != nil { return nil, err } - fieldContextService, err := internal.NewIssueFieldContextService(client, "3", optionService) + fieldContextService, err := internal.NewIssueFieldContextService(client, ApiVersion, optionService) if err != nil { return nil, err } - fieldTrashService, err := internal.NewIssueFieldTrashService(client, "3") + fieldTrashService, err := internal.NewIssueFieldTrashService(client, ApiVersion) if err != nil { return nil, err } - issueFieldService, err := internal.NewIssueFieldService(client, "3", fieldConfigService, fieldContextService, fieldTrashService) + issueFieldService, err := internal.NewIssueFieldService(client, ApiVersion, fieldConfigService, fieldContextService, fieldTrashService) if err != nil { return nil, err } - label, err := internal.NewLabelService(client, "3") + label, err := internal.NewLabelService(client, ApiVersion) if err != nil { return nil, err } - linkType, err := internal.NewLinkTypeService(client, "3") + linkType, err := internal.NewLinkTypeService(client, ApiVersion) if err != nil { return nil, err } - remoteLink, err := internal.NewRemoteLinkService(client, "3") + remoteLink, err := internal.NewRemoteLinkService(client, ApiVersion) if err != nil { return nil, err } - link, _, err := internal.NewLinkService(client, "3", linkType, remoteLink) + link, _, err := internal.NewLinkService(client, ApiVersion, linkType, remoteLink) if err != nil { return nil, err } - metadata, err := internal.NewMetadataService(client, "3") + metadata, err := internal.NewMetadataService(client, ApiVersion) if err != nil { return nil, err } - priority, err := internal.NewPriorityService(client, "3") + priority, err := internal.NewPriorityService(client, ApiVersion) if err != nil { return nil, err } - resolution, err := internal.NewResolutionService(client, "3") + resolution, err := internal.NewResolutionService(client, ApiVersion) if err != nil { return nil, err } - search, _, err := internal.NewSearchService(client, "3") + search, _, err := internal.NewSearchService(client, ApiVersion) if err != nil { return nil, err } - typeScheme, err := internal.NewTypeSchemeService(client, "3") + typeScheme, err := internal.NewTypeSchemeService(client, ApiVersion) if err != nil { return nil, err } - issueTypeScreenScheme, err := internal.NewTypeScreenSchemeService(client, "3") + issueTypeScreenScheme, err := internal.NewTypeScreenSchemeService(client, ApiVersion) if err != nil { return nil, err } - type_, err := internal.NewTypeService(client, "3", typeScheme, issueTypeScreenScheme) + type_, err := internal.NewTypeService(client, ApiVersion, typeScheme, issueTypeScreenScheme) if err != nil { return nil, err } - vote, err := internal.NewVoteService(client, "3") + vote, err := internal.NewVoteService(client, ApiVersion) if err != nil { return nil, err } - watcher, err := internal.NewWatcherService(client, "3") + watcher, err := internal.NewWatcherService(client, ApiVersion) if err != nil { return nil, err } - worklog, err := internal.NewWorklogADFService(client, "3") + worklog, err := internal.NewWorklogADFService(client, ApiVersion) if err != nil { return nil, err } @@ -195,82 +202,82 @@ func New(httpClient common.HttpClient, site string) (*Client, error) { WorklogAdf: worklog, } - mySelf, err := internal.NewMySelfService(client, "3") + mySelf, err := internal.NewMySelfService(client, ApiVersion) if err != nil { return nil, err } - permissionSchemeGrant, err := internal.NewPermissionSchemeGrantService(client, "3") + permissionSchemeGrant, err := internal.NewPermissionSchemeGrantService(client, ApiVersion) if err != nil { return nil, err } - permissionScheme, err := internal.NewPermissionSchemeService(client, "3", permissionSchemeGrant) + permissionScheme, err := internal.NewPermissionSchemeService(client, ApiVersion, permissionSchemeGrant) if err != nil { return nil, err } - permission, err := internal.NewPermissionService(client, "3", permissionScheme) + permission, err := internal.NewPermissionService(client, ApiVersion, permissionScheme) if err != nil { return nil, err } - _, issueService, err := internal.NewIssueService(client, "3", issueServices) + _, issueService, err := internal.NewIssueService(client, ApiVersion, issueServices) if err != nil { return nil, err } - projectCategory, err := internal.NewProjectCategoryService(client, "3") + projectCategory, err := internal.NewProjectCategoryService(client, ApiVersion) if err != nil { return nil, err } - projectComponent, err := internal.NewProjectComponentService(client, "3") + projectComponent, err := internal.NewProjectComponentService(client, ApiVersion) if err != nil { return nil, err } - projectFeature, err := internal.NewProjectFeatureService(client, "3") + projectFeature, err := internal.NewProjectFeatureService(client, ApiVersion) if err != nil { return nil, err } - projectPermission, err := internal.NewProjectPermissionSchemeService(client, "3") + projectPermission, err := internal.NewProjectPermissionSchemeService(client, ApiVersion) if err != nil { return nil, err } - projectProperties, err := internal.NewProjectPropertyService(client, "3") + projectProperties, err := internal.NewProjectPropertyService(client, ApiVersion) if err != nil { return nil, err } - projectRoleActor, err := internal.NewProjectRoleActorService(client, "3") + projectRoleActor, err := internal.NewProjectRoleActorService(client, ApiVersion) if err != nil { return nil, err } - projectRole, err := internal.NewProjectRoleService(client, "3", projectRoleActor) + projectRole, err := internal.NewProjectRoleService(client, ApiVersion, projectRoleActor) if err != nil { return nil, err } - projectType, err := internal.NewProjectTypeService(client, "3") + projectType, err := internal.NewProjectTypeService(client, ApiVersion) if err != nil { return nil, err } - projectValidator, err := internal.NewProjectValidatorService(client, "3") + projectValidator, err := internal.NewProjectValidatorService(client, ApiVersion) if err != nil { return nil, err } - projectVersion, err := internal.NewProjectVersionService(client, "3") + projectVersion, err := internal.NewProjectVersionService(client, ApiVersion) if err != nil { return nil, err } - projectNotificationScheme, err := internal.NewNotificationSchemeService(client, "3") + projectNotificationScheme, err := internal.NewNotificationSchemeService(client, ApiVersion) if err != nil { return nil, err } @@ -287,67 +294,67 @@ func New(httpClient common.HttpClient, site string) (*Client, error) { Version: projectVersion, } - project, err := internal.NewProjectService(client, "3", projectSubService) + project, err := internal.NewProjectService(client, ApiVersion, projectSubService) if err != nil { return nil, err } - screenFieldTabField, err := internal.NewScreenTabFieldService(client, "3") + screenFieldTabField, err := internal.NewScreenTabFieldService(client, ApiVersion) if err != nil { return nil, err } - screenTab, err := internal.NewScreenTabService(client, "3", screenFieldTabField) + screenTab, err := internal.NewScreenTabService(client, ApiVersion, screenFieldTabField) if err != nil { return nil, err } - screenScheme, err := internal.NewScreenSchemeService(client, "3") + screenScheme, err := internal.NewScreenSchemeService(client, ApiVersion) if err != nil { return nil, err } - screen, err := internal.NewScreenService(client, "3", screenScheme, screenTab) + screen, err := internal.NewScreenService(client, ApiVersion, screenScheme, screenTab) if err != nil { return nil, err } - task, err := internal.NewTaskService(client, "3") + task, err := internal.NewTaskService(client, ApiVersion) if err != nil { return nil, err } - server, err := internal.NewServerService(client, "3") + server, err := internal.NewServerService(client, ApiVersion) if err != nil { return nil, err } - userSearch, err := internal.NewUserSearchService(client, "3") + userSearch, err := internal.NewUserSearchService(client, ApiVersion) if err != nil { return nil, err } - user, err := internal.NewUserService(client, "3", userSearch) + user, err := internal.NewUserService(client, ApiVersion, userSearch) if err != nil { return nil, err } workflowScheme := internal.NewWorkflowSchemeService( client, - "3", - internal.NewWorkflowSchemeIssueTypeService(client, "3")) + ApiVersion, + internal.NewWorkflowSchemeIssueTypeService(client, ApiVersion)) - workflowStatus, err := internal.NewWorkflowStatusService(client, "3") + workflowStatus, err := internal.NewWorkflowStatusService(client, ApiVersion) if err != nil { return nil, err } - workflow, err := internal.NewWorkflowService(client, "3", workflowScheme, workflowStatus) + workflow, err := internal.NewWorkflowService(client, ApiVersion, workflowScheme, workflowStatus) if err != nil { return nil, err } - jql, err := internal.NewJQLService(client, "3") + jql, err := internal.NewJQLService(client, ApiVersion) if err != nil { return nil, err } @@ -356,7 +363,7 @@ func New(httpClient common.HttpClient, site string) (*Client, error) { client.Permission = permission client.MySelf = mySelf client.Auth = internal.NewAuthenticationService(client) - client.Banner = internal.NewAnnouncementBannerService(client, "3") + client.Banner = internal.NewAnnouncementBannerService(client, ApiVersion) client.Role = applicationRoleService client.Dashboard = dashboardService client.Filter = filterService @@ -399,64 +406,48 @@ type Client struct { Team *internal.TeamService } -func (c *Client) NewFormRequest(ctx context.Context, method, apiEndpoint, contentType string, payload io.Reader) (*http.Request, error) { - - relativePath, err := url.Parse(apiEndpoint) - if err != nil { - return nil, err - } - - var endpoint = c.Site.ResolveReference(relativePath).String() +func (c *Client) NewRequest(ctx context.Context, method, urlStr, type_ string, body interface{}) (*http.Request, error) { - request, err := http.NewRequestWithContext(ctx, method, endpoint, payload) + rel, err := url.Parse(urlStr) if err != nil { return nil, err } - request.Header.Add("Content-Type", contentType) - request.Header.Add("Accept", "application/json") - request.Header.Set("X-Atlassian-Token", "no-check") + u := c.Site.ResolveReference(rel) - if c.Auth.HasBasicAuth() { - request.SetBasicAuth(c.Auth.GetBasicAuth()) - } - - if c.Auth.HasUserAgent() { - request.Header.Set("User-Agent", c.Auth.GetUserAgent()) + buf := new(bytes.Buffer) + if body != nil { + if err = json.NewEncoder(buf).Encode(body); err != nil { + return nil, err + } } - return request, nil -} - -func (c *Client) NewRequest(ctx context.Context, method, apiEndpoint string, payload io.Reader) (*http.Request, error) { - - relativePath, err := url.Parse(apiEndpoint) + req, err := http.NewRequestWithContext(ctx, method, u.String(), buf) if err != nil { return nil, err } - var endpoint = c.Site.ResolveReference(relativePath).String() + req.Header.Set("Accept", "application/json") - request, err := http.NewRequestWithContext(ctx, method, endpoint, payload) - if err != nil { - return nil, err + if body != nil { + req.Header.Set("Content-Type", "application/json") } - request.Header.Set("Accept", "application/json") - - if payload != nil { - request.Header.Set("Content-Type", "application/json") + if type_ != "" { + // When the type_ is provided, it means the request needs to be created to handle files + req.Header.Set("Content-Type", type_) + req.Header.Set("X-Atlassian-Token", "no-check") } if c.Auth.HasBasicAuth() { - request.SetBasicAuth(c.Auth.GetBasicAuth()) + req.SetBasicAuth(c.Auth.GetBasicAuth()) } if c.Auth.HasUserAgent() { - request.Header.Set("User-Agent", c.Auth.GetUserAgent()) + req.Header.Set("User-Agent", c.Auth.GetUserAgent()) } - return request, nil + return req, nil } func (c *Client) Call(request *http.Request, structure interface{}) (*models.ResponseScheme, error) { @@ -466,12 +457,14 @@ func (c *Client) Call(request *http.Request, structure interface{}) (*models.Res return nil, err } - return c.TransformTheHTTPResponse(response, structure) + return c.processResponse(response, structure) } -func (c *Client) TransformTheHTTPResponse(response *http.Response, structure interface{}) (*models.ResponseScheme, error) { +func (c *Client) processResponse(response *http.Response, structure interface{}) (*models.ResponseScheme, error) { + + defer response.Body.Close() - responseTransformed := &models.ResponseScheme{ + res := &models.ResponseScheme{ Response: response, Code: response.StatusCode, Endpoint: response.Request.URL.String(), @@ -480,39 +473,39 @@ func (c *Client) TransformTheHTTPResponse(response *http.Response, structure int responseAsBytes, err := io.ReadAll(response.Body) if err != nil { - return responseTransformed, err + return res, err } - responseTransformed.Bytes.Write(responseAsBytes) + res.Bytes.Write(responseAsBytes) + + wasSuccess := response.StatusCode >= 200 && response.StatusCode < 300 - var wasSuccess = response.StatusCode >= 200 && response.StatusCode < 300 if !wasSuccess { - return responseTransformed, models.ErrInvalidStatusCodeError - } - if structure != nil { - if err = json.Unmarshal(responseAsBytes, &structure); err != nil { - return responseTransformed, err - } - } + switch response.StatusCode { - return responseTransformed, nil -} + case http.StatusNotFound: + return res, models.ErrNotFound -func (c *Client) TransformStructToReader(structure interface{}) (io.Reader, error) { + case http.StatusUnauthorized: + return res, models.ErrUnauthorized - if structure == nil { - return nil, models.ErrNilPayloadError - } + case http.StatusInternalServerError: + return res, models.ErrInternalError + + case http.StatusBadRequest: + return res, models.ErrBadRequestError - if reflect.ValueOf(structure).Type().Kind() == reflect.Struct { - return nil, models.ErrNonPayloadPointerError + default: + return res, models.ErrInvalidStatusCodeError + } } - structureAsBodyBytes, err := json.Marshal(structure) - if err != nil { - return nil, err + if structure != nil { + if err = json.Unmarshal(responseAsBytes, &structure); err != nil { + return res, err + } } - return bytes.NewReader(structureAsBodyBytes), nil + return res, nil } diff --git a/jira/v3/api_client_impl_test.go b/jira/v3/api_client_impl_test.go index a88efbad..cbf2eb7b 100644 --- a/jira/v3/api_client_impl_test.go +++ b/jira/v3/api_client_impl_test.go @@ -3,16 +3,13 @@ package v3 import ( "bytes" "context" - "encoding/json" "errors" "github.com/ctreminiom/go-atlassian/jira/internal" - "github.com/ctreminiom/go-atlassian/pkg/infra/models" + model "github.com/ctreminiom/go-atlassian/pkg/infra/models" "github.com/ctreminiom/go-atlassian/service/common" - "github.com/ctreminiom/go-atlassian/service/jira" "github.com/ctreminiom/go-atlassian/service/mocks" "github.com/stretchr/testify/assert" "io" - "io/ioutil" "net/http" "net/url" "strings" @@ -23,29 +20,54 @@ func TestClient_Call(t *testing.T) { expectedResponse := &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(strings.NewReader("Hello, world!")), + Body: io.NopCloser(strings.NewReader("Hello, world!")), Request: &http.Request{ Method: http.MethodGet, URL: &url.URL{}, }, } - nonExpectedResponse := &http.Response{ + badRequestResponse := &http.Response{ StatusCode: http.StatusBadRequest, - Body: ioutil.NopCloser(strings.NewReader("Hello, world!")), + Body: io.NopCloser(strings.NewReader("Hello, world!")), Request: &http.Request{ Method: http.MethodGet, URL: &url.URL{}, }, } - type fields struct { - HTTP common.HttpClient - Site *url.URL - Authentication common.Authentication - ApplicationRole jira.AppRoleConnector + internalServerResponse := &http.Response{ + StatusCode: http.StatusInternalServerError, + Body: io.NopCloser(strings.NewReader("Hello, world!")), + Request: &http.Request{ + Method: http.MethodGet, + URL: &url.URL{}, + }, } + unauthorizedResponse := &http.Response{ + StatusCode: http.StatusUnauthorized, + Body: io.NopCloser(strings.NewReader("Hello, world!")), + Request: &http.Request{ + Method: http.MethodGet, + URL: &url.URL{}, + }, + } + + notFoundResponse := &http.Response{ + StatusCode: http.StatusNotFound, + Body: io.NopCloser(strings.NewReader("Hello, world!")), + Request: &http.Request{ + Method: http.MethodGet, + URL: &url.URL{}, + }, + } + + type fields struct { + HTTP common.HttpClient + Site *url.URL + Auth common.Authentication + } type args struct { request *http.Request structure interface{} @@ -54,9 +76,9 @@ func TestClient_Call(t *testing.T) { testCases := []struct { name string fields fields - on func(*fields) args args - want *models.ResponseScheme + on func(*fields) + want *model.ResponseScheme wantErr bool Err error }{ @@ -75,7 +97,7 @@ func TestClient_Call(t *testing.T) { request: nil, structure: nil, }, - want: &models.ResponseScheme{ + want: &model.ResponseScheme{ Response: expectedResponse, Code: http.StatusOK, Method: http.MethodGet, @@ -85,28 +107,38 @@ func TestClient_Call(t *testing.T) { }, { - name: "when the http callback cannot be executed", + name: "when the response status is a bad request", on: func(fields *fields) { client := mocks.NewHttpClient(t) client.On("Do", (*http.Request)(nil)). - Return(nil, errors.New("error, unable to execute the http call")) + Return(badRequestResponse, nil) fields.HTTP = client }, + args: args{ + request: nil, + structure: nil, + }, + want: &model.ResponseScheme{ + Response: badRequestResponse, + Code: http.StatusBadRequest, + Method: http.MethodGet, + Bytes: *bytes.NewBufferString("Hello, world!"), + }, wantErr: true, - Err: errors.New("error, unable to execute the http call"), + Err: model.ErrBadRequestError, }, { - name: "when the response status is not valid", + name: "when the response status is an internal service error", on: func(fields *fields) { client := mocks.NewHttpClient(t) client.On("Do", (*http.Request)(nil)). - Return(nonExpectedResponse, nil) + Return(internalServerResponse, nil) fields.HTTP = client }, @@ -114,19 +146,67 @@ func TestClient_Call(t *testing.T) { request: nil, structure: nil, }, - want: &models.ResponseScheme{ - Response: nonExpectedResponse, - Code: http.StatusBadRequest, + want: &model.ResponseScheme{ + Response: internalServerResponse, + Code: http.StatusInternalServerError, Method: http.MethodGet, Bytes: *bytes.NewBufferString("Hello, world!"), }, wantErr: true, - Err: models.ErrInvalidStatusCodeError, + Err: model.ErrInternalError, }, - } - for _, testCase := range testCases { + { + name: "when the response status is a not found", + on: func(fields *fields) { + client := mocks.NewHttpClient(t) + + client.On("Do", (*http.Request)(nil)). + Return(notFoundResponse, nil) + + fields.HTTP = client + }, + args: args{ + request: nil, + structure: nil, + }, + want: &model.ResponseScheme{ + Response: notFoundResponse, + Code: http.StatusNotFound, + Method: http.MethodGet, + Bytes: *bytes.NewBufferString("Hello, world!"), + }, + wantErr: true, + Err: model.ErrNotFound, + }, + + { + name: "when the response status is unauthorized", + on: func(fields *fields) { + + client := mocks.NewHttpClient(t) + + client.On("Do", (*http.Request)(nil)). + Return(unauthorizedResponse, nil) + + fields.HTTP = client + }, + args: args{ + request: nil, + structure: nil, + }, + want: &model.ResponseScheme{ + Response: unauthorizedResponse, + Code: http.StatusUnauthorized, + Method: http.MethodGet, + Bytes: *bytes.NewBufferString("Hello, world!"), + }, + wantErr: true, + Err: model.ErrUnauthorized, + }, + } + for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { if testCase.on != nil { @@ -136,7 +216,6 @@ func TestClient_Call(t *testing.T) { c := &Client{ HTTP: testCase.fields.HTTP, Site: testCase.fields.Site, - Auth: testCase.fields.Authentication, } got, err := c.Call(testCase.args.request, testCase.args.structure) @@ -153,61 +232,125 @@ func TestClient_Call(t *testing.T) { assert.NoError(t, err) assert.Equal(t, got, testCase.want) } + }) } } -func TestNew(t *testing.T) { +func TestClient_NewRequest(t *testing.T) { - mockClient, err := New(http.DefaultClient, "https://ctreminiom.atlassian.net") + authMocked := internal.NewAuthenticationService(nil) + authMocked.SetBasicAuth("mail", "token") + authMocked.SetUserAgent("firefox") + + siteAsURL, err := url.Parse("https://ctreminiom.atlassian.net") if err != nil { t.Fatal(err) } - mockClient.Auth.SetBasicAuth("test", "test") - mockClient.Auth.SetUserAgent("aaa") + requestMocked, err := http.NewRequestWithContext(context.TODO(), + http.MethodGet, + "https://ctreminiom.atlassian.net/rest/2/issue/attachment", + bytes.NewReader([]byte("Hello World")), + ) + + if err != nil { + t.Fatal(err) + } - mockClient2, _ := New(nil, " https://zhidao.baidu.com/special/view?id=sd&preview=1") + requestMocked.Header.Set("Accept", "application/json") + requestMocked.Header.Set("Content-Type", "application/json") + + type fields struct { + HTTP common.HttpClient + Auth common.Authentication + Site *url.URL + } type args struct { - httpClient common.HttpClient - site string + ctx context.Context + method string + urlStr string + type_ string + body interface{} } testCases := []struct { name string + fields fields args args - on func(*args) - want *Client + want *http.Request wantErr bool - Err error }{ { name: "when the parameters are correct", + fields: fields{ + HTTP: http.DefaultClient, + Auth: authMocked, + Site: siteAsURL, + }, args: args{ - httpClient: http.DefaultClient, - site: "https://ctreminiom.atlassian.net", + ctx: context.TODO(), + method: http.MethodGet, + urlStr: "rest/2/issue/attachment", + type_: "", + body: bytes.NewReader([]byte("Hello World")), }, - want: mockClient, + want: requestMocked, wantErr: false, }, { - name: "when the site url is not valid", + name: "when the url cannot be parsed", + fields: fields{ + HTTP: http.DefaultClient, + Auth: internal.NewAuthenticationService(nil), + Site: siteAsURL, + }, args: args{ - httpClient: http.DefaultClient, - site: " https://zhidao.baidu.com/special/view?id=sd&preview=1", + ctx: context.TODO(), + method: http.MethodGet, + urlStr: " https://zhidao.baidu.com/special/view?id=49105a24626975510000&preview=1", + body: bytes.NewReader([]byte("Hello World")), }, - want: mockClient2, + want: nil, + wantErr: true, + }, + + { + name: "when the request cannot be created", + fields: fields{ + HTTP: http.DefaultClient, + Auth: internal.NewAuthenticationService(nil), + Site: siteAsURL, + }, + args: args{ + ctx: nil, + method: http.MethodGet, + urlStr: "rest/2/issue/attachment", + body: bytes.NewReader([]byte("Hello World")), + }, + want: requestMocked, wantErr: true, - Err: errors.New("parse \" https://zhidao.baidu.com/special/view?id=sd&preview=1/\": first path segment in URL cannot contain colon"), }, } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - gotClient, err := New(testCase.args.httpClient, testCase.args.site) + c := &Client{ + HTTP: testCase.fields.HTTP, + Auth: testCase.fields.Auth, + Site: testCase.fields.Site, + } + + got, err := c.NewRequest( + testCase.args.ctx, + testCase.args.method, + testCase.args.urlStr, + testCase.args.type_, + testCase.args.body, + ) if testCase.wantErr { @@ -216,18 +359,16 @@ func TestNew(t *testing.T) { } assert.Error(t, err) - assert.EqualError(t, err, testCase.Err.Error()) - } else { assert.NoError(t, err) - assert.NotEqual(t, gotClient, nil) + assert.NotEqual(t, got, nil) } }) } } -func TestClient_TransformTheHTTPResponse(t *testing.T) { +func TestClient_processResponse(t *testing.T) { expectedJsonResponse := ` { @@ -239,7 +380,7 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { expectedResponse := &http.Response{ StatusCode: http.StatusOK, - Body: ioutil.NopCloser(strings.NewReader(expectedJsonResponse)), + Body: io.NopCloser(strings.NewReader(expectedJsonResponse)), Request: &http.Request{ Method: http.MethodGet, URL: &url.URL{}, @@ -247,12 +388,10 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { } type fields struct { - HTTP common.HttpClient - Site *url.URL - Authentication common.Authentication - ApplicationRole jira.AppRoleConnector + HTTP common.HttpClient + Site *url.URL + Authentication common.Authentication } - type args struct { response *http.Response structure interface{} @@ -262,7 +401,7 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { name string fields fields args args - want *models.ResponseScheme + want *model.ResponseScheme wantErr bool Err error }{ @@ -271,9 +410,9 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { fields: fields{}, args: args{ response: expectedResponse, - structure: models.BoardScheme{}, + structure: model.BoardScheme{}, }, - want: &models.ResponseScheme{ + want: &model.ResponseScheme{ Response: expectedResponse, Code: http.StatusOK, Method: http.MethodGet, @@ -282,110 +421,16 @@ func TestClient_TransformTheHTTPResponse(t *testing.T) { wantErr: false, }, } - for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - c := &Client{ - HTTP: testCase.fields.HTTP, - Site: testCase.fields.Site, - Auth: testCase.fields.Authentication, - } - - got, err := c.TransformTheHTTPResponse(testCase.args.response, testCase.args.structure) - if testCase.wantErr { - - if err != nil { - t.Logf("error returned: %v", err.Error()) - } - - assert.Error(t, err) - assert.EqualError(t, err, testCase.Err.Error()) - - } else { - assert.NoError(t, err) - assert.NotEqual(t, got, nil) - } - }) - } -} - -func TestClient_TransformStructToReader(t *testing.T) { - - expectedBytes, err := json.Marshal(&models.BoardScheme{ - Name: "Board Sample", - Type: "Scrum", - }) - - if err != nil { - t.Fatal(err) - } - - type fields struct { - HTTP common.HttpClient - Site *url.URL - Authentication common.Authentication - ApplicationRole jira.AppRoleConnector - } - - type args struct { - structure interface{} - } - - testCases := []struct { - name string - fields fields - args args - want io.Reader - wantErr bool - Err error - }{ - { - name: "when the parameters are correct", - args: args{ - structure: &models.BoardScheme{ - Name: "Board Sample", - Type: "Scrum", - }, - }, - want: bytes.NewReader(expectedBytes), - wantErr: false, - }, - - { - name: "when the payload provided is not a pointer", - args: args{ - structure: models.BoardScheme{ - Name: "Board Sample", - Type: "Scrum", - }, - }, - want: bytes.NewReader(expectedBytes), - wantErr: true, - Err: models.ErrNonPayloadPointerError, - }, - - { - name: "when the payload is not provided", - args: args{ - structure: nil, - }, - want: bytes.NewReader(expectedBytes), - wantErr: true, - Err: models.ErrNilPayloadError, - }, - } - - for _, testCase := range testCases { - - t.Run(testCase.name, func(t *testing.T) { c := &Client{ HTTP: testCase.fields.HTTP, Site: testCase.fields.Site, Auth: testCase.fields.Authentication, } - got, err := c.TransformStructToReader(testCase.args.structure) + got, err := c.processResponse(testCase.args.response, testCase.args.structure) if testCase.wantErr { @@ -400,239 +445,74 @@ func TestClient_TransformStructToReader(t *testing.T) { assert.NoError(t, err) assert.NotEqual(t, got, nil) } - }) - } -} - -func TestClient_NewFormRequest(t *testing.T) { - - authMocked := internal.NewAuthenticationService(nil) - authMocked.SetBasicAuth("mail", "token") - authMocked.SetUserAgent("firefox") - - siteAsURL, err := url.Parse("https://ctreminiom.atlassian.net") - if err != nil { - t.Fatal(err) - } - - requestMocked, err := http.NewRequestWithContext(context.TODO(), - http.MethodGet, - "https://ctreminiom.atlassian.net/rest/2/issue/attachment", - bytes.NewReader([]byte("Hello World")), - ) - - if err != nil { - t.Fatal(err) - } - - requestMocked.Header.Add("Content-Type", "form-type-sample") - requestMocked.Header.Add("Accept", "application/json") - requestMocked.Header.Set("X-Atlassian-Token", "no-check") - - type fields struct { - HTTP common.HttpClient - Auth common.Authentication - Site *url.URL - } - - type args struct { - ctx context.Context - method string - apiEndpoint string - contentType string - payload io.Reader - } - - testCases := []struct { - name string - fields fields - args args - want *http.Request - wantErr bool - }{ - { - name: "when the parameters are correct", - fields: fields{ - HTTP: http.DefaultClient, - Auth: authMocked, - Site: siteAsURL, - }, - args: args{ - ctx: context.TODO(), - method: http.MethodGet, - apiEndpoint: "rest/2/issue/attachment", - contentType: "form-type-sample", - payload: bytes.NewReader([]byte("Hello World")), - }, - want: requestMocked, - wantErr: false, - }, - - { - name: "when the url cannot be parsed", - fields: fields{ - HTTP: http.DefaultClient, - Auth: internal.NewAuthenticationService(nil), - Site: siteAsURL, - }, - args: args{ - ctx: context.TODO(), - method: http.MethodGet, - apiEndpoint: " https://zhidao.baidu.com/special/view?id=49105a24626975510000&preview=1", - contentType: "form-type-sample", - payload: bytes.NewReader([]byte("Hello World")), - }, - want: nil, - wantErr: true, - }, - - { - name: "when the request cannot be created", - fields: fields{ - HTTP: http.DefaultClient, - Auth: internal.NewAuthenticationService(nil), - Site: siteAsURL, - }, - args: args{ - ctx: nil, - method: http.MethodGet, - apiEndpoint: "rest/2/issue/attachment", - contentType: "form-type-sample", - payload: bytes.NewReader([]byte("Hello World")), - }, - want: requestMocked, - wantErr: true, - }, - } - for _, testCase := range testCases { - t.Run(testCase.name, func(t *testing.T) { - c := &Client{ - HTTP: testCase.fields.HTTP, - Auth: testCase.fields.Auth, - Site: testCase.fields.Site, - } - got, err := c.NewFormRequest(testCase.args.ctx, testCase.args.method, testCase.args.apiEndpoint, testCase.args.contentType, testCase.args.payload) - - if testCase.wantErr { - - if err != nil { - t.Logf("error returned: %v", err.Error()) - } - - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.NotEqual(t, got, nil) - } }) } } -func TestClient_NewRequest(t *testing.T) { - - authMocked := internal.NewAuthenticationService(nil) - authMocked.SetBasicAuth("mail", "token") - authMocked.SetUserAgent("firefox") +func TestNew(t *testing.T) { - siteAsURL, err := url.Parse("https://ctreminiom.atlassian.net") + mockClient, err := New(http.DefaultClient, "https://ctreminiom.atlassian.net") if err != nil { t.Fatal(err) } - requestMocked, err := http.NewRequestWithContext(context.TODO(), - http.MethodGet, - "https://ctreminiom.atlassian.net/rest/2/issue/attachment", - bytes.NewReader([]byte("Hello World")), - ) - - if err != nil { - t.Fatal(err) - } + mockClient.Auth.SetBasicAuth("test", "test") + mockClient.Auth.SetUserAgent("aaa") - requestMocked.Header.Set("Accept", "application/json") - requestMocked.Header.Set("Content-Type", "application/json") + invalidURLClientMocked, _ := New(nil, " https://zhidao.baidu.com/special/view?id=sd&preview=1") - type fields struct { - HTTP common.HttpClient - Auth common.Authentication - Site *url.URL - } + noURLClientMocked, _ := New(nil, "") type args struct { - ctx context.Context - method string - apiEndpoint string - payload io.Reader + httpClient common.HttpClient + site string } testCases := []struct { name string - fields fields args args - want *http.Request + want *Client wantErr bool + Err error }{ + { name: "when the parameters are correct", - fields: fields{ - HTTP: http.DefaultClient, - Auth: authMocked, - Site: siteAsURL, - }, args: args{ - ctx: context.TODO(), - method: http.MethodGet, - apiEndpoint: "rest/2/issue/attachment", - payload: bytes.NewReader([]byte("Hello World")), + httpClient: http.DefaultClient, + site: "https://ctreminiom.atlassian.net", }, - want: requestMocked, + want: mockClient, wantErr: false, }, { - name: "when the url cannot be parsed", - fields: fields{ - HTTP: http.DefaultClient, - Auth: internal.NewAuthenticationService(nil), - Site: siteAsURL, - }, + name: "when the site url are not provided", args: args{ - ctx: context.TODO(), - method: http.MethodGet, - apiEndpoint: " https://zhidao.baidu.com/special/view?id=49105a24626975510000&preview=1", - payload: bytes.NewReader([]byte("Hello World")), + httpClient: http.DefaultClient, + site: "", }, - want: nil, + want: noURLClientMocked, wantErr: true, + Err: model.ErrNoSiteError, }, - { - name: "when the request cannot be created", - fields: fields{ - HTTP: http.DefaultClient, - Auth: internal.NewAuthenticationService(nil), - Site: siteAsURL, - }, + name: "when the site url is not valid", args: args{ - ctx: nil, - method: http.MethodGet, - apiEndpoint: "rest/2/issue/attachment", - payload: bytes.NewReader([]byte("Hello World")), + httpClient: http.DefaultClient, + site: " https://zhidao.baidu.com/special/view?id=sd&preview=1", }, - want: requestMocked, + want: invalidURLClientMocked, wantErr: true, + Err: errors.New("parse \" https://zhidao.baidu.com/special/view?id=sd&preview=1/\": first path segment in URL cannot contain colon"), }, } + for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - c := &Client{ - HTTP: testCase.fields.HTTP, - Auth: testCase.fields.Auth, - Site: testCase.fields.Site, - } - got, err := c.NewRequest(testCase.args.ctx, testCase.args.method, testCase.args.apiEndpoint, testCase.args.payload) + gotClient, err := New(testCase.args.httpClient, testCase.args.site) if testCase.wantErr { @@ -641,9 +521,11 @@ func TestClient_NewRequest(t *testing.T) { } assert.Error(t, err) + assert.EqualError(t, err, testCase.Err.Error()) + } else { assert.NoError(t, err) - assert.NotEqual(t, got, nil) + assert.NotEqual(t, gotClient, nil) } }) } From 4fda1b53a3d2d90795ac1ddddc76ee9c6f968cab Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 22:04:56 -0600 Subject: [PATCH 67/70] :rotating_light: Fixed the linter problems --- jira/internal/issue_impl_rich_text_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/jira/internal/issue_impl_rich_text_test.go b/jira/internal/issue_impl_rich_text_test.go index fbb58441..843328ba 100644 --- a/jira/internal/issue_impl_rich_text_test.go +++ b/jira/internal/issue_impl_rich_text_test.go @@ -1021,6 +1021,9 @@ func Test_internalRichTextServiceImpl_Move(t *testing.T) { err = operationsMocked.AddArrayOperation("labels", map[string]string{ "triaged": "remove", }) + if err != nil { + t.Fatal(err) + } expectedPayloadWithCustomFieldsAndOperations := map[string]interface{}{ From 929d8c0cc114a342d1efd0f3c8da7a72c5a67cac Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 22:34:03 -0600 Subject: [PATCH 68/70] :bug: Fixed the Issue.Attachment.Add() return struct. --- jira/internal/attachment_impl.go | 6 +++--- jira/internal/attachment_impl_test.go | 4 ++-- service/jira/attachment.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/jira/internal/attachment_impl.go b/jira/internal/attachment_impl.go index bbe3acd1..2ce7fcee 100644 --- a/jira/internal/attachment_impl.go +++ b/jira/internal/attachment_impl.go @@ -74,7 +74,7 @@ func (i *IssueAttachmentService) Human(ctx context.Context, attachmentId string) // POST /rest/api/{2-3}/issue/{issueIdOrKey}/attachments // // https://docs.go-atlassian.io/jira-software-cloud/issues/attachments#add-attachment -func (i *IssueAttachmentService) Add(ctx context.Context, issueKeyOrId, fileName string, file io.Reader) ([]*model.AttachmentScheme, *model.ResponseScheme, error) { +func (i *IssueAttachmentService) Add(ctx context.Context, issueKeyOrId, fileName string, file io.Reader) ([]*model.IssueAttachmentScheme, *model.ResponseScheme, error) { return i.internalClient.Add(ctx, issueKeyOrId, fileName, file) } @@ -197,7 +197,7 @@ func (i *internalIssueAttachmentServiceImpl) Human(ctx context.Context, attachme return metadata, response, nil } -func (i *internalIssueAttachmentServiceImpl) Add(ctx context.Context, issueKeyOrId, fileName string, file io.Reader) ([]*model.AttachmentScheme, *model.ResponseScheme, error) { +func (i *internalIssueAttachmentServiceImpl) Add(ctx context.Context, issueKeyOrId, fileName string, file io.Reader) ([]*model.IssueAttachmentScheme, *model.ResponseScheme, error) { if issueKeyOrId == "" { return nil, nil, model.ErrNoIssueKeyOrIDError @@ -233,7 +233,7 @@ func (i *internalIssueAttachmentServiceImpl) Add(ctx context.Context, issueKeyOr return nil, nil, err } - var attachments []*model.AttachmentScheme + var attachments []*model.IssueAttachmentScheme response, err := i.c.Call(request, attachments) if err != nil { return nil, response, err diff --git a/jira/internal/attachment_impl_test.go b/jira/internal/attachment_impl_test.go index 4f7784cc..f4add1f2 100644 --- a/jira/internal/attachment_impl_test.go +++ b/jira/internal/attachment_impl_test.go @@ -647,7 +647,7 @@ func Test_internalIssueAttachmentServiceImpl_Add(t *testing.T) { client.On("Call", &http.Request{}, - []*model.AttachmentScheme(nil)). + []*model.IssueAttachmentScheme(nil)). Return(&model.ResponseScheme{}, nil) fields.c = client @@ -677,7 +677,7 @@ func Test_internalIssueAttachmentServiceImpl_Add(t *testing.T) { client.On("Call", &http.Request{}, - []*model.AttachmentScheme(nil)). + []*model.IssueAttachmentScheme(nil)). Return(&model.ResponseScheme{}, nil) fields.c = client diff --git a/service/jira/attachment.go b/service/jira/attachment.go index f5e83db4..3b3afef2 100644 --- a/service/jira/attachment.go +++ b/service/jira/attachment.go @@ -45,7 +45,7 @@ type AttachmentConnector interface { // POST /rest/api/{2-3}/issue/{issueIdOrKey}/attachments // // https://docs.go-atlassian.io/jira-software-cloud/issues/attachments#add-attachment - Add(ctx context.Context, issueKeyOrId, fileName string, file io.Reader) ([]*model.AttachmentScheme, *model.ResponseScheme, error) + Add(ctx context.Context, issueKeyOrId, fileName string, file io.Reader) ([]*model.IssueAttachmentScheme, *model.ResponseScheme, error) // Download returns the contents of an attachment. A Range header can be set to define a range of bytes within the attachment to download. // From 415ac905446bafbd56abb64609dd8f26b59d68ca Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 23:39:47 -0600 Subject: [PATCH 69/70] :bug: Edited the api_client_impl.go --- jira/v2/api_client_impl.go | 6 ++++++ jira/v2/api_client_impl_test.go | 18 ++++++++++++++++++ jira/v3/api_client_impl.go | 6 ++++++ jira/v3/api_client_impl_test.go | 18 ++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/jira/v2/api_client_impl.go b/jira/v2/api_client_impl.go index e69386c4..b81f3d82 100644 --- a/jira/v2/api_client_impl.go +++ b/jira/v2/api_client_impl.go @@ -422,6 +422,12 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr, type_ string, b } } + // If the body interface is a *bytes.Buffer type + // it means the NewRequest() requires to handle the RFC 1867 ISO + if attachBuffer, ok := body.(*bytes.Buffer); ok { + buf = attachBuffer + } + req, err := http.NewRequestWithContext(ctx, method, u.String(), buf) if err != nil { return nil, err diff --git a/jira/v2/api_client_impl_test.go b/jira/v2/api_client_impl_test.go index 7db1d1d8..031736ff 100644 --- a/jira/v2/api_client_impl_test.go +++ b/jira/v2/api_client_impl_test.go @@ -300,6 +300,24 @@ func TestClient_NewRequest(t *testing.T) { wantErr: false, }, + { + name: "when the content type is provided", + fields: fields{ + HTTP: http.DefaultClient, + Auth: authMocked, + Site: siteAsURL, + }, + args: args{ + ctx: context.TODO(), + method: http.MethodGet, + urlStr: "rest/2/issue/attachment", + type_: "type_sample", + body: bytes.NewReader([]byte("Hello World")), + }, + want: requestMocked, + wantErr: false, + }, + { name: "when the url cannot be parsed", fields: fields{ diff --git a/jira/v3/api_client_impl.go b/jira/v3/api_client_impl.go index fb23984c..fb670bb5 100644 --- a/jira/v3/api_client_impl.go +++ b/jira/v3/api_client_impl.go @@ -422,6 +422,12 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr, type_ string, b } } + // If the body interface is a *bytes.Buffer type + // it means the NewRequest() requires to handle the RFC 1867 ISO + if attachBuffer, ok := body.(*bytes.Buffer); ok { + buf = attachBuffer + } + req, err := http.NewRequestWithContext(ctx, method, u.String(), buf) if err != nil { return nil, err diff --git a/jira/v3/api_client_impl_test.go b/jira/v3/api_client_impl_test.go index cbf2eb7b..8201cf9f 100644 --- a/jira/v3/api_client_impl_test.go +++ b/jira/v3/api_client_impl_test.go @@ -317,6 +317,24 @@ func TestClient_NewRequest(t *testing.T) { wantErr: true, }, + { + name: "when the content type is provided", + fields: fields{ + HTTP: http.DefaultClient, + Auth: authMocked, + Site: siteAsURL, + }, + args: args{ + ctx: context.TODO(), + method: http.MethodGet, + urlStr: "rest/2/issue/attachment", + type_: "type_sample", + body: bytes.NewReader([]byte("Hello World")), + }, + want: requestMocked, + wantErr: false, + }, + { name: "when the request cannot be created", fields: fields{ From 5d47859d81c7d9bd46e3b3549921d04113059c44 Mon Sep 17 00:00:00 2001 From: Carlos Treminio Date: Sun, 23 Jul 2023 23:44:11 -0600 Subject: [PATCH 70/70] :bug: Edited the api_client_impl.go --- jira/sm/api_client_impl.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jira/sm/api_client_impl.go b/jira/sm/api_client_impl.go index 19dc4144..fe5942a7 100644 --- a/jira/sm/api_client_impl.go +++ b/jira/sm/api_client_impl.go @@ -102,17 +102,25 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr, type_ string, b } } + // If the body interface is a *bytes.Buffer type + // it means the NewRequest() requires to handle the RFC 1867 ISO + if attachBuffer, ok := body.(*bytes.Buffer); ok { + buf = attachBuffer + } + req, err := http.NewRequestWithContext(ctx, method, u.String(), buf) if err != nil { return nil, err } + req.Header.Set("Accept", "application/json") - if body != nil && type_ == "" { + if body != nil { req.Header.Set("Content-Type", "application/json") } - if body != nil && type_ != "" { + if type_ != "" { + // When the type_ is provided, it means the request needs to be created to handle files req.Header.Set("Content-Type", type_) req.Header.Set("X-Atlassian-Token", "no-check") }