|
| 1 | +package em.embedded.com.example.spring; |
| 2 | + |
| 3 | +import com.sw.project.ProjectApiApplication; |
| 4 | +import org.evomaster.client.java.controller.EmbeddedSutController; |
| 5 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 6 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 7 | +import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto; |
| 8 | +import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType; |
| 9 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 10 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 11 | +import org.evomaster.client.java.sql.DbSpecification; |
| 12 | +import org.springframework.boot.SpringApplication; |
| 13 | +import org.springframework.context.ConfigurableApplicationContext; |
| 14 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 15 | +import org.testcontainers.containers.GenericContainer; |
| 16 | + |
| 17 | +import java.sql.Connection; |
| 18 | +import java.sql.SQLException; |
| 19 | +import java.util.*; |
| 20 | + |
| 21 | +public class EmbeddedEvoMasterController extends EmbeddedSutController { |
| 22 | + |
| 23 | + private static final GenericContainer mysql = new GenericContainer("mysql:8.0" ) |
| 24 | + .withEnv(new HashMap<String, String>(){{ |
| 25 | + put("MYSQL_ROOT_PASSWORD", "root"); |
| 26 | + put("MYSQL_DATABASE", "example"); |
| 27 | + }}) |
| 28 | + .withExposedPorts(3306) |
| 29 | + .withTmpFs(Collections.singletonMap("/var/lib/mysql", "rw")); |
| 30 | + |
| 31 | + private ConfigurableApplicationContext ctx; |
| 32 | + |
| 33 | + private Connection sqlConnection; |
| 34 | + private List<DbSpecification> dbSpecification; |
| 35 | + |
| 36 | + |
| 37 | + public EmbeddedEvoMasterController() { |
| 38 | + this(40100); |
| 39 | + } |
| 40 | + |
| 41 | + public EmbeddedEvoMasterController(int port) { |
| 42 | + setControllerPort(port); |
| 43 | + } |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + int port = 40100; |
| 47 | + if (args.length > 0) { |
| 48 | + port = Integer.parseInt(args[0]); |
| 49 | + } |
| 50 | + |
| 51 | + EmbeddedEvoMasterController controller = new EmbeddedEvoMasterController(port); |
| 52 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 53 | + |
| 54 | + starter.start(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean isSutRunning() { |
| 59 | + return ctx!=null && ctx.isRunning(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String getPackagePrefixesToCover() { |
| 64 | + return "com.sw.project."; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public ProblemInfo getProblemInfo() { |
| 74 | + return new RestProblem( |
| 75 | + "http://localhost:" + getSutPort() + "/v3/api-docs", |
| 76 | + null |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 82 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String startSut() { |
| 87 | + mysql.start(); |
| 88 | + |
| 89 | + String host = mysql.getContainerIpAddress(); |
| 90 | + int port = mysql.getMappedPort(3306); |
| 91 | + String url = "jdbc:mysql://"+host+":"+port+"/example?useSSL=false&allowPublicKeyRetrieval=true"; |
| 92 | + |
| 93 | + ctx = SpringApplication.run(ProjectApiApplication.class, new String[]{ |
| 94 | + "--server.port=0", |
| 95 | + "--spring.datasource.url="+url, |
| 96 | + "--spring.datasource.username=root", |
| 97 | + "--spring.datasource.password=root", |
| 98 | + "--management.server.port=-1", |
| 99 | + "--logging.level.root=OFF", |
| 100 | + "--logging.level.org.springframework=INFO" |
| 101 | + }); |
| 102 | + |
| 103 | + if (sqlConnection != null) { |
| 104 | + try { |
| 105 | + sqlConnection.close(); |
| 106 | + } catch (SQLException e) { |
| 107 | + throw new RuntimeException(e); |
| 108 | + } |
| 109 | + } |
| 110 | + JdbcTemplate jdbc = ctx.getBean(JdbcTemplate.class); |
| 111 | + try { |
| 112 | + sqlConnection = jdbc.getDataSource().getConnection(); |
| 113 | + } catch (SQLException e) { |
| 114 | + throw new RuntimeException(e); |
| 115 | + } |
| 116 | + |
| 117 | + dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.MYSQL,sqlConnection)); |
| 118 | + |
| 119 | + return "http://localhost:" + getSutPort(); |
| 120 | + } |
| 121 | + |
| 122 | + protected int getSutPort() { |
| 123 | + return (Integer) ((Map) ctx.getEnvironment() |
| 124 | + .getPropertySources().get("server.ports").getSource()) |
| 125 | + .get("local.server.port"); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void stopSut() { |
| 130 | + ctx.stop(); |
| 131 | + mysql.stop(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void resetStateOfSUT() { |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public List<DbSpecification> getDbSpecifications() { |
| 140 | + return dbSpecification; |
| 141 | + } |
| 142 | +} |
0 commit comments