11package em .embedded .familie .tilbake ;
22
3+ import no .nav .familie .tilbake .Launcher ;
34import org .evomaster .client .java .controller .EmbeddedSutController ;
5+ import org .evomaster .client .java .controller .InstrumentedSutStarter ;
46import org .evomaster .client .java .controller .api .dto .SutInfoDto ;
57import org .evomaster .client .java .controller .api .dto .auth .AuthenticationDto ;
8+ import org .evomaster .client .java .controller .api .dto .database .schema .DatabaseType ;
69import org .evomaster .client .java .controller .problem .ProblemInfo ;
10+ import org .evomaster .client .java .sql .DbCleaner ;
711import org .evomaster .client .java .sql .DbSpecification ;
12+ import org .springframework .boot .SpringApplication ;
813import org .springframework .context .ConfigurableApplicationContext ;
14+ import org .springframework .jdbc .core .JdbcTemplate ;
15+ import org .testcontainers .containers .GenericContainer ;
916
17+ import java .sql .Connection ;
18+ import java .sql .SQLException ;
19+ import java .util .Arrays ;
1020import java .util .List ;
1121import java .util .Map ;
1222
1323public class EmbeddedEvoMasterController extends EmbeddedSutController {
1424
25+ private static final String POSTGRES_VERSION = "13.13" ;
26+
27+ private static final String POSTGRES_PASSWORD = "password" ;
28+
29+ private static final int POSTGRES_PORT = 5432 ;
30+
31+ private static final GenericContainer postgresContainer = new GenericContainer ("postgres:" + POSTGRES_VERSION )
32+ .withEnv ("POSTGRES_PASSWORD" , POSTGRES_PASSWORD )
33+ .withEnv ("POSTGRES_HOST_AUTH_METHOD" , "trust" ) //to allow all connections without a password
34+ .withExposedPorts (POSTGRES_PORT );
35+
1536 private ConfigurableApplicationContext ctx ;
1637
38+ private Connection sqlConnection ;
39+ private List <DbSpecification > dbSpecification ;
40+
1741 public EmbeddedEvoMasterController () {
1842 this (40100 );
1943 }
@@ -23,7 +47,15 @@ public EmbeddedEvoMasterController(int port) {
2347 }
2448
2549 public static void main (String [] args ) {
26- System .out .println ("Hello world!" );
50+ int port = 40100 ;
51+ if (args .length > 0 ) {
52+ port = Integer .parseInt (args [0 ]);
53+ }
54+
55+ EmbeddedEvoMasterController controller = new EmbeddedEvoMasterController (port );
56+ InstrumentedSutStarter starter = new InstrumentedSutStarter (controller );
57+
58+ starter .start ();
2759 }
2860
2961 @ Override
@@ -33,7 +65,7 @@ public boolean isSutRunning() {
3365
3466 @ Override
3567 public String getPackagePrefixesToCover () {
36- return null ;
68+ return "no.nav.familie.tilbake." ;
3769 }
3870
3971 @ Override
@@ -48,20 +80,40 @@ public ProblemInfo getProblemInfo() {
4880
4981 @ Override
5082 public SutInfoDto .OutputFormat getPreferredOutputFormat () {
51- return null ;
83+ return SutInfoDto . OutputFormat . JAVA_JUNIT_5 ;
5284 }
5385
5486 @ Override
5587 public String startSut () {
88+ postgresContainer .start ();
89+
5690 ctx = SpringApplication .run (Launcher .class , new String []{
5791 "--server.port=0" ,
58- "--spring.profiles.active=local,external,internal " ,
92+ "--spring.profiles.active=local" ,
5993 "--management.server.port=-1" ,
6094 "--server.ssl.enabled=false" ,
61- "--spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;" ,
62- "--cwa-testresult-server.url=http://cwa-testresult-server:8088"
95+ "--spring.datasource.url=jdbc:postgresql://localhost:5432/familie-tilbake" ,
96+ "--spring.datasource.username=postgres" ,
97+ "--spring.datasource.password=test" ,
98+ "--sentry.logging.enabled=false" ,
6399 });
64100
101+ if (sqlConnection != null ) {
102+ try {
103+ sqlConnection .close ();
104+ } catch (SQLException e ) {
105+ throw new RuntimeException (e );
106+ }
107+ }
108+
109+ JdbcTemplate jdbc = ctx .getBean (JdbcTemplate .class );try {
110+ sqlConnection = jdbc .getDataSource ().getConnection ();
111+ } catch (SQLException e ) {
112+ throw new RuntimeException (e );
113+ }
114+
115+ dbSpecification = Arrays .asList (new DbSpecification (DatabaseType .POSTGRES , sqlConnection ));
116+
65117 return "http://localhost:" + getSutPort ();
66118 }
67119
@@ -73,16 +125,18 @@ protected int getSutPort() {
73125
74126 @ Override
75127 public void stopSut () {
76-
128+ postgresContainer .stop ();
129+ ctx .stop ();
77130 }
78131
79132 @ Override
80133 public void resetStateOfSUT () {
81-
134+ // TODO: check and see for any necessary steps required
135+ DbCleaner .clearDatabase (sqlConnection , List .of (), DatabaseType .POSTGRES );
82136 }
83137
84138 @ Override
85139 public List <DbSpecification > getDbSpecifications () {
86- return null ;
140+ return dbSpecification ;
87141 }
88142}
0 commit comments