|
| 1 | +package em.external.angular.ecommerce; |
| 2 | + |
| 3 | +import com.mongodb.BasicDBObject; |
| 4 | +import com.mongodb.client.MongoClient; |
| 5 | +import com.mongodb.client.MongoClients; |
| 6 | +import com.mongodb.client.MongoCollection; |
| 7 | +import com.mongodb.client.MongoDatabase; |
| 8 | +import org.bson.Document; |
| 9 | +import org.bson.types.ObjectId; |
| 10 | +import org.evomaster.client.java.controller.AuthUtils; |
| 11 | +import org.evomaster.client.java.controller.ExternalSutController; |
| 12 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 13 | +import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto; |
| 14 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 15 | +import org.evomaster.client.java.sql.DbSpecification; |
| 16 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 17 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 18 | +import org.testcontainers.containers.GenericContainer; |
| 19 | +import org.testcontainers.utility.DockerImageName; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 26 | + |
| 27 | + |
| 28 | + public static void main(String[] args) { |
| 29 | + |
| 30 | + int controllerPort = 40100; |
| 31 | + if (args.length > 0) { |
| 32 | + controllerPort = Integer.parseInt(args[0]); |
| 33 | + } |
| 34 | + int sutPort = 12345; |
| 35 | + if (args.length > 1) { |
| 36 | + sutPort = Integer.parseInt(args[1]); |
| 37 | + } |
| 38 | + String jarLocation = "cs/rest/original/angular-ecommerce/target"; |
| 39 | + if (args.length > 2) { |
| 40 | + jarLocation = args[2]; |
| 41 | + } |
| 42 | + if(! jarLocation.endsWith(".jar")) { |
| 43 | + jarLocation += "/angular-ecommerce-sut.jar"; |
| 44 | + } |
| 45 | + |
| 46 | + int timeoutSeconds = 120; |
| 47 | + if(args.length > 3){ |
| 48 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 49 | + } |
| 50 | + String command = "java"; |
| 51 | + if(args.length > 4){ |
| 52 | + command = args[4]; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + ExternalEvoMasterController controller = |
| 57 | + new ExternalEvoMasterController(controllerPort, jarLocation, |
| 58 | + sutPort, timeoutSeconds, command); |
| 59 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 60 | + |
| 61 | + starter.start(); |
| 62 | + } |
| 63 | + |
| 64 | + private final int timeoutSeconds; |
| 65 | + private final int sutPort; |
| 66 | + private String jarLocation; |
| 67 | + |
| 68 | + private static final int MONGODB_PORT = 27017; |
| 69 | + |
| 70 | + private static final String MONGODB_VERSION = "7.0"; |
| 71 | + |
| 72 | + private static final String MONGODB_DATABASE_NAME = "test"; |
| 73 | + |
| 74 | + private static final GenericContainer mongodbContainer = new GenericContainer("mongo:" + MONGODB_VERSION) |
| 75 | + .withTmpFs(Collections.singletonMap("/data/db", "rw")) |
| 76 | + .withExposedPorts(MONGODB_PORT); |
| 77 | + |
| 78 | + private static final String REDIS_VERSION = "7.0.11"; |
| 79 | + private static final int REDIS_PORT = 6379; |
| 80 | + |
| 81 | + private static final GenericContainer<?> redisContainer = new GenericContainer("redis:" + REDIS_VERSION) |
| 82 | + .withExposedPorts(REDIS_PORT) |
| 83 | + .withCommand("redis-server", "--appendonly", "yes"); |
| 84 | + |
| 85 | + private static final String ELASTICSEARCH_VERSION = "6.8.23"; |
| 86 | + private static final int HTTP_PORT = 9200; |
| 87 | + private static final int TRANSPORT_PORT = 9300; |
| 88 | + |
| 89 | + private static final GenericContainer<?> elasticsearchContainer = |
| 90 | + new GenericContainer<>(DockerImageName.parse( |
| 91 | + "docker.elastic.co/elasticsearch/elasticsearch:" + ELASTICSEARCH_VERSION)) |
| 92 | + .withEnv("discovery.type", "single-node") |
| 93 | + .withEnv("cluster.name", "elasticsearch") |
| 94 | + .withEnv("ES_JAVA_OPTS", "-Xms512m -Xmx512m") |
| 95 | + .withEnv("xpack.security.enabled", "false") |
| 96 | + .withTmpFs(Collections.singletonMap("/usr/share/elasticsearch/data", "rw")) |
| 97 | + .withExposedPorts(HTTP_PORT, TRANSPORT_PORT); |
| 98 | + |
| 99 | + |
| 100 | + private MongoClient mongoClient; |
| 101 | + |
| 102 | + |
| 103 | + public ExternalEvoMasterController(){ |
| 104 | + this(40100, "../core/target", 12345, 120, "java"); |
| 105 | + } |
| 106 | + |
| 107 | + public ExternalEvoMasterController(String jarLocation) { |
| 108 | + this(); |
| 109 | + this.jarLocation = jarLocation; |
| 110 | + } |
| 111 | + |
| 112 | + public ExternalEvoMasterController( |
| 113 | + int controllerPort, String jarLocation, int sutPort, int timeoutSeconds, String command |
| 114 | + ) { |
| 115 | + |
| 116 | + if(jarLocation==null || jarLocation.isEmpty()){ |
| 117 | + throw new IllegalArgumentException("Missing jar location"); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + this.sutPort = sutPort; |
| 122 | + this.jarLocation = jarLocation; |
| 123 | + this.timeoutSeconds = timeoutSeconds; |
| 124 | + setControllerPort(controllerPort); |
| 125 | + setJavaCommand(command); |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + @Override |
| 130 | + public String[] getInputParameters() { |
| 131 | + return new String[]{ |
| 132 | + "--server.port=" + sutPort, |
| 133 | + "--spring.datasource.host=" + mongodbContainer.getContainerIpAddress(), |
| 134 | + "--spring.datasource.port=" + mongodbContainer.getMappedPort(MONGODB_PORT), |
| 135 | + "--spring.datasource.database=" + MONGODB_DATABASE_NAME, |
| 136 | + "--spring.data.mongodb.uri=mongodb://" + mongodbContainer.getContainerIpAddress() + ":" + mongodbContainer.getMappedPort(MONGODB_PORT) + "/" + MONGODB_DATABASE_NAME, |
| 137 | + "--spring.redis.host=" + redisContainer.getContainerIpAddress(), |
| 138 | + "--spring.redis.port=" + redisContainer.getMappedPort(REDIS_PORT), |
| 139 | + "--spring.data.elasticsearch.cluster-name=elasticsearch", |
| 140 | + "--spring.data.elsticsearch.cluster-nodes=" + elasticsearchContainer.getContainerIpAddress() + ":" + elasticsearchContainer.getMappedPort(TRANSPORT_PORT), |
| 141 | + "--spring.cache.type=NONE" |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + public String[] getJVMParameters() { |
| 146 | + |
| 147 | + return new String[]{ |
| 148 | + "-Dfile.encoding=ISO-8859-1" |
| 149 | + }; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public String getBaseURL() { |
| 154 | + return "http://localhost:" + sutPort; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public String getPathToExecutableJar() { |
| 159 | + return jarLocation; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String getLogMessageOfInitializedServer() { |
| 164 | + return "Started NgSpringShoppingStoreApplication in "; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public long getMaxAwaitForInitializationInSeconds() { |
| 169 | + return timeoutSeconds; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void preStart() { |
| 174 | + |
| 175 | + mongodbContainer.start(); |
| 176 | + redisContainer.start(); |
| 177 | + elasticsearchContainer.start(); |
| 178 | + |
| 179 | + mongoClient = MongoClients.create("mongodb://" + mongodbContainer.getContainerIpAddress() + ":" + mongodbContainer.getMappedPort(MONGODB_PORT)); |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void postStart() { |
| 185 | + try { |
| 186 | + Thread.sleep(3_000); |
| 187 | + } catch (InterruptedException e) { |
| 188 | + // do nothing |
| 189 | + } |
| 190 | + |
| 191 | + while (!isMongoClientReady()) { |
| 192 | + try { |
| 193 | + Thread.sleep(1_000); |
| 194 | + } catch (InterruptedException e) { |
| 195 | + // do nothing |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Checks if the mongo database is ready to receive commands using a ping command |
| 202 | + * @return |
| 203 | + */ |
| 204 | + private boolean isMongoClientReady() { |
| 205 | + try { |
| 206 | + MongoDatabase db = mongoClient.getDatabase(MONGODB_DATABASE_NAME); |
| 207 | + Document pingResult = db.runCommand(new Document("ping", 1)); |
| 208 | + return pingResult.getDouble("ok") == 1.0; |
| 209 | + } catch (Exception ex) { |
| 210 | + // Connection error |
| 211 | + return false; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public void resetStateOfSUT() { |
| 217 | + MongoDatabase db = mongoClient.getDatabase(MONGODB_DATABASE_NAME); |
| 218 | + |
| 219 | + |
| 220 | + for(String name: db.listCollectionNames()){ |
| 221 | + db.getCollection(name).deleteMany(new BasicDBObject()); |
| 222 | + } |
| 223 | + |
| 224 | + MongoCollection<Document> users = db.getCollection("User"); |
| 225 | + users.insertMany(Arrays.asList( |
| 226 | + new Document() |
| 227 | + .append("_id", new ObjectId()) |
| 228 | + .append("_class", "com.techie.shoppingstore.model.User") |
| 229 | + .append("username", "user1") |
| 230 | + .append("email", "user1@email.com") |
| 231 | + .append("enabled", true) |
| 232 | + //12345678 |
| 233 | + .append("password", "$2a$12$p9eP3beaPuSMbS1enDn1Z.zFuv6npjm6xjyQnnEqvVG.CD03d1aoi"), |
| 234 | + new Document() |
| 235 | + .append("_id", new ObjectId()) |
| 236 | + .append("_class", "com.techie.shoppingstore.model.User") |
| 237 | + .append("username", "user2") |
| 238 | + .append("email", "user2@email.com") |
| 239 | + .append("enabled", true) |
| 240 | + //12345678 |
| 241 | + .append("password", "$2a$12$p9eP3beaPuSMbS1enDn1Z.zFuv6npjm6xjyQnnEqvVG.CD03d1aoi") |
| 242 | + )); |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public void preStop() { |
| 247 | + } |
| 248 | + |
| 249 | + @Override |
| 250 | + public void postStop() { |
| 251 | + mongodbContainer.stop(); |
| 252 | + mongoClient.close(); |
| 253 | + redisContainer.stop(); |
| 254 | + elasticsearchContainer.stop(); |
| 255 | + } |
| 256 | + |
| 257 | + |
| 258 | + |
| 259 | + @Override |
| 260 | + public String getPackagePrefixesToCover() { |
| 261 | + return "com.techie.shoppingstore."; |
| 262 | + } |
| 263 | + |
| 264 | + @Override |
| 265 | + public ProblemInfo getProblemInfo() { |
| 266 | + return new RestProblem( |
| 267 | + "http://localhost:" + sutPort + "/v2/api-docs", |
| 268 | + null |
| 269 | + ); |
| 270 | + } |
| 271 | + |
| 272 | + @Override |
| 273 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 274 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 275 | + } |
| 276 | + |
| 277 | + String rawPassword = "12345678"; |
| 278 | + |
| 279 | + @Override |
| 280 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 281 | + return Arrays.asList( |
| 282 | + AuthUtils.getForJsonTokenBearer( |
| 283 | + "user1", |
| 284 | + "/api/auth/login", |
| 285 | + "{\"username\":\"user1\", \"password\":\""+rawPassword+"\"}", |
| 286 | + "/accessToken" |
| 287 | + ), |
| 288 | + AuthUtils.getForJsonTokenBearer( |
| 289 | + "user2", |
| 290 | + "/api/auth/login", |
| 291 | + "{\"username\":\"user2\", \"password\":\""+rawPassword+"\"}", |
| 292 | + "/accessToken" |
| 293 | + ) |
| 294 | + ); |
| 295 | + } |
| 296 | + |
| 297 | + @Override |
| 298 | + public List<DbSpecification> getDbSpecifications() { |
| 299 | + return null; |
| 300 | + } |
| 301 | + |
| 302 | + @Override |
| 303 | + public Object getMongoConnection() { |
| 304 | + return mongoClient; |
| 305 | + } |
| 306 | + |
| 307 | + |
| 308 | +} |
0 commit comments