Prefix function names with spring-

* Add `spring-` prefix to function names

Fixes: #9

This commit renames each sub-module in the common, consumer, function
and supplier groups with a prefix of `spring-`.

* Update README.adoc links to new prefixed names
This commit is contained in:
Chris Bono
2024-01-02 13:07:00 -06:00
committed by GitHub
parent b9a3e59074
commit 84e732da08
596 changed files with 146 additions and 147 deletions

View File

@@ -0,0 +1,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();
}
}
}

View File

@@ -0,0 +1,140 @@
/*
* 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.trend;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
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 org.mockserver.model.Header;
import org.mockserver.model.HttpRequest;
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 static org.assertj.core.api.Assertions.assertThat;
import static org.mockserver.matchers.Times.exactly;
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 TwitterTrendFunctionTests {
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;
private static HttpRequest trendsRequest;
@Autowired
protected Function<Message<?>, Message<byte[]>> twitterTrendFunction;
@BeforeAll
public static void startServer() {
mockServer = ClientAndServer.startClientAndServer(MOCK_SERVER_PORT);
mockClient = new MockServerClient(MOCK_SERVER_IP, MOCK_SERVER_PORT);
trendsRequest = setExpectation(request()
.withMethod("GET")
.withPath("/trends/place.json")
.withQueryStringParameter("id", "2972"));
}
@AfterAll
public static void stopServer() {
mockServer.stop();
}
public static HttpRequest setExpectation(HttpRequest request) {
mockClient
.when(request, exactly(1))
.respond(response()
.withStatusCode(200)
.withHeaders(
new Header("Content-Type", "application/json; charset=utf-8"),
new Header("Cache-Control", "public, max-age=86400"))
.withBody(TwitterTestUtils.asString("classpath:/response/trends.json"))
.withDelay(TimeUnit.SECONDS, 1)
);
return request;
}
@TestPropertySource(properties = {
"twitter.trend.locationId='2972'",
"twitter.connection.rawJson=true"
})
public static class TwitterTrendPayloadTests extends TwitterTrendFunctionTests {
@Test
public void testOne() {
Message<?> received = twitterTrendFunction.apply(MessageBuilder.withPayload("Hello").build());
mockClient.verify(trendsRequest, once());
assertThat(received).isNotNull();
}
}
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(TwitterTrendFunctionConfiguration.class)
public static class TwitterTrendFunctionTestApplication {
@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();
}
}
}

View File

@@ -0,0 +1,204 @@
/*
* 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.users;
import java.io.IOException;
import java.util.List;
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 org.mockserver.model.Header;
import org.mockserver.model.HttpRequest;
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 static org.assertj.core.api.Assertions.assertThat;
import static org.mockserver.matchers.Times.exactly;
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 TwitterUsersFunctionTests {
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;
private static HttpRequest searchUsersRequest;
private static HttpRequest lookupUsersRequest;
private static HttpRequest lookupUsersRequest2;
@Autowired
protected ObjectMapper mapper;
@Autowired
Function<Message<?>, Message<byte[]>> twitterUsersFunction;
public static HttpRequest setExpectation(HttpRequest request, String responseUri) {
mockClient
.when(request, exactly(1))
.respond(response()
.withStatusCode(200)
.withHeaders(
new Header("Content-Type", "application/json; charset=utf-8"),
new Header("Cache-Control", "public, max-age=86400"))
.withBody(TwitterTestUtils.asString(responseUri))
.withDelay(TimeUnit.SECONDS, 1)
);
return request;
}
@BeforeAll
public static void startServer() {
mockServer = ClientAndServer.startClientAndServer(MOCK_SERVER_PORT);
mockClient = new MockServerClient(MOCK_SERVER_IP, MOCK_SERVER_PORT);
searchUsersRequest = setExpectation(request()
.withMethod("GET")
.withPath("/users/search.json")
.withQueryStringParameter("q", "tzolov")
.withQueryStringParameter("page", "3"),
"classpath:/response/search_users.json");
lookupUsersRequest = setExpectation(request()
.withMethod("GET")
.withPath("/users/lookup.json")
.withQueryStringParameter("user_id",
"710705860343963648,326896547,267603736,781497571629989888,838754923"),
"classpath:/response/lookup_users_id.json");
lookupUsersRequest2 = setExpectation(request()
.withMethod("GET")
.withPath("/users/lookup.json")
.withQueryStringParameter("screen_name",
"TzolovMarto,Rabotnik57,antzolov,peyo_tzolov,ivantzolov"),
"classpath:/response/lookup_users_id.json");
}
@AfterAll
public static void stopServer() {
mockServer.stop();
}
@TestPropertySource(properties = {
"twitter.users.type=search",
"twitter.users.search.query=payload"
})
public static class TwitterSearchUsersTests extends TwitterUsersFunctionTests {
@Test
public void testOne() throws IOException {
Message<?> received = twitterUsersFunction.apply(MessageBuilder.withPayload("tzolov").build());
mockClient.verify(searchUsersRequest, once());
assertThat(received).isNotNull();
List list = mapper.readValue(new String((byte[]) received.getPayload()), List.class);
assertThat(list).hasSize(20);
}
}
@TestPropertySource(properties = {
"twitter.users.type=lookup",
"twitter.users.lookup.userId='710705860343963648,326896547,267603736,781497571629989888,838754923'"
})
public static class TwitterLookupUserIdLiteralTests extends TwitterUsersFunctionTests {
@Test
public void testOne() throws IOException {
Message<?> received = twitterUsersFunction.apply(MessageBuilder.withPayload("tzolov").build());
mockClient.verify(lookupUsersRequest, once());
assertThat(received).isNotNull();
List list = mapper.readValue(new String((byte[]) received.getPayload()), List.class);
assertThat(list).hasSize(5);
}
}
@TestPropertySource(properties = {
"twitter.users.type=lookup",
"twitter.users.lookup.screenName=#jsonPath(payload,'$[*].code')"
})
public static class TwitterLookupScreenNamePayloadTests extends TwitterUsersFunctionTests {
@Test
public void testOne() throws IOException {
Object payload = "[{\"code\":\"TzolovMarto\"},{\"code\":\"Rabotnik57\"},{\"code\":\"antzolov\"},{\"code\":\"peyo_tzolov\"},{\"code\":\"ivantzolov\"}]";
Message<?> received = twitterUsersFunction.apply(MessageBuilder.withPayload(payload).build());
assertThat(received).isNotNull();
mockClient.verify(lookupUsersRequest2, once());
assertThat(received).isNotNull();
List list = mapper.readValue(new String((byte[]) received.getPayload()), List.class);
assertThat(list).hasSize(5);
}
}
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(TwitterUsersFunctionConfiguration.class)
static class TwitterUsersFunctionTestApplication {
@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();
}
}
}

View File

@@ -0,0 +1,12 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<logger name="org.mockserver" level="WARN" />
<logger name="com.networknt.schema" level="WARN" />
</configuration>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long