Skip to content

Commit

Permalink
🚧 #85 #70 test
Browse files Browse the repository at this point in the history
  • Loading branch information
rucko24 committed Dec 7, 2024
1 parent 64506cd commit acdb170
Show file tree
Hide file tree
Showing 17 changed files with 629 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public String toString() {
return "{" +
"id=" + id +
", name='" + name + '\'' +
", isBundled=" + isBundled +
", absolutePathEsptool='" + absolutePathEsptool + '\'' +
", isBundled=" + isBundled +
", esptoolVersion='" + esptoolVersion + '\'' +
", isSelected=" + isSelected +
", sha256='" + sha256 + '\'' +
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/com/esp/espflow/service/EsptoolService.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,6 @@ public Flux<String> showEsptoolVersion() {
.map(this::processLineEsptoolVersion);
}

/**
* Check if esptool is installed by executing the command esptool.py version for the current system.
*
* @param absolutePath
* @return A {@link Flux<String> }
*/
public Flux<String> showEsptoolVersion(final String absolutePath) {
final String[] commands = {absolutePath, VERSION};
return this.commandService.processInputStreamLineByLine(commands)
.take(FIRST_LINE_TO_CHECK_IF_IT_EXISTS)
.map(this::processLineEsptoolVersion);
}

/**
* Check if esptool is installed by executing the command esptool.py version for the current system.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.esp.espflow.service.respository.impl;

import com.esp.espflow.entity.EsptoolExecutableEntity;
import com.esp.espflow.entity.dto.EsptoolExecutableDto;
import com.esp.espflow.mappers.EsptoolExecutableMapper;
import com.esp.espflow.service.respository.EsptoolExecutableRepository;
Expand Down Expand Up @@ -32,11 +33,12 @@ public EsptoolExecutableDto save(EsptoolExecutableDto esptoolExecutableDto) {
var mappedEntity = EsptoolExecutableMapper.INSTANCE.dtoToEntity(esptoolExecutableDto);
this.esptoolExecutableRepository.findByEsptoolVersionWithBundle(mappedEntity.getEsptoolVersion(), mappedEntity.isBundled())
.ifPresentOrElse(entityIfPresent -> {
var entityToUpdate = EsptoolExecutableMapper.INSTANCE.fromEntityPresent(entityIfPresent.getId(), mappedEntity);
log.info("Entity updated {}", this.esptoolExecutableRepository.save(entityToUpdate));
final EsptoolExecutableEntity entityToUpdate = EsptoolExecutableMapper.INSTANCE.fromEntityPresent(entityIfPresent.getId(), mappedEntity);
final EsptoolExecutableEntity updatedEntity = this.esptoolExecutableRepository.save(entityToUpdate);
log.info("Entity updated {}", updatedEntity);
}, () -> {
this.esptoolExecutableRepository.save(mappedEntity);
log.info("Entity saved {}", mappedEntity);
final EsptoolExecutableEntity entitySaved = this.esptoolExecutableRepository.save(mappedEntity);
log.info("Entity saved {}", entitySaved);
});
return EsptoolExecutableMapper.INSTANCE.entityToDto(mappedEntity);
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/esp/espflow/util/console/OutPutConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ protected void onDetach(DetachEvent detachEvent) {
@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
//if (attachEvent.isInitialAttach()) {
this.getConsole();
this.xterm.getXTerm().fit();
//}
this.getConsole();
this.xterm.getXTerm().fit();
}
}
30 changes: 7 additions & 23 deletions src/main/java/com/esp/espflow/views/flashesp/FlashEspView.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import com.vaadin.flow.component.radiobutton.RadioButtonGroup;
import com.vaadin.flow.component.splitlayout.SplitLayout;
import com.vaadin.flow.component.splitlayout.SplitLayout.Orientation;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;
Expand All @@ -37,8 +35,6 @@
import com.vaadin.flow.theme.lumo.LumoUtility.Display;
import com.vaadin.flow.theme.lumo.LumoUtility.FlexDirection;
import com.vaadin.flow.theme.lumo.LumoUtility.JustifyContent;
import com.vaadin.flow.theme.lumo.LumoUtility.Margin.Left;
import com.vaadin.flow.theme.lumo.LumoUtility.Margin.Right;
import com.vaadin.flow.theme.lumo.LumoUtility.Overflow;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.security.RolesAllowed;
Expand Down Expand Up @@ -80,7 +76,7 @@
@RolesAllowed("ADMIN")
@RequiredArgsConstructor
//@PreserveOnRefresh
public class FlashEspView extends Div implements AfterNavigationObserver, ResponsiveHeaderDiv {
public class FlashEspView extends Div implements ResponsiveHeaderDiv {

private final DivFlashUploader divFlashUploader;
private final DivHeaderPorts divHeaderPorts;
Expand All @@ -99,7 +95,6 @@ public class FlashEspView extends Div implements AfterNavigationObserver, Respon
/**
* OutputConsole
*/
private Div divColumnItems = new Div();
private final OutPutConsole outPutConsole = new OutPutConsole();

private String[] commands;
Expand Down Expand Up @@ -161,12 +156,12 @@ private SplitLayout getSplitLayout() {
divRowToSecondary.addClassNames(Display.FLEX, FlexDirection.ROW);
divRowToSecondary.getStyle().set(OVERFLOW_Y, HIDDEN);

// outPutConsole.getStyle().set("overflow-x", "hidden");
// outPutConsole.getDivTextArea().removeClassNames(Left.LARGE, Right.LARGE);
// outPutConsole.getButtonClear().addClassName(BOX_SHADOW_VAADIN_BUTTON);
// outPutConsole.getButtonDownScroll().addClassName(BOX_SHADOW_VAADIN_BUTTON);
// Div divColumnItems = new Div(outPutConsole.getButtonDownScroll(),
// outPutConsole.getButtonClear());
outPutConsole.getStyle().set("overflow-x", "hidden");
outPutConsole.getDivTextArea().removeClassNames(LumoUtility.Margin.Left.LARGE, LumoUtility.Margin.Minus.Right.LARGE);
outPutConsole.getButtonClear().addClassName(BOX_SHADOW_VAADIN_BUTTON);
outPutConsole.getButtonDownScroll().addClassName(BOX_SHADOW_VAADIN_BUTTON);
Div divColumnItems = new Div(outPutConsole.getButtonDownScroll(),
outPutConsole.getButtonClear());
divColumnItems.setId("divColumnItems");

divColumnItems.addClassNames(
Expand Down Expand Up @@ -426,15 +421,4 @@ protected void onAttach(AttachEvent attachEvent) {

}

@Override
public void afterNavigation(AfterNavigationEvent event) {
//Ignored listener invocation for terminal-initialized event from the client side for an inert fc-xterm element
if(outPutConsole.isAttached()) {
outPutConsole.getStyle().set("overflow-x", "hidden");
outPutConsole.getDivTextArea().removeClassNames(Left.LARGE, Right.LARGE);
outPutConsole.getButtonClear().addClassName(BOX_SHADOW_VAADIN_BUTTON);
outPutConsole.getButtonDownScroll().addClassName(BOX_SHADOW_VAADIN_BUTTON);
this.divColumnItems.add(outPutConsole.getButtonDownScroll(), outPutConsole.getButtonClear());
}
}
}
31 changes: 8 additions & 23 deletions src/main/java/com/esp/espflow/views/readflash/ReadFlashView.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
import com.vaadin.flow.component.splitlayout.SplitLayout.Orientation;
import com.vaadin.flow.component.textfield.IntegerField;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
Expand Down Expand Up @@ -99,7 +97,7 @@
@Route(value = "read-flash", layout = MainLayout.class)
@RolesAllowed("ADMIN")
@RequiredArgsConstructor
public class ReadFlashView extends Div implements ResponsiveHeaderDiv, AfterNavigationObserver, BeforeEnterObserver {
public class ReadFlashView extends Div implements ResponsiveHeaderDiv, BeforeEnterObserver {

private final FlashDownloadButtonService flashDownloadButtonService;
private final EsptoolService esptoolService;
Expand All @@ -119,7 +117,6 @@ public class ReadFlashView extends Div implements ResponsiveHeaderDiv, AfterNavi
* Console output
*/
private final OutPutConsole outPutConsole = new OutPutConsole();
private final Div divColumnItems = new Div();
private final Span spanTotalDevices = new Span("Total devices:");
private final Span spanPortFailure = new Span(PORT_FAILURE);
private final Span spanTotalDevicesValue = new Span();
Expand Down Expand Up @@ -285,13 +282,13 @@ private Div divForConsoleOutput() {
divRowToSecondary.getStyle().set(OVERFLOW_Y, HIDDEN);
divRowToSecondary.getStyle().set("background", "linear-gradient(var(--lumo-shade-5pct), var(--lumo-shade-5pct))");

// outPutConsole.getStyle().set("overflow-x", "hidden");
// outPutConsole.getDivTextArea().removeClassNames(Left.LARGE, Right.LARGE);
// outPutConsole.getButtonClear().addClassName(BOX_SHADOW_VAADIN_BUTTON);
// outPutConsole.getButtonDownScroll().addClassName(BOX_SHADOW_VAADIN_BUTTON);
//
// Div divColumnItems = new Div(outPutConsole.getButtonDownScroll(),
// outPutConsole.getButtonClear());
outPutConsole.getStyle().set("overflow-x", "hidden");
outPutConsole.getDivTextArea().removeClassNames(Left.LARGE, Right.LARGE);
outPutConsole.getButtonClear().addClassName(BOX_SHADOW_VAADIN_BUTTON);
outPutConsole.getButtonDownScroll().addClassName(BOX_SHADOW_VAADIN_BUTTON);

Div divColumnItems = new Div(outPutConsole.getButtonDownScroll(),
outPutConsole.getButtonClear());
divColumnItems.setId("divColumnItems");

divColumnItems.addClassNames(
Expand Down Expand Up @@ -685,18 +682,6 @@ public void beforeEnter(BeforeEnterEvent event) {
});
}

@Override
public void afterNavigation(AfterNavigationEvent event) {
//Ignored listener invocation for terminal-initialized event from the client side for an inert fc-xterm element
if(outPutConsole.isAttached()) {
outPutConsole.getStyle().set("overflow-x", "hidden");
outPutConsole.getDivTextArea().removeClassNames(Left.LARGE, Right.LARGE);
outPutConsole.getButtonClear().addClassName(BOX_SHADOW_VAADIN_BUTTON);
outPutConsole.getButtonDownScroll().addClassName(BOX_SHADOW_VAADIN_BUTTON);
this.divColumnItems.add(outPutConsole.getButtonDownScroll(), outPutConsole.getButtonClear());
}
}

@Override
protected void onDetach(DetachEvent detachEvent) {
super.onDetach(detachEvent);
Expand Down
91 changes: 56 additions & 35 deletions src/test/java/com/esp/espflow/service/EsptoolServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.esp.espflow.service;

import com.esp.espflow.entity.EspDeviceInfoRecord;
import com.esp.espflow.entity.dto.EsptoolExecutableDto;
import com.esp.espflow.enums.BaudRatesEnum;
import com.esp.espflow.enums.GetOsName;
import com.esp.espflow.exceptions.CanNotBeReadDeviceException;
import com.esp.espflow.exceptions.CreateEspBackUpFlashDirException;
import com.esp.espflow.service.provider.EsptoolServiceArgumentsProvider;
import com.esp.espflow.service.provider.EsptoolServiceNoFlashSizeArgumentsProvider;
import com.esp.espflow.service.provider.EsptoolServiceRawFlashIdFromPortArgumentsProvider;
Expand All @@ -23,6 +25,8 @@
import org.junitpioneer.jupiter.SetSystemProperty;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -44,6 +48,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
Expand Down Expand Up @@ -73,6 +78,9 @@ class EsptoolServiceTest {
@Mock
private EsptoolPathService esptoolPathService;

private static final String JAVA_IO_TEMPORAL_DIR_OS = System.getProperty("java.io.tmpdir");
private static final String EXPECTED_DIR = JAVA_IO_TEMPORAL_DIR_OS.concat("/esp-backup-flash-dir");

@ParameterizedTest
@ArgumentsSource(EsptoolServiceReadAllDevicesArgumentsProvider.class)
@SetSystemProperty(key = "os.name", value = "linux")
Expand Down Expand Up @@ -200,6 +208,27 @@ void showEsptoolVersion() {

}

@Test
@DisplayName("show esptoolVersion using the absolute path")
void testShowEsptoolVersion_absolutePath() {
String[] commands = ESPTOOL_PY_VERSION;

when(commandService.processInputStreamLineByLine(commands)).thenReturn(Flux.just("esptool.py v4.7.0"));

EsptoolExecutableDto esptoolExecutableDto = EsptoolExecutableDto
.builder()
.absolutePathEsptool("/tmp/esptool-dir/esptool.py")
.isBundled(false)
.isSelected(false)
.build();

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

StepVerifier.create(esptoolService.showEsptoolVersion(esptoolExecutableDto))
.expectNext("esptool.py v4.7.0")
.verifyComplete();
}

@Test
@DisplayName("esptool.py not found!")
@SneakyThrows
Expand Down Expand Up @@ -326,55 +355,47 @@ void writeFlash(Flux<String> actualLines, String[] expectedLines) {
}

@Test
@SetSystemProperty(key = "java.io.tmpdir", value = "/tmp")
@SetSystemProperty(key = "os.name", value = "linux")
@DisplayName("Creation of backup file in temporary directory, on linux")
void createEspBackUpFlashDirIfNotExists() throws IOException {
void testCreateDirectoryWhenNotExists() throws Exception {
try (MockedStatic<Files> filesMock = Mockito.mockStatic(Files.class)) {
Path mockedPath = Path.of(EXPECTED_DIR);

Path tmpPath = Path.of("/tmp/esp-backup-flash-dir");
filesMock.when(() -> Files.exists(mockedPath)).thenReturn(false);
filesMock.when(() -> Files.createDirectory(mockedPath)).thenReturn(mockedPath);

if (Files.exists(tmpPath)) {
try (Stream<Path> dirStream = Files.walk(tmpPath)) {
esptoolService.createEspBackUpFlashDirIfNotExists();

dirStream
.filter(Files::isRegularFile)
.map(Path::toFile)
.forEach(File::delete);
}
filesMock.verify(() -> Files.exists(mockedPath), times(1));
filesMock.verify(() -> Files.createDirectory(mockedPath), times(1));
}
}

Files.deleteIfExists(tmpPath);
assertThatCode(() ->
this.esptoolService.createEspBackUpFlashDirIfNotExists()).doesNotThrowAnyException();
@Test
void testDirectoryAlreadyExists() throws Exception {
try (MockedStatic<Files> filesMock = Mockito.mockStatic(Files.class)) {
Path mockedPath = Path.of(EXPECTED_DIR);
filesMock.when(() -> Files.exists(mockedPath)).thenReturn(true);

} else {
esptoolService.createEspBackUpFlashDirIfNotExists();

assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> this.esptoolService.createEspBackUpFlashDirIfNotExists())
.withMessage("Error creating directory /esp-backup-flash-dir on not-dir/esp-backup-flash-dir");
filesMock.verify(() -> Files.exists(mockedPath), times(1));
filesMock.verify(() -> Files.createDirectory(mockedPath), never());
}


}

@Test
@SetSystemProperty(key = "os.name", value = "linux")
@SetSystemProperty(key = "java.io.tmpdir", value = "not-dir")
@DisplayName("Error creating directory /esp-backup-flash-dir in a directory that does not exist")
void createEspBackUpFlashDirIfNotExistsFailure() throws IOException {

Files.deleteIfExists(Path.of("not-dir/esp-backup-flash-dir"));
void testIOExceptionThrown() {
try (MockedStatic<Files> filesMock = Mockito.mockStatic(Files.class)) {
Path mockedPath = Path.of(EXPECTED_DIR);

assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> this.esptoolService.createEspBackUpFlashDirIfNotExists())
.withMessage("Error creating directory /esp-backup-flash-dir on not-dir/esp-backup-flash-dir");
filesMock.when(() -> Files.exists(mockedPath)).thenReturn(false);
filesMock.when(() -> Files.createDirectory(mockedPath)).thenThrow(new IOException("/tmp"));

}
assertThatExceptionOfType(CreateEspBackUpFlashDirException.class)
.isThrownBy(() -> this.esptoolService.createEspBackUpFlashDirIfNotExists())
.withMessage("Error creating directory /esp-backup-flash-dir on /tmp");

@Test
void testShowEsptoolVersion() {
}
}

@Test
void testShowEsptoolVersion1() {
}
}
Loading

0 comments on commit acdb170

Please sign in to comment.