Skip to content

Commit

Permalink
🚧 #70 test
Browse files Browse the repository at this point in the history
  • Loading branch information
rucko24 committed Dec 14, 2024
1 parent 4770cf2 commit 9cf38b0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -73,8 +74,9 @@ private Mono<EsptoolSha256Dto> startComputeSha256(final String fileName) {
log.info("File: {} sha256: {}", path.getFileName(), processedSha256);
final String osArch = System.getProperty("os.arch");
log.info("Current-OsArch: [{}]", osArch);
if (this.esptool256Service.findBySha256(processedSha256).isPresent()) {
final EsptoolSha256Dto esptoolSha256Dto = this.esptool256Service.findBySha256(processedSha256).get();
final Optional<EsptoolSha256Dto> optionalEsptoolSha256Dto = this.esptool256Service.findBySha256(processedSha256);
if (optionalEsptoolSha256Dto.isPresent()) {
final EsptoolSha256Dto esptoolSha256Dto = optionalEsptoolSha256Dto.get();
log.info("entity osArch: [{}]", esptoolSha256Dto.osArch());
if (esptoolSha256Dto.osArch().contains(osArch)) {
return Mono.just(esptoolSha256Dto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import java.util.Optional;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

/**
Expand All @@ -32,13 +34,17 @@ class ComputeSha256ServiceTest {
@Mock
private ComputeDigestAlgorithmConfiguration computeDigestAlgorithmConfiguration;

private static final String COMPUTED_SHA_256 = "ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194";

@Test
@DisplayName("Input file is empty")
void computeSha256FailureEmpty() {

StepVerifier.create(computeSha256Service.computeSha256(""))
.expectErrorMatches(error -> error.getMessage().contains("Can not compute sha256"))
.verify();

verifyNoInteractions(esptoolSha256Service);
}

@Test
Expand All @@ -48,6 +54,9 @@ void computeSha256FailureNull() {
StepVerifier.create(computeSha256Service.computeSha256(null))
.expectErrorMatches(error -> error.getMessage().contains("Can not compute sha256"))
.verify();

verifyNoInteractions(esptoolSha256Service);

}

@Test
Expand All @@ -59,22 +68,24 @@ void computeSha256Success() {
EsptoolSha256Dto actualEsptoolSha256Dto = EsptoolSha256Dto.builder()
.osArch("amd64")
.esptoolVersion("v4.7.0")
.sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")
.sha256(COMPUTED_SHA_256)
.build();

when(computeDigestAlgorithmConfiguration.getDigestAlgorithm()).thenReturn("SHA-256");
when(esptoolSha256Service.findBySha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")).thenReturn(Optional.of(actualEsptoolSha256Dto));
when(esptoolSha256Service.findBySha256(COMPUTED_SHA_256)).thenReturn(Optional.of(actualEsptoolSha256Dto));

EsptoolSha256Dto expectedEsptoolSha256Dto = EsptoolSha256Dto.builder()
.osArch("amd64")
.esptoolVersion("v4.7.0")
.sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")
.sha256(COMPUTED_SHA_256)
.build();

StepVerifier.create(computeSha256Service.computeSha256("src/test/resources/esptool/esptool-linux-amd64/esptool"))
.expectNext(expectedEsptoolSha256Dto)
.verifyComplete();

verify(esptoolSha256Service).findBySha256(COMPUTED_SHA_256);

System.clearProperty("os.arch");
System.setProperty("os.arch", property);

Expand All @@ -89,16 +100,18 @@ void computeSha256Failure_emptyMono() {
EsptoolSha256Dto actualEsptoolSha256Dto = EsptoolSha256Dto.builder()
.osArch("amd63")
.esptoolVersion("v4.7.0")
.sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")
.sha256(COMPUTED_SHA_256)
.build();

when(computeDigestAlgorithmConfiguration.getDigestAlgorithm()).thenReturn("SHA-256");
when(esptoolSha256Service.findBySha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")).thenReturn(Optional.of(actualEsptoolSha256Dto));
when(esptoolSha256Service.findBySha256(COMPUTED_SHA_256)).thenReturn(Optional.of(actualEsptoolSha256Dto));

StepVerifier.create(computeSha256Service.computeSha256("src/test/resources/esptool/esptool-linux-amd64/esptool"))
.expectErrorMatches(error -> error.getMessage().contains("Can not compute sha256"))
.verify();

verify(esptoolSha256Service).findBySha256(COMPUTED_SHA_256);

System.clearProperty("os.arch");
System.setProperty("os.arch", property);

Expand All @@ -114,6 +127,8 @@ void computeSha256Failure_NoSuchAlgorithmException() {
.expectErrorMatches(error -> error.getMessage().contains("Can not compute sha256"))
.verify();

verifyNoInteractions(esptoolSha256Service);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class EsptoolExecutableServiceImplTest {
@Mock
private EsptoolExecutableRepository esptoolExecutableRepository;

private static final String COMPUTED_SHA_256 = "ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194";

@ParameterizedTest
@ArgumentsSource(EsptoolExecutableServiceSaveProvider.class)
@DisplayName("Save the entity, if esptoolVersion not found and bundled to false")
Expand Down Expand Up @@ -66,8 +68,8 @@ void update(EsptoolExecutableEntity toSave, EsptoolExecutableDto dtoToSave, Espt

when(esptoolExecutableRepository.findByEsptoolVersionWithBundle("v4.7.0", false)).thenReturn(Optional.of(savedFindByEntity));

final ArgumentCaptor<EsptoolExecutableEntity> captor = ArgumentCaptor.forClass(EsptoolExecutableEntity.class);
esptoolExecutableRepository.save(toSave);
final ArgumentCaptor<EsptoolExecutableEntity> captor = ArgumentCaptor.forClass(EsptoolExecutableEntity.class);
verify(esptoolExecutableRepository).save(captor.capture());

assertThat(this.esptoolExecutableService.save(dtoToSave))
Expand Down Expand Up @@ -95,7 +97,7 @@ void findByEsptoolVersionWithBundle() {
.esptoolVersion("v4.7.0")
.isBundled(false)
.isSelected(false)
.sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")
.sha256(COMPUTED_SHA_256)
.build();

when(esptoolExecutableRepository.findByEsptoolVersionWithBundle("v4.7.0", false))
Expand All @@ -109,7 +111,7 @@ void findByEsptoolVersionWithBundle() {
.esptoolVersion("v4.7.0")
.isBundled(false)
.isSelected(false)
.sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")
.sha256(COMPUTED_SHA_256)
.build());

assertThat(this.esptoolExecutableService.findByEsptoolVersionWithBundle("v4.7.0", false))
Expand Down Expand Up @@ -160,7 +162,7 @@ void findByAbsolutePathEsptoolAndIsBundleAndVersion() {
.esptoolVersion("v4.7.0")
.isBundled(false)
.isSelected(true)
.sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")
.sha256(COMPUTED_SHA_256)
.build();

when(esptoolExecutableRepository.findByAbsolutePathEsptoolAndIsBundleAndVersion(
Expand All @@ -175,7 +177,7 @@ void findByAbsolutePathEsptoolAndIsBundleAndVersion() {
.esptoolVersion("v4.7.0")
.isBundled(false)
.isSelected(true)
.sha256("ae1a3fe6eed5bf7e5dbaee78aea868c5e62f80dd43e13a2f69016da86387a194")
.sha256(COMPUTED_SHA_256)
.build();

assertThat(this.esptoolExecutableService.findByAbsolutePathEsptoolAndIsBundleAndVersion(
Expand Down

0 comments on commit 9cf38b0

Please sign in to comment.