Skip to content

Commit

Permalink
Improve the code and add another test case ... which is working
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Sep 9, 2024
1 parent 99c477b commit d8916c7
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions src/test/java/dev/snowdrop/JacksonYamlTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

public class JacksonYamlTests {

Action myAction;

@Test
public void fromYamlToObject() throws JsonProcessingException {
String yamlStr = """
Expand Down Expand Up @@ -84,7 +87,7 @@ public void fromObjectToYaml() throws JsonProcessingException {
}

@Test
public void checkYAMLBlockLiteral() throws JsonProcessingException {
public void checkScriptTextBlock_YAMLRendering() throws JsonProcessingException {

ObjectMapper mapper = new ObjectMapper(
new YAMLFactory()
Expand Down Expand Up @@ -128,13 +131,14 @@ public void checkYAMLBlockLiteral() throws JsonProcessingException {
finally: false
""";

Action myAction = new Action();
myAction = new Action();
myAction.setId(1);
myAction.setScript(bashScript);
assertEquals(expectedYaml,mapper.writeValueAsString(myAction));
}

@Test
public void checkYAMLScriptWithCarriageReturn() throws JsonProcessingException {
public void checkScriptWithCRMultiLines_YAMLRendering() throws JsonProcessingException {

ObjectMapper mapper = new ObjectMapper(
new YAMLFactory()
Expand All @@ -152,7 +156,49 @@ public void checkYAMLScriptWithCarriageReturn() throws JsonProcessingException {
"fi\n";

String expectedYaml = """
id: 2
id: 1
name: null
ref: null
script: |
#!/usr/bin/env bash
set -e
mkdir -p ~/.ssh
if [ -e "/ssh/error" ]; then
#no server could be provisioned
cat /ssh/error
exit 1
fi
scriptFileUrl: null
runAfter: null
image: null
params: null
workspaces: null
volumes: null
args: null
when: null
results: null
finally: false
""";

myAction = new Action();
myAction.setId(1);
myAction.setScript(bashScript);
assertEquals(expectedYaml,mapper.writeValueAsString(myAction));
}

@Test
public void checkOneLineScriptWithCR_YAMLRendering() throws JsonProcessingException {

ObjectMapper mapper = new ObjectMapper(
new YAMLFactory()
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER) // Disable "---"
.enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE) // Use block style for multi-line strings
);

String bashScript = "#!/usr/bin/env bash\nset -e\nmkdir -p ~/.ssh\nif [ -e \"/ssh/error\" ]; then\n #no server could be provisioned\n cat /ssh/error\n exit 1\nfi\n";

String expectedYaml = """
id: 1
name: null
ref: null
script: |
Expand All @@ -176,7 +222,8 @@ public void checkYAMLScriptWithCarriageReturn() throws JsonProcessingException {
finally: false
""";

Action myAction = new Action();
myAction = new Action();
myAction.setId(1);
myAction.setScript(bashScript);
assertEquals(expectedYaml,mapper.writeValueAsString(myAction));
}
Expand Down

0 comments on commit d8916c7

Please sign in to comment.