-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
10cad15
b510721
5e3f2e8
ccff5d5
2b57124
5aacd8c
db9b314
6c6dab2
99db658
9427e93
900b35f
0747d81
3d30b67
d3f79ac
db72686
0590672
dfc7eda
f81ccd8
1830048
5ce0373
a3f47c6
356f60f
5c798af
8ddd4da
5c0bbaf
290dd09
967030f
85678f0
11d2644
7d75653
6f59c38
6e8deab
0873f08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) | ||
} |
There was a problem hiding this comment.
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)