Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand All @@ -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)))
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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<StudyHistoryDto> =
objectMapper.readValue(
objectMapper.writeValueAsString(data),
object : TypeReference<List<StudyHistoryDto>>() {}
)

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<StudyHistoryDto> =
objectMapper.readValue(
objectMapper.writeValueAsString(data),
object : TypeReference<List<StudyHistoryDto>>() {}
)

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<Boolean>() {})
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<Boolean>() {})
assertTrue(isUserHasStatistics)
}
}

This file was deleted.