Skip to content

Commit e8e1b5d

Browse files
authored
Merge pull request #886 from marle3003/develop
Develop
2 parents 142c3c8 + e0df1f3 commit e8e1b5d

106 files changed

Lines changed: 2415 additions & 1801 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

acceptance/cmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func Start(cfg *static.Config) (*Cmd, error) {
3737
feature.Enable(cfg.Features)
3838

3939
registerDynamicTypes()
40-
app := runtime.New(cfg)
41-
generator.SetConfig(cfg.DataGen)
4240

4341
watcher := server.NewConfigWatcher(cfg)
42+
app := runtime.New(cfg, watcher)
43+
generator.SetConfig(cfg.DataGen)
4444

4545
certStore, err := cert.NewStore(cfg)
4646
if err != nil {

api/handler_events_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"mokapi/config/dynamic/dynamictest"
78
"mokapi/config/static"
89
"mokapi/providers/asyncapi3/kafka/store"
910
"mokapi/providers/openapi"
@@ -202,7 +203,7 @@ func TestHandler_Events(t *testing.T) {
202203
tc := tc
203204
t.Run(tc.name, func(t *testing.T) {
204205
cfg := &static.Config{}
205-
app := runtime.New(cfg)
206+
app := runtime.New(cfg, &dynamictest.Reader{})
206207

207208
h := New(app, static.Api{})
208209
tc.fn(t, h, app.Events)
@@ -330,7 +331,7 @@ func TestHandler_KafkaEvents(t *testing.T) {
330331
tc := tc
331332
t.Run(tc.name, func(t *testing.T) {
332333
cfg := &static.Config{}
333-
app := runtime.New(cfg)
334+
app := runtime.New(cfg, &dynamictest.Reader{})
334335

335336
h := New(app, static.Api{})
336337
tc.fn(t, h, app.Events)

api/handler_fileserver_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"mokapi/api"
66
"mokapi/config/dynamic"
7+
"mokapi/config/dynamic/dynamictest"
78
"mokapi/config/static"
89
"mokapi/providers/openapi/openapitest"
910
"mokapi/runtime"
@@ -131,7 +132,7 @@ func TestHandler_FileServer(t *testing.T) {
131132
t.Parallel()
132133

133134
cfg := &static.Config{}
134-
h := api.New(runtime.New(cfg), tc.config)
135+
h := api.New(runtime.New(cfg, &dynamictest.Reader{}), tc.config)
135136
tc.fn(t, h)
136137
})
137138
}
@@ -146,7 +147,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
146147
name: "http service",
147148
test: func(t *testing.T) {
148149
cfg := &static.Config{}
149-
app := runtime.New(cfg)
150+
app := runtime.New(cfg, &dynamictest.Reader{})
150151
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0", openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."))})
151152
h := api.New(app, static.Api{Path: "/mokapi", Dashboard: true})
152153
try.Handler(t,
@@ -166,7 +167,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
166167
name: "http service path without summary and description",
167168
test: func(t *testing.T) {
168169
cfg := &static.Config{}
169-
app := runtime.New(cfg)
170+
app := runtime.New(cfg, &dynamictest.Reader{})
170171
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
171172
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
172173
openapitest.WithPath("/pet/{petId}"),
@@ -190,7 +191,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
190191
name: "http service path with summary and description",
191192
test: func(t *testing.T) {
192193
cfg := &static.Config{}
193-
app := runtime.New(cfg)
194+
app := runtime.New(cfg, &dynamictest.Reader{})
194195
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
195196
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
196197
openapitest.WithPath("/pet/{petId}",
@@ -216,7 +217,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
216217
name: "http service path with no summary but description",
217218
test: func(t *testing.T) {
218219
cfg := &static.Config{}
219-
app := runtime.New(cfg)
220+
app := runtime.New(cfg, &dynamictest.Reader{})
220221
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
221222
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
222223
openapitest.WithPath("/pet/{petId}",
@@ -241,7 +242,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
241242
name: "http service endpoint no summary and no description",
242243
test: func(t *testing.T) {
243244
cfg := &static.Config{}
244-
app := runtime.New(cfg)
245+
app := runtime.New(cfg, &dynamictest.Reader{})
245246
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
246247
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
247248
openapitest.WithPath("/pet/{petId}",
@@ -266,7 +267,7 @@ func TestOpenGraphInDashboard(t *testing.T) {
266267
name: "http service endpoint get right path",
267268
test: func(t *testing.T) {
268269
cfg := &static.Config{}
269-
app := runtime.New(cfg)
270+
app := runtime.New(cfg, &dynamictest.Reader{})
270271
app.AddHttp(&dynamic.Config{Info: dynamic.ConfigInfo{Url: mustParse("https://foo.bar")}, Data: openapitest.NewConfig("3.0",
271272
openapitest.WithInfo("Swagger Petstore", "1.0", "This is a sample server Petstore server."),
272273
openapitest.WithPath("/pet/{petId}",

api/handler_http_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestHandler_Http(t *testing.T) {
6666
{
6767
name: "get http service info",
6868
app: func() *runtime.App {
69-
app := runtime.New(&static.Config{})
69+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
7070
cfg := &dynamic.Config{
7171
Info: dynamictest.NewConfigInfo(), Data: openapitest.NewConfig("3.0.0",
7272
openapitest.WithInfo("foo", "1.0", "bar"),
@@ -328,7 +328,7 @@ func TestHandler_Http(t *testing.T) {
328328

329329
func TestHandler_Http_NotFound(t *testing.T) {
330330
cfg := &static.Config{}
331-
h := New(runtime.New(cfg), static.Api{})
331+
h := New(runtime.New(cfg, &dynamictest.Reader{}), static.Api{})
332332

333333
try.Handler(t,
334334
http.MethodGet,

api/handler_kafka.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"time"
1818

1919
"github.com/pkg/errors"
20+
log "github.com/sirupsen/logrus"
2021
)
2122

2223
type kafkaSummary struct {
@@ -564,18 +565,38 @@ func newTopic(t *store.Topic, ch *asyncapi3.Channel, cfg *asyncapi3.Config) topi
564565
}
565566

566567
if msg.Payload != nil && msg.Payload.Value != nil {
567-
m.Payload = &schemaInfo{Schema: msg.Payload.Value.Schema, Format: msg.Payload.Value.Format}
568+
format := ""
569+
if msf, ok := msg.Payload.Value.(*asyncapi3.MultiSchemaFormat); ok {
570+
format = msf.Format
571+
}
572+
s, err := msg.Payload.GetSchema()
573+
if err != nil {
574+
log.Errorf("failed to get schema for message in topic '%s': %v", t.Name, err)
575+
}
576+
m.Payload = &schemaInfo{Schema: s, Format: format}
568577
}
569578
if msg.Headers != nil && msg.Headers.Value != nil {
570-
m.Header = &schemaInfo{Schema: msg.Headers.Value.Schema, Format: msg.Headers.Value.Format}
579+
format := ""
580+
if msf, ok := msg.Headers.Value.(*asyncapi3.MultiSchemaFormat); ok {
581+
format = msf.Format
582+
}
583+
s, err := msg.Headers.GetSchema()
584+
if err != nil {
585+
log.Errorf("failed to get schema for headers in topic '%s': %v", t.Name, err)
586+
}
587+
m.Header = &schemaInfo{Schema: s, Format: format}
571588
}
572589

573590
if m.ContentType == "" {
574591
m.ContentType = cfg.DefaultContentType
575592
}
576593

577594
if msg.Bindings.Kafka.Key != nil {
578-
m.Key = &schemaInfo{Schema: msg.Bindings.Kafka.Key.Value.Schema}
595+
s, err := msg.Bindings.Kafka.Key.GetSchema()
596+
if err != nil {
597+
log.Errorf("failed to get schema for key in topic '%s': %v", t.Name, err)
598+
}
599+
m.Key = &schemaInfo{Schema: s}
579600
}
580601
if result.Messages == nil {
581602
result.Messages = map[string]messageConfig{}

api/handler_kafka_test.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"mokapi/api"
66
"mokapi/config/dynamic"
7+
"mokapi/config/dynamic/asyncApi"
78
"mokapi/config/dynamic/dynamictest"
89
"mokapi/config/static"
910
"mokapi/engine/enginetest"
@@ -57,7 +58,7 @@ func TestHandler_Kafka(t *testing.T) {
5758
{
5859
name: "get kafka services",
5960
app: func() *runtime.App {
60-
app := runtime.New(&static.Config{})
61+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
6162
_, _ = app.Kafka.Add(&dynamic.Config{
6263
Info: dynamic.ConfigInfo{Url: try.MustUrl("kafka.yaml")},
6364
Data: asyncapi3test.NewConfig(
@@ -91,7 +92,7 @@ func TestHandler_Kafka(t *testing.T) {
9192
{
9293
name: "get specific",
9394
app: func() *runtime.App {
94-
app := runtime.New(&static.Config{})
95+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
9596
cfg := &dynamic.Config{
9697
Info: dynamictest.NewConfigInfo(),
9798
Data: asyncapi3test.NewConfig(
@@ -221,7 +222,7 @@ func TestHandler_Kafka(t *testing.T) {
221222
{
222223
name: "get specific with group",
223224
app: func() *runtime.App {
224-
app := runtime.New(&static.Config{})
225+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
225226
app.Kafka.Set("foo", getKafkaInfoWithGroup(asyncapi3test.NewConfig(
226227
asyncapi3test.WithInfo("foo", "bar", "1.0"),
227228
asyncapi3test.WithServer("foo", "kafka", "foo.bar"),
@@ -246,7 +247,7 @@ func TestHandler_Kafka(t *testing.T) {
246247
{
247248
name: "get specific with group no generation",
248249
app: func() *runtime.App {
249-
app := runtime.New(&static.Config{})
250+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
250251
app.Kafka.Set("foo", getKafkaInfoWithGroup(asyncapi3test.NewConfig(
251252
asyncapi3test.WithInfo("foo", "bar", "1.0"),
252253
asyncapi3test.WithServer("foo", "kafka", "foo.bar"),
@@ -273,7 +274,7 @@ func TestHandler_Kafka(t *testing.T) {
273274
return t1
274275
}
275276

276-
app := runtime.New(&static.Config{})
277+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
277278
app.Kafka.Set("foo", getKafkaInfoWithGroup(asyncapi3test.NewConfig(
278279
asyncapi3test.WithInfo("foo", "bar", "1.0"),
279280
asyncapi3test.WithServer("foo", "kafka", "foo.bar"),
@@ -321,7 +322,7 @@ func TestHandler_Kafka(t *testing.T) {
321322
{
322323
name: "get specific with topic and openapi schema",
323324
app: func() *runtime.App {
324-
app := runtime.New(&static.Config{})
325+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
325326
app.Kafka.Set("foo", getKafkaInfo(asyncapi3test.NewConfig(
326327
asyncapi3test.WithInfo("foo", "bar", "1.0"),
327328
asyncapi3test.WithChannel("foo",
@@ -369,7 +370,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
369370
{
370371
name: "get kafka topics but empty",
371372
app: func() *runtime.App {
372-
app := runtime.New(&static.Config{})
373+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
373374
_, _ = app.Kafka.Add(&dynamic.Config{
374375
Info: dynamic.ConfigInfo{Url: try.MustUrl("kafka.yaml")},
375376
Data: asyncapi3test.NewConfig(
@@ -394,7 +395,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
394395
{
395396
name: "get kafka topics with one topic",
396397
app: func() *runtime.App {
397-
app := runtime.New(&static.Config{})
398+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
398399
_, _ = app.Kafka.Add(&dynamic.Config{
399400
Info: dynamic.ConfigInfo{Url: try.MustUrl("kafka.yaml")},
400401
Data: asyncapi3test.NewConfig(
@@ -424,7 +425,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
424425
{
425426
name: "get specific kafka topic",
426427
app: func() *runtime.App {
427-
app := runtime.New(&static.Config{})
428+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
428429
_, _ = app.Kafka.Add(&dynamic.Config{
429430
Info: dynamic.ConfigInfo{Url: try.MustUrl("kafka.yaml")},
430431
Data: asyncapi3test.NewConfig(
@@ -454,7 +455,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
454455
{
455456
name: "get specific kafka topic but not found",
456457
app: func() *runtime.App {
457-
app := runtime.New(&static.Config{})
458+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
458459
_, _ = app.Kafka.Add(&dynamic.Config{
459460
Info: dynamic.ConfigInfo{Url: try.MustUrl("kafka.yaml")},
460461
Data: asyncapi3test.NewConfig(
@@ -482,7 +483,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
482483
{
483484
name: "produce kafka message into topic",
484485
app: func() *runtime.App {
485-
app := runtime.New(&static.Config{})
486+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
486487

487488
msg := asyncapi3test.NewMessage(
488489
asyncapi3test.WithContentType("application/json"),
@@ -532,7 +533,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
532533
{
533534
name: "produce kafka message into topic using binary",
534535
app: func() *runtime.App {
535-
app := runtime.New(&static.Config{})
536+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
536537
_, _ = app.Kafka.Add(&dynamic.Config{
537538
Info: dynamic.ConfigInfo{Url: try.MustUrl("kafka.yaml")},
538539
Data: asyncapi3test.NewConfig(
@@ -571,7 +572,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
571572
{
572573
name: "produce invalid kafka message into topic",
573574
app: func() *runtime.App {
574-
app := runtime.New(&static.Config{})
575+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
575576

576577
msg := asyncapi3test.NewMessage(
577578
asyncapi3test.WithContentType("application/json"),
@@ -627,7 +628,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
627628
{
628629
name: "get kafka partitions",
629630
app: func() *runtime.App {
630-
app := runtime.New(&static.Config{})
631+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
631632
_, _ = app.Kafka.Add(&dynamic.Config{
632633
Info: dynamic.ConfigInfo{Url: try.MustUrl("kafka.yaml")},
633634
Data: asyncapi3test.NewConfig(
@@ -656,7 +657,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
656657
{
657658
name: "get specific kafka partition",
658659
app: func() *runtime.App {
659-
app := runtime.New(&static.Config{})
660+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
660661
_, _ = app.Kafka.Add(&dynamic.Config{
661662
Info: dynamic.ConfigInfo{Url: try.MustUrl("kafka.yaml")},
662663
Data: asyncapi3test.NewConfig(
@@ -685,7 +686,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
685686
{
686687
name: "produce kafka message into specific partition",
687688
app: func() *runtime.App {
688-
app := runtime.New(&static.Config{})
689+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
689690

690691
msg := asyncapi3test.NewMessage(
691692
asyncapi3test.WithContentType("application/json"),
@@ -739,7 +740,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
739740
{
740741
name: "produce kafka message into specific partition using XML",
741742
app: func() *runtime.App {
742-
app := runtime.New(&static.Config{})
743+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
743744

744745
msg := asyncapi3test.NewMessage(
745746
asyncapi3test.WithContentType("application/xml"),
@@ -801,7 +802,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
801802
{
802803
name: "produce kafka message into specific partition using plain XML string",
803804
app: func() *runtime.App {
804-
app := runtime.New(&static.Config{})
805+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
805806

806807
msg := asyncapi3test.NewMessage(
807808
asyncapi3test.WithContentType("application/xml"),
@@ -853,7 +854,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
853854
{
854855
name: "produce invalid kafka message into specific partition using plain XML string",
855856
app: func() *runtime.App {
856-
app := runtime.New(&static.Config{})
857+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
857858

858859
msg := asyncapi3test.NewMessage(
859860
asyncapi3test.WithContentType("application/xml"),
@@ -912,7 +913,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
912913
{
913914
name: "get records",
914915
app: func() *runtime.App {
915-
app := runtime.New(&static.Config{})
916+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
916917

917918
msg := asyncapi3test.NewMessage(
918919
asyncapi3test.WithContentType("application/json"),
@@ -970,7 +971,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
970971
{
971972
name: "get specific record",
972973
app: func() *runtime.App {
973-
app := runtime.New(&static.Config{})
974+
app := runtime.New(&static.Config{}, &dynamictest.Reader{})
974975

975976
msg := asyncapi3test.NewMessage(
976977
asyncapi3test.WithContentType("application/json"),
@@ -1035,7 +1036,7 @@ func TestHandler_KafkaAPI(t *testing.T) {
10351036
}
10361037

10371038
func TestHandler_Kafka_NotFound(t *testing.T) {
1038-
h := api.New(runtime.New(&static.Config{}), static.Api{})
1039+
h := api.New(runtime.New(&static.Config{}, &dynamictest.Reader{}), static.Api{})
10391040

10401041
try.Handler(t,
10411042
http.MethodGet,
@@ -1096,6 +1097,13 @@ func TestHandler_Kafka_Metrics(t *testing.T) {
10961097
}
10971098
}
10981099

1100+
func Test_IsAsyncApiConfig(t *testing.T) {
1101+
v2 := &asyncApi.Config{Info: asyncApi.Info{Name: "foo"}}
1102+
_, ok := runtime.IsAsyncApiConfig(&dynamic.Config{Data: v2})
1103+
require.True(t, ok)
1104+
1105+
}
1106+
10991107
func getKafkaInfo(config *asyncapi3.Config) *runtime.KafkaInfo {
11001108
return &runtime.KafkaInfo{
11011109
Config: config,

0 commit comments

Comments
 (0)