diff --git a/src/main/java/com/devskiller/jfairy/data/DataMaster.java b/src/main/java/com/devskiller/jfairy/data/DataMaster.java index 075f2717..c791e26d 100644 --- a/src/main/java/com/devskiller/jfairy/data/DataMaster.java +++ b/src/main/java/com/devskiller/jfairy/data/DataMaster.java @@ -2,6 +2,8 @@ import java.util.List; +import com.devskiller.jfairy.producer.util.LanguageCode; + /** * @author Olga Maciaszek-Sharma * @since 23.04.15 @@ -16,5 +18,7 @@ public interface DataMaster { String getRandomValue(String key); + LanguageCode getLanguage(); + } diff --git a/src/main/java/com/devskiller/jfairy/data/MapBasedDataMaster.java b/src/main/java/com/devskiller/jfairy/data/MapBasedDataMaster.java index 49b98813..3558fcbd 100644 --- a/src/main/java/com/devskiller/jfairy/data/MapBasedDataMaster.java +++ b/src/main/java/com/devskiller/jfairy/data/MapBasedDataMaster.java @@ -12,15 +12,18 @@ import java.util.List; import java.util.Map; +import org.apache.commons.lang3.EnumUtils; import org.yaml.snakeyaml.Yaml; import com.devskiller.jfairy.producer.BaseProducer; +import com.devskiller.jfairy.producer.util.LanguageCode; import static com.google.common.base.Preconditions.checkArgument; public class MapBasedDataMaster implements DataMaster { + public static final String LANGUAGE_TAG = "language"; private final BaseProducer baseProducer; private Map dataSource = new CaseInsensitiveMap(); @@ -68,6 +71,11 @@ public String getRandomValue(String key) { return baseProducer.randomElement(getStringList(key)); } + @Override + public LanguageCode getLanguage() { + return EnumUtils.getEnum(LanguageCode.class, getString(LANGUAGE_TAG).toUpperCase()); + } + @SuppressWarnings({"unchecked", "ConstantConditions"}) // checked by checkArgument T getData(String key, Class type) { checkArgument(key != null, "key cannot be null"); diff --git a/src/main/java/com/devskiller/jfairy/producer/payment/DefaultIBANProvider.java b/src/main/java/com/devskiller/jfairy/producer/payment/DefaultIBANProvider.java index 22af6d38..9091d4dd 100644 --- a/src/main/java/com/devskiller/jfairy/producer/payment/DefaultIBANProvider.java +++ b/src/main/java/com/devskiller/jfairy/producer/payment/DefaultIBANProvider.java @@ -2,6 +2,7 @@ import javax.inject.Inject; import java.util.IllegalFormatCodePointException; +import java.util.List; import com.google.common.base.Strings; import com.google.inject.assistedinject.Assisted; @@ -10,14 +11,16 @@ import org.iban4j.UnsupportedCountryException; import com.devskiller.jfairy.data.DataMaster; +import com.devskiller.jfairy.producer.BaseProducer; +import com.devskiller.jfairy.producer.person.Country; /** * ALPHA: Under development */ public class DefaultIBANProvider implements IBANProvider { - public static final String DEFAULT_COUNTRY_CODE = "defaultCountryCode"; protected DataMaster dataMaster; + protected BaseProducer baseProducer; protected CountryCode countryCode; protected String accountNumber; protected String bankCode; @@ -25,11 +28,12 @@ public class DefaultIBANProvider implements IBANProvider { protected String nationalCheckDigit; @Inject - public DefaultIBANProvider(DataMaster dataMaster, + public DefaultIBANProvider(BaseProducer baseProducer, + DataMaster dataMaster, @Assisted IBANProperties.Property... properties) { this.dataMaster = dataMaster; - + this.baseProducer = baseProducer; for (IBANProperties.Property property : properties) { property.apply(this); } @@ -74,7 +78,9 @@ public IBAN get() { @Override public void fillCountryCode() { if (countryCode == null) { - countryCode = CountryCode.valueOf(dataMaster.getString(DEFAULT_COUNTRY_CODE)); + List countries = Country.findCountryForLanguage(dataMaster.getLanguage()); + Country country = baseProducer.randomElement(countries); + countryCode = CountryCode.valueOf(country.getCode()); } } diff --git a/src/main/java/com/devskiller/jfairy/producer/person/Country.java b/src/main/java/com/devskiller/jfairy/producer/person/Country.java new file mode 100644 index 00000000..325a47cb --- /dev/null +++ b/src/main/java/com/devskiller/jfairy/producer/person/Country.java @@ -0,0 +1,42 @@ +package com.devskiller.jfairy.producer.person; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import com.devskiller.jfairy.producer.util.LanguageCode; + +public enum Country { + Poland("PL", LanguageCode.PL), + UnitedKingdom("GB", LanguageCode.EN), + Australia("AU", LanguageCode.EN), + USA("US", LanguageCode.EN), + Canada("CA", LanguageCode.EN, LanguageCode.FR), + Spain("ES", LanguageCode.ES), + France("FR", LanguageCode.FR), + Georgia("GE", LanguageCode.KA), + Italy("IT", LanguageCode.IT), + Germany("DE", LanguageCode.DE), + Sweden("SE", LanguageCode.SV), + China("CN", LanguageCode.ZH); + + // ISO 3166 code + private final String code; + // ISO 639-1 + private final List languages; + + Country(String code, LanguageCode... language) { + this.code = code; + this.languages = Arrays.asList(language); + } + + public String getCode() { + return code; + } + + public static List findCountryForLanguage(LanguageCode language) { + return Arrays.stream(Country.values()) + .filter(country -> country.languages.contains(language)) + .collect(Collectors.toList()); + } +} 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 6a27ecda..bb5ce1e4 100644 --- a/src/main/java/com/devskiller/jfairy/producer/person/DefaultPersonProvider.java +++ b/src/main/java/com/devskiller/jfairy/producer/person/DefaultPersonProvider.java @@ -4,6 +4,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; +import java.util.List; import com.google.inject.assistedinject.Assisted; import org.apache.commons.lang3.RandomStringUtils; @@ -37,8 +38,9 @@ public class DefaultPersonProvider implements PersonProvider { protected String nationalIdentityCardNumber; protected String nationalIdentificationNumber; protected String passportNumber; - protected final DataMaster dataMaster; + protected Country nationality; + protected final DataMaster dataMaster; protected final DateProducer dateProducer; protected final BaseProducer baseProducer; protected final NationalIdentificationNumberFactory nationalIdentificationNumberFactory; @@ -94,11 +96,12 @@ public Person get() { generateNationalIdentificationNumber(); generatePassportNumber(); generateAddress(); + generateNationality(); return new Person(firstName, middleName, lastName, address, email, username, password, sex, telephoneNumber, dateOfBirth, age, nationalIdentityCardNumber, nationalIdentificationNumber, passportNumber, - company, companyEmail); + company, companyEmail, nationality); } @Override @@ -247,6 +250,11 @@ public void generatePassportNumber() { passportNumber = passportNumberProvider.get(); } + private void generateNationality() { + List countries = Country.findCountryForLanguage(dataMaster.getLanguage()); + nationality = !countries.isEmpty() ? baseProducer.randomElement(countries) : Country.UnitedKingdom; + } + @Override public void setTelephoneNumberFormat(String telephoneFormat) { telephoneNumberFormat = telephoneFormat; diff --git a/src/main/java/com/devskiller/jfairy/producer/person/Person.java b/src/main/java/com/devskiller/jfairy/producer/person/Person.java index e380e44d..ff59887f 100644 --- a/src/main/java/com/devskiller/jfairy/producer/person/Person.java +++ b/src/main/java/com/devskiller/jfairy/producer/person/Person.java @@ -9,7 +9,7 @@ public class Person { - public static enum Sex { + public enum Sex { MALE, FEMALE } @@ -29,10 +29,12 @@ public static enum Sex { private final String nationalIdentityCardNumber; private final String nationalIdentificationNumber; private final String passportNumber; + private final Country nationality; public Person(String firstName, String middleName, String lastName, Address address, String email, String username, String password, Sex sex, String telephoneNumber, LocalDate dateOfBirth, Integer age, - String nationalIdentityCardNumber, String nationalIdentificationNumber, String passportNumber, Company company, String companyEmail) { + String nationalIdentityCardNumber, String nationalIdentificationNumber, String passportNumber, + Company company, String companyEmail, Country nationality) { this.nationalIdentityCardNumber = nationalIdentityCardNumber; this.address = address; this.firstName = firstName; @@ -49,6 +51,7 @@ public Person(String firstName, String middleName, String lastName, Address addr this.company = company; this.companyEmail = companyEmail; this.passportNumber = passportNumber; + this.nationality = nationality; } public String getNationalIdentificationNumber() { @@ -126,4 +129,8 @@ public Company getCompany() { public String getPassportNumber() { return passportNumber; } + + public Country getNationality() { + return nationality; + } } diff --git a/src/main/resources/jfairy.yml b/src/main/resources/jfairy.yml index 7e1c8a22..ac7dc69c 100644 --- a/src/main/resources/jfairy.yml +++ b/src/main/resources/jfairy.yml @@ -8,4 +8,4 @@ data: } loremIpsum: Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi. Cras vel lorem. Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam. Quisque semper justo at risus. Donec venenatis, turpis vel hendrerit interdum, dui ligula ultricies purus, sed posuere libero dui id orci. Nam congue, pede vitae dapibus aliquet, elit magna vulputate arcu, vel tempus metus leo non est. Etiam sit amet lectus quis est congue mollis. Phasellus congue lacus eget neque. Phasellus ornare, ante vitae consectetuer consequat, purus sapien ultricies dolor, et mollis pede metus eget nisi. Praesent sodales velit quis augue. Cras suscipit, urna at aliquam rhoncus, urna quam viverra nisi, in interdum massa nibh nec erat. - defaultCountryCode: 'PL' + language: 'PL' diff --git a/src/main/resources/jfairy_de.yml b/src/main/resources/jfairy_de.yml index b7d97b44..17cbab4e 100644 --- a/src/main/resources/jfairy_de.yml +++ b/src/main/resources/jfairy_de.yml @@ -17,4 +17,4 @@ data: personalEmails: [ gmail.com, yahoo.com, yahoo.de, gmx.de, gmx.com, web.de, t-online.de ] telephone_number_formats: ["0#########"] text: "Ihr naht euch wieder, schwankende Gestalten, Die früh sich einst dem trüben Blick gezeigt. Versuch ich wohl, euch diesmal festzuhalten? Fühl ich mein Herz noch jenem Wahn geneigt? Ihr drängt euch zu! nun gut, so mögt ihr walten, Wie ihr aus Dunst und Nebel um mich steigt; Mein Busen fühlt sich jugendlich erschüttert Vom Zauberhauch, der euren Zug umwittert. Ihr bringt mit euch die Bilder froher Tage, Und manche liebe Schatten steigen auf; Gleich einer alten, halbverklungnen Sage Kommt erste Lieb und Freundschaft mit herauf; Der Schmerz wird neu, es wiederholt die Klage Des Lebens labyrinthisch irren Lauf, Und nennt die Guten, die, um schöne Stunden Vom Glück getäuscht, vor mir hinweggeschwunden. Sie hören nicht die folgenden Gesänge, Die Seelen, denen ich die ersten sang; Zerstoben ist das freundliche Gedränge, Verklungen, ach! der erste Widerklang. Mein Lied ertönt der unbekannten Menge, Ihr Beifall selbst macht meinem Herzen bang, Und was sich sonst an meinem Lied erfreuet, Wenn es noch lebt, irrt in der Welt zerstreuet. Und mich ergreift ein längst entwöhntes Sehnen Nach jenem stillen, ernsten Geisterreich, Es schwebet nun in unbestimmten Tönen Mein lispelnd Lied, der Äolsharfe gleich, Ein Schauer faßt mich, Träne folgt den Tränen, Das strenge Herz, es fühlt sich mild und weich; Was ich besitze, seh ich wie im Weiten, Und was verschwand, wird mir zu Wirklichkeiten." - defaultCountryCode: 'DE' + language: 'DE' diff --git a/src/main/resources/jfairy_en.yml b/src/main/resources/jfairy_en.yml index e4a4b484..6fd46dac 100644 --- a/src/main/resources/jfairy_en.yml +++ b/src/main/resources/jfairy_en.yml @@ -26,4 +26,4 @@ data: country: ['Canada', 'USA', 'Poland'] - defaultCountryCode: 'GB' + language: 'EN' diff --git a/src/main/resources/jfairy_es.yml b/src/main/resources/jfairy_es.yml index 4b55d978..0e78a5a4 100644 --- a/src/main/resources/jfairy_es.yml +++ b/src/main/resources/jfairy_es.yml @@ -27,4 +27,4 @@ data: country: ['España'] #, 'Argentina', 'Uruguay', 'Mexico', 'Chile', 'Colombia', 'Venezuela', 'Cuba', 'Ecuador', 'Honduras', 'Perú'] - defaultCountryCode: 'ES' + language: 'ES' diff --git a/src/main/resources/jfairy_fr.yml b/src/main/resources/jfairy_fr.yml index 07335537..4be09821 100644 --- a/src/main/resources/jfairy_fr.yml +++ b/src/main/resources/jfairy_fr.yml @@ -18,3 +18,4 @@ data: telephone_number_formats: ["0#########"] text: "Quoique ce détail ne touche en aucune manière au fond même de ce que nous avons à raconter, il n’est peut-être pas inutile, ne fût-ce que pour être exact en tout, d’indiquer ici les bruits et les propos qui avaient couru sur son compte au moment où il était arrivé dans le diocèse. Vrai ou faux, ce qu’on dit des hommes tient souvent autant de place dans leur vie et souvent dans leur destinée que ce qu’ils font. M. Myriel était fils d’un conseiller au parlement d’Aix ; noblesse de robe. On contait que son père, le réservant pour hériter de sa charge, l’avait marié de fort bonne heure, à dix-huit ou vingt ans, suivant un usage assez répandu dans les familles parlementaires. Charles Myriel, nonobstant ce mariage, avait, disait-on, beaucoup fait parler de lui. Il était bien fait de sa personne, quoique d’assez petite taille, élégant, gracieux, spirituel ; toute la première partie de sa vie avait été donnée au monde et aux galanteries. La révolution survint, les événements se précipitèrent ; les familles parlementaires, décimées, chassées, traquées, se dispersèrent. M. Charles Myriel, dès les premiers jours de la révolution, émigra en Italie. Sa femme y mourut d’une maladie de poitrine dont elle était atteinte depuis longtemps. Ils n’avaient point d’enfants. Que se passa-t-il ensuite dans la destinée de M. Myriel ? L’écroulement de l’ancienne société française, la chute de sa propre famille, les tragiques spectacles de 93, plus effrayants encore peut-être pour les émigrés qui les voyaient de loin avec le grossissement de l’épouvante, firent-ils germer en lui des idées de renoncement et de solitude ? Fut-il, au milieu d’une de ces distractions et de ces affections qui occupaient sa vie, subitement atteint d’un de ces coups mystérieux et terribles qui viennent quelquefois renverser, en le frappant au cœur, l’homme que les catastrophes publiques n’ébranleraient pas en le frappant dans son existence et dans sa fortune ? Nul n’aurait pu le dire ; tout ce qu’on savait, c’est que, lorsqu’il revint d’Italie, il était prêtre.\n" defaultCountryCode: 'FR' + language: 'FR' diff --git a/src/main/resources/jfairy_it.yml b/src/main/resources/jfairy_it.yml index 5dc7b469..64713303 100644 --- a/src/main/resources/jfairy_it.yml +++ b/src/main/resources/jfairy_it.yml @@ -26,4 +26,4 @@ data: country: ['Italia', 'Francia', 'Germania', 'Spagna'] - defaultCountryCode: 'IT' + language: 'IT' diff --git a/src/main/resources/jfairy_ka.yml b/src/main/resources/jfairy_ka.yml index efdad7e8..b2aaae37 100644 --- a/src/main/resources/jfairy_ka.yml +++ b/src/main/resources/jfairy_ka.yml @@ -26,4 +26,4 @@ data: country: ['Georgia'] #'USA', 'Russia', 'Greece', 'Turkey' - defaultCountryCode: 'KA' + language: 'KA' diff --git a/src/main/resources/jfairy_pl.yml b/src/main/resources/jfairy_pl.yml index 9ec49208..deb9969e 100644 --- a/src/main/resources/jfairy_pl.yml +++ b/src/main/resources/jfairy_pl.yml @@ -27,4 +27,4 @@ data: country: ['Polska'] - defaultCountryCode: 'PL' + language: 'PL' diff --git a/src/main/resources/jfairy_sv.yml b/src/main/resources/jfairy_sv.yml index d3db19a2..4edf3b67 100644 --- a/src/main/resources/jfairy_sv.yml +++ b/src/main/resources/jfairy_sv.yml @@ -26,4 +26,4 @@ data: country: ['Sverige'] - defaultCountryCode: 'SV' + language: 'SV' diff --git a/src/main/resources/jfairy_zh.yml b/src/main/resources/jfairy_zh.yml index 6050e5c2..54098573 100644 --- a/src/main/resources/jfairy_zh.yml +++ b/src/main/resources/jfairy_zh.yml @@ -26,4 +26,4 @@ data: country: [中国, 美国, 法国, 英国, 德国, 西班牙, 意大利, 加拿大, 俄罗斯, 日本, 韩国, 巴西] - defaultCountryCode: 'TW' + language: 'ZH' diff --git a/src/test/groovy/com/devskiller/jfairy/producer/payment/IBANSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/payment/IBANSpec.groovy index 5031558b..2dbf0642 100644 --- a/src/test/groovy/com/devskiller/jfairy/producer/payment/IBANSpec.groovy +++ b/src/test/groovy/com/devskiller/jfairy/producer/payment/IBANSpec.groovy @@ -12,9 +12,10 @@ import com.devskiller.jfairy.producer.RandomGenerator class IBANSpec extends Specification { private DataMaster dataMaster + private BaseProducer baseProducer def setup() { - BaseProducer baseProducer = new BaseProducer(new RandomGenerator()) + baseProducer = new BaseProducer(new RandomGenerator()) dataMaster = new MapBasedDataMaster(baseProducer); dataMaster.readResources("jfairy_pl.yml") } @@ -28,7 +29,9 @@ class IBANSpec extends Specification { */ def "should return valid iban"() { when: - IBANProvider iban = new DefaultIBANProvider(dataMaster, + IBANProvider iban = new DefaultIBANProvider( + baseProducer, + dataMaster, IBANProperties.accountNumber("00234573201"), IBANProperties.country("AT") ); @@ -46,10 +49,9 @@ class IBANSpec extends Specification { PLkk bbbssssx cccccccccccccccc PL60 11401010 1111000234573201 */ - def "should return valid polish iban"() { when: - IBANProvider iban = new DefaultIBANProvider(dataMaster) + IBANProvider iban = new DefaultIBANProvider(baseProducer, dataMaster) then: IbanUtil.validate(iban.get().ibanNumber); } @@ -77,8 +79,8 @@ class IBANSpec extends Specification { def "should set proper country for according to selected language"() { when: - String number = Fairy.create(Locale.ENGLISH).iban().ibanNumber + String number = Fairy.create(new Locale('SV')).iban().ibanNumber then: - number.startsWith('GB') + number.startsWith('SE') } } diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/PersonSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/PersonSpec.groovy index f70ae16e..0ad165a3 100644 --- a/src/test/groovy/com/devskiller/jfairy/producer/person/PersonSpec.groovy +++ b/src/test/groovy/com/devskiller/jfairy/producer/person/PersonSpec.groovy @@ -11,6 +11,7 @@ import com.devskiller.jfairy.Bootstrap import com.devskiller.jfairy.Fairy import com.devskiller.jfairy.producer.company.Company import com.devskiller.jfairy.producer.person.locale.en.EnAddress +import com.devskiller.jfairy.producer.util.LanguageCode import static com.devskiller.jfairy.producer.person.PersonProperties.ageBetween import static com.devskiller.jfairy.producer.person.PersonProperties.female @@ -98,6 +99,13 @@ class PersonSpec extends Specification { person.age <= 3 } + def "should create nationality"() { + when: + Person person = fairy.person() + then: + Country.findCountryForLanguage(LanguageCode.EN)*.getCode().contains(person.nationality.code) + } + @Ignore("Properties are not cleaned") def "should create person older than 98 years"() { when: @@ -204,10 +212,10 @@ class PersonSpec extends Specification { def "should generate middle name only sometimes"() { given: List persons = [] - (1..100).each { persons.add(fairy.person()) } + (1..100).each {persons.add(fairy.person())} when: - List allWithoutMiddleName = persons.findAll { p -> p.middleName.isEmpty() } - List allWithMiddleName = persons.findAll { p -> !p.middleName.isEmpty() } + List allWithoutMiddleName = persons.findAll {p -> p.middleName.isEmpty()} + List allWithMiddleName = persons.findAll {p -> !p.middleName.isEmpty()} then: allWithoutMiddleName.size() > 0 allWithMiddleName.size() > 0 @@ -216,10 +224,10 @@ class PersonSpec extends Specification { def "should generate apartment number only sometimes"() { given: List persons = [] - (1..50).each { persons.add(fairy.person()) } + (1..50).each {persons.add(fairy.person())} when: - List allWithoutApartmentNumber = persons.findAll { p -> p.address.apartmentNumber.isEmpty() } - List allWithApartmentNumber = persons.findAll { p -> !p.address.apartmentNumber.isEmpty() } + List allWithoutApartmentNumber = persons.findAll {p -> p.address.apartmentNumber.isEmpty()} + List allWithApartmentNumber = persons.findAll {p -> !p.address.apartmentNumber.isEmpty()} then: allWithoutApartmentNumber.size() > 0 allWithApartmentNumber.size() > 0 diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/pl/PlPersonSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/pl/PlPersonSpec.groovy index 0687eaaa..e593beb2 100644 --- a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/pl/PlPersonSpec.groovy +++ b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/pl/PlPersonSpec.groovy @@ -14,6 +14,13 @@ class PlPersonSpec extends Specification { Bootstrap.create() } + def "should create nationality"() { + when: + Person person = fairy.person() + then: + person.nationality.code == 'PL' + } + def "should create nationalIdentificationNumber"() { when: Person person = fairy.person() diff --git a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/sv/SvPersonSpec.groovy b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/sv/SvPersonSpec.groovy index 6179a457..d000564e 100644 --- a/src/test/groovy/com/devskiller/jfairy/producer/person/locale/sv/SvPersonSpec.groovy +++ b/src/test/groovy/com/devskiller/jfairy/producer/person/locale/sv/SvPersonSpec.groovy @@ -14,6 +14,13 @@ class SvPersonSpec extends Specification { Bootstrap.create() } + def "should create nationality"() { + when: + Person person = fairy.person() + then: + person.nationality.code == 'SE' + } + def "should create nationalIdentificationNumber"() { when: Person person = fairy.person()