Skip to content

Commit a7f42c8

Browse files
committed
enable smart db clean for features-service
1 parent bb2df13 commit a7f42c8

4 files changed

Lines changed: 87 additions & 5 deletions

File tree

jdk_8_maven/em/embedded/rest/features-service/src/main/java/em/embedded/org/javiermf/features/EmbeddedEvoMasterController.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.evomaster.client.java.controller.InstrumentedSutStarter;
66
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
77
import org.evomaster.client.java.controller.db.DbCleaner;
8+
import org.evomaster.client.java.controller.db.SqlScriptRunner;
89
import org.evomaster.client.java.controller.internal.db.DbSpecification;
910
import org.evomaster.client.java.controller.problem.ProblemInfo;
1011
import org.evomaster.client.java.controller.problem.RestProblem;
@@ -15,6 +16,8 @@
1516
import org.springframework.context.ConfigurableApplicationContext;
1617
import org.springframework.jdbc.core.JdbcTemplate;
1718

19+
import java.io.InputStream;
20+
import java.io.InputStreamReader;
1821
import java.sql.Connection;
1922
import java.sql.SQLException;
2023
import java.util.Arrays;
@@ -42,15 +45,25 @@ public static void main(String[] args) {
4245

4346
private ConfigurableApplicationContext ctx;
4447
private Connection sqlConnection;
48+
private String INIT_DB_SCRIPT_PATH = "/data.sql";
49+
4550
private List<DbSpecification> dbSpecification;
4651

52+
private String initSQLScript;
53+
4754

4855
public EmbeddedEvoMasterController() {
4956
this(40100);
5057
}
5158

5259
public EmbeddedEvoMasterController(int port) {
5360
setControllerPort(port);
61+
62+
try (InputStream in = getClass().getResourceAsStream(INIT_DB_SCRIPT_PATH)) {
63+
initSQLScript = String.join(System.lineSeparator(), (new SqlScriptRunner()).readCommands(new InputStreamReader(in)));
64+
} catch (Exception e) {
65+
throw new RuntimeException(e);
66+
}
5467
}
5568

5669
@Override
@@ -79,8 +92,14 @@ public String startSut() {
7992
throw new RuntimeException(e);
8093
}
8194

95+
/*
96+
the application will be initialized some data in database
97+
to consistently manage data by evomaster
98+
*/
99+
DbCleaner.clearDatabase_H2(sqlConnection);
100+
82101
dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.H2,sqlConnection)
83-
.withDisabledSmartClean());
102+
.withInitSqlScript(initSQLScript));
84103

85104
return "http://localhost:" + getSutPort();
86105
}
@@ -109,7 +128,7 @@ public String getPackagePrefixesToCover() {
109128

110129
@Override
111130
public void resetStateOfSUT() {
112-
DbCleaner.clearDatabase_H2(sqlConnection);
131+
// DbCleaner.clearDatabase_H2(sqlConnection);
113132
}
114133

115134
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
insert into Product (id, name) values (1, 'ELEARNING_SITE');
2+
insert into Feature (id, name,product_id) values (1, 'VIDEO_LESSONS',1);
3+
insert into Feature (id, name,product_id) values (2, 'ONLINE_FORUM',1);
4+
insert into Feature (id, name,product_id) values (3, 'CHAT',1);
5+
insert into Feature (id, name,product_id) values (4, 'MAILING_LIST',1);
6+
insert into Feature (id, name,product_id) values (5, 'COURSE_SELLING',1);
7+
insert into Feature (id, name,product_id) values (6, 'PAYPAL_PAYMENT',1);
8+
insert into Feature (id, name,product_id) values (7, 'CREDIT_CARD_PAYMENT',1);
9+
insert into Feature (id, name,product_id) values (8, 'REDEEM_CODES',1);
10+
insert into Feature (id, name,product_id) values (9, 'IN_TRIAL_PERIOD',1);
11+
12+
insert into Constraint_Requires (id, for_product_id, source_feature_name, required_feature_name ) values (1,1,'PAYPAL_PAYMENT','COURSE_SELLING');
13+
insert into Constraint_Requires (id, for_product_id, source_feature_name, required_feature_name ) values (2,1,'CREDIT_CARD','COURSE_SELLING');
14+
insert into Constraint_Excludes (id, for_product_id, source_feature_name, excluded_feature_name ) values (3,1,'IN_TRIAL_PERIOD','COURSE_SELLING');
15+
16+
insert into Product_Configuration (id, name,product_id, valid) values (1, 'UNIVERSITY_X',1, 1);
17+
insert into Product_Configuration_Actived_Features (in_configurations_id, actived_features_id) values (1,1);
18+
insert into Product_Configuration_Actived_Features (in_configurations_id, actived_features_id) values (1,2);
19+
insert into Product_Configuration_Actived_Features (in_configurations_id, actived_features_id) values (1,3);
20+
insert into Product_Configuration_Actived_Features (in_configurations_id, actived_features_id) values (1,9);
21+
22+
23+

jdk_8_maven/em/external/rest/features-service/src/main/java/em/external/org/javiermf/features/ExternalEvoMasterController.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import org.evomaster.client.java.controller.InstrumentedSutStarter;
55
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
66
import org.evomaster.client.java.controller.db.DbCleaner;
7+
import org.evomaster.client.java.controller.db.SqlScriptRunner;
78
import org.evomaster.client.java.controller.internal.db.DbSpecification;
89
import org.evomaster.client.java.controller.problem.ProblemInfo;
910
import org.evomaster.client.java.controller.problem.RestProblem;
1011
import org.evomaster.client.java.controller.api.dto.AuthenticationDto;
1112
import org.evomaster.client.java.controller.api.dto.SutInfoDto;
1213
import org.h2.tools.Server;
1314

15+
import java.io.InputStream;
16+
import java.io.InputStreamReader;
1417
import java.sql.Connection;
1518
import java.sql.DriverManager;
1619
import java.sql.SQLException;
@@ -59,7 +62,11 @@ public static void main(String[] args) {
5962
private final int dbPort;
6063
private String jarLocation;
6164
private Connection sqlConnection;
65+
private String INIT_DB_SCRIPT_PATH = "/data.sql";
66+
6267
private List<DbSpecification> dbSpecification;
68+
69+
private String initSQLScript;
6370
private Server h2;
6471

6572
public ExternalEvoMasterController() {
@@ -78,6 +85,12 @@ public ExternalEvoMasterController(int controllerPort, String jarLocation, int s
7885
this.timeoutSeconds = timeoutSeconds;
7986
setControllerPort(controllerPort);
8087
setJavaCommand(command);
88+
89+
try (InputStream in = getClass().getResourceAsStream(INIT_DB_SCRIPT_PATH)) {
90+
initSQLScript = String.join(System.lineSeparator(), (new SqlScriptRunner()).readCommands(new InputStreamReader(in)));
91+
} catch (Exception e) {
92+
throw new RuntimeException(e);
93+
}
8194
}
8295

8396
private String dbUrl( ) {
@@ -144,16 +157,20 @@ public void postStart() {
144157
try {
145158
Class.forName("org.h2.Driver");
146159
sqlConnection = DriverManager.getConnection(dbUrl(), "sa", "");
147-
dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.H2,sqlConnection)
148-
.withDisabledSmartClean());
160+
/*
161+
the application will be initialized some data in database
162+
to consistently manage data by evomaster
163+
*/
164+
DbCleaner.clearDatabase_H2(sqlConnection);
165+
dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.H2,sqlConnection).withInitSqlOnResourcePath(initSQLScript));
149166
} catch (Exception e) {
150167
throw new RuntimeException(e);
151168
}
152169
}
153170

154171
@Override
155172
public void resetStateOfSUT() {
156-
DbCleaner.clearDatabase_H2(sqlConnection);
173+
// DbCleaner.clearDatabase_H2(sqlConnection);
157174
}
158175

159176
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
insert into Product (id, name) values (1, 'ELEARNING_SITE');
2+
insert into Feature (id, name,product_id) values (1, 'VIDEO_LESSONS',1);
3+
insert into Feature (id, name,product_id) values (2, 'ONLINE_FORUM',1);
4+
insert into Feature (id, name,product_id) values (3, 'CHAT',1);
5+
insert into Feature (id, name,product_id) values (4, 'MAILING_LIST',1);
6+
insert into Feature (id, name,product_id) values (5, 'COURSE_SELLING',1);
7+
insert into Feature (id, name,product_id) values (6, 'PAYPAL_PAYMENT',1);
8+
insert into Feature (id, name,product_id) values (7, 'CREDIT_CARD_PAYMENT',1);
9+
insert into Feature (id, name,product_id) values (8, 'REDEEM_CODES',1);
10+
insert into Feature (id, name,product_id) values (9, 'IN_TRIAL_PERIOD',1);
11+
12+
insert into Constraint_Requires (id, for_product_id, source_feature_name, required_feature_name ) values (1,1,'PAYPAL_PAYMENT','COURSE_SELLING');
13+
insert into Constraint_Requires (id, for_product_id, source_feature_name, required_feature_name ) values (2,1,'CREDIT_CARD','COURSE_SELLING');
14+
insert into Constraint_Excludes (id, for_product_id, source_feature_name, excluded_feature_name ) values (3,1,'IN_TRIAL_PERIOD','COURSE_SELLING');
15+
16+
insert into Product_Configuration (id, name,product_id, valid) values (1, 'UNIVERSITY_X',1, 1);
17+
insert into Product_Configuration_Actived_Features (in_configurations_id, actived_features_id) values (1,1);
18+
insert into Product_Configuration_Actived_Features (in_configurations_id, actived_features_id) values (1,2);
19+
insert into Product_Configuration_Actived_Features (in_configurations_id, actived_features_id) values (1,3);
20+
insert into Product_Configuration_Actived_Features (in_configurations_id, actived_features_id) values (1,9);
21+
22+
23+

0 commit comments

Comments
 (0)