@Override public BatchJobUploadResponse uploadIncrementalBatchJobOperations( Iterable<? extends Operation> operations, boolean isLastRequest, BatchJobUploadStatus batchJobUploadStatus) throws BatchJobException { BatchJobMutateRequest request = new BatchJobMutateRequest(); request.addOperations(operations); return uploader.uploadIncrementalBatchJobOperations( request, isLastRequest, batchJobUploadStatus); }
@Test public void testTrimStartEndElements_notFirst_isLast() { List<List<String>> testData = Lists.newArrayList(); testData.add(Lists.newArrayList("<mutate><foo></foo></mutate>", "<foo></foo></mutate>")); testData.add(Lists.newArrayList( "<ns1:mutate xmlns:ns1=\"https://adwords.google.com/api/adwords/cm/v209912\"><foo></foo>" + "</ns1:mutate>", "<foo></foo></ns1:mutate>")); testData.add(Lists.newArrayList("<mutate></mutate>", "</mutate>")); for (List<String> testPair : testData) { String requestXml = testPair.get(0); String postProcessed = uploader.trimStartEndElements(requestXml, false, true); assertEquals("Trimmed result should exclude starting mutate if !isFirst and isLast", testPair.get(1), postProcessed); } }
URI uploadUri = initiateResumableUpload(batchJobUploadStatus.getResumableUploadUri()); effectiveStatus = new BatchJobUploadStatus(0, uploadUri); } else { request, effectiveStatus.getTotalContentLength() == 0, isLastRequest); try { content = postProcessContent( content, effectiveStatus.getTotalContentLength() == 0L, isLastRequest); } catch (IOException e) { httpTransport.createRequestFactory( req -> { HttpHeaders headers = createHttpHeaders(); headers.setContentLength(contentLength); headers.setContentRange( constructContentRangeHeaderValue( contentLength, isLastRequest, effectiveStatus)); req.setHeaders(headers);
/** * Tests that IOExceptions from executing an upload request are propagated properly. */ @SuppressWarnings("rawtypes") @Test public void testUploadBatchJobOperations_ioException_fails() throws Exception { final IOException ioException = new IOException("mock IO exception"); MockLowLevelHttpRequest lowLevelHttpRequest = new MockLowLevelHttpRequest(){ @Override public LowLevelHttpResponse execute() throws IOException { throw ioException; } }; when(uploadBodyProvider.getHttpContent(request, true, true)) .thenReturn(new ByteArrayContent(null, "foo".getBytes(UTF_8))); MockHttpTransport transport = new MockHttpTransport.Builder() .setLowLevelHttpRequest(lowLevelHttpRequest).build(); uploader = new BatchJobUploader(adWordsSession, transport, batchJobLogger); BatchJobUploadStatus uploadStatus = new BatchJobUploadStatus(0, URI.create("http://www.example.com")); thrown.expect(BatchJobException.class); thrown.expectCause(Matchers.sameInstance(ioException)); uploader.uploadIncrementalBatchJobOperations(request, true, uploadStatus); }
@Test public void testConstructContentRangeHeaderValue_notLast_nonZeroLength_zeroPrevious() { BatchJobUploadStatus status = new BatchJobUploadStatus(0, null); long requestLength = 100; String expectedContentRange = "bytes 0-99/*"; assertEquals( expectedContentRange, uploader.constructContentRangeHeaderValue(requestLength, false, status)); }
httpTransport.createRequestFactory( req -> { HttpHeaders headers = createHttpHeaders(); headers.setContentLength(0L); headers.set("x-goog-resumable", "start");
@SuppressWarnings("rawtypes") @Before public void setUp() { MockitoAnnotations.initMocks(this); mockHttpServer = new MockHttpServer(); uploader = new BatchJobUploader(adWordsSession, mockHttpServer.getHttpTransport(), batchJobLogger); when(request.createBatchJobUploadBodyProvider()).thenReturn(uploadBodyProvider); }
URI uploadUri = initiateResumableUpload(batchJobUploadStatus.getResumableUploadUri()); effectiveStatus = new BatchJobUploadStatus(0, uploadUri); } else { request, effectiveStatus.getTotalContentLength() == 0, isLastRequest); try { content = postProcessContent( content, effectiveStatus.getTotalContentLength() == 0L, isLastRequest); } catch (IOException e) { httpTransport.createRequestFactory( req -> { HttpHeaders headers = createHttpHeaders(); headers.setContentLength(contentLength); headers.setContentRange( constructContentRangeHeaderValue( contentLength, isLastRequest, effectiveStatus)); req.setHeaders(headers);
/** * Tests that IOExceptions from initiating an upload are propagated properly. */ @SuppressWarnings("rawtypes") @Test public void testUploadBatchJobOperations_initiateFails_fails() throws Exception { final IOException ioException = new IOException("mock IO exception"); MockLowLevelHttpRequest lowLevelHttpRequest = new MockLowLevelHttpRequest(){ @Override public LowLevelHttpResponse execute() throws IOException { throw ioException; } }; when(uploadBodyProvider.getHttpContent(request, true, true)) .thenReturn(new ByteArrayContent(null, "foo".getBytes(UTF_8))); MockHttpTransport transport = new MockHttpTransport.Builder() .setLowLevelHttpRequest(lowLevelHttpRequest).build(); uploader = new BatchJobUploader(adWordsSession, transport, batchJobLogger); thrown.expect(BatchJobException.class); thrown.expectCause(Matchers.sameInstance(ioException)); thrown.expectMessage("initiate upload"); uploader.uploadIncrementalBatchJobOperations(request, true, new BatchJobUploadStatus( 0, URI.create("http://www.example.com"))); }
@Test public void testConstructContentRangeHeaderValue_isLast_nonZeroLength_zeroPrevious() { BatchJobUploadStatus status = new BatchJobUploadStatus(0, null); long requestLength = 100; String expectedContentRange = "bytes 0-99/100"; assertEquals( expectedContentRange, uploader.constructContentRangeHeaderValue(requestLength, true, status)); }
httpTransport.createRequestFactory( req -> { HttpHeaders headers = createHttpHeaders(); headers.setContentLength(0L); headers.set("x-goog-resumable", "start");
@Override public BatchJobUploadResponse uploadIncrementalBatchJobOperations( Iterable<? extends Operation> operations, boolean isLastRequest, BatchJobUploadStatus batchJobUploadStatus) throws BatchJobException { BatchJobMutateRequest request = new BatchJobMutateRequest(); request.addOperations(operations); return uploader.uploadIncrementalBatchJobOperations( request, isLastRequest, batchJobUploadStatus); }
@Test public void testTrimStartEndElements_isFirst_notLast() { List<List<String>> testData = Lists.newArrayList(); testData.add(Lists.newArrayList("<mutate><foo></foo></mutate>", "<mutate><foo></foo>")); testData.add(Lists.newArrayList( "<ns1:mutate xmlns:ns1=\"https://adwords.google.com/api/adwords/cm/v209912\"><foo></foo>" + "</ns1:mutate>", "<ns1:mutate xmlns:ns1=\"https://adwords.google.com/api/adwords/cm/v209912\"><foo></foo>")); testData.add(Lists.newArrayList("<mutate></mutate>", "<mutate>")); for (List<String> testPair : testData) { String requestXml = testPair.get(0); String postProcessed = uploader.trimStartEndElements(requestXml, true, false); assertEquals("Trimmed result should exclude ending mutate if isFirst and !isLast", testPair.get(1), postProcessed); } }
@Test public void testConstructContentRangeHeaderValue_notLast_nonZeroLength_nonZeroPrevious() { BatchJobUploadStatus status = new BatchJobUploadStatus(100, null); long requestLength = 100; String expectedContentRange = "bytes 100-199/*"; assertEquals( expectedContentRange, uploader.constructContentRangeHeaderValue(requestLength, false, status)); }
@Override public BatchJobUploadResponse uploadIncrementalBatchJobOperations( Iterable<? extends Operation> operations, boolean isLastRequest, BatchJobUploadStatus batchJobUploadStatus) throws BatchJobException { BatchJobMutateRequest request = new BatchJobMutateRequest(); request.addOperations(operations); return uploader.uploadIncrementalBatchJobOperations( request, isLastRequest, batchJobUploadStatus); }
@Test public void testTrimStartEndElements_isFirst_isLast() { List<String> testData = Lists.newArrayList(); testData.add("<mutate><foo></foo></mutate>"); testData.add( "<ns1:mutate xmlns:ns1=\"https://adwords.google.com/api/adwords/cm/v209912\"><foo></foo>" + "</ns1:mutate>"); testData.add("<mutate></mutate>"); for (String requestXml : testData) { String postProcessed = uploader.trimStartEndElements(requestXml, true, true); assertEquals( "Trimmed result should not change if isFirst and isLast", requestXml, postProcessed); } }
@Test public void testConstructContentRangeHeaderValue_isLast_nonZeroLength_nonZeroPrevious() { BatchJobUploadStatus status = new BatchJobUploadStatus(100, null); long requestLength = 100; String expectedContentRange = "bytes 100-199/200"; assertEquals( expectedContentRange, uploader.constructContentRangeHeaderValue(requestLength, true, status)); }
@Override public BatchJobUploadResponse uploadIncrementalBatchJobOperations( Iterable<? extends Operation> operations, boolean isLastRequest, BatchJobUploadStatus batchJobUploadStatus) throws BatchJobException { BatchJobMutateRequest request = new BatchJobMutateRequest(); request.addOperations(operations); return uploader.uploadIncrementalBatchJobOperations( request, isLastRequest, batchJobUploadStatus); }
@Test public void testTrimStartEndElements_notFirst_notLast() { List<List<String>> testData = Lists.newArrayList(); testData.add(Lists.newArrayList("<mutate><foo></foo></mutate>", "<foo></foo>")); testData.add(Lists.newArrayList( "<ns1:mutate xmlns:ns1=\"https://adwords.google.com/api/adwords/cm/v209912\"><foo></foo>" + "</ns1:mutate>", "<foo></foo>")); testData.add(Lists.newArrayList("<mutate></mutate>", "")); for (List<String> testPair : testData) { String requestXml = testPair.get(0); String postProcessed = uploader.trimStartEndElements(requestXml, false, false); assertEquals( "Trimmed result should exclude starting and ending mutate if !isFirst and !isLast", testPair.get(1), postProcessed); } }
@Test public void testConstructContentRangeHeaderValue_notLast_zeroLength() { BatchJobUploadStatus status = new BatchJobUploadStatus(0, null); thrown.expect(IllegalArgumentException.class); uploader.constructContentRangeHeaderValue(0, false, status); }