1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.tradefed.util; 18 19 import com.google.api.services.storage.Storage; 20 21 import org.junit.After; 22 import org.junit.Assert; 23 import org.junit.Before; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 import java.io.ByteArrayInputStream; 29 import java.io.ByteArrayOutputStream; 30 import java.io.File; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.util.Collections; 34 35 /** {@link GCSFileUploader} functional test. */ 36 @RunWith(JUnit4.class) 37 public class GCSFileUploaderFuncTest { 38 private static final String BUCKET_NAME = "tradefed_function_test"; 39 private static final String FILE_DATA = "Simple test string to write to file."; 40 private static final String FILE_MIME_TYPE = "text/plain"; 41 private static final boolean ENABLE_OVERWRITE = true; 42 private static final boolean DISABLE_OVERWRITE = false; 43 44 private File mTestFile; 45 46 private GCSFileUploader mUploader; 47 private GCSFileDownloader mDownloader; 48 49 @Before setUp()50 public void setUp() throws Exception { 51 mUploader = new GCSFileUploader(); 52 mDownloader = new GCSFileDownloader(); 53 mTestFile = FileUtil.createTempFile("test-upload-file", ".txt"); 54 } 55 56 @After tearDown()57 public void tearDown() throws IOException { 58 try { 59 Storage storage = 60 mUploader.getStorage( 61 Collections.singleton( 62 "https://www.googleapis.com/auth/devstorage.read_write")); 63 storage.objects().delete(BUCKET_NAME, mTestFile.getName()).execute(); 64 } finally { 65 FileUtil.recursiveDelete(mTestFile); 66 } 67 } 68 69 @Test testUploadFile_roundTrip()70 public void testUploadFile_roundTrip() throws Exception { 71 InputStream uploadFileStream = new ByteArrayInputStream(FILE_DATA.getBytes()); 72 mUploader.uploadFile( 73 BUCKET_NAME, 74 mTestFile.getName(), 75 uploadFileStream, 76 FILE_MIME_TYPE, 77 ENABLE_OVERWRITE); 78 String readBack = new String(toByteArray(pullFileFromGcs(mTestFile.getName()))); 79 Assert.assertEquals(FILE_DATA, readBack); 80 } 81 82 @Test testUploadFile_overwrite()83 public void testUploadFile_overwrite() throws Exception { 84 InputStream uploadFileStream = new ByteArrayInputStream(FILE_DATA.getBytes()); 85 mUploader.uploadFile( 86 BUCKET_NAME, 87 mTestFile.getName(), 88 uploadFileStream, 89 FILE_MIME_TYPE, 90 DISABLE_OVERWRITE); 91 92 try { 93 mUploader.uploadFile( 94 BUCKET_NAME, 95 mTestFile.getName(), 96 uploadFileStream, 97 FILE_MIME_TYPE, 98 DISABLE_OVERWRITE); 99 Assert.fail("Should throw IOException."); 100 } catch (IOException e) { 101 // Expect IOException 102 } 103 104 mUploader.uploadFile( 105 BUCKET_NAME, 106 mTestFile.getName(), 107 uploadFileStream, 108 FILE_MIME_TYPE, 109 ENABLE_OVERWRITE); 110 } 111 toByteArray(InputStream in)112 private byte[] toByteArray(InputStream in) throws IOException { 113 114 ByteArrayOutputStream os = new ByteArrayOutputStream(); 115 116 byte[] buffer = new byte[1024]; 117 int len; 118 119 // read bytes from the input stream and store them in buffer 120 while ((len = in.read(buffer)) != -1) { 121 // write bytes from the buffer into output stream 122 os.write(buffer, 0, len); 123 } 124 125 return os.toByteArray(); 126 } 127 pullFileFromGcs(String gcsFilePath)128 private InputStream pullFileFromGcs(String gcsFilePath) throws IOException { 129 return mDownloader.downloadFile(BUCKET_NAME, gcsFilePath); 130 } 131 } 132