|
| 1 | +package em.embedded.microcks; |
| 2 | + |
| 3 | +import com.mongodb.client.MongoClient; |
| 4 | +import com.mongodb.client.MongoClients; |
| 5 | +import io.github.microcks.MicrocksApplication; |
| 6 | +import org.evomaster.client.java.controller.AuthUtils; |
| 7 | +import org.evomaster.client.java.controller.EmbeddedSutController; |
| 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.springframework.boot.SpringApplication; |
| 15 | +import org.springframework.context.ConfigurableApplicationContext; |
| 16 | +import org.testcontainers.containers.BindMode; |
| 17 | +import org.testcontainers.containers.GenericContainer; |
| 18 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 19 | +import org.testcontainers.utility.DockerImageName; |
| 20 | + |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Class used to start/stop the SUT. This will be controller by the EvoMaster process |
| 30 | + */ |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Class used to start/stop the SUT. This will be controller by the EvoMaster process |
| 35 | + */ |
| 36 | +public class EmbeddedEvoMasterController extends EmbeddedSutController { |
| 37 | + |
| 38 | + public static void main(String[] args) { |
| 39 | + |
| 40 | + int port = 40100; |
| 41 | + if (args.length > 0) { |
| 42 | + port = Integer.parseInt(args[0]); |
| 43 | + } |
| 44 | + |
| 45 | + EmbeddedEvoMasterController controller = new EmbeddedEvoMasterController(port); |
| 46 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 47 | + |
| 48 | + starter.start(); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + private ConfigurableApplicationContext ctx; |
| 53 | + |
| 54 | + private static final int MONGODB_PORT = 27017; |
| 55 | + private static final int KEYCLOAK_PORT = 8080; |
| 56 | + private static final String ADMIN_USER = "admin"; |
| 57 | + private static final String ADMIN_PASSWORD = "admin"; |
| 58 | + private static final String REALM_JSON_PATH = "/opt/keycloak/data/import/microcks-realm.json"; |
| 59 | + private static final String POSTMAN_IMAGE = "quay.io/microcks/microcks-postman-runtime:0.6.0"; |
| 60 | + private static final int POSTMAN_PORT = 3000; |
| 61 | + |
| 62 | + private static final String MONGODB_VERSION = "7.0"; |
| 63 | + |
| 64 | + private static final String MONGODB_DATABASE_NAME = "test"; |
| 65 | + |
| 66 | + private static final GenericContainer mongodbContainer = new GenericContainer("mongo:" + MONGODB_VERSION) |
| 67 | + .withTmpFs(Collections.singletonMap("/data/db", "rw")) |
| 68 | + .withExposedPorts(MONGODB_PORT); |
| 69 | + |
| 70 | + private static final GenericContainer keycloakContainer = new GenericContainer( |
| 71 | + DockerImageName.parse("quay.io/keycloak/keycloak:26.0.0") |
| 72 | + ) |
| 73 | + .withExposedPorts(KEYCLOAK_PORT) |
| 74 | + .withEnv("KEYCLOAK_ADMIN", ADMIN_USER) |
| 75 | + .withEnv("KEYCLOAK_ADMIN_PASSWORD", ADMIN_PASSWORD) |
| 76 | + .withEnv("KC_HEALTH_ENABLED", "true") |
| 77 | + .withEnv("KC_METRICS_ENABLED", "true") |
| 78 | + .withCommand( |
| 79 | + "start-dev", |
| 80 | + "--hostname-strict=false", |
| 81 | + "--import-realm", |
| 82 | + "--health-enabled=true" |
| 83 | + ) |
| 84 | + .withClasspathResourceMapping("microcks-realm-sample.json", REALM_JSON_PATH, BindMode.READ_ONLY) |
| 85 | + .waitingFor(Wait.forListeningPort()); |
| 86 | + |
| 87 | + private static final GenericContainer<?> postmanContainer = new GenericContainer<>(DockerImageName.parse(POSTMAN_IMAGE)) |
| 88 | + .withExposedPorts(POSTMAN_PORT) |
| 89 | +// .withCreateContainerCmdModifier(cmd -> cmd.withName("microcks-postman-runtime")) |
| 90 | + .waitingFor(Wait.forHttp("/health") |
| 91 | + .forPort(POSTMAN_PORT) |
| 92 | + .withStartupTimeout(Duration.ofSeconds(30))) |
| 93 | + .withStartupTimeout(Duration.ofSeconds(30)); |
| 94 | + |
| 95 | + private MongoClient mongoClient; |
| 96 | + |
| 97 | + public EmbeddedEvoMasterController() { |
| 98 | + this(0); |
| 99 | + } |
| 100 | + |
| 101 | + public EmbeddedEvoMasterController(int port) { |
| 102 | + setControllerPort(port); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + @Override |
| 107 | + public String startSut() { |
| 108 | + |
| 109 | + mongodbContainer.start(); |
| 110 | + keycloakContainer.start(); |
| 111 | + postmanContainer.start(); |
| 112 | + |
| 113 | + mongoClient = MongoClients.create("mongodb://" + mongodbContainer.getContainerIpAddress() + ":" + mongodbContainer.getMappedPort(MONGODB_PORT)); |
| 114 | + |
| 115 | + System.setProperty("SPRING_PROFILES_ACTIVE", "prod"); |
| 116 | + System.setProperty("SPRING_DATA_MONGODB_URI", "mongodb://" + mongodbContainer.getContainerIpAddress() + ":" + mongodbContainer.getMappedPort(MONGODB_PORT)); |
| 117 | + System.setProperty("SPRING_DATA_MONGODB_DATABASE", MONGODB_DATABASE_NAME); |
| 118 | + System.setProperty("POSTMAN_RUNNER_URL", "http://" + postmanContainer.getContainerIpAddress() + ":" + postmanContainer.getMappedPort(POSTMAN_PORT)); |
| 119 | + System.setProperty("SERVICES_UPDATE_INTERVAL", "0 0 0/2 * * *"); |
| 120 | + System.setProperty("KEYCLOAK_URL", "http://" + keycloakContainer.getContainerIpAddress() + ":" + keycloakContainer.getMappedPort(KEYCLOAK_PORT)); |
| 121 | + System.setProperty("KEYCLOAK_PUBLIC_URL", "http://localhost:" + keycloakContainer.getMappedPort(KEYCLOAK_PORT)); |
| 122 | + System.setProperty("JAVA_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"); |
| 123 | + System.setProperty("ENABLE_CORS_POLICY", "false"); |
| 124 | + System.setProperty("CORS_REST_ALLOW_CREDENTIALS", "true"); |
| 125 | + |
| 126 | + ctx = SpringApplication.run(MicrocksApplication.class, |
| 127 | + new String[]{"--server.port=0", |
| 128 | + "--spring.profiles.active=prod", |
| 129 | + "--grpc.server.port=0" |
| 130 | + }); |
| 131 | + |
| 132 | + System.setProperty("TEST_CALLBACK_URL", "http://localhost:" + getSutPort()); |
| 133 | + |
| 134 | + return "http://localhost:" + getSutPort(); |
| 135 | + } |
| 136 | + |
| 137 | + protected int getSutPort() { |
| 138 | + return (Integer) ((Map) ctx.getEnvironment() |
| 139 | + .getPropertySources().get("server.ports").getSource()) |
| 140 | + .get("local.server.port"); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean isSutRunning() { |
| 146 | + return ctx != null && ctx.isRunning(); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void stopSut() { |
| 151 | + ctx.stop(); |
| 152 | + ctx.close(); |
| 153 | + |
| 154 | + mongodbContainer.stop(); |
| 155 | + keycloakContainer.stop(); |
| 156 | + postmanContainer.stop(); |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public String getPackagePrefixesToCover() { |
| 162 | + return "io.github.microcks."; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void resetStateOfSUT() { |
| 167 | + mongoClient.getDatabase(MONGODB_DATABASE_NAME).drop(); |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + @Override |
| 172 | + public List<DbSpecification> getDbSpecifications() { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + @Override |
| 178 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 179 | + //http://localhost:{port}/realms/microcks/protocol/openid-connect/token |
| 180 | + String postEndpoint = "http://localhost:" + keycloakContainer.getMappedPort(KEYCLOAK_PORT) + "/realms/microcks/protocol/openid-connect/token"; |
| 181 | + |
| 182 | + String payloadTemplate = "username=%s&password=microcks123&grant_type=password&client_id=microcks-serviceaccount&client_secret=ab54d329-e435-41ae-a900-ec6b3fe15c54"; |
| 183 | + |
| 184 | + return Arrays.asList( |
| 185 | + AuthUtils.getForJsonToken("ADMIN", |
| 186 | + postEndpoint, |
| 187 | + String.format(payloadTemplate, "admin"), |
| 188 | + "/access_token", |
| 189 | + "Bearer ", |
| 190 | + "application/x-www-form-urlencoded"), |
| 191 | + AuthUtils.getForJsonToken("ADMIN_2", |
| 192 | + postEndpoint, |
| 193 | + String.format(payloadTemplate, "admin2"), |
| 194 | + "/access_token", |
| 195 | + "Bearer ", |
| 196 | + "application/x-www-form-urlencoded") |
| 197 | + ); |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + @Override |
| 202 | + public ProblemInfo getProblemInfo() { |
| 203 | + return new RestProblem( |
| 204 | + "http://localhost:" + getSutPort() + "/v3/api-docs", |
| 205 | + null |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 211 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_4; |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public Object getMongoConnection() { |
| 216 | + return mongoClient; |
| 217 | + } |
| 218 | +} |
0 commit comments