Java: Get The Status Of A Letter with GetLetterStatusV2()

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.GetLetterStatusV2Response;
import com.postalmethods.client.api.SendLetterV2Response;
 
public class GetLetterStatusV2Test {
 
    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.
        SendLetterV2Response sendResponse = client.sendLetterV2("Junit test letter", TestConstants.SAMPLE_LETTER_FILENAME);
        int code = sendResponse.getResult().getCode();
        assertTrue(code > 0 );
        this.letterId = code;
 
        try {
            Thread.sleep(6000);  // GetLetterStatus calls should be called no more than once every six seconds.
        } catch(Exception e) {}
    }
 
    @Test
    public void testGetLetterStatus() {
 
        System.out.println("Making call to GetLetterStatusV2...");
        GetLetterStatusV2Response response = client.getLetterStatusV2(letterId);
        System.out.println("Service call completed:\n" + response.getResult().toString());
        int code = response.getResult().getCode();
        assertEquals( -3000, code ); // success
 
        System.out.println(response.getLetterStatus().toString());
    }
}