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:
committed by
GitHub
parent
cff81e0975
commit
b19e44baef
+16
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+16
@@ -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);
|
||||
|
||||
}
|
||||
+34
@@ -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;
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
spring.application.name=openfeign
|
||||
user.api.url=http://localhost:8082/api/user
|
||||
feign.okhttp.enabled=true
|
||||
+16
@@ -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() {
|
||||
}
|
||||
}
|
||||
+75
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user