|
| 1 | +package com.github.dreamhead.moco.junit5; |
| 2 | + |
| 3 | +import com.github.dreamhead.moco.HttpServer; |
| 4 | +import com.github.dreamhead.moco.HttpsCertificate; |
| 5 | +import com.github.dreamhead.moco.Runner; |
| 6 | +import com.github.dreamhead.moco.resource.ContentResource; |
| 7 | +import com.github.dreamhead.moco.resource.Resource; |
| 8 | +import com.google.common.base.Strings; |
| 9 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 10 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 11 | +import org.junit.jupiter.api.extension.Extension; |
| 12 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 13 | +import org.junit.jupiter.api.extension.TestInstancePostProcessor; |
| 14 | + |
| 15 | +import static com.github.dreamhead.moco.Moco.file; |
| 16 | +import static com.github.dreamhead.moco.Moco.pathResource; |
| 17 | +import static com.github.dreamhead.moco.MocoJsonRunner.jsonHttpServer; |
| 18 | +import static com.github.dreamhead.moco.MocoJsonRunner.jsonHttpsServer; |
| 19 | +import static com.github.dreamhead.moco.Runner.runner; |
| 20 | +import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.create; |
| 21 | + |
| 22 | +public class MocoJunit5Extension implements TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, Extension { |
| 23 | + private static final ExtensionContext.Namespace MOCO = create("com.github.dreamhead.moco.junit5"); |
| 24 | + private static final String SERVER = "server"; |
| 25 | + |
| 26 | + @Override |
| 27 | + public void afterEach(final ExtensionContext context) throws Exception { |
| 28 | + Runner runner = context.getStore(MOCO).get(SERVER, Runner.class); |
| 29 | + runner.stop(); |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void beforeEach(final ExtensionContext context) throws Exception { |
| 35 | + Runner runner = context.getStore(MOCO).get(SERVER, Runner.class); |
| 36 | + if (runner == null) { |
| 37 | + throw new IllegalStateException("No Moco server found. Please check if @MocoConfiguration is added."); |
| 38 | + } |
| 39 | + |
| 40 | + runner.start(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void postProcessTestInstance(final Object testInstance, final ExtensionContext context) throws Exception { |
| 45 | + Class<?> testInstanceClass = testInstance.getClass(); |
| 46 | + MocoConfiguration configuration = testInstanceClass.getAnnotation(MocoConfiguration.class); |
| 47 | + MocoCertificate certificate = testInstanceClass.getAnnotation(MocoCertificate.class); |
| 48 | + |
| 49 | + if (configuration == null) { |
| 50 | + throw new IllegalStateException("No Moco server found. Please check if @MocoConfiguration is added."); |
| 51 | + } |
| 52 | + |
| 53 | + context.getStore(MOCO).put(SERVER, runner(newServer(configuration, certificate))); |
| 54 | + } |
| 55 | + |
| 56 | + private HttpServer newServer(final MocoConfiguration configuration, final MocoCertificate certificate) { |
| 57 | + Resource resource = getResource(configuration); |
| 58 | + int port = configuration.port(); |
| 59 | + |
| 60 | + if (certificate != null) { |
| 61 | + return jsonHttpsServer(port, resource, newCertificate(certificate)); |
| 62 | + } |
| 63 | + |
| 64 | + return jsonHttpServer(port, resource); |
| 65 | + } |
| 66 | + |
| 67 | + private HttpsCertificate newCertificate(final MocoCertificate certificate) { |
| 68 | + return HttpsCertificate.certificate(getResource(certificate), certificate.keyStorePassword(), certificate.certPassword()); |
| 69 | + } |
| 70 | + |
| 71 | + private ContentResource getResource(final MocoConfiguration configuration) { |
| 72 | + return getResource(configuration.filepath(), configuration.classpath()); |
| 73 | + } |
| 74 | + |
| 75 | + private ContentResource getResource(final MocoCertificate certificate) { |
| 76 | + return getResource(certificate.filepath(), certificate.classpath()); |
| 77 | + } |
| 78 | + |
| 79 | + private static ContentResource getResource(final String filepath, final String classpath) { |
| 80 | + if (!Strings.isNullOrEmpty(filepath)) { |
| 81 | + return file(filepath); |
| 82 | + } |
| 83 | + |
| 84 | + if (!Strings.isNullOrEmpty(classpath)) { |
| 85 | + return pathResource(classpath); |
| 86 | + } |
| 87 | + |
| 88 | + throw new IllegalArgumentException("No configuration found"); |
| 89 | + } |
| 90 | +} |
0 commit comments