Name: js-handler/legacy/test1.js 
1:
//Test 1: json client
2:
var restify = require('restify');
3:
var url=require ("url");
4:
var assert=require ('assert');
5:
var async=require('async');
6:
 
7:
var client=restify.createJsonClient({
8:
  url: 'http://sumatra.pri.univie.ac.at:9292',
9:
  version: '*'
10:
});
11:
 
12:
var data_elements =
13:
{"data_elements": [
14:
  { "identifier":"numberA",
15:
    "value":20
16:
  },
17:
  { "identifier":"text",
18:
    "value":"'content'"
19:
  }
20:
  ]
21:
};
22:
 
23:
var endpoints={
24:
    "endpoints": [
25:
    { "identifier":"endpointA",
26:
      "endpoint":"'www.xy.com'"
27:
    },
28:
    { "identifier":"endpointB",
29:
      "endpoint":"'www.abc.de'"
30:
    }
31:
    ]
32:
};
33:
 
34:
var code={"a" : "b"};
35:
var body={
36:
    "data_elements": [
37:
                    { "identifier":"numberA",
38:
                      "value":20
39:
                    },
40:
                    { "identifier":"text",
41:
                      "value":"'content'"
42:
                    }
43:
                    ],                    
44:
    "endpoints": [
45:
                 { "identifier":"endpointA",
46:
                  "endpoint":"'www.xy.com'"
47:
                 },
48:
                { "identifier":"endpointB",
49:
                 "endpoint":"'www.abc.de'"
50:
                }
51:
                ],
52:
     "code": "console.log(numberA); numberA=50; console.log(numberA);"  
53:
//     "code": "donsole.log(numberA); numberA=50; console.log(numberA);"  
54:
};
55:
 
56:
var errorBody={
57:
    "data_elements": [
58:
                      { "identifier":"numberA",
59:
                        "value":20
60:
                      },
61:
                      { "identifier":"text",
62:
                        "value":"'content'"
63:
                      }
64:
                      ],                    
65:
      "endpoints": [
66:
                   { "identifier":"endpointA",
67:
                    "endpoint":"'www.xy.com'"
68:
                   },
69:
                  { "identifier":"endpointB",
70:
                   "endpoint":"'www.abc.de'"
71:
                  }
72:
                  ],
73:
//       "code": "console.log(numberA); numberA=50; console.log(numberA);"  
74:
       "code": "donsole.log(numberA); numberA=50; console.log(numberA);"  
75:
  };
76:
 
77:
var jsonToXML={
78:
  "data_elements": [
79:
                    { "identifier":"numberA",
80:
                      "value":20
81:
                    },
82:
                    { "identifier":"text",
83:
                      "value":"'content'"
84:
                    },
85:
                  { "identifier":"xmlOutput",
86:
                        "value":"''"
87:
                  }
88:
                    ],                    
89:
    "endpoints": [
90:
                 { "identifier":"endpointA",
91:
                  "endpoint":"'www.xy.com'"
92:
                 },
93:
                { "identifier":"endpointB",
94:
                 "endpoint":"'www.abc.de'"
95:
                }
96:
                ],
97:
    "code": "console.log(xmlOutput); xmlOutput='<xml><data><numberA>'+numberA*2+'</numberA><text>'+text+'</text></data></xml>'; console.log(xmlOutput); "            
98:
};
99:
 
100:
 
101:
//test wrong url
102:
executeTests();
103:
 
104:
function executeTests(){
105:
  async.series([
106:
               
107:
  function(callback) {          
108:
  client.post('/hel', body, function(err,req,res,obj){
109:
    console.log("0, "+obj.code);
110:
    assert.equal(obj.code,"ResourceNotFound", "...server not responding?");
111:
    callback();
112:
  });
113:
  console.log("finished");
114:
  },
115:
 
116:
  //test without code
117:
  function(callback){
118:
  console.log("enter next");
119:
  client.post('/hello', {}, function(err,req,res,obj){
120:
    assert.equal(obj.status,"no code posted", "no code posted");
121:
    console.log("1");
122:
    callback();
123:
  });
124:
  },
125:
 
126:
  function(callback){
127:
  client.post('/hello', errorBody, function(err,req,res,obj){
128:
//    assert.ifError(err);
129:
//    console.log('%d -> %j', res.statusCode, res.headers);
130:
//    console.log('%j', obj);
131:
//    console.log("response: "+res);
132:
    assert.equal(obj.status," ReferenceError: donsole is not defined","no exception-exception");
133:
//    console.log("object 1:" +obj.status);
134:
    console.log("2");
135:
    callback();
136:
  });
137:
  },
138:
  //correct code
139:
 
140:
  function(callback){
141:
  client.post('/hello', body, function(err,req,res,obj){
142:
//    assert.ifError(err);
143:
//    console.log('%d -> %j', res.statusCode, res.headers);
144:
//    console.log('%j', obj);
145:
//    console.log("response: "+res);
146:
    assert.equal(obj.status,"ok","execution not successful");
147:
    assert.equal(obj.url, "http://sumatra.pri.univie.ac.at:9292/0/", "instance not created");
148:
    console.log("3");
149:
    callback();
150:
  });
151:
  },
152:
 
153:
  function(callback){
154:
  client.get('/0/numberA', function (err, req, res, obj){
155:
    assert.equal(obj,50," could not retrieve value of data element");
156:
    console.log("4");
157:
    callback();
158:
  });
159:
  },
160:
 
161:
  function(callback){
162:
  client.post('/hello', jsonToXML, function(err,req,res,obj){
163:
    assert.ifError(err);
164:
    console.log("5");
165:
    assert.equal(obj.status,"ok","execution not successful");
166:
    assert.equal(obj.url, "http://sumatra.pri.univie.ac.at:9292/1/", "instance not created");
167:
    callback();
168:
  });
169:
  },
170:
  
171:
  function(callback){
172:
    client.get('/1/xmlOutput', function (err, req, res, obj){
173:
      console.log("return: "+obj);
174:
      assert.equal(obj,'<xml><data><numberA>40</numberA><text>content</text></data></xml>'," could not retrieve value of data element");
175:
      console.log("6");
176:
      callback();
177:
    });
178:
  }
179:
  
180:
  
181:
  ]);
182:
}