JAVA-17818 Split or move spring-cloud-openfeign module (conti-2) (#13621)
* JAVA-17818 Split or move spring-cloud-openfeign module (conti-2) * JAVA-17818 Some adjustments in the README.md file --------- Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
@@ -2,10 +2,8 @@ package com.baeldung.core;
|
||||
|
||||
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) {
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.core.client;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.support.SpringEncoder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.model.FormData;
|
||||
|
||||
import feign.codec.Encoder;
|
||||
import feign.form.spring.SpringFormEncoder;
|
||||
|
||||
@FeignClient(name = "form-client", url = "http://localhost:8085/api", configuration = FormFeignEncoderConfig.class)
|
||||
public interface FormClient {
|
||||
|
||||
@PostMapping(value = "/form", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||
void postFormData(@RequestBody FormData data);
|
||||
|
||||
@PostMapping(value = "/form/map", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||
void postFormMapData(Map<String, ?> data);
|
||||
|
||||
}
|
||||
|
||||
class FormFeignEncoderConfig {
|
||||
|
||||
@Bean
|
||||
public Encoder encoder(ObjectFactory<HttpMessageConverters> converters) {
|
||||
return new SpringFormEncoder(new SpringEncoder(converters));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.core.client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import com.baeldung.core.config.FeignConfig;
|
||||
|
||||
@FeignClient(name = "user-client", url="https://jsonplaceholder.typicode.com", configuration = FeignConfig.class)
|
||||
public interface UserClient {
|
||||
|
||||
@GetMapping(value = "/users")
|
||||
String getUsers();
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.core.customizederrorhandling.config.FeignConfig;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
|
||||
@FeignClient(name = "product-client-2", url = "http://localhost:8081/product/", configuration = FeignConfig.class)
|
||||
public interface ProductClient {
|
||||
|
||||
@RequestMapping(value = "{id}", method = RequestMethod.GET)
|
||||
Product getProduct(@PathVariable(value = "id") String id);
|
||||
|
||||
}
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.config;
|
||||
|
||||
import com.baeldung.core.customizederrorhandling.exception.ProductNotFoundException;
|
||||
import com.baeldung.core.customizederrorhandling.exception.ProductServiceNotAvailableException;
|
||||
import com.baeldung.core.exception.BadRequestException;
|
||||
|
||||
import feign.Response;
|
||||
import feign.codec.ErrorDecoder;
|
||||
|
||||
public class CustomErrorDecoder implements ErrorDecoder {
|
||||
|
||||
@Override
|
||||
public Exception decode(String methodKey, Response response) {
|
||||
switch (response.status()){
|
||||
case 400:
|
||||
return new BadRequestException();
|
||||
case 404:
|
||||
return new ProductNotFoundException("Product not found");
|
||||
case 503:
|
||||
return new ProductServiceNotAvailableException("Product Api is unavailable");
|
||||
default:
|
||||
return new Exception("Exception while getting product details");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import feign.Logger;
|
||||
import feign.codec.ErrorDecoder;
|
||||
|
||||
public class FeignConfig {
|
||||
|
||||
@Bean
|
||||
Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ErrorDecoder errorDecoder() {
|
||||
return new CustomErrorDecoder();
|
||||
}
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.core.customizederrorhandling.client.ProductClient;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
|
||||
@RestController("product_controller2")
|
||||
@RequestMapping(value = "myapp2")
|
||||
public class ProductController {
|
||||
|
||||
private final ProductClient productClient;
|
||||
|
||||
@Autowired
|
||||
public ProductController(ProductClient productClient) {
|
||||
this.productClient = productClient;
|
||||
}
|
||||
|
||||
@GetMapping("/product/{id}")
|
||||
public Product getProduct(@PathVariable String id) {
|
||||
return productClient.getProduct(id);
|
||||
}
|
||||
|
||||
}
|
||||
-55
@@ -1,55 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.exception;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class ErrorResponse {
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
|
||||
private Date timestamp;
|
||||
|
||||
@JsonProperty(value = "code")
|
||||
private int code;
|
||||
|
||||
@JsonProperty(value = "status")
|
||||
private String status;
|
||||
@JsonProperty(value = "message")
|
||||
private String message;
|
||||
@JsonProperty(value = "details")
|
||||
private String details;
|
||||
|
||||
public ErrorResponse() {
|
||||
}
|
||||
|
||||
public ErrorResponse(HttpStatus httpStatus, String message, String details) {
|
||||
timestamp = new Date();
|
||||
this.code = httpStatus.value();
|
||||
this.status = httpStatus.name();
|
||||
this.message = message;
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public Date getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String getDetails() {
|
||||
return details;
|
||||
}
|
||||
}
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ProductExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler({ProductServiceNotAvailableException.class})
|
||||
public ResponseEntity<ErrorResponse> handleProductServiceNotAvailableException(ProductServiceNotAvailableException exception, WebRequest request) {
|
||||
return new ResponseEntity<>(new ErrorResponse(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
exception.getMessage(),
|
||||
request.getDescription(false)),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ProductNotFoundException.class})
|
||||
public ResponseEntity<ErrorResponse> handleProductNotFoundException(ProductNotFoundException exception, WebRequest request) {
|
||||
return new ResponseEntity<>(new ErrorResponse(
|
||||
HttpStatus.NOT_FOUND,
|
||||
exception.getMessage(),
|
||||
request.getDescription(false)),
|
||||
HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleGenericException(Exception exception, WebRequest request) {
|
||||
return new ResponseEntity<>(new ErrorResponse(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
exception.getMessage(),
|
||||
request.getDescription(false)),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.exception;
|
||||
|
||||
public class ProductNotFoundException extends RuntimeException {
|
||||
|
||||
public ProductNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.exception;
|
||||
|
||||
public class ProductServiceNotAvailableException extends RuntimeException {
|
||||
|
||||
public ProductServiceNotAvailableException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.core.defaulterrorhandling.client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.config.FeignConfig;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
|
||||
@FeignClient(name = "product-client", url = "http://localhost:8084/product/", configuration = FeignConfig.class)
|
||||
public interface ProductClient {
|
||||
|
||||
@RequestMapping(value = "{id}", method = RequestMethod.GET)
|
||||
Product getProduct(@PathVariable(value = "id") String id);
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.core.defaulterrorhandling.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import feign.Logger;
|
||||
|
||||
public class FeignConfig {
|
||||
|
||||
@Bean
|
||||
Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
}
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.core.defaulterrorhandling.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.client.ProductClient;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
|
||||
@RestController("product_controller1")
|
||||
@RequestMapping(value ="myapp1")
|
||||
public class ProductController {
|
||||
|
||||
private final ProductClient productClient;
|
||||
|
||||
@Autowired
|
||||
public ProductController(ProductClient productClient) {
|
||||
this.productClient = productClient;
|
||||
}
|
||||
|
||||
@GetMapping("/product/{id}")
|
||||
public Product getProduct(@PathVariable String id) {
|
||||
return productClient.getProduct(id);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.core.defaulterrorhandling.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class FormData {
|
||||
|
||||
int id;
|
||||
String name;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.core.defaulterrorhandling.model;
|
||||
|
||||
public class Product {
|
||||
|
||||
private String id;
|
||||
|
||||
private String productName;
|
||||
|
||||
private double price;
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getProductName() {
|
||||
return productName;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.baeldung.core.fileupload.config;
|
||||
|
||||
public class ExceptionMessage {
|
||||
private String timestamp;
|
||||
private int status;
|
||||
private String error;
|
||||
private String message;
|
||||
private String path;
|
||||
|
||||
public String getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(String timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public void setError(String error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ExceptionMessage [timestamp=" + timestamp + ", status=" + status + ", error=" + error + ", message=" + message + ", path=" + path + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.core.fileupload.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.cloud.openfeign.support.SpringEncoder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import feign.codec.Encoder;
|
||||
import feign.codec.ErrorDecoder;
|
||||
import feign.form.spring.SpringFormEncoder;
|
||||
|
||||
public class FeignSupportConfig {
|
||||
@Bean
|
||||
public Encoder multipartFormEncoder() {
|
||||
return new SpringFormEncoder(new SpringEncoder(() -> new HttpMessageConverters(new RestTemplate().getMessageConverters())));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorDecoder errorDecoder() {
|
||||
return new RetreiveMessageErrorDecoder();
|
||||
}
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package com.baeldung.core.fileupload.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.baeldung.core.exception.BadRequestException;
|
||||
import com.baeldung.core.exception.NotFoundException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import feign.Response;
|
||||
import feign.codec.ErrorDecoder;
|
||||
|
||||
public class RetreiveMessageErrorDecoder implements ErrorDecoder {
|
||||
private final ErrorDecoder errorDecoder = new Default();
|
||||
|
||||
@Override
|
||||
public Exception decode(String methodKey, Response response) {
|
||||
ExceptionMessage message;
|
||||
try (InputStream bodyIs = response.body()
|
||||
.asInputStream()) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
message = mapper.readValue(bodyIs, ExceptionMessage.class);
|
||||
} catch (IOException e) {
|
||||
return new Exception(e.getMessage());
|
||||
}
|
||||
switch (response.status()) {
|
||||
case 400:
|
||||
return new BadRequestException(message.getMessage() != null ? message.getMessage() : "Bad Request");
|
||||
case 404:
|
||||
return new NotFoundException(message.getMessage() != null ? message.getMessage() : "Not found");
|
||||
default:
|
||||
return errorDecoder.decode(methodKey, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.core.fileupload.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baeldung.core.fileupload.service.UploadService;
|
||||
|
||||
@RestController
|
||||
public class FileController {
|
||||
|
||||
@Autowired
|
||||
private UploadService service;
|
||||
|
||||
@PostMapping(value = "/upload")
|
||||
public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFile(file);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/upload-mannual-client")
|
||||
public boolean handleFileUploadWithManualClient(
|
||||
@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFileWithManualClient(file);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/upload-error")
|
||||
public String handleFileUploadError(@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFile(file);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.core.fileupload.service;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baeldung.core.fileupload.config.FeignSupportConfig;
|
||||
|
||||
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class)
|
||||
public interface UploadClient {
|
||||
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
||||
|
||||
@PostMapping(value = "/upload-file-error", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
String fileUploadError(@RequestPart(value = "file") MultipartFile file);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung.core.fileupload.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import feign.Headers;
|
||||
import feign.Param;
|
||||
import feign.RequestLine;
|
||||
import feign.Response;
|
||||
|
||||
public interface UploadResource {
|
||||
|
||||
@RequestLine("POST /upload-file")
|
||||
@Headers("Content-Type: multipart/form-data")
|
||||
Response uploadFile(@Param("file") MultipartFile file);
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package com.baeldung.core.fileupload.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import feign.Feign;
|
||||
import feign.Response;
|
||||
import feign.form.spring.SpringFormEncoder;
|
||||
|
||||
@Service
|
||||
public class UploadService {
|
||||
private static final String HTTP_FILE_UPLOAD_URL = "http://localhost:8081";
|
||||
|
||||
@Autowired
|
||||
private UploadClient client;
|
||||
|
||||
public boolean uploadFileWithManualClient(MultipartFile file) {
|
||||
UploadResource fileUploadResource = Feign.builder().encoder(new SpringFormEncoder())
|
||||
.target(UploadResource.class, HTTP_FILE_UPLOAD_URL);
|
||||
Response response = fileUploadResource.uploadFile(file);
|
||||
return response.status() == 200;
|
||||
}
|
||||
|
||||
public String uploadFile(MultipartFile file) {
|
||||
return client.fileUpload(file);
|
||||
}
|
||||
|
||||
public String uploadFileError(MultipartFile file) {
|
||||
return client.fileUpload(file);
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.baeldung.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baeldung.core.fileupload.service.UploadService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ExampleApplication.class)
|
||||
public class OpenFeignFileUploadLiveTest {
|
||||
|
||||
@Autowired
|
||||
private UploadService uploadService;
|
||||
|
||||
private static String FILE_NAME = "fileupload.txt";
|
||||
|
||||
@Test
|
||||
public void whenFeignBuilder_thenFileUploadSuccess() throws IOException {
|
||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
||||
Assert.assertTrue(file.exists());
|
||||
FileInputStream input = new FileInputStream(file);
|
||||
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
|
||||
IOUtils.toByteArray(input));
|
||||
Assert.assertTrue(uploadService.uploadFileWithManualClient(multipartFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnnotatedFeignClient_thenFileUploadSuccess() throws IOException {
|
||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
||||
Assert.assertTrue(file.exists());
|
||||
FileInputStream input = new FileInputStream(file);
|
||||
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
|
||||
IOUtils.toByteArray(input));
|
||||
String uploadFile = uploadService.uploadFile(multipartFile);
|
||||
Assert.assertNotNull(uploadFile);
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package com.baeldung.core.client;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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 com.baeldung.core.defaulterrorhandling.model.FormData;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest
|
||||
@Slf4j
|
||||
class FormClientUnitTest {
|
||||
|
||||
private static WireMockServer wireMockServer;
|
||||
|
||||
@Autowired
|
||||
FormClient formClient;
|
||||
|
||||
@BeforeAll
|
||||
public static void startWireMockServer() {
|
||||
wireMockServer = new WireMockServer(8085);
|
||||
configureFor("localhost", 8085);
|
||||
wireMockServer.start();
|
||||
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void stopWireMockServer() {
|
||||
wireMockServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFormData_whenPostFormDataCalled_thenReturnSuccess() {
|
||||
FormData formData = new FormData(1, "baeldung");
|
||||
stubFor(WireMock.post(urlEqualTo("/api/form"))
|
||||
.willReturn(aResponse().withStatus(HttpStatus.OK.value())));
|
||||
|
||||
formClient.postFormData(formData);
|
||||
wireMockServer.verify(postRequestedFor(urlPathEqualTo("/api/form"))
|
||||
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded; charset=UTF-8"))
|
||||
.withRequestBody(equalTo("name=baeldung&id=1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFormMap_whenPostFormMapDataCalled_thenReturnSuccess() {
|
||||
Map<String, String> mapData = new HashMap<>();
|
||||
mapData.put("name", "baeldung");
|
||||
mapData.put("id", "1");
|
||||
stubFor(WireMock.post(urlEqualTo("/api/form/map"))
|
||||
.willReturn(aResponse().withStatus(HttpStatus.OK.value())));
|
||||
|
||||
formClient.postFormMapData(mapData);
|
||||
wireMockServer.verify(postRequestedFor(urlPathEqualTo("/api/form/map"))
|
||||
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded; charset=UTF-8"))
|
||||
.withRequestBody(equalTo("name=baeldung&id=1")));
|
||||
}
|
||||
|
||||
}
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.client;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.core.ExampleApplication;
|
||||
import com.baeldung.core.customizederrorhandling.exception.ProductNotFoundException;
|
||||
import com.baeldung.core.customizederrorhandling.exception.ProductServiceNotAvailableException;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ExampleApplication.class)
|
||||
public class ProductClientUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProductClient productClient;
|
||||
|
||||
private WireMockServer wireMockServer;
|
||||
|
||||
@Before
|
||||
public void startWireMockServer() {
|
||||
wireMockServer = new WireMockServer(8081);
|
||||
configureFor("localhost", 8081);
|
||||
wireMockServer.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void stopWireMockServer() {
|
||||
wireMockServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductApiIsNotAvailable_whenGetProductCalled_thenThrowProductServiceNotAvailableException() {
|
||||
String productId = "test";
|
||||
|
||||
stubFor(get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse().withStatus(503)));
|
||||
|
||||
assertThrows(ProductServiceNotAvailableException.class, () -> productClient.getProduct(productId));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductNotFound_whenGetProductCalled_thenThrowBadRequestException() {
|
||||
String productId = "test";
|
||||
|
||||
stubFor(get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse().withStatus(404)));
|
||||
|
||||
assertThrows(ProductNotFoundException.class, () -> productClient.getProduct(productId));
|
||||
}
|
||||
}
|
||||
-102
@@ -1,102 +0,0 @@
|
||||
package com.baeldung.core.customizederrorhandling.controller;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
|
||||
import com.baeldung.core.customizederrorhandling.client.ProductClient;
|
||||
import com.baeldung.core.customizederrorhandling.exception.ErrorResponse;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(ProductController.class)
|
||||
@ImportAutoConfiguration({FeignAutoConfiguration.class})
|
||||
public class ProductControllerUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProductClient productClient;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private WireMockServer wireMockServer;
|
||||
|
||||
@Before
|
||||
public void startWireMockServer() {
|
||||
wireMockServer = new WireMockServer(8081);
|
||||
configureFor("localhost", 8081);
|
||||
wireMockServer.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void stopWireMockServer() {
|
||||
wireMockServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductApiIsNotAvailable_whenGetProductCalled_ThenReturnInternalServerError() throws Exception {
|
||||
String productId = "test";
|
||||
|
||||
stubFor(WireMock.get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse()
|
||||
.withStatus(HttpStatus.SERVICE_UNAVAILABLE.value())));
|
||||
|
||||
ErrorResponse expectedError = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
"Product Api is unavailable","uri=/myapp2/product/" + productId);
|
||||
|
||||
MvcResult result = mockMvc.perform(get("/myapp2/product/" + productId))
|
||||
.andExpect(status().isInternalServerError()).andReturn();
|
||||
|
||||
ErrorResponse errorResponse = objectMapper.readValue(result.getResponse().getContentAsString(), ErrorResponse.class);
|
||||
|
||||
assertEquals(expectedError.getCode(), errorResponse.getCode());
|
||||
assertEquals(expectedError.getMessage(), errorResponse.getMessage());
|
||||
assertEquals(expectedError.getStatus(), errorResponse.getStatus());
|
||||
assertEquals(expectedError.getDetails(), errorResponse.getDetails());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductIsNotFound_whenGetProductCalled_ThenReturnInternalServerError() throws Exception {
|
||||
String productId = "test";
|
||||
|
||||
stubFor(WireMock.get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse()
|
||||
.withStatus(HttpStatus.NOT_FOUND.value())));
|
||||
|
||||
ErrorResponse expectedError = new ErrorResponse(HttpStatus.NOT_FOUND,
|
||||
"Product not found","uri=/myapp2/product/" + productId);
|
||||
|
||||
MvcResult result = mockMvc.perform(get("/myapp2/product/" + productId))
|
||||
.andExpect(status().isNotFound()).andReturn();
|
||||
|
||||
ErrorResponse errorResponse = objectMapper.readValue(result.getResponse().getContentAsString(), ErrorResponse.class);
|
||||
|
||||
assertEquals(expectedError.getCode(), errorResponse.getCode());
|
||||
assertEquals(expectedError.getMessage(), errorResponse.getMessage());
|
||||
assertEquals(expectedError.getStatus(), errorResponse.getStatus());
|
||||
assertEquals(expectedError.getDetails(), errorResponse.getDetails());
|
||||
}
|
||||
}
|
||||
-92
@@ -1,92 +0,0 @@
|
||||
package com.baeldung.core.defaulterrorhandling.client;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.core.ExampleApplication;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
|
||||
import feign.FeignException;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ExampleApplication.class)
|
||||
public class ProductClientUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProductClient productClient;
|
||||
|
||||
private WireMockServer wireMockServer;
|
||||
|
||||
@Before
|
||||
public void startWireMockServer() {
|
||||
wireMockServer = new WireMockServer(8084);
|
||||
configureFor("localhost", 8084);
|
||||
wireMockServer.start();
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void stopWireMockServer() {
|
||||
wireMockServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductIsAvailable_whenGetProductCalled_thenReturnMatchingProduct() {
|
||||
String productId = "test";
|
||||
|
||||
String productResponse = "{ " +
|
||||
" \"id\":\"test\",\n" +
|
||||
" \"productName\":\"Watermelon\",\n" +
|
||||
" \"price\":12\n" +
|
||||
"}";
|
||||
|
||||
stubFor(get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse()
|
||||
.withStatus(HttpStatus.OK.value())
|
||||
.withHeader("Content-Type", "application/json")
|
||||
.withBody(productResponse)));
|
||||
|
||||
Product product = productClient.getProduct(productId);
|
||||
|
||||
assertEquals(productId, product.getId());
|
||||
assertEquals("Watermelon", product.getProductName());
|
||||
assertEquals(12.00d, product.getPrice(), 0.00d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductApiIsNotAvailable_whenGetProductCalled_thenThrowFeignException() {
|
||||
String productId = "test";
|
||||
|
||||
stubFor(get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse()
|
||||
.withStatus(HttpStatus.SERVICE_UNAVAILABLE.value())));
|
||||
|
||||
assertThrows(FeignException.class, () -> productClient.getProduct(productId));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductIdNotFound_whenGetProductCalled_thenThrowFeignException() {
|
||||
String productId = "test";
|
||||
|
||||
stubFor(get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse()
|
||||
.withStatus(HttpStatus.NOT_FOUND.value())));
|
||||
|
||||
assertThrows(FeignException.class, () -> productClient.getProduct(productId));
|
||||
}
|
||||
}
|
||||
-76
@@ -1,76 +0,0 @@
|
||||
package com.baeldung.core.defaulterrorhandling.controller;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.client.ProductClient;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(ProductController.class)
|
||||
@ImportAutoConfiguration({FeignAutoConfiguration.class, TestControllerAdvice.class})
|
||||
@EnableWebMvc
|
||||
public class ProductControllerUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ProductClient productClient;
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private WireMockServer wireMockServer;
|
||||
|
||||
@Before
|
||||
public void startWireMockServer() {
|
||||
wireMockServer = new WireMockServer(options().dynamicPort());
|
||||
wireMockServer.start();
|
||||
configureFor("localhost", wireMockServer.port());
|
||||
}
|
||||
|
||||
@After
|
||||
public void stopWireMockServer() {
|
||||
wireMockServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProductServiceIsnotAvailable_whenGetProductCalled_thenReturnInternalServerError() throws Exception {
|
||||
String productId = "test";
|
||||
|
||||
stubFor(WireMock.get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse().withStatus(HttpStatus.SERVICE_UNAVAILABLE.value())));
|
||||
|
||||
mockMvc.perform(get("/myapp1/product/" + productId))
|
||||
.andExpect(status().is(HttpStatus.INTERNAL_SERVER_ERROR.value()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenProductIsNotFound_whenGetProductCalled_thenReturnBadeRequestError() throws Exception {
|
||||
String productId = "test";
|
||||
|
||||
stubFor(WireMock.get(urlEqualTo("/product/" + productId))
|
||||
.willReturn(aResponse().withStatus(HttpStatus.NOT_FOUND.value())));
|
||||
|
||||
mockMvc.perform(get("/myapp1/product/" +productId))
|
||||
.andExpect(status().is(HttpStatus.INTERNAL_SERVER_ERROR.value()));
|
||||
}
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.core.defaulterrorhandling.controller;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import feign.FeignException;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class TestControllerAdvice {
|
||||
|
||||
@ExceptionHandler({FeignException.class})
|
||||
public ResponseEntity<Object> handleFeignException(FeignException exception) {
|
||||
return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user