|
| 1 | +package em.embedded.familie.ba.sak; |
| 2 | + |
| 3 | +import no.nav.familie.ba.sak.ApplicationKt; |
| 4 | +import org.evomaster.client.java.controller.EmbeddedSutController; |
| 5 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 6 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 7 | +import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto; |
| 8 | +import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType; |
| 9 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 10 | +import org.evomaster.client.java.sql.DbCleaner; |
| 11 | +import org.evomaster.client.java.sql.DbSpecification; |
| 12 | +import org.springframework.boot.SpringApplication; |
| 13 | +import org.springframework.context.ConfigurableApplicationContext; |
| 14 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 15 | +import org.testcontainers.containers.GenericContainer; |
| 16 | + |
| 17 | +import java.sql.Connection; |
| 18 | +import java.sql.SQLException; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +public class EmbeddedEvoMasterController extends EmbeddedSutController { |
| 24 | + |
| 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 | + .withEnv("POSTGRES_DB", "familiebasak") |
| 35 | + .withExposedPorts(POSTGRES_PORT); |
| 36 | + |
| 37 | + private ConfigurableApplicationContext ctx; |
| 38 | + |
| 39 | + private Connection sqlConnection; |
| 40 | + private List<DbSpecification> dbSpecification; |
| 41 | + |
| 42 | + public EmbeddedEvoMasterController() { |
| 43 | + this(40100); |
| 44 | + } |
| 45 | + |
| 46 | + public EmbeddedEvoMasterController(int port) { |
| 47 | + setControllerPort(port); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + public static void main(String[] args) { |
| 52 | + int port = 40100; |
| 53 | + if (args.length > 0) { |
| 54 | + port = Integer.parseInt(args[0]); |
| 55 | + } |
| 56 | + |
| 57 | + EmbeddedEvoMasterController controller = new EmbeddedEvoMasterController(port); |
| 58 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 59 | + |
| 60 | + starter.start(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean isSutRunning() { |
| 65 | + return ctx.isRunning(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public String getPackagePrefixesToCover() { |
| 70 | + return "no.nav.familie.ba.sak."; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public ProblemInfo getProblemInfo() { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 85 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String startSut() { |
| 90 | + postgresContainer.start(); |
| 91 | + |
| 92 | + String postgresURL = "jdbc:postgresql://" + postgresContainer.getHost() + ":" + postgresContainer.getMappedPort(POSTGRES_PORT) + "/familiebasak"; |
| 93 | + |
| 94 | + |
| 95 | + ctx = SpringApplication.run(ApplicationKt.class, new String[]{ |
| 96 | + "--server.port=0", |
| 97 | + "--spring.profiles.active=dev", |
| 98 | + "--management.server.port=-1", |
| 99 | + "--server.ssl.enabled=false", |
| 100 | + "--spring.datasource.url=" + postgresURL, |
| 101 | + "--spring.datasource.username=postgres", |
| 102 | + "--spring.datasource.password=" + POSTGRES_PASSWORD, |
| 103 | + "--sentry.logging.enabled=false", |
| 104 | + "--sentry.environment=local", |
| 105 | + "--funksjonsbrytere.kafka.producer.enabled=false", |
| 106 | + "--funksjonsbrytere.enabled=false", |
| 107 | + "--logging.level.root=OFF", |
| 108 | + "--logback.configurationFile=src/main/resources/logback.xml", |
| 109 | + "--logging.level.org.springframework=OFF", |
| 110 | + "--spring.main.web-application-type=none" |
| 111 | + }); |
| 112 | + |
| 113 | + |
| 114 | + // https://www.baeldung.com/spring-boot-application-context-exception |
| 115 | + // spring.main.web-application-type=none |
| 116 | + |
| 117 | + if (sqlConnection != null) { |
| 118 | + try { |
| 119 | + sqlConnection.close(); |
| 120 | + } catch (SQLException e) { |
| 121 | + throw new RuntimeException(e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + JdbcTemplate jdbc = ctx.getBean(JdbcTemplate.class);try { |
| 126 | + sqlConnection = jdbc.getDataSource().getConnection(); |
| 127 | + } catch (SQLException e) { |
| 128 | + throw new RuntimeException(e); |
| 129 | + } |
| 130 | + |
| 131 | + dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.POSTGRES, sqlConnection)); |
| 132 | + |
| 133 | + return "http://localhost:" + getSutPort(); |
| 134 | + } |
| 135 | + |
| 136 | + protected int getSutPort() { |
| 137 | + return (Integer) ((Map) ctx.getEnvironment() |
| 138 | + .getPropertySources().get("server.ports").getSource()) |
| 139 | + .get("local.server.port"); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void stopSut() { |
| 144 | + postgresContainer.stop(); |
| 145 | + ctx.stop(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void resetStateOfSUT() { |
| 150 | + DbCleaner.clearDatabase(sqlConnection, List.of(), DatabaseType.POSTGRES); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public List<DbSpecification> getDbSpecifications() { |
| 155 | + return dbSpecification; |
| 156 | + } |
| 157 | +} |
0 commit comments