/* eslint no-undef: "off" */
/* eslint global-require: "off" */

const SRC = '../../src';
const Where = require(`${SRC}/api/Where`);
const Beans = require('qnode-beans');

class MockedMockServerManager1 {

    get() {
        return null;//{putRule: function(){}};
    }
}

class MockedMockServerManager2 {
    
    get(port) {
        return {port};
    }
}

class MockedMockServer {
    
    putRule(domain, rule) {
        this.domain = domain;
        this.rule = rule;
    }
    
}

describe("api/Where test suite: ", function() {

    it("validate(): missing domain parameter", function() {
        const where = new Where();
        Beans.render(where);

        where.validate({
            request: {
                body: {
                    port: 7086
                }
            }
        }).then( () => failhere() )
        .catch( e => {
            expect(e.type.key).toBe('MISSING_PARAMETER');
            expect(e.args[0]).toBe('domain');
        } );
    });

    it("validate(): missing port parameter", function() {
        const where = new Where();
        Beans.render(where);

        where.validate({
            request: {
                body: {
                    domain: 'wxcount.com'
                }
            }
        }).then( () => failhere() )
        .catch( e => {
            expect(e.type.key).toBe('MISSING_PARAMETER');
            expect(e.args[0]).toBe('port');
        } );
    });

    it("validate(): mock server not found", function() {
        const beans = new Beans();
        beans.create(MockedMockServerManager1, 'MockServerManager');
        const where = beans.create(Where, 'Where');
        beans.init();
        
        where.validate({
            request: {
                body: {
                    port: 7086,
                    domain: 'wxcount.com'
                }
            }
        }).then( () => failhere() )
        .catch( e => {
            expect(e.type.key).toBe('MOCK_SERVER_NOT_FOUND');
            expect(e.args[0]).toBe(7086);
        } );
    });


    it("validate(): happy", function() {
        const beans = new Beans();
        beans.create(MockedMockServerManager2, 'MockServerManager');
        const where = beans.create(Where, 'Where');
        beans.init();
        
        where.validate({
            request: {
                body: {
                    port: 8076,
                    domain: 'wxcount.com'
                }
            }
        })
        .then( ({domain, rule, mockServer}) => {
            expect(domain).toBe('wxcount.com');
            expect(rule.domain).toBe('wxcount.com');
            expect(rule.port).toBe(8076);
            expect(mockServer.port).toBe(8076);
        })
        .catch( e => {
            failhere();
        } );
    });

    it("execute(): happy", function() {
        const beans = new Beans();
        const mockServerManager = beans.create(MockedMockServerManager2, 'MockServerManager');
        const mockServer = new MockedMockServer();
        const where = beans.create(Where, 'Where');
        beans.init();
        
        const ctx = {};
        const domain = 'github.com';
        const rule = {};
        
        const data = {domain, rule, mockServerManager};
            
        where.execute(ctx, {domain, rule, mockServer})
        .then( () => {
            expect(mockServer.domain).toEqual(domain);
            expect(mockServer.rule).toEqual(rule);
        } )
        .catch( e => {
            failhere();
        } );
    });

});
@@ -2250,32 +2250,24 @@
 ans.init();%0A
-        
 %0A        whe
@@ -2284,32 +2284,36 @@
 e(%7B%0A            
+    
 request: %7B%0A     
@@ -2299,32 +2299,36 @@
      request: %7B%0A
+    
                 
@@ -2351,24 +2351,28 @@
             
+    
 port: 8076,%0A
@@ -2383,32 +2383,36 @@
                 
+ 
+   
 domain: 'wxcount
@@ -2425,34 +2425,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -2465,27 +2465,34 @@
         
+ 
-%7D)%0A
+   %7D)%0A   
         
 .then( (
@@ -2487,17 +2487,18 @@
     
+ 
 .then(
- 
 (%7B
+ 
 doma
@@ -2517,16 +2517,17 @@
 ckServer
+ 
 %7D) =%3E %7B%0A
@@ -2534,24 +2534,28 @@
             
+  
+  
 expect(domai
@@ -2570,32 +2570,36 @@
 'wxcount.com');%0A
+    
             expe
@@ -2639,32 +2639,36 @@
 ');%0A            
+    
 expect(rule.port
@@ -2685,32 +2685,36 @@
 6);%0A            
+  
+  
 expect(mockServe
@@ -2733,35 +2733,43 @@
 (8076);%0A        
-%7D)%0A
+    %7D)%0A    
         .catch( 
@@ -2763,32 +2763,36 @@
      .catch(
- e
+()
  =%3E %7B%0A
+    
             
@@ -2803,34 +2803,37 @@
 here();%0A        
-%7D
  
+   %7D
 );%0A    %7D);%0A%0A    
@@ -2914,34 +2914,8 @@
     
- const mockServerManager =
  bea
@@ -3093,32 +3093,24 @@
 ans.init();%0A
-        
 %0A        con
@@ -3188,85 +3188,8 @@
 %7B%7D;%0A
-        %0A        const data = %7Bdomain, rule, mockServerManager%7D;%0A            
 %0A   
@@ -3213,16 +3213,17 @@
 e(ctx, %7B
+ 
 domain, 
@@ -3238,16 +3238,17 @@
 ckServer
+ 
 %7D)%0A     
@@ -3250,23 +3250,26 @@
         
+    
 .then(
- 
 () =%3E %7B%0A
@@ -3272,32 +3272,36 @@
 %3E %7B%0A            
+  
+  
 expect(mockServe
@@ -3327,16 +3327,20 @@
 omain);%0A
+    
         
@@ -3394,20 +3394,26 @@
     
-%7D )%0A
+    %7D)%0A   
         
 .cat
@@ -3404,39 +3404,44 @@
 %0A           
+ 
 .catch(
- e
+()
  =%3E %7B%0A
+    
             
@@ -3452,33 +3452,36 @@
 here();%0A        
-%7D
  
+   %7D
 );%0A    %7D);%0A%0A%7D);

Clean a little bit test code