org.catools.common.facker.etl.CFakerResourceManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-randomize Show documentation
Show all versions of common-randomize Show documentation
The Common Randomize Implementation
package org.catools.common.facker.etl;
import com.mifmif.common.regex.Generex;
import org.catools.common.collections.CHashMap;
import org.catools.common.collections.CList;
import org.catools.common.collections.interfaces.CMap;
import org.catools.common.facker.exception.CFakerCountryNotFoundException;
import org.catools.common.facker.model.CRandomCities;
import org.catools.common.facker.model.CRandomCity;
import org.catools.common.facker.model.CRandomCountry;
import org.catools.common.facker.model.CRandomState;
import org.catools.common.facker.provider.*;
import org.catools.common.io.CResource;
import org.catools.common.text.CStringUtil;
/**
* To load exists data from Ruby Faker resources
*/
public class CFakerResourceManager {
public static synchronized CFakerCountryProvider getCountry(String countryCode3) {
CList lines = readResource("data/country_info.txt");
// remove header
lines.remove(0);
for (String line : lines) {
String[] vals = CStringUtil.split(line, "\t");
if (countryCode3.equalsIgnoreCase(vals[1])) {
return new CFakerCountryProvider(
new CRandomCountry(
vals[1], //ISO
vals[0], //ISO3
vals[2], //Country
vals.length > 3 ? vals[3] : "", //CurrencyCode
vals.length > 4 ? vals[4] : "", //CurrencyName
vals.length > 5 ? vals[5] : "", //Phone
vals.length > 6 ? vals[6] : "", //Postal Code Format
vals.length > 7 ? vals[7] : ""), //Postal Code Regex
getStateProviders(vals[0]),
getNameProvider(vals[0]),
getCompanyProvider(vals[0]),
getAddressProvider(vals[0])
);
}
}
throw new CFakerCountryNotFoundException(countryCode3);
}
private static CFakerStateProviders getStateProviders(String countryCode) {
CMap stateCitiesMap = getStateCitiesMap(countryCode);
CList lines = readResource("states.txt", countryCode);
// remove header
lines.remove(0);
return new CFakerStateProviders(
lines.mapToSet(s -> {
String[] vals = s.split("\t");
return new CFakerStateProvider(new CRandomState(vals[1], vals[0]), stateCitiesMap.get(vals[0]));
}));
}
private static CMap getStateCitiesMap(String countryCode) {
CList lines = readResource("cities.txt", countryCode)
.mapToList(l -> l.split("\t"))
.getAll(v -> v.length == 3);
// remove header
lines.remove(0);
CMap output = new CHashMap<>();
lines.forEach(vals -> {
String stateCode = vals[2];
output.putIfAbsent(stateCode, new CRandomCities());
output.get(stateCode).add(new CRandomCity(vals[1], vals[0]));
});
return output;
}
private static CFakerNameProvider getNameProvider(String countryCode) {
return new CFakerNameProvider(
readResource("male_name.txt", countryCode),
readResource("female_name.txt", countryCode),
readResource("male_middle_name.txt", countryCode),
readResource("female_middle_name.txt", countryCode),
readResource("male_surname.txt", countryCode),
readResource("female_surname.txt", countryCode),
readResource("male_prefix.txt", countryCode),
readResource("female_prefix.txt", countryCode),
readResource("male_suffix.txt", countryCode),
readResource("female_suffix.txt", countryCode));
}
private static CFakerStreetAddressProvider getAddressProvider(String countryCode) {
return new CFakerStreetAddressProvider(
readResource("street_name.txt", countryCode),
readResource("street_suffix.txt", countryCode),
readResource("street_prefix.txt", countryCode),
readResource("street_number_regex.txt", countryCode),
readResource("building_number_regex.txt", countryCode));
}
private static CFakerCompanyProvider getCompanyProvider(String countryCode) {
return new CFakerCompanyProvider(
readResource("company_name.txt", countryCode),
readResource("company_prefix.txt", countryCode),
readResource("company_suffix.txt", countryCode));
}
private static CList readResource(String resourceName, String countryCode) {
return readResource(String.format("data/%s/" + resourceName, countryCode.toLowerCase()));
}
private static CList readResource(String resourceFullName) {
String RESOURCE_MARKER = "<<";
String REGEX_MARKER = "@Reg=";
CList output = new CList<>();
for (String line : new CResource(resourceFullName.trim(), CFakerResourceManager.class).getLines(l -> CStringUtil.isNotBlank(l))) {
if (line.startsWith(RESOURCE_MARKER)) {
output.addAll(readResource(CStringUtil.substringAfter(line, RESOURCE_MARKER)));
} else if (line.startsWith(REGEX_MARKER)) {
new Generex(CStringUtil.substringAfter(line, REGEX_MARKER).trim()).getAllMatchedStrings().forEach(s -> output.add(s.trim()));
} else {
output.add(line.trim());
}
}
return output;
}
}