-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonschema_module_bdd_test.go
More file actions
150 lines (127 loc) · 5.82 KB
/
jsonschema_module_bdd_test.go
File metadata and controls
150 lines (127 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package jsonschema
import (
"context"
"sync"
"testing"
"github.com/GoCodeAlone/modular"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cucumber/godog"
)
// JSONSchema BDD Test Context
type JSONSchemaBDDTestContext struct {
app modular.Application
module *Module
service JSONSchemaService
lastError error
compiledSchema Schema
validationPass bool
tempFile string
capturedEvents []cloudevents.Event
eventObserver *testEventObserver
}
// testEventObserver captures events for testing
type testEventObserver struct {
mu sync.RWMutex
events []cloudevents.Event
id string
}
func newTestEventObserver() *testEventObserver {
return &testEventObserver{
id: "test-observer-jsonschema",
}
}
func (o *testEventObserver) OnEvent(ctx context.Context, event cloudevents.Event) error {
o.mu.Lock()
defer o.mu.Unlock()
o.events = append(o.events, event)
return nil
}
func (o *testEventObserver) ObserverID() string {
return o.id
}
func (o *testEventObserver) GetEvents() []cloudevents.Event {
o.mu.RLock()
defer o.mu.RUnlock()
// Return a copy of the slice to avoid race conditions
result := make([]cloudevents.Event, len(o.events))
copy(result, o.events)
return result
}
func (o *testEventObserver) ClearEvents() {
o.mu.Lock()
defer o.mu.Unlock()
o.events = nil
}
func (ctx *JSONSchemaBDDTestContext) resetContext() {
ctx.app = nil
ctx.module = nil
ctx.service = nil
ctx.lastError = nil
ctx.compiledSchema = nil
ctx.validationPass = false
ctx.capturedEvents = nil
ctx.eventObserver = newTestEventObserver()
}
// Shared utilities and test context structures
// Test logger implementation
type testLogger struct{}
func (l *testLogger) Debug(msg string, keysAndValues ...interface{}) {}
func (l *testLogger) Info(msg string, keysAndValues ...interface{}) {}
func (l *testLogger) Warn(msg string, keysAndValues ...interface{}) {}
func (l *testLogger) Error(msg string, keysAndValues ...interface{}) {}
// TestJSONSchemaModuleBDD runs the BDD tests for the JSONSchema module
func TestJSONSchemaModuleBDD(t *testing.T) {
suite := godog.TestSuite{
ScenarioInitializer: func(ctx *godog.ScenarioContext) {
testCtx := &JSONSchemaBDDTestContext{}
// Background
ctx.Given(`^I have a modular application with jsonschema module configured$`, testCtx.iHaveAModularApplicationWithJSONSchemaModuleConfigured)
// Steps for module initialization
ctx.When(`^the jsonschema module is initialized$`, testCtx.theJSONSchemaModuleIsInitialized)
ctx.Then(`^the jsonschema service should be available$`, testCtx.theJSONSchemaServiceShouldBeAvailable)
// Steps for basic functionality
ctx.Given(`^I have a jsonschema service available$`, testCtx.iHaveAJSONSchemaServiceAvailable)
ctx.When(`^I compile a schema from a JSON string$`, testCtx.iCompileASchemaFromAJSONString)
ctx.Then(`^the schema should be compiled successfully$`, testCtx.theSchemaShouldBeCompiledSuccessfully)
// Steps for validation
ctx.Given(`^I have a compiled schema for user data$`, testCtx.iHaveACompiledSchemaForUserData)
ctx.When(`^I validate valid user JSON data$`, testCtx.iValidateValidUserJSONData)
ctx.Then(`^the validation should pass$`, testCtx.theValidationShouldPass)
ctx.When(`^I validate invalid user JSON data$`, testCtx.iValidateInvalidUserJSONData)
ctx.Then(`^the validation should fail with appropriate errors$`, testCtx.theValidationShouldFailWithAppropriateErrors)
// Steps for different validation methods
ctx.Given(`^I have a compiled schema$`, testCtx.iHaveACompiledSchema)
ctx.When(`^I validate data from bytes$`, testCtx.iValidateDataFromBytes)
ctx.When(`^I validate data from reader$`, testCtx.iValidateDataFromReader)
ctx.When(`^I validate data from interface$`, testCtx.iValidateDataFromInterface)
ctx.Then(`^all validation methods should work correctly$`, testCtx.allValidationMethodsShouldWorkCorrectly)
// Steps for error handling
ctx.When(`^I try to compile an invalid schema$`, testCtx.iTryToCompileAnInvalidSchema)
ctx.Then(`^a schema compilation error should be returned$`, testCtx.aSchemaCompilationErrorShouldBeReturned)
// Event observation steps
ctx.Given(`^I have a jsonschema service with event observation enabled$`, testCtx.iHaveAJSONSchemaServiceWithEventObservationEnabled)
ctx.When(`^I compile a valid schema$`, testCtx.iCompileAValidSchema)
ctx.Then(`^a schema compiled event should be emitted$`, testCtx.aSchemaCompiledEventShouldBeEmitted)
ctx.Then(`^the event should contain the source information$`, testCtx.theEventShouldContainTheSourceInformation)
ctx.Then(`^a schema error event should be emitted$`, testCtx.aSchemaErrorEventShouldBeEmitted)
ctx.When(`^I validate valid user JSON data with bytes method$`, testCtx.iValidateValidUserJSONDataWithBytesMethod)
ctx.When(`^I validate invalid user JSON data with bytes method$`, testCtx.iValidateInvalidUserJSONDataWithBytesMethod)
ctx.Then(`^a validate bytes event should be emitted$`, testCtx.aValidateBytesEventShouldBeEmitted)
ctx.Then(`^a validation success event should be emitted$`, testCtx.aValidationSuccessEventShouldBeEmitted)
ctx.Then(`^a validation failed event should be emitted$`, testCtx.aValidationFailedEventShouldBeEmitted)
ctx.When(`^I validate data using the reader method$`, testCtx.iValidateDataUsingTheReaderMethod)
ctx.When(`^I validate data using the interface method$`, testCtx.iValidateDataUsingTheInterfaceMethod)
ctx.Then(`^a validate reader event should be emitted$`, testCtx.aValidateReaderEventShouldBeEmitted)
ctx.Then(`^a validate interface event should be emitted$`, testCtx.aValidateInterfaceEventShouldBeEmitted)
},
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t,
Strict: true,
},
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}