// Path: src/main/java/com/google/jenkins/plugins/storage/AbstractUploadDescriptor.java
// public static final String GCS_SCHEME = "gs://";
// 
// Path: src/main/java/com/google/jenkins/plugins/storage/GoogleCloudStorageUploader.java
// @Extension
// public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
// 
//   /** {@inheritDoc} */
//   @Override
//   public boolean isApplicable(
//       @SuppressWarnings("rawtypes") Class<? extends AbstractProject> aClass) {
//     // Indicates that this builder can be used with all kinds of project types
//     return true;
//   }
// 
//   /** {@inheritDoc} */
//   @Override
//   public String getDisplayName() {
//     return Messages.GoogleCloudStorageUploader_DisplayName();
//   }
// 
//   /**
//    * @return the default uploads when the user configure {@link GoogleCloudStorageUploader} for
//    *     the first time.
//    */
//   public List<AbstractUpload> getDefaultUploads() {
//     StdoutUpload upload =
//         new StdoutUpload(
//             GCS_SCHEME, null, "build-log.txt", null /* legacy arg: bucketNameWithVars */);
//     upload.setForFailedJobs(true);
//     return ImmutableList.<AbstractUpload>of(upload);
//   }
// 
//   /** @return All registered {@link AbstractUpload}s. */
//   public List<AbstractUploadDescriptor> getUploads() {
//     return AbstractUpload.all();
//   }
// }


import static com.google.jenkins.plugins.storage.AbstractUploadDescriptor.GCS_SCHEME;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.when;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.StorageObject;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.jenkins.plugins.credentials.oauth.GoogleOAuth2ScopeRequirement;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import com.google.jenkins.plugins.storage.GoogleCloudStorageUploader.DescriptorImpl;
import com.google.jenkins.plugins.util.ConflictException;
import com.google.jenkins.plugins.util.ForbiddenException;
import com.google.jenkins.plugins.util.MockExecutor;
import com.google.jenkins.plugins.util.NotFoundException;
import hudson.model.AbstractProject;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.Run;
import hudson.tasks.Shell;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Verifier;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/*
 * Copyright 2013 Google LLC
 *
 * 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
 *
 *     https://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.
 */
package com.google.jenkins.plugins.storage;



/** Tests for {@link GoogleCloudStorageUploader}. */
public class GoogleCloudStorageUploaderTest {

  @Rule public JenkinsRule jenkins = new JenkinsRule();

  @Mock private GoogleRobotCredentials credentials;

  private GoogleCredential credential;

  private String bucket;
  private String glob;

  private FreeStyleProject project;
  private GoogleCloudStorageUploader underTest;
  private boolean sharedPublicly;
  private boolean forFailedJobs;
  private boolean showInline;
  private boolean stripPathPrefix;
  private String pathPrefix;

  private final MockExecutor executor = new MockExecutor();
  private ConflictException conflictException;
  private ForbiddenException forbiddenException;
  private NotFoundException notFoundException;

  private AbstractUpload setOptionalParams(AbstractUpload a) {
    a.setSharedPublicly(sharedPublicly);
    a.setForFailedJobs(forFailedJobs);
    a.setShowInline(showInline);
    if (stripPathPrefix) {
      a.setPathPrefix(pathPrefix);
    }
    return a;
  }

  private static class MockUploadModule extends UploadModule {

    public MockUploadModule(MockExecutor executor) {
      this.executor = executor;
    }

    @Override
    public MockExecutor newExecutor() {
      return executor;
    }

    private final MockExecutor executor;
  }

  @Rule
  public Verifier verifySawAll =
      new Verifier() {
        @Override
        public void verify() {
          assertTrue(executor.sawAll());
          assertFalse(executor.sawUnexpected());
        }
      };

  /**
   * Checks that any object insertion that we do has certain properties at the point of execution.
   */
  private Predicate<Storage.Objects.Insert> checkFieldsMatch =
      new Predicate<Storage.Objects.Insert>() {
        public boolean apply(Storage.Objects.Insert insertion) {
          assertNotNull(insertion.getMediaHttpUploader());
// Path: src/main/java/com/google/jenkins/plugins/storage/AbstractUploadDescriptor.java
// public static final String GCS_SCHEME = "gs://";
// 
// Path: src/main/java/com/google/jenkins/plugins/storage/GoogleCloudStorageUploader.java
// @Extension
// public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
// 
//   /** {@inheritDoc} */
//   @Override
//   public boolean isApplicable(
//       @SuppressWarnings("rawtypes") Class<? extends AbstractProject> aClass) {
//     // Indicates that this builder can be used with all kinds of project types
//     return true;
//   }
// 
//   /** {@inheritDoc} */
//   @Override
//   public String getDisplayName() {
//     return Messages.GoogleCloudStorageUploader_DisplayName();
//   }
// 
//   /**
//    * @return the default uploads when the user configure {@link GoogleCloudStorageUploader} for
//    *     the first time.
//    */
//   public List<AbstractUpload> getDefaultUploads() {
//     StdoutUpload upload =
//         new StdoutUpload(
//             GCS_SCHEME, null, "build-log.txt", null /* legacy arg: bucketNameWithVars */);
//     upload.setForFailedJobs(true);
//     return ImmutableList.<AbstractUpload>of(upload);
//   }
// 
//   /** @return All registered {@link AbstractUpload}s. */
//   public List<AbstractUploadDescriptor> getUploads() {
//     return AbstractUpload.all();
//   }
// }


// Path: src/test/java/com/google/jenkins/plugins/storage/GoogleCloudStorageUploaderTest.java
import static com.google.jenkins.plugins.storage.AbstractUploadDescriptor.GCS_SCHEME;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.when;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.StorageObject;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.jenkins.plugins.credentials.oauth.GoogleOAuth2ScopeRequirement;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import com.google.jenkins.plugins.storage.GoogleCloudStorageUploader.DescriptorImpl;
import com.google.jenkins.plugins.util.ConflictException;
import com.google.jenkins.plugins.util.ForbiddenException;
import com.google.jenkins.plugins.util.MockExecutor;
import com.google.jenkins.plugins.util.NotFoundException;
import hudson.model.AbstractProject;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.Run;
import hudson.tasks.Shell;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Verifier;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/*
 * Copyright 2013 Google LLC
 *
 * 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
 *
 *     https://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.
 */
package com.google.jenkins.plugins.storage;



/** Tests for {@link GoogleCloudStorageUploader}. */
public class GoogleCloudStorageUploaderTest {

  @Rule public JenkinsRule jenkins = new JenkinsRule();

  @Mock private GoogleRobotCredentials credentials;

  private GoogleCredential credential;

  private String bucket;
  private String glob;

  private FreeStyleProject project;
  private GoogleCloudStorageUploader underTest;
  private boolean sharedPublicly;
  private boolean forFailedJobs;
  private boolean showInline;
  private boolean stripPathPrefix;
  private String pathPrefix;

  private final MockExecutor executor = new MockExecutor();
  private ConflictException conflictException;
  private ForbiddenException forbiddenException;
  private NotFoundException notFoundException;

  private AbstractUpload setOptionalParams(AbstractUpload a) {
    a.setSharedPublicly(sharedPublicly);
    a.setForFailedJobs(forFailedJobs);
    a.setShowInline(showInline);
    if (stripPathPrefix) {
      a.setPathPrefix(pathPrefix);
    }
    return a;
  }

  private static class MockUploadModule extends UploadModule {

    public MockUploadModule(MockExecutor executor) {
      this.executor = executor;
    }

    @Override
    public MockExecutor newExecutor() {
      return executor;
    }

    private final MockExecutor executor;
  }

  @Rule
  public Verifier verifySawAll =
      new Verifier() {
        @Override
        public void verify() {
          assertTrue(executor.sawAll());
          assertFalse(executor.sawUnexpected());
        }
      };

  /**
   * Checks that any object insertion that we do has certain properties at the point of execution.
   */
  private Predicate<Storage.Objects.Insert> checkFieldsMatch =
      new Predicate<Storage.Objects.Insert>() {
        public boolean apply(Storage.Objects.Insert insertion) {
          assertNotNull(insertion.getMediaHttpUploader());
          assertEquals(bucket.substring(GCS_SCHEME.length()), insertion.getBucket());