Java: Get The Status Of Letters And Postcards with GetStatus()

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 java.util.List;
 
import org.junit.Before;
import org.junit.Test;
 
import com.postalmethods.client.api.GetStatusResponse;
import com.postalmethods.client.api.SendLetterResponse;
import com.postalmethods.client.model.LetterStatus;
 
/**
 * Integration test that calls the GetStatus web method.
 * <p>
 * For details about PostalMethods.GetStatus see 
 * <a href="http://www.postalmethods.com/method/2009-02-26/GetStatus">the PostalMethods.GetStatus reference</a>
 */
public class GetStatusTest {
 
    private PostalMethodsClient client;
 
    private String 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 = Integer.toString(code);
 
        try {
            Thread.sleep(6000); // GetStatus calls should be called no more than
                                // once every six seconds.
        } catch (Exception e) {
        }
    }
 
    @Test
    public void testGetStatus() {
 
        System.out.println("Making call to GetStatus...");
 
        // GetStatus accepts an "ID" string which can include a single
        // item, multiple items, or a range of items.  Read the relevant documentation
        // for details (http://www.postalmethods.com/method/2009-02-26/GetStatus)
        // We will be looking for a single letter id, the letter created in
        // the setUp() method of this integration test.
        GetStatusResponse response = client.getStatus(letterId);
        System.out.println("Service call completed:\n" + response.getResult().toString());
        int code = response.getResult().getCode();
        assertEquals(-3000, code); // success
 
        List<LetterStatus> letterStatusList = response.getLetterStatusList();
        assertEquals(1, letterStatusList.size());
        System.out.println(letterStatusList.get(0).toString());
    }
}