View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests the @JsonSchemaConfig annotation.
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  	// Basic tests
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  	// Annotation with no values.
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 	// No annotation.
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 }