Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Merge pull request #14 from fictadvisor/schedule_api_tests
Browse files Browse the repository at this point in the history
Schedule api tests
  • Loading branch information
vita133 authored Apr 11, 2024
2 parents d976d2b + 32f2563 commit e9a1e39
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.fictadvisor.android.data.dto.schedule
data class DetailedEventResponse(
val url: String,
val eventInfo: String,
val disciplineType: TDiscipline,
val eventType: TDiscipline,
val disciplineInfo: String,
val period: TEventPeriod,
val teachers: List<Teacher>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package com.fictadvisor.android.data.dto.schedule

data class PatchEventDTO(
val week: Int,
val name: String,
val changeStartDate: Boolean,
val changeEndDate: Boolean,
val disciplineType: String? = null,
val eventType: TDiscipline? = null,
val url: String,
val eventInfo: String,
val disciplineInfo: String,
val period: TEventPeriod,
val teachers: List<String>,
val disciplineId: String
val disciplineId: String,
val startTime: String,
val endTime: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package com.fictadvisor.android.data.dto.schedule

data class PostEventDTO (
val groupId: String,
val name: String,
val teachers: List<String>,
val disciplineId: String,
val url: String,
val eventInfo: String,
val disciplineType: TDiscipline,
val eventType: TDiscipline,
val disciplineInfo: String,
val period: TEventPeriod,
)
val startTime: String,
val endTime: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Path
Expand All @@ -25,6 +26,7 @@ interface ScheduleApi {

@GET("/v2/schedule/groups/{groupId}/events")
suspend fun getEventsAuthorized(
@Header("Authorization") token: String,
@Path("groupId") groupId: String,
@Query("week") week: Int,
@Query("showOwnSelective") showOwnSelective: Boolean,
Expand All @@ -36,24 +38,28 @@ interface ScheduleApi {

@GET("/v2/schedule/events/{eventId}")
suspend fun getEventInfo(
@Header("Authorization") token: String,
@Path("eventId") eventId: String,
@Query("week") week: Any // You can define the type accordingly
): Response<DetailedEventResponse>

@DELETE("/v2/schedule/groups/{groupId}/events/{eventId}")
suspend fun deleteEventById(
@Header("Authorization") token: String,
@Path("groupId") groupId: String,
@Path("eventId") eventId: String
): Response<DetailedEventResponse>

@POST("/v2/schedule/events")
suspend fun addEvent(
@Header("Authorization") token: String,
@Body body: PostEventDTO,
@Query("groupId") groupId: String
): Response<DetailedEventResponse>

@PATCH("/v2/schedule/groups/{groupId}/events/{eventId}")
suspend fun editEvent(
@Header("Authorization") token: String,
@Body body: PatchEventDTO,
@Path("groupId") groupId: String,
@Path("eventId") eventId: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ class ScheduleRepository {
return scheduleService.getEvents(groupId, week)
}

suspend fun getEventsAuthorized(groupId: String, week: Int, showOwnSelective: Boolean): Response<GetEventResponse> {
return scheduleService.getEventsAuthorized(groupId, week, showOwnSelective)
suspend fun getEventsAuthorized(token: String, groupId: String, week: Int, showOwnSelective: Boolean): Response<GetEventResponse> {
return scheduleService.getEventsAuthorized(token, groupId, week, showOwnSelective)
}

suspend fun getEventInfo(eventId: String, week: Any): Response<DetailedEventResponse> {
return scheduleService.getEventInfo(eventId, week)
suspend fun getEventInfo(token: String, eventId: String, week: Any): Response<DetailedEventResponse> {
return scheduleService.getEventInfo(token, eventId, week)
}

suspend fun deleteEventById(groupId: String, eventId: String): Response<DetailedEventResponse> {
return scheduleService.deleteEventById(groupId, eventId)
suspend fun deleteEventById(token: String, groupId: String, eventId: String): Response<DetailedEventResponse> {
return scheduleService.deleteEventById(token, groupId, eventId)
}

suspend fun addEvent(body: PostEventDTO, groupId: String): Response<DetailedEventResponse> {
return scheduleService.addEvent(body, groupId)
suspend fun addEvent(token: String, body: PostEventDTO, groupId: String): Response<DetailedEventResponse> {
return scheduleService.addEvent(token, body, groupId)
}

suspend fun editEvent(body: PatchEventDTO, groupId: String, eventId: String): Response<DetailedEventResponse> {
return scheduleService.editEvent(body, groupId, eventId)
suspend fun editEvent(token: String, body: PatchEventDTO, groupId: String, eventId: String): Response<DetailedEventResponse> {
return scheduleService.editEvent(token, body, groupId, eventId)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ class ResetPasswordFragment : Fragment() {
).get(AuthViewModel::class.java)

setChangePasswordButtonListener()
setPreviousButtonListener()

return view
}

private fun setPreviousButtonListener() {
binding.buttonPrevious.setOnClickListener {
view?.let { it1 -> Navigation.findNavController(it1).navigateUp() }
}
}
private fun setChangePasswordButtonListener() {
binding.buttonChangePassword.setOnClickListener {
if (binding.editTextNewPassword.text.toString() != binding.editTextConfirmPassword.text.toString()) {
Expand Down
169 changes: 166 additions & 3 deletions app/src/main/java/com/fictadvisor/android/ui/ScheduleFragment.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
package com.fictadvisor.android.ui

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.fictadvisor.android.R
import androidx.lifecycle.ViewModelProvider
import com.fictadvisor.android.data.dto.BaseResponse
import com.fictadvisor.android.data.dto.ResetPasswordDTO
import com.fictadvisor.android.data.dto.schedule.DetailedEventResponse
import com.fictadvisor.android.data.dto.schedule.PatchEventDTO
import com.fictadvisor.android.data.dto.schedule.PostEventDTO
import com.fictadvisor.android.data.dto.schedule.TDiscipline
import com.fictadvisor.android.data.dto.schedule.TEventPeriod
import com.fictadvisor.android.databinding.FragmentResetPasswordBinding
import com.fictadvisor.android.repository.ScheduleRepository
import com.fictadvisor.android.utils.StorageUtil
import com.fictadvisor.android.viewmodel.ScheduleViewModel
import com.fictadvisor.android.viewmodel.ScheduleViewModelFactory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ScheduleFragment : Fragment() {
private lateinit var binding: FragmentResetPasswordBinding
private lateinit var scheduleViewModel: ScheduleViewModel
private val scheduleRepository = ScheduleRepository()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
Expand All @@ -17,8 +38,150 @@ class ScheduleFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_schedule, container, false)

binding = FragmentResetPasswordBinding.inflate(inflater, container, false)
val view = binding.root

scheduleViewModel = ViewModelProvider(
this,
ScheduleViewModelFactory(scheduleRepository)
).get(ScheduleViewModel::class.java)

testScheduleApiMethods()

return view
}

private fun testScheduleApiMethods() {
// testGetEvents()
// testGetEventsAuthorized()
// testAddEvent()
testGetEventInfo()
// testEditEvent()
// testDeleteEventById()
}

private fun testGetEvents() {
CoroutineScope(Dispatchers.IO).launch {
scheduleViewModel.getEvents("b49dd1d3-5111-4c74-a10c-768f958a19da", 2)
}
scheduleViewModel.getEventsResponse.observe(viewLifecycleOwner) { response ->
response?.let {
Log.d("getEvents", response.toString())
if (response is BaseResponse.Success) {
Log.d("getEvents", (response.data!!).toString())
}
}
}
}

private fun testGetEventsAuthorized() {
CoroutineScope(Dispatchers.IO).launch {
val token = "Bearer ${StorageUtil(requireContext()).getTokens()!!.accessToken}"
scheduleViewModel.getEventsAuthorized(token, "b49dd1d3-5111-4c74-a10c-768f958a19da", 1, false)
}
scheduleViewModel.getEventsAuthorizedResponse.observe(viewLifecycleOwner) { response ->
response?.let {
Log.d("getEventsAuthorized", response.toString())
if (response is BaseResponse.Success) {
Log.d("getEventsAuthorized", (response.data!!).toString())
}
}
}
}

private fun testAddEvent() {
CoroutineScope(Dispatchers.IO).launch {
val token = "Bearer ${StorageUtil(requireContext()).getTokens()!!.accessToken}"
val groupId = "b49dd1d3-5111-4c74-a10c-768f958a19da"
val testPostEventDTO = PostEventDTO(
groupId = groupId,
name = "Some event",
teachers = listOf("229ab047-2e97-4267-91bc-d5f211891c90"), // Роковий
disciplineId = "080eab0a-24af-406c-879d-91afb1751bfb", // ММІТ
url = "https://example.com",
eventInfo = "This is a test event",
eventType = TDiscipline.LECTURE,
disciplineInfo = "Some discipline info",
period = TEventPeriod.EVERY_WEEK,
startTime = "2024-04-08T14:15:00.823Z",
endTime = "2024-04-08T15:50:00.823Z"
)
scheduleViewModel.addEvent(token, testPostEventDTO, groupId)
}
scheduleViewModel.addEventResponse.observe(viewLifecycleOwner) { response ->
response?.let {
Log.d("addEvent", response.toString())
if (response is BaseResponse.Success) {
Log.d("addEvent", (response.data!!).toString())
}
}
}
}

private fun testGetEventInfo() {
CoroutineScope(Dispatchers.IO).launch {
val token = "Bearer ${StorageUtil(requireContext()).getTokens()!!.accessToken}"
val eventId = "99eb3fdb-3a95-4ec7-b911-eae752a1de48" // TODO: replace every time you delete a test event
scheduleViewModel.getEventInfo(token, eventId, 10) // use different ids, week number is counted from the start of the term
}
scheduleViewModel.getEventInfoResponse.observe(viewLifecycleOwner) { response ->
response?.let {
Log.d("getEventInfo", response.toString())
if (response is BaseResponse.Success) {
Log.d("getEventInfo", (response.data!!).toString())
}
}
}
}

private fun testEditEvent() {
CoroutineScope(Dispatchers.IO).launch {
val token = "Bearer ${StorageUtil(requireContext()).getTokens()!!.accessToken}"
val groupId = "b49dd1d3-5111-4c74-a10c-768f958a19da"
val eventId = "99eb3fdb-3a95-4ec7-b911-eae752a1de48" // TODO: replace every time you delete a test event
val testPatchEventDTO = PatchEventDTO(
week = 10,
name = "Some event",
changeStartDate = false,
changeEndDate = false,
eventType = TDiscipline.LECTURE,
url = "https://example.com",
eventInfo = "This is a test event",
disciplineInfo = "Some discipline info",
period = TEventPeriod.EVERY_WEEK,
teachers = listOf("229ab047-2e97-4267-91bc-d5f211891c90"), // Роковий
disciplineId = "080eab0a-24af-406c-879d-91afb1751bfb", // ММІТ
startTime = "2024-04-08T14:15:00.823Z",
endTime = "2024-04-08T15:50:00.823Z"
)
scheduleViewModel.editEvent(token, testPatchEventDTO, groupId, eventId)
}
scheduleViewModel.editEventResponse.observe(viewLifecycleOwner) { response ->
response?.let {
Log.d("editEvent", response.toString())
if (response is BaseResponse.Success) {
Log.d("editEvent", (response.data!!).toString())
}
}
}
}

private fun testDeleteEventById() {
CoroutineScope(Dispatchers.IO).launch {
val token = "Bearer ${StorageUtil(requireContext()).getTokens()!!.accessToken}"
val groupId = "b49dd1d3-5111-4c74-a10c-768f958a19da"
val eventId = "99eb3fdb-3a95-4ec7-b911-eae752a1de48" // TODO: replace every time you delete a test event
scheduleViewModel.deleteEventById(token, groupId, eventId)
}
scheduleViewModel.deleteEventByIdResponse.observe(viewLifecycleOwner) { response ->
response?.let {
Log.d("deleteEventById", response.toString())
if (response is BaseResponse.Success) {
Log.d("deleteEventById", (response.data!!).toString())
}
}
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class ScheduleViewModel(private val mainRepository: ScheduleRepository) : ViewMo
}
}

fun getEventsAuthorized(groupId: String, week: Int, showOwnSelective: Boolean){
fun getEventsAuthorized(token: String, groupId: String, week: Int, showOwnSelective: Boolean){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.getEventsAuthorized(groupId, week, showOwnSelective)
val response = mainRepository.getEventsAuthorized(token, groupId, week, showOwnSelective)
withContext(mainDispatcher) {
if (response.isSuccessful) {
getEventsAuthorizedResponseMutable.postValue(BaseResponse.Success(response.body()))
Expand All @@ -80,9 +80,9 @@ class ScheduleViewModel(private val mainRepository: ScheduleRepository) : ViewMo
}
}

fun getEventInfo(eventId: String, week: Any){
fun getEventInfo(token: String, eventId: String, week: Any){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.getEventInfo(eventId, week)
val response = mainRepository.getEventInfo(token, eventId, week)
withContext(mainDispatcher) {
if (response.isSuccessful) {
getEventInfoResponseMutable.postValue(BaseResponse.Success(response.body()))
Expand All @@ -97,9 +97,9 @@ class ScheduleViewModel(private val mainRepository: ScheduleRepository) : ViewMo
}
}

fun deleteEventById(groupId: String, eventId: String){
fun deleteEventById(token: String, groupId: String, eventId: String){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.deleteEventById(groupId, eventId)
val response = mainRepository.deleteEventById(token, groupId, eventId)
withContext(mainDispatcher) {
if (response.isSuccessful) {
deleteEventByIdResponseMutable.postValue(BaseResponse.Success(response.body()))
Expand All @@ -114,9 +114,9 @@ class ScheduleViewModel(private val mainRepository: ScheduleRepository) : ViewMo
}
}

fun addEvent(body: PostEventDTO, groupId: String){
fun addEvent(token: String, body: PostEventDTO, groupId: String){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.addEvent(body, groupId)
val response = mainRepository.addEvent(token, body, groupId)
withContext(mainDispatcher) {
if (response.isSuccessful) {
addEventResponseMutable.postValue(BaseResponse.Success(response.body()))
Expand All @@ -131,9 +131,9 @@ class ScheduleViewModel(private val mainRepository: ScheduleRepository) : ViewMo
}
}

fun editEvent(body: PatchEventDTO, groupId: String, eventId: String){
fun editEvent(token: String, body: PatchEventDTO, groupId: String, eventId: String){
job = CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
val response = mainRepository.editEvent(body, groupId, eventId)
val response = mainRepository.editEvent(token, body, groupId, eventId)
withContext(mainDispatcher) {
if (response.isSuccessful) {
editEventResponseMutable.postValue(BaseResponse.Success(response.body()))
Expand Down
Loading

0 comments on commit e9a1e39

Please sign in to comment.