Skip to content

Commit

Permalink
🚧 #70
Browse files Browse the repository at this point in the history
  • Loading branch information
rucko24 committed Oct 30, 2024
1 parent b602528 commit fc30cbd
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.esp.espflow.mappers;

import com.esp.espflow.entity.EspDeviceInfo;
import com.esp.espflow.entity.EspDeviceWithTotalDevices;
import com.esp.espflow.mappers.provider.EspDeviceWithTotalDevicesMapperProvider;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

import static org.assertj.core.api.Assertions.assertThat;

class EspDeviceWithTotalDevicesMapperTest {

@ParameterizedTest
@ArgumentsSource(EspDeviceWithTotalDevicesMapperProvider.class)
@DisplayName("Counting 3 devices and mapping to EspDeviceWithTotalDevices")
void espDeviceWithTotalDevices(EspDeviceInfo actualEspDeviceInfo,
EspDeviceWithTotalDevices expectedEspDeviceWithTotalDevices,
long totalDevices) {

var actualEspDeviceWithTotalDevices = EspDeviceWithTotalDevicesMapper.INSTANCE.espDeviceWithTotalDevices(actualEspDeviceInfo, totalDevices);

assertThat(actualEspDeviceWithTotalDevices)
.usingRecursiveAssertion()
.isEqualTo(expectedEspDeviceWithTotalDevices);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.esp.espflow.mappers.provider;

import com.esp.espflow.entity.EspDeviceInfo;
import com.esp.espflow.entity.EspDeviceWithTotalDevices;
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 EspDeviceWithTotalDevicesMapperProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {

long totalDevices = 3L;

EspDeviceInfo actualEspDeviceInfo = EspDeviceInfo.builder()
.port("/dev/ttyUSB0")
.descriptivePortName("/dev/ttyUSB0")
.chipType("ESP8266")
.chipIs("ESP8266EX")
.crystalIs("24MHz")
.macAddress("2c:f4:32:10:1d:bf")
.decimal("2097152")
.hex("200000")
.detectedFlashSize("2MB")
.build();

EspDeviceWithTotalDevices expectedEspDeviceWithTotalDevices = EspDeviceWithTotalDevices.builder()
.espDeviceInfo(actualEspDeviceInfo)
.totalDevices(totalDevices)
.build();

return Stream.of(Arguments.of(actualEspDeviceInfo, expectedEspDeviceWithTotalDevices, totalDevices));

}
}
69 changes: 69 additions & 0 deletions src/test/java/com/esp/espflow/service/EsptoolPathServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.esp.espflow.service;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.SetSystemProperty;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
class EsptoolPathServiceTest {

@InjectMocks
private EsptoolPathService esptoolPathService;

@Test
@SetSystemProperty(key = "os.name", value = "window")
@DisplayName("Checking temporary directory with esptool Windows")
void esptoolPathWin() {

assertThat(esptoolPathService.esptoolPath())
.isEqualTo("/tmp/esptool-bundle-dir/esptool-winx64/esptool.exe");

}

@Test
@SetSystemProperty(key = "os.name", value = "linux")
@DisplayName("Checking temporary directory with esptool Linux")
void esptoolPathLinux() {

assertThat(esptoolPathService.esptoolPath())
.isEqualTo("/tmp/esptool-bundle-dir/esptool-linux-amd64/esptool");

}

@Test
@SetSystemProperty(key = "os.name", value = "mac os")
@DisplayName("Checking esptool.py MacOs")
void esptoolPathMac() {

assertThat(esptoolPathService.esptoolPath())
.isEqualTo("esptool.py");

}

@Test
@SetSystemProperty(key = "os.name", value = "freebsd")
@DisplayName("Checking esptool.py FreeBSD")
void esptoolPathFreeBSD() {

assertThat(esptoolPathService.esptoolPath())
.isEqualTo("esptool.py");

}

@Test
@SetSystemProperty(key = "os.name", value = "WTFOS")
@DisplayName("OS is not recognized")
void esptoolPathWtfOs() {

assertThat(esptoolPathService.esptoolPath())
.isEqualTo(StringUtils.EMPTY);

}

}
55 changes: 53 additions & 2 deletions src/test/java/com/esp/espflow/service/EsptoolServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.esp.espflow.entity.EspDeviceInfo;
import com.esp.espflow.enums.BaudRates;
import com.esp.espflow.exceptions.CanNotBeReadDeviceException;
import com.esp.espflow.service.provider.EsptoolServiceArgumentsProvider;
import com.esp.espflow.service.provider.EsptoolServiceNoFlashSizeArgumentsProvider;
import com.esp.espflow.service.provider.EsptoolServiceRawFlashIdFromPortArgumentsProvider;
Expand All @@ -15,15 +16,19 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junitpioneer.jupiter.SetSystemProperty;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.Set;

import static com.esp.espflow.util.EspFlowConstants.BAUD_RATE;
import static com.esp.espflow.util.EspFlowConstants.ESPTOOL_PY;
import static com.esp.espflow.util.EspFlowConstants.ESPTOOL_PY_NOT_FOUND;
import static com.esp.espflow.util.EspFlowConstants.ESPTOOL_PY_VERSION;
import static com.esp.espflow.util.EspFlowConstants.FLASH_ID;
import static com.esp.espflow.util.EspFlowConstants.PORT;
Expand Down Expand Up @@ -54,6 +59,28 @@ class EsptoolServiceTest {
@Mock
private EsptoolPathService esptoolPathService;

@Test
@SetSystemProperty(key = "os.name", value = "linux")
void readAllDevices() {

when(comPortService.getPortsListWithFriendlyName())
.thenReturn(Set.of("/dev/ttyUSB1@Serial-1", "/dev/ttyUSB2@Serial-2"));

EspDeviceInfo expectedEspDeviceInfo = EspDeviceInfo.builder()
.port("/dev/ttyUSB1")
.build();

EspDeviceInfo expectedEspDeviceInfo2 = EspDeviceInfo.builder()
.port("/dev/ttyUSB2")
.build();

StepVerifier.create(esptoolService.readAllDevices())
.expectNext(expectedEspDeviceInfo)
.expectNext(expectedEspDeviceInfo2)
.verifyComplete();

}

@ParameterizedTest
@ArgumentsSource(EsptoolServiceArgumentsProvider.class)
@SneakyThrows
Expand Down Expand Up @@ -122,6 +149,23 @@ void showEsptoolVersion() {

}

@Test
@DisplayName("esptool.py not found!")
@SneakyThrows
void showEsptoolVersionFailure() {

String[] commands = ESPTOOL_PY_VERSION;

when(esptoolPathService.esptoolPath()).thenReturn("esptool.py");

when(commandService.processInputStreamLineByLine(commands)).thenReturn(Flux.just("not found"));

StepVerifier.create(esptoolService.showEsptoolVersion())
.expectNext(ESPTOOL_PY_NOT_FOUND)
.verifyComplete();

}

@ParameterizedTest
@ArgumentsSource(EsptoolServiceRawFlashIdFromPortArgumentsProvider.class)
@DisplayName("read raw each String from this inputStream")
Expand All @@ -146,10 +190,17 @@ void readRawFlashIdFromPort(Flux<String> actualLines, String expetedFirtsLine,

}

@Disabled
@Test
@SetSystemProperty(key = "os.name", value = "linux")
@DisplayName("Simple test, counter all devices")
void countAllDevices() {
//FIXME

when(comPortService.countAllDevices()).thenReturn(3L);

StepVerifier.create(esptoolService.countAllDevices())
.expectNext(3L)
.verifyComplete();

}


Expand Down

0 comments on commit fc30cbd

Please sign in to comment.