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