001/*
002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.cuioss.test.generator.domain;
017
018import de.cuioss.test.generator.TypedGenerator;
019
020import java.util.logging.Level;
021import java.util.logging.Logger;
022
023import static de.cuioss.test.generator.Generators.fixedValues;
024
025/**
026 * Generates syntactically valid email addresses for testing purposes.
027 * The generator creates email addresses in the format: firstname.lastname@domain.tld
028 *
029 * <ul>
030 *   <li>First and last names are taken from {@link NameGenerators#FIRSTNAMES_ANY_ENGLISH}
031 *       and {@link NameGenerators#FAMILY_NAMES_ENGLISH}</li>
032 *   <li>Domains include: email, mail, cuioss, message, example, hospital</li>
033 *   <li>TLDs include: de, org, com, net</li>
034 * </ul>
035 *
036 * <p><em>Example usage:</em></p>
037 * <pre>
038 * var generator = new EmailGenerator();
039 * String email = generator.next(); // e.g. "john.doe@mail.com"
040 *
041 * // Or create email directly from names
042 * String email = EmailGenerator.createEmail("john", "doe"); // e.g. "john.doe@example.org"
043 * </pre>
044 *
045 * @author Oliver Wolff
046 */
047public class EmailGenerator implements TypedGenerator<String> {
048
049    private static final Logger LOGGER = Logger.getLogger(EmailGenerator.class.getName());
050
051    private final TypedGenerator<String> firstNames = NameGenerators.FIRSTNAMES_ANY_ENGLISH.generator();
052    private final TypedGenerator<String> familyNames = NameGenerators.FAMILY_NAMES_ENGLISH.generator();
053
054    private static final TypedGenerator<String> TLDS = fixedValues("de", "org", "com", "net");
055    private static final TypedGenerator<String> DOMAINS = fixedValues("email", "mail", "cuioss", "message", "example",
056            "hospital");
057
058    @Override
059    public String next() {
060        return createEmail(firstNames.next(), familyNames.next());
061    }
062
063    /**
064     * Creates an email address from given first and last names.
065     * All components are converted to lowercase.
066     *
067     * @param firstname The person's first name, must not be null or empty
068     * @param lastname  The person's last name, must not be null or empty
069     * @return An email address in the format firstname.lastname@domain.tld.
070     * Returns "invalid.email@example.com" if either name component is null or empty.
071     */
072    public static String createEmail(final String firstname, final String lastname) {
073        if (firstname == null || firstname.isBlank() || lastname == null || lastname.isBlank()) {
074            LOGGER.log(Level.WARNING, "Invalid name components for email generation: firstname=''{0}'', lastname=''{1}''", new Object[]{firstname, lastname});
075            return "invalid.email@example.com";
076        }
077        var tld = TLDS.next();
078        var email = (firstname + "." + lastname + "@" + DOMAINS.next()).toLowerCase();
079        var fullEmail = email + '.' + tld;
080        LOGGER.log(Level.FINE, "Generated email: {0}", fullEmail);
081        return fullEmail;
082    }
083
084}