From 0bd309d4165ca9b411e4bb7120f0c517308727ce Mon Sep 17 00:00:00 2001 From: vedasole Date: Tue, 31 Dec 2024 06:59:45 +0530 Subject: [PATCH] refactor(address): remove unnecessary NotNull and NotBlank annotation and max-length and nullable validations --- .../ekartecommercebackend/entity/Address.java | 26 +-- .../entity/AddressTest.java | 169 ++++++++++++++++++ 2 files changed, 177 insertions(+), 18 deletions(-) create mode 100644 src/test/java/com/vedasole/ekartecommercebackend/entity/AddressTest.java diff --git a/src/main/java/com/vedasole/ekartecommercebackend/entity/Address.java b/src/main/java/com/vedasole/ekartecommercebackend/entity/Address.java index 4341572..54b4dbf 100644 --- a/src/main/java/com/vedasole/ekartecommercebackend/entity/Address.java +++ b/src/main/java/com/vedasole/ekartecommercebackend/entity/Address.java @@ -9,8 +9,6 @@ import org.springframework.validation.annotation.Validated; import javax.persistence.*; -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; import java.time.LocalDateTime; @AllArgsConstructor @@ -28,31 +26,23 @@ public class Address { @SequenceGenerator(name = "address_seq", allocationSize = 0) private long addressId; - @NotNull - @Size(max = 100) - @Column(name = "add_line1") + @Column(name = "add_line1", length = 100, nullable = false) private String addLine1; - @Size(max = 100) - @Column(name = "add_line2") + @Column(name = "add_line2", length = 100) private String addLine2; - @NotNull - @Size(max = 50) - @Column(name = "city") + + @Column(name = "city", length = 50, nullable = false) private String city; - @NotNull - @Size(max = 50) - @Column(name = "state") + + @Column(name = "state", length = 50, nullable = false) private String state; - @NotNull - @Size(max = 50) - @Column(name = "country") + @Column(name = "country", length = 50, nullable = false) private String country; - @NotNull @Column(name = "postal_code", length = 10, nullable = false) private int postalCode; @@ -82,4 +72,4 @@ public Address(String addLine1, String addLine2, String city, String state, Stri this.country = country; this.postalCode = postalCode; } -} +} \ No newline at end of file diff --git a/src/test/java/com/vedasole/ekartecommercebackend/entity/AddressTest.java b/src/test/java/com/vedasole/ekartecommercebackend/entity/AddressTest.java new file mode 100644 index 0000000..c49bb10 --- /dev/null +++ b/src/test/java/com/vedasole/ekartecommercebackend/entity/AddressTest.java @@ -0,0 +1,169 @@ +package com.vedasole.ekartecommercebackend.entity; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AddressTest { + + @Test + void shouldCreateAddressWithAllFieldsSetCorrectly() { + long addressId = 1L; + String addLine1 = "123 Main St"; + String addLine2 = "Apt 4B"; + String city = "Springfield"; + String state = "IL"; + String country = "USA"; + int postalCode = 62701; + + Address address = new Address(addressId, addLine1, addLine2, city, state, country, postalCode); + + assertThat(address.getAddressId()).isEqualTo(addressId); + assertThat(address.getAddLine1()).isEqualTo(addLine1); + assertThat(address.getAddLine2()).isEqualTo(addLine2); + assertThat(address.getCity()).isEqualTo(city); + assertThat(address.getState()).isEqualTo(state); + assertThat(address.getCountry()).isEqualTo(country); + assertThat(address.getPostalCode()).isEqualTo(postalCode); + } + + @Test + void shouldCreateAddressWithMinimumRequiredFields() { + String addLine1 = "123 Main St"; + String city = "Springfield"; + String state = "IL"; + String country = "USA"; + int postalCode = 62701; + + Address address = new Address(addLine1, null, city, state, country, postalCode); + + assertThat(address.getAddLine1()).isEqualTo(addLine1); + assertThat(address.getAddLine2()).isNull(); + assertThat(address.getCity()).isEqualTo(city); + assertThat(address.getState()).isEqualTo(state); + assertThat(address.getCountry()).isEqualTo(country); + assertThat(address.getPostalCode()).isEqualTo(postalCode); + assertEquals(0,address.getAddressId()); + assertThat(address.getCreatedAt()).isNull(); + assertThat(address.getUpdatedAt()).isNull(); + } + + @Test + void shouldCreateAddressUsingBuilder() { + LocalDateTime now = LocalDateTime.now(); + Address address = Address.builder() + .addressId(1L) + .addLine1("123 Main St") + .addLine2("Apt 4B") + .city("Springfield") + .state("IL") + .country("USA") + .postalCode(62701) + .createdAt(now) + .updatedAt(now) + .build(); + + assertThat(address.getAddressId()).isEqualTo(1L); + assertThat(address.getAddLine1()).isEqualTo("123 Main St"); + assertThat(address.getAddLine2()).isEqualTo("Apt 4B"); + assertThat(address.getCity()).isEqualTo("Springfield"); + assertThat(address.getState()).isEqualTo("IL"); + assertThat(address.getCountry()).isEqualTo("USA"); + assertThat(address.getPostalCode()).isEqualTo(62701); + assertThat(address.getCreatedAt()).isEqualTo(now); + assertThat(address.getUpdatedAt()).isEqualTo(now); + } + + @Test + void shouldCreateAddressUsingNoArgsConstructorAndSetters() { + Address address = new Address(); + address.setAddressId(1L); + address.setAddLine1("123 Main St"); + address.setAddLine2("Apt 4B"); + address.setCity("Springfield"); + address.setState("IL"); + address.setCountry("USA"); + address.setPostalCode(62701); + LocalDateTime now = LocalDateTime.now(); + address.setCreatedAt(now); + address.setUpdatedAt(now); + + assertThat(address.getAddressId()).isEqualTo(1L); + assertThat(address.getAddLine1()).isEqualTo("123 Main St"); + assertThat(address.getAddLine2()).isEqualTo("Apt 4B"); + assertThat(address.getCity()).isEqualTo("Springfield"); + assertThat(address.getState()).isEqualTo("IL"); + assertThat(address.getCountry()).isEqualTo("USA"); + assertThat(address.getPostalCode()).isEqualTo(62701); + assertThat(address.getCreatedAt()).isEqualTo(now); + assertThat(address.getUpdatedAt()).isEqualTo(now); + } + + @Test + @Disabled("This test is disabled because the postalCode field is not negative") + void shouldThrowExceptionWhenSettingNegativeValueForPostalCode() { + Address address = new Address(); + int negativePostalCode = -12345; + + assertThatThrownBy(() -> address.setPostalCode(negativePostalCode)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("Postal code must be a non-negative number"); + } + + @Test + @Disabled("This test is disabled because the addLine1 field is not truncated") + void shouldTruncateAddLine1IfExceeds100Characters() { + String longAddLine1 = "A".repeat(101); + String expectedAddLine1 = "A".repeat(100); + + Address address = new Address(); + address.setAddLine1(longAddLine1); + + assertThat(address.getAddLine1()).isEqualTo(expectedAddLine1); + assertThat(address.getAddLine1()).hasSize(100); + } + + @Test + @Disabled("This test is disabled because the addLine2 field is not truncated") + void shouldTruncateCityIfExceeds50Characters() { + String longCity = "A".repeat(51); + String expectedCity = "A".repeat(50); + + Address address = new Address(); + address.setCity(longCity); + + assertThat(address.getCity()).isEqualTo(expectedCity); + assertThat(address.getCity()).hasSize(50); + } + + @Test + @Disabled("This test is disabled because the state field is not truncated") + void shouldTruncateStateIfExceeds50Characters() { + String longState = "A".repeat(51); + String expectedState = "A".repeat(50); + + Address address = new Address(); + address.setState(longState); + + assertThat(address.getState()).isEqualTo(expectedState); + assertThat(address.getState()).hasSize(50); + } + @Test + @Disabled("This test is disabled because the country field is not truncated") + void shouldTruncateCountryIfExceeds50Characters() { + String longCountry = "A".repeat(51); + String expectedCountry = "A".repeat(50); + + Address address = new Address(); + address.setCountry(longCountry); + + assertThat(address.getCountry()).isEqualTo(expectedCountry); + assertThat(address.getCountry()).hasSize(50); + } + +} \ No newline at end of file