TransactionCompositionTest.java
sdk-tools/samples/custom-services/src/test/java/example/technical/TransactionCompositionTest.java
1package example.technical;23import com.servpp.generator.GeneratorUtil;4import com.servpp.sdk.JavaProjectWriter;5import java.io.File;6import java.nio.file.Files;7import java.nio.file.Path;8import java.util.ArrayList;9import java.util.List;10import javax.tools.ToolProvider;11import org.junit.Rule;12import org.junit.Test;13import org.junit.rules.TemporaryFolder;14import org.junit.runner.RunWith;15import org.junit.runners.Parameterized;16import static org.junit.Assert.assertEquals;1718@RunWith(Parameterized.class)19public class TransactionCompositionTest {20@Parameterized.Parameters(name = "{0}")21public static List<String> variants() {22return List.of("flat", "interleaved", "nested", "serial");23}2425@Rule public TemporaryFolder temporary = new TemporaryFolder();2627private final String variant;2829public TransactionCompositionTest(String variant) {30this.variant = variant;31}3233@Test34public void generatedScenarioCommitsAndRollsBackOnH2() throws Exception {35Path fixtures = Path.of("src/test/composition");36Path project = temporary.newFolder().toPath();37Path sources = project.resolve("src/main/java");38Path openApi = project.resolve("openapi");39String previousEnvironment = System.getProperty("spp.environment");40System.setProperty("spp.environment", "testing");41try {42GeneratorUtil.execute(fixtures.resolve(variant).toFile(), openApi.toFile(), sources.toFile(), "com.example.cv1");43new JavaProjectWriter().write(project, "com.example.cv1");44} finally {45if (previousEnvironment == null) System.clearProperty("spp.environment");46else System.setProperty("spp.environment", previousEnvironment);47}4849Path classes = Files.createDirectory(project.resolve("classes"));50String testClasspath = System.getProperty("surefire.test.class.path");51var options = new ArrayList<>(List.of("-classpath", testClasspath,52"-d", classes.toString(), fixtures.resolve("ComposeTest.java").toString()));53try (var files = Files.walk(sources)) {54files.filter(path -> path.toString().endsWith(".java")).map(Path::toString).sorted().forEach(options::add);55}56assertEquals("Generated Java compilation for " + variant, 0, ToolProvider.getSystemJavaCompiler()57.run(null, null, null, options.toArray(String[]::new)));58// Each generated application owns the APP catalogs and the runtime singletons.59String classpath = String.join(File.pathSeparator, classes.toString(),60project.resolve("src/main/resources").toString(), testClasspath);61var process = new ProcessBuilder(Path.of(System.getProperty("java.home"), "bin", "java").toString(),62"-classpath", classpath, "com.example.cv1.ComposeTest", variant).redirectErrorStream(true).start();63String output = new String(process.getInputStream().readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);64assertEquals("Generated H2 application for " + variant + "\n" + output, 0, process.waitFor());65}66}