|
| 1 | +package em.external.uk.gov.pay.api.app; |
| 2 | + |
| 3 | +import org.evomaster.client.java.controller.AuthUtils; |
| 4 | +import org.evomaster.client.java.controller.ExternalSutController; |
| 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.DbCleaner; |
| 12 | +import org.evomaster.client.java.sql.DbSpecification; |
| 13 | +import org.h2.tools.Server; |
| 14 | +import org.testcontainers.containers.GenericContainer; |
| 15 | +import redis.clients.jedis.Jedis; |
| 16 | +import redis.clients.jedis.JedisPool; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.nio.file.Paths; |
| 23 | +import java.nio.file.StandardCopyOption; |
| 24 | +import java.sql.Connection; |
| 25 | +import java.sql.DriverManager; |
| 26 | +import java.sql.SQLException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + |
| 34 | + int controllerPort = 40100; |
| 35 | + if (args.length > 0) { |
| 36 | + controllerPort = Integer.parseInt(args[0]); |
| 37 | + } |
| 38 | + int sutPort = 12345; |
| 39 | + if (args.length > 1) { |
| 40 | + sutPort = Integer.parseInt(args[1]); |
| 41 | + } |
| 42 | + String jarLocation = "cs/rest/pay-publicapi/target"; |
| 43 | + if(args.length > 2){ |
| 44 | + jarLocation = args[2]; |
| 45 | + } |
| 46 | + if(! jarLocation.endsWith(".jar")) { |
| 47 | + jarLocation += "/pay-publicapi-sut.jar"; |
| 48 | + } |
| 49 | + int timeoutSeconds = 120; |
| 50 | + if(args.length > 3){ |
| 51 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 52 | + } |
| 53 | + String command = "java"; |
| 54 | + if(args.length > 4){ |
| 55 | + command = args[4]; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + ExternalEvoMasterController controller = |
| 61 | + new ExternalEvoMasterController(controllerPort, jarLocation, sutPort, timeoutSeconds, command); |
| 62 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 63 | + |
| 64 | + starter.start(); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + private final int timeoutSeconds; |
| 69 | + private final int sutPort; |
| 70 | + |
| 71 | + private String jarLocation; |
| 72 | + private final String CONFIG_FILE = "em_config.yaml"; |
| 73 | + |
| 74 | + private static final int REDIS_PORT = 6379; |
| 75 | + |
| 76 | + private static final String REDIS_VERSION = "7.2.3"; |
| 77 | + |
| 78 | + private static final GenericContainer redisContainer = new GenericContainer("redis:" + REDIS_VERSION) |
| 79 | + .withExposedPorts(REDIS_PORT); |
| 80 | + |
| 81 | + private static String REDIS_URL = ""; |
| 82 | + |
| 83 | + private static JedisPool jedisPool; |
| 84 | + |
| 85 | + public ExternalEvoMasterController(){ |
| 86 | + this(40100, "../api/target", 12345, 120, "java"); |
| 87 | + } |
| 88 | + |
| 89 | + public ExternalEvoMasterController(String jarLocation) { |
| 90 | + this(); |
| 91 | + this.jarLocation = jarLocation; |
| 92 | + } |
| 93 | + |
| 94 | + public ExternalEvoMasterController(int controllerPort, |
| 95 | + String jarLocation, |
| 96 | + int sutPort, |
| 97 | + int timeoutSeconds, |
| 98 | + String command) { |
| 99 | + this.sutPort = sutPort; |
| 100 | + this.jarLocation = jarLocation; |
| 101 | + this.timeoutSeconds = timeoutSeconds; |
| 102 | + setControllerPort(controllerPort); |
| 103 | + setJavaCommand(command); |
| 104 | + createConfigurationFile(); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + Unfortunately, it seems like Dropwizard is buggy, and has |
| 110 | + problems with overriding params without a YML file :( |
| 111 | + */ |
| 112 | + private void createConfigurationFile() { |
| 113 | + |
| 114 | + //save config to same folder of JAR file |
| 115 | + Path path = getConfigPath(); |
| 116 | + |
| 117 | + try(InputStream is = this.getClass().getResourceAsStream("/"+ CONFIG_FILE )){ |
| 118 | + Files.copy(is, path, StandardCopyOption.REPLACE_EXISTING); |
| 119 | + } catch (Exception e){ |
| 120 | + throw new RuntimeException(e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private Path getConfigPath(){ |
| 125 | + return Paths.get(jarLocation) |
| 126 | + .toAbsolutePath() |
| 127 | + .getParent() |
| 128 | + .resolve(CONFIG_FILE) |
| 129 | + .normalize(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String[] getInputParameters() { |
| 134 | + return new String[]{"server", getConfigPath().toAbsolutePath().toString()}; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public String[] getJVMParameters() { |
| 139 | + |
| 140 | + return new String[]{ |
| 141 | + "-Ddw.server.applicationConnectors[0].port="+sutPort, |
| 142 | + "-Ddw.server.adminConnectors[0].port=0", |
| 143 | + "-Ddw.redis.endpoint="+REDIS_URL |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String getBaseURL() { |
| 149 | + return "http://localhost:"+sutPort; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String getPathToExecutableJar() { |
| 154 | + return jarLocation; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public String getLogMessageOfInitializedServer() { |
| 159 | + return "Server: Started"; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public long getMaxAwaitForInitializationInSeconds() { |
| 164 | + return timeoutSeconds; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void preStart() { |
| 169 | + redisContainer.start(); |
| 170 | + |
| 171 | + REDIS_URL = redisContainer.getHost() + redisContainer.getMappedPort(REDIS_PORT); |
| 172 | + |
| 173 | + jedisPool = new JedisPool(redisContainer.getHost(), redisContainer.getMappedPort(REDIS_PORT)); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void postStart() { |
| 178 | + |
| 179 | + resetStateOfSUT(); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void preStop() { |
| 184 | + |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void postStop() { |
| 189 | + redisContainer.stop(); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public String getPackagePrefixesToCover() { |
| 194 | + return "uk.gov.pay.api."; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void resetStateOfSUT() { |
| 199 | + try (Jedis jedis = jedisPool.getResource()) { |
| 200 | + jedis.flushAll(); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public ProblemInfo getProblemInfo() { |
| 206 | + return new RestProblem( |
| 207 | + getBaseURL() + "/assets/swagger.json", |
| 208 | + null |
| 209 | + ); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 214 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 219 | + //TODO |
| 220 | + return null; |
| 221 | + } |
| 222 | + |
| 223 | + |
| 224 | + @Override |
| 225 | + public List<DbSpecification> getDbSpecifications() { |
| 226 | + return null; |
| 227 | + } |
| 228 | +} |
0 commit comments