diff --git a/src/main/java/com/esp/espflow/entity/dto/EsptoolExecutableDto.java b/src/main/java/com/esp/espflow/entity/dto/EsptoolExecutableDto.java index 433f7f2..31cd0d9 100644 --- a/src/main/java/com/esp/espflow/entity/dto/EsptoolExecutableDto.java +++ b/src/main/java/com/esp/espflow/entity/dto/EsptoolExecutableDto.java @@ -70,8 +70,8 @@ public String toString() { return "{" + "id=" + id + ", name='" + name + '\'' + - ", isBundled=" + isBundled + ", absolutePathEsptool='" + absolutePathEsptool + '\'' + + ", isBundled=" + isBundled + ", esptoolVersion='" + esptoolVersion + '\'' + ", isSelected=" + isSelected + ", sha256='" + sha256 + '\'' + diff --git a/src/main/java/com/esp/espflow/service/EsptoolService.java b/src/main/java/com/esp/espflow/service/EsptoolService.java index 562e39a..5751356 100644 --- a/src/main/java/com/esp/espflow/service/EsptoolService.java +++ b/src/main/java/com/esp/espflow/service/EsptoolService.java @@ -154,19 +154,6 @@ public Flux 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 } - */ - public Flux 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. * diff --git a/src/main/java/com/esp/espflow/service/respository/impl/EsptoolExecutableServiceImpl.java b/src/main/java/com/esp/espflow/service/respository/impl/EsptoolExecutableServiceImpl.java index 10633a2..a3b7c05 100644 --- a/src/main/java/com/esp/espflow/service/respository/impl/EsptoolExecutableServiceImpl.java +++ b/src/main/java/com/esp/espflow/service/respository/impl/EsptoolExecutableServiceImpl.java @@ -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; @@ -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); } diff --git a/src/main/java/com/esp/espflow/util/console/OutPutConsole.java b/src/main/java/com/esp/espflow/util/console/OutPutConsole.java index 851f989..8f97ff9 100644 --- a/src/main/java/com/esp/espflow/util/console/OutPutConsole.java +++ b/src/main/java/com/esp/espflow/util/console/OutPutConsole.java @@ -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(); } } diff --git a/src/main/java/com/esp/espflow/views/flashesp/FlashEspView.java b/src/main/java/com/esp/espflow/views/flashesp/FlashEspView.java index c29c064..49ef77e 100644 --- a/src/main/java/com/esp/espflow/views/flashesp/FlashEspView.java +++ b/src/main/java/com/esp/espflow/views/flashesp/FlashEspView.java @@ -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; @@ -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; @@ -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; @@ -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; @@ -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( @@ -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()); - } - } } diff --git a/src/main/java/com/esp/espflow/views/readflash/ReadFlashView.java b/src/main/java/com/esp/espflow/views/readflash/ReadFlashView.java index 9e3cc3a..a74f14d 100644 --- a/src/main/java/com/esp/espflow/views/readflash/ReadFlashView.java +++ b/src/main/java/com/esp/espflow/views/readflash/ReadFlashView.java @@ -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; @@ -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; @@ -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(); @@ -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( @@ -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); diff --git a/src/test/java/com/esp/espflow/service/EsptoolServiceTest.java b/src/test/java/com/esp/espflow/service/EsptoolServiceTest.java index daea6c8..5a8d489 100644 --- a/src/test/java/com/esp/espflow/service/EsptoolServiceTest.java +++ b/src/test/java/com/esp/espflow/service/EsptoolServiceTest.java @@ -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; @@ -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; @@ -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; @@ -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") @@ -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 @@ -326,55 +355,47 @@ void writeFlash(Flux 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 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 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 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 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() { - } } diff --git a/src/test/java/com/esp/espflow/service/respository/impl/EsptoolExecutableServiceImplTest.java b/src/test/java/com/esp/espflow/service/respository/impl/EsptoolExecutableServiceImplTest.java new file mode 100644 index 0000000..990a06f --- /dev/null +++ b/src/test/java/com/esp/espflow/service/respository/impl/EsptoolExecutableServiceImplTest.java @@ -0,0 +1,221 @@ +package com.esp.espflow.service.respository.impl; + +import com.esp.espflow.entity.EsptoolExecutableEntity; +import com.esp.espflow.entity.dto.EsptoolExecutableDto; +import com.esp.espflow.service.respository.EsptoolExecutableRepository; +import com.esp.espflow.service.respository.impl.provider.esptoolexecutableprovider.EsptoolExecutableServiceSaveProvider; +import com.esp.espflow.service.respository.impl.provider.esptoolexecutableprovider.EsptoolExecutableServiceUpdateProvider; +import com.esp.espflow.service.respository.impl.provider.esptoolexecutableprovider.EsptoollExecutableServiceFindAllProvider; +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.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +/** + * @author rubn + */ +@ExtendWith(MockitoExtension.class) +class EsptoolExecutableServiceImplTest { + + @InjectMocks + private EsptoolExecutableServiceImpl esptoolExecutableService; + + @Mock + private EsptoolExecutableRepository esptoolExecutableRepository; + + @ParameterizedTest + @ArgumentsSource(EsptoolExecutableServiceSaveProvider.class) + @DisplayName("Save the entity, if esptoolVersion not found and bundled to false") + void save(EsptoolExecutableDto dtoToSave, EsptoolExecutableEntity entityToSave) { + + when(esptoolExecutableRepository.findByEsptoolVersionWithBundle("v4.7.0", false)).thenReturn(Optional.empty()); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(EsptoolExecutableEntity.class); + esptoolExecutableRepository.save(entityToSave); + verify(esptoolExecutableRepository).save(captor.capture()); + + assertThat(this.esptoolExecutableService.save(dtoToSave)) + .usingRecursiveComparison() + .isEqualTo(captor.getValue()); + + verify(esptoolExecutableRepository, times(1)).save(captor.getValue()); + + } + + @ParameterizedTest + @ArgumentsSource(EsptoolExecutableServiceUpdateProvider.class) + @DisplayName("Update the entity to isSelected to true, when version and bundled is exist") + void update(EsptoolExecutableEntity toSave, EsptoolExecutableDto dtoToSave, EsptoolExecutableDto savedDto, + EsptoolExecutableEntity savedFindByEntity, EsptoolExecutableEntity savedEntity) { + + when(esptoolExecutableRepository.findByEsptoolVersionWithBundle("v4.7.0", false)).thenReturn(Optional.of(savedFindByEntity)); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(EsptoolExecutableEntity.class); + esptoolExecutableRepository.save(toSave); + verify(esptoolExecutableRepository).save(captor.capture()); + + assertThat(this.esptoolExecutableService.save(dtoToSave)) + .usingRecursiveComparison() + .isEqualTo(captor.getValue()); + + assertAll(() -> { + assertThat(esptoolExecutableService.findByEsptoolVersionWithBundle("v4.7.0", false)) + .map(EsptoolExecutableDto::isSelected) + .hasValue(true); + }); + + verify(esptoolExecutableRepository, times(2)).findByEsptoolVersionWithBundle("v4.7.0", false); + verify(esptoolExecutableRepository, times(1)).save(captor.getValue()); + + } + + @Test + @DisplayName("find by version and bundle to true or false") + void findByEsptoolVersionWithBundle() { + + final EsptoolExecutableEntity esptoolExecutableEntityToSave = EsptoolExecutableEntity + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .esptoolVersion("v4.7.0") + .isBundled(false) + .isSelected(false) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + when(esptoolExecutableRepository.findByEsptoolVersionWithBundle("v4.7.0", false)) + .thenReturn(Optional.of(esptoolExecutableEntityToSave)); + + final Optional expectedEsptoolExecutableDto = Optional.of(EsptoolExecutableDto + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .esptoolVersion("v4.7.0") + .isBundled(false) + .isSelected(false) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build()); + + assertThat(this.esptoolExecutableService.findByEsptoolVersionWithBundle("v4.7.0", false)) + .usingRecursiveComparison() + .isEqualTo(expectedEsptoolExecutableDto); + + verify(esptoolExecutableRepository, times(1)).findByEsptoolVersionWithBundle("v4.7.0", false); + verifyNoMoreInteractions(esptoolExecutableRepository); + + } + + @Test + @DisplayName("update all items except this id") + void updateAllSelectedToFalseExcept() { + + assertThatCode(() -> esptoolExecutableService.updateAllSelectedToFalseExcept(1L)) + .doesNotThrowAnyException(); + + verify(esptoolExecutableRepository, times(1)).updateAllSelectedToFalseExcept(1L); + verifyNoMoreInteractions(esptoolExecutableRepository); + + } + + @Test + @DisplayName("Only is set to true") + void findByIsSelectedToTrue() { + final EsptoolExecutableEntity esptoolExecutableEntityToSave = EsptoolExecutableEntity + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .esptoolVersion("v4.7.0") + .isBundled(false) + .isSelected(true) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + when(esptoolExecutableRepository.findByIsSelectedToTrue()) + .thenReturn(Optional.of(esptoolExecutableEntityToSave)); + + final Optional expectedEsptoolExecutableDto = Optional.of(EsptoolExecutableDto + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .esptoolVersion("v4.7.0") + .isBundled(false) + .isSelected(true) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build()); + + assertThat(this.esptoolExecutableService.findByIsSelectedToTrue()) + .usingRecursiveComparison() + .isEqualTo(expectedEsptoolExecutableDto); + } + + @Test + void findByAbsolutePathEsptoolAndIsBundleAndVersion() { + final EsptoolExecutableEntity esptoolExecutableEntityToFind = EsptoolExecutableEntity + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .esptoolVersion("v4.7.0") + .isBundled(false) + .isSelected(true) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + when(esptoolExecutableRepository.findByAbsolutePathEsptoolAndIsBundleAndVersion( + esptoolExecutableEntityToFind.getAbsolutePathEsptool(), false, + esptoolExecutableEntityToFind.getEsptoolVersion())).thenReturn(Optional.of(esptoolExecutableEntityToFind)); + + final EsptoolExecutableDto expectedEsptoolExecutableDto = EsptoolExecutableDto + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .esptoolVersion("v4.7.0") + .isBundled(false) + .isSelected(true) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + assertThat(this.esptoolExecutableService.findByAbsolutePathEsptoolAndIsBundleAndVersion( + expectedEsptoolExecutableDto.absolutePathEsptool(), false, expectedEsptoolExecutableDto.esptoolVersion())) + .usingRecursiveComparison() + .isEqualTo(Optional.of(expectedEsptoolExecutableDto)); + } + + @ParameterizedTest + @ArgumentsSource(EsptoollExecutableServiceFindAllProvider.class) + @DisplayName("findAll ") + void findAll(EsptoolExecutableEntity entity, EsptoolExecutableEntity entity2, + EsptoolExecutableDto expectedEsptoolExecutableDto, EsptoolExecutableDto expectedEsptoolExecutableDto2) { + + when(esptoolExecutableRepository.findAll()).thenReturn(List.of(entity, entity2)); + + assertThat(this.esptoolExecutableService.findAll()) + .isEqualTo(List.of(expectedEsptoolExecutableDto, expectedEsptoolExecutableDto2)) + .isNotEmpty() + .isNotNull() + .hasSize(2); + + } + +} \ No newline at end of file diff --git a/src/test/java/com/esp/espflow/service/respository/impl/EsptoolSha256ServiceImplTest.java b/src/test/java/com/esp/espflow/service/respository/impl/EsptoolSha256ServiceImplTest.java new file mode 100644 index 0000000..346e0ae --- /dev/null +++ b/src/test/java/com/esp/espflow/service/respository/impl/EsptoolSha256ServiceImplTest.java @@ -0,0 +1,62 @@ +package com.esp.espflow.service.respository.impl; + +import com.esp.espflow.entity.EsptoolSha256Entity; +import com.esp.espflow.entity.dto.EsptoolSha256Dto; +import com.esp.espflow.service.respository.EsptoolSha256Repository; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +/** + * @author rubn + */ +@ExtendWith(MockitoExtension.class) +class EsptoolSha256ServiceImplTest { + + @InjectMocks + private EsptoolSha256ServiceImpl esptoolSha256Service; + + @Mock + private EsptoolSha256Repository esptoolSha256Repository; + + @Test + @DisplayName("findBy SHA-256") + void findBySha256() { + + String sha256 = "ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194"; + + EsptoolSha256Entity esptoolSha256Entity = EsptoolSha256Entity + .builder() + .id(1L) + .sha256(sha256) + .build(); + + when(esptoolSha256Repository.findBySha256(sha256)).thenReturn(Optional.of(esptoolSha256Entity)); + + EsptoolSha256Dto esptoolSha256Dto = EsptoolSha256Dto + .builder() + .id(1L) + .sha256(sha256) + .build(); + + assertThat(esptoolSha256Service.findBySha256(sha256)) + .usingRecursiveComparison() + .isEqualTo(Optional.of(esptoolSha256Dto)); + + verify(esptoolSha256Repository, times(1)).findBySha256(sha256); + verifyNoMoreInteractions(esptoolSha256Repository); + + } + +} \ No newline at end of file diff --git a/src/test/java/com/esp/espflow/service/respository/impl/WizardEspServiceTest.java b/src/test/java/com/esp/espflow/service/respository/impl/WizardEspServiceTest.java index bdba419..abc943f 100644 --- a/src/test/java/com/esp/espflow/service/respository/impl/WizardEspServiceTest.java +++ b/src/test/java/com/esp/espflow/service/respository/impl/WizardEspServiceTest.java @@ -2,10 +2,11 @@ import com.esp.espflow.entity.WizardEspEntity; import com.esp.espflow.entity.dto.WizardEspDto; -import com.esp.espflow.mappers.WizardEspMapper; import com.esp.espflow.service.respository.WizardEspRepository; -import com.esp.espflow.service.respository.impl.provider.WizardEspServiceProvider; -import com.esp.espflow.service.respository.impl.provider.WizardUpdateEspServiceProvider; +import com.esp.espflow.service.respository.impl.provider.wizardsproviders.WizardEspServiceFindByNameProvider; +import com.esp.espflow.service.respository.impl.provider.wizardsproviders.WizardEspServiceSaveAllProvider; +import com.esp.espflow.service.respository.impl.provider.wizardsproviders.WizardEspServiceSaveProvider; +import com.esp.espflow.service.respository.impl.provider.wizardsproviders.WizardEspServiceUpdateProvider; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -40,16 +41,14 @@ class WizardEspServiceTest { private WizardEspRepository repository; @ParameterizedTest - @ArgumentsSource(WizardEspServiceProvider.class) + @ArgumentsSource(WizardEspServiceSaveAllProvider.class) @DisplayName("save items") - void saveAll(WizardEspDto wizardReadFlashViewDto, WizardEspDto wizardFlashViewDto, + void saveAll(WizardEspEntity toSaveReadDto, WizardEspEntity toSaveFlashDto, WizardEspDto wizardReadFlashViewDto, + WizardEspDto wizardFlashViewDto, WizardEspEntity entityReadViewToReturn, WizardEspEntity entityFlashViewToReturn) { - WizardEspEntity entityFlash = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); - WizardEspEntity entityRead = WizardEspMapper.INSTANCE.dtoToEntity(wizardFlashViewDto); - - when(repository.save(entityRead)).thenReturn(entityReadViewToReturn); - when(repository.save(entityFlash)).thenReturn(entityFlashViewToReturn); + when(repository.save(toSaveFlashDto)).thenReturn(entityReadViewToReturn); + when(repository.save(toSaveReadDto)).thenReturn(entityFlashViewToReturn); assertThatCode(() -> wizardEspService.saveAll(List.of(wizardFlashViewDto, wizardReadFlashViewDto))) .doesNotThrowAnyException(); @@ -60,61 +59,60 @@ void saveAll(WizardEspDto wizardReadFlashViewDto, WizardEspDto wizardFlashViewDt } @ParameterizedTest - @ArgumentsSource(WizardUpdateEspServiceProvider.class) + @ArgumentsSource(WizardEspServiceUpdateProvider.class) @DisplayName("We update if the entity exists, the id 1L exists, we update the field isWizardEnabled to false") - void updateEntityWizardReadFlash(WizardEspDto wizardReadFlashViewDto, WizardEspDto updatedDto, + void updateEntityWizardReadFlash(WizardEspEntity toSave, WizardEspEntity findById, WizardEspDto updatedDto, WizardEspEntity updatedEntity) { - WizardEspEntity toSave = WizardEspMapper.INSTANCE.dtoToEntity(updatedDto); - WizardEspEntity findById = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); - when(repository.findById(findById.getId())).thenReturn(Optional.of(updatedEntity)); when(repository.save(toSave)).thenReturn(updatedEntity); + when(repository.findByName(WIZARD_READ_FLASH_ESP_VIEW)).thenReturn(Optional.of(updatedEntity)); assertThatCode(() -> wizardEspService.save(updatedDto)).doesNotThrowAnyException(); - verify(repository, times(1)).findById(findById.getId()); - verify(repository, times(1)).save(toSave); + assertAll(() -> { + assertThat(wizardEspService.findByName(WIZARD_READ_FLASH_ESP_VIEW)) + .map(WizardEspDto::isWizardEnabled) + .hasValue(false); + }); + + verify(repository, times(1)).findById(updatedEntity.getId()); + verify(repository, times(1)).findByName(WIZARD_READ_FLASH_ESP_VIEW); + verify(repository, times(1)).save(updatedEntity); verifyNoMoreInteractions(repository); } @ParameterizedTest - @ArgumentsSource(WizardEspServiceProvider.class) + @ArgumentsSource(WizardEspServiceSaveProvider.class) @DisplayName("id is empty, the new entity is saved") - void save(WizardEspDto wizardReadFlashViewDto, WizardEspDto wizardFlashViewDto, - WizardEspEntity entityReadView, WizardEspEntity entityFlashView) { - - WizardEspEntity toSave = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); - WizardEspEntity toFindBy = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); + void save(WizardEspEntity toFindBy, WizardEspEntity toSave, WizardEspDto wizardReadFlashViewDto, + WizardEspEntity entitySaved) { when(repository.findById(toFindBy.getId())).thenReturn(Optional.empty()); - when(repository.save(toSave)).thenReturn(entityReadView); + when(repository.save(toSave)).thenReturn(entitySaved); assertThatCode(() -> wizardEspService.save(wizardReadFlashViewDto)).doesNotThrowAnyException(); - verify(repository, times(1)).findById(toFindBy.getId()); - verify(repository, times(1)).save(entityReadView); + verify(repository, times(1)).findById(entitySaved.getId()); + verify(repository, times(1)).save(entitySaved); verifyNoMoreInteractions(repository); } @ParameterizedTest - @ArgumentsSource(WizardUpdateEspServiceProvider.class) + @ArgumentsSource(WizardEspServiceFindByNameProvider.class) @DisplayName("We save the entity and search by name") - void findByName(WizardEspDto wizardReadFlashViewDto, WizardEspDto wizardReadFlashViewDtoToSave, - WizardEspEntity expectedUpdatedEntity) { - - WizardEspEntity toSave = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDtoToSave); - WizardEspEntity toFindBy = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); + void findByName(WizardEspEntity toSave, WizardEspEntity toFindBy, WizardEspDto updatedDto, + WizardEspEntity updatedEntity) { - when(repository.findById(toFindBy.getId())).thenReturn(Optional.of(expectedUpdatedEntity)); - when(repository.save(toSave)).thenReturn(expectedUpdatedEntity); - when(repository.findByName(WIZARD_READ_FLASH_ESP_VIEW)).thenReturn(Optional.of(expectedUpdatedEntity)); + when(repository.findById(toFindBy.getId())).thenReturn(Optional.of(updatedEntity)); + when(repository.save(toSave)).thenReturn(updatedEntity); + when(repository.findByName(WIZARD_READ_FLASH_ESP_VIEW)).thenReturn(Optional.of(updatedEntity)); - assertThatCode(() -> wizardEspService.save(wizardReadFlashViewDtoToSave)).doesNotThrowAnyException(); + assertThatCode(() -> wizardEspService.save(updatedDto)).doesNotThrowAnyException(); assertThat(wizardEspService.findByName(WIZARD_READ_FLASH_ESP_VIEW)) .usingRecursiveComparison() - .isEqualTo(Optional.of(expectedUpdatedEntity)); + .isEqualTo(Optional.of(updatedEntity)); assertAll(() -> { wizardEspService.findByName(WIZARD_READ_FLASH_ESP_VIEW) @@ -126,13 +124,13 @@ void findByName(WizardEspDto wizardReadFlashViewDto, WizardEspDto wizardReadFlas }); verify(repository, times(1)).findById(toFindBy.getId()); - verify(repository, times(1)).save(expectedUpdatedEntity); + verify(repository, times(1)).save(updatedEntity); verify(repository, times(2)).findByName(WIZARD_READ_FLASH_ESP_VIEW); verifyNoMoreInteractions(repository); } @Test - @DisplayName("Count how many wizards are true or enabled") + @DisplayName("We return a boolean to true if each wizard is enabled to true by counting") void areAllWizardsEnabled() { when(this.repository.areAllWizardsEnabled()).thenReturn(2); diff --git a/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoolExecutableServiceSaveProvider.java b/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoolExecutableServiceSaveProvider.java new file mode 100644 index 0000000..8c8b399 --- /dev/null +++ b/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoolExecutableServiceSaveProvider.java @@ -0,0 +1,41 @@ +package com.esp.espflow.service.respository.impl.provider.esptoolexecutableprovider; + +import com.esp.espflow.entity.EsptoolExecutableEntity; +import com.esp.espflow.entity.dto.EsptoolExecutableDto; +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 EsptoolExecutableServiceSaveProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + + final EsptoolExecutableEntity entityToSave = EsptoolExecutableEntity + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .isBundled(false) + .esptoolVersion("v4.7.0") + .isSelected(false) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + final EsptoolExecutableDto dtoToSave = EsptoolExecutableDto + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .isBundled(false) + .esptoolVersion("v4.7.0") + .isSelected(false) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + return Stream.of(Arguments.of(dtoToSave, entityToSave)); + } + +} diff --git a/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoolExecutableServiceUpdateProvider.java b/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoolExecutableServiceUpdateProvider.java new file mode 100644 index 0000000..d65fc21 --- /dev/null +++ b/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoolExecutableServiceUpdateProvider.java @@ -0,0 +1,47 @@ +package com.esp.espflow.service.respository.impl.provider.esptoolexecutableprovider; + +import com.esp.espflow.entity.EsptoolExecutableEntity; +import com.esp.espflow.entity.dto.EsptoolExecutableDto; +import com.esp.espflow.mappers.EsptoolExecutableMapper; +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 EsptoolExecutableServiceUpdateProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + + final EsptoolExecutableEntity toSave = EsptoolExecutableEntity + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .isBundled(false) + .esptoolVersion("v4.7.0") + .isSelected(false) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + final EsptoolExecutableEntity savedFindByEntity = EsptoolExecutableEntity + .builder() + .id(1L) + .name("esptool") + .absolutePathEsptool("/tmp/esptool-dir/esptool.py") + .isBundled(false) + .esptoolVersion("v4.7.0") + .isSelected(true) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + final EsptoolExecutableDto toSaveDto = EsptoolExecutableMapper.INSTANCE.entityToDto(toSave); + final EsptoolExecutableDto dtoToSave = EsptoolExecutableMapper.INSTANCE.entityToDto(toSave); + + final EsptoolExecutableEntity savedEntity = EsptoolExecutableMapper.INSTANCE.fromEntityPresent(1L, toSave); + + return Stream.of(Arguments.of(toSave, dtoToSave, toSaveDto, savedFindByEntity, savedEntity)); + } + +} diff --git a/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoollExecutableServiceFindAllProvider.java b/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoollExecutableServiceFindAllProvider.java new file mode 100644 index 0000000..1a3af55 --- /dev/null +++ b/src/test/java/com/esp/espflow/service/respository/impl/provider/esptoolexecutableprovider/EsptoollExecutableServiceFindAllProvider.java @@ -0,0 +1,40 @@ +package com.esp.espflow.service.respository.impl.provider.esptoolexecutableprovider; + +import com.esp.espflow.entity.EsptoolExecutableEntity; +import com.esp.espflow.entity.dto.EsptoolExecutableDto; +import com.esp.espflow.mappers.EsptoolExecutableMapper; +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 EsptoollExecutableServiceFindAllProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + + final EsptoolExecutableEntity esptoolExcutableEntity = EsptoolExecutableEntity.builder() + .id(1L) + .isBundled(true) + .esptoolVersion("v4.7.0") + .isSelected(false) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + final EsptoolExecutableEntity esptoolExcutableEntity2 = EsptoolExecutableEntity.builder() + .id(2L) + .isBundled(false) + .esptoolVersion("v4.7.0") + .isSelected(true) + .sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194") + .build(); + + final EsptoolExecutableDto expectedDto = EsptoolExecutableMapper.INSTANCE.entityToDto(esptoolExcutableEntity); + + final EsptoolExecutableDto expectedDto2 = EsptoolExecutableMapper.INSTANCE.entityToDto(esptoolExcutableEntity2); + + return Stream.of(Arguments.of(esptoolExcutableEntity, esptoolExcutableEntity2, expectedDto, expectedDto2)); + } + +} diff --git a/src/test/java/com/esp/espflow/service/respository/impl/provider/WizardUpdateEspServiceProvider.java b/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceFindByNameProvider.java similarity index 68% rename from src/test/java/com/esp/espflow/service/respository/impl/provider/WizardUpdateEspServiceProvider.java rename to src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceFindByNameProvider.java index 1b1a2e9..16a1629 100644 --- a/src/test/java/com/esp/espflow/service/respository/impl/provider/WizardUpdateEspServiceProvider.java +++ b/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceFindByNameProvider.java @@ -1,7 +1,8 @@ -package com.esp.espflow.service.respository.impl.provider; +package com.esp.espflow.service.respository.impl.provider.wizardsproviders; import com.esp.espflow.entity.WizardEspEntity; import com.esp.espflow.entity.dto.WizardEspDto; +import com.esp.espflow.mappers.WizardEspMapper; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.ArgumentsProvider; @@ -10,7 +11,7 @@ import static com.esp.espflow.util.EspFlowConstants.WIZARD_READ_FLASH_ESP_VIEW; -public class WizardUpdateEspServiceProvider implements ArgumentsProvider { +public class WizardEspServiceFindByNameProvider implements ArgumentsProvider { @Override public Stream provideArguments(ExtensionContext extensionContext) throws Exception { @@ -27,12 +28,17 @@ public Stream provideArguments(ExtensionContext extensionCo .isWizardEnabled(false) .build(); + + WizardEspEntity toSave = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDtoToSave); + WizardEspEntity findById = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); + + var updatedEntity = new WizardEspEntity(); updatedEntity.setId(1L); updatedEntity.setWizardEnabled(false); updatedEntity.setName(WIZARD_READ_FLASH_ESP_VIEW); - return Stream.of(Arguments.of(wizardReadFlashViewDto, wizardReadFlashViewDtoToSave, updatedEntity)); + return Stream.of(Arguments.of(toSave, findById, wizardReadFlashViewDtoToSave, updatedEntity)); } } diff --git a/src/test/java/com/esp/espflow/service/respository/impl/provider/WizardEspServiceProvider.java b/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceSaveAllProvider.java similarity index 71% rename from src/test/java/com/esp/espflow/service/respository/impl/provider/WizardEspServiceProvider.java rename to src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceSaveAllProvider.java index 97a9bec..424e3e6 100644 --- a/src/test/java/com/esp/espflow/service/respository/impl/provider/WizardEspServiceProvider.java +++ b/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceSaveAllProvider.java @@ -1,7 +1,8 @@ -package com.esp.espflow.service.respository.impl.provider; +package com.esp.espflow.service.respository.impl.provider.wizardsproviders; import com.esp.espflow.entity.WizardEspEntity; import com.esp.espflow.entity.dto.WizardEspDto; +import com.esp.espflow.mappers.WizardEspMapper; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.ArgumentsProvider; @@ -11,7 +12,7 @@ import static com.esp.espflow.util.EspFlowConstants.WIZARD_FLASH_ESP_VIEW; import static com.esp.espflow.util.EspFlowConstants.WIZARD_READ_FLASH_ESP_VIEW; -public class WizardEspServiceProvider implements ArgumentsProvider { +public class WizardEspServiceSaveAllProvider implements ArgumentsProvider { @Override public Stream provideArguments(ExtensionContext extensionContext) throws Exception { @@ -38,7 +39,10 @@ public Stream provideArguments(ExtensionContext extensionCo entityFlashEspView.setWizardEnabled(true); entityFlashEspView.setName(WIZARD_FLASH_ESP_VIEW); - return Stream.of(Arguments.of(wizardReadFlashViewDto, wizardFlashViewDto, entityReadFlashView, entityFlashEspView)); + WizardEspEntity toSaveReadDto = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); + WizardEspEntity toSaveFlashDto = WizardEspMapper.INSTANCE.dtoToEntity(wizardFlashViewDto); + + return Stream.of(Arguments.of(toSaveReadDto, toSaveFlashDto, wizardReadFlashViewDto, wizardFlashViewDto, entityReadFlashView, entityFlashEspView)); } } diff --git a/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceSaveProvider.java b/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceSaveProvider.java new file mode 100644 index 0000000..29cdbe0 --- /dev/null +++ b/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceSaveProvider.java @@ -0,0 +1,43 @@ +package com.esp.espflow.service.respository.impl.provider.wizardsproviders; + +import com.esp.espflow.entity.WizardEspEntity; +import com.esp.espflow.entity.dto.WizardEspDto; +import com.esp.espflow.mappers.WizardEspMapper; +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; + +import static com.esp.espflow.util.EspFlowConstants.WIZARD_FLASH_ESP_VIEW; +import static com.esp.espflow.util.EspFlowConstants.WIZARD_READ_FLASH_ESP_VIEW; + +public class WizardEspServiceSaveProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + + var wizardReadFlashViewDto = WizardEspDto.builder() + .id(1L) + .name(WIZARD_READ_FLASH_ESP_VIEW) + .isWizardEnabled(true) + .build(); + + var entityReadFlashView = new WizardEspEntity(); + entityReadFlashView.setId(1L); + entityReadFlashView.setWizardEnabled(true); + entityReadFlashView.setName(WIZARD_READ_FLASH_ESP_VIEW); + + var entityFlashEspView = new WizardEspEntity(); + entityFlashEspView.setId(2L); + entityFlashEspView.setWizardEnabled(true); + entityFlashEspView.setName(WIZARD_FLASH_ESP_VIEW); + + WizardEspEntity toFindBy = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); + WizardEspEntity toSave = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); + + return Stream.of(Arguments.of(toFindBy, toSave, wizardReadFlashViewDto, entityReadFlashView)); + } + +} + diff --git a/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceUpdateProvider.java b/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceUpdateProvider.java new file mode 100644 index 0000000..7df296c --- /dev/null +++ b/src/test/java/com/esp/espflow/service/respository/impl/provider/wizardsproviders/WizardEspServiceUpdateProvider.java @@ -0,0 +1,42 @@ +package com.esp.espflow.service.respository.impl.provider.wizardsproviders; + +import com.esp.espflow.entity.WizardEspEntity; +import com.esp.espflow.entity.dto.WizardEspDto; +import com.esp.espflow.mappers.WizardEspMapper; +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; + +import static com.esp.espflow.util.EspFlowConstants.WIZARD_READ_FLASH_ESP_VIEW; + +public class WizardEspServiceUpdateProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + + WizardEspDto wizardReadFlashViewDto = WizardEspDto.builder() + .id(1L) + .name(WIZARD_READ_FLASH_ESP_VIEW) + .isWizardEnabled(false) + .build(); + + WizardEspDto wizardReadFlashViewDtoToSave = WizardEspDto.builder() + .id(1L) + .name(WIZARD_READ_FLASH_ESP_VIEW) + .isWizardEnabled(false) + .build(); + + WizardEspEntity toSave = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); + WizardEspEntity findById = WizardEspMapper.INSTANCE.dtoToEntity(wizardReadFlashViewDto); + + WizardEspEntity updatedEntity = new WizardEspEntity(); + updatedEntity.setId(1L); + updatedEntity.setWizardEnabled(true); + updatedEntity.setName(WIZARD_READ_FLASH_ESP_VIEW); + + return Stream.of(Arguments.of(toSave, findById, wizardReadFlashViewDtoToSave, updatedEntity)); + } + +}