diff --git a/src/main/kotlin/com/epam/brn/controller/StudyHistoryController.kt b/src/main/kotlin/com/epam/brn/controller/StudyHistoryController.kt index c29272a46..cffb150df 100644 --- a/src/main/kotlin/com/epam/brn/controller/StudyHistoryController.kt +++ b/src/main/kotlin/com/epam/brn/controller/StudyHistoryController.kt @@ -1,8 +1,8 @@ package com.epam.brn.controller +import com.epam.brn.dto.StudyHistoryDto import com.epam.brn.dto.response.BaseResponse import com.epam.brn.dto.response.BaseSingleObjectResponse -import com.epam.brn.dto.StudyHistoryDto import com.epam.brn.service.StudyHistoryService import io.swagger.annotations.Api import io.swagger.annotations.ApiOperation @@ -15,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController +import org.springframework.web.bind.annotation.PathVariable +import java.time.LocalDateTime @RestController @RequestMapping("/study-history") @@ -40,4 +42,19 @@ class StudyHistoryController(@Autowired val studyHistoryService: StudyHistorySer @RequestParam("year", required = true) year: Int ) = ResponseEntity.ok() .body(BaseResponse(data = studyHistoryService.getMonthHistoriesForCurrentUser(month, year))) + + @GetMapping("/histories") + @ApiOperation("Get current user's study histories for period from <= startTime < to. Where from and to are dates in ISO format") + fun getHistories( + @RequestParam("from", required = true) from: LocalDateTime, + @RequestParam("to", required = true) to: LocalDateTime + ) = ResponseEntity.ok() + .body(BaseResponse(data = studyHistoryService.getHistoriesForCurrentUser(from, to))) + + @GetMapping("/user/{userId}/has/statistics") + @ApiOperation("Check if user has statistics") + fun isUserHasStatistics( + @PathVariable("userId") userId: Long + ) = ResponseEntity.ok() + .body(BaseSingleObjectResponse(data = studyHistoryService.isUserHasStatistics(userId))) } diff --git a/src/main/kotlin/com/epam/brn/controller/StudyHistoryControllerV2.kt b/src/main/kotlin/com/epam/brn/controller/StudyHistoryControllerV2.kt deleted file mode 100644 index c11f251c5..000000000 --- a/src/main/kotlin/com/epam/brn/controller/StudyHistoryControllerV2.kt +++ /dev/null @@ -1,38 +0,0 @@ -package com.epam.brn.controller - -import com.epam.brn.dto.response.BaseResponse -import com.epam.brn.dto.response.BaseSingleObjectResponse -import com.epam.brn.service.StudyHistoryService -import io.swagger.annotations.Api -import io.swagger.annotations.ApiOperation -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.http.ResponseEntity -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestParam -import org.springframework.web.bind.annotation.RestController -import java.time.LocalDateTime - -@RestController -@RequestMapping("/v2/study-history") -@Api(value = "/v2/study-history", description = "Contains actions over the results of finished exercise") -class StudyHistoryControllerV2( - @Autowired val studyHistoryService: StudyHistoryService -) { - - @GetMapping("/histories") - @ApiOperation("Get current user's study histories for period from <= startTime < to. Where from and to are dates in ISO format") - fun getHistories( - @RequestParam("from", required = true) from: LocalDateTime, - @RequestParam("to", required = true) to: LocalDateTime - ) = ResponseEntity.ok() - .body(BaseResponse(data = studyHistoryService.getHistoriesForCurrentUser(from, to))) - - @GetMapping("/user/{userId}/has/statistics") - @ApiOperation("Check if user has statistics") - fun isUserHasStatistics( - @PathVariable("userId") userId: Long - ) = ResponseEntity.ok() - .body(BaseSingleObjectResponse(data = studyHistoryService.isUserHasStatistics(userId))) -} diff --git a/src/test/kotlin/com/epam/brn/integration/StudyHistoryControllerIT.kt b/src/test/kotlin/com/epam/brn/integration/StudyHistoryControllerIT.kt index e3b06b15b..678d3d9c9 100644 --- a/src/test/kotlin/com/epam/brn/integration/StudyHistoryControllerIT.kt +++ b/src/test/kotlin/com/epam/brn/integration/StudyHistoryControllerIT.kt @@ -1,8 +1,10 @@ package com.epam.brn.integration -import com.epam.brn.dto.response.BaseSingleObjectResponse import com.epam.brn.dto.StudyHistoryDto +import com.epam.brn.dto.response.BaseResponse +import com.epam.brn.dto.response.BaseSingleObjectResponse import com.epam.brn.repo.StudyHistoryRepository +import com.fasterxml.jackson.core.type.TypeReference import com.google.gson.Gson import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test @@ -15,7 +17,9 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import java.nio.charset.StandardCharsets import java.time.LocalDateTime import java.time.format.DateTimeFormatter +import kotlin.test.assertEquals import kotlin.test.assertNotNull +import kotlin.test.assertTrue @WithMockUser(username = "test@test.test", roles = ["ADMIN"]) class StudyHistoryControllerIT : BaseIT() { @@ -82,4 +86,126 @@ class StudyHistoryControllerIT : BaseIT() { // THEN assertNotNull(singleObjectResponseDto) } + + @Test + @WithMockUser(username = "test@test.test", roles = ["ADMIN"]) + fun `getHistories should return histories for period of time`() { + // GIVEN + val user = insertDefaultUser() + val exercise = insertDefaultExercise() + val exercisingYear = 2019 + val exercisingMonth = 3 + val studyHistoryFirst = + insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 13, 0), 25) + val studyHistorySecond = + insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 14, 0), 25) + val from = LocalDateTime.of(exercisingYear, exercisingMonth, 1, 1, 1) + val to = LocalDateTime.of(exercisingYear, exercisingMonth, 28, 1, 1) + val datePattern = DateTimeFormatter.ISO_DATE_TIME + val expectedStudyHistories = listOf(studyHistoryFirst.toDto(), studyHistorySecond.toDto()) + + // WHEN + val response = mockMvc.perform( + get("$baseUrl/histories") + .param(fromParameterName, from.format(datePattern)) + .param(toParameterName, to.format(datePattern)) + ) + .andExpect(status().isOk) + .andReturn().response.getContentAsString(StandardCharsets.UTF_8) + + // THEN + val data = objectMapper.readValue(response, BaseResponse::class.java).data + val studyHistories: List = + objectMapper.readValue( + objectMapper.writeValueAsString(data), + object : TypeReference>() {} + ) + + assertNotNull(studyHistories) + assertEquals(expectedStudyHistories, studyHistories) + } + + @Test + @WithMockUser(username = "test@test.test", roles = ["USER"]) + fun `getHistories should return histories for period of time for user with role user`() { + // GIVEN + val user = insertDefaultUser() + val exercise = insertDefaultExercise() + val exercisingYear = 2019 + val exercisingMonth = 3 + val studyHistoryFirst = + insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 13, 0), 25) + val studyHistorySecond = + insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 14, 0), 25) + val from = LocalDateTime.of(exercisingYear, exercisingMonth, 1, 1, 1) + val to = LocalDateTime.of(exercisingYear, exercisingMonth, 28, 1, 1) + val datePattern = DateTimeFormatter.ISO_DATE_TIME + val expectedStudyHistories = listOf(studyHistoryFirst.toDto(), studyHistorySecond.toDto()) + + // WHEN + val response = mockMvc.perform( + get("$baseUrl/histories") + .param(fromParameterName, from.format(datePattern)) + .param(toParameterName, to.format(datePattern)) + ) + .andExpect(status().isOk) + .andReturn().response.getContentAsString(StandardCharsets.UTF_8) + + // THEN + val data = objectMapper.readValue(response, BaseResponse::class.java).data + val studyHistories: List = + objectMapper.readValue( + objectMapper.writeValueAsString(data), + object : TypeReference>() {} + ) + + assertNotNull(studyHistories) + assertEquals(expectedStudyHistories, studyHistories) + } + + @Test + @WithMockUser(username = "test@test.test", roles = ["ADMIN"]) + fun `isUserHasStatistics should return true when user has statistics`() { + // GIVEN + val user = insertDefaultUser() + val exercise = insertDefaultExercise() + val exercisingYear = 2019 + val exercisingMonth = 3 + insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 13, 0), 25) + + // WHEN + val response = mockMvc.perform( + get("$baseUrl/user/{userId}/has/statistics", user.id) + ).andExpect(status().isOk) + .andReturn().response.getContentAsString(StandardCharsets.UTF_8) + + // THEN + val data = objectMapper.readValue(response, BaseSingleObjectResponse::class.java).data + val isUserHasStatistics: Boolean = + objectMapper.readValue(objectMapper.writeValueAsString(data), object : TypeReference() {}) + assertTrue(isUserHasStatistics) + } + + @Test + @WithMockUser(username = "test@test.test", roles = ["USER"]) + fun `isUserHasStatistics should return true when user has statistics for user with role user`() { + // GIVEN + val user = insertDefaultUser() + val exercise = insertDefaultExercise() + val exercisingYear = 2019 + val exercisingMonth = 3 + insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 13, 0), 25) + + // WHEN + val response = mockMvc.perform( + get("$baseUrl/user/{userId}/has/statistics", user.id) + ).andExpect(status().isOk) + .andReturn().response.getContentAsString(StandardCharsets.UTF_8) + + // THEN + val data = objectMapper.readValue(response, BaseSingleObjectResponse::class.java).data + val isUserHasStatistics: Boolean = + objectMapper.readValue(objectMapper.writeValueAsString(data), object : TypeReference() {}) + assertTrue(isUserHasStatistics) + } } diff --git a/src/test/kotlin/com/epam/brn/integration/StudyHistoryControllerV2IT.kt b/src/test/kotlin/com/epam/brn/integration/StudyHistoryControllerV2IT.kt deleted file mode 100644 index 3faef53bf..000000000 --- a/src/test/kotlin/com/epam/brn/integration/StudyHistoryControllerV2IT.kt +++ /dev/null @@ -1,151 +0,0 @@ -package com.epam.brn.integration - -import com.epam.brn.dto.StudyHistoryDto -import com.epam.brn.dto.response.BaseResponse -import com.epam.brn.dto.response.BaseSingleObjectResponse -import com.fasterxml.jackson.core.type.TypeReference -import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.Test -import org.springframework.security.test.context.support.WithMockUser -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders -import org.springframework.test.web.servlet.result.MockMvcResultMatchers -import java.nio.charset.StandardCharsets -import java.time.LocalDateTime -import java.time.format.DateTimeFormatter -import kotlin.test.assertEquals -import kotlin.test.assertNotNull -import kotlin.test.assertTrue - -class StudyHistoryControllerV2IT : BaseIT() { - - private val baseUrl = "/v2/study-history" - private val fromParameterName = "from" - private val toParameterName = "to" - - @AfterEach - fun rollback() { - deleteInsertedTestData() - } - - @Test - @WithMockUser(username = "test@test.test", roles = ["ADMIN"]) - fun `getHistories should return histories for period of time`() { - // GIVEN - val user = insertDefaultUser() - val exercise = insertDefaultExercise() - val exercisingYear = 2019 - val exercisingMonth = 3 - val studyHistoryFirst = - insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 13, 0), 25) - val studyHistorySecond = - insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 14, 0), 25) - val from = LocalDateTime.of(exercisingYear, exercisingMonth, 1, 1, 1) - val to = LocalDateTime.of(exercisingYear, exercisingMonth, 28, 1, 1) - val datePattern = DateTimeFormatter.ISO_DATE_TIME - val expectedStudyHistories = listOf(studyHistoryFirst.toDto(), studyHistorySecond.toDto()) - - // WHEN - val response = mockMvc.perform( - MockMvcRequestBuilders.get("$baseUrl/histories") - .param(fromParameterName, from.format(datePattern)) - .param(toParameterName, to.format(datePattern)) - ) - .andExpect(MockMvcResultMatchers.status().isOk) - .andReturn().response.getContentAsString(StandardCharsets.UTF_8) - - // THEN - val data = objectMapper.readValue(response, BaseResponse::class.java).data - val studyHistories: List = - objectMapper.readValue( - objectMapper.writeValueAsString(data), - object : TypeReference>() {} - ) - - assertNotNull(studyHistories) - assertEquals(expectedStudyHistories, studyHistories) - } - - @Test - @WithMockUser(username = "test@test.test", roles = ["USER"]) - fun `getHistories should return histories for period of time for user with role user`() { - // GIVEN - val user = insertDefaultUser() - val exercise = insertDefaultExercise() - val exercisingYear = 2019 - val exercisingMonth = 3 - val studyHistoryFirst = - insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 13, 0), 25) - val studyHistorySecond = - insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 14, 0), 25) - val from = LocalDateTime.of(exercisingYear, exercisingMonth, 1, 1, 1) - val to = LocalDateTime.of(exercisingYear, exercisingMonth, 28, 1, 1) - val datePattern = DateTimeFormatter.ISO_DATE_TIME - val expectedStudyHistories = listOf(studyHistoryFirst.toDto(), studyHistorySecond.toDto()) - - // WHEN - val response = mockMvc.perform( - MockMvcRequestBuilders.get("$baseUrl/histories") - .param(fromParameterName, from.format(datePattern)) - .param(toParameterName, to.format(datePattern)) - ) - .andExpect(MockMvcResultMatchers.status().isOk) - .andReturn().response.getContentAsString(StandardCharsets.UTF_8) - - // THEN - val data = objectMapper.readValue(response, BaseResponse::class.java).data - val studyHistories: List = - objectMapper.readValue( - objectMapper.writeValueAsString(data), - object : TypeReference>() {} - ) - - assertNotNull(studyHistories) - assertEquals(expectedStudyHistories, studyHistories) - } - - @Test - @WithMockUser(username = "test@test.test", roles = ["ADMIN"]) - fun `isUserHasStatistics should return true when user has statistics`() { - // GIVEN - val user = insertDefaultUser() - val exercise = insertDefaultExercise() - val exercisingYear = 2019 - val exercisingMonth = 3 - insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 13, 0), 25) - - // WHEN - val response = mockMvc.perform( - MockMvcRequestBuilders.get("$baseUrl/user/{userId}/has/statistics", user.id) - ).andExpect(MockMvcResultMatchers.status().isOk) - .andReturn().response.getContentAsString(StandardCharsets.UTF_8) - - // THEN - val data = objectMapper.readValue(response, BaseSingleObjectResponse::class.java).data - val isUserHasStatistics: Boolean = - objectMapper.readValue(objectMapper.writeValueAsString(data), object : TypeReference() {}) - assertTrue(isUserHasStatistics) - } - - @Test - @WithMockUser(username = "test@test.test", roles = ["USER"]) - fun `isUserHasStatistics should return true when user has statistics for user with role user`() { - // GIVEN - val user = insertDefaultUser() - val exercise = insertDefaultExercise() - val exercisingYear = 2019 - val exercisingMonth = 3 - insertDefaultStudyHistory(user, exercise, LocalDateTime.of(exercisingYear, exercisingMonth, 20, 13, 0), 25) - - // WHEN - val response = mockMvc.perform( - MockMvcRequestBuilders.get("$baseUrl/user/{userId}/has/statistics", user.id) - ).andExpect(MockMvcResultMatchers.status().isOk) - .andReturn().response.getContentAsString(StandardCharsets.UTF_8) - - // THEN - val data = objectMapper.readValue(response, BaseSingleObjectResponse::class.java).data - val isUserHasStatistics: Boolean = - objectMapper.readValue(objectMapper.writeValueAsString(data), object : TypeReference() {}) - assertTrue(isUserHasStatistics) - } -}