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