Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repository implementation #32

Merged
merged 5 commits into from
Apr 12, 2024
Merged
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
Expand Up @@ -30,6 +30,7 @@ class EventInfoSheetTest {
val event =
Event(
eventId = "1",
creatorId = "1",
organizerId = "1",
title = "Event Title",
description = "Event Description",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MapViewAndroidTest {
listOf(
Event(
eventId = "a",
creatorId = "a",
organizerId = "a",
title = "Bowling Event",
description = "",
Expand All @@ -44,6 +45,7 @@ class MapViewAndroidTest {
),
Event(
eventId = "b",
creatorId = "a",
organizerId = "a",
title = "Swimming Event",
description = "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.github.swent.echo.data.repository.datasources

import com.github.swent.echo.data.model.Association
import com.github.swent.echo.data.model.Event
import com.github.swent.echo.data.model.Location
import com.github.swent.echo.data.model.Tag
import com.github.swent.echo.data.model.UserProfile
import com.github.swent.echo.data.repository.datasources.Supabase as SupabaseSource
import io.github.jan.supabase.SupabaseClient
import io.github.jan.supabase.createSupabaseClient
import io.github.jan.supabase.exceptions.UnauthorizedRestException
import io.github.jan.supabase.gotrue.Auth
import io.github.jan.supabase.postgrest.Postgrest
import java.util.Arrays
import java.util.Date
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.SerializationException
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.function.ThrowingRunnable

class SupabaseTest {
private val supabaseUrl = "ulejnivguxeiibkbpwnb.supabase.co"
private val supabasePublicKey =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InVsZWpuaXZndXhlaWlia2Jwd25iIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTA4MzgxODQsImV4cCI6MjAyNjQxNDE4NH0.9Hkj-Gox2XHcHfs_U2GyQFc9sZ_nu2Xs16-KYBri32g"
private lateinit var supabaseClient: SupabaseClient
private lateinit var source: SupabaseSource

private val association =
Association(
"b0122e3e-82ed-4409-83f9-dbfb9761db20",
"Dummy Assoc",
"DO NOT DELETE/MODIFY: tests in repository.datasources.SupabaseTest will fail"
)
private val tag = Tag("daba142a-a276-4b7e-824d-43ca088633ff", "Dummy Tag")
private val event =
Event(
"3bcf6f25-81d4-4a14-9caa-c05feb593da0",
"e65e9435-a9f2-4474-be11-9054305f1a54",
"b0122e3e-82ed-4409-83f9-dbfb9761db20",
"Dummy Event",
"blabla description",
Location("testLocation", 0.0, 0.0),
Date(0),
Date(1),
HashSet<Tag>(Arrays.asList(tag))
)
private val userProfile = UserProfile("b0122e3e-82ed-4409-83f9-dbfb9761db20", "Dummy User")

@Before
fun setUp() {
supabaseClient =
createSupabaseClient(supabaseUrl, supabasePublicKey) {
install(Auth)
install(Postgrest)
}
source = SupabaseSource(supabaseClient)
}

@Test
fun getAssociationTest() {
assertThrows(
NoSuchElementException::class.java,
ThrowingRunnable {
runBlocking { source.getAssociation("b0122e3e-82ed-4409-83f9-dbfb9761db20") }
}
)
}

@Test
fun setAssociationTest() {
assertThrows(
UnauthorizedRestException::class.java,
ThrowingRunnable { runBlocking { source.setAssociation(association) } }
)
}

@Test
fun getAllAssociationsTest() {
val associations = runBlocking { source.getAllAssociations() }
assertNotNull(associations)
}

@Test
fun getEventTest() {
assertThrows(
NoSuchElementException::class.java,
ThrowingRunnable {
runBlocking { source.getEvent("3bcf6f25-81d4-4a14-9caa-c05feb593da0") }
}
)
}

@Test
fun setEventTest() {
assertThrows(
SerializationException::class.java,
ThrowingRunnable { runBlocking { source.setEvent(event) } }
)
}

@Test
fun getAllEventsTest() {
val associations = runBlocking { source.getAllEvents() }
assertNotNull(associations)
}

@Test
fun getTagTest() {
assertThrows(
NoSuchElementException::class.java,
ThrowingRunnable {
runBlocking { source.getTag("daba142a-a276-4b7e-824d-43ca088633ff") }
}
)
}

@Test
fun setTagTest() {
assertThrows(
UnauthorizedRestException::class.java,
ThrowingRunnable { runBlocking { source.setTag(tag) } }
)
}

@Test
fun getAllTagsTest() {
val associations = runBlocking { source.getAllTags() }
assertNotNull(associations)
}

@Test
fun getUserProfileTest() {
assertThrows(
NoSuchElementException::class.java,
ThrowingRunnable {
runBlocking { source.getUserProfile("b0122e3e-82ed-4409-83f9-dbfb9761db20") }
}
)
}

@Test
fun setUserProfileTest() {
assertThrows(
UnauthorizedRestException::class.java,
ThrowingRunnable { runBlocking { source.setUserProfile(userProfile) } }
)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package com.github.swent.echo.data.model

import kotlinx.serialization.Serializable

@Serializable
data class Association(val associationId: String, val name: String, val description: String)
8 changes: 6 additions & 2 deletions app/src/main/java/com/github/swent/echo/data/model/Event.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.github.swent.echo.data.model

import java.util.Date
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable

@Serializable
data class Event(
val eventId: String,
val creatorId: String,
val organizerId: String,
val title: String,
val description: String,
val location: Location,
val startDate: Date,
val endDate: Date,
@Contextual val startDate: Date,
@Contextual val endDate: Date,
val tags: Set<Tag>
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.swent.echo.data.model

import kotlinx.serialization.Serializable
import org.maplibre.android.geometry.LatLng
import org.osmdroid.util.GeoPoint

@Serializable
data class Location(val name: String, val lat: Double, val long: Double) {
constructor(name: String, point: GeoPoint) : this(name, point.latitude, point.longitude)

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/github/swent/echo/data/model/Tag.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.github.swent.echo.data.model

data class Tag(val tagId: String, val name: String)
import kotlinx.serialization.Serializable

@Serializable data class Tag(val tagId: String, val name: String)
3 changes: 0 additions & 3 deletions app/src/main/java/com/github/swent/echo/data/model/User.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.github.swent.echo.data.model

data class UserProfile(val userId: String, val name: String)
import kotlinx.serialization.Serializable

@Serializable data class UserProfile(val userId: String, val name: String)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.github.swent.echo.data.repository
import com.github.swent.echo.data.model.Association
import com.github.swent.echo.data.model.Event
import com.github.swent.echo.data.model.Tag
import com.github.swent.echo.data.model.User
import com.github.swent.echo.data.model.UserProfile

interface Repository {
Expand All @@ -25,10 +24,6 @@ interface Repository {

suspend fun getAllTags(): List<Tag>

suspend fun getUser(userId: String): User

suspend fun setUser(user: User)

suspend fun getUserProfile(userId: String): UserProfile

suspend fun setUserProfile(userProfile: UserProfile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.github.swent.echo.data.repository

import com.github.swent.echo.data.model.Association
import com.github.swent.echo.data.model.Event
import com.github.swent.echo.data.model.Tag
import com.github.swent.echo.data.model.UserProfile
import com.github.swent.echo.data.repository.datasources.RemoteDataSource

class RepositoryImpl(private val remoteDataSource: RemoteDataSource) : Repository {
override suspend fun getAssociation(associationId: String): Association {
return remoteDataSource.getAssociation(associationId)
}

override suspend fun setAssociation(association: Association) {
return remoteDataSource.setAssociation(association)
}

override suspend fun getAllAssociations(): List<Association> {
return remoteDataSource.getAllAssociations()
}

override suspend fun getEvent(eventId: String): Event {
return remoteDataSource.getEvent(eventId)
}

override suspend fun setEvent(event: Event) {
return remoteDataSource.setEvent(event)
}

override suspend fun getAllEvents(): List<Event> {
return remoteDataSource.getAllEvents()
}

override suspend fun getTag(tagId: String): Tag {
return remoteDataSource.getTag(tagId)
}

override suspend fun setTag(tag: Tag) {
return remoteDataSource.setTag(tag)
}

override suspend fun getAllTags(): List<Tag> {
return remoteDataSource.getAllTags()
}

override suspend fun getUserProfile(userId: String): UserProfile {
return remoteDataSource.getUserProfile(userId)
}

override suspend fun setUserProfile(userProfile: UserProfile) {
return remoteDataSource.setUserProfile(userProfile)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
package com.github.swent.echo.data.repository.datasources

import com.github.swent.echo.data.model.Association
import com.github.swent.echo.data.model.Event
import com.github.swent.echo.data.model.Tag
import com.github.swent.echo.data.model.UserProfile

interface RemoteDataSource {
fun getData(): String
suspend fun getAssociation(associationId: String): Association

suspend fun setAssociation(association: Association)

suspend fun getAllAssociations(): List<Association>

suspend fun getEvent(eventId: String): Event

suspend fun setEvent(event: Event)

suspend fun getAllEvents(): List<Event>

suspend fun getTag(tagId: String): Tag

suspend fun setTag(tag: Tag)

suspend fun getAllTags(): List<Tag>

suspend fun getUserProfile(userId: String): UserProfile

suspend fun setUserProfile(userProfile: UserProfile)
}
Loading