From ad6dc591ab11172867193a2e3f38bc4b2e9191f9 Mon Sep 17 00:00:00 2001 From: andrsam Date: Mon, 19 Aug 2024 09:24:48 +0300 Subject: [PATCH] 2592 [BE] job for deleting autotest contributors --- .../epam/brn/job/AutoTestUsersDeletionJob.kt | 17 ++++++++ .../service/impl/UserAccountServiceImpl.kt | 2 +- src/main/resources/application.properties | 3 +- .../integration/UserDetailsControllerIT.kt | 6 +-- .../brn/job/AutoTestUsersDeletionJobTest.kt | 40 +++++++++++++++++++ .../brn/service/UserAccountServiceTest.kt | 1 - src/test/resources/application.properties | 3 +- 7 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/com/epam/brn/job/AutoTestUsersDeletionJob.kt create mode 100644 src/test/kotlin/com/epam/brn/job/AutoTestUsersDeletionJobTest.kt diff --git a/src/main/kotlin/com/epam/brn/job/AutoTestUsersDeletionJob.kt b/src/main/kotlin/com/epam/brn/job/AutoTestUsersDeletionJob.kt new file mode 100644 index 000000000..700aac29d --- /dev/null +++ b/src/main/kotlin/com/epam/brn/job/AutoTestUsersDeletionJob.kt @@ -0,0 +1,17 @@ +package com.epam.brn.job + +import com.epam.brn.service.UserAccountService +import org.apache.logging.log4j.kotlin.logger +import org.springframework.scheduling.annotation.Scheduled +import org.springframework.stereotype.Component + +@Component +class AutoTestUsersDeletionJob(val userAccountService: UserAccountService) { + private val log = logger() + + @Scheduled(cron = "\${autotest.users.deletion.cron}") + fun deleteAutoTestUsers() { + val usersCount = userAccountService.deleteAutoTestUsers() + log.info("Deleted $usersCount autotest users") + } +} diff --git a/src/main/kotlin/com/epam/brn/service/impl/UserAccountServiceImpl.kt b/src/main/kotlin/com/epam/brn/service/impl/UserAccountServiceImpl.kt index 85d53ebec..3d6e89d65 100644 --- a/src/main/kotlin/com/epam/brn/service/impl/UserAccountServiceImpl.kt +++ b/src/main/kotlin/com/epam/brn/service/impl/UserAccountServiceImpl.kt @@ -29,7 +29,7 @@ class UserAccountServiceImpl( private val headphonesService: HeadphonesService, private val timeService: TimeService, ) : UserAccountService { - @Value("\${users.delete.prefix}") + @Value("\${autotest.users.deletion.prefix}") private lateinit var prefix: String override fun findUserByEmail(email: String): UserAccountDto = diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 181712d9a..bbc57eae6 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -152,7 +152,8 @@ github.api.url.base=https://api.github.com github.api.url.path.contributors=/repos/{OWNER}/{REPO}/contributors github.api.url.path.users=/users/{username} -users.delete.prefix=autotest +autotest.users.deletion.prefix=autotest +autotest.users.deletion.job.cron=0 0 0 * * * # Swagger springdoc.swagger-ui.tagsSorter=alpha diff --git a/src/test/kotlin/com/epam/brn/integration/UserDetailsControllerIT.kt b/src/test/kotlin/com/epam/brn/integration/UserDetailsControllerIT.kt index e1f0c9dfc..4ab2f94f4 100644 --- a/src/test/kotlin/com/epam/brn/integration/UserDetailsControllerIT.kt +++ b/src/test/kotlin/com/epam/brn/integration/UserDetailsControllerIT.kt @@ -22,14 +22,13 @@ import org.assertj.core.api.Assertions import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired -import org.springframework.beans.factory.annotation.Value import org.springframework.security.test.context.support.WithMockUser import org.springframework.test.web.servlet.ResultActions import org.springframework.test.web.servlet.request.MockMvcRequestBuilders import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import java.nio.charset.StandardCharsets @@ -49,9 +48,6 @@ class UserDetailsControllerIT : BaseIT() { @Autowired lateinit var roleRepository: RoleRepository - @Value("\${users.delete.prefix}") - private lateinit var prefix: String - internal val email: String = "test@test.test" private val baseUrl = "/users" private val currentUserBaseUrl = "$baseUrl/current" diff --git a/src/test/kotlin/com/epam/brn/job/AutoTestUsersDeletionJobTest.kt b/src/test/kotlin/com/epam/brn/job/AutoTestUsersDeletionJobTest.kt new file mode 100644 index 000000000..e68cac7f6 --- /dev/null +++ b/src/test/kotlin/com/epam/brn/job/AutoTestUsersDeletionJobTest.kt @@ -0,0 +1,40 @@ +package com.epam.brn.job + +import com.epam.brn.repo.UserAccountRepository +import com.epam.brn.service.impl.UserAccountServiceImpl +import io.mockk.every +import io.mockk.impl.annotations.InjectMockKs +import io.mockk.impl.annotations.MockK +import io.mockk.junit5.MockKExtension +import io.mockk.verify +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.test.util.ReflectionTestUtils + +@DisplayName("AutoTestUsersDeletionJob test using MockK") +@ExtendWith(MockKExtension::class) +class AutoTestUsersDeletionJobTest { + + @InjectMockKs + lateinit var autoTestUsersDeletionJob: AutoTestUsersDeletionJob + + @MockK + lateinit var userAccountService: UserAccountServiceImpl + + @MockK + lateinit var userAccountRepository: UserAccountRepository + @Test + fun deleteAutoTestUsers() { + // GIVEN + val prefix = "autotest" + ReflectionTestUtils.setField(userAccountService, "prefix", prefix) + every { userAccountService.deleteAutoTestUsers() } returns 2L + + // WHEN + autoTestUsersDeletionJob.deleteAutoTestUsers() + + // THEN + verify { userAccountService.deleteAutoTestUsers() } + } +} diff --git a/src/test/kotlin/com/epam/brn/service/UserAccountServiceTest.kt b/src/test/kotlin/com/epam/brn/service/UserAccountServiceTest.kt index f7a681fdb..e8ef12f29 100644 --- a/src/test/kotlin/com/epam/brn/service/UserAccountServiceTest.kt +++ b/src/test/kotlin/com/epam/brn/service/UserAccountServiceTest.kt @@ -533,7 +533,6 @@ internal class UserAccountServiceTest { @Test fun `should throw IllegalArgumentException when email not starts from prefix`() { // GIVEN - val usersCount = 1L val email = "aaa@bbb.com" val prefix = "autotest" ReflectionTestUtils.setField(userAccountService, "prefix", prefix) diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 8b4f9b73a..15e3083f3 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -131,4 +131,5 @@ github.api.url.base=https://api.github.com github.api.url.path.contributors=/repos/{OWNER}/{REPO}/contributors github.api.url.path.users=/users/{username} -users.delete.prefix=autotest \ No newline at end of file +autotest.users.deletion.prefix=autotest +autotest.users.deletion.cron=0 0 0 * * * \ No newline at end of file