|
| 1 | +package em.external.reservationsapi; |
| 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.AuthenticationDto; |
| 6 | +import org.evomaster.client.java.controller.api.dto.JsonTokenPostLoginDto; |
| 7 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 8 | +import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType; |
| 9 | +import org.evomaster.client.java.controller.db.DbCleaner; |
| 10 | +import org.evomaster.client.java.controller.db.SqlScriptRunnerCached; |
| 11 | +import org.evomaster.client.java.controller.internal.db.DbSpecification; |
| 12 | +import org.evomaster.client.java.controller.problem.GraphQlProblem; |
| 13 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 14 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 15 | +import org.testcontainers.containers.GenericContainer; |
| 16 | + |
| 17 | +import java.sql.Connection; |
| 18 | +import java.sql.DriverManager; |
| 19 | +import java.sql.SQLException; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 25 | + |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + |
| 29 | + int controllerPort = 40100; |
| 30 | + if (args.length > 0) { |
| 31 | + controllerPort = Integer.parseInt(args[0]); |
| 32 | + } |
| 33 | + int sutPort = 12345; |
| 34 | + if (args.length > 1) { |
| 35 | + sutPort = Integer.parseInt(args[1]); |
| 36 | + } |
| 37 | + String jarLocation = "cs/rest/reservations-api/build/libs"; |
| 38 | + if (args.length > 2) { |
| 39 | + jarLocation = args[2]; |
| 40 | + } |
| 41 | + if(! jarLocation.endsWith(".jar")) { |
| 42 | + jarLocation += "/reservations-api-sut.jar"; |
| 43 | + } |
| 44 | + |
| 45 | + int timeoutSeconds = 120; |
| 46 | + if(args.length > 3){ |
| 47 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 48 | + } |
| 49 | + String command = "java"; |
| 50 | + if(args.length > 4){ |
| 51 | + command = args[4]; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + ExternalEvoMasterController controller = |
| 56 | + new ExternalEvoMasterController(controllerPort, jarLocation, |
| 57 | + sutPort, timeoutSeconds, command); |
| 58 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 59 | + |
| 60 | + starter.start(); |
| 61 | + } |
| 62 | + |
| 63 | + private final int timeoutSeconds; |
| 64 | + private final int sutPort; |
| 65 | + private String jarLocation; |
| 66 | + private static final int MONGODB_PORT = 27017; |
| 67 | + |
| 68 | + //https://www.mongodb.com/docs/drivers/java/sync/current/compatibility/ |
| 69 | + private static final String MONGODB_VERSION = "4.4"; |
| 70 | + |
| 71 | + private static final String MONGODB_DATABASE_NAME = "Reservations"; |
| 72 | + |
| 73 | + private static final GenericContainer mongodbContainer = new GenericContainer("mongo:" + MONGODB_VERSION) |
| 74 | + .withExposedPorts(MONGODB_PORT); |
| 75 | + |
| 76 | + private String mongoDbUrl; |
| 77 | + |
| 78 | + private MongoClient mongoClient; |
| 79 | + |
| 80 | + |
| 81 | + public ExternalEvoMasterController(){ |
| 82 | + this(40100, "../core/target", 12345, 120, "java"); |
| 83 | + } |
| 84 | + |
| 85 | + public ExternalEvoMasterController(String jarLocation) { |
| 86 | + this(); |
| 87 | + this.jarLocation = jarLocation; |
| 88 | + } |
| 89 | + |
| 90 | + public ExternalEvoMasterController( |
| 91 | + int controllerPort, String jarLocation, int sutPort, int timeoutSeconds, String command |
| 92 | + ) { |
| 93 | + |
| 94 | + if(jarLocation==null || jarLocation.isEmpty()){ |
| 95 | + throw new IllegalArgumentException("Missing jar location"); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + this.sutPort = sutPort; |
| 100 | + this.jarLocation = jarLocation; |
| 101 | + this.timeoutSeconds = timeoutSeconds; |
| 102 | + setControllerPort(controllerPort); |
| 103 | + setJavaCommand(command); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + @Override |
| 108 | + public String[] getInputParameters() { |
| 109 | + return new String[]{ |
| 110 | + "-micronaut.server.port="+sutPort, |
| 111 | + "-datasources.default.url=" + dbUrl() |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + public String[] getJVMParameters() { |
| 116 | + return new String[]{}; |
| 117 | + } |
| 118 | + |
| 119 | + private String dbUrl() { |
| 120 | + |
| 121 | + String host = postgres.getContainerIpAddress(); |
| 122 | + int port = postgres.getMappedPort(5432); |
| 123 | + |
| 124 | + String url = "jdbc"; |
| 125 | + url += ":postgresql://"+host+":"+port+"/patio"; |
| 126 | + |
| 127 | + return url; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public String getBaseURL() { |
| 132 | + return "http://localhost:" + sutPort; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public String getPathToExecutableJar() { |
| 137 | + return jarLocation; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String getLogMessageOfInitializedServer() { |
| 142 | + return "Startup completed in"; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public long getMaxAwaitForInitializationInSeconds() { |
| 147 | + return timeoutSeconds; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void preStart() { |
| 152 | + postgres.start(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void postStart() { |
| 157 | + closeDataBaseConnection(); |
| 158 | + |
| 159 | + try { |
| 160 | + sqlConnection = DriverManager.getConnection(dbUrl(), "patio", "patio"); |
| 161 | + dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.POSTGRES,sqlConnection) |
| 162 | + .withSchemas("public").withDisabledSmartClean()); |
| 163 | + // initSqlOnResourcePath = "/initDb.sql"; |
| 164 | + |
| 165 | + } catch (Exception e) { |
| 166 | + throw new RuntimeException(e); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void resetStateOfSUT() { |
| 172 | + mongoClient.getDatabase(MONGODB_DATABASE_NAME).drop(); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void preStop() { |
| 177 | + closeDataBaseConnection(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void postStop() { |
| 182 | + postgres.stop(); |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + @Override |
| 188 | + public String getPackagePrefixesToCover() { |
| 189 | + return "sk.cyrilgavala.reservationsApi."; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public ProblemInfo getProblemInfo() { |
| 194 | + return new RestProblem( |
| 195 | + "http://localhost:" + getSutPort() + "/v3/api-docs", |
| 196 | + null |
| 197 | + ); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 202 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + @Override |
| 208 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 209 | + return null; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public List<DbSpecification> getDbSpecifications() { |
| 214 | + return null; |
| 215 | + } |
| 216 | +} |
0 commit comments