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.rest.beans;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.net.*;
23  import java.util.*;
24  
25  import org.apache.http.*;
26  import org.apache.juneau.*;
27  import org.apache.juneau.http.*;
28  import org.apache.juneau.http.header.*;
29  import org.junit.jupiter.api.*;
30  
31  class SeeOtherRoot_Test extends TestBase {
32  
33  	@Test void a01_basic() {
34  		var x = new SeeOtherRoot();
35  		assertNotNull(x);
36  	}
37  
38  	@Test void a02_withContent() {
39  		var x = new SeeOtherRoot("test content");
40  		assertNotNull(x);
41  	}
42  
43  	@Test void a03_fluentSetters() throws Exception {
44  		var x = new SeeOtherRoot();
45  
46  		// Test setContent(String) returns same instance
47  		assertSame(x, x.setContent("test content"));
48  
49  		// Test setContent(HttpEntity) returns same instance
50  		HttpEntity entity = x.getEntity();
51  		assertSame(x, x.setContent(entity));
52  
53  		// Test setHeader2(Header) returns same instance
54  		assertSame(x, x.setHeader2(BasicHeader.of("X-Test", "test-value")));
55  
56  		// Test setHeader2(String, String) returns same instance
57  		assertSame(x, x.setHeader2("X-Test2", "test-value2"));
58  
59  		// Test setHeaders(List<Header>) returns same instance
60  		List<Header> headerList = l(BasicHeader.of("X-Header1", "value1"));
61  		assertSame(x, x.setHeaders(headerList));
62  		assertEquals("value1", x.getFirstHeader("X-Header1").getValue());
63  
64  		// Test setHeaders(HeaderList) returns same instance
65  		var headers = HeaderList.of(BasicHeader.of("X-Header2", "value2"));
66  		assertSame(x, x.setHeaders(headers));
67  
68  		// Test setHeaders2(Header...) returns same instance
69  		assertSame(x, x.setHeaders2(BasicHeader.of("X-Header3", "value3")));
70  
71  		// Test setLocale2 returns same instance
72  		assertSame(x, x.setLocale2(Locale.US));
73  
74  		// Test setLocation(String) returns same instance
75  		assertSame(x, x.setLocation("servlet:/newpath"));
76  
77  		// Test setLocation(URI) returns same instance
78  		assertSame(x, x.setLocation(new URI("http://example.com")));
79  
80  		// Test setProtocolVersion returns same instance
81  		assertSame(x, x.setProtocolVersion(new ProtocolVersion("HTTP", 1, 1)));
82  
83  		// Test setReasonPhrase2 returns same instance
84  		assertSame(x, x.setReasonPhrase2("Custom Reason"));
85  
86  		// Test setReasonPhraseCatalog returns same instance
87  		assertSame(x, x.setReasonPhraseCatalog(null));
88  
89  		// Test setStatusCode2 returns same instance
90  		assertSame(x, x.setStatusCode2(303));
91  
92  		// Test setStatusLine returns same instance
93  		assertSame(x, x.setStatusLine(BasicStatusLine.create(303, "See Other")));
94  
95  		// Test setUnmodifiable returns same instance (must be last - makes bean read-only)
96  		assertSame(x, x.setUnmodifiable());
97  	}
98  
99  	@Test void a04_fluentChaining() throws Exception {
100 		// Test multiple fluent calls can be chained
101 		var x = new SeeOtherRoot()
102 			.setHeaders(l(BasicHeader.of("X-Chain", "chained")))
103 			.setContent("Redirect content");
104 
105 		assertEquals("chained", x.getFirstHeader("X-Chain").getValue());
106 	}
107 
108 	@Test void a05_reusableInstance() {
109 		// Test that INSTANCE is available and usable
110 		assertNotNull(SeeOtherRoot.INSTANCE);
111 	}
112 
113 	@Test void a06_copy() throws Exception {
114 		// Test that copy() returns correct type
115 		var x = new SeeOtherRoot();
116 
117 		SeeOtherRoot copy = x.copy();
118 
119 		// Verify it's a different instance
120 		assertNotSame(x, copy);
121 
122 		// Verify it returns the correct type (not SeeOther)
123 		assertInstanceOf(SeeOtherRoot.class, copy);
124 
125 		// Verify location is copied (default is servlet:/)
126 		assertNotNull(copy.getFirstHeader("Location"));
127 		assertTrue(copy.getFirstHeader("Location").getValue().contains("servlet:/"));
128 	}
129 }