|
| 1 | +package em.embedded.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.evomaster.client.java.controller.AuthUtils; |
| 9 | +import org.evomaster.client.java.controller.EmbeddedSutController; |
| 10 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 11 | +import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto; |
| 12 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 13 | +import org.evomaster.client.java.sql.DbSpecification; |
| 14 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 15 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 16 | +import org.springframework.boot.SpringApplication; |
| 17 | +import org.springframework.context.ConfigurableApplicationContext; |
| 18 | +import org.testcontainers.containers.GenericContainer; |
| 19 | +import org.testcontainers.utility.DockerImageName; |
| 20 | +import com.techie.shoppingstore.NgSpringShoppingStoreApplication; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import org.bson.Document; |
| 27 | +import org.bson.types.ObjectId; |
| 28 | + |
| 29 | +/** |
| 30 | + * Class used to start/stop the SUT. This will be controller by the EvoMaster process |
| 31 | + */ |
| 32 | +public class EmbeddedEvoMasterController extends EmbeddedSutController { |
| 33 | + |
| 34 | + public static void main(String[] args) { |
| 35 | + |
| 36 | + int port = 40100; |
| 37 | + if (args.length > 0) { |
| 38 | + port = Integer.parseInt(args[0]); |
| 39 | + } |
| 40 | + |
| 41 | + EmbeddedEvoMasterController controller = new EmbeddedEvoMasterController(port); |
| 42 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 43 | + |
| 44 | + starter.start(); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + private ConfigurableApplicationContext ctx; |
| 49 | + |
| 50 | + private static final int MONGODB_PORT = 27017; |
| 51 | + |
| 52 | + private static final String MONGODB_VERSION = "7.0"; |
| 53 | + |
| 54 | + private static final String MONGODB_DATABASE_NAME = "test"; |
| 55 | + |
| 56 | + private static final GenericContainer mongodbContainer = new GenericContainer("mongo:" + MONGODB_VERSION) |
| 57 | + .withTmpFs(Collections.singletonMap("/data/db", "rw")) |
| 58 | + .withExposedPorts(MONGODB_PORT); |
| 59 | + |
| 60 | + private static final String REDIS_VERSION = "7.0.11"; |
| 61 | + private static final int REDIS_PORT = 6379; |
| 62 | + |
| 63 | + private static final GenericContainer<?> redisContainer = new GenericContainer("redis:" + REDIS_VERSION) |
| 64 | + .withExposedPorts(REDIS_PORT) |
| 65 | + .withCommand("redis-server", "--appendonly", "yes"); |
| 66 | + |
| 67 | + private static final String ELASTICSEARCH_VERSION = "6.8.23"; |
| 68 | + private static final int HTTP_PORT = 9200; |
| 69 | + private static final int TRANSPORT_PORT = 9300; |
| 70 | + |
| 71 | + private static final GenericContainer<?> elasticsearchContainer = |
| 72 | + new GenericContainer<>(DockerImageName.parse( |
| 73 | + "docker.elastic.co/elasticsearch/elasticsearch:" + ELASTICSEARCH_VERSION)) |
| 74 | + .withEnv("discovery.type", "single-node") |
| 75 | + .withEnv("cluster.name", "elasticsearch") |
| 76 | + .withEnv("ES_JAVA_OPTS", "-Xms512m -Xmx512m") |
| 77 | + .withEnv("xpack.security.enabled", "false") |
| 78 | + .withTmpFs(Collections.singletonMap("/usr/share/elasticsearch/data", "rw")) |
| 79 | + .withExposedPorts(HTTP_PORT, TRANSPORT_PORT); |
| 80 | + |
| 81 | + |
| 82 | + private MongoClient mongoClient; |
| 83 | + |
| 84 | + public EmbeddedEvoMasterController() { |
| 85 | + this(0); |
| 86 | + } |
| 87 | + |
| 88 | + public EmbeddedEvoMasterController(int port) { |
| 89 | + setControllerPort(port); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + @Override |
| 94 | + public String startSut() { |
| 95 | + |
| 96 | + mongodbContainer.start(); |
| 97 | + redisContainer.start(); |
| 98 | + elasticsearchContainer.start(); |
| 99 | + |
| 100 | + mongoClient = MongoClients.create("mongodb://" + mongodbContainer.getContainerIpAddress() + ":" + mongodbContainer.getMappedPort(MONGODB_PORT)); |
| 101 | + |
| 102 | + try { |
| 103 | + Thread.sleep(3_000); |
| 104 | + } catch (InterruptedException e) { |
| 105 | + throw new RuntimeException(e); |
| 106 | + } |
| 107 | + |
| 108 | + ctx = SpringApplication.run(NgSpringShoppingStoreApplication.class, |
| 109 | + new String[]{"--server.port=0", |
| 110 | + "--spring.datasource.host=" + mongodbContainer.getContainerIpAddress(), |
| 111 | + "--spring.datasource.port=" + mongodbContainer.getMappedPort(MONGODB_PORT), |
| 112 | + "--spring.datasource.database=" + MONGODB_DATABASE_NAME, |
| 113 | + "--spring.data.mongodb.uri=mongodb://" + mongodbContainer.getContainerIpAddress() + ":" + mongodbContainer.getMappedPort(MONGODB_PORT) + "/" + MONGODB_DATABASE_NAME, |
| 114 | + "--spring.redis.host=" + redisContainer.getContainerIpAddress(), |
| 115 | + "--spring.redis.port=" + redisContainer.getMappedPort(REDIS_PORT), |
| 116 | + "--spring.data.elasticsearch.cluster-name=elasticsearch", |
| 117 | + "--spring.data.elsticsearch.cluster-nodes=" + elasticsearchContainer.getContainerIpAddress() + ":" + elasticsearchContainer.getMappedPort(TRANSPORT_PORT), |
| 118 | + "--spring.cache.type=NONE" |
| 119 | + }); |
| 120 | + |
| 121 | + return "http://localhost:" + getSutPort(); |
| 122 | + } |
| 123 | + |
| 124 | + protected int getSutPort() { |
| 125 | + return (Integer) ((Map) ctx.getEnvironment() |
| 126 | + .getPropertySources().get("server.ports").getSource()) |
| 127 | + .get("local.server.port"); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean isSutRunning() { |
| 133 | + return ctx != null && ctx.isRunning(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void stopSut() { |
| 138 | + ctx.stop(); |
| 139 | + ctx.close(); |
| 140 | + |
| 141 | + mongodbContainer.stop(); |
| 142 | + mongoClient.close(); |
| 143 | + redisContainer.stop(); |
| 144 | + elasticsearchContainer.stop(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public String getPackagePrefixesToCover() { |
| 149 | + return "com.techie.shoppingstore."; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void resetStateOfSUT() { |
| 154 | + MongoDatabase db = mongoClient.getDatabase(MONGODB_DATABASE_NAME); |
| 155 | + |
| 156 | + |
| 157 | + for(String name: db.listCollectionNames()){ |
| 158 | + db.getCollection(name).deleteMany(new BasicDBObject()); |
| 159 | + } |
| 160 | + |
| 161 | + MongoCollection<Document> users = db.getCollection("User"); |
| 162 | + users.insertMany(Arrays.asList( |
| 163 | + new Document() |
| 164 | + .append("_id", new ObjectId()) |
| 165 | + .append("_class", "com.techie.shoppingstore.model.User") |
| 166 | + .append("username", "user1") |
| 167 | + .append("email", "user1@email.com") |
| 168 | + .append("enabled", true) |
| 169 | + //12345678 |
| 170 | + .append("password", "$2a$12$p9eP3beaPuSMbS1enDn1Z.zFuv6npjm6xjyQnnEqvVG.CD03d1aoi"), |
| 171 | + new Document() |
| 172 | + .append("_id", new ObjectId()) |
| 173 | + .append("_class", "com.techie.shoppingstore.model.User") |
| 174 | + .append("username", "user2") |
| 175 | + .append("email", "user2@email.com") |
| 176 | + .append("enabled", true) |
| 177 | + //12345678 |
| 178 | + .append("password", "$2a$12$p9eP3beaPuSMbS1enDn1Z.zFuv6npjm6xjyQnnEqvVG.CD03d1aoi") |
| 179 | + )); |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | + @Override |
| 184 | + public List<DbSpecification> getDbSpecifications() { |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + String rawPassword = "12345678"; |
| 189 | + |
| 190 | + @Override |
| 191 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 192 | + return Arrays.asList( |
| 193 | + AuthUtils.getForJsonTokenBearer( |
| 194 | + "user1", |
| 195 | + "/api/auth/login", |
| 196 | + "{\"username\":\"user1\", \"password\":\""+rawPassword+"\"}", |
| 197 | + "/accessToken" |
| 198 | + ), |
| 199 | + AuthUtils.getForJsonTokenBearer( |
| 200 | + "user2", |
| 201 | + "/api/auth/login", |
| 202 | + "{\"username\":\"user2\", \"password\":\""+rawPassword+"\"}", |
| 203 | + "/accessToken" |
| 204 | + ) |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + @Override |
| 212 | + public ProblemInfo getProblemInfo() { |
| 213 | + return new RestProblem( |
| 214 | + "http://localhost:" + getSutPort() + "/v2/api-docs", |
| 215 | + null |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 221 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public Object getMongoConnection() { |
| 226 | + return mongoClient; |
| 227 | + } |
| 228 | +} |
0 commit comments