Java: Get Letters And Postcards' Delivery Details with GetDetails()

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.GetDetailsResponse;
import com.postalmethods.client.api.SendLetterResponse;
import com.postalmethods.client.model.LetterDetails;
 
/**
 * Integration test that calls the GetDetails web method.
 * <p>
 * For details about PostalMethods.GetDetails see
 * <a href="http://www.postalmethods.com/method/2009-02-26/GetDetails">the PostalMethods.GetDetails reference</a>
 */
public class GetDetailsTest {
 
    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 1", TestConstants.SAMPLE_LETTER_FILENAME);
        int code = sendResponse.getResult().getCode();
        assertTrue(code > 0);
        this.letterId = Integer.toString(code);
    }
 
    @Test
    public void testGetDetails() {
 
        System.out.println("Making call to GetDetails...");
 
        // GetDetails 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/GetDetails)
        // We will be looking for a single letter id, the letter created in
        // the setUp() method of this integration test.
        GetDetailsResponse response = client.getDetails(letterId);
        System.out.println("Service call completed:\n" + response.getResult().toString());
 
        int code = response.getResult().getCode();
        assertEquals(-3000, code); // success
 
        List<LetterDetails> letterDetailsList = response.getLetterDetailsList();
        assertEquals(1, letterDetailsList.size());
        System.out.println(letterDetailsList.get(0).toString());
    }
}