def test_parse_get_leader(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        msg = ncore.GetLeader(destination=d0)

        req = nhttp.build_request(msg)
        parsed = nhttp.parse_request(req)
        self.assertEqual(parsed, msg)
    def test_parse_new_predecessor(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        msg = ncore.NewPredecessor(destination=d1, predecessor=d0)

        req = nhttp.build_request(msg)
        parsed = nhttp.parse_request(req)
        self.assertEqual(parsed, msg)
    def test_parse_join(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        msg = ncore.Join(destination=d0, new_node=d1)

        req = nhttp.build_request(msg)
        parsed = nhttp.parse_request(req)
        self.assertEqual(parsed, msg)
    def test_parse_election_result(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        msg = ncore.ElectionResult(destination=d0, new_leader=d1)

        req = nhttp.build_request(msg)
        parsed = nhttp.parse_request(req)
        self.assertEqual(parsed, msg)
    def test_parse_election(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        msg = ncore.Election(destination=d0, participants=[d0,d1])

        req = nhttp.build_request(msg)
        parsed = nhttp.parse_request(req)
        self.assertEqual(parsed, msg)
    def test_build_get_leader(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        msg = ncore.GetLeader(destination=d0)

        req = nhttp.build_request(msg)

        self.assertEqual(req.method, "GET")
        self.assertEqual(req.path, "/getCurrentLeader")
        self.assertEqual(req.body, "")
    def test_build_new_predecessor(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        msg = ncore.NewPredecessor(destination=d1, predecessor=d0)

        req = nhttp.build_request(msg)

        self.assertEqual(req.method, "PUT")
        self.assertEqual(req.path, "/predecessor")
        self.assertEqual(req.body, "localhost:8000")
    def test_parse_join_accept(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        d2 = ncore.NodeDescriptor(host_port="localhost:8002")
        d3 = ncore.NodeDescriptor(host_port="localhost:8003")
        msg = ncore.JoinAccepted(destination=d1, successor=d2, predecessor=d0, leader=d3)

        req = nhttp.build_request(msg)
        parsed = nhttp.parse_request(req)
        self.assertEqual(parsed, msg)
    def test_build_join(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        msg = ncore.Join(destination=d0, new_node=d1)

        req = nhttp.build_request(msg)

        self.assertEqual(req.method, "POST")
        self.assertEqual(req.path, "/join")
        self.assertEqual(req.body, "localhost:8001")
    def test_build_election_result(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        msg = ncore.ElectionResult(destination=d0, new_leader=d1)

        req = nhttp.build_request(msg)

        self.assertEqual(req.method, "POST")
        self.assertEqual(req.path, "/election/result")
        self.assertEqual(req.body, "localhost:8001")
    def test_build_election(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        msg = ncore.Election(destination=d0, participants=[d0,d1])

        req = nhttp.build_request(msg)

        self.assertEqual(req.method, "POST")
        self.assertEqual(req.path, "/election")
        self.assertEqual(req.body, "localhost:8000\nlocalhost:8001")
    def test_build_join_accept(self):
        d0 = ncore.NodeDescriptor(host_port="localhost:8000")
        d1 = ncore.NodeDescriptor(host_port="localhost:8001")
        d2 = ncore.NodeDescriptor(host_port="localhost:8002")
        d3 = ncore.NodeDescriptor(host_port="localhost:8003")
        msg = ncore.JoinAccepted(destination=d1, successor=d2, predecessor=d0, leader=d3)

        req = nhttp.build_request(msg)

        self.assertEqual(req.method, "POST")
        self.assertEqual(req.path, "/join/accepted")
        self.assertEqual(req.body,
                "successor = localhost:8002\npredecessor = localhost:8000\nleader = localhost:8003\n")