Skip to content

Commit

Permalink
2369 add caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Elena Moshnikova committed Nov 17, 2024
1 parent a0e5f7e commit a872c88
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 83 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/epam/brn/model/ExerciseGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.epam.brn.upload.csv.group.GroupRecord
import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
Expand All @@ -27,7 +28,7 @@ data class ExerciseGroup(
val name: String,
@Column
val description: String? = "",
@OneToMany(mappedBy = "exerciseGroup", cascade = [(CascadeType.ALL)])
@OneToMany(mappedBy = "exerciseGroup", fetch = FetchType.LAZY, cascade = [(CascadeType.ALL)])
val series: MutableList<Series> = ArrayList()
) {
constructor(record: GroupRecord) : this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ExerciseGroupsService(
@Cacheable("groupsByLocale")
fun findByLocale(locale: String): List<ExerciseGroupDto> {
return if (locale.isEmpty())
exerciseGroupRepository.findAll().map { group -> group.toDto() }
exerciseGroupRepository.findAll().map { group -> group.toDtoWithoutSeries() }
else exerciseGroupRepository.findByLocale(locale)
.map { group -> group.toDtoWithoutSeries() }
}
Expand Down
27 changes: 12 additions & 15 deletions src/test/kotlin/com/epam/brn/integration/BaseIT.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package com.epam.brn.integration

import com.epam.brn.model.Role
import com.epam.brn.enums.BrnGender
import com.epam.brn.model.Exercise
import com.epam.brn.model.ExerciseGroup
import com.epam.brn.enums.BrnGender
import com.epam.brn.model.Role
import com.epam.brn.model.Series
import com.epam.brn.model.StudyHistory
import com.epam.brn.model.SubGroup
import com.epam.brn.model.UserAccount
import com.epam.brn.repo.RoleRepository
import com.epam.brn.repo.ExerciseGroupRepository
import com.epam.brn.repo.ExerciseRepository
import com.epam.brn.repo.RoleRepository
import com.epam.brn.repo.SeriesRepository
import com.epam.brn.repo.StudyHistoryRepository
import com.epam.brn.repo.SubGroupRepository
import com.epam.brn.repo.TaskRepository
import com.epam.brn.repo.UserAccountRepository
import com.fasterxml.jackson.databind.ObjectMapper
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import org.amshove.kluent.internal.platformClassName
import org.junit.jupiter.api.Tag
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -24,9 +28,6 @@ import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.web.servlet.MockMvc
import org.testcontainers.junit.jupiter.Testcontainers
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
Expand All @@ -43,22 +44,18 @@ abstract class BaseIT {

@Autowired
private lateinit var userAccountRepository: UserAccountRepository

@Autowired
private lateinit var studyHistoryRepository: StudyHistoryRepository

@Autowired
private lateinit var exerciseRepository: ExerciseRepository

private lateinit var exerciseGroupRepository: ExerciseGroupRepository
@Autowired
private lateinit var seriesRepository: SeriesRepository
@Autowired
private lateinit var subGroupRepository: SubGroupRepository

@Autowired
private lateinit var exerciseGroupRepository: ExerciseGroupRepository

private lateinit var exerciseRepository: ExerciseRepository
@Autowired
private lateinit var seriesRepository: SeriesRepository

private lateinit var taskRepository: TaskRepository
@Autowired
private lateinit var roleRepository: RoleRepository

Expand Down
29 changes: 9 additions & 20 deletions src/test/kotlin/com/epam/brn/integration/ExercisesControllerIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.epam.brn.repo.SeriesRepository
import com.epam.brn.repo.StudyHistoryRepository
import com.epam.brn.repo.SubGroupRepository
import com.epam.brn.repo.UserAccountRepository
import com.google.gson.Gson
import org.json.JSONObject
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions
Expand Down Expand Up @@ -60,9 +59,6 @@ class ExercisesControllerIT : BaseIT() {
@Autowired
lateinit var exerciseGroupRepository: ExerciseGroupRepository

@Autowired
lateinit var gson: Gson

@AfterEach
fun deleteAfterTest() {
studyHistoryRepository.deleteAll()
Expand All @@ -80,7 +76,7 @@ class ExercisesControllerIT : BaseIT() {
val exerciseName = "ExerciseNameTest"
val existingSeries = insertSeries()
val subGroup = insertSubGroup(existingSeries)
val existingExercise = insertExercise(exerciseName, subGroup)
val existingExercise = insertExercise(subGroup, exerciseName)
insertStudyHistory(existingUser, existingExercise, LocalDateTime.now().minusHours(1))
insertStudyHistory(existingUser, existingExercise, LocalDateTime.now())
// WHEN
Expand All @@ -103,7 +99,7 @@ class ExercisesControllerIT : BaseIT() {
val exerciseName = "ExerciseNameTest"
val existingSeries = insertSeries()
val subGroup = insertSubGroup(existingSeries)
val existingExercise = insertExercise(exerciseName, subGroup)
val existingExercise = insertExercise(subGroup, exerciseName)
// WHEN
val resultAction = mockMvc.perform(
MockMvcRequestBuilders
Expand Down Expand Up @@ -131,7 +127,7 @@ class ExercisesControllerIT : BaseIT() {
val exerciseName = "ExerciseNameTest"
val existingSeries = insertSeries()
val subGroup = insertSubGroup(existingSeries)
val exercise = insertExercise(exerciseName, subGroup)
val exercise = insertExercise(subGroup, exerciseName)
val requestJson: String = objectMapper.writeValueAsString(ExerciseRequest(listOf(exercise.id!!)))
// WHEN
val resultAction = mockMvc.perform(
Expand Down Expand Up @@ -299,12 +295,11 @@ class ExercisesControllerIT : BaseIT() {
)
}

private fun insertSubGroup(series: Series): SubGroup = subGroupRepository.save(
SubGroup(series = series, level = 1, code = "code", name = "subGroup name")
)
private fun insertSubGroup(series: Series): SubGroup =
subGroupRepository.save(SubGroup(series = series, level = 1, code = "code", name = "subGroup name"))

fun insertExercise(exerciseName: String, subGroup: SubGroup): Exercise {
return exerciseRepository.save(
fun insertExercise(subGroup: SubGroup, exerciseName: String): Exercise =
exerciseRepository.save(
Exercise(
subGroup = subGroup,
level = 0,
Expand All @@ -313,13 +308,7 @@ class ExercisesControllerIT : BaseIT() {
noiseUrl = "/testNoiseUrl"
)
)
}

fun insertExercise(exerciseName: String): Exercise {
return exerciseRepository.save(
Exercise(
name = exerciseName
)
)
}
fun insertExercise(exerciseName: String): Exercise =
exerciseRepository.save(Exercise(name = exerciseName))
}
94 changes: 68 additions & 26 deletions src/test/kotlin/com/epam/brn/integration/GroupControllerIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ package com.epam.brn.integration

import com.epam.brn.enums.BrnLocale
import com.epam.brn.enums.BrnRole
import com.epam.brn.repo.ExerciseGroupRepository
import com.epam.brn.enums.ExerciseType
import com.epam.brn.model.Exercise
import com.epam.brn.model.ExerciseGroup
import com.epam.brn.model.Series
import com.epam.brn.model.SubGroup
import com.epam.brn.model.Task
import com.epam.brn.repo.ExerciseGroupRepository
import com.epam.brn.repo.ExerciseRepository
import com.epam.brn.repo.SeriesRepository
import com.epam.brn.repo.SubGroupRepository
import com.epam.brn.repo.TaskRepository
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
Expand All @@ -21,42 +30,34 @@ class GroupControllerIT : BaseIT() {
private val baseUrl = "/groups"

@Autowired
lateinit var exerciseGroupRepository: ExerciseGroupRepository
private lateinit var exerciseGroupRepository: ExerciseGroupRepository
@Autowired
private lateinit var seriesRepository: SeriesRepository
@Autowired
private lateinit var subGroupRepository: SubGroupRepository
@Autowired
private lateinit var exerciseRepository: ExerciseRepository
@Autowired
private lateinit var taskRepository: TaskRepository

@AfterEach
fun deleteAfterTest() {
exerciseGroupRepository.deleteAll()
}

@Test
fun `test get all groups`() {
// GIVEN
val exerciseGroupName1 = "GroupName1"
val existingExerciseGroup1 = insertExerciseGroup(exerciseGroupName1)
val exerciseGroupName2 = "GroupName2"
val existingExerciseGroup2 = insertExerciseGroup(exerciseGroupName2)
// WHEN
val resultAction = mockMvc.perform(
MockMvcRequestBuilders
.get(baseUrl)
.contentType(MediaType.APPLICATION_JSON)
)
// THEN
resultAction
.andExpect(status().isOk)
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
val response = resultAction.andReturn().response.contentAsString
assertTrue(response.contains(existingExerciseGroup1.name))
assertTrue(response.contains(existingExerciseGroup2.name))
}

@Test
fun `test find ru groups`() {
// GIVEN
val exerciseGroupName1 = "GroupName1"
val groupRu = insertExerciseGroup(exerciseGroupName1, BrnLocale.RU.locale)
val exerciseGroupName2 = "GroupName2"
val groupEn = insertExerciseGroup(exerciseGroupName2, BrnLocale.EN.locale)
val series1 = insertSeries(groupRu, "series1Name")
val series2 = insertSeries(groupRu, "series2Name")
val subGroup1 = insertSubGroup(series1, 1)
val subGroup2 = insertSubGroup(series1, 2)
val exercise = insertExercise(subGroup1, "ex1")
val task = insertTask(exercise)
// WHEN
val resultAction = mockMvc.perform(
MockMvcRequestBuilders
Expand Down Expand Up @@ -92,14 +93,55 @@ class GroupControllerIT : BaseIT() {
assertTrue(response.contains(existingExerciseGroup.name))
}

private fun insertExerciseGroup(exerciseGroupName: String, locale: String = BrnLocale.RU.locale): ExerciseGroup {
return exerciseGroupRepository.save(
fun insertExerciseGroup(exerciseGroupName: String, locale: String = BrnLocale.RU.locale): ExerciseGroup =
exerciseGroupRepository.save(
ExerciseGroup(
code = "CODE",
description = "desc",
name = exerciseGroupName,
locale = locale
)
)

fun insertSeries(group: ExerciseGroup, name: String): Series {
val series = Series(
name = name,
description = "description",
exerciseGroup = group,
level = 1,
type = ExerciseType.SINGLE_SIMPLE_WORDS.name
)
return seriesRepository.save(series)
}

fun insertSubGroup(series: Series, level: Int): SubGroup =
subGroupRepository.save(
SubGroup(
series = series,
level = level,
code = "code",
name = "${series.name}subGroupName$level"
)
)

fun insertExercise(subGroup: SubGroup, exerciseName: String): Exercise =
exerciseRepository.save(
Exercise(
subGroup = subGroup,
level = 0,
name = exerciseName,
noiseLevel = 50,
noiseUrl = "/testNoiseUrl"
)
)

fun insertTask(exercise: Exercise): Task =
taskRepository.save(
Task(
id = 1,
name = "${exercise.name} Task",
serialNumber = 1,
exercise = exercise
)
)
}
18 changes: 12 additions & 6 deletions src/test/kotlin/com/epam/brn/integration/SeriesControllerIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.epam.brn.repo.SeriesRepository
import com.epam.brn.dto.SeriesDto
import com.epam.brn.dto.response.BrnResponse
import com.epam.brn.enums.BrnRole
import com.epam.brn.model.ExerciseGroup
import com.epam.brn.enums.ExerciseType
import com.epam.brn.model.ExerciseGroup
import com.epam.brn.model.Series
import com.fasterxml.jackson.core.type.TypeReference
import com.google.gson.Gson
Expand Down Expand Up @@ -47,11 +47,6 @@ class SeriesControllerIT : BaseIT() {
return exerciseGroupRepository.save(group)
}

private fun insertSeries(group: ExerciseGroup, name: String): Series {
val series = Series(name = name, description = "description", exerciseGroup = group, level = 1, type = ExerciseType.SINGLE_SIMPLE_WORDS.name)
return seriesRepository.save(series)
}

@Test
fun `test get series for group`() {
// GIVEN
Expand Down Expand Up @@ -120,4 +115,15 @@ class SeriesControllerIT : BaseIT() {
Assertions.assertTrue(response.contains("1,family,Семья,(сын ребёнок мама),0,"))
Assertions.assertEquals(expectedResponse, response)
}

fun insertSeries(group: ExerciseGroup, name: String): Series {
val series = Series(
name = name,
description = "description",
exerciseGroup = group,
level = 1,
type = ExerciseType.SINGLE_SIMPLE_WORDS.name
)
return seriesRepository.save(series)
}
}
13 changes: 9 additions & 4 deletions src/test/kotlin/com/epam/brn/integration/SubGroupControllerIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ class SubGroupControllerIT : BaseIT() {
.contentType(MediaType.APPLICATION_JSON)
)
// THEN
resultAction
.andExpect(status().isOk)
resultAction.andExpect(status().isOk)
}

@Test
Expand All @@ -115,7 +114,10 @@ class SubGroupControllerIT : BaseIT() {
resultAction
.andExpect(status().isBadRequest)
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.content().string(containsString("Can not delete subGroup because there are exercises that refer to the subGroup.")))
.andExpect(
MockMvcResultMatchers.content()
.string(containsString("Can not delete subGroup because there are exercises that refer to the subGroup."))
)
}

@Test
Expand All @@ -133,7 +135,10 @@ class SubGroupControllerIT : BaseIT() {
resultAction
.andExpect(status().isBadRequest)
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.content().string(containsString("Can not delete subGroup because subGroup is not found by this id.")))
.andExpect(
MockMvcResultMatchers.content()
.string(containsString("Can not delete subGroup because subGroup is not found by this id."))
)
}

@Test
Expand Down
Loading

0 comments on commit a872c88

Please sign in to comment.