'use strict';
const chai = require('chai'),
    should = chai.should(),
    config = require('config'),
    Promise = require('bluebird'),
    request = Promise.promisifyAll(require('request')),
    solrAdmin = require('../lib/index/solr-admin');

describe('Test Solr admin/util module.', function() {
    this.timeout(0);
    const solrTestCollection = 'test-collection',
        testField = { 'name': 'package_name', 'type': 'string',
                      'indexed': true, 'required': true, 'stored': true
                    };

    before((done) => {
      solrAdmin.createCollection(solrTestCollection)
          .then(() => {
              return solrAdmin.addField(solrTestCollection, testField);
          })
          .then(() => {
              done();
          })
          .catch((e) => {
              done(e);
          });
    });

    it('It should ensure that the test collection ('
        + solrTestCollection + ') exists.', (done) => {
            solrAdmin.exists(solrTestCollection).then(() => {
                done();
            }).catch((e) => {
                done(e);
            });
    });

    it('It should ensure that the test field has been added to the test collection.', (done) => {
        const solrUrl = 'http://' + config.get('dbConfig.solr.host') + ':' +
            config.get('dbConfig.solr.port') + '/solr/' + solrTestCollection + '/schema/fields';
        return request.getAsync({
            url: solrUrl,
            qs: { wt: 'json' }
        }).spread((response, body) => {
            response.statusCode.should.be.equal(200);
            const resObj = JSON.parse(body);
            resObj.responseHeader.status.should.be.equal(0);
            resObj.fields.should.deep.include.members([testField]);
            done();
        }).catch((e) => {
            done(e);
      });
    });

    after((done) => {
      const solrUrl = 'http://' + config.get('dbConfig.solr.host') + ':' +
          config.get('dbConfig.solr.port') + '/solr/admin/collections';
      return request.postAsync({
          url: solrUrl,
          qs: {
              action: 'DELETE', name: solrTestCollection, wt: 'json'
          },
          json: true
      }).spread((response, body) => {
          if (response.statusCode === 200 && body.responseHeader.status === 0) {
              console.log('Solr test collection ' + solrTestCollection + ' has been deleted.');
              done();
          }
          else {
              throw new Error('Failed to delete our test collection: '
                  + solrTestCollection + '. Reason: ' + body.error.msg);
          }
      }).catch((e) => {
          done(e);
      });
    });
});

@@ -326,21 +326,17 @@
   const 
-solrT
+t
 estColle
@@ -578,21 +578,17 @@
 lection(
-solrT
+t
 estColle
@@ -610,32 +610,102 @@
   .then(() =%3E %7B%0A
+              console.log('Adding a new field to ' + testCollection);%0A
               re
@@ -728,21 +728,17 @@
 ddField(
-solrT
+t
 estColle
@@ -955,37 +955,33 @@
 on ('%0A        + 
-solrT
+t
 estCollection + 
@@ -1034,21 +1034,642 @@
 .exists(
-solrT
+testCollection).then(() =%3E %7B%0A                done();%0A            %7D).catch((e) =%3E %7B%0A                done(e);%0A            %7D);%0A    %7D);%0A%0A    it('It should ensure that a collection named no-collection does not exist.', (done) =%3E %7B%0A        solrAdmin.exists('no-collection')%0A            .then((res) =%3E %7B%0A                should.not.exist(res);%0A                done(new Error('collection should not exist.')); %7D)%0A            .catch((e) =%3E %7B%0A                should.exist(e);%0A                done();%0A            %7D);%0A    %7D);%0A%0A    it('It should not create a collection that already exists', (done) =%3E %7B%0A        solrAdmin.createCollection(t
 estColle
@@ -1673,32 +1673,35 @@
 llection).then((
+res
 ) =%3E %7B%0A         
@@ -1707,79 +1707,781 @@
     
-    done();%0A            %7D).catch((e) =%3E %7B%0A                done(e);%0A    
+res.should.contain('already exist');%0A            done();%0A        %7D).catch((e) =%3E %7B%0A            should.not.exist(e);%0A            done(e);%0A        %7D);%0A    %7D);%0A%0A    it('It should not create a field that already exists', (done) =%3E %7B%0A        solrAdmin.addField(testCollection, testField).then((res) =%3E %7B%0A            res.should.contain('already exist');%0A            done();%0A        %7D).catch((e) =%3E %7B%0A            should.not.exist(e);%0A            done(e);%0A        %7D);%0A    %7D);%0A%0A    it('It should not create an invalid field', (done) =%3E %7B%0A        solrAdmin.addField(testCollection, 'test').then((res) =%3E %7B%0A            should.not.exist(res);%0A            done(new Error('An invalid field has been created.'));%0A        %7D).catch((e) =%3E %7B%0A            should.exist(e);%0A            done();%0A
     
@@ -2726,29 +2726,25 @@
  '/solr/' + 
-solrT
+t
 estCollectio
@@ -3490,21 +3490,17 @@
 , name: 
-solrT
+t
 estColle
@@ -3722,21 +3722,17 @@
 ion ' + 
-solrT
+t
 estColle
@@ -3908,13 +3908,9 @@
   + 
-solrT
+t
 estC

add more test cases