1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.jsonschema;
18
19 import static org.apache.juneau.commons.utils.CollectionUtils.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import java.util.*;
23 import java.util.function.*;
24 import java.util.stream.*;
25
26 import org.apache.juneau.*;
27 import org.apache.juneau.commons.reflect.*;
28 import org.apache.juneau.jsonschema.annotation.*;
29 import org.apache.juneau.svl.*;
30 import org.junit.jupiter.api.*;
31
32
33
34
35 class JsonSchemaConfigAnnotationTest extends TestBase {
36
37 private static void check(String expected, Object o) {
38 assertEquals(expected, TO_STRING.apply(o));
39 }
40
41 private static final Function<Object,String> TO_STRING = t -> {
42 if (t instanceof Collection)
43 return ((Collection<?>)t)
44 .stream()
45 .map(JsonSchemaConfigAnnotationTest.TO_STRING)
46 .collect(Collectors.joining(","));
47 if (t instanceof BeanDefMapper)
48 return t.getClass().getSimpleName();
49 return t.toString();
50 };
51
52 static VarResolverSession sr = VarResolver.create().vars(XVar.class).build().createSession();
53
54
55
56
57
58 @JsonSchemaConfig(
59 addDescriptionsTo="$X{BEAN}",
60 addExamplesTo="$X{BEAN}",
61 allowNestedDescriptions="$X{true}",
62 allowNestedExamples="$X{true}",
63 beanDefMapper=BasicBeanDefMapper.class,
64 ignoreTypes="$X{foo}",
65 useBeanDefs="$X{true}"
66 )
67 static class A {}
68 static ClassInfo a = ClassInfo.of(A.class);
69
70 @Test void basic() {
71 var al = AnnotationWorkList.of(sr, rstream(a.getAnnotations()));
72 var x = JsonSchemaGenerator.create().apply(al).build().getSession();
73 check("BEAN", x.getAddDescriptionsTo());
74 check("BEAN", x.getAddExamplesTo());
75 check("true", x.isAllowNestedDescriptions());
76 check("true", x.isAllowNestedExamples());
77 check("BasicBeanDefMapper", x.getBeanDefMapper());
78 check("foo", x.getIgnoreTypes());
79 check("true", x.isUseBeanDefs());
80 }
81
82
83
84
85
86 @JsonSchemaConfig()
87 static class B {}
88 static ClassInfo b = ClassInfo.of(B.class);
89
90 @Test void noValues() {
91 var al = AnnotationWorkList.of(sr, rstream(b.getAnnotations()));
92 var x = JsonSchemaGenerator.create().apply(al).build().getSession();
93 check("", x.getAddDescriptionsTo());
94 check("", x.getAddExamplesTo());
95 check("false", x.isAllowNestedDescriptions());
96 check("false", x.isAllowNestedExamples());
97 check("BasicBeanDefMapper", x.getBeanDefMapper());
98 check("", x.getIgnoreTypes());
99 check("false", x.isUseBeanDefs());
100 }
101
102
103
104
105
106 static class C {}
107 static ClassInfo c = ClassInfo.of(C.class);
108
109 @Test void noAnnotation() {
110 var al = AnnotationWorkList.of(sr, rstream(c.getAnnotations()));
111 var x = JsonSchemaGenerator.create().apply(al).build().getSession();
112 check("", x.getAddDescriptionsTo());
113 check("", x.getAddExamplesTo());
114 check("false", x.isAllowNestedDescriptions());
115 check("false", x.isAllowNestedExamples());
116 check("BasicBeanDefMapper", x.getBeanDefMapper());
117 check("", x.getIgnoreTypes());
118 check("false", x.isUseBeanDefs());
119 }
120 }