Skip to content

Instantly share code, notes, and snippets.

@samiraghayarov
Created September 21, 2021 10:03
Show Gist options
  • Save samiraghayarov/693f014d7306ceb5aef467abf01bef16 to your computer and use it in GitHub Desktop.
Save samiraghayarov/693f014d7306ceb5aef467abf01bef16 to your computer and use it in GitHub Desktop.
Test pgs test docker compose
spring.liquibase.enabled=false
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:h2:mem:test
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
version: '3'
services:
db:
image: postgres:10
ports:
- 5432
environment:
- POSTGRES_DB=pay
- POSTGRES_USER=pay
- POSTGRES_PASSWORD=test1234
package cobienpte.paymentengine.pay;
import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.connection.DockerPort;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import javax.sql.DataSource;
@TestConfiguration
@Profile("docker-container")
public class DockerConfig {
@Bean
public DockerPort dockerPort(DockerComposeRule docker) {
return docker.containers().container("db").port(5432);
}
@Bean
@Primary
public DataSource datasource(@Value("${spring.datasource.driverClassName}") String driverClassName,
@Value("${spring.datasource.username}") String username,
@Value("${spring.datasource.password}") String password,
DockerPort dockerPort) {
DataSourceBuilder<?> create = DataSourceBuilder.create();
create.url(String.format("jdbc:postgresql://localhost:%d/pay", dockerPort.getExternalPort()));
create.username(username);
create.password(password);
create.driverClassName(driverClassName);
// sleep added to avoid illegal state exception thrown by docker compose during some tests
try {
Thread.sleep(2000);
} catch(Throwable e){
}
return create.build();
}
}
package cobienpte.paymentengine.pay.pay.checkexisting;
import cobienpte.paymentengine.model.actualpay.OrderStatus;
import cobienpte.paymentengine.pay.DockerConfig;
import cobienpte.paymentengine.paymentstore.PaymentType;
import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.connection.waiting.HealthChecks;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
@ActiveProfiles({ "test", "docker-container" })
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@Import(DockerConfig.class)
public class PaymentHistoryRepositoryTest {
private static final Integer INTERVAL_MINUTES = 3;
private static final ChronoUnit INTERVAL_UNIT = ChronoUnit.MINUTES;
private static final int TOTAL_PAYMENTS = 9;
private static final int EXPECTED_EXPIRED_PAYMENTS = 5;
@Autowired
private PaymentHistoryRepository paymentHistoryRepository;
@ClassRule
public static DockerComposeRule docker;
static {
try {
docker = DockerComposeRule.builder()
.file("src/test/resources/docker-compose-postgresql.yml")
.waitingForService("db", HealthChecks.toHaveAllPortsOpen())
.saveLogsTo("build/dockerLogs/dockerComposeRuleTest")
.build();
} catch (Throwable t) {
System.err.println("Error starting docker: " + t.getMessage());
t.printStackTrace();
throw new RuntimeException(t);
}
}
@TestConfiguration
static class Config {
@Bean
public DockerComposeRule dockerComposeRule() {
return docker;
}
}
@Test
@Transactional
public void deleteExpiredPayments_happyFlow() {
Instant expiryMoment = Instant.now().minus(INTERVAL_MINUTES, INTERVAL_UNIT);
for (int index = 0; index < TOTAL_PAYMENTS; index++) {
PaymentHistoryEntity paymentHistoryEntity = new PaymentHistoryEntity();
paymentHistoryEntity.setPaymentId(UUID.randomUUID());
paymentHistoryEntity.setMerchantId(UUID.randomUUID());
paymentHistoryEntity.setOrderReference(String.format("item-%d", index));
paymentHistoryEntity.setPaymentType(PaymentType.INSTANT);
paymentHistoryEntity.setStatus(OrderStatus.PROCESSING_INTERNALLY);
paymentHistoryEntity.setAddedTime(Instant.now().minus(index, INTERVAL_UNIT));
paymentHistoryEntity.setUpdatedTime(Instant.now());
paymentHistoryRepository.save(paymentHistoryEntity);
}
assertEquals(TOTAL_PAYMENTS, paymentHistoryRepository.findAll().size());
int deletedExpiredPayments = paymentHistoryRepository.deleteExpiredPayments(expiryMoment,
OrderStatus.PROCESSING_INTERNALLY);
assertEquals(EXPECTED_EXPIRED_PAYMENTS, deletedExpiredPayments);
assertEquals(TOTAL_PAYMENTS - EXPECTED_EXPIRED_PAYMENTS, paymentHistoryRepository.findAll().size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment