Java: Cancel Delivery Of A Letter with CancelDelivery()

Download
Download our Java samples from this page.
Find this sample in directory /test/com/postalmethods/client
package com.postalmethods.client;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
 
import org.junit.Before;
import org.junit.Test;
 
import com.postalmethods.client.api.CancelDeliveryResponse;
import com.postalmethods.client.api.SendLetterResponse;
 
/**
 * Integration test that calls the CancelDelivery web method. This method is 
 * documented 
 * <p>
 * For details about PostalMethods.CancelDelivery see
 * <a href="http://www.postalmethods.com/method/2009-02-26/CancelDelivery">the PostalMethods.CancelDelivery reference</a>
 */
public class CancelDeliveryTest {
 
    private PostalMethodsClient client;
 
    private int letterId;
 
    @Before
    public void setUp() {
 
        client = new PostalMethodsClientFactory().build();
 
        // Send a sample letter so we have a letterId to check status on.
        SendLetterResponse sendResponse = client.sendLetter("Junit test letter", TestConstants.SAMPLE_LETTER_FILENAME);
        int code = sendResponse.getResult().getCode();
        assertTrue(code > 0);
        this.letterId = code;
    }
 
    @Test
    public void testCancelDelivery() {
 
        System.out.println("Making call to CancelDelivery...");
        CancelDeliveryResponse response = client.cancelDelivery(letterId);
        int code = response.getResult().getCode();
        System.out.println("Service call completed:\n" + response.getResult().toString());
        assertEquals(-3000, code); // success
    }
}