diff --git a/src/main/java/com/esp/espflow/mappers/ExtractChipIsFromStringMapper.java b/src/main/java/com/esp/espflow/mappers/ExtractChipIsFromStringMapper.java index 4a71680..13b5d4e 100644 --- a/src/main/java/com/esp/espflow/mappers/ExtractChipIsFromStringMapper.java +++ b/src/main/java/com/esp/espflow/mappers/ExtractChipIsFromStringMapper.java @@ -20,14 +20,16 @@ private ExtractChipIsFromStringMapper() {} * @return A {@link String} */ public String getChipIsFromThisString(String input) { - String chipRegex = "(?s)Chip is (\\S+).*?Features:"; - Pattern pattern = Pattern.compile(chipRegex); - Matcher matcher = pattern.matcher(input); + final String notNullInput = Objects.isNull(input) ? StringUtils.EMPTY : input; + final String chipRegex = "(?s)Chip is (\\S+).*?Features:"; + final Pattern pattern = Pattern.compile(chipRegex); + final Matcher matcher = pattern.matcher(notNullInput); String chipIs = StringUtils.EMPTY; if (matcher.find()) { chipIs = matcher.group(1); } - return Objects.equals(chipIs, StringUtils.EMPTY) ? "Can`t parsed this Chip" : - chipIs.split("Features:")[0]; + return Objects.equals(chipIs, StringUtils.EMPTY) + ? "Can`t parsed this Chip" + : chipIs.split("Features:")[0]; } } diff --git a/src/test/java/com/esp/espflow/mappers/ExtractChipIsFromStringMapperTest.java b/src/test/java/com/esp/espflow/mappers/ExtractChipIsFromStringMapperTest.java new file mode 100644 index 0000000..25ef953 --- /dev/null +++ b/src/test/java/com/esp/espflow/mappers/ExtractChipIsFromStringMapperTest.java @@ -0,0 +1,49 @@ +package com.esp.espflow.mappers; + +import com.esp.espflow.mappers.provider.ExtractChipIsFromStringMapperFailureProvider; +import com.esp.espflow.mappers.provider.ExtractChipIsFromStringMapperSuccessProvider; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ArgumentsSource; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("When extracting the 'Chip is ESP82XX' from the text processed in the Xterm, we check for possible " + + "errors and then send the event correctly in the notification panel.") +class ExtractChipIsFromStringMapperTest { + + @ParameterizedTest + @ArgumentsSource(ExtractChipIsFromStringMapperSuccessProvider.class) + @DisplayName("The chip is obtained from the input String") + void getChipIsFromThisString_Success(String inputLines) { + + final String chipIs = ExtractChipIsFromStringMapper.INSTANCE.getChipIsFromThisString(inputLines); + + assertThat(chipIs).isEqualTo("ESP8266EX"); + + } + + @ParameterizedTest + @ArgumentsSource(ExtractChipIsFromStringMapperFailureProvider.class) + @DisplayName("When the Chip cannot be parsed, cannot be matched because of lowercase f") + void attemptToParseTheChip_Failure(final String inputLine) { + + final String chipIs = ExtractChipIsFromStringMapper.INSTANCE.getChipIsFromThisString(inputLine); + + assertThat(chipIs).isEqualTo("Can`t parsed this Chip"); + + } + + @Test + @DisplayName("Input String is null, something happens when the text arrives at the Xterm and its text is null") + void attemptToParseTheChip_Failure2() { + + final String chipIs = ExtractChipIsFromStringMapper.INSTANCE.getChipIsFromThisString(null); + + assertThat(chipIs).isEqualTo("Can`t parsed this Chip"); + + } + + +} \ No newline at end of file diff --git a/src/test/java/com/esp/espflow/mappers/provider/ExtractChipIsFromStringMapperFailureProvider.java b/src/test/java/com/esp/espflow/mappers/provider/ExtractChipIsFromStringMapperFailureProvider.java new file mode 100644 index 0000000..f11a19f --- /dev/null +++ b/src/test/java/com/esp/espflow/mappers/provider/ExtractChipIsFromStringMapperFailureProvider.java @@ -0,0 +1,36 @@ +package com.esp.espflow.mappers.provider; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; + +import java.util.stream.Stream; + +public class ExtractChipIsFromStringMapperFailureProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + + String parseMePlease = "esptool.py --port COM5 --baud 115200 " + + "flash_id esptool.py v4.7.0 " + + "Serial port COM5 " + + "Connecting.... " + + "Detecting chip type... " + + "Unsupported detection protocol, switching and trying again... " + + "Connecting.... " + + "Detecting chip type... ESP8266 " + + "Chip is ESP8266EX " + + "features: WiFi " + + "Crystal is 26MHz " + + "MAC: 2c:f4:32:10:1d:bf " + + "Uploading stub... " + + "Running stub... " + + "Stub running... " + + "Manufacturer: 85 " + + "Device: 6014 " + + "Detected flash size: 1MB " + + "Hard resetting via RTS pin..."; + + return Stream.of(Arguments.of(parseMePlease)); + } +} diff --git a/src/test/java/com/esp/espflow/mappers/provider/ExtractChipIsFromStringMapperSuccessProvider.java b/src/test/java/com/esp/espflow/mappers/provider/ExtractChipIsFromStringMapperSuccessProvider.java new file mode 100644 index 0000000..97b2288 --- /dev/null +++ b/src/test/java/com/esp/espflow/mappers/provider/ExtractChipIsFromStringMapperSuccessProvider.java @@ -0,0 +1,35 @@ +package com.esp.espflow.mappers.provider; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; + +import java.util.stream.Stream; + +public class ExtractChipIsFromStringMapperSuccessProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + + String parseMePlease = "esptool.py --port COM5 --baud 115200 " + + "flash_id esptool.py v4.7.0 " + + "Serial port COM5 " + + "Connecting.... " + + "Detecting chip type... " + + "Unsupported detection protocol, switching and trying again... " + + "Connecting.... " + + "Detecting chip type... ESP8266 " + + "Chip is ESP8266EX " + + "Features: WiFi " + + "Crystal is 26MHz " + + "MAC: 2c:f4:32:10:1d:bf " + + "Uploading stub... " + + "Running stub... " + + "Stub running... " + + "Manufacturer: 85 " + + "Device: 6014 " + + "Detected flash size: 1MB " + + "Hard resetting via RTS pin..."; + return Stream.of(Arguments.of(parseMePlease)); + } +}