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  	//====================================================================================================
39  	// text/json
40  	//====================================================================================================
41  	@Test
42  	public void testJson() throws Exception {
43  		RestClient client = SamplesMicroservice.DEFAULT_CLIENT;
44  
45  		ResourceDescription[] x = client.get("").run().getContent().as(ResourceDescription[].class);
46  		assertEquals("helloWorld", x[0].getName());
47  		assertEquals("Hello World", x[0].getDescription());
48  
49  		JsonMap x2 = jsonClient.get("api").run().getContent().as(JsonMap.class);
50  		String s = x2.getMap("info").getString("description");
51  		if (debug) 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 (RestClient client = SamplesMicroservice.client().xml().build()) {
61  
62  			ResourceDescription[] x = client.get("").run().getContent().as(ResourceDescription[].class);
63  			assertEquals("helloWorld", x[0].getName());
64  			assertEquals("Hello World", x[0].getDescription());
65  
66  			JsonMap x2 = jsonClient.get("api").run().getContent().as(JsonMap.class);
67  			String s = x2.getMap("info").getString("description");
68  			if (debug) System.err.println(s);
69  			assertTrue(s, s.startsWith("Example of a router resource page"));
70  		}
71  	}
72  
73  	//====================================================================================================
74  	// text/html+stripped
75  	//====================================================================================================
76  	@Test
77  	public void testHtmlStripped() throws Exception {
78  		try (RestClient client = SamplesMicroservice.client().parsers(HtmlParser.DEFAULT).accept("text/html+stripped").build()) {
79  
80  			ResourceDescription[] x = client.get("").run().getContent().as(ResourceDescription[].class);
81  			assertEquals("helloWorld", x[0].getName());
82  			assertEquals("Hello World", x[0].getDescription());
83  
84  			JsonMap x2 = jsonClient.get("api").run().getContent().as(JsonMap.class);
85  			String s = x2.getMap("info").getString("description");
86  			if (debug) System.err.println(s);
87  			assertTrue(s, s.startsWith("Example of a router resource page"));
88  		}
89  	}
90  
91  	//====================================================================================================
92  	// application/json+schema
93  	//====================================================================================================
94  	@Test
95  	public void testJsonSchema() throws Exception {
96  		try (RestClient client = SamplesMicroservice.client().parsers(JsonParser.DEFAULT).accept("text/json+schema").build()) {
97  			JsonMap m = client.get("").run().getContent().as(JsonMap.class);
98  			if (debug) System.err.println(m);
99  			client.closeQuietly();
100 		}
101 	}
102 
103 	//====================================================================================================
104 	// OPTIONS page
105 	//====================================================================================================
106 	@Test
107 	public void testOptionsPage() throws Exception {
108 		Swagger o = jsonClient.get("api").run().getContent().as(Swagger.class);
109 		if (debug) System.err.println(o);
110 		assertEquals("Example of a router resource page.", o.getInfo().getDescription());
111 	}
112 }