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

Association Utils #60

Merged
merged 33 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
10cad15
feat: initialized the association view model
ortolino Mar 27, 2024
b510721
feat: added some initial useful functions
ortolino Mar 28, 2024
5e3f2e8
feat: wrote the getter functions
ortolino Mar 28, 2024
ccff5d5
feat: implemented getAssociationDescription, getAssociationDate and g…
ortolino Mar 28, 2024
2b57124
fix: merged with main branch so now I have access to the firebase API
ortolino Apr 6, 2024
5aacd8c
feat: added the update method and the initialization
ortolino Apr 6, 2024
db9b314
feat: added the function to accept a new user
ortolino Apr 6, 2024
6c6dab2
fix: corrected the format which was wrong
ortolino Apr 6, 2024
99db658
feat: added the association request
ortolino Apr 6, 2024
9427e93
feat: added the create association function
ortolino Apr 7, 2024
900b35f
fix: reverted to previous role implementation to avoid problems
ortolino Apr 7, 2024
0747d81
style: ktfmt
ortolino Apr 7, 2024
3d30b67
fix: set the state as private
ortolino Apr 7, 2024
d3f79ac
test: added both tests for empty or created assoc
ortolino Apr 7, 2024
db72686
style: ktfmt
ortolino Apr 7, 2024
0590672
test: added the test for the associationAcceptation
ortolino Apr 7, 2024
dfc7eda
feat: added the getAllAssociations and getFilteredAssociation functions
ortolino Apr 7, 2024
f81ccd8
test: added the test for the new functions
ortolino Apr 7, 2024
1830048
fix: transformed fake viewModel in utils class
ortolino Apr 8, 2024
5ce0373
fix: reverted to versions without test, trying to understand how Mock…
ortolino Apr 8, 2024
a3f47c6
test: done the empty test
ortolino Apr 8, 2024
356f60f
fix: removed the createAssociation function since it probably won't b…
ortolino Apr 8, 2024
5c798af
fix: finally the createAssociation will maybe be useful
ortolino Apr 8, 2024
8ddd4da
test: restarted the tests in the good way
ortolino Apr 8, 2024
5c0bbaf
test: added the checkAcceptedNewUser test
ortolino Apr 8, 2024
290dd09
test: added the requestToBeAdded test
ortolino Apr 8, 2024
967030f
test: added the createAssociation test
ortolino Apr 8, 2024
85678f0
test: added a corrrection to the createAssociation test
ortolino Apr 8, 2024
11d2644
test: added a getter test
ortolino Apr 9, 2024
7d75653
fix: removed unused import
ortolino Apr 9, 2024
6f59c38
fix: cleaned the tests
ortolino Apr 9, 2024
6e8deab
fix: cleaned what remained of the viewModel
ortolino Apr 9, 2024
0873f08
style: ktfmt
ortolino Apr 9, 2024
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 @@ -74,7 +74,6 @@ class AssociationAPITest {

@Test
fun testAddAssociation() {

Mockito.`when`(db.collection(Mockito.any())).thenReturn(collectionReference)
Mockito.`when`(db.collection(Mockito.any()).document(Mockito.any()))
.thenReturn(documentReference)
Expand All @@ -87,7 +86,6 @@ class AssociationAPITest {

@Test
fun testDeleteAssociation() {

Mockito.`when`(db.collection(Mockito.any())).thenReturn(collectionReference)
Mockito.`when`(db.collection(Mockito.any()).document(Mockito.any()))
.thenReturn(documentReference)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package com.github.se.assocify

import com.github.se.assocify.model.associations.AssociationUtils
import com.github.se.assocify.model.database.AssociationAPI
import com.github.se.assocify.model.entities.Association
import com.github.se.assocify.model.entities.Event
import com.github.se.assocify.model.entities.Role
import com.github.se.assocify.model.entities.User
import com.google.android.gms.tasks.Tasks
import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito
import org.mockito.Mockito.mock

class AssociationUtilsTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(if you choose to change the name of your class don't forget to change the name of the test class to be consistent)

private lateinit var db: FirebaseFirestore
private lateinit var assoApi: AssociationAPI
private val documentSnapshot = mock(DocumentSnapshot::class.java)
private val documentReference = mock(DocumentReference::class.java)
private val collectionReference = mock(CollectionReference::class.java)
private val president = User("testId", "Carlo", Role("president"))
private val newUser = User()
val oldAsso =
Association(
"aId",
"cassify",
"a cool association",
"31/09/2005",
"active",
listOf(president),
emptyList())
val oldAssoUpdated =
Association(
"aId",
"cassify",
"a cool association",
"31/09/2005",
"active",
listOf(president, newUser),
emptyList())
val oldAssoReviewed =
Association(
"aId",
"cassify",
"a cool association",
"31/09/2005",
"active",
listOf(president, User("", "", Role("newRole"))),
emptyList())
val u1 = User("a", "1", Role("president"))
val u2 = User("b", "2", Role("user"))
val u3 = User("c", "3", Role("user"))
val u4 = User("d", "4", Role("pending"))
val u5 = User("e", "5", Role("pending"))
val e1 = Event("s1", "e1", emptyList(), emptyList())
val e2 = Event("s2", "e2", emptyList(), emptyList())
val e3 = Event("s3", "e3", emptyList(), emptyList())
val getterAsso =
Association(
"getId",
"gettify",
"association to test the getters",
"31/09/2005",
"active",
listOf(u1, u2, u3, u4, u5),
listOf(e1, e2, e3))

@Before
fun setup() {
db = mock(FirebaseFirestore::class.java)
assoApi = AssociationAPI(db)
}

@Test
fun checkThatEmptyAssocWorksWell() {
val assoUtilsNewUser = AssociationUtils(newUser, associationDatabase = assoApi)
assert(assoUtilsNewUser.getPendingUsers() == emptyList<User>())
assert(assoUtilsNewUser.getRecordedUsers() == emptyList<User>())
assert(assoUtilsNewUser.getEvents() == emptyList<User>())
assert(assoUtilsNewUser.getCreationDate() == "")
assert(assoUtilsNewUser.getAssociationName() == "")
}

@Test
fun checkAcceptNewUser() {
Mockito.`when`(documentSnapshot.exists()).thenReturn(true)
Mockito.`when`(documentSnapshot.toObject(Association::class.java)).thenReturn(oldAssoUpdated)
Mockito.`when`(documentReference.get()).thenReturn(Tasks.forResult(documentSnapshot))

Mockito.`when`(db.collection(Mockito.any())).thenReturn(Mockito.mock())
Mockito.`when`(db.collection(Mockito.any()).document(Mockito.any()))
.thenReturn(documentReference)
val assocUtilsUpdated = AssociationUtils(president, oldAssoUpdated.uid, assoApi)
val pendingUsers = assocUtilsUpdated.getPendingUsers()
assert(pendingUsers == listOf(newUser))

Mockito.`when`(db.collection(Mockito.any())).thenReturn(collectionReference)
Mockito.`when`(db.collection(Mockito.any()).document(Mockito.any()))
.thenReturn(documentReference)
Mockito.`when`(documentReference.set(Mockito.any())).thenReturn(Tasks.forResult(null))

assocUtilsUpdated.acceptNewUser(newUser.uid, "newRole")
Mockito.verify(db.collection(assoApi.collectionName).document(oldAssoUpdated.uid))
.set(oldAssoReviewed)
}

@Test
fun checkRequestAssocEntry() {
Mockito.`when`(documentSnapshot.exists()).thenReturn(true)
Mockito.`when`(documentSnapshot.toObject(Association::class.java)).thenReturn(oldAsso)
Mockito.`when`(documentReference.get()).thenReturn(Tasks.forResult(documentSnapshot))

Mockito.`when`(db.collection(Mockito.any())).thenReturn(Mockito.mock())
Mockito.`when`(db.collection(Mockito.any()).document(Mockito.any()))
.thenReturn(documentReference)
val newUserUtils = AssociationUtils(newUser, oldAsso.uid, assoApi)

Mockito.`when`(db.collection(Mockito.any())).thenReturn(collectionReference)
Mockito.`when`(db.collection(Mockito.any()).document(Mockito.any()))
.thenReturn(documentReference)
Mockito.`when`(documentReference.set(Mockito.any())).thenReturn(Tasks.forResult(null))

newUserUtils.requestAssociationAccess()
Mockito.verify(db.collection(assoApi.collectionName).document(oldAssoUpdated.uid))
.set(oldAssoUpdated)
}

@Test
fun checkCreateNewAssociation() {
Mockito.`when`(db.collection(Mockito.any())).thenReturn(collectionReference)
Mockito.`when`(db.collection(Mockito.any()).document()).thenReturn(documentReference)
Mockito.`when`(documentReference.id).thenReturn("testId")
Mockito.`when`(documentReference.set(Mockito.any())).thenReturn(Tasks.forResult(null))

val newAssoUtils = AssociationUtils(president, associationDatabase = assoApi)
val name = "pollify"
val description = "a small association to have fun"
val creationDate = "12/06/1987"
val status = "open"
val newAsso =
Association("testId", name, description, creationDate, status, emptyList(), emptyList())

Mockito.`when`(db.collection(Mockito.any()).document(Mockito.any()))
.thenReturn(documentReference)
Mockito.`when`(documentReference.set(Mockito.any())).thenReturn(Tasks.forResult(null))
newAssoUtils.createNewAssoc(name, description, creationDate, status, emptyList(), emptyList())
assert(newAssoUtils.getAssocId() == "testId")
assert(newAssoUtils.getAssociationDescription() == "a small association to have fun")
Mockito.verify(db.collection(assoApi.collectionName).document(newAsso.uid)).set(newAsso)
}

@Test
fun checkGetters() {
Mockito.`when`(documentSnapshot.exists()).thenReturn(true)
Mockito.`when`(documentSnapshot.toObject(Association::class.java)).thenReturn(getterAsso)
Mockito.`when`(documentReference.get()).thenReturn(Tasks.forResult(documentSnapshot))

Mockito.`when`(db.collection(Mockito.any())).thenReturn(Mockito.mock())
Mockito.`when`(db.collection(Mockito.any()).document(Mockito.any()))
.thenReturn(documentReference)

val getterUtil = AssociationUtils(newUser, "getId", assoApi)
assert(getterUtil.getAllUsers() == listOf(u1, u2, u3, u4, u5))
assert(getterUtil.getAssociationDescription() == "association to test the getters")
assert(getterUtil.getAssocId() == "getId")
assert(getterUtil.getPendingUsers() == listOf(u4, u5))
assert(getterUtil.getRecordedUsers() == listOf(u1, u2, u3))
assert(getterUtil.getEvents() == listOf(e1, e2, e3))
assert(getterUtil.getCreationDate() == "31/09/2005")
assert(getterUtil.getAssociationName() == "gettify")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package com.github.se.assocify.model.associations

import com.github.se.assocify.model.database.AssociationAPI
import com.github.se.assocify.model.entities.Association
import com.github.se.assocify.model.entities.Event
import com.github.se.assocify.model.entities.Role
import com.github.se.assocify.model.entities.User

class AssociationUtils(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a view model, wouldn't it be better to name this class AssociationModel or something else containing the word model in it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a viewModel, I got confused. I used the functions I wrote for the wrong viewModel to create a class containing utility methods to create/modify/send requests to a certain association

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so I think you can remove the fact that AssociationUtils inherits from ViewModel()

private var user: User,
private var assocId: String = "",
private val associationDatabase: AssociationAPI
) {
private var _associationState: Association? = null

init {
update()
}

fun update() {
if (assocId != "") _associationState = associationDatabase.getAssociation(assocId)
}

fun getAssocId(): String {
return assocId
}

fun getPendingUsers(): List<User> {
update()
if (_associationState == null) {
return emptyList()
}
return _associationState!!.members.filter { x -> x.role == Role("pending") }
}

fun getRecordedUsers(): List<User> {
update()
if (_associationState == null) {
return emptyList()
}
return _associationState!!.members.filter { x -> x.role != Role("pending") }
}

fun getAllUsers(): List<User> {
update()
if (_associationState == null) {
return emptyList()
}
return _associationState!!.members
}

fun getAssociationName(): String {
if (_associationState == null) return ""
return _associationState!!.name
}

fun getAssociationDescription(): String {
if (_associationState == null) return ""
return _associationState!!.description
}

fun getCreationDate(): String {
if (_associationState == null) return ""
return _associationState!!.creationDate
}

fun getEvents(): List<Event> {
if (_associationState == null) return emptyList()
return _associationState!!.events
}

fun acceptNewUser(uid: String, role: String) {
if (_associationState != null &&
(user.role == Role("president") || user.role == Role("co-president"))) {
val userList = getPendingUsers().filter { u -> u.uid == uid }
if (userList.isEmpty()) return
val user = userList[0]
val updatedUser = User(uid, user.name, Role(role))
val ass = _associationState!!
val updatedAssoc =
Association(
ass.uid,
ass.name,
ass.description,
ass.creationDate,
ass.status,
ass.members.filter { us -> us.uid != uid } + updatedUser,
ass.events)
associationDatabase.addAssociation(updatedAssoc)
}
}

fun requestAssociationAccess() {
if (_associationState != null) {
val ass = _associationState!!
val updatedAssoc =
Association(
ass.uid,
ass.name,
ass.description,
ass.creationDate,
ass.status,
ass.members + User(user.uid, user.name, Role("pending")),
ass.events)
associationDatabase.addAssociation(updatedAssoc)
}
}

fun createNewAssoc(
name: String,
description: String,
creationDate: String,
status: String,
members: List<User>,
events: List<Event>
): Association {
val uid = associationDatabase.getNewId()
val assoc = Association(uid, name, description, creationDate, status, members, events)
associationDatabase.addAssociation(assoc)
assocId = uid
_associationState = assoc
return assoc
}

fun deleteAssoc() {
if (user.role != Role("president")) return
associationDatabase.deleteAssociation(assocId)
assocId = ""
_associationState = null
}

fun getAllAssociations(): List<Association> {
if (_associationState == null) return emptyList()
return associationDatabase.getAssociations()
}

fun getFilteredAssociations(searchQuery: String): List<Association> {
return getAllAssociations().filter { ass ->
ass.name == searchQuery || ass.description == searchQuery
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.github.se.assocify.model.entities

data class User(val uid: String, val name: String, val role: Role) {
constructor() : this("", "", Role(""))
constructor() : this("", "", Role("pending"))
}
Loading