 class AccountPNLDelegate: NSObject, StompClientDelegate {
     
     // MARK: - Public Properties
-    var expectation: XCTestExpectation!
+    var expectation: XCTestExpectation?
     
     // MARK: - Private Properties
     private let destination = "/account/accountpnl/laphone"
     
     // MARK: - Stomp Client Delegate
     func stompClientDidConnected(client: StompClient) {
-        client.subscribe(destination, parameters: nil)
-    }
-    
-    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
-        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
-        expectation.fulfill()
-    }
-    
-    func stompClient(client: StompClient, didReceivedData data: NSData) {
-        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
-        XCTAssertEqual(json["accountId"], "laphone", "Account Id is wrong.")
-        
-        client.unsubscribe(destination)
-        
-        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
-        expectation.fulfill()
-    }
-    
-}
+        client.subscribe(destination)
+    }
+    
+    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
+        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
+        expectation?.fulfill()
+    }
+    
+    func stompClient(client: StompClient, didReceivedData data: NSData) {
+        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
+        XCTAssertEqual(json["accountId"], "laphone")
+        XCTAssertNotNil(json["closePnl"])
+        
+        client.unsubscribe(destination)
+        
+        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
+        expectation?.fulfill()
+        expectation = nil
+    }
+    
+}
+
+class MarginUsageDelegate: NSObject, StompClientDelegate {
+    
+    // MARK: - Public Properties
+    var expectation: XCTestExpectation?
+    
+    // MARK: - Private Properties
+    private let destination = "/account/marginusage/laphone"
+    
+    // MARK: - Stomp Client Delegate
+    func stompClientDidConnected(client: StompClient) {
+        client.subscribe(destination)
+    }
+    
+    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
+        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
+        expectation?.fulfill()
+    }
+    
+    func stompClient(client: StompClient, didReceivedData data: NSData) {
+        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
+        XCTAssertEqual(json["accountId"], "laphone")
+        XCTAssertNotNil(json["marginUsage"])
+        
+        client.unsubscribe(destination)
+        
+        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
+        expectation?.fulfill()
+        expectation = nil
+    }
+    
+}
+
+class ModelInfoDelegate: NSObject, StompClientDelegate {
+    
+    // MARK: - Public Properties
+    var expectation: XCTestExpectation?
+    
+    // MARK: - Private Properties
+    private let destination = "/account/modelinfo/laphone"
+    
+    // MARK: - Stomp Client Delegate
+    func stompClientDidConnected(client: StompClient) {
+        client.subscribe(destination)
+    }
+    
+    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
+        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
+        expectation?.fulfill()
+    }
+    
+    func stompClient(client: StompClient, didReceivedData data: NSData) {
+        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
+        XCTAssertNotNil(json["eid"])
+        XCTAssertNotNil(json["modelName"])
+        XCTAssertNotNil(json["modelId"])
+        
+        client.unsubscribe(destination)
+        
+        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
+        expectation?.fulfill()
+        expectation = nil
+    }
+    
+}
+
+class SystemInfoDelegate: NSObject, StompClientDelegate {
+    
+    // MARK: - Public Properties
+    var expectation: XCTestExpectation?
+    
+    // MARK: - Private Properties
+    private let destination = "/account/sysinfo/laphone"
+    
+    // MARK: - Stomp Client Delegate
+    func stompClientDidConnected(client: StompClient) {
+        client.subscribe(destination)
+    }
+    
+    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
+        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
+        expectation?.fulfill()
+    }
+    
+    func stompClient(client: StompClient, didReceivedData data: NSData) {
+        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
+        XCTAssertNotNil(json["eid"])
+        XCTAssertNotNil(json["modelId"])
+        XCTAssertNotNil(json["level"])
+        
+        client.unsubscribe(destination)
+        
+        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
+        expectation?.fulfill()
+        expectation = nil
+    }
+    
+}
+
+class ModelPNLDelegate: NSObject, StompClientDelegate {
+    
+    // MARK: - Public Properties
+    var expectation: XCTestExpectation?
+    
+    // MARK: - Private Properties
+    private let destination = "/engine/modelpnl/5566/47"
+    
+    // MARK: - Stomp Client Delegate
+    func stompClientDidConnected(client: StompClient) {
+        client.subscribe(destination)
+    }
+    
+    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
+        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
+        expectation?.fulfill()
+    }
+    
+    func stompClient(client: StompClient, didReceivedData data: NSData) {
+        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
+        XCTAssertEqual(json["modelId"], 47)
+        XCTAssertNotNil(json["floatingPnl"])
+        
+        client.unsubscribe(destination)
+        
+        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
+        expectation?.fulfill()
+        expectation = nil
+    }
+}
+
+class BlotterDelegate: NSObject, StompClientDelegate {
+    
+    // MARK: - Public Properties
+    var expectation: XCTestExpectation?
+    
+    // MARK: - Private Properties
+    private let destination = "/engine/blotter/5566/47"
+    
+    // MARK: - Stomp Client Delegate
+    func stompClientDidConnected(client: StompClient) {
+        client.subscribe(destination)
+    }
+    
+    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
+        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
+        expectation?.fulfill()
+    }
+    
+    func stompClient(client: StompClient, didReceivedData data: NSData) {
+        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
+        XCTAssert(json is [AnyObject])
+        
+        client.unsubscribe(destination)
+        
+        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
+        expectation?.fulfill()
+        expectation = nil
+    }
+    
+}
+
+class SymbolsPriceDelegate: NSObject, StompClientDelegate {
+    
+    // MARK: - Public Properties
+    var expectation: XCTestExpectation?
+    
+    // MARK: - Private Properties
+    private let destination = "/symbol/symbols/price/cu1606"
+    
+    // MARK: - Stomp Client Delegate
+    func stompClientDidConnected(client: StompClient) {
+        client.subscribe(destination)
+    }
+    
+    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
+        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
+        expectation?.fulfill()
+    }
+    
+    func stompClient(client: StompClient, didReceivedData data: NSData) {
+        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
+        XCTAssertEqual(json["symbol"], "cu1606")
+        XCTAssertNotNil(json["time"])
+        
+        client.unsubscribe(destination)
+        
+        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
+        expectation?.fulfill()
+        expectation = nil
+    }
+    
+}
+
+class TopicLogDelegate: NSObject, StompClientDelegate {
+    
+    // MARK: - Public Properties
+    var expectation: XCTestExpectation?
+    
+    // MARK: - Private Properties
+    private let destination = "/user/topic/log"
+    
+    // MARK: - Stomp Client Delegate
+    func stompClientDidConnected(client: StompClient) {
+        client.subscribe(destination, parameters: ["eid" : "5566", "mid" : "47"])
+    }
+    
+    func stompClient(client: StompClient, didErrorOccurred error: NSError) {
+        XCTAssertTrue(false, "Error: \(error.localizedDescription)")
+        expectation?.fulfill()
+    }
+    
+    func stompClient(client: StompClient, didReceivedData data: NSData) {
+        let json = try! NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
+        XCTAssert(json is [AnyObject])
+        
+        client.unsubscribe(destination)
+        
+        XCTAssertNotNil(expectation, "Expectation doesn't setup.")
+        expectation?.fulfill()
+        expectation = nil
+    }
+    
+}

    
    private var client: StompClient!
    private var socket: WebSocket!
    private let host = "http://10.1.20.28:8080"
    
    override func setUp() {
        super.setUp()
        
        let url = NSURL(string: host + "/traderdesktop")!.appendServerIdAndSessionId()
        socket = WebSocket(url: url)
        client = StompClient(socket: socket)
    }
    
    override func tearDown() {
        super.tearDown()
    }
    
    // MARK: - Disabled Tests
    func disable_testSubscribeAccountPNL() {
        let delegate = AccountPNLDelegate()
        delegate.expectation = expectationWithDescription("Subscribe account pnl")
        client.delegate = delegate

        let session = NSURLSession.sharedSession()
        let loginURLString = host + "/trader/desktop/auth/login"
        let request = NSMutableURLRequest(URL: NSURL(string: loginURLString)!)
        request.HTTPMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        
        let param = ["id" : "laphone", "pass" : "laphone"]
        let data = try! NSJSONSerialization.dataWithJSONObject(param, options: NSJSONWritingOptions(rawValue: 0))
        request.HTTPBody = data
        

                let cookie = cookies.filter({
                    $0.name == "JSESSIONID"
                }).first!
                self.jSession = cookie.name + "=" + cookie.value
            } else {
                XCTAssert(false, "Status code isn't 200. Login failed.")
            }
            
            expectation.fulfill()
        }
        task.resume()
        
        waitForExpectationsWithTimeout(5.0, handler: nil)
    }
    
    private func logOut() {
        let expectation = expectationWithDescription("Log out")
        
        let session = NSURLSession.sharedSession()
        let url = baseURL.URLByAppendingPathComponent("/trader/desktop/auth/logout")
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        
        let task = session.dataTaskWithRequest(request) { (data, response, error) in
            let res = response as! NSHTTPURLResponse
            if res.statusCode != 200 {
                XCTAssert(false, "Status code isn't 200. Logout failed.")
            }
            
            expectation.fulfill()
        }
        task.resume()
        
        waitForExpectationsWithTimeout(5.0, handler: nil)
    }
    
    // MARK: - Disabled Tests
    func disbale_testSubscribeTopicLog() {
        let delegate = TopicLogDelegate()
        delegate.expectation = expectationWithDescription("Subscribe topci log")
        client.delegate = delegate
        
        client.connect()
        
        waitForExpectationsWithTimeout(5.0, handler: nil)
    }
    
    func disable_testSubscribeSystemInfo() {
        let delegate = SystemInfoDelegate()
        delegate.expectation = expectationWithDescription("Subscribe system info")
        client.delegate = delegate
        
        client.connect()
        
        waitForExpectationsWithTimeout(5.0, handler: nil)
    }
    
}
