-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(base64): Base64 artifacts not utilizing encode flag
This commit addresses an issue where Base64ArtifactDefinition was not properly utilizing the `shouldEncode` method/field. This was mainly due to having multiple `@Builder`s, and also `Boolean`s defaulting to false. To fix this we simply add a `null` check against the new flag that was added * update(gitignore): add some appropriate gitignore options * idea folder * lombok.config Authored by: benjamin-j-powell <[email protected]>
- Loading branch information
Showing
4 changed files
with
53 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...c/test/java/io/spinnaker/pipelinebuilder/json/artifacts/Base64ArtifactDefinitionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.spinnaker.pipelinebuilder.json.artifacts; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.Arrays; | ||
import java.util.Base64; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class Base64ArtifactDefinitionTest { | ||
@ParameterizedTest | ||
@MethodSource("referenceArguments") | ||
public void reference(String content, String expected, Boolean shouldEncode) { | ||
Base64ArtifactDefinition artifact = Base64ArtifactDefinition.builder() | ||
.shouldEncode(shouldEncode) | ||
.contents(content) | ||
.build(); | ||
|
||
byte[] bytes = content.getBytes(); | ||
String reference = artifact.getReference(); | ||
// resolvedShouldEncode should consider whether we planned to encode, | ||
// and account for it being null. | ||
// | ||
// The default value for shouldEncode is true, so if null was passed in, | ||
// we need to ensure that resolvedShouldEncode is true. | ||
Boolean resolvedShouldEncode = shouldEncode == null || shouldEncode; | ||
// didContentBytesGetEncoded compares the bytes based on whether it | ||
// encoded the reference or not. So if the reference was base64 encoded, | ||
// they should be different, and same otherwise. | ||
Boolean didContentBytesGetEncoded = !Arrays.equals(bytes, reference.getBytes()); | ||
assertEquals(resolvedShouldEncode, didContentBytesGetEncoded); | ||
assertEquals(expected, reference); | ||
} | ||
|
||
public static Stream<Arguments> referenceArguments() { | ||
String content = "hello world"; | ||
String contentBase64 = Base64.getEncoder().encodeToString(content.getBytes()); | ||
|
||
return Stream.of( | ||
Arguments.of(content, contentBase64, null), | ||
Arguments.of(content, contentBase64, true), | ||
Arguments.of(content, content, false) | ||
); | ||
} | ||
} |