Setup PATCH method in the OpenFeign (#14304)

* Fix the integration tests which failed due to defined port

* Add open feign patch example

* refactor the feign code

* refactor the feign code

* refactor the feign code

* refactor the code

* remove space

* refactor code

* refactor code and set the user id for testing

* refactor code
This commit is contained in:
Saikat Chakraborty 2023-07-09 18:37:52 +05:30 committed by GitHub
parent cff81e0975
commit b19e44baef
8 changed files with 222 additions and 0 deletions

View File

@ -56,6 +56,7 @@
<module>spring-cloud-sleuth</module> <module>spring-cloud-sleuth</module>
<module>spring-cloud-open-telemetry</module> <module>spring-cloud-open-telemetry</module>
<module>spring-cloud-azure</module> <module>spring-cloud-azure</module>
<module>spring-cloud-openfeign-2</module>
</modules> </modules>
<build> <build>

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.cloud</groupId>
<artifactId>spring-cloud-openfeign-2</artifactId>
<name>spring-cloud-openfeign-2</name>
<description>OpenFeign project for Spring Boot</description>
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>${wire.mock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<spring-cloud.version>2021.0.7</spring-cloud.version>
<wire.mock.version>2.35.0</wire.mock.version>
</properties>
</project>

View File

@ -0,0 +1,16 @@
package com.baeldung.cloud.openfeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.cloud.openfeign.patcherror.client;
import com.baeldung.cloud.openfeign.patcherror.model.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "user-client", url = "${user.api.url}")
public interface UserClient {
@RequestMapping(value = "{userId}", method = RequestMethod.PATCH)
User updateUser(@PathVariable(value = "userId") String userId, @RequestBody User user);
}

View File

@ -0,0 +1,34 @@
package com.baeldung.cloud.openfeign.patcherror.model;
public class User {
private String userId;
private String userName;
private String email;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}

View File

@ -0,0 +1,3 @@
spring.application.name=openfeign
user.api.url=http://localhost:8082/api/user
feign.okhttp.enabled=true

View File

@ -0,0 +1,16 @@
package com.baeldung.cloud.openfeign;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ExampleApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@ -0,0 +1,75 @@
package com.baeldung.cloud.openfeign.patcherror.client;
import com.baeldung.cloud.openfeign.ExampleApplication;
import com.baeldung.cloud.openfeign.patcherror.model.User;
import com.github.tomakehurst.wiremock.WireMockServer;
import feign.FeignException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = ExampleApplication.class)
public class UserClientUnitTest {
@Autowired
private UserClient userClient;
private WireMockServer wireMockServer;
@BeforeEach
public void startWireMockServer() {
wireMockServer = new WireMockServer(8082);
configureFor("localhost", 8082);
wireMockServer.start();
}
@AfterEach
public void stopWireMockServer() {
wireMockServer.stop();
}
@Test
void givenUserExistsAndIsValid_whenUpdateUserCalled_thenReturnSuccess() {
String updatedUserResponse = "{\n" +
" \"userId\": 100001,\n" +
" \"userName\": \"name\",\n" +
" \"email\": \"updated-email@mail.in\"\n" +
"}";
stubFor(patch(urlEqualTo("/api/user/".concat("100001")))
.willReturn(aResponse().withStatus(HttpStatus.OK.value())
.withHeader("Content-Type", "application/json")
.withBody(updatedUserResponse)));
User user = new User();
user.setUserId("100001");
user.setEmail("updated-email@mail.in");
User updatedUser = userClient.updateUser("100001", user);
assertEquals(user.getUserId(), updatedUser.getUserId());
assertEquals(user.getEmail(), updatedUser.getEmail());
}
@Test
void givenUserNotFound_whenUpdateUserCalled_thenReturnNotFoundErrorAndFeignException() {
User user = new User();
user.setUserId("100002");
user.setEmail("updated-email@mail.in");
stubFor(patch(urlEqualTo("/api/user/".concat("100002")))
.willReturn(aResponse().withStatus(404)));
assertThrows(FeignException.class, () -> userClient.updateUser("100002", user));
}
}