From eb4e462b36f2e486e7b51865ea6e9a62c161d928 Mon Sep 17 00:00:00 2001 From: crist Date: Tue, 4 Feb 2020 18:20:00 +0200 Subject: [PATCH 01/29] [BAEL-3750] Breaking YAML Strings Over Multiple Lines --- yaml/README.md | 7 ++ yaml/pom.xml | 29 ++++++ .../multiline/MultiLineStringsUnitTest.java | 90 +++++++++++++++++++ .../src/test/resources/multi-line/folded.yaml | 4 + .../test/resources/multi-line/folded2.yaml | 9 ++ .../resources/multi-line/folded_keep.yaml | 6 ++ .../resources/multi-line/folded_strip.yaml | 7 ++ .../test/resources/multi-line/literal.yaml | 4 + .../test/resources/multi-line/literal2.yaml | 10 +++ .../resources/multi-line/literal_keep.yaml | 5 ++ .../resources/multi-line/literal_strip.yaml | 5 ++ .../multi-line/plain_double_quotes.yaml | 1 + .../multi-line/plain_single_quotes.yaml | 3 + 13 files changed, 180 insertions(+) create mode 100644 yaml/README.md create mode 100644 yaml/pom.xml create mode 100644 yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java create mode 100644 yaml/src/test/resources/multi-line/folded.yaml create mode 100644 yaml/src/test/resources/multi-line/folded2.yaml create mode 100644 yaml/src/test/resources/multi-line/folded_keep.yaml create mode 100644 yaml/src/test/resources/multi-line/folded_strip.yaml create mode 100644 yaml/src/test/resources/multi-line/literal.yaml create mode 100644 yaml/src/test/resources/multi-line/literal2.yaml create mode 100644 yaml/src/test/resources/multi-line/literal_keep.yaml create mode 100644 yaml/src/test/resources/multi-line/literal_strip.yaml create mode 100644 yaml/src/test/resources/multi-line/plain_double_quotes.yaml create mode 100644 yaml/src/test/resources/multi-line/plain_single_quotes.yaml diff --git a/yaml/README.md b/yaml/README.md new file mode 100644 index 0000000000..ebf403eb57 --- /dev/null +++ b/yaml/README.md @@ -0,0 +1,7 @@ +## YAML + +This module contains articles about YAML + +### Relevant articles + + diff --git a/yaml/pom.xml b/yaml/pom.xml new file mode 100644 index 0000000000..c801505c6a --- /dev/null +++ b/yaml/pom.xml @@ -0,0 +1,29 @@ + + + + 4.0.0 + yaml + 1.0-SNAPSHOT + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.yaml + snakeyaml + 1.21 + + + + + 1.8 + 1.21 + + + \ No newline at end of file diff --git a/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java b/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java new file mode 100644 index 0000000000..c97faa355a --- /dev/null +++ b/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java @@ -0,0 +1,90 @@ +package com.baeldung.multiline; + +import org.junit.Before; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +import java.io.File; +import java.io.InputStream; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class MultiLineStringsUnitTest { + + private Yaml yaml; + + @Before + public void setup() { + yaml = new Yaml(); + } + + @Test + public void whenLiteral_ThenLineBreaksArePresent() { + String key = parseYamlKey("literal.yaml"); + assertEquals("Line1\nLine2\nLine3", key); + } + + @Test + public void whenLiteral_ThenEndingBreaksAreReducedToOne() { + String key = parseYamlKey("literal2.yaml"); + assertEquals("\n\nLine1\n\nLine2\n\nLine3\n", key); + } + + @Test + public void whenFolded_ThenLineBreaksAreReplaced() { + String key = parseYamlKey("folded.yaml"); + assertEquals("Line1 Line2 Line3", key); + } + + @Test + public void whenFolded_ThenEmptyLinesAreReducedToOne() { + String key = parseYamlKey("folded2.yaml"); + assertEquals("Line1\nLine2\n\nLine3\n", key); + } + + @Test + public void whenLiteralKeep_ThenLastEmptyLinesArePresent() { + String key = parseYamlKey("literal_keep.yaml"); + assertEquals("Line1\nLine2\nLine3\n\n", key); + } + + @Test + public void whenLiteralStrip_ThenLastEmptyLinesAreRemoved() { + String key = parseYamlKey("literal_strip.yaml"); + assertEquals("Line1\nLine2\nLine3", key); + } + + @Test + public void whenFoldedKeep_ThenLastEmptyLinesArePresent() { + String key = parseYamlKey("folded_keep.yaml"); + assertEquals("Line1 Line2 Line3\n\n\n", key); + } + + @Test + public void whenFoldedStrip_ThenLastEmptyLinesAreRemoved() { + String key = parseYamlKey("folded_strip.yaml"); + assertEquals("Line1 Line2 Line3", key); + } + + @Test + public void whenDoubleQuotes_ThenExplicitBreaksArePreserved() { + String key = parseYamlKey("plain_double_quotes.yaml"); + assertEquals("Line1\nLine2\nLine3", key); + } + + @Test + public void whenSingleQuotes_ThenExplicitBreaksAreIgnored() { + String key = parseYamlKey("plain_single_quotes.yaml"); + assertEquals("Line1\\nLine2\nLine3", key); + } + + private String parseYamlKey(String fileName) { + InputStream inputStream = this.getClass() + .getClassLoader() + .getResourceAsStream("multi-line" + File.separator + fileName); + Map parsed = yaml.load(inputStream); + return parsed.get("key"); + } + +} diff --git a/yaml/src/test/resources/multi-line/folded.yaml b/yaml/src/test/resources/multi-line/folded.yaml new file mode 100644 index 0000000000..c5fa743a08 --- /dev/null +++ b/yaml/src/test/resources/multi-line/folded.yaml @@ -0,0 +1,4 @@ +key: > + Line1 + Line2 + Line3 \ No newline at end of file diff --git a/yaml/src/test/resources/multi-line/folded2.yaml b/yaml/src/test/resources/multi-line/folded2.yaml new file mode 100644 index 0000000000..a7f244994a --- /dev/null +++ b/yaml/src/test/resources/multi-line/folded2.yaml @@ -0,0 +1,9 @@ +key: > + Line1 + + Line2 + + + Line3 + + diff --git a/yaml/src/test/resources/multi-line/folded_keep.yaml b/yaml/src/test/resources/multi-line/folded_keep.yaml new file mode 100644 index 0000000000..555291fd26 --- /dev/null +++ b/yaml/src/test/resources/multi-line/folded_keep.yaml @@ -0,0 +1,6 @@ +key: >+ + Line1 + Line2 + Line3 + + diff --git a/yaml/src/test/resources/multi-line/folded_strip.yaml b/yaml/src/test/resources/multi-line/folded_strip.yaml new file mode 100644 index 0000000000..0a3a246dc2 --- /dev/null +++ b/yaml/src/test/resources/multi-line/folded_strip.yaml @@ -0,0 +1,7 @@ +key: >- + Line1 + Line2 + Line3 + + + diff --git a/yaml/src/test/resources/multi-line/literal.yaml b/yaml/src/test/resources/multi-line/literal.yaml new file mode 100644 index 0000000000..7e02501a33 --- /dev/null +++ b/yaml/src/test/resources/multi-line/literal.yaml @@ -0,0 +1,4 @@ +key: | + Line1 + Line2 + Line3 \ No newline at end of file diff --git a/yaml/src/test/resources/multi-line/literal2.yaml b/yaml/src/test/resources/multi-line/literal2.yaml new file mode 100644 index 0000000000..7c7fed0163 --- /dev/null +++ b/yaml/src/test/resources/multi-line/literal2.yaml @@ -0,0 +1,10 @@ +key: | + + + Line1 + + Line2 + + Line3 + + diff --git a/yaml/src/test/resources/multi-line/literal_keep.yaml b/yaml/src/test/resources/multi-line/literal_keep.yaml new file mode 100644 index 0000000000..37f22684dd --- /dev/null +++ b/yaml/src/test/resources/multi-line/literal_keep.yaml @@ -0,0 +1,5 @@ +key: |+ + Line1 + Line2 + Line3 + diff --git a/yaml/src/test/resources/multi-line/literal_strip.yaml b/yaml/src/test/resources/multi-line/literal_strip.yaml new file mode 100644 index 0000000000..0791e13d5d --- /dev/null +++ b/yaml/src/test/resources/multi-line/literal_strip.yaml @@ -0,0 +1,5 @@ +key: |- + Line1 + Line2 + Line3 + diff --git a/yaml/src/test/resources/multi-line/plain_double_quotes.yaml b/yaml/src/test/resources/multi-line/plain_double_quotes.yaml new file mode 100644 index 0000000000..ccab040a27 --- /dev/null +++ b/yaml/src/test/resources/multi-line/plain_double_quotes.yaml @@ -0,0 +1 @@ +key: "Line1\nLine2\nLine3" \ No newline at end of file diff --git a/yaml/src/test/resources/multi-line/plain_single_quotes.yaml b/yaml/src/test/resources/multi-line/plain_single_quotes.yaml new file mode 100644 index 0000000000..acbf35a462 --- /dev/null +++ b/yaml/src/test/resources/multi-line/plain_single_quotes.yaml @@ -0,0 +1,3 @@ +key: 'Line1\nLine2 + + Line3' \ No newline at end of file From a1d246f2e1c6849abd0bdca347f910d3ee9f183b Mon Sep 17 00:00:00 2001 From: crist Date: Tue, 4 Feb 2020 18:22:20 +0200 Subject: [PATCH 02/29] Added yamo module to appropriate profiles --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index 2a0ae0d8d8..379c741648 100644 --- a/pom.xml +++ b/pom.xml @@ -781,6 +781,7 @@ wildfly xml xstream + yaml @@ -1304,6 +1305,7 @@ wildfly xml xstream + yaml From e2de2a78d19dd74eb4f66b212693644c5448bbf5 Mon Sep 17 00:00:00 2001 From: crist Date: Thu, 6 Feb 2020 02:32:40 +0200 Subject: [PATCH 03/29] Article improvements --- .../multiline/MultiLineStringsUnitTest.java | 26 +++++++++---------- .../test/resources/multi-line/folded2.yaml | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java b/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java index c97faa355a..a99de63a47 100644 --- a/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java +++ b/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java @@ -21,70 +21,70 @@ public class MultiLineStringsUnitTest { @Test public void whenLiteral_ThenLineBreaksArePresent() { - String key = parseYamlKey("literal.yaml"); + String key = parseYamlKey("literal.yaml", "key"); assertEquals("Line1\nLine2\nLine3", key); } @Test public void whenLiteral_ThenEndingBreaksAreReducedToOne() { - String key = parseYamlKey("literal2.yaml"); + String key = parseYamlKey("literal2.yaml", "key"); assertEquals("\n\nLine1\n\nLine2\n\nLine3\n", key); } @Test public void whenFolded_ThenLineBreaksAreReplaced() { - String key = parseYamlKey("folded.yaml"); + String key = parseYamlKey("folded.yaml", "key"); assertEquals("Line1 Line2 Line3", key); } @Test public void whenFolded_ThenEmptyLinesAreReducedToOne() { - String key = parseYamlKey("folded2.yaml"); - assertEquals("Line1\nLine2\n\nLine3\n", key); + String key = parseYamlKey("folded2.yaml", "key"); + assertEquals("Line1 Line2\n\nLine3\n", key); } @Test public void whenLiteralKeep_ThenLastEmptyLinesArePresent() { - String key = parseYamlKey("literal_keep.yaml"); + String key = parseYamlKey("literal_keep.yaml", "key"); assertEquals("Line1\nLine2\nLine3\n\n", key); } @Test public void whenLiteralStrip_ThenLastEmptyLinesAreRemoved() { - String key = parseYamlKey("literal_strip.yaml"); + String key = parseYamlKey("literal_strip.yaml", "key"); assertEquals("Line1\nLine2\nLine3", key); } @Test public void whenFoldedKeep_ThenLastEmptyLinesArePresent() { - String key = parseYamlKey("folded_keep.yaml"); + String key = parseYamlKey("folded_keep.yaml", "key"); assertEquals("Line1 Line2 Line3\n\n\n", key); } @Test public void whenFoldedStrip_ThenLastEmptyLinesAreRemoved() { - String key = parseYamlKey("folded_strip.yaml"); + String key = parseYamlKey("folded_strip.yaml", "key"); assertEquals("Line1 Line2 Line3", key); } @Test public void whenDoubleQuotes_ThenExplicitBreaksArePreserved() { - String key = parseYamlKey("plain_double_quotes.yaml"); + String key = parseYamlKey("plain_double_quotes.yaml", "key"); assertEquals("Line1\nLine2\nLine3", key); } @Test public void whenSingleQuotes_ThenExplicitBreaksAreIgnored() { - String key = parseYamlKey("plain_single_quotes.yaml"); + String key = parseYamlKey("plain_single_quotes.yaml", "key"); assertEquals("Line1\\nLine2\nLine3", key); } - private String parseYamlKey(String fileName) { + String parseYamlKey(String fileName, String key) { InputStream inputStream = this.getClass() .getClassLoader() .getResourceAsStream("multi-line" + File.separator + fileName); Map parsed = yaml.load(inputStream); - return parsed.get("key"); + return parsed.get(key); } } diff --git a/yaml/src/test/resources/multi-line/folded2.yaml b/yaml/src/test/resources/multi-line/folded2.yaml index a7f244994a..5c55ef62f2 100644 --- a/yaml/src/test/resources/multi-line/folded2.yaml +++ b/yaml/src/test/resources/multi-line/folded2.yaml @@ -1,9 +1,9 @@ key: > Line1 - Line2 Line3 +... \ No newline at end of file From 845f66ee2e21265beedbc78eaeaeaf1a02e90cf3 Mon Sep 17 00:00:00 2001 From: crist Date: Thu, 6 Feb 2020 02:37:44 +0200 Subject: [PATCH 04/29] Fix --- yaml/src/test/resources/multi-line/folded2.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/yaml/src/test/resources/multi-line/folded2.yaml b/yaml/src/test/resources/multi-line/folded2.yaml index 5c55ef62f2..735abf9b2f 100644 --- a/yaml/src/test/resources/multi-line/folded2.yaml +++ b/yaml/src/test/resources/multi-line/folded2.yaml @@ -6,4 +6,3 @@ key: > Line3 -... \ No newline at end of file From 2b51ce0ebcfda726fc43dbf3439a216cf209a483 Mon Sep 17 00:00:00 2001 From: crist Date: Mon, 10 Feb 2020 15:26:56 +0200 Subject: [PATCH 05/29] [BAEL-3750] Moved multi line yaml files from the yaml module to the libraries-data-io folder --- .../snakeyaml/MultiLineStringsUnitTest.java | 90 +++++++++++++++++++ .../test/resources/yaml/multiline/folded.yaml | 4 + .../resources/yaml/multiline/folded2.yaml | 8 ++ .../resources/yaml/multiline/folded_keep.yaml | 6 ++ .../yaml/multiline/folded_strip.yaml | 7 ++ .../resources/yaml/multiline/literal.yaml | 4 + .../resources/yaml/multiline/literal2.yaml | 10 +++ .../yaml/multiline/literal_keep.yaml | 5 ++ .../yaml/multiline/literal_strip.yaml | 5 ++ .../yaml/multiline/plain_double_quotes.yaml | 1 + .../yaml/multiline/plain_single_quotes.yaml | 3 + 11 files changed, 143 insertions(+) create mode 100644 libraries-data-io/src/test/java/com/baeldung/libraries/snakeyaml/MultiLineStringsUnitTest.java create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/folded.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/folded2.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/folded_keep.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/folded_strip.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/literal.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/literal2.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/literal_keep.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/literal_strip.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/plain_double_quotes.yaml create mode 100644 libraries-data-io/src/test/resources/yaml/multiline/plain_single_quotes.yaml diff --git a/libraries-data-io/src/test/java/com/baeldung/libraries/snakeyaml/MultiLineStringsUnitTest.java b/libraries-data-io/src/test/java/com/baeldung/libraries/snakeyaml/MultiLineStringsUnitTest.java new file mode 100644 index 0000000000..836ac86339 --- /dev/null +++ b/libraries-data-io/src/test/java/com/baeldung/libraries/snakeyaml/MultiLineStringsUnitTest.java @@ -0,0 +1,90 @@ +package com.baeldung.libraries.snakeyaml; + +import org.junit.Before; +import org.junit.Test; +import org.yaml.snakeyaml.Yaml; + +import java.io.File; +import java.io.InputStream; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class MultiLineStringsUnitTest { + + private Yaml yaml; + + @Before + public void setup() { + yaml = new Yaml(); + } + + @Test + public void whenLiteral_ThenLineBreaksArePresent() { + String key = parseYamlKey("literal.yaml", "key"); + assertEquals("Line1\nLine2\nLine3", key); + } + + @Test + public void whenLiteral_ThenEndingBreaksAreReducedToOne() { + String key = parseYamlKey("literal2.yaml", "key"); + assertEquals("\n\nLine1\n\nLine2\n\nLine3\n", key); + } + + @Test + public void whenFolded_ThenLineBreaksAreReplaced() { + String key = parseYamlKey("folded.yaml", "key"); + assertEquals("Line1 Line2 Line3", key); + } + + @Test + public void whenFolded_ThenEmptyLinesAreReducedToOne() { + String key = parseYamlKey("folded2.yaml", "key"); + assertEquals("Line1 Line2\n\nLine3\n", key); + } + + @Test + public void whenLiteralKeep_ThenLastEmptyLinesArePresent() { + String key = parseYamlKey("literal_keep.yaml", "key"); + assertEquals("Line1\nLine2\nLine3\n\n", key); + } + + @Test + public void whenLiteralStrip_ThenLastEmptyLinesAreRemoved() { + String key = parseYamlKey("literal_strip.yaml", "key"); + assertEquals("Line1\nLine2\nLine3", key); + } + + @Test + public void whenFoldedKeep_ThenLastEmptyLinesArePresent() { + String key = parseYamlKey("folded_keep.yaml", "key"); + assertEquals("Line1 Line2 Line3\n\n\n", key); + } + + @Test + public void whenFoldedStrip_ThenLastEmptyLinesAreRemoved() { + String key = parseYamlKey("folded_strip.yaml", "key"); + assertEquals("Line1 Line2 Line3", key); + } + + @Test + public void whenDoubleQuotes_ThenExplicitBreaksArePreserved() { + String key = parseYamlKey("plain_double_quotes.yaml", "key"); + assertEquals("Line1\nLine2\nLine3", key); + } + + @Test + public void whenSingleQuotes_ThenExplicitBreaksAreIgnored() { + String key = parseYamlKey("plain_single_quotes.yaml", "key"); + assertEquals("Line1\\nLine2\nLine3", key); + } + + String parseYamlKey(String fileName, String key) { + InputStream inputStream = this.getClass() + .getClassLoader() + .getResourceAsStream("yaml" + File.separator + "multiline" + File.separator + fileName); + Map parsed = yaml.load(inputStream); + return parsed.get(key); + } + +} diff --git a/libraries-data-io/src/test/resources/yaml/multiline/folded.yaml b/libraries-data-io/src/test/resources/yaml/multiline/folded.yaml new file mode 100644 index 0000000000..c5fa743a08 --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/folded.yaml @@ -0,0 +1,4 @@ +key: > + Line1 + Line2 + Line3 \ No newline at end of file diff --git a/libraries-data-io/src/test/resources/yaml/multiline/folded2.yaml b/libraries-data-io/src/test/resources/yaml/multiline/folded2.yaml new file mode 100644 index 0000000000..735abf9b2f --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/folded2.yaml @@ -0,0 +1,8 @@ +key: > + Line1 + Line2 + + + Line3 + + diff --git a/libraries-data-io/src/test/resources/yaml/multiline/folded_keep.yaml b/libraries-data-io/src/test/resources/yaml/multiline/folded_keep.yaml new file mode 100644 index 0000000000..555291fd26 --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/folded_keep.yaml @@ -0,0 +1,6 @@ +key: >+ + Line1 + Line2 + Line3 + + diff --git a/libraries-data-io/src/test/resources/yaml/multiline/folded_strip.yaml b/libraries-data-io/src/test/resources/yaml/multiline/folded_strip.yaml new file mode 100644 index 0000000000..0a3a246dc2 --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/folded_strip.yaml @@ -0,0 +1,7 @@ +key: >- + Line1 + Line2 + Line3 + + + diff --git a/libraries-data-io/src/test/resources/yaml/multiline/literal.yaml b/libraries-data-io/src/test/resources/yaml/multiline/literal.yaml new file mode 100644 index 0000000000..7e02501a33 --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/literal.yaml @@ -0,0 +1,4 @@ +key: | + Line1 + Line2 + Line3 \ No newline at end of file diff --git a/libraries-data-io/src/test/resources/yaml/multiline/literal2.yaml b/libraries-data-io/src/test/resources/yaml/multiline/literal2.yaml new file mode 100644 index 0000000000..7c7fed0163 --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/literal2.yaml @@ -0,0 +1,10 @@ +key: | + + + Line1 + + Line2 + + Line3 + + diff --git a/libraries-data-io/src/test/resources/yaml/multiline/literal_keep.yaml b/libraries-data-io/src/test/resources/yaml/multiline/literal_keep.yaml new file mode 100644 index 0000000000..37f22684dd --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/literal_keep.yaml @@ -0,0 +1,5 @@ +key: |+ + Line1 + Line2 + Line3 + diff --git a/libraries-data-io/src/test/resources/yaml/multiline/literal_strip.yaml b/libraries-data-io/src/test/resources/yaml/multiline/literal_strip.yaml new file mode 100644 index 0000000000..0791e13d5d --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/literal_strip.yaml @@ -0,0 +1,5 @@ +key: |- + Line1 + Line2 + Line3 + diff --git a/libraries-data-io/src/test/resources/yaml/multiline/plain_double_quotes.yaml b/libraries-data-io/src/test/resources/yaml/multiline/plain_double_quotes.yaml new file mode 100644 index 0000000000..ccab040a27 --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/plain_double_quotes.yaml @@ -0,0 +1 @@ +key: "Line1\nLine2\nLine3" \ No newline at end of file diff --git a/libraries-data-io/src/test/resources/yaml/multiline/plain_single_quotes.yaml b/libraries-data-io/src/test/resources/yaml/multiline/plain_single_quotes.yaml new file mode 100644 index 0000000000..acbf35a462 --- /dev/null +++ b/libraries-data-io/src/test/resources/yaml/multiline/plain_single_quotes.yaml @@ -0,0 +1,3 @@ +key: 'Line1\nLine2 + + Line3' \ No newline at end of file From 7feb9bfb1ec7e9502d482fef57867439f831f6b4 Mon Sep 17 00:00:00 2001 From: crist Date: Mon, 10 Feb 2020 15:29:53 +0200 Subject: [PATCH 06/29] [BAEL-3750] deleted the yaml module --- pom.xml | 2 - yaml/README.md | 7 -- yaml/pom.xml | 29 ------ .../multiline/MultiLineStringsUnitTest.java | 90 ------------------- .../src/test/resources/multi-line/folded.yaml | 4 - .../test/resources/multi-line/folded2.yaml | 8 -- .../resources/multi-line/folded_keep.yaml | 6 -- .../resources/multi-line/folded_strip.yaml | 7 -- .../test/resources/multi-line/literal.yaml | 4 - .../test/resources/multi-line/literal2.yaml | 10 --- .../resources/multi-line/literal_keep.yaml | 5 -- .../resources/multi-line/literal_strip.yaml | 5 -- .../multi-line/plain_double_quotes.yaml | 1 - .../multi-line/plain_single_quotes.yaml | 3 - 14 files changed, 181 deletions(-) delete mode 100644 yaml/README.md delete mode 100644 yaml/pom.xml delete mode 100644 yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java delete mode 100644 yaml/src/test/resources/multi-line/folded.yaml delete mode 100644 yaml/src/test/resources/multi-line/folded2.yaml delete mode 100644 yaml/src/test/resources/multi-line/folded_keep.yaml delete mode 100644 yaml/src/test/resources/multi-line/folded_strip.yaml delete mode 100644 yaml/src/test/resources/multi-line/literal.yaml delete mode 100644 yaml/src/test/resources/multi-line/literal2.yaml delete mode 100644 yaml/src/test/resources/multi-line/literal_keep.yaml delete mode 100644 yaml/src/test/resources/multi-line/literal_strip.yaml delete mode 100644 yaml/src/test/resources/multi-line/plain_double_quotes.yaml delete mode 100644 yaml/src/test/resources/multi-line/plain_single_quotes.yaml diff --git a/pom.xml b/pom.xml index b0d3f2297d..0c9896cf35 100644 --- a/pom.xml +++ b/pom.xml @@ -782,7 +782,6 @@ wildfly xml xstream - yaml @@ -1307,7 +1306,6 @@ wildfly xml xstream - yaml diff --git a/yaml/README.md b/yaml/README.md deleted file mode 100644 index ebf403eb57..0000000000 --- a/yaml/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## YAML - -This module contains articles about YAML - -### Relevant articles - - diff --git a/yaml/pom.xml b/yaml/pom.xml deleted file mode 100644 index c801505c6a..0000000000 --- a/yaml/pom.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - 4.0.0 - yaml - 1.0-SNAPSHOT - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - - org.yaml - snakeyaml - 1.21 - - - - - 1.8 - 1.21 - - - \ No newline at end of file diff --git a/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java b/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java deleted file mode 100644 index a99de63a47..0000000000 --- a/yaml/src/test/java/com/baeldung/multiline/MultiLineStringsUnitTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.baeldung.multiline; - -import org.junit.Before; -import org.junit.Test; -import org.yaml.snakeyaml.Yaml; - -import java.io.File; -import java.io.InputStream; -import java.util.Map; - -import static org.junit.Assert.assertEquals; - -public class MultiLineStringsUnitTest { - - private Yaml yaml; - - @Before - public void setup() { - yaml = new Yaml(); - } - - @Test - public void whenLiteral_ThenLineBreaksArePresent() { - String key = parseYamlKey("literal.yaml", "key"); - assertEquals("Line1\nLine2\nLine3", key); - } - - @Test - public void whenLiteral_ThenEndingBreaksAreReducedToOne() { - String key = parseYamlKey("literal2.yaml", "key"); - assertEquals("\n\nLine1\n\nLine2\n\nLine3\n", key); - } - - @Test - public void whenFolded_ThenLineBreaksAreReplaced() { - String key = parseYamlKey("folded.yaml", "key"); - assertEquals("Line1 Line2 Line3", key); - } - - @Test - public void whenFolded_ThenEmptyLinesAreReducedToOne() { - String key = parseYamlKey("folded2.yaml", "key"); - assertEquals("Line1 Line2\n\nLine3\n", key); - } - - @Test - public void whenLiteralKeep_ThenLastEmptyLinesArePresent() { - String key = parseYamlKey("literal_keep.yaml", "key"); - assertEquals("Line1\nLine2\nLine3\n\n", key); - } - - @Test - public void whenLiteralStrip_ThenLastEmptyLinesAreRemoved() { - String key = parseYamlKey("literal_strip.yaml", "key"); - assertEquals("Line1\nLine2\nLine3", key); - } - - @Test - public void whenFoldedKeep_ThenLastEmptyLinesArePresent() { - String key = parseYamlKey("folded_keep.yaml", "key"); - assertEquals("Line1 Line2 Line3\n\n\n", key); - } - - @Test - public void whenFoldedStrip_ThenLastEmptyLinesAreRemoved() { - String key = parseYamlKey("folded_strip.yaml", "key"); - assertEquals("Line1 Line2 Line3", key); - } - - @Test - public void whenDoubleQuotes_ThenExplicitBreaksArePreserved() { - String key = parseYamlKey("plain_double_quotes.yaml", "key"); - assertEquals("Line1\nLine2\nLine3", key); - } - - @Test - public void whenSingleQuotes_ThenExplicitBreaksAreIgnored() { - String key = parseYamlKey("plain_single_quotes.yaml", "key"); - assertEquals("Line1\\nLine2\nLine3", key); - } - - String parseYamlKey(String fileName, String key) { - InputStream inputStream = this.getClass() - .getClassLoader() - .getResourceAsStream("multi-line" + File.separator + fileName); - Map parsed = yaml.load(inputStream); - return parsed.get(key); - } - -} diff --git a/yaml/src/test/resources/multi-line/folded.yaml b/yaml/src/test/resources/multi-line/folded.yaml deleted file mode 100644 index c5fa743a08..0000000000 --- a/yaml/src/test/resources/multi-line/folded.yaml +++ /dev/null @@ -1,4 +0,0 @@ -key: > - Line1 - Line2 - Line3 \ No newline at end of file diff --git a/yaml/src/test/resources/multi-line/folded2.yaml b/yaml/src/test/resources/multi-line/folded2.yaml deleted file mode 100644 index 735abf9b2f..0000000000 --- a/yaml/src/test/resources/multi-line/folded2.yaml +++ /dev/null @@ -1,8 +0,0 @@ -key: > - Line1 - Line2 - - - Line3 - - diff --git a/yaml/src/test/resources/multi-line/folded_keep.yaml b/yaml/src/test/resources/multi-line/folded_keep.yaml deleted file mode 100644 index 555291fd26..0000000000 --- a/yaml/src/test/resources/multi-line/folded_keep.yaml +++ /dev/null @@ -1,6 +0,0 @@ -key: >+ - Line1 - Line2 - Line3 - - diff --git a/yaml/src/test/resources/multi-line/folded_strip.yaml b/yaml/src/test/resources/multi-line/folded_strip.yaml deleted file mode 100644 index 0a3a246dc2..0000000000 --- a/yaml/src/test/resources/multi-line/folded_strip.yaml +++ /dev/null @@ -1,7 +0,0 @@ -key: >- - Line1 - Line2 - Line3 - - - diff --git a/yaml/src/test/resources/multi-line/literal.yaml b/yaml/src/test/resources/multi-line/literal.yaml deleted file mode 100644 index 7e02501a33..0000000000 --- a/yaml/src/test/resources/multi-line/literal.yaml +++ /dev/null @@ -1,4 +0,0 @@ -key: | - Line1 - Line2 - Line3 \ No newline at end of file diff --git a/yaml/src/test/resources/multi-line/literal2.yaml b/yaml/src/test/resources/multi-line/literal2.yaml deleted file mode 100644 index 7c7fed0163..0000000000 --- a/yaml/src/test/resources/multi-line/literal2.yaml +++ /dev/null @@ -1,10 +0,0 @@ -key: | - - - Line1 - - Line2 - - Line3 - - diff --git a/yaml/src/test/resources/multi-line/literal_keep.yaml b/yaml/src/test/resources/multi-line/literal_keep.yaml deleted file mode 100644 index 37f22684dd..0000000000 --- a/yaml/src/test/resources/multi-line/literal_keep.yaml +++ /dev/null @@ -1,5 +0,0 @@ -key: |+ - Line1 - Line2 - Line3 - diff --git a/yaml/src/test/resources/multi-line/literal_strip.yaml b/yaml/src/test/resources/multi-line/literal_strip.yaml deleted file mode 100644 index 0791e13d5d..0000000000 --- a/yaml/src/test/resources/multi-line/literal_strip.yaml +++ /dev/null @@ -1,5 +0,0 @@ -key: |- - Line1 - Line2 - Line3 - diff --git a/yaml/src/test/resources/multi-line/plain_double_quotes.yaml b/yaml/src/test/resources/multi-line/plain_double_quotes.yaml deleted file mode 100644 index ccab040a27..0000000000 --- a/yaml/src/test/resources/multi-line/plain_double_quotes.yaml +++ /dev/null @@ -1 +0,0 @@ -key: "Line1\nLine2\nLine3" \ No newline at end of file diff --git a/yaml/src/test/resources/multi-line/plain_single_quotes.yaml b/yaml/src/test/resources/multi-line/plain_single_quotes.yaml deleted file mode 100644 index acbf35a462..0000000000 --- a/yaml/src/test/resources/multi-line/plain_single_quotes.yaml +++ /dev/null @@ -1,3 +0,0 @@ -key: 'Line1\nLine2 - - Line3' \ No newline at end of file From 5b59902cb254becc9521689d8a01c3d184f941b2 Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Thu, 16 Apr 2020 09:55:40 -0400 Subject: [PATCH 07/29] Removed rest of project --- aws-app-sync/pom.xml | 49 +++++++++++++++++++ .../awsappsync/AwsAppSyncApplication.java | 13 +++++ .../src/main/resources/application.properties | 1 + .../AwsAppSyncApplicationTests.java | 13 +++++ 4 files changed, 76 insertions(+) create mode 100644 aws-app-sync/pom.xml create mode 100644 aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java create mode 100644 aws-app-sync/src/main/resources/application.properties create mode 100644 aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java diff --git a/aws-app-sync/pom.xml b/aws-app-sync/pom.xml new file mode 100644 index 0000000000..af02307e66 --- /dev/null +++ b/aws-app-sync/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.6.RELEASE + + + com.baeldung + aws-app-sync + 0.0.1-SNAPSHOT + aws-app-sync + Demo project for Spring Boot using AWS App Sync + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java new file mode 100644 index 0000000000..ae012c91a5 --- /dev/null +++ b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.awsappsync; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AwsAppSyncApplication { + + public static void main(String[] args) { + SpringApplication.run(AwsAppSyncApplication.class, args); + } + +} diff --git a/aws-app-sync/src/main/resources/application.properties b/aws-app-sync/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/aws-app-sync/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java new file mode 100644 index 0000000000..7b82393865 --- /dev/null +++ b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java @@ -0,0 +1,13 @@ +package com.baeldung.awsappsync; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class AwsAppSyncApplicationTests { + + @Test + void contextLoads() { + } + +} From 6bf0d97d62bdfe986fde782544d47ebba9cb9705 Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Fri, 17 Apr 2020 19:50:36 -0400 Subject: [PATCH 08/29] Bael-3893 AWS AppSync with Spring Boot --- aws-app-sync/pom.xml | 15 +++- .../src/main/resources/application.properties | 1 - .../AwsAppSyncApplicationTests.java | 90 ++++++++++++++++++- 3 files changed, 102 insertions(+), 4 deletions(-) delete mode 100644 aws-app-sync/src/main/resources/application.properties diff --git a/aws-app-sync/pom.xml b/aws-app-sync/pom.xml index af02307e66..d30cfc96a4 100644 --- a/aws-app-sync/pom.xml +++ b/aws-app-sync/pom.xml @@ -8,17 +8,26 @@ 2.2.6.RELEASE + com.baeldung aws-app-sync 0.0.1-SNAPSHOT aws-app-sync - Demo project for Spring Boot using AWS App Sync + + Spring Boot using AWS App Sync 1.8 + + + com.amazonaws + aws-java-sdk-appsync + 1.11.762 + + org.springframework.boot spring-boot-starter-web @@ -35,6 +44,10 @@ + + org.springframework.boot + spring-boot-starter-webflux + diff --git a/aws-app-sync/src/main/resources/application.properties b/aws-app-sync/src/main/resources/application.properties deleted file mode 100644 index 8b13789179..0000000000 --- a/aws-app-sync/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java index 7b82393865..15bcc6e9c9 100644 --- a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java +++ b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java @@ -1,13 +1,99 @@ package com.baeldung.awsappsync; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; @SpringBootTest class AwsAppSyncApplicationTests { - @Test - void contextLoads() { + static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com"; + static String apiKey = "da2-es2s6oj4mzhbxk5yu26ss2ruj4"; + static String API_KEY_HEADER = "x-api-key"; + + static WebClient.RequestBodySpec requestBodySpec; + + @BeforeAll + static void setUp() { + requestBodySpec = WebClient + .builder() + .baseUrl(apiUrl) + .defaultHeader(API_KEY_HEADER, apiKey) + .build() + .method(HttpMethod.POST) + .uri("/graphql"); } + @Test + void testGraphQuery() { + + Map requestBody = new HashMap<>(); + requestBody.put("query", "query ListEvents {\n" + + " listEvents {\n" + + " items {\n" + + " id\n" + + " name\n" + + " where\n" + + " when\n" + + " description\n" + + " }\n" + + " }\n" + + "}"); + requestBody.put("variables", ""); + requestBody.put("operationName", "ListEvents"); + + WebClient.ResponseSpec response = requestBodySpec + .body(BodyInserters.fromValue(requestBody)) + .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) + .acceptCharset(StandardCharsets.UTF_8) + .retrieve(); + + String bodyString = response.bodyToMono(String.class).block(); + + assertTrue(bodyString != null && bodyString.contains("My First Event")); + } + + @Test + void testGraphAdd() { + + String queryString = "mutation add {\n" + + " createEvent(\n" + + " name:\"My added GraphQL event\"\n" + + " where:\"Day 2\"\n" + + " when:\"Saturday night\"\n" + + " description:\"Studying GraphQL\"\n" + + " ){\n" + + " id\n" + + " name\n" + + " where\n" + + " when\n" + + " description\n" + + " }\n" + + "}"; + + + Map requestBody = new HashMap<>(); + requestBody.put("query", queryString); + requestBody.put("variables", ""); + requestBody.put("operationName", "add"); + + WebClient.ResponseSpec response = requestBodySpec + .body(BodyInserters.fromValue(requestBody)) + .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) + .acceptCharset(StandardCharsets.UTF_8) + .retrieve(); + + String bodyString = response.bodyToMono(String.class).block(); + assertTrue(bodyString != null && bodyString.contains("Day 2")); + } } From 9a1e98598f8c8015dff5dc2edc86446d28f606b6 Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Fri, 17 Apr 2020 19:56:42 -0400 Subject: [PATCH 09/29] Bael-3893 AWS AppSync with Spring Boot --- .../awsappsync/AwsAppSyncApplicationTests.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java index 15bcc6e9c9..005d533cbf 100644 --- a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java +++ b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java @@ -35,7 +35,7 @@ class AwsAppSyncApplicationTests { } @Test - void testGraphQuery() { + void givenGraphQuery_whenListEvents_thenReturnAllEvents() { Map requestBody = new HashMap<>(); requestBody.put("query", "query ListEvents {\n" + @@ -60,11 +60,14 @@ class AwsAppSyncApplicationTests { String bodyString = response.bodyToMono(String.class).block(); - assertTrue(bodyString != null && bodyString.contains("My First Event")); + assertNotNull(bodyString); + assertTrue(bodyString.contains("My First Event")); + assertTrue(bodyString.contains("where")); + assertTrue(bodyString.contains("when")); } @Test - void testGraphAdd() { + void givenGraphAdd_whenMutation_thenReturnIdNameDesc() { String queryString = "mutation add {\n" + " createEvent(\n" + @@ -75,8 +78,6 @@ class AwsAppSyncApplicationTests { " ){\n" + " id\n" + " name\n" + - " where\n" + - " when\n" + " description\n" + " }\n" + "}"; @@ -94,6 +95,10 @@ class AwsAppSyncApplicationTests { .retrieve(); String bodyString = response.bodyToMono(String.class).block(); - assertTrue(bodyString != null && bodyString.contains("Day 2")); + + assertNotNull(bodyString); + assertTrue(bodyString.contains("My added GraphQL event")); + assertFalse(bodyString.contains("where")); + assertFalse(bodyString.contains("when")); } } From 3746a5328554b56ff614d5abf328f104e4b6c727 Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Fri, 17 Apr 2020 20:16:59 -0400 Subject: [PATCH 10/29] Bael 3893 - Added to modules --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 71e5d21b02..22d73f3a9e 100644 --- a/pom.xml +++ b/pom.xml @@ -1015,6 +1015,7 @@ asm atomix aws + aws-app-sync aws-lambda axon azure From 8b6c667ce058c09870c0784627fdf831a2dfe912 Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Wed, 22 Apr 2020 21:10:52 -0400 Subject: [PATCH 11/29] Bael-3893 AWS AppSync with Spring Boot --- aws-app-sync/pom.xml | 6 -- .../awsappsync/AppSyncClientHelper.java | 32 ++++++++++ .../AwsAppSyncApplicationTests.java | 63 ++----------------- 3 files changed, 38 insertions(+), 63 deletions(-) create mode 100644 aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java diff --git a/aws-app-sync/pom.xml b/aws-app-sync/pom.xml index d30cfc96a4..ef2085ff35 100644 --- a/aws-app-sync/pom.xml +++ b/aws-app-sync/pom.xml @@ -22,12 +22,6 @@ - - com.amazonaws - aws-java-sdk-appsync - 1.11.762 - - org.springframework.boot spring-boot-starter-web diff --git a/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java new file mode 100644 index 0000000000..fcbc0aa275 --- /dev/null +++ b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java @@ -0,0 +1,32 @@ +package com.baeldung.awsappsync; + +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; + +import java.nio.charset.StandardCharsets; +import java.util.Map; + +public class AppSyncClientHelper { + + static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com"; + static String apiKey = "da2-es2s6oj4mzhbxk5yu26ss2ruj4"; + static String API_KEY_HEADER = "x-api-key"; + + public static WebClient.ResponseSpec getResponseBodySpec(Map requestBody) { + return WebClient + .builder() + .baseUrl(apiUrl) + .defaultHeader(API_KEY_HEADER, apiKey) + .defaultHeader("Content-Type", "application/json") + .build() + .method(HttpMethod.POST) + .uri("/graphql") + .body(BodyInserters.fromValue(requestBody)) + .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) + .acceptCharset(StandardCharsets.UTF_8) + .retrieve(); + } + +} diff --git a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java index 005d533cbf..cd14bcd5b2 100644 --- a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java +++ b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java @@ -1,14 +1,9 @@ package com.baeldung.awsappsync; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.HttpMethod; -import org.springframework.http.MediaType; -import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; -import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -17,48 +12,16 @@ import static org.junit.jupiter.api.Assertions.*; @SpringBootTest class AwsAppSyncApplicationTests { - static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com"; - static String apiKey = "da2-es2s6oj4mzhbxk5yu26ss2ruj4"; - static String API_KEY_HEADER = "x-api-key"; - - static WebClient.RequestBodySpec requestBodySpec; - - @BeforeAll - static void setUp() { - requestBodySpec = WebClient - .builder() - .baseUrl(apiUrl) - .defaultHeader(API_KEY_HEADER, apiKey) - .build() - .method(HttpMethod.POST) - .uri("/graphql"); - } - @Test void givenGraphQuery_whenListEvents_thenReturnAllEvents() { Map requestBody = new HashMap<>(); - requestBody.put("query", "query ListEvents {\n" + - " listEvents {\n" + - " items {\n" + - " id\n" + - " name\n" + - " where\n" + - " when\n" + - " description\n" + - " }\n" + - " }\n" + - "}"); + requestBody.put("query", "query ListEvents { listEvents { items { id name where when description } } }"); requestBody.put("variables", ""); requestBody.put("operationName", "ListEvents"); - WebClient.ResponseSpec response = requestBodySpec - .body(BodyInserters.fromValue(requestBody)) - .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) - .acceptCharset(StandardCharsets.UTF_8) - .retrieve(); - - String bodyString = response.bodyToMono(String.class).block(); + String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody) + .bodyToMono(String.class).block(); assertNotNull(bodyString); assertTrue(bodyString.contains("My First Event")); @@ -69,18 +32,8 @@ class AwsAppSyncApplicationTests { @Test void givenGraphAdd_whenMutation_thenReturnIdNameDesc() { - String queryString = "mutation add {\n" + - " createEvent(\n" + - " name:\"My added GraphQL event\"\n" + - " where:\"Day 2\"\n" + - " when:\"Saturday night\"\n" + - " description:\"Studying GraphQL\"\n" + - " ){\n" + - " id\n" + - " name\n" + - " description\n" + - " }\n" + - "}"; + String queryString = "mutation add { createEvent( name:\"My added GraphQL event\" where:\"Day 2\"" + + " when:\"Saturday night\" description:\"Studying GraphQL\" ){ id name description } }"; Map requestBody = new HashMap<>(); @@ -88,11 +41,7 @@ class AwsAppSyncApplicationTests { requestBody.put("variables", ""); requestBody.put("operationName", "add"); - WebClient.ResponseSpec response = requestBodySpec - .body(BodyInserters.fromValue(requestBody)) - .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) - .acceptCharset(StandardCharsets.UTF_8) - .retrieve(); + WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody); String bodyString = response.bodyToMono(String.class).block(); From c10208b4d926ad4ee6795082cecba8c3228415bc Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Fri, 1 May 2020 19:48:38 -0400 Subject: [PATCH 12/29] Bael 3893 - Code review updates --- .../awsappsync/AppSyncClientHelper.java | 2 +- .../awsappsync/AwsAppSyncApplication.java | 6 +- .../AwsAppSyncApplicationTests.java | 77 ++++++++++++------- pom.xml | 2 +- 4 files changed, 53 insertions(+), 34 deletions(-) diff --git a/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java index fcbc0aa275..b310e60748 100644 --- a/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java +++ b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AppSyncClientHelper.java @@ -11,7 +11,7 @@ import java.util.Map; public class AppSyncClientHelper { static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com"; - static String apiKey = "da2-es2s6oj4mzhbxk5yu26ss2ruj4"; + static String apiKey = "da2-bm4rpatkkrc5jfyhvvq7itjeke"; static String API_KEY_HEADER = "x-api-key"; public static WebClient.ResponseSpec getResponseBodySpec(Map requestBody) { diff --git a/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java index ae012c91a5..19fffd2994 100644 --- a/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java +++ b/aws-app-sync/src/main/java/com/baeldung/awsappsync/AwsAppSyncApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AwsAppSyncApplication { - public static void main(String[] args) { - SpringApplication.run(AwsAppSyncApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(AwsAppSyncApplication.class, args); + } } diff --git a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java index cd14bcd5b2..6e94651789 100644 --- a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java +++ b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java @@ -12,42 +12,61 @@ import static org.junit.jupiter.api.Assertions.*; @SpringBootTest class AwsAppSyncApplicationTests { - @Test - void givenGraphQuery_whenListEvents_thenReturnAllEvents() { + @Test + void givenGraphQuery_whenListEvents_thenReturnAllEvents() { - Map requestBody = new HashMap<>(); - requestBody.put("query", "query ListEvents { listEvents { items { id name where when description } } }"); - requestBody.put("variables", ""); - requestBody.put("operationName", "ListEvents"); + Map requestBody = new HashMap<>(); + requestBody.put("query", "query ListEvents {" + + " listEvents {" + + " items {" + + " id" + + " name" + + " where" + + " when" + + " description" + + " }" + + " }" + + "}"); + requestBody.put("variables", ""); + requestBody.put("operationName", "ListEvents"); - String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody) - .bodyToMono(String.class).block(); + String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody) + .bodyToMono(String.class).block(); - assertNotNull(bodyString); - assertTrue(bodyString.contains("My First Event")); - assertTrue(bodyString.contains("where")); - assertTrue(bodyString.contains("when")); - } + assertNotNull(bodyString); + assertTrue(bodyString.contains("My First Event")); + assertTrue(bodyString.contains("where")); + assertTrue(bodyString.contains("when")); + } - @Test - void givenGraphAdd_whenMutation_thenReturnIdNameDesc() { + @Test + void givenGraphAdd_whenMutation_thenReturnIdNameDesc() { - String queryString = "mutation add { createEvent( name:\"My added GraphQL event\" where:\"Day 2\"" + - " when:\"Saturday night\" description:\"Studying GraphQL\" ){ id name description } }"; + String queryString = "mutation add {" + + " createEvent(" + + " name:\"My added GraphQL event\"" + + " where:\"Day 2\"" + + " when:\"Saturday night\"" + + " description:\"Studying GraphQL\"" + + " ){" + + " id" + + " name" + + " description" + + " }" + + "}"; + Map requestBody = new HashMap<>(); + requestBody.put("query", queryString); + requestBody.put("variables", ""); + requestBody.put("operationName", "add"); - Map requestBody = new HashMap<>(); - requestBody.put("query", queryString); - requestBody.put("variables", ""); - requestBody.put("operationName", "add"); + WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody); - WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody); + String bodyString = response.bodyToMono(String.class).block(); - String bodyString = response.bodyToMono(String.class).block(); - - assertNotNull(bodyString); - assertTrue(bodyString.contains("My added GraphQL event")); - assertFalse(bodyString.contains("where")); - assertFalse(bodyString.contains("when")); - } + assertNotNull(bodyString); + assertTrue(bodyString.contains("My added GraphQL event")); + assertFalse(bodyString.contains("where")); + assertFalse(bodyString.contains("when")); + } } diff --git a/pom.xml b/pom.xml index 22d73f3a9e..8f2cc8e902 100644 --- a/pom.xml +++ b/pom.xml @@ -1015,7 +1015,7 @@ asm atomix aws - aws-app-sync + aws-app-sync aws-lambda axon azure From 05a4f60e38babd42b62b825157247195bd5778da Mon Sep 17 00:00:00 2001 From: Cristian Rosu Date: Sun, 3 May 2020 17:28:27 +0300 Subject: [PATCH 13/29] BAEL-3826: creating PDFs using Thymeleaf as template engine --- pdf/pom.xml | 10 ++++ .../com/baeldung/pdf/PDFThymeleafExample.java | 48 +++++++++++++++++++ .../main/resources/thymeleaf_template.html | 7 +++ 3 files changed, 65 insertions(+) create mode 100644 pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java create mode 100644 pdf/src/main/resources/thymeleaf_template.html diff --git a/pdf/pom.xml b/pdf/pom.xml index d148aa1670..463c88948d 100644 --- a/pdf/pom.xml +++ b/pdf/pom.xml @@ -60,6 +60,16 @@ poi-ooxml ${poi-ooxml.version} + + org.thymeleaf + thymeleaf + 3.0.11.RELEASE + + + org.xhtmlrenderer + flying-saucer-pdf + 9.1.20 + diff --git a/pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java b/pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java new file mode 100644 index 0000000000..2e1df1d320 --- /dev/null +++ b/pdf/src/main/java/com/baeldung/pdf/PDFThymeleafExample.java @@ -0,0 +1,48 @@ +package com.baeldung.pdf; + +import com.lowagie.text.DocumentException; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.xhtmlrenderer.pdf.ITextRenderer; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class PDFThymeleafExample { + + public static void main(String[] args) throws IOException, DocumentException { + PDFThymeleafExample thymeleaf2Pdf = new PDFThymeleafExample(); + String html = thymeleaf2Pdf.parseThymeleafTemplate(); + thymeleaf2Pdf.generatePdfFromHtml(html); + } + + public void generatePdfFromHtml(String html) throws IOException, DocumentException { + String outputFolder = System.getProperty("user.home") + File.separator + "thymeleaf.pdf"; + OutputStream outputStream = new FileOutputStream(outputFolder); + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocumentFromString(html); + renderer.layout(); + renderer.createPDF(outputStream); + + outputStream.close(); + } + + private String parseThymeleafTemplate() { + ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode(TemplateMode.HTML); + + TemplateEngine templateEngine = new TemplateEngine(); + templateEngine.setTemplateResolver(templateResolver); + + Context context = new Context(); + context.setVariable("to", "Baeldung.com"); + + return templateEngine.process("thymeleaf_template", context); + } +} diff --git a/pdf/src/main/resources/thymeleaf_template.html b/pdf/src/main/resources/thymeleaf_template.html new file mode 100644 index 0000000000..3e856367cc --- /dev/null +++ b/pdf/src/main/resources/thymeleaf_template.html @@ -0,0 +1,7 @@ + + +

+ +

+ + \ No newline at end of file From f8cae12a3830b303442bcd808439a9f057027def Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Sun, 3 May 2020 13:53:19 -0400 Subject: [PATCH 14/29] Bael-3893 - Updated indents with 4 spaces --- aws-app-sync/pom.xml | 90 ++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/aws-app-sync/pom.xml b/aws-app-sync/pom.xml index ef2085ff35..1f915978ab 100644 --- a/aws-app-sync/pom.xml +++ b/aws-app-sync/pom.xml @@ -1,56 +1,56 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.2.6.RELEASE - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.6.RELEASE + + - com.baeldung - aws-app-sync - 0.0.1-SNAPSHOT - aws-app-sync + com.baeldung + aws-app-sync + 0.0.1-SNAPSHOT + aws-app-sync - Spring Boot using AWS App Sync + Spring Boot using AWS App Sync - - 1.8 - + + 1.8 + - + - - org.springframework.boot - spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-starter-test - test - - - org.junit.vintage - junit-vintage-engine - - - - - org.springframework.boot - spring-boot-starter-webflux - - + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.springframework.boot + spring-boot-starter-webflux + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + From 7e8d36d0840862273aca040356f5c04846b6b20d Mon Sep 17 00:00:00 2001 From: Cristian Rosu Date: Fri, 8 May 2020 20:17:17 +0300 Subject: [PATCH 15/29] BAEL-3826: test Thymeleaf PDF generation using ByteArrayOutputStream --- .../baeldung/pdf/PDFThymeleafUnitTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java diff --git a/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java b/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java new file mode 100644 index 0000000000..7d927072aa --- /dev/null +++ b/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.pdf; + +import com.lowagie.text.DocumentException; +import org.junit.Test; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.xhtmlrenderer.pdf.ITextRenderer; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import static org.junit.Assert.assertTrue; + +public class PDFThymeleafUnitTest { + + @Test + public void givenThymeleafTemplate_whenParsedAndRenderedToPDF_thenItShouldNotBeEmpty() throws DocumentException, IOException { + String html = parseThymeleafTemplate(); + ByteArrayOutputStream outputStream = generatePdfOutputStreamFromHtml(html); + assertTrue(outputStream.size() > 0); + } + + private ByteArrayOutputStream generatePdfOutputStreamFromHtml(String html) throws IOException, DocumentException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + ITextRenderer renderer = new ITextRenderer(); + renderer.setDocumentFromString(html); + renderer.layout(); + renderer.createPDF(outputStream); + + outputStream.close(); + return outputStream; + } + + private String parseThymeleafTemplate() { + ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); + templateResolver.setSuffix(".html"); + templateResolver.setTemplateMode(TemplateMode.HTML); + + TemplateEngine templateEngine = new TemplateEngine(); + templateEngine.setTemplateResolver(templateResolver); + + Context context = new Context(); + context.setVariable("to", "Baeldung.com"); + + return templateEngine.process("thymeleaf_template", context); + } +} From f83c73a830814faee3411215dbaf5f82b8fcf278 Mon Sep 17 00:00:00 2001 From: Cristian Rosu Date: Fri, 8 May 2020 20:18:22 +0300 Subject: [PATCH 16/29] BAEL-3826: appropriate spacing --- pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java b/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java index 7d927072aa..e253dce06c 100644 --- a/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java +++ b/pdf/src/test/java/com/baeldung/pdf/PDFThymeleafUnitTest.java @@ -18,7 +18,9 @@ public class PDFThymeleafUnitTest { @Test public void givenThymeleafTemplate_whenParsedAndRenderedToPDF_thenItShouldNotBeEmpty() throws DocumentException, IOException { String html = parseThymeleafTemplate(); + ByteArrayOutputStream outputStream = generatePdfOutputStreamFromHtml(html); + assertTrue(outputStream.size() > 0); } From 9194d94eeb45c347a4a559de40509fdd147c931b Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Mon, 11 May 2020 08:28:13 -0400 Subject: [PATCH 17/29] Bael-3893 - Fixed string concatenation operator --- .../AwsAppSyncApplicationTests.java | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java index 6e94651789..22d99959b3 100644 --- a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java +++ b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java @@ -16,17 +16,17 @@ class AwsAppSyncApplicationTests { void givenGraphQuery_whenListEvents_thenReturnAllEvents() { Map requestBody = new HashMap<>(); - requestBody.put("query", "query ListEvents {" + - " listEvents {" + - " items {" + - " id" + - " name" + - " where" + - " when" + - " description" + - " }" + - " }" + - "}"); + requestBody.put("query", "query ListEvents {" + + " listEvents {" + + " items {" + + " id" + + " name" + + " where" + + " when" + + " description" + + " }" + + " }" + + "}"); requestBody.put("variables", ""); requestBody.put("operationName", "ListEvents"); @@ -42,18 +42,18 @@ class AwsAppSyncApplicationTests { @Test void givenGraphAdd_whenMutation_thenReturnIdNameDesc() { - String queryString = "mutation add {" + - " createEvent(" + - " name:\"My added GraphQL event\"" + - " where:\"Day 2\"" + - " when:\"Saturday night\"" + - " description:\"Studying GraphQL\"" + - " ){" + - " id" + - " name" + - " description" + - " }" + - "}"; + String queryString = "mutation add {" + + " createEvent(" + + " name:\"My added GraphQL event\"" + + " where:\"Day 2\"" + + " when:\"Saturday night\"" + + " description:\"Studying GraphQL\"" + + " ){" + + " id" + + " name" + + " description" + + " }" + + "}"; Map requestBody = new HashMap<>(); requestBody.put("query", queryString); From 20843c7f224fe68e772b0518532c881fff483483 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 01:56:25 +0200 Subject: [PATCH 18/29] BAEL-3863 Introduction to Finagle --- finagle/README.md | 7 +++++ finagle/pom.xml | 30 +++++++++++++++++++ .../java/com/baeldung/finagle/Client.java | 24 +++++++++++++++ .../com/baeldung/finagle/GreetingService.java | 18 +++++++++++ .../java/com/baeldung/finagle/LogFilter.java | 17 +++++++++++ .../java/com/baeldung/finagle/Server.java | 15 ++++++++++ finagle/src/main/resources/logback.xml | 13 ++++++++ .../src/main/resources/twitter4j.properties | 4 +++ 8 files changed, 128 insertions(+) create mode 100644 finagle/README.md create mode 100644 finagle/pom.xml create mode 100644 finagle/src/main/java/com/baeldung/finagle/Client.java create mode 100644 finagle/src/main/java/com/baeldung/finagle/GreetingService.java create mode 100644 finagle/src/main/java/com/baeldung/finagle/LogFilter.java create mode 100644 finagle/src/main/java/com/baeldung/finagle/Server.java create mode 100644 finagle/src/main/resources/logback.xml create mode 100644 finagle/src/main/resources/twitter4j.properties diff --git a/finagle/README.md b/finagle/README.md new file mode 100644 index 0000000000..d16afdc10d --- /dev/null +++ b/finagle/README.md @@ -0,0 +1,7 @@ +## Finagle + +This module contains articles about Finagle and associated libraries. + +### Relevant articles + +- [Introduction to Finagle](https://www.baeldung.com/introduction-to-finagle) diff --git a/finagle/pom.xml b/finagle/pom.xml new file mode 100644 index 0000000000..f1dc932c13 --- /dev/null +++ b/finagle/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + Finagle + Finagle + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + com.twitter + finagle-core_2.13 + 20.4.0 + + + com.twitter + finagle-http_2.13 + 20.4.0 + + + + + + diff --git a/finagle/src/main/java/com/baeldung/finagle/Client.java b/finagle/src/main/java/com/baeldung/finagle/Client.java new file mode 100644 index 0000000000..a881f1c37c --- /dev/null +++ b/finagle/src/main/java/com/baeldung/finagle/Client.java @@ -0,0 +1,24 @@ +package com.baeldung.finagle; + +import com.twitter.finagle.Http; +import com.twitter.finagle.Service; +import com.twitter.finagle.http.Method; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.util.Await; +import com.twitter.util.Future; +import scala.runtime.BoxedUnit; + +public class Client { + public static void main(String[] args) throws Exception { + Service service = new LogFilter().andThen(Http.newService(":8080")); + Request request = Request.apply(Method.Get(), "/?name=John"); + request.host("localhost"); + Future response = service.apply(request); + Await.result(response + .onSuccess(r -> { + System.out.println(r.getContentString()); + return BoxedUnit.UNIT; + })); + } +} diff --git a/finagle/src/main/java/com/baeldung/finagle/GreetingService.java b/finagle/src/main/java/com/baeldung/finagle/GreetingService.java new file mode 100644 index 0000000000..c795724192 --- /dev/null +++ b/finagle/src/main/java/com/baeldung/finagle/GreetingService.java @@ -0,0 +1,18 @@ +package com.baeldung.finagle; + +import com.twitter.finagle.Service; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.finagle.http.Status; +import com.twitter.io.Buf; +import com.twitter.io.Reader; +import com.twitter.util.Future; + +public class GreetingService extends Service { + @Override + public Future apply(Request request) { + String greeting = "Hello " + request.getParam("name"); + Reader reader = Reader.fromBuf(new Buf.ByteArray(greeting.getBytes(), 0, greeting.length())); + return Future.value(Response.apply(request.version(), Status.Ok(), reader)); + } +} diff --git a/finagle/src/main/java/com/baeldung/finagle/LogFilter.java b/finagle/src/main/java/com/baeldung/finagle/LogFilter.java new file mode 100644 index 0000000000..d381456cfe --- /dev/null +++ b/finagle/src/main/java/com/baeldung/finagle/LogFilter.java @@ -0,0 +1,17 @@ +package com.baeldung.finagle; + +import com.twitter.finagle.Service; +import com.twitter.finagle.SimpleFilter; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.util.Future; + +public class LogFilter extends SimpleFilter { + @Override + public Future apply(Request request, Service service) { + System.out.println("Request host:" + request.host().getOrElse(() -> "")); + System.out.println("Request params:"); + request.getParams().forEach(entry -> System.out.println("\t" + entry.getKey() + " : " + entry.getValue())); + return service.apply(request); + } +} diff --git a/finagle/src/main/java/com/baeldung/finagle/Server.java b/finagle/src/main/java/com/baeldung/finagle/Server.java new file mode 100644 index 0000000000..8a9c9be192 --- /dev/null +++ b/finagle/src/main/java/com/baeldung/finagle/Server.java @@ -0,0 +1,15 @@ +package com.baeldung.finagle; + +import com.twitter.finagle.Http; +import com.twitter.finagle.ListeningServer; +import com.twitter.finagle.Service; +import com.twitter.util.Await; +import com.twitter.util.TimeoutException; + +public class Server { + public static void main(String[] args) throws TimeoutException, InterruptedException { + Service service = new LogFilter().andThen(new GreetingService()); + ListeningServer server = Http.serve(":8080", service); + Await.ready(server); + } +} diff --git a/finagle/src/main/resources/logback.xml b/finagle/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/finagle/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/finagle/src/main/resources/twitter4j.properties b/finagle/src/main/resources/twitter4j.properties new file mode 100644 index 0000000000..ee11dc62a1 --- /dev/null +++ b/finagle/src/main/resources/twitter4j.properties @@ -0,0 +1,4 @@ +oauth.consumerKey=//TODO +oauth.consumerSecret=//TODO +oauth.accessToken=//TODO +oauth.accessTokenSecret=//TODO From 33edef47e18c16e03ce056af609dc984d6f4a566 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 02:05:49 +0200 Subject: [PATCH 19/29] BAEL-3863 add Finagle module to root pom.xml --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 34703c6f73..813a42c93d 100644 --- a/pom.xml +++ b/pom.xml @@ -418,6 +418,7 @@ ethereum feign + finagle flyway-cdi-extension geotools From ff684f13b6d4d27f063d0e912ca9e6b0151acb0e Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 20:52:23 +0200 Subject: [PATCH 20/29] BAEL-3863 moved Finagle package to libraries-rpc, added test --- {finagle => libraries-rpc}/README.md | 0 {finagle => libraries-rpc}/pom.xml | 4 +- .../com/baeldung/rpc}/finagle/Client.java | 2 +- .../rpc}/finagle/GreetingService.java | 2 +- .../com/baeldung/rpc}/finagle/LogFilter.java | 2 +- .../com/baeldung/rpc}/finagle/Server.java | 2 +- .../src/main/resources/logback.xml | 0 .../src/main/resources/twitter4j.properties | 0 .../rpc/finagle/ClientServerTest.java | 40 +++++++++++++++++++ pom.xml | 2 +- 10 files changed, 47 insertions(+), 7 deletions(-) rename {finagle => libraries-rpc}/README.md (100%) rename {finagle => libraries-rpc}/pom.xml (92%) rename {finagle/src/main/java/com/baeldung => libraries-rpc/src/main/java/com/baeldung/rpc}/finagle/Client.java (96%) rename {finagle/src/main/java/com/baeldung => libraries-rpc/src/main/java/com/baeldung/rpc}/finagle/GreetingService.java (95%) rename {finagle/src/main/java/com/baeldung => libraries-rpc/src/main/java/com/baeldung/rpc}/finagle/LogFilter.java (95%) rename {finagle/src/main/java/com/baeldung => libraries-rpc/src/main/java/com/baeldung/rpc}/finagle/Server.java (93%) rename {finagle => libraries-rpc}/src/main/resources/logback.xml (100%) rename {finagle => libraries-rpc}/src/main/resources/twitter4j.properties (100%) create mode 100644 libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java diff --git a/finagle/README.md b/libraries-rpc/README.md similarity index 100% rename from finagle/README.md rename to libraries-rpc/README.md diff --git a/finagle/pom.xml b/libraries-rpc/pom.xml similarity index 92% rename from finagle/pom.xml rename to libraries-rpc/pom.xml index f1dc932c13..145693cac1 100644 --- a/finagle/pom.xml +++ b/libraries-rpc/pom.xml @@ -2,8 +2,8 @@ 4.0.0 - Finagle - Finagle + libraries-rpc + libraries-rpc jar diff --git a/finagle/src/main/java/com/baeldung/finagle/Client.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java similarity index 96% rename from finagle/src/main/java/com/baeldung/finagle/Client.java rename to libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java index a881f1c37c..989c79f302 100644 --- a/finagle/src/main/java/com/baeldung/finagle/Client.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java @@ -1,4 +1,4 @@ -package com.baeldung.finagle; +package com.baeldung.rpc.finagle; import com.twitter.finagle.Http; import com.twitter.finagle.Service; diff --git a/finagle/src/main/java/com/baeldung/finagle/GreetingService.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java similarity index 95% rename from finagle/src/main/java/com/baeldung/finagle/GreetingService.java rename to libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java index c795724192..7f3d741f94 100644 --- a/finagle/src/main/java/com/baeldung/finagle/GreetingService.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/GreetingService.java @@ -1,4 +1,4 @@ -package com.baeldung.finagle; +package com.baeldung.rpc.finagle; import com.twitter.finagle.Service; import com.twitter.finagle.http.Request; diff --git a/finagle/src/main/java/com/baeldung/finagle/LogFilter.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java similarity index 95% rename from finagle/src/main/java/com/baeldung/finagle/LogFilter.java rename to libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java index d381456cfe..3ef57ed18f 100644 --- a/finagle/src/main/java/com/baeldung/finagle/LogFilter.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java @@ -1,4 +1,4 @@ -package com.baeldung.finagle; +package com.baeldung.rpc.finagle; import com.twitter.finagle.Service; import com.twitter.finagle.SimpleFilter; diff --git a/finagle/src/main/java/com/baeldung/finagle/Server.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java similarity index 93% rename from finagle/src/main/java/com/baeldung/finagle/Server.java rename to libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java index 8a9c9be192..5bc8d93eaf 100644 --- a/finagle/src/main/java/com/baeldung/finagle/Server.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java @@ -1,4 +1,4 @@ -package com.baeldung.finagle; +package com.baeldung.rpc.finagle; import com.twitter.finagle.Http; import com.twitter.finagle.ListeningServer; diff --git a/finagle/src/main/resources/logback.xml b/libraries-rpc/src/main/resources/logback.xml similarity index 100% rename from finagle/src/main/resources/logback.xml rename to libraries-rpc/src/main/resources/logback.xml diff --git a/finagle/src/main/resources/twitter4j.properties b/libraries-rpc/src/main/resources/twitter4j.properties similarity index 100% rename from finagle/src/main/resources/twitter4j.properties rename to libraries-rpc/src/main/resources/twitter4j.properties diff --git a/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java new file mode 100644 index 0000000000..b3faa1968c --- /dev/null +++ b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java @@ -0,0 +1,40 @@ +package com.baeldung.rpc.finagle; + +import com.twitter.finagle.Http; +import com.twitter.finagle.Service; +import com.twitter.finagle.http.Method; +import com.twitter.finagle.http.Request; +import com.twitter.finagle.http.Response; +import com.twitter.util.Await; +import com.twitter.util.Future; +import org.junit.Test; +import scala.runtime.BoxedUnit; + +import static org.junit.Assert.assertEquals; + +public class ClientServerTest { + @Test + public void clientShouldReceiveResponseFromServer() throws Exception { + // given + Service serverService = new LogFilter().andThen(new GreetingService()); + Http.serve(":8080", serverService); + + Service clientService = new LogFilter().andThen(Http.newService(":8080")); + + // when + Request request = Request.apply(Method.Get(), "/?name=John"); + request.host("localhost"); + Future response = clientService.apply(request); + + // then + Await.result(response + .onSuccess(r -> { + assertEquals("Hello John", r.getContentString()); + return BoxedUnit.UNIT; + }) + .onFailure(r -> { + throw new RuntimeException(r); + }) + ); + } +} diff --git a/pom.xml b/pom.xml index 813a42c93d..c16775bfc9 100644 --- a/pom.xml +++ b/pom.xml @@ -418,7 +418,6 @@ ethereum feign - finagle flyway-cdi-extension geotools @@ -509,6 +508,7 @@ libraries-http-2 libraries-io libraries-primitive + libraries-rpc libraries-security libraries-server libraries-testing From 3e0c3a0d0a621a3dcf909314fffb5a25b3a17a93 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 20:54:23 +0200 Subject: [PATCH 21/29] BAEL-3863 removed README.md --- libraries-rpc/README.md | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 libraries-rpc/README.md diff --git a/libraries-rpc/README.md b/libraries-rpc/README.md deleted file mode 100644 index d16afdc10d..0000000000 --- a/libraries-rpc/README.md +++ /dev/null @@ -1,7 +0,0 @@ -## Finagle - -This module contains articles about Finagle and associated libraries. - -### Relevant articles - -- [Introduction to Finagle](https://www.baeldung.com/introduction-to-finagle) From ca7d40de8fdca6741021f8273613a8ee53f7c907 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Fri, 24 Apr 2020 20:59:20 +0200 Subject: [PATCH 22/29] BAEL-3863 removed redundant examples --- .../java/com/baeldung/rpc/finagle/Client.java | 24 ------------------- .../java/com/baeldung/rpc/finagle/Server.java | 15 ------------ 2 files changed, 39 deletions(-) delete mode 100644 libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java delete mode 100644 libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java diff --git a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java deleted file mode 100644 index 989c79f302..0000000000 --- a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Client.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.rpc.finagle; - -import com.twitter.finagle.Http; -import com.twitter.finagle.Service; -import com.twitter.finagle.http.Method; -import com.twitter.finagle.http.Request; -import com.twitter.finagle.http.Response; -import com.twitter.util.Await; -import com.twitter.util.Future; -import scala.runtime.BoxedUnit; - -public class Client { - public static void main(String[] args) throws Exception { - Service service = new LogFilter().andThen(Http.newService(":8080")); - Request request = Request.apply(Method.Get(), "/?name=John"); - request.host("localhost"); - Future response = service.apply(request); - Await.result(response - .onSuccess(r -> { - System.out.println(r.getContentString()); - return BoxedUnit.UNIT; - })); - } -} diff --git a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java deleted file mode 100644 index 5bc8d93eaf..0000000000 --- a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/Server.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.rpc.finagle; - -import com.twitter.finagle.Http; -import com.twitter.finagle.ListeningServer; -import com.twitter.finagle.Service; -import com.twitter.util.Await; -import com.twitter.util.TimeoutException; - -public class Server { - public static void main(String[] args) throws TimeoutException, InterruptedException { - Service service = new LogFilter().andThen(new GreetingService()); - ListeningServer server = Http.serve(":8080", service); - Await.ready(server); - } -} From cf6cbe92b9e229c28a354d683d6a3cb6a2d8eb3a Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sat, 25 Apr 2020 18:07:27 +0200 Subject: [PATCH 23/29] BAEL-3863 Renamed tests to follow convention --- .../{ClientServerTest.java => FinagleIntegrationTest.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename libraries-rpc/src/test/java/com/baeldung/rpc/finagle/{ClientServerTest.java => FinagleIntegrationTest.java} (88%) diff --git a/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java similarity index 88% rename from libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java rename to libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java index b3faa1968c..8dcdb19e7e 100644 --- a/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/ClientServerTest.java +++ b/libraries-rpc/src/test/java/com/baeldung/rpc/finagle/FinagleIntegrationTest.java @@ -12,9 +12,9 @@ import scala.runtime.BoxedUnit; import static org.junit.Assert.assertEquals; -public class ClientServerTest { +public class FinagleIntegrationTest { @Test - public void clientShouldReceiveResponseFromServer() throws Exception { + public void givenServerAndClient_whenRequestSent_thenClientShouldReceiveResponseFromServer() throws Exception { // given Service serverService = new LogFilter().andThen(new GreetingService()); Http.serve(":8080", serverService); From 6deafc178d25d2b6247074bf02249349591dba4c Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sun, 26 Apr 2020 16:15:17 +0200 Subject: [PATCH 24/29] BAEL-3863 replace stdouts with logger --- .../main/java/com/baeldung/rpc/finagle/LogFilter.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java index 3ef57ed18f..7fc5440016 100644 --- a/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java +++ b/libraries-rpc/src/main/java/com/baeldung/rpc/finagle/LogFilter.java @@ -5,13 +5,18 @@ import com.twitter.finagle.SimpleFilter; import com.twitter.finagle.http.Request; import com.twitter.finagle.http.Response; import com.twitter.util.Future; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class LogFilter extends SimpleFilter { + + private static final Logger logger = LoggerFactory.getLogger(LogFilter.class); + @Override public Future apply(Request request, Service service) { - System.out.println("Request host:" + request.host().getOrElse(() -> "")); - System.out.println("Request params:"); - request.getParams().forEach(entry -> System.out.println("\t" + entry.getKey() + " : " + entry.getValue())); + logger.info("Request host:" + request.host().getOrElse(() -> "")); + logger.info("Request params:"); + request.getParams().forEach(entry -> logger.info("\t" + entry.getKey() + " : " + entry.getValue())); return service.apply(request); } } From 7da38b99cb4d8f4d02ef4eac8efc4143433671d6 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sun, 26 Apr 2020 16:25:51 +0200 Subject: [PATCH 25/29] BAEL-3863 add versions properties --- libraries-rpc/pom.xml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libraries-rpc/pom.xml b/libraries-rpc/pom.xml index 145693cac1..8741a41062 100644 --- a/libraries-rpc/pom.xml +++ b/libraries-rpc/pom.xml @@ -16,15 +16,20 @@ com.twitter finagle-core_2.13 - 20.4.0 + ${finagle.core.version} com.twitter finagle-http_2.13 - 20.4.0 + ${finagle.http.version} + + 20.4.0 + 20.4.0 + + From 6709017cc02040e210265e91a9792fdc405d6aef Mon Sep 17 00:00:00 2001 From: mdabrowski-eu Date: Sat, 9 May 2020 18:01:17 +0200 Subject: [PATCH 26/29] BAEL-3863 removed redundant twitter4j.properties file --- libraries-rpc/src/main/resources/twitter4j.properties | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 libraries-rpc/src/main/resources/twitter4j.properties diff --git a/libraries-rpc/src/main/resources/twitter4j.properties b/libraries-rpc/src/main/resources/twitter4j.properties deleted file mode 100644 index ee11dc62a1..0000000000 --- a/libraries-rpc/src/main/resources/twitter4j.properties +++ /dev/null @@ -1,4 +0,0 @@ -oauth.consumerKey=//TODO -oauth.consumerSecret=//TODO -oauth.accessToken=//TODO -oauth.accessTokenSecret=//TODO From 00f5e0012cfa89c0a38c5ff123dd88af76aecfc3 Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Wed, 13 May 2020 03:46:33 +0200 Subject: [PATCH 27/29] BAEL-3925 - How to call Python from Java? (#9277) * BAEL-3491 - Check for null before calling parse in the Double.parseDouble * BAEL-3491 - Check for null before calling parse in the Double.parseDouble - Return to indentation with spaces. * BAEL-3854 - Pattern Matching for instanceof in Java 14 * BAEL-3854 - Pattern Matching for instanceof in Java 14 - add unit test * BAEL-3868 - Fix the integrations tests in mocks * BAEL-3925 - How to call Python from Java Co-authored-by: Jonathan Cook --- java-python-interop/README.md | 5 + java-python-interop/pom.xml | 55 ++++++++ .../interop/ScriptEngineManagerUtils.java | 34 +++++ .../src/main/resources/logback.xml | 13 ++ .../interop/JavaPythonInteropUnitTest.java | 131 ++++++++++++++++++ .../src/test/resources/hello.py | 1 + pom.xml | 3 +- 7 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 java-python-interop/README.md create mode 100644 java-python-interop/pom.xml create mode 100644 java-python-interop/src/main/java/com/baeldung/python/interop/ScriptEngineManagerUtils.java create mode 100644 java-python-interop/src/main/resources/logback.xml create mode 100644 java-python-interop/src/test/java/com/baeldung/python/interop/JavaPythonInteropUnitTest.java create mode 100644 java-python-interop/src/test/resources/hello.py diff --git a/java-python-interop/README.md b/java-python-interop/README.md new file mode 100644 index 0000000000..dc9573ecde --- /dev/null +++ b/java-python-interop/README.md @@ -0,0 +1,5 @@ +## Java Python Interop + +This module contains articles about Java and Python interoperability. + +### Relevant Articles: diff --git a/java-python-interop/pom.xml b/java-python-interop/pom.xml new file mode 100644 index 0000000000..6ee5a0be3b --- /dev/null +++ b/java-python-interop/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + java-python-interop + 0.0.1-SNAPSHOT + java-python-interop + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.python + jython-slim + ${jython.version} + + + org.apache.commons + commons-exec + ${commons-exec.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + java-python-interop + + + src/main/resources + true + + + src/test/resources + true + + + + + + 2.7.2 + 1.3 + 3.6.1 + + + \ No newline at end of file diff --git a/java-python-interop/src/main/java/com/baeldung/python/interop/ScriptEngineManagerUtils.java b/java-python-interop/src/main/java/com/baeldung/python/interop/ScriptEngineManagerUtils.java new file mode 100644 index 0000000000..981f174c33 --- /dev/null +++ b/java-python-interop/src/main/java/com/baeldung/python/interop/ScriptEngineManagerUtils.java @@ -0,0 +1,34 @@ +package com.baeldung.python.interop; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.script.ScriptEngineFactory; +import javax.script.ScriptEngineManager; + +public class ScriptEngineManagerUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(ScriptEngineManagerUtils.class); + + private ScriptEngineManagerUtils() { + } + + public static void listEngines() { + ScriptEngineManager manager = new ScriptEngineManager(); + List engines = manager.getEngineFactories(); + + for (ScriptEngineFactory engine : engines) { + LOGGER.info("Engine name: {}", engine.getEngineName()); + LOGGER.info("Version: {}", engine.getEngineVersion()); + LOGGER.info("Language: {}", engine.getLanguageName()); + + LOGGER.info("Short Names:"); + for (String names : engine.getNames()) { + LOGGER.info(names); + } + } + } + +} diff --git a/java-python-interop/src/main/resources/logback.xml b/java-python-interop/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/java-python-interop/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/java-python-interop/src/test/java/com/baeldung/python/interop/JavaPythonInteropUnitTest.java b/java-python-interop/src/test/java/com/baeldung/python/interop/JavaPythonInteropUnitTest.java new file mode 100644 index 0000000000..5ec3a2b61f --- /dev/null +++ b/java-python-interop/src/test/java/com/baeldung/python/interop/JavaPythonInteropUnitTest.java @@ -0,0 +1,131 @@ +package com.baeldung.python.interop; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.util.List; +import java.util.stream.Collectors; + +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.SimpleScriptContext; + +import org.apache.commons.exec.CommandLine; +import org.apache.commons.exec.DefaultExecutor; +import org.apache.commons.exec.ExecuteException; +import org.apache.commons.exec.PumpStreamHandler; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.python.core.PyException; +import org.python.core.PyObject; +import org.python.util.PythonInterpreter; + +public class JavaPythonInteropUnitTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void givenPythonScript_whenPythonProcessInvoked_thenSuccess() throws Exception { + ProcessBuilder processBuilder = new ProcessBuilder("python", resolvePythonScriptPath("hello.py")); + processBuilder.redirectErrorStream(true); + + Process process = processBuilder.start(); + List results = readProcessOutput(process.getInputStream()); + + assertThat("Results should not be empty", results, is(not(empty()))); + assertThat("Results should contain output of script: ", results, hasItem(containsString("Hello Baeldung Readers!!"))); + + int exitCode = process.waitFor(); + assertEquals("No errors should be detected", 0, exitCode); + } + + @Test + public void givenPythonScriptEngineIsAvailable_whenScriptInvoked_thenOutputDisplayed() throws Exception { + StringWriter output = new StringWriter(); + ScriptContext context = new SimpleScriptContext(); + context.setWriter(output); + + ScriptEngineManager manager = new ScriptEngineManager(); + ScriptEngine engine = manager.getEngineByName("python"); + engine.eval(new FileReader(resolvePythonScriptPath("hello.py")), context); + assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", output.toString() + .trim()); + } + + @Test + public void givenPythonInterpreter_whenPrintExecuted_thenOutputDisplayed() { + try (PythonInterpreter pyInterp = new PythonInterpreter()) { + StringWriter output = new StringWriter(); + pyInterp.setOut(output); + + pyInterp.exec("print('Hello Baeldung Readers!!')"); + assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", output.toString() + .trim()); + } + } + + @Test + public void givenPythonInterpreter_whenNumbersAdded_thenOutputDisplayed() { + try (PythonInterpreter pyInterp = new PythonInterpreter()) { + pyInterp.exec("x = 10+10"); + PyObject x = pyInterp.get("x"); + assertEquals("x: ", 20, x.asInt()); + } + } + + @Test + public void givenPythonInterpreter_whenErrorOccurs_thenExceptionIsThrown() { + thrown.expect(PyException.class); + thrown.expectMessage("ImportError: No module named syds"); + + try (PythonInterpreter pyInterp = new PythonInterpreter()) { + pyInterp.exec("import syds"); + } + } + + @Test + public void givenPythonScript_whenPythonProcessExecuted_thenSuccess() throws ExecuteException, IOException { + String line = "python " + resolvePythonScriptPath("hello.py"); + CommandLine cmdLine = CommandLine.parse(line); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); + + DefaultExecutor executor = new DefaultExecutor(); + executor.setStreamHandler(streamHandler); + + int exitCode = executor.execute(cmdLine); + assertEquals("No errors should be detected", 0, exitCode); + assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", outputStream.toString() + .trim()); + } + + private List readProcessOutput(InputStream inputStream) throws IOException { + try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) { + return output.lines() + .collect(Collectors.toList()); + } + } + + private String resolvePythonScriptPath(String filename) { + File file = new File("src/test/resources/" + filename); + return file.getAbsolutePath(); + } + +} diff --git a/java-python-interop/src/test/resources/hello.py b/java-python-interop/src/test/resources/hello.py new file mode 100644 index 0000000000..13275d9257 --- /dev/null +++ b/java-python-interop/src/test/resources/hello.py @@ -0,0 +1 @@ +print("Hello Baeldung Readers!!") \ No newline at end of file diff --git a/pom.xml b/pom.xml index 34703c6f73..f750ea8869 100644 --- a/pom.xml +++ b/pom.xml @@ -461,7 +461,8 @@ java-lite java-numbers java-numbers-2 - java-numbers-3 + java-numbers-3 + java-python-interop java-rmi java-spi java-vavr-stream From 020837fa2f2feb4295a315ff285691a459ba54a9 Mon Sep 17 00:00:00 2001 From: sasam0320 <63002713+sasam0320@users.noreply.github.com> Date: Wed, 13 May 2020 11:09:41 +0200 Subject: [PATCH 28/29] BAEL 3234 - Add missing code snippets from the Spring Properties article (#9280) --- .../main/resources/configForDbProperties.xml | 19 +++++++++++++++++++ .../main/resources/configForProperties.xml | 3 ++- .../src/main/resources/database.properties | 5 +++-- ...plePropertiesXmlConfigIntegrationTest.java | 2 +- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 spring-boot-modules/spring-boot-properties/src/main/resources/configForDbProperties.xml diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/configForDbProperties.xml b/spring-boot-modules/spring-boot-properties/src/main/resources/configForDbProperties.xml new file mode 100644 index 0000000000..00fd5f0508 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/configForDbProperties.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/configForProperties.xml b/spring-boot-modules/spring-boot-properties/src/main/resources/configForProperties.xml index 4468bb485f..bf4452da4a 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/configForProperties.xml +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/configForProperties.xml @@ -7,7 +7,8 @@ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd" > - + + diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/database.properties b/spring-boot-modules/spring-boot-properties/src/main/resources/database.properties index 6524ce6109..eb5703ca72 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/database.properties +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/database.properties @@ -1,4 +1,5 @@ -database.url=jdbc:postgresql:/localhost:5432/instance + +jdbc.url=jdbc:postgresql:/localhost:5432 database.username=foo database.password=bar -jdbc.url=jdbc:postgresql:/localhost:5432 \ No newline at end of file + diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java index 6827ee1cf1..2150d4b3ec 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/multiple/MultiplePropertiesXmlConfigIntegrationTest.java @@ -6,7 +6,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; -@SpringJUnitConfig(locations = "classpath:configForProperties.xml") +@SpringJUnitConfig(locations = {"classpath:configForProperties.xml", "classpath:configForDbProperties.xml"}) public class MultiplePropertiesXmlConfigIntegrationTest { @Value("${key.something}") private String something; From b48a87a352ac4d8fec84c257fcb182111a8f6e83 Mon Sep 17 00:00:00 2001 From: Benjamin Caure Date: Wed, 13 May 2020 17:43:22 +0200 Subject: [PATCH 29/29] Copy "BSON to JSON" tutorial entities to dedicated package (#9283) com.baeldung.bsontojson --- .../java/com/baeldung/bsontojson/Book.java | 168 ++++++++++++++++++ .../com/baeldung/bsontojson/Publisher.java | 70 ++++++++ .../bsontojson/BsonToJsonLiveTest.java | 22 +-- 3 files changed, 242 insertions(+), 18 deletions(-) create mode 100644 persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Book.java create mode 100644 persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Publisher.java diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Book.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Book.java new file mode 100644 index 0000000000..44e4ecb539 --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Book.java @@ -0,0 +1,168 @@ +package com.baeldung.bsontojson; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.HashSet; +import java.util.Set; + +import dev.morphia.annotations.Embedded; +import dev.morphia.annotations.Entity; +import dev.morphia.annotations.Field; +import dev.morphia.annotations.Id; +import dev.morphia.annotations.Index; +import dev.morphia.annotations.IndexOptions; +import dev.morphia.annotations.Indexes; +import dev.morphia.annotations.Property; +import dev.morphia.annotations.Reference; +import dev.morphia.annotations.Validation; + +@Entity("Books") +@Indexes({ @Index(fields = @Field("title"), options = @IndexOptions(name = "book_title")) }) +@Validation("{ price : { $gt : 0 } }") +public class Book { + @Id + private String isbn; + @Property + private String title; + private String author; + @Embedded + private Publisher publisher; + @Property("price") + private double cost; + @Reference + private Set companionBooks; + @Property + private LocalDateTime publishDate; + + public Book() { + + } + + public Book(String isbn, String title, String author, double cost, Publisher publisher) { + this.isbn = isbn; + this.title = title; + this.author = author; + this.cost = cost; + this.publisher = publisher; + this.companionBooks = new HashSet<>(); + } + + // Getters and setters ... + public String getIsbn() { + return isbn; + } + + public Book setIsbn(String isbn) { + this.isbn = isbn; + return this; + } + + public String getTitle() { + return title; + } + + public Book setTitle(String title) { + this.title = title; + return this; + } + + public String getAuthor() { + return author; + } + + public Book setAuthor(String author) { + this.author = author; + return this; + } + + public Publisher getPublisher() { + return publisher; + } + + public Book setPublisher(Publisher publisher) { + this.publisher = publisher; + return this; + } + + public double getCost() { + return cost; + } + + public Book setCost(double cost) { + this.cost = cost; + return this; + } + + public LocalDateTime getPublishDate() { + return publishDate; + } + + public Book setPublishDate(LocalDateTime publishDate) { + this.publishDate = publishDate; + return this; + } + + public Set getCompanionBooks() { + return companionBooks; + } + + public Book addCompanionBooks(Book book) { + if (companionBooks != null) + this.companionBooks.add(book); + return this; + } + + @Override + public String toString() { + return "Book [isbn=" + isbn + ", title=" + title + ", author=" + author + ", publisher=" + publisher + ", cost=" + cost + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((author == null) ? 0 : author.hashCode()); + long temp; + temp = Double.doubleToLongBits(cost); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + ((isbn == null) ? 0 : isbn.hashCode()); + result = prime * result + ((publisher == null) ? 0 : publisher.hashCode()); + result = prime * result + ((title == null) ? 0 : title.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Book other = (Book) obj; + if (author == null) { + if (other.author != null) + return false; + } else if (!author.equals(other.author)) + return false; + if (Double.doubleToLongBits(cost) != Double.doubleToLongBits(other.cost)) + return false; + if (isbn == null) { + if (other.isbn != null) + return false; + } else if (!isbn.equals(other.isbn)) + return false; + if (publisher == null) { + if (other.publisher != null) + return false; + } else if (!publisher.equals(other.publisher)) + return false; + if (title == null) { + if (other.title != null) + return false; + } else if (!title.equals(other.title)) + return false; + return true; + } + +} diff --git a/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Publisher.java b/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Publisher.java new file mode 100644 index 0000000000..1ab262c82b --- /dev/null +++ b/persistence-modules/java-mongodb/src/main/java/com/baeldung/bsontojson/Publisher.java @@ -0,0 +1,70 @@ +package com.baeldung.bsontojson; + +import org.bson.types.ObjectId; + +import dev.morphia.annotations.Entity; +import dev.morphia.annotations.Id; + +@Entity +public class Publisher { + + @Id + private ObjectId id; + private String name; + + public Publisher() { + + } + + public Publisher(ObjectId id, String name) { + this.id = id; + this.name = name; + } + + public ObjectId getId() { + return id; + } + + public void setId(ObjectId id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Catalog [id=" + id + ", name=" + name + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Publisher other = (Publisher) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + +} diff --git a/persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonLiveTest.java b/persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonLiveTest.java index 4e70394069..12053523f8 100644 --- a/persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonLiveTest.java +++ b/persistence-modules/java-mongodb/src/test/java/com/baeldung/bsontojson/BsonToJsonLiveTest.java @@ -2,32 +2,18 @@ package com.baeldung.bsontojson; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import java.sql.Timestamp; -import java.time.Instant; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.Date; -import java.util.TimeZone; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.bson.Document; -import org.bson.json.Converter; import org.bson.json.JsonMode; import org.bson.json.JsonWriterSettings; -import org.bson.json.StrictJsonWriter; import org.bson.types.ObjectId; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import org.junit.jupiter.api.AfterEach; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import com.baeldung.morphia.domain.Book; -import com.baeldung.morphia.domain.Publisher; import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; @@ -42,7 +28,7 @@ public class BsonToJsonLiveTest { @BeforeClass public static void setUp() { Morphia morphia = new Morphia(); - morphia.mapPackage("com.baeldung.morphia"); + morphia.mapPackage("com.baeldung.bsontojson"); datastore = morphia.createDatastore(new MongoClient(), DB_NAME); datastore.ensureIndexes(); @@ -72,7 +58,7 @@ public class BsonToJsonLiveTest { } String expectedJson = "{\"_id\": \"isbn\", " + - "\"className\": \"com.baeldung.morphia.domain.Book\", " + + "\"className\": \"com.baeldung.bsontojson.Book\", " + "\"title\": \"title\", " + "\"author\": \"author\", " + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + @@ -100,7 +86,7 @@ public class BsonToJsonLiveTest { } String expectedJson = "{\"_id\": \"isbn\", " + - "\"className\": \"com.baeldung.morphia.domain.Book\", " + + "\"className\": \"com.baeldung.bsontojson.Book\", " + "\"title\": \"title\", " + "\"author\": \"author\", " + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + @@ -127,7 +113,7 @@ public class BsonToJsonLiveTest { } String expectedJson = "{\"_id\": \"isbn\", " + - "\"className\": \"com.baeldung.morphia.domain.Book\", " + + "\"className\": \"com.baeldung.bsontojson.Book\", " + "\"title\": \"title\", " + "\"author\": \"author\", " + "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " +