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.http.annotation;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.apache.juneau.junit.bct.BctAssertions.*;
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  import org.apache.juneau.*;
24  import org.apache.juneau.annotation.*;
25  import org.apache.juneau.oapi.*;
26  import org.junit.jupiter.api.*;
27  
28  class ResponseAnnotation_Test extends TestBase {
29  
30  	private static final String CNAME = ResponseAnnotation_Test.class.getName();
31  
32  	public static class X1 {}
33  
34  	//------------------------------------------------------------------------------------------------------------------
35  	// Basic tests
36  	//------------------------------------------------------------------------------------------------------------------
37  
38  	Response a1 = ResponseAnnotation.create()
39  		.description("a")
40  		.examples("b")
41  		.headers(HeaderAnnotation.DEFAULT)
42  		.on("c")
43  		.onClass(X1.class)
44  		.parser(OpenApiParser.class)
45  		.schema(SchemaAnnotation.DEFAULT)
46  		.serializer(OpenApiSerializer.class)
47  		.build();
48  
49  	Response a2 = ResponseAnnotation.create()
50  		.description("a")
51  		.examples("b")
52  		.headers(HeaderAnnotation.DEFAULT)
53  		.on("c")
54  		.onClass(X1.class)
55  		.parser(OpenApiParser.class)
56  		.schema(SchemaAnnotation.DEFAULT)
57  		.serializer(OpenApiSerializer.class)
58  		.build();
59  
60  	@Test void a01_basic() {
61  		assertBean(
62  			a1,
63  			"description,examples,on,onClass{#{simpleName}},parser{simpleName},serializer{simpleName}",
64  			"[a],[b],[c],{[{X1}]},{OpenApiParser},{OpenApiSerializer}"
65  		);
66  	}
67  
68  	@Test void a02_testEquivalency() {
69  		assertEquals(a2, a1);
70  		assertNotEqualsAny(a1.hashCode(), 0, -1);
71  		assertEquals(a1.hashCode(), a2.hashCode());
72  	}
73  
74  	//------------------------------------------------------------------------------------------------------------------
75  	// PropertyStore equivalency.
76  	//------------------------------------------------------------------------------------------------------------------
77  
78  	@Test void b01_testEquivalencyInPropertyStores() {
79  		var bc1 = BeanContext.create().annotations(a1).build();
80  		var bc2 = BeanContext.create().annotations(a2).build();
81  		assertSame(bc1, bc2);
82  	}
83  
84  	//------------------------------------------------------------------------------------------------------------------
85  	// Other methods.
86  	//------------------------------------------------------------------------------------------------------------------
87  
88  	public static class C1 {
89  		public int f1;
90  		public void m1() {}  // NOSONAR
91  	}
92  	public static class C2 {
93  		public int f2;
94  		public void m2() {}  // NOSONAR
95  	}
96  
97  	@Test void c01_otherMethods() throws Exception {
98  		var c1 = ResponseAnnotation.create(C1.class).on(C2.class).build();
99  		var c2 = ResponseAnnotation.create("a").on("b").build();
100 		var c4 = ResponseAnnotation.create().on(C1.class.getMethod("m1")).on(C2.class.getMethod("m2")).build();
101 
102 		assertBean(c1, "on", "["+CNAME+"$C1,"+CNAME+"$C2]");
103 		assertBean(c2, "on", "[a,b]");
104 		assertBean(c4, "on", "["+CNAME+"$C1.m1(),"+CNAME+"$C2.m2()]");
105 	}
106 
107 	//------------------------------------------------------------------------------------------------------------------
108 	// Comparison with declared annotations.
109 	//------------------------------------------------------------------------------------------------------------------
110 
111 	@Response(
112 		description={ "a" },
113 		examples="b",
114 		headers=@Header,
115 		on="c",
116 		onClass=X1.class,
117 		parser=OpenApiParser.class,
118 		schema=@Schema,
119 		serializer=OpenApiSerializer.class
120 	)
121 	public static class D1 {}
122 	Response d1 = D1.class.getAnnotationsByType(Response.class)[0];
123 
124 	@Response(
125 		description={ "a" },
126 		examples="b",
127 		headers=@Header,
128 		on="c",
129 		onClass=X1.class,
130 		parser=OpenApiParser.class,
131 		schema=@Schema,
132 		serializer=OpenApiSerializer.class
133 	)
134 	public static class D2 {}
135 	Response d2 = D2.class.getAnnotationsByType(Response.class)[0];
136 
137 	@Test void d01_comparisonWithDeclarativeAnnotations() {
138 		assertEqualsAll(a1, d1, d2);
139 		assertNotEqualsAny(a1.hashCode(), 0, -1);
140 		assertEqualsAll(a1.hashCode(), d1.hashCode(), d2.hashCode());
141 	}
142 }