diff --git a/cypress/fixtures/regions.json b/cypress/fixtures/regions.json index 37cb4004..e84c310c 100644 --- a/cypress/fixtures/regions.json +++ b/cypress/fixtures/regions.json @@ -25,7 +25,7 @@ }, { "country": "british", - "region": "AA67", + "region": "AA67 VXD", "result": "Anglia, Peterborough", "description": "AA" }, diff --git a/src/main/java/com/regions/simpleregions/service/BritishService.java b/src/main/java/com/regions/simpleregions/service/BritishService.java index 2780784a..2a5a26c8 100644 --- a/src/main/java/com/regions/simpleregions/service/BritishService.java +++ b/src/main/java/com/regions/simpleregions/service/BritishService.java @@ -8,6 +8,7 @@ import com.regions.simpleregions.model.BritishRegionModel; import com.regions.simpleregions.respository.BritishAgeRepo; import com.regions.simpleregions.respository.BritishRepo; +import com.regions.simpleregions.util.RegionParse; import lombok.Data; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Value; @@ -17,6 +18,7 @@ import java.util.List; import java.util.Optional; +import static com.regions.simpleregions.util.DetailPlate.getPlateDetail; import static com.regions.simpleregions.util.Utils.getFirstTwoSymbols; import static com.regions.simpleregions.util.Utils.getLastTwoSymbols; @@ -36,16 +38,23 @@ public class BritishService { @Cacheable(value = "british_region", key = "#region") public BritishRegionModel getBritishPlatesByRegion(final String region) throws RegionNotFoundException { + Optional britishRegion = getOptionalBritishEntity(); + Optional britishAgeEntity = getOptionalBritishAgeEntity(); + log.info("Start method getBritishPlatesByRegion"); - Optional britishRegion = britishRepo.findByRegion(getFirstTwoSymbols(region)); - Optional britishAgeEntity = britishAgeRepo.findByCode(getLastTwoSymbols(region)); - if (britishRegion.isEmpty()) { - throw new RegionNotFoundException(String.format(regionNotFound, region)); - } + final RegionParse details = getPlateDetail(region); + if (details.getLetter() == 5 && details.getNumber() == 2 && details.getSpace() == 1) { + britishRegion = britishRepo.findByRegion(getFirstTwoSymbols(region)); + britishAgeEntity = britishAgeRepo.findByCode(getLastTwoSymbols(region)); - if (britishAgeEntity.isEmpty()) { - throw new RegionNotFoundException(String.format(regionNotFound, region)); + if (britishRegion.isEmpty()) { + throw new RegionNotFoundException(String.format(regionNotFound, region)); + } + + if (britishAgeEntity.isEmpty()) { + throw new RegionNotFoundException(String.format(regionNotFound, region)); + } } return BritishRegionModel.toModelRegion(britishRegion, britishAgeEntity); @@ -66,4 +75,12 @@ public Iterable getAllRegions() { log.info("Start method getAllRegions"); return britishRepo.findAll(); } + + public Optional getOptionalBritishEntity() { + return Optional.empty(); + } + + public Optional getOptionalBritishAgeEntity() { + return Optional.empty(); + } } diff --git a/src/main/java/com/regions/simpleregions/service/PolandDiplomaticService.java b/src/main/java/com/regions/simpleregions/service/PolandDiplomaticService.java index a9c75d0a..510d9d8d 100644 --- a/src/main/java/com/regions/simpleregions/service/PolandDiplomaticService.java +++ b/src/main/java/com/regions/simpleregions/service/PolandDiplomaticService.java @@ -44,9 +44,9 @@ public PolandDiplomaticModel getPolandPlatesByRegion(final String region) throws .orElseThrow(() -> new RegionNotFoundException(String.format(regionNotFound, region)))); - final String test = getDestinationCode(region); + final String destinationCode = getDestinationCode(region); - return PolandDiplomaticModel.toModel(polandDiplomaticRegion, test); + return PolandDiplomaticModel.toModel(polandDiplomaticRegion, destinationCode); } @Cacheable(value = "poland_diplomatic_description", key = "#description") diff --git a/src/main/java/com/regions/simpleregions/util/DetailPlate.java b/src/main/java/com/regions/simpleregions/util/DetailPlate.java new file mode 100644 index 00000000..589a42c9 --- /dev/null +++ b/src/main/java/com/regions/simpleregions/util/DetailPlate.java @@ -0,0 +1,26 @@ +package com.regions.simpleregions.util; + +public class DetailPlate { + private DetailPlate() { + throw new IllegalStateException("Utility class"); + } + + public static RegionParse getPlateDetail(final String region) { + int letter = 0; + int number = 0; + int space = 0; + + for (char c : region.toCharArray()) { + if (Character.isLetter(c)) { + letter++; + } else if (Character.isDigit(c)) { + number++; + } else if (Character.isSpaceChar(c)) { + space++; + } else { + throw new IllegalStateException("This item is incorrect"); + } + } + return new RegionParse(letter, number, space); + } +} diff --git a/src/main/java/com/regions/simpleregions/util/RegionParse.java b/src/main/java/com/regions/simpleregions/util/RegionParse.java new file mode 100644 index 00000000..6910de1b --- /dev/null +++ b/src/main/java/com/regions/simpleregions/util/RegionParse.java @@ -0,0 +1,14 @@ +package com.regions.simpleregions.util; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +public class RegionParse { + private int letter; + private int number; + private int space; +} diff --git a/src/main/java/com/regions/simpleregions/util/Utils.java b/src/main/java/com/regions/simpleregions/util/Utils.java index cad2d6c5..596663c1 100644 --- a/src/main/java/com/regions/simpleregions/util/Utils.java +++ b/src/main/java/com/regions/simpleregions/util/Utils.java @@ -16,27 +16,28 @@ public static String getFirstTwoSymbols(String inputData) { throw new MissingFormatArgumentException(String.format(IS_EMPTY_DATA, inputData)); } - if (inputData.length() > 4) { - throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 4)); + if (inputData.length() > 8) { + throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 8)); } - if (inputData.length() < 4) { - throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 4)); + if (inputData.length() < 8) { + throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 8)); } - return inputData.length() < 2 ? inputData : inputData.substring(0, 2); + return inputData.substring(0, 2); } public static String getLastTwoSymbols(String inputData) { if (inputData.isEmpty()) { throw new MissingFormatArgumentException(String.format(IS_EMPTY_DATA, inputData)); } - if (inputData.length() > 4) { - throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 4)); + if (inputData.length() > 8) { + throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 8)); } - if (inputData.length() < 4) { - throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 4)); + if (inputData.length() < 8) { + throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 8)); } - return inputData.substring(Math.max(inputData.length() - 2, 0)); + + return inputData.substring(0, inputData.indexOf(' ')).substring(2, 4); } public static String getFirstThreeSymbols(String inputData) { @@ -49,7 +50,7 @@ public static String getFirstThreeSymbols(String inputData) { if (inputData.length() < 6) { throw new MissingFormatArgumentException(String.format(DATA_SHOULD_BE_EQUALS, inputData, 6)); } - return inputData.length() < 3 ? inputData : inputData.substring(0, 3); + return inputData.substring(0, 3); } public static String getLastThreeSymbols(String inputData) { diff --git a/src/test/java/com/regions/simpleregions/countries/SimpleIntegrationBritishTest.java b/src/test/java/com/regions/simpleregions/countries/SimpleIntegrationBritishTest.java index 9715de71..1dbcecd3 100644 --- a/src/test/java/com/regions/simpleregions/countries/SimpleIntegrationBritishTest.java +++ b/src/test/java/com/regions/simpleregions/countries/SimpleIntegrationBritishTest.java @@ -39,7 +39,7 @@ class SimpleIntegrationBritishTest { @Test void getRegionHandle_whenGetBritishByRegion_thenStatus200() throws Exception { mockMvc.perform(MockMvcRequestBuilders - .get(PATH + "/region/AA11") + .get(PATH + "/region/AA11 VXD") .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON).header(headerName, authToken)) .andExpect(status().isOk()) @@ -99,7 +99,7 @@ void getRegionHandle_whenExceptionBritishByDescription_thenStatus400() throws Ex @Test void getRegionHandle_whenExceptionBritishByRegion_thenStatus404() throws Exception { - String region = "AAA1"; + String region = "AA99 VXD"; mockMvc.perform(MockMvcRequestBuilders .get(PATH + "/region/" + region) .contentType(MediaType.APPLICATION_JSON) diff --git a/src/test/java/com/regions/simpleregions/util/UtilsTest.java b/src/test/java/com/regions/simpleregions/util/UtilsTest.java index 5961af61..6aeab89e 100644 --- a/src/test/java/com/regions/simpleregions/util/UtilsTest.java +++ b/src/test/java/com/regions/simpleregions/util/UtilsTest.java @@ -10,65 +10,55 @@ import static org.junit.jupiter.api.Assertions.assertThrows; class UtilsTest { - private Utils utils; - @Test void getFirstTwoSymbols() { - String text = "MJ56"; - String actual = utils.getFirstTwoSymbols(text); + String text = "MJ56 VXD"; + String actual = Utils.getFirstTwoSymbols(text); assertThat(actual).isEqualTo("MJ"); } @ParameterizedTest @ValueSource(strings = {"", "123", "123456"}) void getFirstSymbolsExceptionsTest(String testValue) { - assertThrows(MissingFormatArgumentException.class, () -> { - utils.getFirstTwoSymbols(testValue); - }); + assertThrows(MissingFormatArgumentException.class, () -> Utils.getFirstTwoSymbols(testValue)); } @Test void getLastTwoSymbols() { - String text = "MJ56"; - String actual = utils.getLastTwoSymbols(text); + String text = "MJ56 VXD"; + String actual = Utils.getLastTwoSymbols(text); assertThat(actual).isEqualTo("56"); } @ParameterizedTest @ValueSource(strings = {"", "123", "123456"}) void getLastSymbolsExceptionsTest(String testValue) { - assertThrows(MissingFormatArgumentException.class, () -> { - utils.getLastTwoSymbols(testValue); - }); + assertThrows(MissingFormatArgumentException.class, () -> Utils.getLastTwoSymbols(testValue)); } @Test void getFirstThreeSymbols() { String text = "076140"; - String actual = utils.getFirstThreeSymbols(text); + String actual = Utils.getFirstThreeSymbols(text); assertThat(actual).isEqualTo("076"); } @ParameterizedTest @ValueSource(strings = {"", "07614", "0761401"}) void getFirstThreeSymbolsExceptionsTest(String testValue) { - assertThrows(MissingFormatArgumentException.class, () -> { - utils.getFirstThreeSymbols(testValue); - }); + assertThrows(MissingFormatArgumentException.class, () -> Utils.getFirstThreeSymbols(testValue)); } @Test void getLastThreeSymbols() { String text = "076140"; - String actual = utils.getLastThreeSymbols(text); + String actual = Utils.getLastThreeSymbols(text); assertThat(actual).isEqualTo("140"); } @ParameterizedTest @ValueSource(strings = {"", "07614", "0761401"}) void getLastThreeSymbolsExceptionsTest(String testValue) { - assertThrows(MissingFormatArgumentException.class, () -> { - utils.getLastThreeSymbols(testValue); - }); + assertThrows(MissingFormatArgumentException.class, () -> Utils.getLastThreeSymbols(testValue)); } } \ No newline at end of file