Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
#1 reduce code duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
gessnerfl committed Apr 19, 2019
1 parent 60f4b93 commit cef1034
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions instana/restapi/resources/application-configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/google/go-cmp/cmp"
)

const testApplicationConfigId = "test-application-config-id"

func TestSuccessfulGetOneApplicationConfig(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down Expand Up @@ -42,7 +44,7 @@ func TestFailedGetOneApplicationConfigWhenRestClientReturnsError(t *testing.T) {

client := mocks.NewMockRestClient(ctrl)
sut := NewApplicationConfigResource(client)
applicationConfigID := "test-application-config-id"
applicationConfigID := testApplicationConfigId

client.EXPECT().GetOne(gomock.Eq(applicationConfigID), gomock.Eq(restapi.ApplicationConfigsResourcePath)).Return(nil, errors.New("error during test"))

Expand All @@ -59,7 +61,7 @@ func TestFailedGetOneApplicationConfigWhenResponseContainsInvalidJsonArray(t *te

client := mocks.NewMockRestClient(ctrl)
sut := NewApplicationConfigResource(client)
applicationConfigID := "test-application-config-id"
applicationConfigID := testApplicationConfigId

client.EXPECT().GetOne(gomock.Eq(applicationConfigID), gomock.Eq(restapi.ApplicationConfigsResourcePath)).Return([]byte("[{ \"invalid\" : \"data\" }]"), nil)

Expand All @@ -76,7 +78,7 @@ func TestFailedGetOneApplicationConfigWhenResponseContainsInvalidJsonObject(t *t

client := mocks.NewMockRestClient(ctrl)
sut := NewApplicationConfigResource(client)
applicationConfigID := "test-application-config-id"
applicationConfigID := testApplicationConfigId

client.EXPECT().GetOne(gomock.Eq(applicationConfigID), gomock.Eq(restapi.ApplicationConfigsResourcePath)).Return([]byte("{ \"invalid\" : \"data\" }"), nil)

Expand All @@ -93,7 +95,7 @@ func TestFailedGetOneApplicationConfigWhenResponseContainsNoJsonAsResponse(t *te

client := mocks.NewMockRestClient(ctrl)
sut := NewApplicationConfigResource(client)
applicationConfigID := "test-application-config-id"
applicationConfigID := testApplicationConfigId

client.EXPECT().GetOne(gomock.Eq(applicationConfigID), gomock.Eq(restapi.ApplicationConfigsResourcePath)).Return([]byte("Invalid Data"), nil)

Expand Down Expand Up @@ -173,34 +175,11 @@ func testFailGetOneApplicationConfigWhenOneSideOfBinaryExpressionIsNotValue(left
}

func TestSuccessfulUpsertOfApplicationConfig(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

client := mocks.NewMockRestClient(ctrl)
sut := NewApplicationConfigResource(client)
applicationConfig := makeTestApplicationConfig()
serializedJSON, _ := json.Marshal(applicationConfig)

client.EXPECT().Put(gomock.Eq(applicationConfig), gomock.Eq(restapi.ApplicationConfigsResourcePath)).Return(serializedJSON, nil)

result, err := sut.Upsert(applicationConfig)

if err != nil {
t.Fatalf(testutils.ExpectedNoErrorButGotMessage, err)
}

if !cmp.Equal(applicationConfig, result) {
t.Fatalf(testutils.ExpectedUnmarshalledJSONWithStruct, applicationConfig, result, cmp.Diff(applicationConfig, result))
}
testSuccessfulUpsertOfApplicationConfig(applicationConfig, t)
}

func TestSuccessfulUpsertOfComplexApplicationConfig(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

client := mocks.NewMockRestClient(ctrl)
sut := NewApplicationConfigResource(client)

applicationConfig := restapi.ApplicationConfig{
ID: "id",
Label: "label",
Expand All @@ -215,6 +194,16 @@ func TestSuccessfulUpsertOfComplexApplicationConfig(t *testing.T) {
),
Scope: "scope",
}
testSuccessfulUpsertOfApplicationConfig(applicationConfig, t)
}

func testSuccessfulUpsertOfApplicationConfig(applicationConfig restapi.ApplicationConfig, t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

client := mocks.NewMockRestClient(ctrl)
sut := NewApplicationConfigResource(client)

serializedJSON, _ := json.Marshal(applicationConfig)

client.EXPECT().Put(gomock.Eq(applicationConfig), gomock.Eq(restapi.ApplicationConfigsResourcePath)).Return(serializedJSON, nil)
Expand Down

0 comments on commit cef1034

Please sign in to comment.