From 98175251d9886c7e176a6b12322673bc339ccb3f Mon Sep 17 00:00:00 2001
From: Roland Weisleder <roland.weisleder@googlemail.com>
Date: Wed, 27 May 2020 20:04:37 +0200
Subject: [PATCH] Ensure same passwords for same random seeds

Fixes #130
---
 .../person/DefaultPersonProvider.java         |  7 ++++--
 .../com/devskiller/jfairy/FairyDeSpec.groovy  |  2 +-
 .../com/devskiller/jfairy/FairyFrSpec.groovy  |  2 +-
 .../com/devskiller/jfairy/FairyKaSpec.groovy  |  6 ++---
 .../com/devskiller/jfairy/FairySpec.groovy    | 22 +++++++++++++++++++
 .../com/devskiller/jfairy/FairyZhSpec.groovy  |  4 ++--
 .../person/locale/de/DeAddressSpec.groovy     | 16 +++++++-------
 .../person/locale/en/EnAddressSpec.groovy     | 16 +++++++-------
 .../person/locale/es/EsAddressSpec.groovy     | 16 +++++++-------
 .../person/locale/ka/KaAddressSpec.groovy     | 14 ++++++------
 .../person/locale/pl/PlAddressSpec.groovy     | 16 +++++++-------
 .../person/locale/sv/SvAddressSpec.groovy     | 14 ++++++------
 12 files changed, 80 insertions(+), 55 deletions(-)

diff --git a/src/main/java/com/devskiller/jfairy/producer/person/DefaultPersonProvider.java b/src/main/java/com/devskiller/jfairy/producer/person/DefaultPersonProvider.java
index bb5ce1e4..47236066 100644
--- a/src/main/java/com/devskiller/jfairy/producer/person/DefaultPersonProvider.java
+++ b/src/main/java/com/devskiller/jfairy/producer/person/DefaultPersonProvider.java
@@ -212,8 +212,11 @@ public void generatePassword() {
 		if (password != null) {
 			return;
 		}
-		// FIXME: Replace this with baseProducer
-		password = RandomStringUtils.randomAlphanumeric(8);
+		StringBuilder passwordPattern = new StringBuilder();
+		for (int i = baseProducer.randomBetween(6, 14); i > 0; i--) {
+			passwordPattern.append(baseProducer.randomElement("?", "#"));
+		}
+		password = baseProducer.bothify(passwordPattern.toString());
 	}
 
 	@Override
diff --git a/src/test/groovy/com/devskiller/jfairy/FairyDeSpec.groovy b/src/test/groovy/com/devskiller/jfairy/FairyDeSpec.groovy
index 2ccbf17e..e03f7144 100644
--- a/src/test/groovy/com/devskiller/jfairy/FairyDeSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/FairyDeSpec.groovy
@@ -23,7 +23,7 @@ class FairyDeSpec extends Specification {
 		when:
 			Person person = fairy.person();
 		then:
-			person.address.city == 'Schlitz'
+			person.address.city == 'Eschenburg'
 	}
 
 }
diff --git a/src/test/groovy/com/devskiller/jfairy/FairyFrSpec.groovy b/src/test/groovy/com/devskiller/jfairy/FairyFrSpec.groovy
index 026bbed9..ff2842fb 100644
--- a/src/test/groovy/com/devskiller/jfairy/FairyFrSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/FairyFrSpec.groovy
@@ -20,7 +20,7 @@ class FairyFrSpec extends Specification {
 		when:
 			Person person = fairy.person();
 		then:
-			person.address.city == 'Échirolles'
+			person.address.city == 'Rosny-sous-Bois'
 	}
 
 }
diff --git a/src/test/groovy/com/devskiller/jfairy/FairyKaSpec.groovy b/src/test/groovy/com/devskiller/jfairy/FairyKaSpec.groovy
index dc83f83c..27d28afa 100644
--- a/src/test/groovy/com/devskiller/jfairy/FairyKaSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/FairyKaSpec.groovy
@@ -21,20 +21,20 @@ class FairyKaSpec extends Specification {
 		when:
 		Person person = fairy.person()
 		then:
-		person.address.street == 'აგლაძის ქუჩა' // Agladze Street
+		person.address.street == 'კოსტავას' // Kostava Street
 	}
 
 	def "Should create Georgian city"() {
 		when:
 		Person person = fairy.person()
 		then:
-		person.address.city == 'ზესტაფონი' // Sagarejo
+		person.address.city == 'გორი' // Gori
 	}
 
 	def "Should create Georgian ID card"() {
 		when:
 		Person person = fairy.person()
 		then:
-		person.nationalIdentityCardNumber == 'Nლ4757825'
+		person.nationalIdentityCardNumber == 'Nხ4686471'
 	}
 }
diff --git a/src/test/groovy/com/devskiller/jfairy/FairySpec.groovy b/src/test/groovy/com/devskiller/jfairy/FairySpec.groovy
index 3ff66a87..e30c5129 100644
--- a/src/test/groovy/com/devskiller/jfairy/FairySpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/FairySpec.groovy
@@ -74,6 +74,28 @@ class FairySpec extends Specification {
 			!firstPerson.fullName.equals(secondPerson.fullName)
 	}
 
+	def "Second person should have the same password with the same random seed"() {
+		given:
+			Fairy firstFairy = Fairy.builder().withRandomSeed(10).build()
+			Fairy secondFairy = Fairy.builder().withRandomSeed(10).build()
+
+			Person firstPerson = firstFairy.person()
+			Person secondPerson = secondFairy.person()
+		expect:
+			firstPerson.getPassword().equals(secondPerson.getPassword())
+	}
+
+	def "Second person should not have the same password with the different random seeds"() {
+		given:
+			Fairy firstFairy = Fairy.builder().withRandomSeed(10).build()
+			Fairy secondFairy = Fairy.builder().withRandomSeed(20).build()
+
+			Person firstPerson = firstFairy.person()
+			Person secondPerson = secondFairy.person()
+		expect:
+			!firstPerson.getPassword().equals(secondPerson.getPassword())
+	}
+
 	def "should use default DataMaster when custom not provided"() {
 		given:
 			Fairy fairy = Fairy.create();
diff --git a/src/test/groovy/com/devskiller/jfairy/FairyZhSpec.groovy b/src/test/groovy/com/devskiller/jfairy/FairyZhSpec.groovy
index 2655dfe2..01531bc5 100644
--- a/src/test/groovy/com/devskiller/jfairy/FairyZhSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/FairyZhSpec.groovy
@@ -43,14 +43,14 @@ class FairyZhSpec extends Specification {
 		when:
 		Person person = fairy.person()
 		then:
-		person.address.addressLine1 == '杭州市内环路45号'
+		person.address.addressLine1 == '广州市滨江路153号'
 	}
 
 	def "Should create Chinese city"() {
 		when:
 		Person person = fairy.person()
 		then:
-		person.address.city == '杭州'
+		person.address.city == '广州'
 	}
 
 	/********************
diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/de/DeAddressSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/de/DeAddressSpec.groovy
index c32b8384..2e9a3bec 100644
--- a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/de/DeAddressSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/de/DeAddressSpec.groovy
@@ -18,42 +18,42 @@ class DeAddressSpec extends Specification {
 
 	def "should generate random street"() {
 		expect:
-			address.street == 'Sautterweg'
+			address.street == 'Sonnenburger Weg'
 	}
 
 	def "should generate random streetNumber"() {
 		expect:
-			address.streetNumber == '21'
+			address.streetNumber == '99'
 	}
 
 	def "should generate random apartmentNumber"() {
 		expect:
-			address.apartmentNumber == '50'
+			address.apartmentNumber == ''
 	}
 
 	def "should generate random postalCode"() {
 		expect:
-			address.postalCode == '71138'
+			address.postalCode == '98126'
 	}
 
 	def "should generate random city"() {
 		expect:
-			address.city == 'Maxhütte Haidhof'
+			address.city == 'Wetschen'
 	}
 
 	def "should return addressLine1 in de locale format"() {
 		expect:
-			address.addressLine1 == 'Sautterweg 21, 50'
+			address.addressLine1 == 'Sonnenburger Weg 99'
 	}
 
 	def "should return addressLine2 in de locale format"() {
 		expect:
-			address.addressLine2 == '71138 Maxhütte Haidhof'
+			address.addressLine2 == '98126 Wetschen'
 	}
 
 	def "should return address in de locale format"() {
 		expect:
-			address.toString() == "Sautterweg 21, 50${System.lineSeparator()}71138 Maxhütte Haidhof"
+			address.toString() == "Sonnenburger Weg 99${System.lineSeparator()}98126 Wetschen"
 	}
 
 }
diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/en/EnAddressSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/en/EnAddressSpec.groovy
index 11654c54..f79d896a 100644
--- a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/en/EnAddressSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/en/EnAddressSpec.groovy
@@ -18,42 +18,42 @@ class EnAddressSpec extends Specification {
 
 	def "should generate random street"() {
 		expect:
-			address.street == "Washington Walk"
+			address.street == "Herzi Street"
 	}
 
 	def "should generate random streetNumber"() {
 		expect:
-			address.streetNumber == "42"
+			address.streetNumber == "63"
 	}
 
     def "should generate random apartmentNumber"() {
         expect:
-            address.apartmentNumber == "86"
+            address.apartmentNumber == ""
     }
 
 	def "should generate random postalCode"() {
 		expect:
-			address.postalCode == "86645"
+			address.postalCode == "42856"
 	}
 
 	def "should generate random city"() {
 		expect:
-		    address.city == "San Francisco"
+		    address.city == "Miami"
 	}
 
 	def "should return addressLine1 in en locale format"() {
 		expect:
-			address.addressLine1 == "42 Washington Walk APT 86"
+			address.addressLine1 == "63 Herzi Street"
 	}
 
 	def "should return addressLine2 in en locale format"() {
 		expect:
-			address.addressLine2 == "San Francisco 86645"
+			address.addressLine2 == "Miami 42856"
 	}
 
 	def "should return address in en locale format"() {
 		expect:
-            address.toString() == "42 Washington Walk APT 86" + System.lineSeparator() + "San Francisco 86645"
+            address.toString() == "63 Herzi Street" + System.lineSeparator() + "Miami 42856"
 	}
 
 }
diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/es/EsAddressSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/es/EsAddressSpec.groovy
index 1ccbff5c..d9fae541 100644
--- a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/es/EsAddressSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/es/EsAddressSpec.groovy
@@ -18,42 +18,42 @@ class EsAddressSpec extends Specification {
 
 	def "should generate random street"() {
 		expect:
-			address.street == "Gran Vía"
+			address.street == "Colón"
 	}
 
 	def "should generate random streetNumber"() {
 		expect:
-			address.streetNumber == "32"
+			address.streetNumber == "101"
 	}
 
 	def "should generate random apartmentNumber"() {
 		expect:
-			address.apartmentNumber == "64"
+			address.apartmentNumber == ""
 	}
 
 	def "should generate random postalCode"() {
 		expect:
-			address.postalCode == "79.638"
+			address.postalCode == "79.240"
 	}
 
 	def "should generate random city"() {
 		expect:
-			address.city == "Ciudad Real"
+			address.city == "Algeciras"
 	}
 
 	def "should return addressLine1 in es locale format"() {
 		expect:
-			address.addressLine1 == "Gran Vía, 32 64"
+			address.addressLine1 == "Colón, 101"
 	}
 
 	def "should return addressLine2 in es locale format"() {
 		expect:
-			address.addressLine2 == "79.638 Ciudad Real"
+			address.addressLine2 == "79.240 Algeciras"
 	}
 
 	def "should return address in es locale format"() {
 		expect:
-			address.toString() == "Gran Vía, 32 64" + System.lineSeparator() + "79.638 Ciudad Real"
+			address.toString() == "Colón, 101" + System.lineSeparator() + "79.240 Algeciras"
 	}
 
 }
diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/ka/KaAddressSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/ka/KaAddressSpec.groovy
index 17026325..ff6f3722 100644
--- a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/ka/KaAddressSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/ka/KaAddressSpec.groovy
@@ -21,12 +21,12 @@ class KaAddressSpec extends Specification {
 
 	def "should generate random street"() {
 		expect:
-		address.street == "ნინოშვილის ქუჩა"
+		address.street == "დავით აღმაშენებლის გამზირი"
 	}
 
 	def "should generate random streetNumber"() {
 		expect:
-		address.streetNumber == "179"
+		address.streetNumber == "110"
 	}
 
 	def "should generate random apartmentNumber"() {
@@ -36,26 +36,26 @@ class KaAddressSpec extends Specification {
 
 	def "should generate random postalCode"() {
 		expect:
-		address.postalCode == "6981"
+		address.postalCode == "6003"
 	}
 
 	def "should generate random city"() {
 		expect:
-		address.city == "წყალტუბო"
+		address.city == "გარდაბანი"
  	}
 
 	def "should return addressLine1 in GE locale format"() {
 		expect:
-		address.addressLine1 == "6981, წყალტუბო"  // ZIP, city
+		address.addressLine1 == "6003, გარდაბანი"  // ZIP, city
 	}
 
 	def "should return addressLine2 in GE locale format"() {
 		expect:
-		address.addressLine2 == "ნინოშვილის ქუჩა №179"  // street & number & appartment
+		address.addressLine2 == "დავით აღმაშენებლის გამზირი №110"  // street & number & appartment
 	}
 
 	def "should return address in GE locale format"() {
 		expect:
-		address.toString() == "6981, წყალტუბო${lineSeparator()}ნინოშვილის ქუჩა №179"
+		address.toString() == "6003, გარდაბანი${lineSeparator()}დავით აღმაშენებლის გამზირი №110"
 	}
 }
diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/pl/PlAddressSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/pl/PlAddressSpec.groovy
index 108f6cec..f985b457 100644
--- a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/pl/PlAddressSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/pl/PlAddressSpec.groovy
@@ -18,42 +18,42 @@ class PlAddressSpec extends Specification {
 
 	def "should generate random street"() {
 		expect:
-			address.street == "Długorzeczna"
+			address.street == "Prosta"
 	}
 
 	def "should generate random streetNumber"() {
 		expect:
-			address.streetNumber == "62"
+			address.streetNumber == "71"
 	}
 
     def "should generate random apartmentNumber"() {
         expect:
-            address.apartmentNumber == "89"
+            address.apartmentNumber == ""
     }
 
 	def "should generate random postalCode"() {
 		expect:
-			address.postalCode == "39-682"
+			address.postalCode == "26-077"
 	}
 
 	def "should generate random city"() {
 		expect:
-		    address.city == "Zławieś Wielka"
+		    address.city == "Pniewy"
 	}
 
 	def "should return addressLine1 in pl locale format"() {
 		expect:
-			address.addressLine1 == "Długorzeczna 62, 89"
+			address.addressLine1 == "Prosta 71"
 	}
 
 	def "should return addressLine2 in pl locale format"() {
 		expect:
-			address.addressLine2 == "39-682 Zławieś Wielka"
+			address.addressLine2 == "26-077 Pniewy"
 	}
 
 	def "should return address in pl locale format"() {
 		expect:
-			address.toString() == "Długorzeczna 62, 89" + System.lineSeparator() + "39-682 Zławieś Wielka"
+			address.toString() == "Prosta 71" + System.lineSeparator() + "26-077 Pniewy"
 	}
 
 }
diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/sv/SvAddressSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/sv/SvAddressSpec.groovy
index cfd50b53..d687732d 100644
--- a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/sv/SvAddressSpec.groovy
+++ b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/sv/SvAddressSpec.groovy
@@ -18,12 +18,12 @@ class SvAddressSpec extends Specification {
 
 	def "should generate random street"() {
 		expect:
-			address.street == "Trångsund"
+			address.street == "Sandåsgatan"
 	}
 
 	def "should generate random streetNumber"() {
 		expect:
-			address.streetNumber == "70"
+			address.streetNumber == "107"
 	}
 
 	def "should generate random apartmentNumber"() {
@@ -33,27 +33,27 @@ class SvAddressSpec extends Specification {
 
 	def "should generate random postalCode"() {
 		expect:
-			address.postalCode == "915 28"
+			address.postalCode == "745 69"
 	}
 
 	def "should generate random city"() {
 		expect:
-			address.city == "Brätte"
+			address.city == "Lödöse"
 	}
 
 	def "should return addressLine1 in sv locale format"() {
 		expect:
-			address.addressLine1 == "Trångsund 70"
+			address.addressLine1 == "Sandåsgatan 107"
 	}
 
 	def "should return addressLine2 in sv locale format"() {
 		expect:
-			address.addressLine2 == "915 28 Brätte"
+			address.addressLine2 == "745 69 Lödöse"
 	}
 
 	def "should return address in sv locale format"() {
 		expect:
-			address.toString() == "Trångsund 70" + System.lineSeparator() + "915 28 Brätte"
+			address.toString() == "Sandåsgatan 107" + System.lineSeparator() + "745 69 Lödöse"
 	}
 
 }