Add Cloud OpenFeign tests

See gh-174
This commit is contained in:
Olga Maciaszek-Sharma
2023-03-24 14:08:37 +01:00
committed by Andy Wilkinson
parent a0a4b31c0f
commit f8e213739c
7 changed files with 194 additions and 0 deletions

View File

@@ -113,6 +113,9 @@ groups:
- name: cloud-loadbalancing-webflux
app_test: true
test: false
- name: cloud-openfeign
app_test: true
test: false
- name: cloud-stream-kafka
app_test: true
test: false

View File

@@ -0,0 +1,29 @@
import org.springframework.boot.gradle.plugin.SpringBootPlugin
plugins {
id 'java'
id 'org.springframework.boot'
id 'org.springframework.aot.smoke-test'
id 'org.graalvm.buildtools.native'
}
ext {
set('springCloudVersion', "2022.0.2-SNAPSHOT")
}
dependencies {
implementation(platform(SpringBootPlugin.BOM_COORDINATES))
implementation(platform("org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"))
implementation("org.springframework.cloud:spring-cloud-starter-openfeign")
implementation("org.springframework.cloud:spring-cloud-starter-loadbalancer")
implementation ('org.springframework.boot:spring-boot-starter-web')
testImplementation("org.springframework.boot:spring-boot-starter-test")
appTestImplementation(project(":aot-smoke-test-support"))
appTestImplementation("org.awaitility:awaitility:4.2.0")
}
aotSmokeTest {
webApplication = true
}

View File

@@ -0,0 +1,6 @@
version: '3'
services:
test-service:
image: springcloud/test-service:latest
ports:
- "8081"

View File

@@ -0,0 +1,28 @@
package com.example.cloud.openfeign;
import java.time.Duration;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.aot.smoketest.support.assertj.AssertableOutput;
import org.springframework.aot.smoketest.support.junit.ApplicationTest;
import static org.assertj.core.api.Assertions.assertThat;
@ApplicationTest
class CloudOpenFeignApplicationAotTests {
@Test
void shouldCallTestService(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
assertThat(output).hasLineContaining("c.e.c.o.CloudOpenFeignApplication : test");
assertThat(output).hasLineContaining("c.e.c.o.CloudOpenFeignApplication : test type hints");
// verify custom config with Logger.Level.FULL loaded
assertThat(output).hasLineContaining("---> POST http://test-service/test HTTP/1.1");
assertThat(output).hasNoLinesContaining("ERROR");
});
}
}

View File

@@ -0,0 +1,95 @@
package com.example.cloud.openfeign;
import com.example.cloud.openfeign.config.CustomFeignConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@SpringBootApplication
@EnableFeignClients(clients = TestServiceClient.class)
public class CloudOpenFeignApplication implements CommandLineRunner {
private static final Log LOG = LogFactory.getLog(CloudOpenFeignApplication.class);
@Autowired
private TestServiceClient testServiceClient;
public static void main(String[] args) {
SpringApplication.run(CloudOpenFeignApplication.class, args);
}
@Override
public void run(String... args) {
callTestService();
}
private void callTestService() {
if (LOG.isInfoEnabled()) {
LOG.info(testServiceClient.test());
LOG.info(testServiceClient.testTypesForReflection(new TestArgType("test type hints")).getTest());
}
}
}
@FeignClient(value = "test-service", configuration = CustomFeignConfig.class)
interface TestServiceClient {
@GetMapping("/")
String test();
@PostMapping(value = "/test", produces = "application/json", consumes = "application/json")
TestReturnType testTypesForReflection(@RequestBody TestArgType testArg);
}
class TestReturnType {
private String test;
public TestReturnType(String test) {
this.test = test;
}
public TestReturnType() {
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
class TestArgType {
private String test;
public TestArgType(String test) {
this.test = test;
}
public TestArgType() {
}
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}

View File

@@ -0,0 +1,14 @@
package com.example.cloud.openfeign.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
public class CustomFeignConfig {
@Bean
Logger.Level loggingLevel() {
return Logger.Level.FULL;
}
}

View File

@@ -0,0 +1,19 @@
logging:
level:
com.example.cloud.openfeign.TestServiceClient: DEBUG
spring:
application:
name: cloud-openfeign
cloud:
discovery:
client:
simple:
instances:
test-service:
- uri: http://${TEST-SERVICE_HOST:localhost}:${TEST-SERVICE_PORT_8081:8081}
refresh:
enabled: false
loadbalancer:
eager-load:
clients:
- test-service