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

io.sphere.sdk.queries.QueryIntegrationTest Maven / Gradle / Ivy

The newest version!
package io.sphere.sdk.queries;

import io.sphere.sdk.models.Versioned;
import io.sphere.sdk.client.SphereRequest;
import io.sphere.sdk.test.IntegrationTest;
import io.sphere.sdk.utils.IterableUtils;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.IntStream;

import com.github.slugify.Slugify;

import static java.util.stream.Collectors.*;
import static org.fest.assertions.Assertions.assertThat;

import static io.sphere.sdk.utils.SphereInternalLogger.*;

/**
 * A base class concerning queries. It is only applicable if the model can be created by a (possible unique) name.
 * @param  the type of the resource
 */
public abstract class QueryIntegrationTest> extends IntegrationTest {

    @Test
    public void queryByNameScenario() {
        assertModelsNotPresent();
        assertModelsInBackend();
        final String nameToFind = modelNames().get(1);
        final List results = queryByName(nameToFind).getResults();
        assertThat(results).hasSize(1);
        assertThat(getNames(results)).containsExactly(nameToFind);
        assertModelsNotPresent();
    }

    protected void withByName(final String name, final Consumer consumer) {
        cleanUpByName(name);
        final T instance = createInBackendByName(name);
        consumer.accept(instance);
        cleanUpByName(name);
    }

    /**
     * Removes all items with the name in {@code names}
     * Should not throw exceptions if the elements are not existing.
     * @param names the names of the items to delete
     */
    protected void cleanUpByName(final List names){
        queryByName(names).getResults().forEach(item -> delete(item));
    }

    protected void cleanUpByName(final String name) {
        cleanUpByName(Arrays.asList(name));
    }

    protected void delete(T item) {
        try {
            execute(deleteCommand(item));
        } catch (final Exception e) {
            getLogger("test.fixtures").warn(() -> String.format("tried to delete %s but an Exception occurred: %s", item, e.toString()));
        }
    }

    protected abstract SphereRequest deleteCommand(T item);

    protected List createInBackendByName(final List names) {
        return names.stream().map(name -> execute(newCreateCommandForName(name))).collect(toList());
    }

    protected T createInBackendByName(final String name) {
        return createInBackendByName(Arrays.asList(name)).get(0);
    }

    protected abstract SphereRequest newCreateCommandForName(String name);

    protected abstract String extractName(final T instance);

    protected PagedQueryResult queryAll() {
        return execute(queryRequestForQueryAll());
    }

    protected abstract SphereRequest> queryRequestForQueryAll();

    protected PagedQueryResult queryByName(final String name) {
        return execute(queryObjectForName(name));
    }

    protected abstract SphereRequest> queryObjectForName(final String name);

    protected PagedQueryResult queryByName(final List names) {
        return execute(queryObjectForNames(names));
    }

    protected abstract SphereRequest> queryObjectForNames(List names);

    private String sluggedClassName() {
        final String className = this.getClass().toString();
        return new Slugify().slugify(className);
    }

    protected List modelNames(){
        return IntStream.of(1, 2, 3).mapToObj(i -> sluggedClassName() + i).collect(toList());
    }

    protected List getNames(final List elements) {
        return elements.stream().map(o -> extractName(o)).collect(toList());
    }

    private void assertModelsInBackend() {
        final List instances = createInBackendByName(modelNames());
        final List actualNames = instances.stream().map(o -> extractName(o)).
                filter(name -> modelNames().contains(name)).sorted().collect(toList());
        assertThat(actualNames).
                overridingErrorMessage(String.format("The test requires instances with the names %s.", IterableUtils.toString(modelNames()))).
                isEqualTo(modelNames());
    }

    private void assertModelsNotPresent() {
        cleanUpByName(modelNames());
        assertThat(getNames(queryAll().getResults())).overridingErrorMessage("the instances with the names " + modelNames() + " should not be present.").excludes(modelNames());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy