Skip to content

Commit

Permalink
🚧 #80 #70
Browse files Browse the repository at this point in the history
  • Loading branch information
rucko24 committed Nov 22, 2024
1 parent e4fa24b commit 2157cc6
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.esp.espflow.service.respository.impl;

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 org.junit.jupiter.api.DisplayName;
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.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Optional;

import static com.esp.espflow.util.EspFlowConstants.WIZARD_READ_FLASH_ESP_VIEW;
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.*;

/**
* @author rub'n
*/
@ExtendWith(MockitoExtension.class)
class WizardEspServiceTest {

@InjectMocks
private WizardEspService wizardEspService;

@Mock
private WizardEspRepository repository;

@ParameterizedTest
@ArgumentsSource(WizardEspServiceProvider.class)
@DisplayName("save items")
void saveAll(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);

assertThatCode(() -> wizardEspService.saveAll(List.of(wizardFlashViewDto, wizardReadFlashViewDto)))
.doesNotThrowAnyException();

verify(repository, times(1)).save(entityReadViewToReturn);
verify(repository, times(1)).save(entityFlashViewToReturn);
verifyNoMoreInteractions(repository);
}

@ParameterizedTest
@ArgumentsSource(WizardUpdateEspServiceProvider.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,
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);

assertThatCode(() -> wizardEspService.save(updatedDto)).doesNotThrowAnyException();

verify(repository, times(1)).findById(findById.getId());
verify(repository, times(1)).save(toSave);
verifyNoMoreInteractions(repository);
}

@ParameterizedTest
@ArgumentsSource(WizardUpdateEspServiceProvider.class)
@DisplayName("")
void updateTwoEntities(WizardEspDto wizardReadFlashViewDto, 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);

assertThatCode(() -> wizardEspService.save(updatedDto)).doesNotThrowAnyException();

verify(repository, times(1)).findById(findById.getId());
verify(repository, times(1)).save(toSave);
verifyNoMoreInteractions(repository);

}

@ParameterizedTest
@ArgumentsSource(WizardEspServiceProvider.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);

when(repository.findById(toFindBy.getId())).thenReturn(Optional.empty());
when(repository.save(toSave)).thenReturn(entityReadView);

assertThatCode(() -> wizardEspService.save(wizardReadFlashViewDto)).doesNotThrowAnyException();

verify(repository, times(1)).findById(toFindBy.getId());
verify(repository, times(1)).save(entityReadView);
verifyNoMoreInteractions(repository);
}

@ParameterizedTest
@ArgumentsSource(WizardUpdateEspServiceProvider.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);

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));

assertThatCode(() -> wizardEspService.save(wizardReadFlashViewDtoToSave)).doesNotThrowAnyException();

assertThat(wizardEspService.findByName(WIZARD_READ_FLASH_ESP_VIEW))
.usingRecursiveComparison()
.isEqualTo(Optional.of(expectedUpdatedEntity));

assertAll(() -> {
wizardEspService.findByName(WIZARD_READ_FLASH_ESP_VIEW)
.ifPresent(present -> {
assertThat(present.id()).isEqualTo(1);
assertThat(present.isWizardEnabled()).isFalse();
assertThat(present.name()).contains(WIZARD_READ_FLASH_ESP_VIEW);
});
});

verify(repository, times(1)).findById(toFindBy.getId());
verify(repository, times(1)).save(expectedUpdatedEntity);
verify(repository, times(2)).findByName(WIZARD_READ_FLASH_ESP_VIEW);
verifyNoMoreInteractions(repository);
}

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

import com.esp.espflow.entity.WizardEspEntity;
import com.esp.espflow.entity.dto.WizardEspDto;
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 WizardEspServiceProvider implements ArgumentsProvider {

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

var wizardReadFlashViewDto = WizardEspDto.builder()
.id(1L)
.name(WIZARD_READ_FLASH_ESP_VIEW)
.isWizardEnabled(true)
.build();

var wizardFlashViewDto = WizardEspDto.builder()
.id(2L)
.name(WIZARD_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);

return Stream.of(Arguments.of(wizardReadFlashViewDto, wizardFlashViewDto, entityReadFlashView, entityFlashEspView));
}

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

import com.esp.espflow.entity.WizardEspEntity;
import com.esp.espflow.entity.dto.WizardEspDto;
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 WizardUpdateEspServiceProvider implements ArgumentsProvider {

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

var wizardReadFlashViewDto = WizardEspDto.builder()
.id(1L)
.name(WIZARD_READ_FLASH_ESP_VIEW)
.isWizardEnabled(true)
.build();

var wizardReadFlashViewDtoToSave = WizardEspDto.builder()
.id(1L)
.name(WIZARD_READ_FLASH_ESP_VIEW)
.isWizardEnabled(false)
.build();

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));
}

}

0 comments on commit 2157cc6

Please sign in to comment.