|
| 1 | +package em.external.rg.cbioportal.session_service; |
| 2 | + |
| 3 | + |
| 4 | +import com.mongodb.client.MongoClient; |
| 5 | +import com.mongodb.client.MongoClients; |
| 6 | +import org.evomaster.client.java.controller.ExternalSutController; |
| 7 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 8 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 9 | +import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto; |
| 10 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 11 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 12 | +import org.evomaster.client.java.sql.DbSpecification; |
| 13 | +import org.testcontainers.containers.GenericContainer; |
| 14 | + |
| 15 | +import java.sql.Connection; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 20 | + |
| 21 | + private static final int DEFAULT_CONTROLLER_PORT = 40100; |
| 22 | + |
| 23 | + private static final int DEFAULT_SUT_PORT = 12345; |
| 24 | + |
| 25 | + private static final int DEFAULT_DB_PORT = 27017; |
| 26 | + private static final int MONGODB_PORT = 27017; |
| 27 | + |
| 28 | + private static final String MONGODB_VERSION = "6.0"; |
| 29 | + |
| 30 | + private static final String MONGODB_DATABASE_NAME = "session"; |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + |
| 34 | + int controllerPort = DEFAULT_CONTROLLER_PORT; |
| 35 | + if (args.length > 0) { |
| 36 | + controllerPort = Integer.parseInt(args[0]); |
| 37 | + } |
| 38 | + int sutPort = DEFAULT_SUT_PORT; |
| 39 | + if (args.length > 1) { |
| 40 | + sutPort = Integer.parseInt(args[1]); |
| 41 | + } |
| 42 | + String jarLocation = "cs/rest/original/session-service/target"; |
| 43 | + if (args.length > 2) { |
| 44 | + jarLocation = args[2]; |
| 45 | + } |
| 46 | + if (!jarLocation.endsWith(".jar")) { |
| 47 | + jarLocation += "/session-service-sut.jar"; |
| 48 | + } |
| 49 | + |
| 50 | + int timeoutSeconds = 120; |
| 51 | + if (args.length > 3) { |
| 52 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 53 | + } |
| 54 | + |
| 55 | + String command = "java"; |
| 56 | + if (args.length > 4) { |
| 57 | + command = args[4]; |
| 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 | + |
| 70 | + private final int sutPort; |
| 71 | + |
| 72 | + |
| 73 | + private String jarLocation; |
| 74 | + private Connection sqlConnection; |
| 75 | + private List<DbSpecification> dbSpecification; |
| 76 | + |
| 77 | + private MongoClient mongoClient; |
| 78 | + |
| 79 | + private final GenericContainer<?> mongodb; |
| 80 | + |
| 81 | + public ExternalEvoMasterController() { |
| 82 | + this(DEFAULT_CONTROLLER_PORT, "../target/session-service-sut.jar", DEFAULT_SUT_PORT, 120, "java"); |
| 83 | + } |
| 84 | + |
| 85 | + public ExternalEvoMasterController(String jarLocation) { |
| 86 | + this(); |
| 87 | + this.jarLocation = jarLocation; |
| 88 | + } |
| 89 | + |
| 90 | + public ExternalEvoMasterController(int controllerPort, String jarLocation, int sutPort, int timeoutSeconds, String command) { |
| 91 | + this.sutPort = sutPort; |
| 92 | + this.jarLocation = jarLocation; |
| 93 | + this.timeoutSeconds = timeoutSeconds; |
| 94 | + setControllerPort(controllerPort); |
| 95 | + this.mongodb = new GenericContainer<>("mongo:" + MONGODB_VERSION) |
| 96 | + .withTmpFs(Collections.singletonMap("/data/db", "rw")) |
| 97 | + .withExposedPorts(DEFAULT_DB_PORT); |
| 98 | + setJavaCommand(command); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String[] getInputParameters() { |
| 103 | + return new String[]{ |
| 104 | + "--server.port=" + sutPort, |
| 105 | + "--spring.data.mongodb.uri=mongodb://" + mongodb.getContainerIpAddress() + ":" + mongodb.getMappedPort(DEFAULT_DB_PORT) + "/" + MONGODB_DATABASE_NAME, |
| 106 | + "--spring.cache.type=NONE" |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + private String dbUrl() { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String[] getJVMParameters() { |
| 117 | + return new String[]{ |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + @Override |
| 123 | + public String getBaseURL() { |
| 124 | + return "http://localhost:" + sutPort; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String getPathToExecutableJar() { |
| 129 | + return jarLocation; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String getLogMessageOfInitializedServer() { |
| 134 | + return "Started SessionService in "; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public long getMaxAwaitForInitializationInSeconds() { |
| 139 | + return timeoutSeconds; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void preStart() { |
| 144 | + |
| 145 | + mongodb.start(); |
| 146 | + |
| 147 | + try { |
| 148 | + mongoClient = MongoClients.create("mongodb://" + mongodb.getContainerIpAddress() + ":" + mongodb.getMappedPort(DEFAULT_DB_PORT)); |
| 149 | + } catch (Exception e) { |
| 150 | + System.out.println("ERROR: " + e.getMessage()); |
| 151 | + throw new RuntimeException(e); |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void postStart() { |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void preStop() { |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void postStop() { |
| 168 | + mongodb.stop(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public String getPackagePrefixesToCover() { |
| 173 | + return "org.cbioportal.session_service."; |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + public void resetStateOfSUT() { |
| 178 | + mongoClient.getDatabase(MONGODB_DATABASE_NAME).drop(); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + @Override |
| 183 | + public ProblemInfo getProblemInfo() { |
| 184 | + return new RestProblem( |
| 185 | + getBaseURL() + "/v2/api-docs", |
| 186 | + null |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 192 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_4; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 197 | + return null; |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + @Override |
| 202 | + public List<DbSpecification> getDbSpecifications() { |
| 203 | + return dbSpecification; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public Object getMongoConnection() { |
| 208 | + return mongoClient; |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | +} |
0 commit comments