From ef481d19ee865911ed5ec5c9423c79d9242e4631 Mon Sep 17 00:00:00 2001 From: Erdem Date: Thu, 3 May 2018 22:02:16 +0300 Subject: [PATCH] BAEL-1746 added property editor implementation --- .../baeldung/propertyeditor/CreditCard.java | 41 +++++++++++++++++++ .../propertyeditor/CreditCardEditor.java | 39 ++++++++++++++++++ .../CreditCardRestController.java | 17 ++++++++ .../PropertyEditorApplication.java | 12 ++++++ .../propertyeditor/CreditCardEditorTest.java | 41 +++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCard.java create mode 100644 spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardEditor.java create mode 100644 spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardRestController.java create mode 100644 spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java create mode 100644 spring-rest/src/test/java/com/baeldung/propertyeditor/CreditCardEditorTest.java diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCard.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCard.java new file mode 100644 index 0000000000..2b1fbb9b6c --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCard.java @@ -0,0 +1,41 @@ +package com.baeldung.propertyeditor; + +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; + } + +} diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardEditor.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardEditor.java new file mode 100644 index 0000000000..4b1374a76a --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardEditor.java @@ -0,0 +1,39 @@ +package com.baeldung.propertyeditor; + +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); + } + } +} diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardRestController.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardRestController.java new file mode 100644 index 0000000000..bf8e0586f8 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardRestController.java @@ -0,0 +1,17 @@ +package com.baeldung.propertyeditor; + +import org.springframework.http.MediaType; +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; + +@RestController +@RequestMapping(value = "/credit-card") +public class CreditCardRestController { + + @GetMapping(value = "/parse/{card-no}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public CreditCard parseCreditCardNumber(@PathVariable("card-no") CreditCard creditCard) { + return creditCard; + } +} diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java new file mode 100644 index 0000000000..b0d75cd072 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java @@ -0,0 +1,12 @@ +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); + } +} diff --git a/spring-rest/src/test/java/com/baeldung/propertyeditor/CreditCardEditorTest.java b/spring-rest/src/test/java/com/baeldung/propertyeditor/CreditCardEditorTest.java new file mode 100644 index 0000000000..b7da905f13 --- /dev/null +++ b/spring-rest/src/test/java/com/baeldung/propertyeditor/CreditCardEditorTest.java @@ -0,0 +1,41 @@ +package com.baeldung.propertyeditor; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CreditCardEditorTest { + + 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()); + } + +}