Created
April 17, 2017 22:57
-
-
Save Dkendal/1e6e012eefe118400e197a2112c6f808 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package jcourse | |
| import com.ericsson.otp.erlang._ | |
| import org.scalatest.FunSpec | |
| import java.io._ | |
| class JcourseSpec extends FunSpec { | |
| describe("convert") { | |
| it("converts an erlang message into the domain model") { | |
| var fis = new FileInputStream("fixtures/sections") | |
| var ois = new ObjectInputStream(fis) | |
| var sections : OtpErlangList = | |
| ois.readObject().asInstanceOf[OtpErlangList] | |
| ois.close() | |
| def get[T](map : OtpErlangMap, key : String) : T = { | |
| map.get(new OtpErlangAtom(key)).asInstanceOf[T] | |
| } | |
| def convertTime(time : OtpErlangMap) : Integer = { | |
| var day = get[OtpErlangInt](time, "day").intValue | |
| var hour = get[OtpErlangInt](time, "hour").intValue | |
| var minute = get[OtpErlangInt](time, "minute").intValue | |
| minute * 60 + | |
| hour * 60 * 60 + | |
| day * 24 * 60 * 60 | |
| } | |
| def convertPeriod(period : OtpErlangMap) : Period = { | |
| var startTime = convertTime(get[OtpErlangMap](period, "start_time")) | |
| var endTime = convertTime(get[OtpErlangMap](period, "end_time")) | |
| Period(startTime = startTime, endTime = endTime) | |
| } | |
| def convertPeriods(section : OtpErlangMap) : List[Period] = { | |
| get[OtpErlangList](section, "periods"). | |
| elements. | |
| map({ case period : OtpErlangMap => convertPeriod(period) }). | |
| toList | |
| } | |
| def convertSection(section : OtpErlangMap) : Section = { | |
| val periods = convertPeriods(section) | |
| val course = null // TODO remove this field from class | |
| val scheduleType = null // TODO remove this field from class | |
| Section(scheduleType = scheduleType, periods = periods, course = course) | |
| } | |
| def convertSections(sections : OtpErlangList) : List[Section] = { | |
| sections. | |
| elements. | |
| map({ case section : OtpErlangMap => convertSection(section) }). | |
| toList | |
| } | |
| println(convertSections(sections)) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment