"use strict";

var Promise = require('bluebird'),
    common = require('ng-common'),
    httpClient = new common.net.HttpClient(),
    RestAdapter = require('../lib/rest/RestAdapter'),
    TestInterface = require('./fixtures/TestInterface'),
    TestType = require('./fixtures/TestType');

describe('Rest', function () {

    it("should build a service for rest", function () {
        spyOn(httpClient, "get").and.returnValue(Promise.resolve());
        var restAdapter = new RestAdapter(httpClient),
            service = restAdapter.create(TestInterface);

        expect(service.getList).toBeDefined();
        expect(service.getList() instanceof Promise).toBe(true);
        expect(restAdapter._httpClient.get.calls.argsFor(0)[0]).toEqual("getListApi");
    });

    it("should build a service for rest with an endpoint", function () {
        spyOn(httpClient, "get").and.returnValue(Promise.resolve());
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.getList();
        expect(restAdapter._httpClient.get.calls.argsFor(0)[0]).toEqual("http://endpoint.com/getListApi");
    });

    it("should create a post method on the service", function () {
        spyOn(httpClient, "post").and.returnValue(Promise.resolve());
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.addList();
        expect(restAdapter._httpClient.post.calls.argsFor(0)[0]).toEqual("http://endpoint.com/addListApi");
    });

    it("should create a delete method on the service", function () {
        spyOn(httpClient, "delete").and.returnValue(Promise.resolve());
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.deleteList();
        expect(restAdapter._httpClient.delete.calls.argsFor(0)[0]).toEqual("http://endpoint.com/deleteListApi");
    });

    it("should create a put method on the service", function () {
        spyOn(httpClient, "put").and.returnValue(Promise.resolve());
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.updateList();
        expect(restAdapter._httpClient.put.calls.argsFor(0)[0]).toEqual("http://endpoint.com/updateListApi");
    });

    it("should create a jsonp method on the service", function () {
        spyOn(httpClient, "jsonp").and.returnValue(Promise.resolve());
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.jsonpList();
        expect(restAdapter._httpClient.jsonp.calls.argsFor(0)[0]).toEqual("http://endpoint.com/jsonpListApi");
    });

    it("should parse the result with the parser specified", function () {
        spyOn(httpClient, "get").and.returnValue(Promise.resolve());
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.getList();
        var args = restAdapter._httpClient.get.calls.argsFor(0);
        expect(args[0]).toEqual("http://endpoint.com/getListApi");
        expect(args[1]._gson._types[0]).toEqual(TestType);
    });

    it("should return the result as-is if no parser is specified", function (done) {
        spyOn(httpClient, "get").and.returnValue(Promise.resolve({'description': 'desc'}));
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.getListNoParse().then(function (response) {
            var args = restAdapter._httpClient.get.calls.argsFor(0);
            expect(response.description).toEqual('desc');
            expect(args[0]).toEqual("http://endpoint.com/getListApi");
            expect(args[1]).toBe(null);
            done();
        });
    });

    it("should substitute the url param if matches", function () {
        spyOn(httpClient, "get").and.returnValue(Promise.resolve({'description': 'desc'}));
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.getListWithId(55);
        expect(restAdapter._httpClient.get.calls.argsFor(0)[0]).toEqual("http://endpoint.com/getListApi/55");
    });

    it("should substitute the query string param if matches", function () {
        spyOn(httpClient, "get").and.returnValue(Promise.resolve({'description': 'desc'}));
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.getListWithIdAndQuery(55, 'couch');
        expect(restAdapter._httpClient.get.calls.argsFor(0)[0]).toEqual("http://endpoint.com/getListApi/55?query=couch");
    });

    it("should pass an object to the body post", function () {
        spyOn(httpClient, "post").and.returnValue(Promise.resolve({'description': 'desc'}));
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.addListWithIdAndData(60, {
            "description": "test list"
        });

        expect(restAdapter._httpClient.post).toHaveBeenCalledWith("http://endpoint.com/addListApi/60", {
            "description": "test list"
        }, null);
    });

    it("should pass an object also to the put body", function () {
        spyOn(httpClient, "put").and.returnValue(Promise.resolve({'description': 'desc'}));
        var restAdapter = new RestAdapter(httpClient);
        restAdapter.setEndpoint("http://endpoint.com/");
        var service = restAdapter.create(TestInterface);

        service.updateListWithIdAndData(60, {
            "description": "test list"
        });

        expect(restAdapter._httpClient.put).toHaveBeenCalledWith("http://endpoint.com/updateListApi/60", {
            "description": "test list"
        }, null);
    });
});
@@ -676,37 +676,24 @@
      expect(
-restAdapter._
 httpClient.g
@@ -731,32 +731,32 @@
 (%22getListApi%22);%0A
+
     %7D);%0A%0A    it(
@@ -1098,37 +1098,24 @@
      expect(
-restAdapter._
 httpClient.g
@@ -1535,37 +1535,24 @@
      expect(
-restAdapter._
 httpClient.p
@@ -1980,37 +1980,24 @@
      expect(
-restAdapter._
 httpClient.d
@@ -2424,37 +2424,24 @@
      expect(
-restAdapter._
 httpClient.p
@@ -2868,37 +2868,24 @@
      expect(
-restAdapter._
 httpClient.j
@@ -3319,37 +3319,24 @@
  var args = 
-restAdapter._
 httpClient.g
@@ -3921,29 +3921,16 @@
  args = 
-restAdapter._
 httpClie
@@ -4539,37 +4539,24 @@
      expect(
-restAdapter._
 httpClient.g
@@ -5035,37 +5035,24 @@
      expect(
-restAdapter._
 httpClient.g
@@ -5574,37 +5574,24 @@
      expect(
-restAdapter._
 httpClient.p
@@ -6140,32 +6140,32 @@
 t%22%0A        %7D);%0A%0A
+
         expect(r
@@ -6167,21 +6167,8 @@
 ect(
-restAdapter._
 http

Change access to http client for assertions