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