<commit_before>/**
 * @license
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

describe('RestDAO', function() {
  var baseURL = 'http://localhost:8765/v0/testDAO';
  var dao;
  var url = require('url');

  beforeEach(function() {
    foam.CLASS({
      package: 'foam.dao.test',
      name: 'MockRestDAOHttpRequest',
      extends: 'foam.net.HTTPRequest',

      requires: [
        'foam.dao.ArrayDAO',
        'foam.net.HTTPResponse'
      ],

      properties: [
        {
          class: 'String',
          name: 'baseURL',
          value: baseURL
        }
      ],

      methods: [
        function send() {
          var jsonify = this.jsonify;
          var createResponse = this.createResponse;

          if ( this.method === 'PUT' && this.url === this.baseURL ) {
            // put()
            var obj;
            var payload;
            var skip;
            var limit;
            var order;
            var predicate;
            try {
              obj = foam.json.parseString(this.payload);
              payload = jsonify(obj);
            } catch (err) {
              return Promise.resolve(this.createResponse({ status: 500 }));
            }

            return dao.put(obj).then(function() {
              return createResponse({ status: 200, payload: payload });
            });
          } else if ( this.method === 'DELETE' && this.url.indexOf(this.baseURL) === 0 ) {
            // remove()
            var id = JSON.parse(decodeURIComponent(this.url.substr(this.baseURL.length + 1)));
            return dao.find(id).then(function(o) {
              payload = jsonify(o);
              return dao.remove(o);
            }).then(function() {
              return createResponse({ status: 200, payload: payload });
            });
          } else if ( this.method === 'GET' && this.url.indexOf(this.baseURL) === 0 && this.url.charAt(this.baseURL.length) === '/' ) {
            // find()
            var id = JSON.parse(decodeURIComponent(this.url.substr(this.baseURL.length + 1)));
            return dao.find(id).then(function(o) {
              return createResponse({ status: 200, payload: jsonify(o) });
            });
          } else if ( this.method === 'GET' && this.url.indexOf(this.baseURL) === 0 && this.url.substring(this.baseURL.length).match(/^:select([?].*)$/) ) {
            // select()
            var data = url.parse(this.url, true).query;
            try {
              for ( var key in data ) {
                // Note: This would use data.hasOwnProperty(key), except that
                // Node JS ParsedQueryString objects do not descend from
                // Object.prototype.
                data[key] = foam.json.parseString(decodeURIComponent(data[key]));
              }
            } catch (err) {
              return Promise.resolve(createResponse({ status: 500 }));
            }
            skip = data.skip;
            limit = data.limit;
            order = data.order;
            predicate = data.predicate;
            return dao.select(undefined, skip, limit, order, predicate).then(function(sink) {
              var payload = jsonify(sink.a);
              return createResponse({ status: 200, payload: payload });
            });
          } else if ( this.method === 'POST' && this.url === this.baseURL + ':removeAll' ) {
            // removeAll()
            var data;
            try {
              data = foam.json.parseString(this.payload);
            } catch (err) {
              return Promise.resolve(createResponse({ status: 500 }));
            }
            skip = data.skip;
            limit = data.limit;
            order = data.order;
            predicate = data.predicate;
            return dao.removeAll(skip, limit, order, predicate).then(function() {
              return Promise.resolve(createResponse({ status: 200 }));
            });
          }

          debugger;

          // Request doesn't match DAO REST API. Return "Not Found".
          return Promise.resolve(createResponse({ status: 404 }));
        }
      ],

      listeners: [
        function jsonify(o) {
          return JSON.stringify(foam.json.Network.objectify(o));
        },
        function createResponse(o) {
          return this.HTTPResponse.create(
            Object.assign({responseType: 'json'}, o));
        }
      ]
    });

    foam.register(foam.lookup('foam.dao.test.MockRestDAOHttpRequest'), 'foam.net.HTTPRequest');
  });

  global.genericDAOTestBattery(function(of) {
    // Each test MUST assume that it operates over one and the same DAO for
    // all that test's expectations.
    dao = foam.dao.ArrayDAO.create({ of: of });

    return Promise.resolve(foam.dao.RestDAO.create({
      of: of,
      baseURL: baseURL<commit_msg>RestDAO tests: Fix long lines<commit_after>/**
 * @license
 * Copyright 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

describe('RestDAO', function() {
  var baseURL = 'http://localhost:8765/v0/testDAO';
  var dao;
  var url = require('url');

  beforeEach(function() {
    foam.CLASS({
      package: 'foam.dao.test',
      name: 'MockRestDAOHttpRequest',
      extends: 'foam.net.HTTPRequest',

      requires: [
        'foam.dao.ArrayDAO',
        'foam.net.HTTPResponse'
      ],

      properties: [
        {
          class: 'String',
          name: 'baseURL',
          value: baseURL
        }
      ],

      methods: [
        function send() {
          var jsonify = this.jsonify;
          var createResponse = this.createResponse;

          if ( this.method === 'PUT' && this.url === this.baseURL ) {
            // put()
            var obj;
            var id;
            var payload;
            var skip;
            var limit;
            var order;
            var predicate;
            try {
              obj = foam.json.parseString(this.payload);
              payload = jsonify(obj);
            } catch (err) {
              return Promise.resolve(this.createResponse({ status: 500 }));
            }

            return dao.put(obj).then(function() {
              return createResponse({ status: 200, payload: payload });
            });
          } else if ( this.method === 'DELETE' &&
                      this.url.indexOf(this.baseURL) === 0 ) {
            // remove()
            id = JSON.parse(decodeURIComponent(this.url.substr(
              this.baseURL.length + 1)));
            return dao.find(id).then(function(o) {
              payload = jsonify(o);
              return dao.remove(o);
            }).then(function() {
              return createResponse({ status: 200, payload: payload });
            });
          } else if ( this.method === 'GET' &&
                      this.url.indexOf(this.baseURL) === 0 &&
                      this.url.charAt(this.baseURL.length) === '/' ) {
            // find()
            id = JSON.parse(decodeURIComponent(this.url.substr(
              this.baseURL.length + 1)));
            return dao.find(id).then(function(o) {
              return createResponse({ status: 200, payload: jsonify(o) });
            });
          } else if ( this.method === 'GET' &&
                      this.url.indexOf(this.baseURL) === 0 &&
                      this.url.substring(this.baseURL.length)
                          .match(/^:select([?].*)$/) ) {
            // select()
            var data = url.parse(this.url, true).query;
            try {
              for ( var key in data ) {
                // Note: This would use data.hasOwnProperty(key), except that
                // Node JS ParsedQueryString objects do not descend from
                // Object.prototype.
                data[key] = foam.json.parseString(decodeURIComponent(
                  data[key]));
              }
            } catch (err) {
              return Promise.resolve(createResponse({ status: 500 }));
            }
            skip = data.skip;
            limit = data.limit;
            order = data.order;
            predicate = data.predicate;
            return dao.select(undefined, skip, limit, order, predicate)
              .then(function(sink) {
                var payload = jsonify(sink.a);
                return createResponse({ status: 200, payload: payload });
              });
          } else if ( this.method === 'POST' &&
                      this.url === this.baseURL + ':removeAll' ) {
            // removeAll()
            var data;
            try {
              data = foam.json.parseString(this.payload);
            } catch (err) {
              return Promise.resolve(createResponse({ status: 500 }));
            }
            skip = data.skip;
            limit = data.limit;
            order = data.order;
            predicate = data.predicate;
            return dao.removeAll(skip, limit, order, predicate)
              .then(function() {
                return Promise.resolve(createResponse({ status: 200 }));
              });
          }

          debugger;

          // Request doesn't match DAO REST API. Return "Not Found".
          return Promise.resolve(createResponse({ status: 404 }));
        }
      ],

      listeners: [
        function jsonify(o) {
          return JSON.stringify(foam.json.Network.objectify(o));
        },
        function createResponse(o) {
          return this.HTTPResponse.create(
            Object.assign({responseType: 'json'}, o));
        }
      ]
    });

    foam.register(
      foam.lookup('foam.dao.test.MockRestDAOHttpRequest'),
      'foam.net.HTTPRequest');
  });

  global.genericDAOTestBattery(function(of) {
    // Each test MUST assume that it operates over one and the same DAO for
    // all that test's expectations.
    dao = foam.dao.ArrayDAO.create({ of: of });

    return Promise.resolve(foam.dao.RestDAO.create({
      of: of,
      baseURL: baseURL
{'fraction_non_alphanumeric': 0.12572267596586217, 'fraction_numerical': 0.008259153895567587, 'mean_word_length': 1.7042183622828784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6055571', 'n_tokens_mistral': 3014, 'n_tokens_neox': 2918, 'n_words': 736}