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

complete validation and tests for CreateRollCall data #1952

Merged
merged 4 commits into from
Jun 27, 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 @@ -5,6 +5,7 @@ import com.github.dedis.popstellar.model.network.method.message.data.Action
import com.github.dedis.popstellar.model.network.method.message.data.Data
import com.github.dedis.popstellar.model.network.method.message.data.Objects
import com.github.dedis.popstellar.model.objects.RollCall
import com.github.dedis.popstellar.utility.MessageValidator
import com.google.gson.annotations.SerializedName
import java.util.Optional

Expand Down Expand Up @@ -41,6 +42,8 @@ class CreateRollCall : Data {
description: String?,
laoId: String
) {
validate(laoId, "laoId", name, creation, proposedStart, proposedEnd, location, description)
.let { if (description != null) it.stringNotEmpty(description, "description") else it }
this.name = name
this.creation = creation
this.proposedStart = proposedStart
Expand All @@ -63,6 +66,8 @@ class CreateRollCall : Data {
location: String,
description: String?
) {
validate(id, "id", name, creation, proposedStart, proposedEnd, location, description)

this.id = id
this.name = name
this.creation = creation
Expand Down Expand Up @@ -108,4 +113,24 @@ class CreateRollCall : Data {
return "CreateRollCall{id='$id', name='$name', creation=$creation, proposedStart=$proposedStart, " +
"proposedEnd=$proposedEnd, location='$location', description='$description'}"
}

private fun validate(
id: String,
idLabel: String,
name: String,
creation: Long,
proposedStart: Long,
proposedEnd: Long,
location: String,
description: String?
): MessageValidator.MessageValidatorBuilder {
return MessageValidator.verify()
.isNotEmptyBase64(id, idLabel)
.stringNotEmpty(name, "name")
.greaterOrEqualThan(creation, 0, "creation")
.greaterOrEqualThan(proposedStart, 0, "proposedStart")
.greaterOrEqualThan(proposedEnd, 0, "proposedEnd")
.stringNotEmpty(location, "location")
.let { if (description != null) it.stringNotEmpty(description, "description") else it }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ constructor(

val createRollCall =
CreateRollCall(
title, creation, proposedStart, proposedEnd, location, description, laoView.id)
title,
creation,
proposedStart,
proposedEnd,
location,
description?.ifEmpty { null },
laoView.id)

return networkManager.messageSender
.publish(keyManager.mainKeyPair, laoView.channel, createRollCall)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import com.github.dedis.popstellar.model.network.method.message.data.rollcall.Cr
import com.github.dedis.popstellar.model.objects.Channel.Companion.fromString
import com.github.dedis.popstellar.testutils.Base64DataUtils
import com.google.gson.Gson
import java.util.Objects
import org.junit.Assert
import org.junit.Test
import java.time.Instant
import java.util.Objects

class BroadcastTest {
@Test
Expand Down Expand Up @@ -59,8 +60,19 @@ class BroadcastTest {

companion object {
private val CHANNEL = fromString("root/stuff")
private const val ID = 42
private val DATA = CreateRollCall("title", 0, 1, 2, "EPFL", "rc", "an id")
private const val LAO_ID = "fEvAfdtNrykd9NPYl9ReHLX-6IP6SFLKTZJLeGUHZ_U="
private const val NAME = "NAME"
private val NOW = Instant.now().epochSecond
private val END = NOW + 30L
private const val LOCATION = "Location"
private val DATA = CreateRollCall(
NAME,
NOW,
NOW,
END,
LOCATION, null,
LAO_ID
)
private val KEYPAIR = Base64DataUtils.generateKeyPair()
private val MESSAGE_GENERAL = MessageGeneral(KEYPAIR, DATA, Gson())
private val BROADCAST = Broadcast(CHANNEL, MESSAGE_GENERAL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.google.gson.Gson
import java.util.Objects
import org.junit.Assert
import org.junit.Test
import java.time.Instant

class PublishTest {
@Test
Expand Down Expand Up @@ -50,7 +51,19 @@ class PublishTest {
companion object {
private val CHANNEL = fromString("root/stuff")
private const val ID = 42
private val DATA = CreateRollCall("title", 0, 1, 2, "EPFL", "rc", "an id")
private const val LAO_ID = "fEvAfdtNrykd9NPYl9ReHLX-6IP6SFLKTZJLeGUHZ_U="
private const val NAME = "NAME"
private val NOW = Instant.now().epochSecond
private val END = NOW + 30L
private const val LOCATION = "Location"
private val DATA = CreateRollCall(
NAME,
NOW,
NOW,
END,
LOCATION, null,
LAO_ID
)
private val KEYPAIR = Base64DataUtils.generateKeyPair()
private val MESSAGE_GENERAL = MessageGeneral(KEYPAIR, DATA, Gson())
private val PUBLISH = Publish(CHANNEL, ID, MESSAGE_GENERAL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.github.dedis.popstellar.model.network.JsonTestUtils.testData
import com.github.dedis.popstellar.model.network.method.message.data.Action
import com.github.dedis.popstellar.model.network.method.message.data.Objects
import com.github.dedis.popstellar.model.objects.event.EventType
import com.github.dedis.popstellar.model.objects.security.Base64URLData
import com.github.dedis.popstellar.utility.security.HashSHA256.hash
import java.time.Instant
import junit.framework.TestCase.assertNotNull
import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.time.Instant

@RunWith(AndroidJUnit4::class)
class CreateRollCallTest {
Expand Down Expand Up @@ -61,6 +63,107 @@ class CreateRollCallTest {
MatcherAssert.assertThat(CREATE_ROLL_CALL.location, CoreMatchers.`is`(LOCATION))
}

@Test
fun constructor1SucceedsWithValidData() {
val createRollCall = CreateRollCall(NAME, NOW, NOW, END, LOCATION, null, LAO_ID)
assertNotNull(createRollCall)
}

fun constructor1SucceedsWithValidDataDescription() {
CreateRollCall(NAME, NOW, NOW, END, LOCATION, "super cool description", LAO_ID)
}

@Test(expected = IllegalArgumentException::class)
fun constructor1FailsWhenNameEmpty() {
CreateRollCall("", NOW, NOW, END, LOCATION, null, LAO_ID)
}

@Test(expected = IllegalArgumentException::class)
fun constructor1FailsWhenLAOIdEmpty() {
CreateRollCall(NAME, NOW, NOW, END, LOCATION, null, EMPTY_B64)
}

@Test(expected = IllegalArgumentException::class)
fun constructor1FailsWhenLAOIdNotB64() {
CreateRollCall(NAME, NOW, NOW, END, LOCATION, null, INVALID_B64)
}

@Test(expected = IllegalArgumentException::class)
fun constructor1FailsWhenCreationNegative() {
CreateRollCall(NAME, -1, NOW, END, LOCATION, null, LAO_ID)
}

@Test(expected = IllegalArgumentException::class)
fun constructor1FailsWhenProposedStartNegative() {
CreateRollCall(NAME, NOW, -1, END, LOCATION, null, LAO_ID)
}

@Test(expected = IllegalArgumentException::class)
fun constructor1FailsWhenProposedEndNegative() {
CreateRollCall(NAME, NOW, NOW, -1, LOCATION, null, LAO_ID)
}

@Test(expected = IllegalArgumentException::class)
fun constructor1FailsWhenLocationEmpty() {
CreateRollCall(NAME, NOW, NOW, END, EMPTY_STRING, null, LAO_ID)
}

@Test(expected = IllegalArgumentException::class)
fun constructor1FailsWhenDescriptionEmpty() {
CreateRollCall(NAME, NOW, NOW, END, LOCATION, EMPTY_STRING, LAO_ID)
}

@Test
fun constructor2SucceedsWithValidData() {
val createRollCall = CreateRollCall(ID, NAME, NOW, NOW, END, LOCATION, null)
assertNotNull(createRollCall)
}

fun constructor2SucceedsWithValidDataDescription() {
CreateRollCall(ID, NAME, NOW, NOW, END, LOCATION, "super cool description")
}

@Test(expected = IllegalArgumentException::class)
fun constructor2FailsWhenNameEmpty() {
CreateRollCall(ID, "", NOW, NOW, END, LOCATION, null)
}

@Test(expected = IllegalArgumentException::class)
fun constructor2FailsWhenIdEmpty() {
CreateRollCall(EMPTY_B64, NAME, NOW, NOW, END, LOCATION, null)
}

@Test(expected = IllegalArgumentException::class)
fun constructor2FailsWhenIdNotB64() {
CreateRollCall(INVALID_B64, NAME, NOW, NOW, END, LOCATION, null)
}

@Test(expected = IllegalArgumentException::class)
fun constructor2FailsWhenCreationNegative() {
CreateRollCall(ID, NAME, -1, NOW, END, LOCATION, null)
}

@Test(expected = IllegalArgumentException::class)
fun constructor2FailsWhenProposedStartNegative() {
CreateRollCall(ID, NAME, NOW, -1, END, LOCATION, null)
}

@Test(expected = IllegalArgumentException::class)
fun constructor2FailsWhenProposedEndNegative() {
CreateRollCall(ID, NAME, NOW, NOW, -1, LOCATION, null)
}

@Test(expected = IllegalArgumentException::class)
fun constructor2FailsWhenLocationEmpty() {
CreateRollCall(ID, NAME, NOW, NOW, END, EMPTY_STRING, null)
}

@Test(expected = IllegalArgumentException::class)
fun constructor2FailsWhenDescriptionEmpty() {
CreateRollCall(ID, NAME, NOW, NOW, END, LOCATION, EMPTY_STRING)
}


@Test
fun jsonValidationTest() {
testData(CREATE_ROLL_CALL)
Expand Down Expand Up @@ -88,6 +191,9 @@ class CreateRollCallTest {
private val NOW = Instant.now().epochSecond
private val END = NOW + 30L
private const val LOCATION = "Location"
private const val EMPTY_STRING = ""
private val INVALID_B64 = "invalidBase64String"
private val EMPTY_B64 = Base64URLData("".toByteArray()).encoded
private val CREATE_ROLL_CALL = CreateRollCall(NAME, NOW, NOW, END, LOCATION, null, LAO_ID)
private val ID = hash(EventType.ROLL_CALL.suffix, LAO_ID, NOW.toString(), NAME)
}
Expand Down
Loading