/*!
 * Copyright 2015 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.
 */

'use strict';

var assert = require('assert');

var env = require('./env.js');
var Resource = require('../lib/resource/index.js');

describe('Resource', function() {
  // -------------
  // >> Attention!
  // -------------
  // As of 9/14/15, creating projects is not supported. Once we have support,
  // the following description outlines how we should run our tests.
  // -------------
  //
  // Before the tests run, we create a project. That acts as the test for being
  // able to create a project. Similarly, the after hook deletes it, testing if
  // a project can be deleted.
  //
  // ----------
  // >> Notice!
  // ----------
  // All tests should only manipulate a short-lived project. NOT the project the
  // user has been running our test suite with. That would just be rude.
  // ----------

  // var PROJECT_ID = 'gcloud-tests-' + Date.now();
  var PROJECT_NAME = 'gcloud-tests-project-name';

  var resource = new Resource(env);
  var project;

  before(function(done) {
    // Uncomment after we support creating a project.
    // resource.createProject(PROJECT_ID, function(err, project_) {
    //   if (err) {
    //     done(err);
    //     return;
    //   }
    //
    //   project = project_;
    // });

    // ** SEE "Notice!" SECTION ABOVE **
    // Remove once we support creating a project.
    project = resource.project();
    done();
  });

  // Uncomment after we support creating a project.
  // after(function(done) {
  //   project.delete(done);
  // });

  describe('resource', function() {
    it('should get a list of projects', function(done) {
      resource.getProjects(function(err, projects) {
        assert.ifError(err);
        assert(projects.length > 0);
        done();
      });
    });

    it('should get a list of projects in stream mode', function(done) {
      var resultsMatched = 0;

      resource.getProjects()
        .on('error', done)
        .on('data', function() {
          resultsMatched++;
        })
        .on('end', function() {
          assert(resultsMatched > 0);
          done();
        });
    });
  });

  describe('project', function() {
    it('should get metadata', function(done) {
      project.getMetadata(function(err, metadata) {
        assert.ifError(err);
        assert.strictEqual(metadata.projectId, project.id);
        done();
      });
    });

    it.skip('should set metadata', function(done) {
      project.getMetadata(function(err, metadata) {
        assert.ifError(err);

        var originalProjectName = metadata.name;

        project.setMetadata({
          name: PROJECT_NAME
        }, function(err) {
          assert.ifError(err);

          project.setMetadata({
            name: originalProjectName
          }, done);
        });
      });
    });

    it.skip('should delete and restore a project', function(done) {
      project.delete(function(err) {
        assert.ifError(err);
        project.restore(done);
      });
    });
  });
});

@@ -784,1331 +784,75 @@
 %7B%0A  
-// -------------%0A  // %3E%3E Attention!%0A  // -------------%0A  // As of 9/14/15, creating projects is not supported. Once we have support,%0A  // the following description outlines how we should run our tests.%0A  // -------------%0A  //%0A  // Before the tests run, we create a project. That acts as the test for being%0A  // able to create a project. Similarly, the after hook deletes it, testing if%0A  // a project can be deleted.%0A  //%0A  // ----------%0A  // %3E%3E Notice!%0A  // ----------%0A  // All tests should only manipulate a short-lived project. NOT the project the%0A  // user has been running our test suite with. That would just be rude.%0A  // ----------%0A%0A  // var PROJECT_ID = 'gcloud-tests-' + Date.now();%0A  var PROJECT_NAME = 'gcloud-tests-project-name';%0A%0A  var resource = new Resource(env);%0A  var project;%0A%0A  before(function(done) %7B%0A    // Uncomment after we support creating a project.%0A    // resource.createProject(PROJECT_ID, function(err, project_) %7B%0A    //   if (err) %7B%0A    //     done(err);%0A    //     return;%0A    //   %7D%0A    //%0A    //   project = project_;%0A    // %7D);%0A%0A    // ** SEE %22Notice!%22 SECTION ABOVE **%0A    // Remove once we support creating a project.%0A    project = resource.project();%0A    done();%0A  %7D);%0A%0A  // Uncomment after we support creating a project.%0A  // after(function(done) %7B%0A  //   project.delete(done);%0A  // %7D
+var resource = new Resource(env);%0A  var project = resource.project(
 );%0A%0A
@@ -1705,60 +1705,762 @@
 %7D);%0A
-%0A    it.skip('should set metadata', function(done) %7B
+  %7D);%0A%0A  // Auth through the gcloud SDK is required to:%0A  //%0A  //   - Create a project%0A  //   - Set metadata%0A  //   - Restore a project%0A  //   - Delete a project%0A  describe('user-auth only functionality', function() %7B%0A    var resource = new Resource(%7B%0A      projectId: env.projectId%0A    %7D);%0A%0A    var PROJECT_ID = 'gcloud-tests-' + Date.now();%0A    var project = resource.project(PROJECT_ID);%0A%0A    before(function(done) %7B%0A      project.create(done);%0A    %7D);%0A%0A    after(function(done) %7B%0A      project.delete(done);%0A    %7D);%0A%0A    it('should have created the project', function() %7B%0A      assert.strictEqual(project.metadata.projectId, PROJECT_ID);%0A    %7D);%0A%0A    it('should set metadata', function(done) %7B%0A      var newProjectName = 'gcloud-tests-project-name';%0A
 %0A   
@@ -2586,16 +2586,84 @@
 ta.name;
+%0A        assert.notStrictEqual(originalProjectName, newProjectName);
 %0A%0A      
@@ -2706,20 +2706,22 @@
 me: 
-PROJECT_NAME
+newProjectName
 %0A   
@@ -2907,13 +2907,8 @@
   it
-.skip
 ('sh
@@ -2916,27 +2916,16 @@
 uld 
-delete and 
 restore 
 a pr
@@ -2916,25 +2916,27 @@
 uld restore 
-a
+the
  project', f

use default method options for Resource