Skip to content

Commit

Permalink
2592 [BE] job for deleting autotest contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrsam committed Aug 19, 2024
1 parent 65e7d5e commit ad6dc59
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/main/kotlin/com/epam/brn/job/AutoTestUsersDeletionJob.kt
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions src/test/kotlin/com/epam/brn/job/AutoTestUsersDeletionJobTest.kt
Original file line number Diff line number Diff line change
@@ -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() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
autotest.users.deletion.prefix=autotest
autotest.users.deletion.cron=0 0 0 * * *

0 comments on commit ad6dc59

Please sign in to comment.