Skip to content

Commit

Permalink
Add test for RuntimeEnvironment new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
K1li4nL committed May 15, 2023
1 parent 419ea3b commit 7f0939c
Showing 1 changed file with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ch.epfl.pop.config

import ch.epfl.pop.config.RuntimeEnvironment.{readClientEndpoints, readServerPeers}
import org.scalatest.funsuite.{AnyFunSuiteLike => FunSuite}
import org.scalatest.matchers.should.Matchers.{convertToAnyShouldWrapper, equal}

import java.io.{BufferedWriter, FileWriter}
import scala.io.Source.fromFile

class RuntimeEnvironmentSuite extends FunSuite {

test("readClientEndpoints() should only return the addresses that match the regex from the schema") {
val path = "src/main/scala/ch/epfl/pop/config/client-endpoints-list.conf"
// Save old file content
val source = fromFile(path)
val oldContent = source.mkString
source.close()

// Values to add in the file
val address1 = "ws://127.0.0.1:7000/client"
val address2 = "ws://127.0.0.1:8000/client"
val address3 = "ws://127.0.0.1:9000/client"
val invalidAddress = "http://127.0.0.1/"

// Writing the values
var file = new BufferedWriter(new FileWriter(path))

file.append(address1)
file.newLine()
file.append(address2)
file.newLine()
file.append(invalidAddress)
file.newLine()
file.append(address3)
file.close()

// Verify it works as expected
readClientEndpoints() should equal(List(address1, address2, address3))

// Restore old content
file = new BufferedWriter(new FileWriter(path))
file.write(oldContent)
file.close()
}

// If this test doesn't pass it may be because the default config were not restored in the previous test
test("Read of default client endpoint and server peers config should be empty") {
readClientEndpoints().isEmpty should equal(true)
readServerPeers().isEmpty should equal(true)
}

}

0 comments on commit 7f0939c

Please sign in to comment.