internal/openapi/generator/coordinatingGenerator_test.go   A
last analyzed

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 85
dl 0
loc 123
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A generator.*CoordinatingGeneratorSuite.TestGenerateResponse_ContentGenerationError_Error 0 24 1
A generator.TestCoordinatingGeneratorSuite 0 2 1
A generator.*CoordinatingGeneratorSuite.TestGenerateResponse_StatusCodeNegotiationError_Error 0 22 1
A generator.*CoordinatingGeneratorSuite.assertExpectations 0 4 1
A generator.*CoordinatingGeneratorSuite.TestGenerateResponse_RouteWithValidResponse_ResponseGeneratedAndReturned 0 26 1
A generator.*CoordinatingGeneratorSuite.SetupTest 0 9 1
1
package generator
2
3
import (
4
	"errors"
5
	"net/http"
6
	"testing"
7
8
	"github.com/getkin/kin-openapi/openapi3"
9
	"github.com/getkin/kin-openapi/routers"
10
	contentmock "github.com/muonsoft/openapi-mock/test/mocks/openapi/generator/content"
11
	negotiatormock "github.com/muonsoft/openapi-mock/test/mocks/openapi/generator/negotiator"
12
	"github.com/stretchr/testify/mock"
13
	"github.com/stretchr/testify/suite"
14
)
15
16
type CoordinatingGeneratorSuite struct {
17
	suite.Suite
18
19
	statusCodeNegotiator  *negotiatormock.StatusCodeNegotiator
20
	contentTypeNegotiator *negotiatormock.ContentTypeNegotiator
21
	contentGenerator      *contentmock.Generator
22
23
	generator *coordinatingGenerator
24
}
25
26
func (suite *CoordinatingGeneratorSuite) SetupTest() {
27
	suite.statusCodeNegotiator = &negotiatormock.StatusCodeNegotiator{}
28
	suite.contentTypeNegotiator = &negotiatormock.ContentTypeNegotiator{}
29
	suite.contentGenerator = &contentmock.Generator{}
30
31
	suite.generator = &coordinatingGenerator{
32
		statusCodeNegotiator:  suite.statusCodeNegotiator,
33
		contentTypeNegotiator: suite.contentTypeNegotiator,
34
		contentGenerator:      suite.contentGenerator,
35
	}
36
}
37
38
func TestCoordinatingGeneratorSuite(t *testing.T) {
39
	suite.Run(t, new(CoordinatingGeneratorSuite))
40
}
41
42
func (suite *CoordinatingGeneratorSuite) TestGenerateResponse_RouteWithValidResponse_ResponseGeneratedAndReturned() {
43
	request, _ := http.NewRequest("", "", nil)
44
	mediaType := &openapi3.MediaType{}
45
	matchingResponse := &openapi3.Response{
46
		Content: map[string]*openapi3.MediaType{
47
			"contentType": mediaType,
48
		},
49
	}
50
	route := &routers.Route{}
51
	route.Operation = &openapi3.Operation{}
52
	route.Operation.Responses = openapi3.Responses{
53
		"200": {
54
			Value: matchingResponse,
55
		},
56
	}
57
	suite.statusCodeNegotiator.On("NegotiateStatusCode", request, route.Operation.Responses).Return("200", http.StatusOK, nil).Once()
58
	suite.contentTypeNegotiator.On("NegotiateContentType", request, matchingResponse).Return("contentType").Once()
59
	suite.contentGenerator.On("GenerateContent", mock.Anything, matchingResponse, "contentType").Return("data", nil).Once()
60
61
	apiResponse, err := suite.generator.GenerateResponse(request, route)
62
63
	suite.assertExpectations()
64
	suite.NoError(err)
65
	suite.Equal(http.StatusOK, apiResponse.StatusCode)
66
	suite.Equal("contentType", apiResponse.ContentType)
67
	suite.Equal("data", apiResponse.Data)
68
}
69
70
func (suite *CoordinatingGeneratorSuite) TestGenerateResponse_ContentGenerationError_Error() {
71
	request, _ := http.NewRequest("", "", nil)
72
	mediaType := &openapi3.MediaType{}
73
	matchingResponse := &openapi3.Response{
74
		Content: map[string]*openapi3.MediaType{
75
			"contentType": mediaType,
76
		},
77
	}
78
	route := &routers.Route{}
79
	route.Operation = &openapi3.Operation{}
80
	route.Operation.Responses = openapi3.Responses{
81
		"200": {
82
			Value: matchingResponse,
83
		},
84
	}
85
	suite.statusCodeNegotiator.On("NegotiateStatusCode", request, route.Operation.Responses).Return("200", http.StatusOK, nil).Once()
86
	suite.contentTypeNegotiator.On("NegotiateContentType", request, matchingResponse).Return("contentType").Once()
87
	suite.contentGenerator.On("GenerateContent", mock.Anything, matchingResponse, "contentType").Return(nil, errors.New("error")).Once()
88
89
	apiResponse, err := suite.generator.GenerateResponse(request, route)
90
91
	suite.assertExpectations()
92
	suite.EqualError(err, "[coordinatingGenerator] failed to generate response data: error")
93
	suite.Nil(apiResponse)
94
}
95
96
func (suite *CoordinatingGeneratorSuite) TestGenerateResponse_StatusCodeNegotiationError_Error() {
97
	request, _ := http.NewRequest("", "", nil)
98
	mediaType := &openapi3.MediaType{}
99
	matchingResponse := &openapi3.Response{
100
		Content: map[string]*openapi3.MediaType{
101
			"contentType": mediaType,
102
		},
103
	}
104
	route := &routers.Route{}
105
	route.Operation = &openapi3.Operation{}
106
	route.Operation.Responses = openapi3.Responses{
107
		"200": {
108
			Value: matchingResponse,
109
		},
110
	}
111
	suite.statusCodeNegotiator.On("NegotiateStatusCode", request, route.Operation.Responses).Return("", 0, errors.New("error")).Once()
112
113
	apiResponse, err := suite.generator.GenerateResponse(request, route)
114
115
	suite.assertExpectations()
116
	suite.EqualError(err, "[coordinatingGenerator] failed to negotiate response: error")
117
	suite.Nil(apiResponse)
118
}
119
120
func (suite *CoordinatingGeneratorSuite) assertExpectations() {
121
	suite.statusCodeNegotiator.AssertExpectations(suite.T())
122
	suite.contentTypeNegotiator.AssertExpectations(suite.T())
123
	suite.contentGenerator.AssertExpectations(suite.T())
124
}
125