All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.sdk.test.SphereTestUtils Maven / Gradle / Ivy

There is a newer version: 1.0.0-M12
Show newest version
package io.sphere.sdk.test;

import com.neovisionaries.i18n.CountryCode;
import io.sphere.sdk.models.*;
import io.sphere.sdk.queries.PagedQueryResult;
import io.sphere.sdk.utils.MoneyImpl;

import javax.money.CurrencyUnit;
import javax.money.MonetaryAmount;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;

import static io.sphere.sdk.utils.IterableUtils.toStream;
import static java.util.stream.Collectors.toList;

public final class SphereTestUtils {
    public static final int MASTER_VARIANT_ID = 1;
    private static final Random random = new Random();

    private SphereTestUtils() {
        //pure utility class
    }

    public static final Locale GERMAN = Locale.GERMAN;
    public static final Locale ENGLISH = Locale.ENGLISH;

    public static final CountryCode DE = CountryCode.DE;
    public static final CountryCode GB = CountryCode.GB;
    public static final CountryCode US = CountryCode.US;

    public static final CurrencyUnit EUR = DefaultCurrencyUnits.EUR;
    public static final CurrencyUnit USD = DefaultCurrencyUnits.USD;
    public static final MonetaryAmount EURO_10 = MoneyImpl.of(10, EUR);

    /**
     * Creates a LocalizedStrings for the {@code Locale.ENGLISH}.
     * @param value the value of the english translation
     * @return localized string with value
     */
    public static LocalizedStrings en(final String value) {
        return LocalizedStrings.of(Locale.ENGLISH, value);
    }

    public static String en(final Optional localizedStringsOption) {
        return localizedStringsOption.get().get(ENGLISH).get();
    }

    public static String englishSlugOf(final WithLocalizedSlug model) {
        return model.getSlug().get(ENGLISH).get();
    }

    public static  T firstOf(final PagedQueryResult result) {
        return result.head().get();
    }

    public static LocalizedStrings randomSlug() {
        return LocalizedStrings.of(Locale.ENGLISH, randomKey());
    }

    public static Address randomAddress() {
        return AddressBuilder.of(CountryCode.DE).build();
    }


    public static String randomKey() {
        return  "random-slug-" + random.nextInt();
    }

    public static String randomEmail(final Class clazz) {
        return  "random-email-" + random.nextInt() + "-" + clazz.getSimpleName() + "@test.commercetools.de";
    }

    public static String randomString() {
        return "random string " + random.nextInt() + System.currentTimeMillis();
    }

    public static MetaAttributes randomMetaAttributes() {
        final String metaTitle = "meta title" + randomString();
        final String metaDescription = "meta description" + randomString();
        final String metaKeywords = "meta keywords," + randomString();
        return MetaAttributes.metaAttributesOf(ENGLISH, metaTitle, metaDescription, metaKeywords);
    }

    public static  List toIds(final Iterable> elements) {
        return toStream(elements).map(element -> element.getId()).collect(toList());
    }

    @SafeVarargs
    @SuppressWarnings("varargs")
    public static  List asList(T... a) {
        return Arrays.asList(a);
    }

    public static   T oneOf(final Set set) {
        return set.iterator().next();
    }

    public static  Function consumerToFunction(final Consumer consumer) {
        return x -> {
            consumer.accept(x);
            return x;
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy