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

io.codearte.accurest.stubrunner.spring.StubRunnerConfiguration Maven / Gradle / Ivy

package io.codearte.accurest.stubrunner.spring;

import io.codearte.accurest.messaging.AccurestMessaging;
import io.codearte.accurest.messaging.noop.NoOpAccurestMessaging;
import io.codearte.accurest.stubrunner.AetherStubDownloader;
import io.codearte.accurest.stubrunner.BatchStubRunner;
import io.codearte.accurest.stubrunner.BatchStubRunnerFactory;
import io.codearte.accurest.stubrunner.StubDownloader;
import io.codearte.accurest.stubrunner.StubRunner;
import io.codearte.accurest.stubrunner.StubRunnerOptions;
import io.codearte.accurest.stubrunner.StubRunnerOptionsBuilder;
import io.codearte.accurest.stubrunner.StubRunning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.IOException;

/**
 * Configuration that initializes a {@link BatchStubRunner} that runs {@link StubRunner} instance for each stub
 */
@Configuration
public class StubRunnerConfiguration {

	@Autowired(required = false) AccurestMessaging accurestMessaging;
	@Autowired(required = false) StubDownloader stubDownloader;

	/**
	 * Bean that initializes stub runners, runs them and on shutdown closes them. Upon its instantiation
	 * JAR with stubs is downloaded and unpacked to a temporary folder and WireMock server are started
	 * for each of those stubs
	 *
	 * @param minPortValue       min port value of the WireMock instance for stubs
	 * @param maxPortValue       max port value of the WireMock instance for stubs
	 * @param stubRepositoryRoot root URL from where the JAR with stub mappings will be downloaded
	 * @param stubsSuffix        classifier for the jar containing stubs
	 * @param workOffline        forces offline work
	 * @param stubs              comma separated list of stubs presented in Ivy notation
	 */
	@Bean(destroyMethod = "close")
	public StubRunning batchStubRunner(
			@Value("${stubrunner.port.range.min:10000}") Integer minPortValue,
			@Value("${stubrunner.port.range.max:15000}") Integer maxPortValue,
			@Value("${stubrunner.stubs.repository.root:}") Resource stubRepositoryRoot,
			@Value("${stubrunner.stubs.classifier:stubs}") String stubsSuffix,
			@Value("${stubrunner.work-offline:false}") boolean workOffline,
			@Value("${stubrunner.stubs.ids:}") String stubs) throws IOException {
		StubRunnerOptions stubRunnerOptions = new StubRunnerOptionsBuilder()
				.withMinMaxPort(minPortValue, maxPortValue)
				.withStubRepositoryRoot(uriStringOrEmpty(stubRepositoryRoot))
				.withWorkOffline(stubRepositoryRoot == null || workOffline)
				.withStubsClassifier(stubsSuffix)
				.withStubs(stubs)
				.build();
		BatchStubRunner batchStubRunner = new BatchStubRunnerFactory(stubRunnerOptions,
				stubDownloader != null ? stubDownloader : new AetherStubDownloader(stubRunnerOptions),
				accurestMessaging != null ? accurestMessaging :  new NoOpAccurestMessaging()).buildBatchStubRunner();
		// TODO: Consider running it in a separate thread
		batchStubRunner.runStubs();
		return batchStubRunner;
	}

	private String uriStringOrEmpty(Resource stubRepositoryRoot) throws IOException {
		return stubRepositoryRoot != null ? stubRepositoryRoot.getURI().toString() : "";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy