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.apache.juneau.examples.rest.TestUtils.*;
20  import static org.apache.juneau.internal.CollectionUtils.*;
21  import static org.junit.runners.MethodSorters.*;
22  
23  import java.io.*;
24  import java.util.*;
25  import java.util.function.*;
26  
27  import org.apache.juneau.*;
28  import org.apache.juneau.csv.*;
29  import org.apache.juneau.html.*;
30  import org.apache.juneau.json.*;
31  import org.apache.juneau.msgpack.*;
32  import org.apache.juneau.parser.*;
33  import org.apache.juneau.plaintext.*;
34  import org.apache.juneau.rest.client.*;
35  import org.apache.juneau.serializer.*;
36  import org.apache.juneau.uon.*;
37  import org.apache.juneau.urlencoding.*;
38  import org.apache.juneau.xml.*;
39  import org.junit.*;
40  
41  @FixMethodOrder(NAME_ASCENDING)
42  @SuppressWarnings("unchecked")
43  public class ContentComboTestBase extends RestTestcase {
44  
45  	// Reusable RestClients keyed by label that live for the duration of a testcase class.
46  	private static Map<String,RestClient> clients = map();
47  
48  	protected RestClient getClient(MediaType mediaType) {
49  		String mt = mediaType.toString();
50  		switch (mt) {
51  			case "text/csv": return getClient(mt, CsvSerializer.DEFAULT, CsvParser.DEFAULT);
52  			case "text/html": return getClient(mt, HtmlSerializer.DEFAULT, HtmlParser.DEFAULT);
53  			case "application/json": return getClient(mt, JsonSerializer.DEFAULT, JsonParser.DEFAULT);
54  			case "octal/msgpack": return getClient(mt, MsgPackSerializer.DEFAULT, MsgPackParser.DEFAULT, x -> x.queryData("plainText","true"));
55  			case "text/plain": return getClient(mt, PlainTextSerializer.DEFAULT, PlainTextParser.DEFAULT);
56  			case "text/uon": return getClient(mt, UonSerializer.DEFAULT, UonParser.DEFAULT);
57  			case "application/x-www-form-urlencoded": return getClient(mt, UrlEncodingSerializer.DEFAULT, UrlEncodingParser.DEFAULT);
58  			case "text/xml": return getClient(mt, XmlSerializer.DEFAULT, XmlParser.DEFAULT);
59  			default: throw new BasicRuntimeException("Client for mediaType ''{0}'' not found", mt);
60  		}
61  	}
62  
63  	protected RestClient getClient(String label, Serializer serializer, Parser parser, Consumer<RestClient.Builder>...postApply) {
64  		if (! clients.containsKey(label)) {
65  			RestClient.Builder b = SamplesMicroservice.client(serializer, parser);
66  			for (Consumer<RestClient.Builder> c : postApply)
67  				c.accept(b);
68  			clients.put(label, b.build());
69  		}
70  		return clients.get(label);
71  	}
72  
73  	@AfterClass
74  	public static void tearDown() {
75  		clients.values().forEach(rc -> {
76  			try {
77  				rc.close();
78  			} catch (IOException e) {
79  				e.printStackTrace();
80  			}
81  		});
82  		clients.clear();
83  	}
84  
85  	public static class ComboInput {
86  		public final String name, url;
87  		public final MediaType mediaType;
88  		public final String[] expectedResults;
89  
90  		public ComboInput(String name, String url, MediaType mediaType, String...expectedResults) {
91  			this.name = name;
92  			this.url = url;
93  			this.mediaType = mediaType;
94  			this.expectedResults = expectedResults;
95  		}
96  	}
97  
98  
99  	private final ComboInput comboInput;
100 
101 	public ContentComboTestBase(ComboInput comboInput) {
102 		this.comboInput = comboInput;
103 	}
104 
105 	@Test
106 	public void doTest() throws Exception {
107 		RestClient rc = getClient(comboInput.mediaType);
108 		String s = rc.get(comboInput.url).run().getContent().asString();
109 		assertContains(s, comboInput.expectedResults);
110 	}
111 }