Skip to content

Commit

Permalink
Merge pull request #15 from flygare/adding_unit_tests
Browse files Browse the repository at this point in the history
Adding unit tests
  • Loading branch information
Max-Meldrum authored Mar 14, 2017
2 parents 0be9b79 + 401c9fb commit 158c15e
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jdk:
- oraclejdk8

script:
- sbt -jvm-opts travis/jvmopts.compile assembly
- sbt -jvm-opts travis/jvmopts.compile compile
- sbt -jvm-opts travis/jvmopts.test test
- sbt -jvm-opts travis/jvmopts.test scalastyle

Expand Down
7 changes: 4 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version := "1.0"
scalaVersion := "2.11.8"

assemblyJarName in assembly := "DBConnector.jar"
mainClass in assembly := Some("DBConnector")
mainClass in assembly := Some("se.qvantel.connector.DBConnector")

lazy val execScript = taskKey[Unit]("Download mcc library")

Expand All @@ -15,7 +15,7 @@ execScript := {
}

compile in Compile <<= (compile in Compile).dependsOn(execScript)

logLevel in assembly := Level.Error
resolvers += "Spark Packages Repo" at "https://dl.bintray.com/spark-packages/maven"

libraryDependencies ++= Seq(
Expand All @@ -25,7 +25,8 @@ libraryDependencies ++= Seq(
"org.json4s" %% "json4s-native" % "3.5.0",
"com.typesafe" % "config" % "1.3.1",
"org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.4",
"com.typesafe.scala-logging" %% "scala-logging" % "3.5.0")
"com.typesafe.scala-logging" %% "scala-logging" % "3.5.0"
)

// This part is required for spark to assemble
// Why? I don't know, but it works
Expand Down
3 changes: 3 additions & 0 deletions get_latest_mcc_table.bash
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ then
echo "Copying mcc table to test/resources/"
cp mcc-mnc-table.json src/test/resources/
fi

test -f "src/test/resources/mcc-mnc-table.json" && echo "Test/res/mcc table exists" || exit 1
test -f "src/main/resources/mcc-mnc-table.json" && echo "Main/res/mcc table exists" || exit 1
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
package se.qvantel.connector

import com.datastax.spark.connector._
import com.datastax.spark.connector.{CassandraRow, SomeColumns}
import com.datastax.spark.connector.rdd.CassandraTableScanRDD
import org.joda.time.{DateTime, DateTimeZone}
import property.{CountryCodes, Logger, Processing}

import scala.concurrent.ExecutionContext.Implicits.global
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import java.net._
package se.qvantel.connector

import java.io._
import java.net._

import org.joda.time.DateTime
import scala.collection.mutable.Queue
import property.Logger

import scala.collection.mutable.Queue
import scala.util.Try

class DatapointDispatcher(ip: String, port: Int) extends Logger {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package se.qvantel.connector

import com.datastax.spark.connector.cql.CassandraConnector
import org.apache.spark.{SparkConf, SparkContext}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package model
package se.qvantel.connector.model

case class Country(network: String, country: String, mcc: String, iso: String, country_code: String, mnc: String)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package property
package se.qvantel.connector.property

import com.typesafe.config.ConfigFactory

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package property
package se.qvantel.connector.property

import model.Country
import se.qvantel.connector.model.Country
import org.json4s.{DefaultFormats, _}
import org.json4s.native.JsonMethods._
import scala.collection.mutable.HashMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package property
package se.qvantel.connector.property


trait Logger {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package property
package se.qvantel.connector.property


trait Processing extends Config {
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gen.countries.file="/mcc-mnc-table.json"
45 changes: 45 additions & 0 deletions src/test/scala/se/qvantel/connector/model/CountryTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package se.qvantel.connector.model

import org.json4s.DefaultFormats
import org.scalatest.FunSuite
import org.json4s.native.JsonMethods._


class CountryTest extends FunSuite {

test("Test parsing of JSON-string to model.Country case class") {
val string = "[" +
"{\"network\":\"A-Mobile\",\"country\":\"Abkhazia\",\"mcc\":\"289\",\"iso\":\"ge\"," +
"\"country_code\":\"7\",\"mnc\":\"68\"}," +
"{\"network\":\"A-Mobile\",\"country\":\"Abkhazia\"," +
"\"mcc\":\"289\",\"iso\":\"ge\",\"country_code\":\"7\",\"mnc\":\"88\"}," +
"{\"network\":\"Aquafon\"," +
"\"country\":\"Abkhazia\",\"mcc\":\"289\",\"iso\":\"ge\",\"country_code\":\"7\",\"mnc\":\"67\"}" +
"{\"network\":\"BCELL\"," +
"\"country\":\"Belgium\",\"mcc\":\"100\",\"iso\":\"ge\",\"country_code\":\"8\",\"mnc\":\"67\"}" +
"]"

// Specify format method
implicit val format = DefaultFormats

// Parse the object
val obj = parse(string).extract[List[Country]]

// Cherry picked testing.
assert(obj.head.network.equals("A-Mobile"))
assert(obj.head.country.equals("Abkhazia"))
assert(obj.head.mcc.equals("289"))

assert(obj(1).network.equals("A-Mobile"))
assert(obj(1).country.equals("Abkhazia"))
assert(obj(1).mcc.equals("289"))

assert(obj(2).network.equals("Aquafon"))
assert(obj(2).country.equals("Abkhazia"))
assert(obj(2).mcc.equals("289"))

assert(obj(3).network.equals("BCELL"))
assert(obj(3).country.equals("Belgium"))
assert(obj(3).mcc.equals("100"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package se.qvantel.connector.property

import java.io.InputStream
import org.scalatest.FunSuite

class CountryCodesTest extends FunSuite with Config with CountryCodes {

test("Check that there is a mcc-resources file") {
try {
val res = config.getString("gen.countries.file")

val stream : InputStream = getClass.getResourceAsStream(res)
val lines = scala.io.Source.fromInputStream(stream).mkString

assert(lines.nonEmpty)
stream.close()
}
catch {
case ex: Exception => {
fail(ex.getMessage())
}
}
}

test("Mapping of mcc to country") {
// Loads MCC and countries ISO code into a HashMap, variable in CountryCodes
getCountriesByMcc()
// Check we actually map something
assert(!countries.isEmpty)
// Check if code 240 is mapped to se (Sweden)
val swedenISO = countries("240")
assert(swedenISO == "se")
// Check if code 432 is mapped to ir (Iran)
val iranISO = countries("432")
assert(iranISO == "ir")
}

}
12 changes: 12 additions & 0 deletions src/test/scala/se/qvantel/connector/property/ProcessingTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package se.qvantel.connector.property

import org.scalatest.FunSuite

class ProcessingTest extends FunSuite with Processing {

test("That a valid processing config exists") {
assert(batchSize > 0 && batchSize <= 10000)
assert(fetchBatchSize > 0 && fetchBatchSize <= 20000)
assert(updateInterval > 0 && updateInterval <= 10000)
}
}

0 comments on commit 158c15e

Please sign in to comment.