Search in sources :

Example 1 with CustomDifferenceEvaluator

use of com.google.api.ads.common.lib.soap.testing.CustomDifferenceEvaluator in project googleads-java-lib by googleads.

the class JaxWsSerializationTest method testSerializeBatchJobMutateRequest.

@SuppressWarnings("cast")
@Test
public void testSerializeBatchJobMutateRequest() throws IOException, SAXException {
    BatchJobMutateRequest request = new BatchJobMutateRequest();
    CampaignOperation campaignOp = new CampaignOperation();
    campaignOp.setOperator(Operator.ADD);
    Campaign campaign = new Campaign();
    campaign.setId(-1L);
    campaign.setName("Test campaign");
    campaign.setAdvertisingChannelType(AdvertisingChannelType.SEARCH);
    campaignOp.setOperand(campaign);
    request.addOperation(campaignOp);
    AdGroupOperation adGroupOp = new AdGroupOperation();
    adGroupOp.setOperator(Operator.ADD);
    AdGroup adGroup = new AdGroup();
    adGroup.setName("Test ad group");
    adGroup.setCampaignId(campaign.getId());
    adGroupOp.setOperand(adGroup);
    request.addOperation(adGroupOp);
    JaxBSerializer<BatchJobMutateRequest> serializer = new JaxBSerializer<>(BatchJobMutateRequest.class, new QName("https://adwords.google.com/api/adwords/cm/v201809", "mutate"));
    String serializedRequest = serializer.serialize(request);
    assertNotNull("Null serialized request returned", serializedRequest);
    String expectedSerializedRequest = CharStreams.toString(new InputStreamReader(JaxWsSerializationTest.class.getResourceAsStream("resources/BatchJobMutate.request.xml"), UTF_8));
    // Perform XML diffs using the custom difference listener that properly handles namespaces
    // and attributes.
    Diff diff = DiffBuilder.compare(expectedSerializedRequest).withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, new CustomDifferenceEvaluator())).withTest(serializedRequest).checkForSimilar().build();
    assertFalse("Serialized request does not match expected value", diff.hasDifferences());
    // Serialize again, this time without the XML declaration, and confirm the serialized XML
    // matches the expected string.
    serializedRequest = serializer.serialize(request, false);
    expectedSerializedRequest = expectedSerializedRequest.replaceFirst("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
    diff = DiffBuilder.compare(expectedSerializedRequest).normalizeWhitespace().withDifferenceEvaluator(new CustomDifferenceEvaluator()).withTest(serializedRequest).checkForSimilar().build();
    assertFalse("Serialized request with XML declaration excluded does not match expected value", diff.hasDifferences());
}
Also used : CustomDifferenceEvaluator(com.google.api.ads.common.lib.soap.testing.CustomDifferenceEvaluator) BatchJobMutateRequest(com.google.api.ads.adwords.jaxws.utils.v201809.batchjob.BatchJobMutateRequest) Campaign(com.google.api.ads.adwords.jaxws.v201809.cm.Campaign) InputStreamReader(java.io.InputStreamReader) Diff(org.xmlunit.diff.Diff) QName(javax.xml.namespace.QName) CampaignOperation(com.google.api.ads.adwords.jaxws.v201809.cm.CampaignOperation) JaxBSerializer(com.google.api.ads.common.lib.soap.jaxb.JaxBSerializer) AdGroup(com.google.api.ads.adwords.jaxws.v201809.cm.AdGroup) AdGroupOperation(com.google.api.ads.adwords.jaxws.v201809.cm.AdGroupOperation) Test(org.junit.Test)

Example 2 with CustomDifferenceEvaluator

use of com.google.api.ads.common.lib.soap.testing.CustomDifferenceEvaluator in project googleads-java-lib by googleads.

the class BatchJobUploadBodyProviderTest method testValidOperations.

@Test
public void testValidOperations() throws BatchJobException, IOException, SAXException {
    RequestT request = createMutateRequest();
    addBudgetOperation(request, -1L, "Test budget", 50000000L, "STANDARD");
    addCampaignOperation(request, -2L, "Test campaign #1", "PAUSED", "SEARCH", -1L, "MANUAL_CPC", false);
    addCampaignOperation(request, -3L, "Test campaign #2", "PAUSED", "SEARCH", -1L, "MANUAL_CPC", false);
    addCampaignNegativeKeywordOperation(request, -2L, "venus", "BROAD");
    addCampaignNegativeKeywordOperation(request, -3L, "venus", "BROAD");
    ByteArrayContent httpContent = request.createBatchJobUploadBodyProvider().getHttpContent(request, true, true);
    String actualRequestXml = Streams.readAll(httpContent.getInputStream(), Charset.forName(UTF_8));
    actualRequestXml = SoapRequestXmlProvider.normalizeXmlForComparison(actualRequestXml, getApiVersion());
    String expectedRequestXml = SoapRequestXmlProvider.getTestBatchUploadRequest(getApiVersion());
    // Perform an XML diff using the custom difference listener that properly handles namespaces
    // and attributes.
    Diff diff = DiffBuilder.compare(expectedRequestXml).normalizeWhitespace().withDifferenceEvaluator(new CustomDifferenceEvaluator()).withTest(actualRequestXml).checkForSimilar().build();
    assertFalse("XML similar " + diff, diff.hasDifferences());
}
Also used : CustomDifferenceEvaluator(com.google.api.ads.common.lib.soap.testing.CustomDifferenceEvaluator) Diff(org.xmlunit.diff.Diff) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Test(org.junit.Test)

Aggregations

CustomDifferenceEvaluator (com.google.api.ads.common.lib.soap.testing.CustomDifferenceEvaluator)2 Test (org.junit.Test)2 Diff (org.xmlunit.diff.Diff)2 BatchJobMutateRequest (com.google.api.ads.adwords.jaxws.utils.v201809.batchjob.BatchJobMutateRequest)1 AdGroup (com.google.api.ads.adwords.jaxws.v201809.cm.AdGroup)1 AdGroupOperation (com.google.api.ads.adwords.jaxws.v201809.cm.AdGroupOperation)1 Campaign (com.google.api.ads.adwords.jaxws.v201809.cm.Campaign)1 CampaignOperation (com.google.api.ads.adwords.jaxws.v201809.cm.CampaignOperation)1 JaxBSerializer (com.google.api.ads.common.lib.soap.jaxb.JaxBSerializer)1 ByteArrayContent (com.google.api.client.http.ByteArrayContent)1 InputStreamReader (java.io.InputStreamReader)1 QName (javax.xml.namespace.QName)1