GoogleSpeechTranscriptionServiceStorage { public int startUpload(CloseableHttpClient httpClient, String bucket, String mpId, String fileExtension, File file, String byteSize, String contentType, String accessToken) throws IOException { CloseableHttpResponse postResponse = null; CloseableHttpResponse putResponse = null; String location = null; try { HttpPost httpPost = new HttpPost(GOOGLE_STORAGE_LINK + String.format( "%s/o?uploadType=resumable&name=%s.%s", bucket, mpId, fileExtension)); logger.debug("Url to store media file on Google Cloud Storage : {}", httpPost.getURI().toString()); httpPost.addHeader("Authorization", "Bearer " + accessToken); httpPost.addHeader("X-Upload-Content-Type", contentType); httpPost.addHeader("X-Upload-Content-Length", byteSize); httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8"); postResponse = httpClient.execute(httpPost); int postCode = postResponse.getStatusLine().getStatusCode(); if (postCode == HttpStatus.SC_OK) { logger.info("Upload session has been successfully created"); try { location = postResponse.getLastHeader("Location").getValue(); } catch (Exception ex) { logger.warn("Exception when uploading file to Google Storage", ex); return DEFAULT_CODE; } try { HttpPut httpPut = new HttpPut(location); httpPut.setHeader(HttpHeaders.CONTENT_TYPE, contentType); FileEntity entity = new FileEntity(file); httpPut.setEntity(entity); putResponse = httpClient.execute(httpPut); int putCode = putResponse.getStatusLine().getStatusCode(); switch (putCode) { case HttpStatus.SC_OK: case HttpStatus.SC_CREATED: logger.info("File {} uploaded to Google Storage", mpId + "." + fileExtension); return putCode; default: logger.warn("Unable to upload file to Google Storage, returned code: {}.", putCode); return putCode; } } catch (Exception e) { logger.warn("Exception when uploading file to Google Storage", e); } } else { logger.warn("Uploading file to Google Storage failed and returned, status: {}.", postCode); return postCode; } } catch (Exception e) { logger.warn("Exception when uploading file to Google Storage", e); } finally { try { httpClient.close(); if (postResponse != null) { postResponse.close(); } if (putResponse != null) { putResponse.close(); } } catch (IOException e) { } } return DEFAULT_CODE; }  int startUpload(CloseableHttpClient httpClient, String bucket, String mpId,
          String fileExtension, File file, String byteSize, String contentType, String accessToken); void deleteGoogleStorageFile(CloseableHttpClient httpClient, String bucket, String objectName, String accessToken);  }
@Test public void startUploadTest() throws IOException { CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class); StatusLine status = EasyMock.createNiceMock(StatusLine.class); EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes(); EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_OK).anyTimes(); EasyMock.replay(response, status); Capture<HttpPost> capturedPost = Capture.newInstance(); EasyMock.expect(httpClient.execute(EasyMock.capture(capturedPost))).andReturn(response).anyTimes(); EasyMock.replay(httpClient); service.startUpload(httpClient, bucket, mpId, fileExtension, file, size, contentType, token); Assert.assertEquals("https: capturedPost.getValue().getURI().toString()); }