Skip to content

Commit e923fd5

Browse files
refactor: Modifies mocking of search deployment unit test directly to SDK removing intermediate service (#2028)
* refactor: Modifies mocking of search deployment unit test directly to SDK removing intermediate service * adjust mockery file during update sdk script * remove redundant assertions * pass AtlasSearchApi instead of APIClient
1 parent 09357b9 commit e923fd5

11 files changed

+1969
-120
lines changed

.mockery.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
with-expecter: false
1+
with-expecter: true
22
disable-version-string: true
33
dir: internal/testutil/mocksvc
44
outpkg: mocksvc
55
filename: "{{ .InterfaceName | snakecase }}.go"
66
mockname: "{{.InterfaceName}}"
77

88
packages:
9-
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment:
9+
go.mongodb.org/atlas-sdk/v20231115007/admin:
1010
interfaces:
11-
DeploymentService:
11+
AtlasSearchApi:
1212

1313
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/encryptionatrest:
1414
interfaces:

internal/service/searchdeployment/resource_search_deployment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (r *searchDeploymentRS) Create(ctx context.Context, req resource.CreateRequ
6666
if resp.Diagnostics.HasError() {
6767
return
6868
}
69-
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, ServiceFromClient(connV2),
69+
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, connV2.AtlasSearchApi,
7070
retryTimeConfig(createTimeout, minTimeoutCreateUpdate))
7171
if err != nil {
7272
resp.Diagnostics.AddError("error during search deployment creation", err.Error())
@@ -125,7 +125,7 @@ func (r *searchDeploymentRS) Update(ctx context.Context, req resource.UpdateRequ
125125
if resp.Diagnostics.HasError() {
126126
return
127127
}
128-
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, ServiceFromClient(connV2),
128+
deploymentResp, err := WaitSearchNodeStateTransition(ctx, projectID, clusterName, connV2.AtlasSearchApi,
129129
retryTimeConfig(updateTimeout, minTimeoutCreateUpdate))
130130
if err != nil {
131131
resp.Diagnostics.AddError("error during search deployment update", err.Error())
@@ -159,7 +159,7 @@ func (r *searchDeploymentRS) Delete(ctx context.Context, req resource.DeleteRequ
159159
if resp.Diagnostics.HasError() {
160160
return
161161
}
162-
if err := WaitSearchNodeDelete(ctx, projectID, clusterName, ServiceFromClient(connV2), retryTimeConfig(deleteTimeout, minTimeoutDelete)); err != nil {
162+
if err := WaitSearchNodeDelete(ctx, projectID, clusterName, connV2.AtlasSearchApi, retryTimeConfig(deleteTimeout, minTimeoutDelete)); err != nil {
163163
resp.Diagnostics.AddError("error during search deployment delete", err.Error())
164164
return
165165
}

internal/service/searchdeployment/service_search_deployment.go

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

internal/service/searchdeployment/state_transition_search_deployment.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
const SearchDeploymentDoesNotExistsError = "ATLAS_FTS_DEPLOYMENT_DOES_NOT_EXIST"
1717

18-
func WaitSearchNodeStateTransition(ctx context.Context, projectID, clusterName string, client DeploymentService,
18+
func WaitSearchNodeStateTransition(ctx context.Context, projectID, clusterName string, client admin.AtlasSearchApi,
1919
timeConfig retrystrategy.TimeConfig) (*admin.ApiSearchDeploymentResponse, error) {
2020
stateConf := &retry.StateChangeConf{
2121
Pending: []string{retrystrategy.RetryStrategyUpdatingState, retrystrategy.RetryStrategyPausedState},
@@ -36,7 +36,7 @@ func WaitSearchNodeStateTransition(ctx context.Context, projectID, clusterName s
3636
return nil, errors.New("did not obtain valid result when waiting for search deployment state transition")
3737
}
3838

39-
func WaitSearchNodeDelete(ctx context.Context, projectID, clusterName string, client DeploymentService, timeConfig retrystrategy.TimeConfig) error {
39+
func WaitSearchNodeDelete(ctx context.Context, projectID, clusterName string, client admin.AtlasSearchApi, timeConfig retrystrategy.TimeConfig) error {
4040
stateConf := &retry.StateChangeConf{
4141
Pending: []string{retrystrategy.RetryStrategyIdleState, retrystrategy.RetryStrategyUpdatingState, retrystrategy.RetryStrategyPausedState},
4242
Target: []string{retrystrategy.RetryStrategyDeletedState},
@@ -49,9 +49,9 @@ func WaitSearchNodeDelete(ctx context.Context, projectID, clusterName string, cl
4949
return err
5050
}
5151

52-
func searchDeploymentRefreshFunc(ctx context.Context, projectID, clusterName string, client DeploymentService) retry.StateRefreshFunc {
52+
func searchDeploymentRefreshFunc(ctx context.Context, projectID, clusterName string, client admin.AtlasSearchApi) retry.StateRefreshFunc {
5353
return func() (any, string, error) {
54-
deploymentResp, resp, err := client.GetAtlasSearchDeployment(ctx, projectID, clusterName)
54+
deploymentResp, resp, err := client.GetAtlasSearchDeployment(ctx, projectID, clusterName).Execute()
5555
if err != nil && deploymentResp == nil && resp == nil {
5656
return nil, "", err
5757
}

internal/service/searchdeployment/state_transition_search_deployment_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment"
1313
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mocksvc"
1414
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/mock"
1516
"go.mongodb.org/atlas-sdk/v20231115007/admin"
1617
)
1718

@@ -73,15 +74,16 @@ func TestSearchDeploymentStateTransition(t *testing.T) {
7374

7475
for _, tc := range testCases {
7576
t.Run(tc.name, func(t *testing.T) {
76-
svc := mocksvc.NewDeploymentService(t)
77-
ctx := context.Background()
77+
m := mocksvc.NewAtlasSearchApi(t)
78+
m.EXPECT().GetAtlasSearchDeployment(mock.Anything, mock.Anything, mock.Anything).Return(admin.GetAtlasSearchDeploymentApiRequest{ApiService: m})
79+
7880
for _, resp := range tc.mockResponses {
79-
svc.On("GetAtlasSearchDeployment", ctx, dummyProjectID, clusterName).Return(resp.get()...).Once()
81+
modelResp, httpResp, err := resp.get()
82+
m.EXPECT().GetAtlasSearchDeploymentExecute(mock.Anything).Return(modelResp, httpResp, err).Once()
8083
}
81-
resp, err := searchdeployment.WaitSearchNodeStateTransition(ctx, dummyProjectID, "Cluster0", svc, testTimeoutConfig)
84+
resp, err := searchdeployment.WaitSearchNodeStateTransition(context.Background(), dummyProjectID, "Cluster0", m, testTimeoutConfig)
8285
assert.Equal(t, tc.expectedError, err != nil)
8386
assert.Equal(t, responseWithState(tc.expectedState), resp)
84-
svc.AssertExpectations(t)
8587
})
8688
}
8789
}
@@ -115,14 +117,15 @@ func TestSearchDeploymentStateTransitionForDelete(t *testing.T) {
115117

116118
for _, tc := range testCases {
117119
t.Run(tc.name, func(t *testing.T) {
118-
svc := mocksvc.NewDeploymentService(t)
119-
ctx := context.Background()
120+
m := mocksvc.NewAtlasSearchApi(t)
121+
m.EXPECT().GetAtlasSearchDeployment(mock.Anything, mock.Anything, mock.Anything).Return(admin.GetAtlasSearchDeploymentApiRequest{ApiService: m})
122+
120123
for _, resp := range tc.mockResponses {
121-
svc.On("GetAtlasSearchDeployment", ctx, dummyProjectID, clusterName).Return(resp.get()...).Once()
124+
modelResp, httpResp, err := resp.get()
125+
m.EXPECT().GetAtlasSearchDeploymentExecute(mock.Anything).Return(modelResp, httpResp, err).Once()
122126
}
123-
err := searchdeployment.WaitSearchNodeDelete(ctx, dummyProjectID, clusterName, svc, testTimeoutConfig)
127+
err := searchdeployment.WaitSearchNodeDelete(context.Background(), dummyProjectID, clusterName, m, testTimeoutConfig)
124128
assert.Equal(t, tc.expectedError, err != nil)
125-
svc.AssertExpectations(t)
126129
})
127130
}
128131
}
@@ -156,10 +159,10 @@ type response struct {
156159
err error
157160
}
158161

159-
func (r *response) get() []interface{} {
162+
func (r *response) get() (*admin.ApiSearchDeploymentResponse, *http.Response, error) {
160163
var httpResp *http.Response
161164
if r.statusCode != nil {
162165
httpResp = &http.Response{StatusCode: *r.statusCode}
163166
}
164-
return []interface{}{responseWithState(r.state), httpResp, r.err}
167+
return responseWithState(r.state), httpResp, r.err
165168
}

0 commit comments

Comments
 (0)