package com.faforever.api.league;

import com.faforever.api.AbstractIntegrationTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
public abstract class LeagueAbstractIntegrationTest extends AbstractIntegrationTest {
  private static final MariaDBContainer<?> leagueServiceDBContainer = new MariaDBContainer<>("mariadb:10.6");
  protected static GenericContainer<?> leagueServiceContainer = new GenericContainer<>("faforever/faf-league-service:0.1.7");

  static {
    leagueServiceDBContainer
      .withUsername("faf-league-service")
      .withPassword("banana")
      .withDatabaseName("faf-league")
      .withReuse(true)
      .start();

    final Logger logger = LoggerFactory.getLogger(LeagueAbstractIntegrationTest.class);
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);

    leagueServiceContainer
      .withEnv("DB_SERVER", leagueServiceDBContainer.getContainerIpAddress() + ":" + leagueServiceDBContainer.getFirstMappedPort().toString())
      .withEnv("DB_LOGIN", "faf-league-service")
      .withEnv("DB_PASSWORD", "banana")
      .withEnv("DB_NAME", "faf-league")
      .withEnv("AUTO_APPLY_MIGRATIONS", "1")
      .withCommand()
      .withLogConsumer(logConsumer)
      .start();
  }

  @DynamicPropertySource
  public static void setDatasourceProperties(final DynamicPropertyRegistry registry) {
    registry.add("spring.league-datasource.url", leagueServiceDBContainer::getJdbcUrl);
    registry.add("spring.league-datasource.password", leagueServiceDBContainer::getPassword);
    registry.add("spring.league-datasource.username", leagueServiceDBContainer::getUsername);
  }
}

@@ -365,32 +365,78 @@
 riaDBContainer;%0A
+import org.testcontainers.containers.Network;%0A
 import org.testc
@@ -477,24 +477,24 @@
 ogConsumer;%0A
-
 import org.t
@@ -875,16 +875,85 @@
 0.1.7%22);
+%0A  private static final Network sharedNetwork = Network.newNetwork();
 %0A%0A  stat
@@ -957,16 +957,17 @@
 tatic %7B%0A
+%0A
     leag
@@ -979,32 +979,109 @@
 viceDBContainer%0A
+      .withNetwork(sharedNetwork)%0A      .withNetworkAliases(%22faf-league-db%22)%0A
       .withUsern
@@ -1410,138 +1410,74 @@
 with
-Env(%22DB_SERVER%22, leagueServiceDBContainer.getContainerIpAddress() + %22:%22 + leagueServiceDBContainer.getFirstMappedPort().toString()
+Network(sharedNetwork)%0A      .withEnv(%22DB_SERVER%22, %22faf-league-db%22
 )%0A  

Put league service testcontainers in same network and use alias