|
| 1 | +package em.external.microcks; |
| 2 | + |
| 3 | + |
| 4 | +import com.mongodb.client.MongoClient; |
| 5 | +import com.mongodb.client.MongoClients; |
| 6 | +import org.evomaster.client.java.controller.AuthUtils; |
| 7 | +import org.evomaster.client.java.controller.ExternalSutController; |
| 8 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 9 | +import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto; |
| 10 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 11 | +import org.evomaster.client.java.sql.DbSpecification; |
| 12 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 13 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 14 | +import org.testcontainers.containers.BindMode; |
| 15 | +import org.testcontainers.containers.GenericContainer; |
| 16 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 17 | +import org.testcontainers.utility.DockerImageName; |
| 18 | + |
| 19 | +import java.sql.Connection; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 26 | + |
| 27 | + private static final int DEFAULT_CONTROLLER_PORT = 40100; |
| 28 | + |
| 29 | + private static final int DEFAULT_SUT_PORT = 12345; |
| 30 | + |
| 31 | + private static final String MONGODB_VERSION = "7.0"; |
| 32 | + |
| 33 | + private static final int KEYCLOAK_PORT = 8080; |
| 34 | + private static final String ADMIN_USER = "admin"; |
| 35 | + private static final String ADMIN_PASSWORD = "admin"; |
| 36 | + private static final String REALM_JSON_PATH = "/opt/keycloak/data/import/microcks-realm.json"; |
| 37 | + private static final String POSTMAN_IMAGE = "quay.io/microcks/microcks-postman-runtime:0.6.0"; |
| 38 | + private static final int POSTMAN_PORT = 3000; |
| 39 | + private static final int MONGODB_PORT = 27017; |
| 40 | + |
| 41 | + private static final String MONGODB_DATABASE_NAME = "test"; |
| 42 | + |
| 43 | + |
| 44 | + public static void main(String[] args) { |
| 45 | + |
| 46 | + int controllerPort = DEFAULT_CONTROLLER_PORT; |
| 47 | + if (args.length > 0) { |
| 48 | + controllerPort = Integer.parseInt(args[0]); |
| 49 | + } |
| 50 | + int sutPort = DEFAULT_SUT_PORT; |
| 51 | + if (args.length > 1) { |
| 52 | + sutPort = Integer.parseInt(args[1]); |
| 53 | + } |
| 54 | + String jarLocation = "cs/rest-gui/microcks/webapp/target"; |
| 55 | + if (args.length > 2) { |
| 56 | + jarLocation = args[2]; |
| 57 | + } |
| 58 | + if (!jarLocation.endsWith(".jar")) { |
| 59 | + jarLocation += "/microcks-sut.jar"; |
| 60 | + } |
| 61 | + |
| 62 | + int timeoutSeconds = 120; |
| 63 | + if (args.length > 3) { |
| 64 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 65 | + } |
| 66 | + |
| 67 | + String command = "java"; |
| 68 | + if (args.length > 4) { |
| 69 | + command = args[4]; |
| 70 | + } |
| 71 | + |
| 72 | + ExternalEvoMasterController controller = |
| 73 | + new ExternalEvoMasterController(controllerPort, jarLocation, sutPort, timeoutSeconds, command); |
| 74 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 75 | + |
| 76 | + starter.start(); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + private final int timeoutSeconds; |
| 81 | + |
| 82 | + private final int sutPort; |
| 83 | + |
| 84 | + |
| 85 | + private String jarLocation; |
| 86 | + private Connection sqlConnection; |
| 87 | + private List<DbSpecification> dbSpecification; |
| 88 | + |
| 89 | + private MongoClient mongoClient; |
| 90 | + |
| 91 | + private final GenericContainer<?> mongodb; |
| 92 | + private final GenericContainer<?> keycloakContainer; |
| 93 | + private final GenericContainer<?> postmanContainer; |
| 94 | + |
| 95 | + public ExternalEvoMasterController() { |
| 96 | + this(DEFAULT_CONTROLLER_PORT, "cs/rest-gui/microcks/webapp/target/microcks-sut.jar", DEFAULT_SUT_PORT, 120, "java"); |
| 97 | + } |
| 98 | + |
| 99 | + public ExternalEvoMasterController(String jarLocation) { |
| 100 | + this(); |
| 101 | + this.jarLocation = jarLocation; |
| 102 | + } |
| 103 | + |
| 104 | + public ExternalEvoMasterController(int controllerPort, String jarLocation, int sutPort, int timeoutSeconds, String command) { |
| 105 | + this.sutPort = sutPort; |
| 106 | + this.jarLocation = jarLocation; |
| 107 | + this.timeoutSeconds = timeoutSeconds; |
| 108 | + setControllerPort(controllerPort); |
| 109 | + |
| 110 | + this.mongodb = new GenericContainer<>("mongo:" + MONGODB_VERSION) |
| 111 | + .withTmpFs(Collections.singletonMap("/data/db", "rw")) |
| 112 | + .withExposedPorts(MONGODB_PORT); |
| 113 | + |
| 114 | + this.keycloakContainer = new GenericContainer( |
| 115 | + DockerImageName.parse("quay.io/keycloak/keycloak:26.0.0") |
| 116 | + ) |
| 117 | + .withExposedPorts(KEYCLOAK_PORT) |
| 118 | + .withEnv("KEYCLOAK_ADMIN", ADMIN_USER) |
| 119 | + .withEnv("KEYCLOAK_ADMIN_PASSWORD", ADMIN_PASSWORD) |
| 120 | + .withEnv("KC_HEALTH_ENABLED", "true") |
| 121 | + .withEnv("KC_METRICS_ENABLED", "true") |
| 122 | + .withCommand( |
| 123 | + "start-dev", |
| 124 | + "--hostname-strict=false", |
| 125 | + "--import-realm", |
| 126 | + "--health-enabled=true" |
| 127 | + ) |
| 128 | + .withClasspathResourceMapping("microcks-realm-sample.json", REALM_JSON_PATH, BindMode.READ_ONLY) |
| 129 | + .waitingFor(Wait.forListeningPort()); |
| 130 | + |
| 131 | + this.postmanContainer = new GenericContainer<>(DockerImageName.parse(POSTMAN_IMAGE)) |
| 132 | + .withExposedPorts(POSTMAN_PORT) |
| 133 | + .waitingFor(Wait.forHttp("/health") |
| 134 | + .forPort(POSTMAN_PORT) |
| 135 | + .withStartupTimeout(Duration.ofSeconds(30))) |
| 136 | + .withStartupTimeout(Duration.ofSeconds(30)); |
| 137 | + this.setNeedsJdk17Options(true); |
| 138 | + setJavaCommand(command); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public String[] getInputParameters() { |
| 143 | + return new String[]{"--server.port="+sutPort, |
| 144 | + "--spring.profiles.active=prod", |
| 145 | + "--grpc.server.port=0" |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public String[] getJVMParameters() { |
| 151 | + return new String[]{ |
| 152 | + "-DSPRING_PROFILES_ACTIVE=prod", |
| 153 | + "-DSPRING_DATA_MONGODB_URI="+"mongodb://" + mongodb.getContainerIpAddress() + ":" + mongodb.getMappedPort(MONGODB_PORT), |
| 154 | + "-DSPRING_DATA_MONGODB_DATABASE="+MONGODB_DATABASE_NAME, |
| 155 | + "-DPOSTMAN_RUNNER_URL="+"http://" + postmanContainer.getContainerIpAddress() + ":" + postmanContainer.getMappedPort(POSTMAN_PORT), |
| 156 | + "-DSERVICES_UPDATE_INTERVAL='0 0 0/2 * * *'", |
| 157 | + "-DKEYCLOAK_URL=" + "http://" + keycloakContainer.getContainerIpAddress() + ":" + keycloakContainer.getMappedPort(KEYCLOAK_PORT), |
| 158 | + "-DKEYCLOAK_PUBLIC_URL=" + "http://localhost:" + keycloakContainer.getMappedPort(KEYCLOAK_PORT), |
| 159 | + "-DJAVA_OPTIONS="+"-Dspring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:" + keycloakContainer.getMappedPort(KEYCLOAK_PORT) + "/realms/microcks -Dspring.security.oauth2.resourceserver.jwt.jwk-set-uri=http://" + keycloakContainer.getContainerIpAddress() + ":" + keycloakContainer.getMappedPort(KEYCLOAK_PORT) + "/realms/microcks/protocol/openid-connect/certs", |
| 160 | + "-DENABLE_CORS_POLICY=false", |
| 161 | + "-DCORS_REST_ALLOW_CREDENTIALS=true", |
| 162 | + "-DTEST_CALLBACK_URL=" + "http://localhost:" + sutPort |
| 163 | + }; |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + @Override |
| 168 | + public String getBaseURL() { |
| 169 | + return "http://localhost:" + sutPort; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public String getPathToExecutableJar() { |
| 174 | + return jarLocation; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public String getLogMessageOfInitializedServer() { |
| 179 | + return "Started MicrocksApplication in "; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public long getMaxAwaitForInitializationInSeconds() { |
| 184 | + return timeoutSeconds; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void preStart() { |
| 189 | + |
| 190 | + mongodb.start(); |
| 191 | + postmanContainer.start(); |
| 192 | + keycloakContainer.start(); |
| 193 | + |
| 194 | + try { |
| 195 | + mongoClient = MongoClients.create("mongodb://" + mongodb.getContainerIpAddress() + ":" + mongodb.getMappedPort(MONGODB_PORT)); |
| 196 | + } catch (Exception e) { |
| 197 | + System.out.println("ERROR: " + e.getMessage()); |
| 198 | + throw new RuntimeException(e); |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public void postStart() { |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public void preStop() { |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public void postStop() { |
| 215 | + mongodb.stop(); |
| 216 | + keycloakContainer.stop(); |
| 217 | + postmanContainer.stop(); |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public String getPackagePrefixesToCover() { |
| 222 | + return "io.github.microcks."; |
| 223 | + } |
| 224 | + |
| 225 | + public void resetStateOfSUT() { |
| 226 | + mongoClient.getDatabase(MONGODB_DATABASE_NAME).drop(); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public ProblemInfo getProblemInfo() { |
| 231 | + return new RestProblem( |
| 232 | + getBaseURL() + "/v3/api-docs", |
| 233 | + null |
| 234 | + ); |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 239 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 244 | + //http://localhost:{port}/realms/microcks/protocol/openid-connect/token |
| 245 | + String postEndpoint = "http://localhost:" + keycloakContainer.getMappedPort(KEYCLOAK_PORT) + "/realms/microcks/protocol/openid-connect/token"; |
| 246 | + |
| 247 | + String payloadTemplate = "username=%s&password=microcks123&grant_type=password&client_id=microcks-serviceaccount&client_secret=ab54d329-e435-41ae-a900-ec6b3fe15c54"; |
| 248 | + |
| 249 | + return Arrays.asList( |
| 250 | + AuthUtils.getForJsonToken("ADMIN", |
| 251 | + postEndpoint, |
| 252 | + String.format(payloadTemplate, "admin"), |
| 253 | + "/access_token", |
| 254 | + "Bearer ", |
| 255 | + "application/x-www-form-urlencoded"), |
| 256 | + AuthUtils.getForJsonToken("ADMIN_2", |
| 257 | + postEndpoint, |
| 258 | + String.format(payloadTemplate, "admin2"), |
| 259 | + "/access_token", |
| 260 | + "Bearer ", |
| 261 | + "application/x-www-form-urlencoded") |
| 262 | + ); |
| 263 | + } |
| 264 | + |
| 265 | + |
| 266 | + @Override |
| 267 | + public List<DbSpecification> getDbSpecifications() { |
| 268 | + return dbSpecification; |
| 269 | + } |
| 270 | + |
| 271 | + @Override |
| 272 | + public Object getMongoConnection() { |
| 273 | + return mongoClient; |
| 274 | + } |
| 275 | + |
| 276 | + |
| 277 | +} |
0 commit comments