-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathTestUtil.java
More file actions
164 lines (144 loc) · 5.57 KB
/
TestUtil.java
File metadata and controls
164 lines (144 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package BehaviorTests;
import tools.jackson.core.JacksonException;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.datatype.guava.GuavaModule;
import com.google.common.base.Charsets;
import com.google.common.collect.Multimap;
import com.google.common.io.Resources;
import kong.unirest.core.Client;
import kong.unirest.core.Unirest;
import kong.unirest.core.java.JavaClient;
import org.assertj.guava.api.MultimapAssert;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import static org.assertj.core.api.Fail.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class TestUtil {
private static final ObjectMapper om = JsonMapper.builderWithJackson2Defaults().addModule(new GuavaModule()).build();
public static <T> T readValue(String body, Class<T> as) {
try {
return om.readValue(body, as);
} catch (JacksonException e) {
throw new RuntimeException(e);
}
}
public static File getImageFile() {
return rezFile("/image.jpg");
}
public static File rezFile(String name) {
try {
return new File(MockServer.class.getResource(name).toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
public static InputStream rezInput(String name) {
try {
return MockServer.class.getResourceAsStream(name);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static byte[] getFileBytes(String s) {
try {
final InputStream stream = new FileInputStream(rezFile(s));
final byte[] bytes = new byte[stream.available()];
stream.read(bytes);
stream.close();
return bytes;
}catch (IOException e){
throw new RuntimeException(e);
}
}
public static <K, V> Map<K, V> mapOf(Object... keyValues) {
Map<K, V> map = new HashMap<>();
K key = null;
for (int index = 0; index < keyValues.length; index++) {
if (index % 2 == 0) {
key = (K)keyValues[index];
}
else {
map.put(key, (V)keyValues[index]);
}
}
return map;
}
public static String getResource(String resourceName) {
try {
return Resources.toString(Resources.getResource(resourceName), Charsets.UTF_8);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Client getFailureClient() throws Exception {
HttpClient mock = mock(HttpClient.class);
when(mock.send(any(java.net.http.HttpRequest.class),
any(HttpResponse.BodyHandler.class)))
.thenThrow(new IOException("Something horrible happened"));
return new JavaClient(Unirest.config(), mock);
}
public static void reset() {
}
public static Client getFailureAsyncClient() {
HttpClient client = HttpClient.newBuilder()
//.sslContext(new SSLContext())
.authenticator(new Authenticator() {
@Override
public PasswordAuthentication requestPasswordAuthenticationInstance(String host, InetAddress addr, int port, String protocol, String prompt, String scheme, URL url, RequestorType reqType) {
throw new RuntimeException("boo");
}
}).build();
return new JavaClient(Unirest.config(), client);
}
public static <K, V> MultimapAssert<K, V> assertMultiMap(final Multimap<K, V> actual) {
return org.assertj.guava.api.Assertions.assertThat(actual);
}
public static final class Matchers {
public static Consumer<HeaderAsserts.HeaderValue> isBase64Encoded() {
return h -> {
var value = h.getValue();
try {
Base64.getDecoder().decode(value);
}catch (IllegalArgumentException e){
fail("Header was not base64 encoded: " + value);
}
};
}
}
}