fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-codegen / src / test / java / wtf / metio / yosql / codegen / files / DefaultSqlConfigurationParserTest.java
blob5fe154f561cfeba534e2a3d7e918ba345db558d3
1 /*
2 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at https://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
8 package wtf.metio.yosql.codegen.files;
10 import org.junit.jupiter.api.BeforeEach;
11 import org.junit.jupiter.api.DisplayName;
12 import org.junit.jupiter.api.Test;
13 import wtf.metio.yosql.codegen.orchestration.OrchestrationObjectMother;
14 import wtf.metio.yosql.models.configuration.ReturningMode;
15 import wtf.metio.yosql.models.configuration.SqlStatementType;
17 import static org.junit.jupiter.api.Assertions.*;
19 @DisplayName("DefaultSqlConfigurationParser")
20 class DefaultSqlConfigurationParserTest {
22 private DefaultSqlConfigurationParser parser;
24 @BeforeEach
25 void setUp() {
26 parser = new DefaultSqlConfigurationParser(OrchestrationObjectMother.executionErrors());
29 @Test
30 void shouldParseRepository() {
31 final var yaml = """
32 repository: com.example.MyRepository
33 """;
34 final var config = parser.parseConfig(yaml);
35 assertTrue(config.repository().isPresent());
36 assertEquals("com.example.MyRepository", config.repository().get());
39 @Test
40 void shouldParseName() {
41 final var yaml = """
42 name: findItemByName
43 """;
44 final var config = parser.parseConfig(yaml);
45 assertTrue(config.name().isPresent());
46 assertEquals("findItemByName", config.name().get());
49 @Test
50 void shouldParseDescription() {
51 final var yaml = """
52 description: some descriptive message about this statement
53 """;
54 final var config = parser.parseConfig(yaml);
55 assertTrue(config.description().isPresent());
56 assertEquals("some descriptive message about this statement", config.description().get());
59 @Test
60 void shouldParseVendor() {
61 final var yaml = """
62 vendor: Postgres
63 """;
64 final var config = parser.parseConfig(yaml);
65 assertTrue(config.vendor().isPresent());
66 assertEquals("Postgres", config.vendor().get());
69 @Test
70 void shouldParseTypeReading() {
71 final var yaml = """
72 type: READING
73 """;
74 final var config = parser.parseConfig(yaml);
75 assertTrue(config.type().isPresent());
76 assertEquals(SqlStatementType.READING, config.type().get());
79 @Test
80 void shouldParseTypeWriting() {
81 final var yaml = """
82 type: WRITING
83 """;
84 final var config = parser.parseConfig(yaml);
85 assertTrue(config.type().isPresent());
86 assertEquals(SqlStatementType.WRITING, config.type().get());
89 @Test
90 void shouldParseTypeCalling() {
91 final var yaml = """
92 type: CALLING
93 """;
94 final var config = parser.parseConfig(yaml);
95 assertTrue(config.type().isPresent());
96 assertEquals(SqlStatementType.CALLING, config.type().get());
99 @Test
100 void shouldParseTypeReadingCaseInsensitive() {
101 final var yaml = """
102 type: reading
103 """;
104 final var config = parser.parseConfig(yaml);
105 assertTrue(config.type().isPresent());
106 assertEquals(SqlStatementType.READING, config.type().get());
109 @Test
110 void shouldParseTypeWritingCaseInsensitive() {
111 final var yaml = """
112 type: writing
113 """;
114 final var config = parser.parseConfig(yaml);
115 assertTrue(config.type().isPresent());
116 assertEquals(SqlStatementType.WRITING, config.type().get());
119 @Test
120 void shouldParseTypeCallingCaseInsensitive() {
121 final var yaml = """
122 type: calling
123 """;
124 final var config = parser.parseConfig(yaml);
125 assertTrue(config.type().isPresent());
126 assertEquals(SqlStatementType.CALLING, config.type().get());
129 @Test
130 void shouldParseReturningModeNone() {
131 final var yaml = """
132 returning: NONE
133 """;
134 final var config = parser.parseConfig(yaml);
135 assertTrue(config.returningMode().isPresent());
136 assertEquals(ReturningMode.NONE, config.returningMode().get());
139 @Test
140 void shouldParseReturningModeFirst() {
141 final var yaml = """
142 returning: SINGLE
143 """;
144 final var config = parser.parseConfig(yaml);
145 assertTrue(config.returningMode().isPresent());
146 assertEquals(ReturningMode.SINGLE, config.returningMode().get());
149 @Test
150 void shouldParseReturningModeList() {
151 final var yaml = """
152 returning: MULTIPLE
153 """;
154 final var config = parser.parseConfig(yaml);
155 assertTrue(config.returningMode().isPresent());
156 assertEquals(ReturningMode.MULTIPLE, config.returningMode().get());
159 @Test
160 void shouldParseReturningModeNoneCaseInsensitive() {
161 final var yaml = """
162 returning: none
163 """;
164 final var config = parser.parseConfig(yaml);
165 assertTrue(config.returningMode().isPresent());
166 assertEquals(ReturningMode.NONE, config.returningMode().get());
169 @Test
170 void shouldParseReturningModeFirstCaseInsensitive() {
171 final var yaml = """
172 returning: single
173 """;
174 final var config = parser.parseConfig(yaml);
175 assertTrue(config.returningMode().isPresent());
176 assertEquals(ReturningMode.SINGLE, config.returningMode().get());
179 @Test
180 void shouldParseReturningModeListCaseInsensitive() {
181 final var yaml = """
182 returning: multiple
183 """;
184 final var config = parser.parseConfig(yaml);
185 assertTrue(config.returningMode().isPresent());
186 assertEquals(ReturningMode.MULTIPLE, config.returningMode().get());
189 @Test
190 void shouldParseGenerateStandardApiEnabled() {
191 final var yaml = """
192 generateStandardApi: true
193 """;
194 final var config = parser.parseConfig(yaml);
195 assertTrue(config.generateStandardApi().isPresent());
196 assertTrue(config.generateStandardApi().get());
199 @Test
200 void shouldParseGenerateStandardApiDisabled() {
201 final var yaml = """
202 generateStandardApi: false
203 """;
204 final var config = parser.parseConfig(yaml);
205 assertTrue(config.generateStandardApi().isPresent());
206 assertFalse(config.generateStandardApi().get());
209 @Test
210 void shouldParseGenerateBatchApiEnabled() {
211 final var yaml = """
212 generateBatchApi: true
213 """;
214 final var config = parser.parseConfig(yaml);
215 assertTrue(config.generateBatchApi().isPresent());
216 assertTrue(config.generateBatchApi().get());
219 @Test
220 void shouldParseGenerateBatchApiDisabled() {
221 final var yaml = """
222 generateBatchApi: false
223 """;
224 final var config = parser.parseConfig(yaml);
225 assertTrue(config.generateBatchApi().isPresent());
226 assertFalse(config.generateBatchApi().get());
229 @Test
230 void shouldParseCatchAndRethrowEnabled() {
231 final var yaml = """
232 catchAndRethrow: true
233 """;
234 final var config = parser.parseConfig(yaml);
235 assertTrue(config.catchAndRethrow().isPresent());
236 assertTrue(config.catchAndRethrow().get());
239 @Test
240 void shouldParseCatchAndRethrowDisabled() {
241 final var yaml = """
242 catchAndRethrow: false
243 """;
244 final var config = parser.parseConfig(yaml);
245 assertTrue(config.catchAndRethrow().isPresent());
246 assertFalse(config.catchAndRethrow().get());
249 @Test
250 void shouldParseInjectConvertersEnabled() {
251 final var yaml = """
252 injectConverters: true
253 """;
254 final var config = parser.parseConfig(yaml);
255 assertTrue(config.injectConverters().isPresent());
256 assertTrue(config.injectConverters().get());
259 @Test
260 void shouldParseInjectConvertersDisabled() {
261 final var yaml = """
262 injectConverters: false
263 """;
264 final var config = parser.parseConfig(yaml);
265 assertTrue(config.injectConverters().isPresent());
266 assertFalse(config.injectConverters().get());
269 @Test
270 void shouldParseStandardPrefix() {
271 final var yaml = """
272 standardPrefix: prefix
273 """;
274 final var config = parser.parseConfig(yaml);
275 assertTrue(config.standardPrefix().isPresent());
276 assertEquals("prefix", config.standardPrefix().get());
279 @Test
280 void shouldParseStandardSuffix() {
281 final var yaml = """
282 standardSuffix: suffix
283 """;
284 final var config = parser.parseConfig(yaml);
285 assertTrue(config.standardSuffix().isPresent());
286 assertEquals("suffix", config.standardSuffix().get());
289 @Test
290 void shouldParseBatchPrefix() {
291 final var yaml = """
292 batchPrefix: prefix
293 """;
294 final var config = parser.parseConfig(yaml);
295 assertTrue(config.batchPrefix().isPresent());
296 assertEquals("prefix", config.batchPrefix().get());
299 @Test
300 void shouldParseBatchSuffix() {
301 final var yaml = """
302 batchSuffix: suffix
303 """;
304 final var config = parser.parseConfig(yaml);
305 assertTrue(config.batchSuffix().isPresent());
306 assertEquals("suffix", config.batchSuffix().get());
309 @Test
310 void shouldParseParameter() {
311 final var yaml = """
312 parameters:
313 - name: name
314 type: java.lang.String
315 """;
316 final var config = parser.parseConfig(yaml);
317 assertEquals(1, config.parameters().size());
318 assertAll(
319 () -> assertEquals("name", config.parameters().get(0).name().get()),
320 () -> assertEquals("java.lang.String", config.parameters().get(0).type().get()));
323 @Test
324 void shouldParseParameters() {
325 final var yaml = """
326 parameters:
327 - name: string
328 type: java.lang.String
329 - name: bool
330 type: boolean
331 - name: number
332 type: int
333 """;
334 final var config = parser.parseConfig(yaml);
335 assertEquals(3, config.parameters().size());
336 assertAll(
337 () -> assertEquals("string", config.parameters().get(0).name().get()),
338 () -> assertEquals("java.lang.String", config.parameters().get(0).type().get()),
339 () -> assertEquals("bool", config.parameters().get(1).name().get()),
340 () -> assertEquals("boolean", config.parameters().get(1).type().get()),
341 () -> assertEquals("number", config.parameters().get(2).name().get()),
342 () -> assertEquals("int", config.parameters().get(2).type().get()));
345 @Test
346 void shouldParseResultRowConverterAlias() {
347 final var yaml = """
348 resultRowConverter:
349 alias: converterAlias
350 """;
351 final var config = parser.parseConfig(yaml);
352 assertTrue(config.resultRowConverter().isPresent());
353 assertAll(
354 () -> assertEquals("converterAlias", config.resultRowConverter().get().alias().get()),
355 () -> assertTrue(config.resultRowConverter().get().methodName().isEmpty()),
356 () -> assertTrue(config.resultRowConverter().get().converterType().isEmpty()),
357 () -> assertTrue(config.resultRowConverter().get().resultType().isEmpty()));
360 @Test
361 void shouldParseResultRowConverterMethodName() {
362 final var yaml = """
363 resultRowConverter:
364 methodName: someMethod
365 """;
366 final var config = parser.parseConfig(yaml);
367 assertTrue(config.resultRowConverter().isPresent());
368 assertAll(
369 () -> assertTrue(config.resultRowConverter().get().alias().isEmpty()),
370 () -> assertEquals("someMethod", config.resultRowConverter().get().methodName().get()),
371 () -> assertTrue(config.resultRowConverter().get().converterType().isEmpty()),
372 () -> assertTrue(config.resultRowConverter().get().resultType().isEmpty()));
375 @Test
376 void shouldParseResultRowConverterConverterType() {
377 final var yaml = """
378 resultRowConverter:
379 converterType: com.example.MyConverter
380 """;
381 final var config = parser.parseConfig(yaml);
382 assertTrue(config.resultRowConverter().isPresent());
383 assertAll(
384 () -> assertTrue(config.resultRowConverter().get().alias().isEmpty()),
385 () -> assertTrue(config.resultRowConverter().get().methodName().isEmpty()),
386 () -> assertEquals("com.example.MyConverter", config.resultRowConverter().get().converterType().get()),
387 () -> assertTrue(config.resultRowConverter().get().resultType().isEmpty()));
390 @Test
391 void shouldParseResultRowConverterResultType() {
392 final var yaml = """
393 resultRowConverter:
394 resultType: com.example.MyResult
395 """;
396 final var config = parser.parseConfig(yaml);
397 assertTrue(config.resultRowConverter().isPresent());
398 assertAll(
399 () -> assertTrue(config.resultRowConverter().get().alias().isEmpty()),
400 () -> assertTrue(config.resultRowConverter().get().methodName().isEmpty()),
401 () -> assertTrue(config.resultRowConverter().get().converterType().isEmpty()),
402 () -> assertEquals("com.example.MyResult", config.resultRowConverter().get().resultType().get()));
405 @Test
406 void shouldParseResultRowConverter() {
407 final var yaml = """
408 resultRowConverter:
409 alias: converterAlias
410 methodName: someMethod
411 converterType: com.example.MyConverter
412 resultType: com.example.MyResult
413 """;
414 final var config = parser.parseConfig(yaml);
415 assertTrue(config.resultRowConverter().isPresent());
416 assertAll(
417 () -> assertEquals("converterAlias", config.resultRowConverter().get().alias().get()),
418 () -> assertEquals("someMethod", config.resultRowConverter().get().methodName().get()),
419 () -> assertEquals("com.example.MyConverter", config.resultRowConverter().get().converterType().get()),
420 () -> assertEquals("com.example.MyResult", config.resultRowConverter().get().resultType().get()));
423 @Test
424 void shouldParseAnnotation() {
425 final var yaml = """
426 annotations:
427 - type: com.example.MyAnnotation
428 members:
429 - key: some
430 value: here
431 type: java.lang.String
432 """;
433 final var config = parser.parseConfig(yaml);
434 assertFalse(config.annotations().isEmpty());
435 assertAll("annotation",
436 () -> assertEquals("com.example.MyAnnotation", config.annotations().get(0).type()),
437 () -> assertAll("members",
438 () -> assertFalse(config.annotations().get(0).members().isEmpty()),
439 () -> assertEquals("some", config.annotations().get(0).members().get(0).key()),
440 () -> assertEquals("here", config.annotations().get(0).members().get(0).value()),
441 () -> assertEquals("java.lang.String", config.annotations().get(0).members().get(0).type())));
444 @Test
445 void shouldParseAnnotations() {
446 final var yaml = """
447 annotations:
448 - type: com.example.MyAnnotation
449 members:
450 - key: some
451 value: here
452 type: java.lang.String
453 - type: com.example.OtherAnnotation
454 members:
455 - key: another
456 value: 12345
457 type: int
458 """;
459 final var config = parser.parseConfig(yaml);
460 assertFalse(config.annotations().isEmpty());
461 assertAll("annotation",
462 () -> assertEquals("com.example.MyAnnotation", config.annotations().get(0).type()),
463 () -> assertEquals("com.example.OtherAnnotation", config.annotations().get(1).type()),
464 () -> assertAll("members",
465 () -> assertFalse(config.annotations().get(0).members().isEmpty()),
466 () -> assertEquals("some", config.annotations().get(0).members().get(0).key()),
467 () -> assertEquals("here", config.annotations().get(0).members().get(0).value()),
468 () -> assertEquals("java.lang.String", config.annotations().get(0).members().get(0).type()),
469 () -> assertEquals("another", config.annotations().get(1).members().get(0).key()),
470 () -> assertEquals("12345", config.annotations().get(1).members().get(0).value()),
471 () -> assertEquals("int", config.annotations().get(1).members().get(0).type())));
474 @Test
475 void shouldParseAnnotationMembers() {
476 final var yaml = """
477 annotations:
478 - type: com.example.MyAnnotation
479 members:
480 - key: some
481 value: here
482 type: java.lang.String
483 - key: another
484 value: 12345
485 type: int
486 """;
487 final var config = parser.parseConfig(yaml);
488 assertFalse(config.annotations().isEmpty());
489 assertAll("annotation",
490 () -> assertEquals("com.example.MyAnnotation", config.annotations().get(0).type()),
491 () -> assertAll("members",
492 () -> assertFalse(config.annotations().get(0).members().isEmpty()),
493 () -> assertEquals("some", config.annotations().get(0).members().get(0).key()),
494 () -> assertEquals("here", config.annotations().get(0).members().get(0).value()),
495 () -> assertEquals("java.lang.String", config.annotations().get(0).members().get(0).type()),
496 () -> assertEquals("another", config.annotations().get(0).members().get(1).key()),
497 () -> assertEquals("12345", config.annotations().get(0).members().get(1).value()),
498 () -> assertEquals("int", config.annotations().get(0).members().get(1).type())));