diff --git a/testing-modules/mocks/pom.xml b/testing-modules/mocks/pom.xml index e447639288..82f1eaa8cf 100644 --- a/testing-modules/mocks/pom.xml +++ b/testing-modules/mocks/pom.xml @@ -41,6 +41,18 @@ jimfs ${jimf.version} + + net.datafaker + datafaker + ${datefaker.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + @@ -49,6 +61,8 @@ 3.5.1 1.41 1.1 + 1.6.0 + 2.13.4 \ No newline at end of file diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Collection.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Collection.java new file mode 100644 index 0000000000..1133480c34 --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Collection.java @@ -0,0 +1,22 @@ +package com.baeldung.datafaker; + +import java.util.List; +import net.datafaker.Faker; + +public class Collection { + public static final int MIN = 1; + public static final int MAX = 100; + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println(getFictionalCharacters()); + } + + static List getFictionalCharacters() { + return faker.collection( + () -> faker.starWars().character(), + () -> faker.starTrek().character()) + .len(MIN, MAX) + .generate(); + } +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Csv.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Csv.java new file mode 100644 index 0000000000..19bdbc236e --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Csv.java @@ -0,0 +1,25 @@ +package com.baeldung.datafaker; + +import net.datafaker.Faker; + +public class Csv { + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println("First expression:\n" + getFirstExpression()); + System.out.println("Second expression:\n" + getSecondExpression()); + } + + static String getSecondExpression() { + final String secondExpressionString + = "#{csv ',','\"','true','4','name_column','#{Name.first_name}','last_name_column','#{Name.last_name}'}"; + return faker.expression(secondExpressionString); + } + + static String getFirstExpression() { + final String firstExpressionString + = "#{csv '4','name_column','#{Name.first_name}','last_name_column','#{Name.last_name}'}"; + return faker.expression(firstExpressionString); + } + +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Examplify.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Examplify.java new file mode 100644 index 0000000000..87cac49284 --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Examplify.java @@ -0,0 +1,21 @@ +package com.baeldung.datafaker; + + +import net.datafaker.Faker; + +public class Examplify { + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println("Expression: " + getExpression()); + System.out.println("Number expression: " + getNumberExpression()); + } + + static String getNumberExpression() { + return faker.expression("#{examplify '123-123-123'}"); + } + + static String getExpression() { + return faker.expression("#{examplify 'Cat in the Hat'}"); + } +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Json.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Json.java new file mode 100644 index 0000000000..0ee2fc547e --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Json.java @@ -0,0 +1,19 @@ +package com.baeldung.datafaker; + +import net.datafaker.Faker; + +public class Json { + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println(getExpression()); + } + + static String getExpression() { + return faker.expression( + "#{json 'person'," + + "'#{json ''first_name'',''#{Name.first_name}'',''last_name'',''#{Name.last_name}''}'," + + "'address'," + + "'#{json ''country'',''#{Address.country}'',''city'',''#{Address.city}''}'}"); + } +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MethodInvocation.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MethodInvocation.java new file mode 100644 index 0000000000..3f719a59d8 --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MethodInvocation.java @@ -0,0 +1,20 @@ +package com.baeldung.datafaker; + +import net.datafaker.Faker; + +public class MethodInvocation { + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println("Name from a method: " + getNameFromMethod()); + System.out.println("Name from an expression: " + getNameFromExpression()); + } + + static String getNameFromExpression() { + return faker.expression("#{Name.first_Name}"); + } + + static String getNameFromMethod() { + return faker.name().firstName(); + } +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MethodInvocationWithParams.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MethodInvocationWithParams.java new file mode 100644 index 0000000000..aaec458724 --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MethodInvocationWithParams.java @@ -0,0 +1,25 @@ +package com.baeldung.datafaker; + +import net.datafaker.Faker; + +import java.time.Duration; + +public class MethodInvocationWithParams { + public static final int MIN = 1; + public static final int MAX = 10; + public static final String UNIT = "SECONDS"; + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println("Duration from the method :" + getDurationFromMethod()); + System.out.println("Duration from the expression: " + getDurationFromExpression()); + } + + static String getDurationFromExpression() { + return faker.expression("#{date.duration '1', '10', 'SECONDS'}"); + } + + static Duration getDurationFromMethod() { + return faker.date().duration(MIN, MAX, UNIT); + } +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MixedCollection.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MixedCollection.java new file mode 100644 index 0000000000..fa7513d5b6 --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/MixedCollection.java @@ -0,0 +1,25 @@ +package com.baeldung.datafaker; + +import net.datafaker.Faker; + +import java.io.Serializable; +import java.util.List; + +public class MixedCollection { + public static final int MIN = 1; + public static final int MAX = 20; + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println(getMixedCollection()); + } + + static List getMixedCollection() { + return faker.collection( + () -> faker.date().birthday(), + () -> faker.name().fullName() + ) + .len(MIN, MAX) + .generate(); + } +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Option.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Option.java new file mode 100644 index 0000000000..489af6d243 --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Option.java @@ -0,0 +1,25 @@ +package com.baeldung.datafaker; + +import net.datafaker.Faker; + +public class Option { + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println("First expression: " + getFirstExpression()); + System.out.println("Second expression: " + getSecondExpression()); + System.out.println("Third expression: " + getThirdExpression()); + } + + static String getThirdExpression() { + return faker.expression("#{regexify '(Hi|Hello|Hey)'}"); + } + + static String getSecondExpression() { + return faker.expression("#{options.option '1','2','3','4','*'}"); + } + + static String getFirstExpression() { + return faker.expression("#{options.option 'Hi','Hello','Hey'}"); + } +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Regexify.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Regexify.java new file mode 100644 index 0000000000..9b718431a2 --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Regexify.java @@ -0,0 +1,20 @@ +package com.baeldung.datafaker; + +import com.github.javafaker.Faker; + +public class Regexify { + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println("Expression: " + getExpression()); + System.out.println("Regexify with a method: " + getMethodExpression()); + } + + static String getMethodExpression() { + return faker.regexify("[A-D]{4,10}"); + } + + static String getExpression() { + return faker.expression("#{regexify '(hello|bye|hey)'}"); + } +} diff --git a/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Templatify.java b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Templatify.java new file mode 100644 index 0000000000..6159244c73 --- /dev/null +++ b/testing-modules/mocks/src/main/java/com/baeldung/datafaker/Templatify.java @@ -0,0 +1,21 @@ +package com.baeldung.datafaker; + +import net.datafaker.Faker; + +public class Templatify { + private static final Faker faker = new Faker(); + + public static void main(String[] args) { + System.out.println("Expression: " + getExpression()); + System.out.println("Expression with a placeholder: " + getExpressionWithPlaceholder()); + } + + static String getExpressionWithPlaceholder() { + return faker.expression("#{templatify '#ight', '#', 'f', 'l', 'm', 'n'}"); + } + + static String getExpression() { + return faker.expression("#{templatify 'test','t','j','r'}"); + } +} + diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/CollectionUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/CollectionUnitTest.java new file mode 100644 index 0000000000..95e61c795a --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/CollectionUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +class CollectionUnitTest { + + @Test + void whenGettingFictionaCharacters_thenResultNotEmptyAndOfCorrectSize() { + assertAll( + () -> assertThat(Collection.getFictionalCharacters()).isNotEmpty(), + () -> assertThat(Collection.getFictionalCharacters()).size().isGreaterThanOrEqualTo(Collection.MIN), + () -> assertThat(Collection.getFictionalCharacters()).size().isLessThanOrEqualTo(Collection.MAX) + ); + + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/CsvUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/CsvUnitTest.java new file mode 100644 index 0000000000..fb78a1689d --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/CsvUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +class CsvUnitTest { + + @Test + void whenGettingFirstExpression_thenResultNotEmptyAndOfCorrectSizeAndFormat() { + assertAll( + () -> assertThat(Csv.getFirstExpression()).isNotBlank(), + () -> assertThat(Csv.getFirstExpression().split("\n")).hasSize(5), + () -> assertThat(Csv.getFirstExpression().split("\n")[0]).isEqualTo("\"name_column\",\"last_name_column\"") + ); + } + + @Test + void whenGettingSecondExpression_thenResultNotEmptyAndOfCorrectSizeAndFormat() { + assertAll( + () -> assertThat(Csv.getFirstExpression()).isNotBlank(), + () -> assertThat(Csv.getFirstExpression().split("\n")).hasSize(5), + () -> assertThat(Csv.getFirstExpression().split("\n")[0]).isEqualTo("\"name_column\",\"last_name_column\"") + ); + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/ExamplifyUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/ExamplifyUnitTest.java new file mode 100644 index 0000000000..fb633a18fd --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/ExamplifyUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class ExamplifyUnitTest { + + @Test + void whenGettingNumberExpression_thenResultNotEmptyAndMathesRegex() { + assertAll( + () -> assertThat(Examplify.getNumberExpression()).isNotBlank(), + () -> assertThat(Examplify.getNumberExpression()).matches("\\d{3}-\\d{3}-\\d{3}") + ); + } + + @Test + void whenGettingExpression_thenResultNotEmptyAndMathesRegex() { + assertAll( + () -> assertThat(Examplify.getExpression()).isNotBlank(), + () -> assertThat(Examplify.getExpression()) + .matches("[A-Z][a-z]{2} [a-z]{2} [a-z]{3} [A-Z][a-z]{2}") + ); + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/JsonUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/JsonUnitTest.java new file mode 100644 index 0000000000..7da3448efb --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/JsonUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.datafaker; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNoException; +import static org.junit.jupiter.api.Assertions.*; + +class JsonUnitTest { + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + @Test + void whenGettingJsonExpression_thenResultIsValidJson() { + assertAll( + () -> assertThatNoException().isThrownBy(() -> objectMapper.readTree(Json.getExpression())), + () -> assertThat(Json.getExpression()).isNotBlank() + ); + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MethodInvocationUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MethodInvocationUnitTest.java new file mode 100644 index 0000000000..f2a1bebab4 --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MethodInvocationUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class MethodInvocationUnitTest { + + @Test + void whenGettingNameFromExpression_thenResultNotEmpty() { + assertThat(MethodInvocation.getNameFromMethod()).isNotBlank(); + } + + @Test + void whenGettingNameFromMethod_thenResultNotEmpty() { + assertThat(MethodInvocation.getNameFromExpression()).isNotBlank(); + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MethodInvocationWithParamsUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MethodInvocationWithParamsUnitTest.java new file mode 100644 index 0000000000..1a31ade31d --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MethodInvocationWithParamsUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import java.time.Duration; + +import static org.assertj.core.api.Assertions.assertThat; + +class MethodInvocationWithParamsUnitTest { + + @Test + void whenGettingDurationFromExpression_thenResultNotEmpty() { + assertThat(MethodInvocationWithParams.getDurationFromExpression()).isNotBlank(); + } + + @Test + void whenGettingDurationFromMethod_thenResultNotNullAndInBoundaries() { + assertThat(MethodInvocationWithParams.getDurationFromMethod()) + .isNotNull() + .isBetween(Duration.ofSeconds(MethodInvocationWithParams.MIN), + Duration.ofSeconds(MethodInvocationWithParams.MAX)); + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MixedCollectionUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MixedCollectionUnitTest.java new file mode 100644 index 0000000000..07c8e07c3a --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/MixedCollectionUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +class MixedCollectionUnitTest { + + @Test + void whenGettingMixedCollection_thenResultNotEmptyAndOfCorrectSize() { + assertAll( + () -> assertThat(MixedCollection.getMixedCollection()).isNotEmpty(), + () -> assertThat(MixedCollection.getMixedCollection()).size().isGreaterThanOrEqualTo(MixedCollection.MIN), + () -> assertThat(MixedCollection.getMixedCollection()).size().isLessThanOrEqualTo(MixedCollection.MAX) + ); + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/OptionUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/OptionUnitTest.java new file mode 100644 index 0000000000..8e3a38958e --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/OptionUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class OptionUnitTest { + @Test + void whenGettingThirdExpression_thenResultNotBlankAndMatchesRegex() { + assertThat(Option.getThirdExpression()).isNotBlank() + .matches("(Hi|Hello|Hey)"); + } + + @Test + void whenGettingSecondExpression_thenResultNotBlankAndMatchesRegex() { + assertThat(Option.getSecondExpression()).isNotBlank() + .matches("(1|2|3|4|\\*)"); + } + + @Test + void whenGettingFirstExpression_thenResultNotBlankAndMatchesRegex() { + assertThat(Option.getFirstExpression()).isNotBlank() + .matches("(Hi|Hello|Hey)"); + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/RegexifyUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/RegexifyUnitTest.java new file mode 100644 index 0000000000..98d4e9e85c --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/RegexifyUnitTest.java @@ -0,0 +1,20 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class RegexifyUnitTest { + + @Test + void whenGettingMethidExpression_thenResultNotBlankAndMatchesRegex() { + assertThat(Regexify.getMethodExpression()).isNotBlank() + .matches("[A-D]{4,10}"); + } + + @Test + void whenGettingExpression_thenResultNotBlankAndMatchesRegex() { + assertThat(Regexify.getExpression()).isNotBlank() + .matches("(hello|bye|hey)"); + } +} \ No newline at end of file diff --git a/testing-modules/mocks/src/test/java/com/baeldung/datafaker/TemplatifyUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/TemplatifyUnitTest.java new file mode 100644 index 0000000000..491c3b6929 --- /dev/null +++ b/testing-modules/mocks/src/test/java/com/baeldung/datafaker/TemplatifyUnitTest.java @@ -0,0 +1,19 @@ +package com.baeldung.datafaker; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class TemplatifyUnitTest { + @Test + void whenGettingPlaceholderExpression_thenResultNotBlankAndMatchesRegex() { + assertThat(Templatify.getExpressionWithPlaceholder()).isNotBlank() + .matches(".ight"); + } + + @Test + void whenGettingExpression_thenResultNotBlankAndMatchesRegex() { + assertThat(Templatify.getExpression()).isNotBlank() + .matches(".es."); + } +} \ No newline at end of file