6
6
import org .junit .jupiter .api .Test ;
7
7
8
8
import static com .relogiclabs .json .schema .message .ErrorCode .DTYP04 ;
9
+ import static com .relogiclabs .json .schema .positive .ExternalFunctions .ERRACCESS01 ;
9
10
import static org .junit .jupiter .api .Assertions .assertEquals ;
10
11
import static org .junit .jupiter .api .Assertions .assertThrows ;
11
12
12
13
public class AggregatedTests {
13
-
14
14
@ Test
15
15
public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown () {
16
- var schema =
17
- """
16
+ var schema = """
18
17
%title: "User Profile Response"
19
18
%version: 1.0.0
20
19
%schema:
@@ -41,8 +40,7 @@ public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown() {
41
40
}
42
41
}
43
42
""" ;
44
- var json =
45
- """
43
+ var json = """
46
44
{
47
45
"user": {
48
46
"id": "not number",
@@ -70,4 +68,166 @@ public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown() {
70
68
assertEquals (DTYP04 , exception .getCode ());
71
69
exception .printStackTrace ();
72
70
}
71
+
72
+ @ Test
73
+ public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown () {
74
+ var schema = """
75
+ %title: "Extended User Profile Dashboard API Response"
76
+ %version: 2.0.0
77
+ %include: com.relogiclabs.json.schema.positive.ExternalFunctions
78
+ %pragma IgnoreUndefinedProperties: true
79
+
80
+ %define $post: {
81
+ "id": @range(1, 1000) #integer,
82
+ "title": @length(10, 100) #string,
83
+ "content": @length(30, 1000) #string,
84
+ "tags": $tags
85
+ } #object
86
+ %define $product: {
87
+ "id": @length(2, 10) @regex("[a-z][a-z0-9]+") #string,
88
+ "name": @length(5, 30) #string,
89
+ "brand": @length(5, 30) #string,
90
+ "price": @range(0.1, 1000000),
91
+ "inStock": #boolean,
92
+ "specs": {
93
+ "cpu": @length(5, 30) #string,
94
+ "ram": @regex("[0-9]{1,2}GB") #string,
95
+ "storage": @regex("[0-9]{1,4}GB (SSD|HDD)") #string
96
+ } #object #null
97
+ }
98
+ %define $tags: @length(1, 10) #string*($tag) #array
99
+ %define $tag: @length(3, 20) @regex("[A-Za-z_]+") #string
100
+ %schema:
101
+ {
102
+ "user": {
103
+ "id": @range(1, 10000) #integer,
104
+ /*username does not allow special characters*/
105
+ "username": @regex("[a-z_]{3,30}") #string,
106
+ "role": @enum("user", "admin") #string &role,
107
+ "isActive": #boolean, //user account current status
108
+ "registeredAt": @time("DD-MM-YYYY hh:mm:ss") #string,
109
+ "dataAccess": @checkAccess(&role) #integer,
110
+ "profile": {
111
+ "firstName": @regex("[A-Za-z]{3,50}") #string,
112
+ "lastName": @regex("[A-Za-z]{3,50}") #string,
113
+ "dateOfBirth": @date("DD-MM-YYYY") #string,
114
+ "age": @range(18, 128) #integer,
115
+ "email": @email #string,
116
+ "pictureURL": @url #string,
117
+ "address": {
118
+ "street": @length(10, 200) #string,
119
+ "city": @length(3, 50) #string,
120
+ "country": @regex("[A-Za-z ]{3,50}") #string
121
+ } #object #null,
122
+ "hobbies": !?
123
+ },
124
+ "posts": @length(0, 1000) #object*($post) #array,
125
+ "preferences": {
126
+ "theme": @enum("light", "dark") #string,
127
+ "fontSize": @range(9, 24) #integer,
128
+ "autoSave": #boolean
129
+ }
130
+ },
131
+ "products": #object*($product) #array,
132
+ "weather": {
133
+ "temperature": @range(-50, 60) #integer #float,
134
+ "isCloudy": #boolean
135
+ }
136
+ }
137
+ """ ;
138
+ var json = """
139
+ {
140
+ "user": {
141
+ "id": 1234,
142
+ "username": "johndoe",
143
+ "role": "user",
144
+ "isActive": true,
145
+ "registeredAt": "06-09-2023 15:10:30",
146
+ "dataAccess": 6,
147
+ "profile": {
148
+ "firstName": "John",
149
+ "lastName": "Doe",
150
+ "dateOfBirth": "17-06-1993",
151
+ "age": 30,
152
+
153
+ "pictureURL": "https://example.com/picture.jpg",
154
+ "address": {
155
+ "street": "123 Some St",
156
+ "city": "Some town",
157
+ "country": "Some Country"
158
+ }
159
+ },
160
+ "posts": [
161
+ {
162
+ "id": 1,
163
+ "title": "Introduction to JSON",
164
+ "content": "JSON (JavaScript Object Notation) is a lightweight data interchange format...",
165
+ "tags": [
166
+ "JSON",
167
+ "tutorial",
168
+ "data"
169
+ ]
170
+ },
171
+ {
172
+ "id": 2,
173
+ "title": "Working with JSON in C#",
174
+ "content": "C# provides built-in support for working with JSON...",
175
+ "tags": [
176
+ "CSharp",
177
+ "JSON",
178
+ "tutorial"
179
+ ]
180
+ },
181
+ {
182
+ "id": 3,
183
+ "title": "Introduction to JSON Schema",
184
+ "content": "A JSON schema defines the structure and data types of JSON objects...",
185
+ "tags": [
186
+ "Schema",
187
+ "JSON",
188
+ "tutorial"
189
+ ]
190
+ }
191
+ ],
192
+ "preferences": {
193
+ "theme": "dark",
194
+ "fontSize": 14,
195
+ "autoSave": true
196
+ }
197
+ },
198
+ "products": [
199
+ {
200
+ "id": "p1",
201
+ "name": "Smartphone",
202
+ "brand": "TechGiant",
203
+ "price": 599.99,
204
+ "inStock": true,
205
+ "specs": null
206
+ },
207
+ {
208
+ "id": "p2",
209
+ "name": "Laptop",
210
+ "brand": "SuperTech",
211
+ "price": 1299.99,
212
+ "inStock": false,
213
+ "specs": {
214
+ "cpu": "Intel i11",
215
+ "ram": "11GB",
216
+ "storage": "11GB SSD"
217
+ }
218
+ }
219
+ ],
220
+ "weather": {
221
+ "temperature": 25.5,
222
+ "isCloudy": false,
223
+ "conditions": null
224
+ }
225
+ }
226
+ """ ;
227
+ JsonSchema .isValid (schema , json );
228
+ var exception = assertThrows (JsonSchemaException .class ,
229
+ () -> JsonAssert .isValid (schema , json ));
230
+ assertEquals (ERRACCESS01 , exception .getCode ());
231
+ exception .printStackTrace ();
232
+ }
73
233
}
0 commit comments