Skip to content

Commit

Permalink
Merge pull request #336 from vaskocuturilo/dv000288_fix_british_regio…
Browse files Browse the repository at this point in the history
…n_parse_plates

DV-000288: Fix British region parse plates
  • Loading branch information
vaskocuturilo authored Oct 22, 2023
2 parents 990ec12 + 6375af9 commit b8cc925
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 43 deletions.
2 changes: 1 addition & 1 deletion cypress/fixtures/regions.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
{
"country": "british",
"region": "AA67",
"region": "AA67 VXD",
"result": "Anglia, Peterborough",
"description": "AA"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -36,16 +38,23 @@ public class BritishService {

@Cacheable(value = "british_region", key = "#region")
public BritishRegionModel getBritishPlatesByRegion(final String region) throws RegionNotFoundException {
Optional<BritishEntity> britishRegion = getOptionalBritishEntity();
Optional<BritishAgeEntity> britishAgeEntity = getOptionalBritishAgeEntity();

log.info("Start method getBritishPlatesByRegion");
Optional<BritishEntity> britishRegion = britishRepo.findByRegion(getFirstTwoSymbols(region));
Optional<BritishAgeEntity> 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);
Expand All @@ -66,4 +75,12 @@ public Iterable<BritishEntity> getAllRegions() {
log.info("Start method getAllRegions");
return britishRepo.findAll();
}

public Optional<BritishEntity> getOptionalBritishEntity() {
return Optional.empty();
}

public Optional<BritishAgeEntity> getOptionalBritishAgeEntity() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/regions/simpleregions/util/DetailPlate.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/regions/simpleregions/util/RegionParse.java
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 12 additions & 11 deletions src/main/java/com/regions/simpleregions/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 10 additions & 20 deletions src/test/java/com/regions/simpleregions/util/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit b8cc925

Please sign in to comment.