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.examples.rest;
18  
19  import static org.junit.Assert.*;
20  import static org.junit.runners.MethodSorters.*;
21  
22  import org.apache.juneau.bean.swagger.*;
23  import org.apache.juneau.collections.*;
24  import org.apache.juneau.html.*;
25  import org.apache.juneau.json.*;
26  import org.apache.juneau.rest.beans.*;
27  import org.apache.juneau.rest.client.*;
28  import org.junit.*;
29  
30  @FixMethodOrder(NAME_ASCENDING)
31  public class RootResourcesTest extends RestTestcase {
32  
33  	private static boolean debug;
34  
35  	private RestClient jsonClient = SamplesMicroservice.DEFAULT_CLIENT;
36  
37  	//====================================================================================================
38  	// text/json
39  	//====================================================================================================
40  	@Test
41  	public void testJson() throws Exception {
42  		var client = SamplesMicroservice.DEFAULT_CLIENT;
43  
44  		var x = client.get("").run().getContent().as(ResourceDescription[].class);
45  		assertEquals("helloWorld", x[0].getName());
46  		assertEquals("Hello World", x[0].getDescription());
47  
48  		var x2 = jsonClient.get("api").run().getContent().as(JsonMap.class);
49  		var s = x2.getMap("info").getString("description");
50  		if (debug)
51  			System.err.println(s);
52  		assertTrue(s, s.startsWith("Example of a router resource page"));
53  	}
54  
55  	//====================================================================================================
56  	// text/xml
57  	//====================================================================================================
58  	@Test
59  	public void testXml() throws Exception {
60  		try (var client = SamplesMicroservice.client().xml().build()) {
61  
62  			var x = client.get("").run().getContent().as(ResourceDescription[].class);
63  			assertEquals("helloWorld", x[0].getName());
64  			assertEquals("Hello World", x[0].getDescription());
65  
66  			var x2 = jsonClient.get("api").run().getContent().as(JsonMap.class);
67  			var s = x2.getMap("info").getString("description");
68  			if (debug)
69  				System.err.println(s);
70  			assertTrue(s, s.startsWith("Example of a router resource page"));
71  		}
72  	}
73  
74  	//====================================================================================================
75  	// text/html+stripped
76  	//====================================================================================================
77  	@Test
78  	public void testHtmlStripped() throws Exception {
79  		try (RestClient client = SamplesMicroservice.client().parsers(HtmlParser.DEFAULT).accept("text/html+stripped").build()) {
80  
81  			var x = client.get("").run().getContent().as(ResourceDescription[].class);
82  			assertEquals("helloWorld", x[0].getName());
83  			assertEquals("Hello World", x[0].getDescription());
84  
85  			var x2 = jsonClient.get("api").run().getContent().as(JsonMap.class);
86  			var s = x2.getMap("info").getString("description");
87  			if (debug)
88  				System.err.println(s);
89  			assertTrue(s, s.startsWith("Example of a router resource page"));
90  		}
91  	}
92  
93  	//====================================================================================================
94  	// application/json+schema
95  	//====================================================================================================
96  	@Test
97  	public void testJsonSchema() throws Exception {
98  		try (RestClient client = SamplesMicroservice.client().parsers(JsonParser.DEFAULT).accept("text/json+schema").build()) {
99  			var m = client.get("").run().getContent().as(JsonMap.class);
100 			if (debug)
101 				System.err.println(m);
102 			client.closeQuietly();
103 		}
104 	}
105 
106 	//====================================================================================================
107 	// OPTIONS page
108 	//====================================================================================================
109 	@Test
110 	public void testOptionsPage() throws Exception {
111 		Swagger o = jsonClient.get("api").run().getContent().as(Swagger.class);
112 		if (debug)
113 			System.err.println(o);
114 		assertEquals("Example of a router resource page.", o.getInfo().getDescription());
115 	}
116 }