-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities_test.go
154 lines (141 loc) · 5.21 KB
/
entities_test.go
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
151
152
153
154
package eventuate_test
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xeipuuv/gojsonschema"
"github.com/eventuate-clients/eventuate-client-golang"
)
const APPLICATION_NAMESPACE = "net.chrisrichardson.eventstore.example.todo"
func TestRESTClient_JsonSchemas(t *testing.T) {
jsonSchemas := map[string]string{
"Find Response": "json_schemas/rest/get-response.json",
//"Create Request": "json_schemas/rest/create-request.json",
"Create Response": "json_schemas/rest/create-response.json",
//"Update Request": "json_schemas/rest/update-request.json",
"Update Response": "json_schemas/rest/update-response.json"}
factualStructures := map[string]interface{}{
"Find Response": eventuate.LoadedEvents{
Events: []eventuate.EventIdTypeAndData{
{
EventId: eventuate.Int128FromString(EVENT_ID_1),
EventTypeAndData: eventuate.EventTypeAndData{
EventType: EVENT_CREATED,
EventData: EVENT_DATA_1}}}},
"Create Response": eventuate.EntityIdVersionAndEventIds(eventuate.CreateResponse{
EntityId: eventuate.Int128FromString(ENTITY_ID),
EntityVersion: eventuate.Int128FromString(EVENT_ID_1),
EventIds: []eventuate.Int128{eventuate.Int128FromString(EVENT_ID_1)}}),
"Update Response": eventuate.UpdateResponse{
EntityId: eventuate.Int128FromString(ENTITY_ID),
EntityVersion: eventuate.Int128FromString(EVENT_ID_2),
EventIds: []eventuate.Int128{eventuate.Int128FromString(EVENT_ID_2)}}}
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
for name, sPath := range jsonSchemas {
expectedSchemaPath := fmt.Sprintf("file://%s/%s", pwd, sPath)
expectedSchema := gojsonschema.NewReferenceLoader(expectedSchemaPath)
factualStructure, ok := factualStructures[name]
if !ok {
t.Errorf("Structure is missing for: %s", name)
continue
}
factualSchema := gojsonschema.NewGoLoader(factualStructure)
validationResult, err := gojsonschema.Validate(expectedSchema, factualSchema)
if err != nil {
t.Error(err)
}
assert.Equal(t, true, validationResult.Valid())
if !validationResult.Valid() {
t.Error(validationResult.Errors())
}
}
}
//
//func TestJsonSchemas(t *testing.T) {
// t.Skip()
// return
// t.Parallel()
//
// workDirectory, workDirectoryErr := os.Getwd()
// logErrorAndQuit(workDirectoryErr)
//
// loader := gojsonschema.NewGoLoader(
// CreateEntityRequest{
// Events: []EventTypeAndData{
// {
// EventType: "net.chrisrichardson.eventstore.example.MyEntity",
// EventData: `"name":"Fred"`}},
// EntityTypeName: "net.chrisrichardson.eventstore.example.MyEntity"})
//
// schemaLoader := gojsonschema.NewReferenceLoader(
// fmt.Sprintf("file://%s/json_schema_test_suite/create-request.json", workDirectory))
//
// checkCreateRequest, createRequestError := gojsonschema.Validate(schemaLoader, loader)
// assertNoError(t, createRequestError)
//
// assert.Equal(t, true, checkCreateRequest.Valid())
//
// loader = gojsonschema.NewGoLoader(
// CreateResponse{
// EntityId: "000001588bce5f47-0242ac1100dc0000",
// EntityVersion: "000001588bce5f44-0242ac1101020002",
// EventIds: []string{"000001588bce5f44-0242ac1101020002"}})
//
// schemaLoader = gojsonschema.NewReferenceLoader(
// fmt.Sprintf("file://%s/json_schema_test_suite/create-response.json", workDirectory))
//
// checkCreateResponse, createResponseError := gojsonschema.Validate(schemaLoader, loader)
// assertNoError(t, createResponseError)
//
// assert.Equal(t, true, checkCreateResponse.Valid())
//
// loader = gojsonschema.NewGoLoader(
// UpdateEntityRequest{
// Events: []EventTypeAndData{
// {
// EventType: "net.chrisrichardson.eventstore.example.MyEntityWasUpdated",
// EventData: `"{name": "John"}`}},
// EntityVersion: "000001588bce5f44-0242ac1101020002"})
//
// schemaLoader = gojsonschema.NewReferenceLoader(
// fmt.Sprintf("file://%s/json_schema_test_suite/update-request.json", workDirectory))
//
// checkUpdateRequest, updateRequestError := gojsonschema.Validate(schemaLoader, loader)
// assertNoError(t, updateRequestError)
//
// assert.Equal(t, true, checkUpdateRequest.Valid())
//
// loader = gojsonschema.NewGoLoader(
// &UpdateResponse{
// EntityId: "000001588bce5f47-0242ac1100dc0000",
// EntityVersion: "000001588bce5f44-0242ac1101020002",
// EventIds: []string{"000001588bce5f44-0242ac1101020002"}})
//
// schemaLoader = gojsonschema.NewReferenceLoader(
// fmt.Sprintf("file://%s/json_schema_test_suite/update-response.json", workDirectory))
//
// checkUpdateResponse, updateResponseError := gojsonschema.Validate(schemaLoader, loader)
// assertNoError(t, updateResponseError)
//
// assert.Equal(t, true, checkUpdateResponse.Valid())
//
// loader = gojsonschema.NewGoLoader(
// &GetResponse{
// Events: []EventIdTypeAndData{
// {
// EventType: "net.chrisrichardson.eventstore.example.MyEntityWasCreated",
// EventData: `{"name": "Fred"}`,
// EntityId: "000001588bce5f44-0242ac1101020002"}}})
//
// schemaLoader = gojsonschema.NewReferenceLoader(
// fmt.Sprintf("file://%s/json_schema_test_suite/get-response.json", workDirectory))
//
// checkGetResponse, getResponseError := gojsonschema.Validate(schemaLoader, loader)
// assertNoError(t, getResponseError)
//
// assert.Equal(t, true, checkGetResponse.Valid())
//}