Fixed conflicts

This commit is contained in:
amit2103
2019-11-25 00:24:40 +05:30
327 changed files with 4913 additions and 1361 deletions

View File

@@ -5,17 +5,14 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.changeport.CustomApplication;
import com.baeldung.produceimage.ImageApplication;
import com.baeldung.propertyeditor.PropertyEditorApplication;
import com.baeldung.responseheaders.ResponseHeadersApplication;
import com.baeldung.sampleapp.config.MainApplication;
import com.baeldung.web.log.app.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { CustomApplication.class, ImageApplication.class, PropertyEditorApplication.class,
ResponseHeadersApplication.class, Application.class, com.baeldung.web.upload.app.UploadApplication.class,
MainApplication.class})
@SpringBootTest(classes = { ImageApplication.class,
ResponseHeadersApplication.class,
com.baeldung.web.upload.app.UploadApplication.class,
})
public class SpringContextTest {
@Test

View File

@@ -1,17 +0,0 @@
package com.baeldung.changeport;
import java.util.Collections;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CustomApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(CustomApplication.class);
app.setDefaultProperties(Collections.singletonMap("server.port", "8083"));
app.run(args);
}
}

View File

@@ -1,15 +0,0 @@
package com.baeldung.changeport;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;
//@Component
public class ServerPortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8086);
}
}

View File

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

View File

@@ -1,36 +0,0 @@
package com.baeldung.propertyeditor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.propertyeditor.creditcard.CreditCard;
import com.baeldung.propertyeditor.exotictype.editor.CustomExoticTypeEditor;
import com.baeldung.propertyeditor.exotictype.model.ExoticType;
@RestController
@RequestMapping(value = "/property-editor")
public class PropertyEditorRestController {
@GetMapping(value = "/credit-card/{card-no}",
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public CreditCard parseCreditCardNumber(@PathVariable("card-no") CreditCard creditCard) {
return creditCard;
}
@GetMapping(value = "/exotic-type/{value}",
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ExoticType parseExoticType(@PathVariable("value") ExoticType exoticType) {
return exoticType;
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(ExoticType.class, new CustomExoticTypeEditor());
}
}

View File

@@ -1,41 +0,0 @@
package com.baeldung.propertyeditor.creditcard;
public class CreditCard {
private String rawCardNumber;
private Integer bankIdNo;
private Integer accountNo;
private Integer checkCode;
public String getRawCardNumber() {
return rawCardNumber;
}
public void setRawCardNumber(String rawCardNumber) {
this.rawCardNumber = rawCardNumber;
}
public Integer getBankIdNo() {
return bankIdNo;
}
public void setBankIdNo(Integer bankIdNo) {
this.bankIdNo = bankIdNo;
}
public Integer getAccountNo() {
return accountNo;
}
public void setAccountNo(Integer accountNo) {
this.accountNo = accountNo;
}
public Integer getCheckCode() {
return checkCode;
}
public void setCheckCode(Integer checkCode) {
this.checkCode = checkCode;
}
}

View File

@@ -1,39 +0,0 @@
package com.baeldung.propertyeditor.creditcard;
import java.beans.PropertyEditorSupport;
import org.springframework.util.StringUtils;
public class CreditCardEditor extends PropertyEditorSupport {
@Override
public String getAsText() {
CreditCard creditCard = (CreditCard) getValue();
return creditCard == null ? "" : creditCard.getRawCardNumber();
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.isEmpty(text)) {
setValue(null);
} else {
CreditCard creditCard = new CreditCard();
creditCard.setRawCardNumber(text);
String cardNo = text.replaceAll("-", "");
if (cardNo.length() != 16)
throw new IllegalArgumentException("Credit card format should be xxxx-xxxx-xxxx-xxxx");
try {
creditCard.setBankIdNo( Integer.valueOf(cardNo.substring(0, 6)) );
creditCard.setAccountNo( Integer.valueOf(cardNo.substring(6, cardNo.length() - 1)) );
creditCard.setCheckCode( Integer.valueOf(cardNo.substring(cardNo.length() - 1)) );
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException(nfe);
}
setValue(creditCard);
}
}
}

View File

@@ -1,23 +0,0 @@
package com.baeldung.propertyeditor.exotictype.editor;
import java.beans.PropertyEditorSupport;
import com.baeldung.propertyeditor.exotictype.model.ExoticType;
public class CustomExoticTypeEditor extends PropertyEditorSupport {
@Override
public String getAsText() {
ExoticType exoticType = (ExoticType) getValue();
return exoticType == null ? "" : exoticType.getName();
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
ExoticType exoticType = new ExoticType();
exoticType.setName(text.toUpperCase());
setValue(exoticType);
}
}

View File

@@ -1,14 +0,0 @@
package com.baeldung.propertyeditor.exotictype.model;
public class ExoticType {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -1,41 +0,0 @@
package com.baeldung.web.log.app;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import com.baeldung.web.log.config.CustomeRequestLoggingFilter;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@EnableAutoConfiguration
@ComponentScan("com.baeldung.web.log")
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.baeldung.web.log");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
container.addFilter("customRequestLoggingFilter", CustomeRequestLoggingFilter.class).addMappingForServletNames(null, false, "dispatcher");
}
}

View File

@@ -1,44 +0,0 @@
package com.baeldung.web.log.app;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import com.baeldung.web.log.util.RequestLoggingUtil;
@Component
public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
private final static Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String postData;
HttpServletRequest requestCacheWrapperObject = null;
try {
// Uncomment to produce the stream closed issue
// postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream());
// To overcome request stream closed issue
requestCacheWrapperObject = new ContentCachingRequestWrapper(request);
requestCacheWrapperObject.getParameterMap();
} catch (Exception exception) {
exception.printStackTrace();
} finally {
postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject);
LOGGER.info("REQUEST DATA: " + postData);
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
LOGGER.info("RESPONSE: " + response.getStatus());
}
}

View File

@@ -1,12 +0,0 @@
package com.baeldung.web.log.config;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
public class CustomeRequestLoggingFilter extends CommonsRequestLoggingFilter {
public CustomeRequestLoggingFilter() {
super.setIncludeQueryString(true);
super.setIncludePayload(true);
super.setMaxPayloadLength(10000);
}
}

View File

@@ -1,20 +0,0 @@
package com.baeldung.web.log.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
@Configuration
public class RequestLoggingFilterConfig {
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(10000);
filter.setIncludeHeaders(false);
filter.setAfterMessagePrefix("REQUEST DATA : ");
return filter;
}
}

View File

@@ -1,19 +0,0 @@
package com.baeldung.web.log.config;
import com.baeldung.web.log.app.TaxiFareRequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class TaxiFareMVCConfig implements WebMvcConfigurer {
@Autowired
private TaxiFareRequestInterceptor taxiFareRequestInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/");
}
}

View File

@@ -1,40 +0,0 @@
package com.baeldung.web.log.controller;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baeldung.web.log.data.RateCard;
import com.baeldung.web.log.data.TaxiRide;
import com.baeldung.web.log.service.TaxiFareCalculatorService;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TaxiFareController {
@Autowired
private TaxiFareCalculatorService taxiFareCalculatorService;
private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class);
@GetMapping("/taxifare/get/")
public RateCard getTaxiFare() {
LOGGER.debug("getTaxiFare() - START");
return new RateCard();
}
@PostMapping("/taxifare/calculate/")
public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) {
LOGGER.debug("calculateTaxiFare() - START");
String totalFare = taxiFareCalculatorService.calculateFare(taxiRide);
LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare);
LOGGER.debug("calculateTaxiFare() - END");
return totalFare;
}
}

View File

@@ -1,31 +0,0 @@
package com.baeldung.web.log.data;
public class RateCard {
private String nightSurcharge;
private String ratePerMile;
public RateCard() {
nightSurcharge = "Extra $ 100";
ratePerMile = "$ 10 Per Mile";
}
public String getNightSurcharge() {
return nightSurcharge;
}
public void setNightSurcharge(String nightSurcharge) {
this.nightSurcharge = nightSurcharge;
}
public String getRatePerMile() {
return ratePerMile;
}
public void setRatePerMile(String ratePerMile) {
this.ratePerMile = ratePerMile;
}
}

View File

@@ -1,33 +0,0 @@
package com.baeldung.web.log.data;
public class TaxiRide {
private Boolean isNightSurcharge;
private Long distanceInMile;
public TaxiRide() {
}
public TaxiRide(Boolean isNightSurcharge, Long distanceInMile) {
this.isNightSurcharge = isNightSurcharge;
this.distanceInMile = distanceInMile;
}
public Boolean getIsNightSurcharge() {
return isNightSurcharge;
}
public void setIsNightSurcharge(Boolean isNightSurcharge) {
this.isNightSurcharge = isNightSurcharge;
}
public Long getDistanceInMile() {
return distanceInMile;
}
public void setDistanceInMile(Long distanceInMile) {
this.distanceInMile = distanceInMile;
}
}

View File

@@ -1,14 +0,0 @@
package com.baeldung.web.log.service;
import com.baeldung.web.log.data.TaxiRide;
import org.springframework.stereotype.Service;
@Service
public class TaxiFareCalculatorService {
public String calculateFare(TaxiRide taxiRide) {
return String.valueOf((Long) (taxiRide.getIsNightSurcharge()
? taxiRide.getDistanceInMile() * 10 + 100
: taxiRide.getDistanceInMile() * 10));
}
}

View File

@@ -1,38 +0,0 @@
package com.baeldung.web.log.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;
public class RequestLoggingUtil {
public static String getStringFromInputStream(InputStream is) {
StringWriter writer = new StringWriter();
String encoding = "UTF-8";
try {
IOUtils.copy(is, writer, encoding);
} catch (IOException e) {
e.printStackTrace();
}
return writer.toString();
}
public static String readPayload(final HttpServletRequest request) throws IOException {
String payloadData = null;
ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (null != contentCachingRequestWrapper) {
byte[] buf = contentCachingRequestWrapper.getContentAsByteArray();
if (buf.length > 0) {
payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding());
}
}
return payloadData;
}
}

View File

@@ -1,41 +0,0 @@
package com.baeldung.propertyeditor.creditcard;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class CreditCardEditorUnitTest {
private CreditCardEditor creditCardEditor;
@Before
public void setup() {
creditCardEditor = new CreditCardEditor();
}
@Test(expected=IllegalArgumentException.class)
public void whenInvalidCardNoWithLessDigits_thenThrowsException() {
creditCardEditor.setAsText("123-123-123-123");
}
@Test(expected=IllegalArgumentException.class)
public void whenInvalidCardNoWithNonDigits_thenThrowsException() {
creditCardEditor.setAsText("1234-1234-xxxx-yyyy");
}
@Test
public void whenCardNoWithNonDigits_parseCreditCard() {
creditCardEditor.setAsText("1234-5678-9123-4560");
CreditCard creditCard = (CreditCard) creditCardEditor.getValue();
Assert.assertNotNull(creditCard);
Assert.assertEquals(123456, creditCard.getBankIdNo().intValue());
Assert.assertEquals(789123456, creditCard.getAccountNo().intValue());
Assert.assertEquals(0, creditCard.getCheckCode().intValue());
}
}

View File

@@ -1,71 +0,0 @@
package com.baeldung.web.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.HashMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.sampleapp.config.WebConfig;
import com.baeldung.sampleapp.web.dto.HeavyResource;
import com.baeldung.sampleapp.web.dto.HeavyResourceAddressOnly;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebConfig.class)
@WebAppConfiguration
public class HeavyResourceControllerIntegrationTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
private final ObjectMapper objectMapper = new ObjectMapper();
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void givenHeavyResource_whenSendPutRequest_thenCreateResource() throws Exception {
mockMvc.perform(put("/heavyresource/1")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(new HeavyResource(1, "Tom", "Jackson", 12, "heaven street")))
).andExpect(status().isOk());
}
@Test
public void givenNewAddressOfResource_whenExecutePatchRequest_thenUpdateResourcePartially() throws Exception {
mockMvc.perform(patch("/heavyresource/1")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(new HeavyResourceAddressOnly(1, "5th avenue")))
).andExpect(status().isOk());
}
@Test
public void givenNewAddressOfResource_whenExecutePatchGeneric_thenUpdateResourcePartially() throws Exception {
HashMap<String, Object> updates = new HashMap<>();
updates.put("address", "5th avenue");
mockMvc.perform(patch("/heavyresource/1")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(updates))
).andExpect(status().isOk());
}
}

View File

@@ -1,35 +0,0 @@
package com.baeldung.web.controller;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.web.log.app.Application;
import com.baeldung.web.log.data.TaxiRide;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TaxiFareControllerIntegrationTest {
@LocalServerPort
private int port;
@Test
public void givenRequest_whenFetchTaxiFareRateCard_thanOK() {
String URL = "http://localhost:" + port + "/spring-rest";
TestRestTemplate testRestTemplate = new TestRestTemplate();
TaxiRide taxiRide = new TaxiRide(true, 10l);
String fare = testRestTemplate.postForObject(
URL + "/taxifare/calculate/",
taxiRide, String.class);
assertThat(fare, equalTo("200"));
}
}