1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.examples.rest;
18
19 import static org.apache.juneau.commons.utils.CollectionUtils.*;
20 import static org.apache.juneau.commons.utils.ThrowableUtils.*;
21 import static org.apache.juneau.examples.rest.TestUtils.*;
22 import static org.junit.runners.MethodSorters.*;
23
24 import java.io.*;
25 import java.util.*;
26 import java.util.function.*;
27
28 import org.apache.juneau.*;
29 import org.apache.juneau.csv.*;
30 import org.apache.juneau.html.*;
31 import org.apache.juneau.json.*;
32 import org.apache.juneau.msgpack.*;
33 import org.apache.juneau.parser.*;
34 import org.apache.juneau.plaintext.*;
35 import org.apache.juneau.rest.client.*;
36 import org.apache.juneau.serializer.*;
37 import org.apache.juneau.uon.*;
38 import org.apache.juneau.urlencoding.*;
39 import org.apache.juneau.xml.*;
40 import org.junit.*;
41
42 @FixMethodOrder(NAME_ASCENDING)
43 public class ContentComboTestBase extends RestTestcase {
44
45
46 private static Map<String,RestClient> clients = map();
47
48 protected RestClient getClient(MediaType mediaType) {
49 var mt = mediaType.toString();
50 return switch (mt) {
51 case "text/csv" -> getClient(mt, CsvSerializer.DEFAULT, CsvParser.DEFAULT);
52 case "text/html" -> getClient(mt, HtmlSerializer.DEFAULT, HtmlParser.DEFAULT);
53 case "application/json" -> getClient(mt, JsonSerializer.DEFAULT, JsonParser.DEFAULT);
54 case "octal/msgpack" -> getClient(mt, MsgPackSerializer.DEFAULT, MsgPackParser.DEFAULT, x -> x.queryData("plainText", "true"));
55 case "text/plain" -> getClient(mt, PlainTextSerializer.DEFAULT, PlainTextParser.DEFAULT);
56 case "text/uon" -> getClient(mt, UonSerializer.DEFAULT, UonParser.DEFAULT);
57 case "application/x-www-form-urlencoded" -> getClient(mt, UrlEncodingSerializer.DEFAULT, UrlEncodingParser.DEFAULT);
58 case "text/xml" -> getClient(mt, XmlSerializer.DEFAULT, XmlParser.DEFAULT);
59 default -> throw rex("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 var b = SamplesMicroservice.client(serializer, parser);
66 for (var 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 private final ComboInput comboInput;
99
100 public ContentComboTestBase(ComboInput comboInput) {
101 this.comboInput = comboInput;
102 }
103
104 @Test
105 public void doTest() throws Exception {
106 var rc = getClient(comboInput.mediaType);
107 var s = rc.get(comboInput.url).run().getContent().asString();
108 assertContains(s, comboInput.expectedResults);
109 }
110 }