|
| 1 | +package com.trendyol.recomengine.webservice.controller; |
| 2 | + |
| 3 | +import com.trendyol.recomengine.webservice.model.Recommendation; |
| 4 | +import com.trendyol.recomengine.webservice.repository.RecommendationRepository; |
| 5 | +import com.trendyol.recomengine.webservice.service.RecommendationService; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.boot.test.util.TestPropertyValues; |
| 12 | +import org.springframework.context.ApplicationContextInitializer; |
| 13 | +import org.springframework.context.ConfigurableApplicationContext; |
| 14 | +import org.springframework.context.event.ContextClosedEvent; |
| 15 | +import org.springframework.test.context.ActiveProfiles; |
| 16 | +import org.springframework.test.context.ContextConfiguration; |
| 17 | +import org.springframework.test.context.junit4.SpringRunner; |
| 18 | +import org.testcontainers.containers.GenericContainer; |
| 19 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 20 | + |
| 21 | +@RunWith(SpringRunner.class) |
| 22 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) |
| 23 | +@ActiveProfiles("test") |
| 24 | +@ContextConfiguration(initializers = BaseMongoIntegrationTest.Initializer.class) |
| 25 | +public class BaseMongoIntegrationTest { |
| 26 | + private static final int MONGO_PORT = 27017; |
| 27 | + |
| 28 | + private static GenericContainer mongoContainer = new GenericContainer("mongo:latest") |
| 29 | + .withExposedPorts(MONGO_PORT) |
| 30 | + .waitingFor(Wait.forListeningPort()); |
| 31 | + @Autowired |
| 32 | + RecommendationRepository recommendationRepository; |
| 33 | + @Autowired |
| 34 | + RecommendationService recommendationService; |
| 35 | + |
| 36 | + @Test |
| 37 | + public void dummyTest() { |
| 38 | + String userId = "3"; |
| 39 | + String[] recommendations = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; |
| 40 | + |
| 41 | + Recommendation recommendation = new Recommendation(userId, recommendations); |
| 42 | + recommendationRepository.save(recommendation); |
| 43 | + Assert.assertEquals(recommendationService.getRecommendations(userId).get_id(), userId); |
| 44 | + } |
| 45 | + |
| 46 | + static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
| 47 | + @Override |
| 48 | + public void initialize(ConfigurableApplicationContext applicationContext) { |
| 49 | + mongoContainer.start(); |
| 50 | + |
| 51 | + TestPropertyValues.of("spring.data.mongodb.uri=mongodb://localhost:" + mongoContainer.getMappedPort(MONGO_PORT)) |
| 52 | + .applyTo(applicationContext); |
| 53 | + |
| 54 | + applicationContext.addApplicationListener( |
| 55 | + applicationEvent -> { |
| 56 | + if (applicationEvent instanceof ContextClosedEvent) { |
| 57 | + mongoContainer.stop(); |
| 58 | + } |
| 59 | + } |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
0 commit comments