JAVA-17818 Split or move spring-cloud-openfeign module (conti-2) (#13485)

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1
2023-02-20 18:18:26 +02:00
committed by GitHub
parent 639de9687e
commit 09382f545e
42 changed files with 269 additions and 140 deletions
@@ -0,0 +1,50 @@
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);
}
}
@@ -0,0 +1,63 @@
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));
}
}
@@ -0,0 +1,102 @@
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());
}
}
@@ -0,0 +1,92 @@
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));
}
}
@@ -0,0 +1,76 @@
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()));
}
}
@@ -0,0 +1,17 @@
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);
}
}