-
Notifications
You must be signed in to change notification settings - Fork 7
/
TwitterGeoFunctionTest.java
279 lines (220 loc) · 9.4 KB
/
TwitterGeoFunctionTest.java
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 2020-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.fn.twitter.geo;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;
import twitter4j.conf.ConfigurationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.fn.common.twitter.TwitterConnectionProperties;
import org.springframework.cloud.fn.common.twitter.util.TwitterTestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.util.TestSocketUtils;
import org.springframework.util.MimeTypeUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockserver.matchers.Times.unlimited;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static org.mockserver.verify.VerificationTimes.once;
/**
* @author Christian Tzolov
*/
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = {
"twitter.connection.consumerKey=consumerKey666",
"twitter.connection.consumerSecret=consumerSecret666",
"twitter.connection.accessToken=accessToken666",
"twitter.connection.accessTokenSecret=accessTokenSecret666"
})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public abstract class TwitterGeoFunctionTest {
private static final String MOCK_SERVER_IP = "127.0.0.1";
private static final Integer MOCK_SERVER_PORT = TestSocketUtils.findAvailableTcpPort();
private static ClientAndServer mockServer;
private static MockServerClient mockClient;
@Autowired
protected Function<Message<?>, Message<byte[]>> twitterUsersFunction;
public static void recordRequestExpectation(Map<String, List<String>> parameters) {
mockClient
.when(
request()
.withMethod("GET")
.withPath("/geo/search.json")
.withQueryStringParameters(parameters),
unlimited())
.respond(
response()
.withStatusCode(200)
.withHeader("Content-Type", "application/json; charset=utf-8")
.withBody(TwitterTestUtils.asString("classpath:/response/search_places_amsterdam.json"))
.withDelay(TimeUnit.SECONDS, 1));
}
@BeforeAll
public static void startMockServer() {
mockServer = ClientAndServer.startClientAndServer(MOCK_SERVER_PORT);
mockClient = new MockServerClient(MOCK_SERVER_IP, MOCK_SERVER_PORT);
}
@AfterAll
public static void stopMockServer() {
mockServer.stop();
}
@TestPropertySource(properties = {
"twitter.geo.search.ip='127.0.0.1'",
"twitter.geo.search.query=payload.toUpperCase()"
})
public static class TwitterGeoSearchByIPAndQueryTests extends TwitterGeoFunctionTest {
@Test
public void testOne() throws IOException {
Map<String, List<String>> queryParameters = new HashMap<>();
queryParameters.put("ip", Collections.singletonList("127.0.0.1"));
queryParameters.put("query", Collections.singletonList("Amsterdam"));
recordRequestExpectation(queryParameters);
String inPayload = "Amsterdam";
Message<?> received = twitterUsersFunction.apply(MessageBuilder.withPayload(inPayload).build());
mockClient.verify(request()
.withMethod("GET")
.withPath("/geo/search.json")
.withQueryStringParameter("ip", "127.0.0.1")
.withQueryStringParameter("query", "AMSTERDAM"),
once());
String outPayload = new String((byte[]) received.getPayload());
assertThat(outPayload).isNotNull();
List places = new ObjectMapper().readValue(outPayload, List.class);
assertThat(places).hasSize(12);
}
}
@TestPropertySource(properties = {
"twitter.geo.location.lat='52.378'",
"twitter.geo.location.lon='4.9'",
"twitter.geo.search.query=payload.toUpperCase()"
})
public static class TwitterGeoSearchByLocationTests extends TwitterGeoFunctionTest {
@Test
public void testOne() throws IOException {
Map<String, List<String>> queryParameters = new HashMap<>();
queryParameters.put("lat", Collections.singletonList("52.378"));
queryParameters.put("long", Collections.singletonList("4.9"));
queryParameters.put("query", Collections.singletonList("Amsterdam"));
recordRequestExpectation(queryParameters);
String inPayload = "Amsterdam";
Message<?> received = twitterUsersFunction.apply(MessageBuilder.withPayload(inPayload).build());
mockClient.verify(request()
.withMethod("GET")
.withPath("/geo/search.json")
.withQueryStringParameter("lat", "52.378")
.withQueryStringParameter("long", "4.9")
.withQueryStringParameter("query", "Amsterdam"),
once());
String outPayload = new String((byte[]) received.getPayload());
assertThat(outPayload).isNotNull();
List places = new ObjectMapper().readValue(outPayload, List.class);
assertThat(places).hasSize(12);
}
}
@TestPropertySource(properties = {
"twitter.geo.type=reverse",
"twitter.geo.location.lat='52.378'",
"twitter.geo.location.lon='4.9'"
})
public static class TwitterGeoSearchByLocation2Tests extends TwitterGeoFunctionTest {
@Test
public void testOne() throws IOException {
Map<String, List<String>> queryParameters = new HashMap<>();
queryParameters.put("lat", Arrays.asList("52.378"));
queryParameters.put("long", Arrays.asList("4.9"));
recordRequestExpectation(queryParameters);
String inPayload = "Amsterdam";
Message<?> received = twitterUsersFunction.apply(MessageBuilder.withPayload(inPayload).build());
mockClient.verify(request()
.withMethod("GET")
.withPath("/geo/search.json")
.withQueryStringParameter("lat", "52.378")
.withQueryStringParameter("long", "4.9"),
once());
String outPayload = new String((byte[]) received.getPayload());
assertThat(outPayload).isNotNull();
List places = new ObjectMapper().readValue(outPayload, List.class);
assertThat(places).hasSize(12);
}
}
@TestPropertySource(properties = {
"twitter.geo.location.lat=#jsonPath(new String(payload),'$.location.lat')",
"twitter.geo.location.lon=#jsonPath(new String(payload),'$.location.lon')",
"twitter.geo.search.query=#jsonPath(new String(payload),'$.country')"
})
public static class TwitterGeoSearchJsonPathTests extends TwitterGeoFunctionTest {
@Test
public void testOne() throws IOException {
Map<String, List<String>> queryParameters = new HashMap<>();
queryParameters.put("lat", Collections.singletonList("52.0"));
queryParameters.put("long", Collections.singletonList("5.0"));
queryParameters.put("query", Collections.singletonList("Netherlands"));
recordRequestExpectation(queryParameters);
String inPayload = "{ \"country\" : \"Netherlands\", \"location\" : { \"lat\" : 52.00 , \"lon\" : 5.0 } }";
Message<?> received = twitterUsersFunction.apply(MessageBuilder
.withPayload(inPayload)
.setHeader("contentType", MimeTypeUtils.APPLICATION_JSON_VALUE)
.build());
mockClient.verify(request()
.withMethod("GET")
.withPath("/geo/search.json")
.withQueryStringParameter("lat", "52.0")
.withQueryStringParameter("long", "5.0")
.withQueryStringParameter("query", "Netherlands"),
once());
String outPayload = new String((byte[]) received.getPayload());
assertThat(outPayload).isNotNull();
List places = new ObjectMapper().readValue(outPayload, List.class);
assertThat(places).hasSize(12);
}
}
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(TwitterGeoFunctionConfiguration.class)
public static class TwitterGeoFunctionTestApplication {
@Bean
@Primary
public twitter4j.conf.Configuration twitterConfiguration2(TwitterConnectionProperties properties,
Function<TwitterConnectionProperties, ConfigurationBuilder> toConfigurationBuilder) {
Function<TwitterConnectionProperties, ConfigurationBuilder> mockedConfiguration =
toConfigurationBuilder.andThen(
new TwitterTestUtils().mockTwitterUrls(
String.format("http://%s:%s", MOCK_SERVER_IP, MOCK_SERVER_PORT)));
return mockedConfiguration.apply(properties).build();
}
}
}