|
| 1 | +package em.external.familie.ba.sak; |
| 2 | + |
| 3 | +import org.evomaster.client.java.controller.ExternalSutController; |
| 4 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 5 | +import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto; |
| 6 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 7 | +import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType; |
| 8 | +import org.evomaster.client.java.sql.DbCleaner; |
| 9 | +import org.evomaster.client.java.sql.SqlScriptRunner; |
| 10 | +import org.evomaster.client.java.sql.SqlScriptRunnerCached; |
| 11 | +import org.evomaster.client.java.sql.DbSpecification; |
| 12 | +import org.evomaster.client.java.controller.problem.GraphQlProblem; |
| 13 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 14 | +import org.testcontainers.containers.GenericContainer; |
| 15 | + |
| 16 | +import java.io.InputStream; |
| 17 | +import java.io.InputStreamReader; |
| 18 | +import java.sql.Connection; |
| 19 | +import java.sql.DriverManager; |
| 20 | +import java.sql.SQLException; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 26 | + |
| 27 | + |
| 28 | + public static void main(String[] args) { |
| 29 | + |
| 30 | + int controllerPort = 40100; |
| 31 | + if (args.length > 0) { |
| 32 | + controllerPort = Integer.parseInt(args[0]); |
| 33 | + } |
| 34 | + int sutPort = 12345; |
| 35 | + if (args.length > 1) { |
| 36 | + sutPort = Integer.parseInt(args[1]); |
| 37 | + } |
| 38 | + String jarLocation = "cs/rest/familie-ba-sak/target"; |
| 39 | + if (args.length > 2) { |
| 40 | + jarLocation = args[2]; |
| 41 | + } |
| 42 | + if(! jarLocation.endsWith(".jar")) { |
| 43 | + jarLocation += "/familie-ba-sak-sut.jar"; |
| 44 | + } |
| 45 | + |
| 46 | + int timeoutSeconds = 120; |
| 47 | + if(args.length > 3){ |
| 48 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 49 | + } |
| 50 | + String command = "java"; |
| 51 | + if(args.length > 4){ |
| 52 | + command = args[4]; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + ExternalEvoMasterController controller = |
| 57 | + new ExternalEvoMasterController(controllerPort, jarLocation, |
| 58 | + sutPort, timeoutSeconds, command); |
| 59 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 60 | + |
| 61 | + starter.start(); |
| 62 | + } |
| 63 | + |
| 64 | + private final int timeoutSeconds; |
| 65 | + private final int sutPort; |
| 66 | + private String jarLocation; |
| 67 | + private Connection sqlConnection; |
| 68 | + //private String INIT_DB_SCRIPT_PATH = "/populateDB.sql"; |
| 69 | + |
| 70 | + private List<DbSpecification> dbSpecification; |
| 71 | + |
| 72 | + private String initSQLScript; |
| 73 | + |
| 74 | + private static final String POSTGRES_VERSION = "13.13"; |
| 75 | + |
| 76 | + private static final String POSTGRES_PASSWORD = "password"; |
| 77 | + |
| 78 | + private static final int POSTGRES_PORT = 5432; |
| 79 | + |
| 80 | + private static final GenericContainer postgres = new GenericContainer("postgres:" + POSTGRES_VERSION) |
| 81 | + .withEnv("POSTGRES_PASSWORD", POSTGRES_PASSWORD) |
| 82 | + .withEnv("POSTGRES_HOST_AUTH_METHOD", "trust") //to allow all connections without a password |
| 83 | + .withEnv("POSTGRES_DB", "familiebasak") |
| 84 | + .withTmpFs(Collections.singletonMap("/var/lib/postgresql/data", "rw")) |
| 85 | + .withExposedPorts(POSTGRES_PORT); |
| 86 | + |
| 87 | + public ExternalEvoMasterController(){ |
| 88 | + this(40100, "../core/target", 12345, 120, "java"); |
| 89 | + } |
| 90 | + |
| 91 | + public ExternalEvoMasterController(String jarLocation) { |
| 92 | + this(); |
| 93 | + this.jarLocation = jarLocation; |
| 94 | + } |
| 95 | + |
| 96 | + public ExternalEvoMasterController( |
| 97 | + int controllerPort, String jarLocation, int sutPort, int timeoutSeconds, String command |
| 98 | + ) { |
| 99 | + |
| 100 | + if(jarLocation==null || jarLocation.isEmpty()){ |
| 101 | + throw new IllegalArgumentException("Missing jar location"); |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + this.sutPort = sutPort; |
| 106 | + this.jarLocation = jarLocation; |
| 107 | + this.timeoutSeconds = timeoutSeconds; |
| 108 | + setControllerPort(controllerPort); |
| 109 | + setJavaCommand(command); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + @Override |
| 114 | + public String[] getInputParameters() { |
| 115 | + return new String[]{"--server.port=" + sutPort}; |
| 116 | + } |
| 117 | + |
| 118 | + public String[] getJVMParameters() { |
| 119 | + |
| 120 | + return new String[]{ |
| 121 | + "-Dspring.datasource.url=" + dbUrl(), |
| 122 | + "-Dspring.cache.type=none", |
| 123 | + "-Dspring.profiles.active=postgresql,spring-data-jpa", |
| 124 | + "-Dspring.jmx.enabled=false", |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + private String dbUrl() { |
| 129 | + |
| 130 | + String host = postgres.getContainerIpAddress(); |
| 131 | + int port = postgres.getMappedPort(5432); |
| 132 | + |
| 133 | + String url = "jdbc"; |
| 134 | + url += ":postgresql://"+host+":"+port+"/petclinic"; |
| 135 | + |
| 136 | + return url; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public String getBaseURL() { |
| 141 | + return "http://localhost:" + sutPort; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String getPathToExecutableJar() { |
| 146 | + return jarLocation; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public String getLogMessageOfInitializedServer() { |
| 151 | + return "Tomcat started on port"; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public long getMaxAwaitForInitializationInSeconds() { |
| 156 | + return timeoutSeconds; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void preStart() { |
| 161 | + postgres.start(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void postStart() { |
| 166 | + closeDataBaseConnection(); |
| 167 | + |
| 168 | + try { |
| 169 | + sqlConnection = DriverManager.getConnection(dbUrl(), "postgres", POSTGRES_PASSWORD); |
| 170 | + |
| 171 | + dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.POSTGRES,sqlConnection) |
| 172 | + .withSchemas("public") |
| 173 | + ); |
| 174 | + } catch (Exception e) { |
| 175 | + throw new RuntimeException(e); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void resetStateOfSUT() { |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void preStop() { |
| 185 | + closeDataBaseConnection(); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void postStop() { |
| 190 | + postgres.stop(); |
| 191 | + } |
| 192 | + |
| 193 | + private void closeDataBaseConnection() { |
| 194 | + if (sqlConnection != null) { |
| 195 | + try { |
| 196 | + sqlConnection.close(); |
| 197 | + } catch (SQLException e) { |
| 198 | + e.printStackTrace(); |
| 199 | + } |
| 200 | + sqlConnection = null; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public String getPackagePrefixesToCover() { |
| 206 | + return "org.springframework.samples.petclinic"; |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public ProblemInfo getProblemInfo() { |
| 211 | + return new GraphQlProblem("/graphql"); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 216 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 217 | + } |
| 218 | + |
| 219 | + |
| 220 | + @Override |
| 221 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 222 | + return null; |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public List<DbSpecification> getDbSpecifications() { |
| 227 | + return dbSpecification; |
| 228 | + } |
| 229 | +} |
0 commit comments