Make spring-twitter-function as auto-config

* Fix Checkstyle violations in this module
* Make all the Twitter function auto-configurations as conditional on their specific properties
to avoid extra beans not expected in the target application
* Fix README respectively
This commit is contained in:
Artem Bilan
2024-01-10 11:21:21 -05:00
parent e1874eee92
commit e2f721b69f
14 changed files with 164 additions and 175 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
@@ -40,13 +40,11 @@ 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;
@@ -58,17 +56,14 @@ 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" })
@SpringBootTest(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 {
public abstract class TwitterGeoFunctionTests {
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;
@@ -90,8 +85,8 @@ public abstract class TwitterGeoFunctionTest {
@BeforeAll
public static void startMockServer() {
mockServer = ClientAndServer.startClientAndServer(MOCK_SERVER_PORT);
mockClient = new MockServerClient(MOCK_SERVER_IP, MOCK_SERVER_PORT);
mockServer = ClientAndServer.startClientAndServer();
mockClient = new MockServerClient(MOCK_SERVER_IP, mockServer.getPort());
}
@AfterAll
@@ -101,7 +96,7 @@ public abstract class TwitterGeoFunctionTest {
@TestPropertySource(
properties = { "twitter.geo.search.ip='127.0.0.1'", "twitter.geo.search.query=payload.toUpperCase()" })
public static class TwitterGeoSearchByIPAndQueryTests extends TwitterGeoFunctionTest {
public static class TwitterGeoSearchByIPAndQueryTests extends TwitterGeoFunctionTests {
@Test
public void testOne() throws IOException {
@@ -125,7 +120,7 @@ public abstract class TwitterGeoFunctionTest {
assertThat(outPayload).isNotNull();
List places = new ObjectMapper().readValue(outPayload, List.class);
List<?> places = new ObjectMapper().readValue(outPayload, List.class);
assertThat(places).hasSize(12);
}
@@ -133,7 +128,7 @@ public abstract class TwitterGeoFunctionTest {
@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 {
public static class TwitterGeoSearchByLocationTests extends TwitterGeoFunctionTests {
@Test
public void testOne() throws IOException {
@@ -159,7 +154,7 @@ public abstract class TwitterGeoFunctionTest {
assertThat(outPayload).isNotNull();
List places = new ObjectMapper().readValue(outPayload, List.class);
List<?> places = new ObjectMapper().readValue(outPayload, List.class);
assertThat(places).hasSize(12);
}
@@ -167,7 +162,7 @@ public abstract class TwitterGeoFunctionTest {
@TestPropertySource(properties = { "twitter.geo.type=reverse", "twitter.geo.location.lat='52.378'",
"twitter.geo.location.lon='4.9'" })
public static class TwitterGeoSearchByLocation2Tests extends TwitterGeoFunctionTest {
public static class TwitterGeoSearchByLocation2Tests extends TwitterGeoFunctionTests {
@Test
public void testOne() throws IOException {
@@ -191,7 +186,7 @@ public abstract class TwitterGeoFunctionTest {
assertThat(outPayload).isNotNull();
List places = new ObjectMapper().readValue(outPayload, List.class);
List<?> places = new ObjectMapper().readValue(outPayload, List.class);
assertThat(places).hasSize(12);
}
@@ -200,7 +195,7 @@ public abstract class TwitterGeoFunctionTest {
@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 {
public static class TwitterGeoSearchJsonPathTests extends TwitterGeoFunctionTests {
@Test
public void testOne() throws IOException {
@@ -228,7 +223,7 @@ public abstract class TwitterGeoFunctionTest {
assertThat(outPayload).isNotNull();
List places = new ObjectMapper().readValue(outPayload, List.class);
List<?> places = new ObjectMapper().readValue(outPayload, List.class);
assertThat(places).hasSize(12);
}
@@ -236,7 +231,6 @@ public abstract class TwitterGeoFunctionTest {
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(TwitterGeoFunctionConfiguration.class)
public static class TwitterGeoFunctionTestApplication {
@Bean
@@ -246,7 +240,7 @@ public abstract class TwitterGeoFunctionTest {
Function<TwitterConnectionProperties, ConfigurationBuilder> mockedConfiguration = toConfigurationBuilder
.andThen(new TwitterTestUtils()
.mockTwitterUrls(String.format("http://%s:%s", MOCK_SERVER_IP, MOCK_SERVER_PORT)));
.mockTwitterUrls(String.format("http://%s:%s", MOCK_SERVER_IP, mockServer.getPort())));
return mockedConfiguration.apply(properties).build();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
@@ -35,13 +35,11 @@ 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;
@@ -61,8 +59,6 @@ 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;
@@ -74,8 +70,8 @@ public abstract class TwitterTrendFunctionTests {
@BeforeAll
public static void startServer() {
mockServer = ClientAndServer.startClientAndServer(MOCK_SERVER_PORT);
mockClient = new MockServerClient(MOCK_SERVER_IP, MOCK_SERVER_PORT);
mockServer = ClientAndServer.startClientAndServer();
mockClient = new MockServerClient(MOCK_SERVER_IP, mockServer.getPort());
trendsRequest = setExpectation(
request().withMethod("GET").withPath("/trends/place.json").withQueryStringParameter("id", "2972"));
@@ -96,7 +92,8 @@ public abstract class TwitterTrendFunctionTests {
return request;
}
@TestPropertySource(properties = { "twitter.trend.locationId='2972'", "twitter.connection.rawJson=true" })
@TestPropertySource(properties = { "twitter.trend.trendQueryType=trend", "twitter.trend.locationId='2972'",
"twitter.connection.rawJson=true" })
public static class TwitterTrendPayloadTests extends TwitterTrendFunctionTests {
@Test
@@ -110,7 +107,6 @@ public abstract class TwitterTrendFunctionTests {
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(TwitterTrendFunctionConfiguration.class)
public static class TwitterTrendFunctionTestApplication {
@Bean
@@ -120,7 +116,7 @@ public abstract class TwitterTrendFunctionTests {
Function<TwitterConnectionProperties, ConfigurationBuilder> mockedConfiguration = toConfigurationBuilder
.andThen(new TwitterTestUtils()
.mockTwitterUrls(String.format("http://%s:%s", MOCK_SERVER_IP, MOCK_SERVER_PORT)));
.mockTwitterUrls(String.format("http://%s:%s", MOCK_SERVER_IP, mockServer.getPort())));
return mockedConfiguration.apply(properties).build();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 the original author or authors.
* Copyright 2020-2024 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.
@@ -38,13 +38,11 @@ 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;
@@ -64,8 +62,6 @@ 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;
@@ -94,8 +90,8 @@ public abstract class TwitterUsersFunctionTests {
@BeforeAll
public static void startServer() {
mockServer = ClientAndServer.startClientAndServer(MOCK_SERVER_PORT);
mockClient = new MockServerClient(MOCK_SERVER_IP, MOCK_SERVER_PORT);
mockServer = ClientAndServer.startClientAndServer();
mockClient = new MockServerClient(MOCK_SERVER_IP, mockServer.getPort());
searchUsersRequest = setExpectation(request().withMethod("GET")
.withPath("/users/search.json")
@@ -127,7 +123,7 @@ public abstract class TwitterUsersFunctionTests {
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);
List<?> list = mapper.readValue(new String((byte[]) received.getPayload()), List.class);
assertThat(list).hasSize(20);
}
@@ -144,7 +140,7 @@ public abstract class TwitterUsersFunctionTests {
mockClient.verify(lookupUsersRequest, once());
assertThat(received).isNotNull();
List list = mapper.readValue(new String((byte[]) received.getPayload()), List.class);
List<?> list = mapper.readValue(new String((byte[]) received.getPayload()), List.class);
assertThat(list).hasSize(5);
}
@@ -164,7 +160,7 @@ public abstract class TwitterUsersFunctionTests {
mockClient.verify(lookupUsersRequest2, once());
assertThat(received).isNotNull();
List list = mapper.readValue(new String((byte[]) received.getPayload()), List.class);
List<?> list = mapper.readValue(new String((byte[]) received.getPayload()), List.class);
assertThat(list).hasSize(5);
}
@@ -172,17 +168,16 @@ public abstract class TwitterUsersFunctionTests {
@SpringBootConfiguration
@EnableAutoConfiguration
@Import(TwitterUsersFunctionConfiguration.class)
static class TwitterUsersFunctionTestApplication {
@Bean
@Primary
public twitter4j.conf.Configuration twitterConfiguration2(TwitterConnectionProperties properties,
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)));
.mockTwitterUrls(String.format("http://%s:%s", MOCK_SERVER_IP, mockServer.getPort())));
return mockedConfiguration.apply(properties).build();
}