com.fitbur.jestify.junit.spring.IntegrationTestCreator.java Source code

Java tutorial

Introduction

Here is the source code for com.fitbur.jestify.junit.spring.IntegrationTestCreator.java

Source

/*
 * Copyright 2015 Sharmarke Aden <saden1 at gmail.org>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fitbur.jestify.junit.spring;

import com.fitbur.jestify.core.TestContext;
import com.fitbur.jestify.core.TestReifier;
import com.fitbur.jestify.core.descriptor.DescriptorKey;
import com.fitbur.jestify.core.descriptor.FieldDescriptor;
import com.fitbur.jestify.core.descriptor.ParameterDescriptor;
import com.fitbur.jestify.junit.spring.injector.IndexMockInjector;
import com.fitbur.jestify.junit.spring.injector.NameMockInjector;
import com.fitbur.jestify.junit.spring.injector.RealInjector;
import com.fitbur.jestify.junit.spring.injector.TypeMockInjector;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import static java.util.stream.Collectors.toSet;
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 *
 * @author Sharmarke Aden <saden1 at gmail.org>
 */
class IntegrationTestCreator {

    private final TestContext context;
    private final TestReifier testReifier;
    private final AnnotationConfigApplicationContext appContext;

    IntegrationTestCreator(TestContext context, TestReifier testReifier,
            AnnotationConfigApplicationContext appContext) {
        this.context = context;
        this.testReifier = testReifier;
        this.appContext = appContext;
    }

    void create() {
        Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors();
        Map<DescriptorKey, FieldDescriptor> fieldDescriptors = context.getFieldDescriptors();
        Object[] arguments = new Object[parameterDescriptors.size()];
        Collection<FieldDescriptor> descriptors = fieldDescriptors.values();

        Set<FieldDescriptor> mockDescriptors = descriptors.parallelStream().filter(p -> p.getMock().isPresent())
                .collect(toSet());

        //process fields with a custom index first
        mockDescriptors.parallelStream().filter(p -> p.getMock().get().index() != -1)
                .map(p -> new IndexMockInjector(context, testReifier, appContext, p, arguments))
                .forEach(IndexMockInjector::inject);

        //process fields with custom names second
        mockDescriptors.parallelStream().filter(p -> !p.getMock().get().name().isEmpty())
                .map(p -> new NameMockInjector(context, testReifier, appContext, p, arguments))
                .forEach(NameMockInjector::inject);

        //process fields with type based injection
        mockDescriptors.parallelStream()
                .filter(p -> p.getMock().get().index() == -1 && p.getMock().get().name().isEmpty())
                .map(p -> new TypeMockInjector(context, testReifier, appContext, p, arguments))
                .forEach(TypeMockInjector::inject);

        //process fields with custom names second
        descriptors.parallelStream().filter(p -> !p.getMock().isPresent()
                && (p.getAnnotation(Inject.class).isPresent() || p.getAnnotation(Autowired.class).isPresent()))
                .map(p -> new RealInjector(context, testReifier, appContext, p, arguments))
                .forEach(RealInjector::inject);

        testReifier.reifyCut(context.getCutDescriptor(), arguments);
    }

}