Skip to content

Commit

Permalink
Merge pull request #55 from MonsantoCo/ip-address-type
Browse files Browse the repository at this point in the history
Adding IPAddress type
  • Loading branch information
T.J. Corrigan committed Jan 19, 2016
2 parents 04ecf92 + 30ee973 commit 634ee34
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,21 @@ object CidrBlock extends DefaultJsonProtocol {
}
}

case class IPAddress(a: IPAddressSegment, b: IPAddressSegment, c: IPAddressSegment, d: IPAddressSegment) {
def toJsString: JsString = JsString( Seq(a, b, c, d).map(_.value.toString).mkString("."))
}
object IPAddress extends DefaultJsonProtocol {
implicit val format: JsonFormat[IPAddress] = new JsonFormat[IPAddress] {
def write(obj: IPAddress) = obj.toJsString

def read(json: JsValue) = {
val parts = json.convertTo[String].split(Array('.')).map(_.toInt)

IPAddress(parts(0), parts(1), parts(2), parts(3))
}
}
}

sealed trait EgressSpec
object EgressSpec extends DefaultJsonProtocol {
implicit val format: JsonFormat[EgressSpec] = new JsonFormat[EgressSpec] {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.monsanto.arch.cloudformation.model.resource

import org.scalatest.{Matchers, FunSpec}
import spray.json._

class EC2_UT extends FunSpec with Matchers {

describe("CidrBlock"){

val cidr = CidrBlock(192,168,1,2,32)

it("should write valid CidrBlock"){
cidr.toJson shouldEqual JsString("192.168.1.2/32")
}

it("should read valid CidrBlock") {
JsString("192.168.1.2/32").convertTo[CidrBlock] shouldEqual cidr
}

}

describe("IPAddress"){
val ipAddress = IPAddress(192,168,1,2)

it("should write valid IPAddress"){
ipAddress.toJson shouldEqual JsString("192.168.1.2")
}

it("should read valid IPAddress"){
JsString("192.168.1.2").convertTo[IPAddress] shouldEqual ipAddress
}

}
}

0 comments on commit 634ee34

Please sign in to comment.