|
1
|
|
|
package content |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"testing" |
|
6
|
|
|
|
|
7
|
|
|
"github.com/getkin/kin-openapi/openapi3" |
|
8
|
|
|
"github.com/muonsoft/openapi-mock/pkg/logcontext" |
|
9
|
|
|
generatormock "github.com/muonsoft/openapi-mock/test/mocks/mock/generator" |
|
10
|
|
|
"github.com/sirupsen/logrus" |
|
11
|
|
|
"github.com/sirupsen/logrus/hooks/test" |
|
12
|
|
|
"github.com/stretchr/testify/mock" |
|
13
|
|
|
"github.com/stretchr/testify/suite" |
|
14
|
|
|
) |
|
15
|
|
|
|
|
16
|
|
|
type PlainTextGeneratorSuite struct { |
|
17
|
|
|
suite.Suite |
|
18
|
|
|
|
|
19
|
|
|
contentGenerator *generatormock.MediaGenerator |
|
20
|
|
|
|
|
21
|
|
|
logger *logrus.Logger |
|
22
|
|
|
hook *test.Hook |
|
23
|
|
|
|
|
24
|
|
|
examples openapi3.Examples |
|
25
|
|
|
|
|
26
|
|
|
plainTextGenerator *plainTextGenerator |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
func (suite *PlainTextGeneratorSuite) SetupTest() { |
|
30
|
|
|
suite.contentGenerator = &generatormock.MediaGenerator{} |
|
31
|
|
|
suite.logger, suite.hook = test.NewNullLogger() |
|
32
|
|
|
|
|
33
|
|
|
suite.examples = make(openapi3.Examples) |
|
34
|
|
|
|
|
35
|
|
|
suite.plainTextGenerator = &plainTextGenerator{ |
|
36
|
|
|
contentGenerator: suite.contentGenerator, |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
func TestPlainTextGeneratorSuite(t *testing.T) { |
|
41
|
|
|
suite.Run(t, new(PlainTextGeneratorSuite)) |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
func (suite *PlainTextGeneratorSuite) TestPlainTextProcessor_GenerateContent_ResponseWithString_GeneratedDataWithoutWarnings() { |
|
45
|
|
|
response := suite.givenResponse() |
|
46
|
|
|
suite.contentGenerator.On("GenerateData", mock.Anything, suite.expectedMediaType()).Return("data", nil).Once() |
|
47
|
|
|
|
|
48
|
|
|
content, err := suite.plainTextGenerator.GenerateContent(context.Background(), response, "text/plain") |
|
49
|
|
|
|
|
50
|
|
|
suite.contentGenerator.AssertExpectations(suite.T()) |
|
51
|
|
|
suite.NoError(err) |
|
52
|
|
|
suite.Equal("data", content) |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
func (suite *PlainTextGeneratorSuite) TestPlainTextProcessor_GenerateContent_ResponseWithObject_Warning() { |
|
56
|
|
|
response := suite.givenResponse() |
|
57
|
|
|
response.Content["text/plain"].Schema.Value.Type = "object" |
|
58
|
|
|
ctx := logcontext.WithLogger(context.Background(), suite.logger) |
|
59
|
|
|
suite.contentGenerator.On("GenerateData", mock.Anything, suite.expectedMediaType()).Return("data", nil).Once() |
|
60
|
|
|
|
|
61
|
|
|
content, err := suite.plainTextGenerator.GenerateContent(ctx, response, "text/plain") |
|
62
|
|
|
|
|
63
|
|
|
suite.contentGenerator.AssertExpectations(suite.T()) |
|
64
|
|
|
suite.NoError(err) |
|
65
|
|
|
suite.Equal("data", content) |
|
66
|
|
|
suite.assertWarningWasLogged() |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
func (suite *PlainTextGeneratorSuite) TestPlainTextProcessor_GenerateContent_ResponseWithoutSchema_Warning() { |
|
70
|
|
|
response := suite.givenResponse() |
|
71
|
|
|
response.Content["text/plain"].Schema = nil |
|
72
|
|
|
ctx := logcontext.WithLogger(context.Background(), suite.logger) |
|
73
|
|
|
suite.contentGenerator.On("GenerateData", mock.Anything, suite.expectedMediaType()).Return("data", nil).Once() |
|
74
|
|
|
|
|
75
|
|
|
content, err := suite.plainTextGenerator.GenerateContent(ctx, response, "text/plain") |
|
76
|
|
|
|
|
77
|
|
|
suite.contentGenerator.AssertExpectations(suite.T()) |
|
78
|
|
|
suite.NoError(err) |
|
79
|
|
|
suite.Equal("data", content) |
|
80
|
|
|
suite.assertWarningWasLogged() |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
func (suite *PlainTextGeneratorSuite) givenResponse() *openapi3.Response { |
|
84
|
|
|
return &openapi3.Response{ |
|
85
|
|
|
Content: map[string]*openapi3.MediaType{ |
|
86
|
|
|
"text/plain": { |
|
87
|
|
|
Example: "example", |
|
88
|
|
|
Examples: suite.examples, |
|
89
|
|
|
Schema: &openapi3.SchemaRef{ |
|
90
|
|
|
Value: &openapi3.Schema{ |
|
91
|
|
|
Type: "string", |
|
92
|
|
|
}, |
|
93
|
|
|
}, |
|
94
|
|
|
}, |
|
95
|
|
|
}, |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
func (suite *PlainTextGeneratorSuite) expectedMediaType() interface{} { |
|
100
|
|
|
return mock.MatchedBy(func(mediaType *openapi3.MediaType) bool { |
|
101
|
|
|
suite.Equal("string", mediaType.Schema.Value.Type) |
|
102
|
|
|
suite.Equal("", mediaType.Schema.Value.Format) |
|
103
|
|
|
suite.Equal("example", mediaType.Example) |
|
104
|
|
|
suite.Equal(suite.examples, mediaType.Examples) |
|
105
|
|
|
|
|
106
|
|
|
return true |
|
107
|
|
|
}) |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
func (suite *PlainTextGeneratorSuite) assertWarningWasLogged() { |
|
111
|
|
|
suite.Len(suite.hook.Entries, 1) |
|
112
|
|
|
suite.Equal(logrus.WarnLevel, suite.hook.LastEntry().Level) |
|
113
|
|
|
suite.Equal("only string schema is supported for 'text/plain' content type", suite.hook.LastEntry().Message) |
|
114
|
|
|
} |
|
115
|
|
|
|