com.turbospaces.boot.test.AbstractSpringBootTestContextBootstrapper Maven / Gradle / Ivy
package com.turbospaces.boot.test;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.SpringBootContextLoader;
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.springframework.kafka.test.EmbeddedKafkaZKBroker;
import org.springframework.kafka.test.core.BrokerAddress;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextLoader;
import com.google.common.collect.Iterables;
import com.google.common.net.HostAndPort;
import com.turbospaces.boot.AbstractBootstrap;
import com.turbospaces.cfg.ApplicationProperties;
import com.turbospaces.ups.KafkaServiceInfo;
import com.turbospaces.ups.RawServiceInfo;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class AbstractSpringBootTestContextBootstrapper extends SpringBootTestContextBootstrapper {
static {
System.setProperty("ebean.registerShutdownHook", Boolean.FALSE.toString());
}
@Override
protected ContextLoader resolveContextLoader(Class> testClass, List configAttributesList) {
return new SpringBootContextLoader() {
@Override
protected SpringApplication getSpringApplication() {
try {
return new ConfigurableApplicationBootstrap(createProps());
} catch (Throwable err) {
log.error(err.getMessage(), err);
return super.getSpringApplication();
}
}
};
}
protected abstract T createProps() throws Exception;
public static RawServiceInfo genRawServiceInfo(String id) throws Exception {
byte[] data = new byte[64];
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.nextBytes(data);
return new RawServiceInfo(id, data);
}
private static class ConfigurableApplicationBootstrap extends AbstractBootstrap {
protected ConfigurableApplicationBootstrap(T props) throws Throwable {
super(props);
}
@Override
protected void applyInitializers(ConfigurableApplicationContext context) {
super.applyInitializers(context);
if (context.containsBean(EmbeddedKafkaBroker.BEAN_NAME)) {
EmbeddedKafkaZKBroker broker = (EmbeddedKafkaZKBroker) context.getBeanFactory().getSingleton(EmbeddedKafkaBroker.BEAN_NAME);
BrokerAddress address = Iterables.getOnlyElement(Arrays.asList(broker.getBrokerAddresses()));
KafkaServiceInfo ksi = new KafkaServiceInfo(HostAndPort.fromParts(address.getHost(), address.getPort()));
addUps(ksi);
}
}
}
}