Skip to content

Commit

Permalink
Excavator: Prefer AssertJ (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
svc-excavator-bot authored Nov 2, 2022
1 parent 0b1842f commit dd1019c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.palantir.config.crypto;

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

import com.google.common.collect.ImmutableMap;
import com.palantir.config.crypto.algorithm.Algorithm;
Expand All @@ -28,7 +29,6 @@
import java.nio.file.Path;
import net.sourceforge.argparse4j.inf.Namespace;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -87,16 +87,18 @@ public void weEncryptAndPrintAValueUsingRsa() throws Exception {

@Test
public void weFailIfTheKeyfileDoesNotExist() {
Assertions.assertThrows(NoSuchFileException.class, () -> {
Path tempFilePath = Files.createTempDirectory("temp-key-directory").resolve("test.key");

Namespace namespace = new Namespace(ImmutableMap.of(
EncryptConfigValueCommand.KEYFILE,
tempFilePath.toString(),
EncryptConfigValueCommand.VALUE,
plaintext));

command.run(null, namespace);
});
assertThatThrownBy(() -> {
Path tempFilePath =
Files.createTempDirectory("temp-key-directory").resolve("test.key");

Namespace namespace = new Namespace(ImmutableMap.of(
EncryptConfigValueCommand.KEYFILE,
tempFilePath.toString(),
EncryptConfigValueCommand.VALUE,
plaintext));

command.run(null, namespace);
})
.isInstanceOf(NoSuchFileException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package com.palantir.config.crypto;

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

import com.google.common.collect.ImmutableMap;
import com.palantir.config.crypto.algorithm.Algorithm;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import net.sourceforge.argparse4j.inf.Namespace;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public final class GenerateKeyCommandTest {
Expand Down Expand Up @@ -55,17 +55,19 @@ public void weGenerateAValidAesKey() throws Exception {

@Test
public void weDoNotOverwriteAnExistingKeyfile() {
Assertions.assertThrows(FileAlreadyExistsException.class, () -> {
Path tempFilePath = Files.createTempDirectory("temp-key-directory").resolve("test.key");
String algorithm = "AES";
assertThatThrownBy(() -> {
Path tempFilePath =
Files.createTempDirectory("temp-key-directory").resolve("test.key");
String algorithm = "AES";

// create the file
Files.createFile(tempFilePath);
// create the file
Files.createFile(tempFilePath);

Namespace namespace = new Namespace(ImmutableMap.of(
GenerateKeyCommand.ALGORITHM, algorithm, GenerateKeyCommand.FILE, tempFilePath.toString()));
Namespace namespace = new Namespace(ImmutableMap.of(
GenerateKeyCommand.ALGORITHM, algorithm, GenerateKeyCommand.FILE, tempFilePath.toString()));

command.run(null, namespace);
});
command.run(null, namespace);
})
.isInstanceOf(FileAlreadyExistsException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
package com.palantir.config.crypto;

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

import com.palantir.config.crypto.algorithm.Algorithm;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public final class EncryptedValueTest {
Expand Down Expand Up @@ -97,18 +97,20 @@ public void weCanDecryptValueEncryptedUsingExistingRsaKey() {

@Test
public void weFailToConstructWithInvalidPrefix() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
String invalid = "anc:TCkE/OT7xsKWqP4SRNBEj54Pk7wDMQzMGJtX90toFuGeejM/LQBDTZ8hEaKQt/3i";
EncryptedValue.fromString(invalid);
}); // throws
assertThatThrownBy(() -> {
String invalid = "anc:TCkE/OT7xsKWqP4SRNBEj54Pk7wDMQzMGJtX90toFuGeejM/LQBDTZ8hEaKQt/3i";
EncryptedValue.fromString(invalid);
})
.isInstanceOf(IllegalArgumentException.class); // throws
}

@Test
public void weFailToConstructWithAnInvalidEncryptedValue() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
String invalid = "enc:verysecret^^";
EncryptedValue.fromString(invalid);
}); // throws if suffix is not a base64-encoded string
assertThatThrownBy(() -> {
String invalid = "enc:verysecret^^";
EncryptedValue.fromString(invalid);
})
.isInstanceOf(IllegalArgumentException.class); // throws if suffix is not a base64-encoded string
}

private void weCannotDecryptWithTheWrongKey(Algorithm algorithm) throws NoSuchAlgorithmException {
Expand All @@ -123,16 +125,18 @@ private void weCannotDecryptWithTheWrongKey(Algorithm algorithm) throws NoSuchAl

@Test
public void weCannotDecryptWithTheWrongAesKey() throws NoSuchAlgorithmException {
Assertions.assertThrows(RuntimeException.class, () -> {
weCannotDecryptWithTheWrongKey(Algorithm.AES);
});
assertThatThrownBy(() -> {
weCannotDecryptWithTheWrongKey(Algorithm.AES);
})
.isInstanceOf(RuntimeException.class);
}

@Test
public void weCannotDecryptWithTheWrongRsaKey() throws NoSuchAlgorithmException {
Assertions.assertThrows(RuntimeException.class, () -> {
weCannotDecryptWithTheWrongKey(Algorithm.RSA);
});
assertThatThrownBy(() -> {
weCannotDecryptWithTheWrongKey(Algorithm.RSA);
})
.isInstanceOf(RuntimeException.class);
}

private void weCanDecryptAValue(Algorithm algorithm) {
Expand Down

0 comments on commit dd1019c

Please sign in to comment.