@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 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); } }
@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 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); } }
/** * Post-processes the request content to conform to the requirements of Google Cloud Storage. * * @param content the content produced by the {@link BatchJobUploadBodyProvider}. * @param isFirstRequest if this is the first request for the batch job. * @param isLastRequest if this is the last request for the batch job. */ private ByteArrayContent postProcessContent( ByteArrayContent content, boolean isFirstRequest, boolean isLastRequest) throws IOException { if (isFirstRequest && isLastRequest) { return content; } String serializedRequest = Streams.readAll(content.getInputStream(), UTF_8); serializedRequest = trimStartEndElements(serializedRequest, isFirstRequest, isLastRequest); // The request is part of a set of incremental uploads, so pad to the required content // length. This is not necessary if all operations for the job are being uploaded in a // single request. int numBytes = serializedRequest.getBytes(UTF_8).length; int remainder = numBytes % REQUIRED_CONTENT_LENGTH_INCREMENT; if (remainder > 0) { int pad = REQUIRED_CONTENT_LENGTH_INCREMENT - remainder; serializedRequest = Strings.padEnd(serializedRequest, numBytes + pad, ' '); } return new ByteArrayContent(content.getType(), serializedRequest.getBytes(UTF_8)); }
/** * Post-processes the request content to conform to the requirements of Google Cloud Storage. * * @param content the content produced by the {@link BatchJobUploadBodyProvider}. * @param isFirstRequest if this is the first request for the batch job. * @param isLastRequest if this is the last request for the batch job. */ private ByteArrayContent postProcessContent( ByteArrayContent content, boolean isFirstRequest, boolean isLastRequest) throws IOException { if (isFirstRequest && isLastRequest) { return content; } String serializedRequest = Streams.readAll(content.getInputStream(), UTF_8); serializedRequest = trimStartEndElements(serializedRequest, isFirstRequest, isLastRequest); // The request is part of a set of incremental uploads, so pad to the required content // length. This is not necessary if all operations for the job are being uploaded in a // single request. int numBytes = serializedRequest.getBytes(UTF_8).length; int remainder = numBytes % REQUIRED_CONTENT_LENGTH_INCREMENT; if (remainder > 0) { int pad = REQUIRED_CONTENT_LENGTH_INCREMENT - remainder; serializedRequest = Strings.padEnd(serializedRequest, numBytes + pad, ' '); } return new ByteArrayContent(content.getType(), serializedRequest.getBytes(UTF_8)); }
uploader.trimStartEndElements(requestXml, false, true); fail("Should have thrown an IllegalArgumentException for isLast and input: " + requestXml); } catch (IllegalArgumentException e) { uploader.trimStartEndElements(requestXml, true, false); fail("Should have thrown an IllegalArgumentException for isFirst and input: " + requestXml); } catch (IllegalArgumentException e) {