|
| 1 | +package em.embedded.market; |
| 2 | + |
| 3 | + |
| 4 | +import org.evomaster.client.java.controller.AuthUtils; |
| 5 | +import org.evomaster.client.java.controller.EmbeddedSutController; |
| 6 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 7 | +import org.evomaster.client.java.controller.api.dto.AuthenticationDto; |
| 8 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 9 | +import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType; |
| 10 | +import org.evomaster.client.java.controller.db.DbCleaner; |
| 11 | +import org.evomaster.client.java.controller.db.SqlScriptRunner; |
| 12 | +import org.evomaster.client.java.controller.db.SqlScriptRunnerCached; |
| 13 | +import org.evomaster.client.java.controller.internal.db.DbSpecification; |
| 14 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 15 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 16 | +import org.springframework.boot.SpringApplication; |
| 17 | +import org.springframework.context.ConfigurableApplicationContext; |
| 18 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 19 | + |
| 20 | +import java.sql.Connection; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Scanner; |
| 26 | + |
| 27 | +/** |
| 28 | + * Class used to start/stop the SUT. This will be controller by the EvoMaster process |
| 29 | + */ |
| 30 | +public class EmbeddedEvoMasterController extends EmbeddedSutController { |
| 31 | + |
| 32 | + FIXME |
| 33 | + public static void main(String[] args) { |
| 34 | + |
| 35 | + int port = 40100; |
| 36 | + if (args.length > 0) { |
| 37 | + port = Integer.parseInt(args[0]); |
| 38 | + } |
| 39 | + |
| 40 | + EmbeddedEvoMasterController controller = new EmbeddedEvoMasterController(port); |
| 41 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 42 | + |
| 43 | + starter.start(); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + private ConfigurableApplicationContext ctx; |
| 48 | + private Connection sqlConnection; |
| 49 | + private List<DbSpecification> dbSpecification; |
| 50 | + |
| 51 | + |
| 52 | + public EmbeddedEvoMasterController() { |
| 53 | + this(40100); |
| 54 | + } |
| 55 | + |
| 56 | + public EmbeddedEvoMasterController(int port) { |
| 57 | + setControllerPort(port); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String startSut() { |
| 62 | + |
| 63 | + ctx = SpringApplication.run(market.RestApplication.class, new String[]{ |
| 64 | + "--server.port=0", |
| 65 | + "--spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;" |
| 66 | + }); |
| 67 | + |
| 68 | + if (sqlConnection != null) { |
| 69 | + try { |
| 70 | + sqlConnection.close(); |
| 71 | + } catch (SQLException e) { |
| 72 | + throw new RuntimeException(e); |
| 73 | + } |
| 74 | + } |
| 75 | + JdbcTemplate jdbc = ctx.getBean(JdbcTemplate.class);try { |
| 76 | + sqlConnection = jdbc.getDataSource().getConnection(); |
| 77 | + } catch (SQLException e) { |
| 78 | + throw new RuntimeException(e); |
| 79 | + } |
| 80 | + |
| 81 | + dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.H2,sqlConnection) |
| 82 | + //.withInitSqlOnResourcePath("/data.sql")); |
| 83 | + .withDisabledSmartClean()); |
| 84 | + |
| 85 | + return "http://localhost:" + getSutPort(); |
| 86 | + } |
| 87 | + |
| 88 | + protected int getSutPort() { |
| 89 | + return (Integer) ((Map) ctx.getEnvironment() |
| 90 | + .getPropertySources().get("server.ports").getSource()) |
| 91 | + .get("local.server.port"); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean isSutRunning() { |
| 97 | + return ctx != null && ctx.isRunning(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void stopSut() { |
| 102 | + ctx.stop(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getPackagePrefixesToCover() { |
| 107 | + return "market."; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void resetStateOfSUT() { |
| 112 | + DbCleaner.clearDatabase_H2(sqlConnection, null); |
| 113 | + SqlScriptRunnerCached.runScriptFromResourceFile(sqlConnection,"/data.sql"); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public ProblemInfo getProblemInfo() { |
| 118 | + |
| 119 | + return new RestProblem( |
| 120 | + "http://localhost:" + getSutPort() + "/v2/api-docs", |
| 121 | + null, |
| 122 | + null |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 128 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 133 | + return Arrays.asList( |
| 134 | + AuthUtils.getForBasic("admin","admin","password"), |
| 135 | + AuthUtils.getForBasic("user", "ivan.petrov@yandex.ru", "petrov") |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + @Override |
| 142 | + public List<DbSpecification> getDbSpecifications() { |
| 143 | + return dbSpecification; |
| 144 | + } |
| 145 | +} |
0 commit comments