|
| 1 | +package io.tolgee.automation |
| 2 | + |
| 3 | +import io.tolgee.ProjectAuthControllerTest |
| 4 | +import io.tolgee.component.contentDelivery.ContentDeliveryFileStorageProvider |
| 5 | +import io.tolgee.component.contentDelivery.cachePurging.ContentDeliveryCachePurging |
| 6 | +import io.tolgee.component.contentDelivery.cachePurging.ContentDeliveryCachePurgingProvider |
| 7 | +import io.tolgee.component.enabledFeaturesProvider.EnabledFeaturesProvider |
| 8 | +import io.tolgee.component.fileStorage.FileStorage |
| 9 | +import io.tolgee.constants.Feature |
| 10 | +import io.tolgee.development.testDataBuilder.data.ContentDeliveryConfigBranchingTestData |
| 11 | +import io.tolgee.fixtures.andIsOk |
| 12 | +import io.tolgee.fixtures.waitForNotThrowing |
| 13 | +import io.tolgee.testing.annotations.ProjectJWTAuthTestMethod |
| 14 | +import io.tolgee.testing.assert |
| 15 | +import org.junit.jupiter.api.AfterEach |
| 16 | +import org.junit.jupiter.api.BeforeEach |
| 17 | +import org.junit.jupiter.api.Test |
| 18 | +import org.mockito.Mockito |
| 19 | +import org.mockito.kotlin.doReturn |
| 20 | +import org.mockito.kotlin.mock |
| 21 | +import org.mockito.kotlin.whenever |
| 22 | +import org.springframework.beans.factory.annotation.Autowired |
| 23 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc |
| 24 | +import org.springframework.boot.test.context.SpringBootTest |
| 25 | +import org.springframework.test.context.bean.override.mockito.MockitoBean |
| 26 | +import java.util.UUID |
| 27 | + |
| 28 | +@SpringBootTest |
| 29 | +@AutoConfigureMockMvc |
| 30 | +class ContentDeliveryBranchAutopushTest : ProjectAuthControllerTest("/v2/projects/") { |
| 31 | + @MockitoBean |
| 32 | + @Autowired |
| 33 | + lateinit var contentDeliveryFileStorageProvider: ContentDeliveryFileStorageProvider |
| 34 | + |
| 35 | + lateinit var fileStorageMock: FileStorage |
| 36 | + |
| 37 | + @MockitoBean |
| 38 | + @Autowired |
| 39 | + lateinit var contentDeliveryCachePurgingProvider: ContentDeliveryCachePurgingProvider |
| 40 | + |
| 41 | + lateinit var purgingMock: ContentDeliveryCachePurging |
| 42 | + |
| 43 | + @MockitoBean |
| 44 | + @Autowired |
| 45 | + lateinit var enabledFeaturesProvider: EnabledFeaturesProvider |
| 46 | + |
| 47 | + lateinit var testData: ContentDeliveryConfigBranchingTestData |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + fun setup() { |
| 51 | + doReturn(arrayOf(Feature.BRANCHING)).whenever(enabledFeaturesProvider).get(org.mockito.kotlin.any()) |
| 52 | + |
| 53 | + currentDateProvider.forcedDate = currentDateProvider.date |
| 54 | + testData = ContentDeliveryConfigBranchingTestData() |
| 55 | + testDataService.saveTestData(testData.root) |
| 56 | + userAccount = testData.user |
| 57 | + this.projectSupplier = { testData.projectBuilder.self } |
| 58 | + |
| 59 | + fileStorageMock = mock() |
| 60 | + doReturn(fileStorageMock).whenever(contentDeliveryFileStorageProvider).getContentStorageWithDefaultClient() |
| 61 | + purgingMock = mock() |
| 62 | + doReturn(listOf(purgingMock)).whenever(contentDeliveryCachePurgingProvider).purgings |
| 63 | + |
| 64 | + // wait for initial invocations from test data saving, then clear |
| 65 | + Thread.sleep(1000) |
| 66 | + Mockito.clearInvocations(fileStorageMock) |
| 67 | + } |
| 68 | + |
| 69 | + @AfterEach |
| 70 | + fun after() { |
| 71 | + currentDateProvider.forcedDate = null |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @ProjectJWTAuthTestMethod |
| 76 | + fun `publishes CDN only for matching branch on main branch change`() { |
| 77 | + modifyTranslationOnBranch("key", "main") |
| 78 | + waitForStoreFileCalls(1) |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @ProjectJWTAuthTestMethod |
| 83 | + fun `publishes CDN only for matching branch on feature branch change`() { |
| 84 | + modifyTranslationOnBranch("feature-key", "feature") |
| 85 | + waitForStoreFileCalls(1) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + @ProjectJWTAuthTestMethod |
| 90 | + fun `does not publish feature CDN on main branch change`() { |
| 91 | + modifyTranslationOnBranch("key", "main") |
| 92 | + // only main CDN should publish (1 storeFile call) |
| 93 | + waitForStoreFileCalls(1) |
| 94 | + |
| 95 | + Mockito.clearInvocations(fileStorageMock) |
| 96 | + |
| 97 | + modifyTranslationOnBranch("feature-key", "feature") |
| 98 | + // only feature CDN should publish (1 storeFile call) |
| 99 | + waitForStoreFileCalls(1) |
| 100 | + } |
| 101 | + |
| 102 | + private fun modifyTranslationOnBranch( |
| 103 | + keyName: String, |
| 104 | + branchName: String, |
| 105 | + ) { |
| 106 | + performProjectAuthPost( |
| 107 | + "/translations", |
| 108 | + mapOf( |
| 109 | + "key" to keyName, |
| 110 | + "translations" to mapOf("en" to UUID.randomUUID().toString()), |
| 111 | + "branch" to branchName, |
| 112 | + ), |
| 113 | + ).andIsOk |
| 114 | + } |
| 115 | + |
| 116 | + private fun waitForStoreFileCalls(expectedCount: Int) { |
| 117 | + waitForNotThrowing(timeout = 3000, pollTime = 200) { |
| 118 | + storeFileInvocations.assert.hasSize(expectedCount) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private val storeFileInvocations |
| 123 | + get() = |
| 124 | + Mockito |
| 125 | + .mockingDetails(fileStorageMock) |
| 126 | + .invocations |
| 127 | + .filter { it.method.name == "storeFile" } |
| 128 | +} |
0 commit comments