Skip to content

Commit

Permalink
🚧 #67 #70 Updated ComPortServiceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
rucko24 committed Oct 23, 2024
1 parent 4a34442 commit 3ee4ba7
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 62 deletions.
34 changes: 19 additions & 15 deletions src/main/java/com/esp/espflow/data/service/ComPortService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@
@Service
public class ComPortService {

private final SerialPort[] serialPorts = SerialPort.getCommPorts();

/**
* A Set to avoid repeating the processed ports.
*
* @return A {@link Set} with name of the ports,
*/
public Set<String> getPortsListWithFriendlyName() {
final SerialPort[] serialPorts = SerialPort.getCommPorts();
return Optional.of(Arrays.stream(serialPorts))
.map(this::mappingPorts)
.orElseGet(() -> {
log.info("orElseGet getPortsList {}", serialPorts.length);
return Collections.emptySet();
});
return serialPorts == null
? Collections.emptySet()
: this.mappingPorts(Arrays.stream(serialPorts));
}

/**
Expand All @@ -61,11 +59,13 @@ private Set<String> mappingPorts(Stream<SerialPort> portsStream) {
/**
* Here we concatenate the friendlyName or vendor
*
* <blockquote>
* <pre>COM3@Silicon Labs CP210x USB to UART Bridge (COM3)</pre>
* </blockquote>
* @param serialPort the current serial port
* <blockquote>
* <pre>Windows: COM3@Silicon Labs CP210x USB to UART Bridge (COM3)</pre>
* <pre>Linux: /dev/[email protected]</pre>
* <pre>FreeBSD: /dev/cuaU0@Serial</pre>
* </blockquote>
*
* @param serialPort the current serial port
* @return A {@link String}
*/
private String concatSystemPortPathWithFriendlyName(final SerialPort serialPort) {
Expand All @@ -82,12 +82,16 @@ public long countAllDevices() {
/**
* Filter if the port is Future Technology Devices International, Ltd FT232 Serial (UART) IC -> FT232R
*
* @param filterSerialPortFT232R the infamous <strong>FT232R</strong>
* @param serialPort maybe the infamous <strong>FT232R</strong>
* @return Boolean
*/
private Boolean isSerialPortFT232R(SerialPort filterSerialPortFT232R) {
return !(filterSerialPortFT232R.getDescriptivePortName().startsWith("FT232R")
|| filterSerialPortFT232R.getPortDescription().startsWith("FT232R"));
private Boolean isSerialPortFT232R(SerialPort serialPort) {
if (Objects.isNull(serialPort)) {
log.debug("serialPort isSerialPortFT232R() {}", "is null");
return false;
}
return !(serialPort.getDescriptivePortName().startsWith("FT232R")
|| serialPort.getPortDescription().startsWith("FT232R"));
}

/**
Expand Down
47 changes: 0 additions & 47 deletions src/test/java/com/esp/espflow/data/service/ComPortServiceIT.java

This file was deleted.

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

import com.esp.espflow.data.service.provider.ComPortServiceArgumentProvider;
import com.fazecast.jSerialComm.SerialPort;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Set;

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

/**
* @author rubn
*/
@ExtendWith(MockitoExtension.class)
class ComPortServiceTest {

@InjectMocks
private ComPortService comPortService;

@ParameterizedTest
@ArgumentsSource(ComPortServiceArgumentProvider.class)
@DisplayName("We obtain the list of serial ports, of the type comPort@friendlyName, " +
"the portDescription is equal to the comPort")
void getPortsListWithFriendlyName(SerialPort[] actualSerialPorts, String expectedDevUsb1, String expectedDevUsb2) {

ReflectionTestUtils.setField(comPortService, "serialPorts", actualSerialPorts);

Set<String> actualPortsListWithFriendlyName= comPortService.getPortsListWithFriendlyName();

assertThat(actualPortsListWithFriendlyName)
.isNotEmpty()
.containsExactly(expectedDevUsb1, expectedDevUsb2);

}

@ParameterizedTest
@ArgumentsSource(ComPortServiceArgumentProvider.class)
@DisplayName("Serial port count, for this example there are 2 ports")
void countAllDevices(SerialPort[] actualSerialPorts, String expectedDevUsb1, String expectedDevUsb2) {

ReflectionTestUtils.setField(comPortService, "serialPorts", actualSerialPorts);

assertThat(comPortService.countAllDevices())
.isNotZero()
.isEqualTo(2);

}

@ParameterizedTest
@ArgumentsSource(ComPortServiceArgumentProvider.class)
@DisplayName("We verify that you only have the name of the ports before the @, for linux")
void getOnlyPortsList(SerialPort[] actualSerialPorts, String expectedDevUsb1, String expectedDevUsb2) {

ReflectionTestUtils.setField(comPortService, "serialPorts", actualSerialPorts);

assertThat(comPortService.getOnlyPortsList())
.isNotEmpty()
.containsExactly(expectedDevUsb1.split("@")[0], expectedDevUsb2.split("@")[0]);

}

@Test
@DisplayName("When the port list is empty")
void listaDePuertosVacia() {
final SerialPort[] serialPorts = null;

ReflectionTestUtils.setField(comPortService, "serialPorts", serialPorts);

Set<String> portDescription = comPortService.getPortsListWithFriendlyName();

assertThat(portDescription).isEmpty();

}

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

import com.esp.espflow.data.util.GetOsName;
import com.fazecast.jSerialComm.SerialPort;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.stream.Stream;

public class ComPortServiceArgumentProvider implements ArgumentsProvider {

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

if(GetOsName.getOsName() == GetOsName.WINDOWS) {

return Stream.of(windowsSerialPorts());

} else if(GetOsName.getOsName() == GetOsName.LINUX) {

return Stream.of(linuxSerialPorts());

} else if(GetOsName.getOsName() == GetOsName.FREEBSD) {

return Stream.of(freeBsdSerialPorts());

} else if (GetOsName.getOsName() == GetOsName.MAC) {
//
return null;
}

return Stream.of(Arguments.of(windowsSerialPorts()));

}

/**
* //COM3@Silicon Labs CP210x USB to UART Bridge (COM3)
*
* @return A {@link Arguments}
*/
private Arguments windowsSerialPorts() {
System.setProperty("os.name", "Win");

final SerialPort serialPort1 = SerialPort.getCommPort("COM1");
ReflectionTestUtils.setField(serialPort1, "comPort", "COM2");
ReflectionTestUtils.setField(serialPort1, "portDescription", "COM2");
ReflectionTestUtils.setField(serialPort1, "friendlyName", "Silicon Labs CP210x USB to UART Bridge (COM2)");

final SerialPort serialPort2 = SerialPort.getCommPort("COM1");
ReflectionTestUtils.setField(serialPort1, "comPort", "COM3");
ReflectionTestUtils.setField(serialPort1, "portDescription", "COM3");
ReflectionTestUtils.setField(serialPort1, "friendlyName", "Silicon Labs CP210x USB to UART Bridge (COM3)");

final SerialPort[] actualSerialPorts = new SerialPort[]{serialPort1, serialPort2};

final String expectedPort1 = "COM2@Silicon Labs CP210x USB to UART Bridge (COM2)";
final String expectedPort2 = "COM3@Silicon Labs CP210x USB to UART Bridge (COM3)";

return Arguments.of(actualSerialPorts, expectedPort1, expectedPort2);
}

private Arguments linuxSerialPorts() {

final SerialPort serialPort1 = SerialPort.getCommPort("/dev/ttyUSB0");
/**
* Set custom port
*/
ReflectionTestUtils.setField(serialPort1, "comPort", "/dev/ttyUSB1");
ReflectionTestUtils.setField(serialPort1, "portDescription", "/dev/ttyUSB1");
ReflectionTestUtils.setField(serialPort1, "friendlyName", "Serial-1");

final SerialPort serialPort2 = SerialPort.getCommPort("/dev/ttyUSB0");
/**
* Set custom port
*/
ReflectionTestUtils.setField(serialPort2, "comPort", "/dev/ttyUSB2");
ReflectionTestUtils.setField(serialPort2, "portDescription", "/dev/ttyUSB2");
ReflectionTestUtils.setField(serialPort2, "friendlyName", "Serial-2");

final SerialPort[] actualSerialPorts = new SerialPort[]{serialPort1, serialPort2};

final String expectedPort1 = "/dev/ttyUSB1@Serial-1";
final String expectedPort2 = "/dev/ttyUSB2@Serial-2";

return Arguments.of(actualSerialPorts, expectedPort1, expectedPort2);


}

private Arguments freeBsdSerialPorts() {

final SerialPort serialPort1 = SerialPort.getCommPort("/dev/cuaU0");
/**
* Set custom port
*/
ReflectionTestUtils.setField(serialPort1, "comPort", "/dev/cuaU1");
ReflectionTestUtils.setField(serialPort1, "portDescription", "/dev/cuaU1");
ReflectionTestUtils.setField(serialPort1, "friendlyName", "Serial-1");

final SerialPort serialPort2 = SerialPort.getCommPort("/dev/cuaU0");
/**
* Set custom port
*/
ReflectionTestUtils.setField(serialPort2, "comPort", "/dev/cuaU2");
ReflectionTestUtils.setField(serialPort2, "portDescription", "/dev/cuaU2");
ReflectionTestUtils.setField(serialPort2, "friendlyName", "Serial-2");

final SerialPort[] actualSerialPorts = new SerialPort[]{serialPort1, serialPort2};

final String expectedPort1 = "/dev/cuaU1@Serial-1";
final String expectedPort2 = "/dev/cuaU2@Serial-2";

return Arguments.of(actualSerialPorts, expectedPort1, expectedPort2);


}

}

0 comments on commit 3ee4ba7

Please sign in to comment.