Search in sources :

Example 6 with RequestFailedException

use of com.cribbstechnologies.clients.mandrill.exception.RequestFailedException in project Java-Mandrill-Wrapper by cribbstechnologies.

the class UsersTest method testPing.

@Test
public void testPing() {
    BaseMandrillRequest baseRequest = new BaseMandrillRequest();
    try {
        BaseMandrillStringResponse response = usersRequest.performPing(baseRequest);
        assertEquals("\"PONG!\"", response.getResponse());
    } catch (RequestFailedException e) {
        fail(e.getMessage());
    }
}
Also used : BaseMandrillStringResponse(com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse) RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) BaseMandrillRequest(com.cribbstechnologies.clients.mandrill.model.BaseMandrillRequest) Test(org.junit.Test)

Example 7 with RequestFailedException

use of com.cribbstechnologies.clients.mandrill.exception.RequestFailedException in project Java-Mandrill-Wrapper by cribbstechnologies.

the class MandrillRESTRequestTest method testPostRequest.

@Test
public void testPostRequest() throws ClientProtocolException, IOException {
    this.request = new MandrillRESTRequest();
    this.request.setHttpClient(this.client);
    this.request.setConfig(this.config);
    this.request.setObjectMapper(new ObjectMapper());
    doThrow(new MalformedURLException("Mockito!")).when(this.client).execute(isA(HttpPost.class));
    try {
        this.request.postRequest(this.emptyBaseRequest, "test", null);
        fail("Exception not thrown");
    } catch (RequestFailedException e) {
        assertEquals("Malformed url", e.getMessage());
    }
    doThrow(new IOException("Mockito!")).when(this.client).execute(isA(HttpPost.class));
    try {
        this.request.postRequest(this.emptyBaseRequest, "test", null);
        fail("Exception not thrown");
    } catch (RequestFailedException e) {
        assertEquals("IOException", e.getMessage());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MalformedURLException(java.net.MalformedURLException) RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 8 with RequestFailedException

use of com.cribbstechnologies.clients.mandrill.exception.RequestFailedException in project Java-Mandrill-Wrapper by cribbstechnologies.

the class MandrillRESTRequestTest method testPostRequestNon200Response.

@Test
public void testPostRequestNon200Response() {
    try {
        this.request = new MandrillRESTRequest();
        this.request.setHttpClient(this.client);
        this.request.setConfig(this.config);
        this.request.setObjectMapper(this.mapper);
        doReturn("postData").when(this.mapper).writeValueAsString(this.emptyBaseRequest);
        doReturn(this.response).when(this.client).execute(isA(HttpPost.class));
        doReturn(this.manager).when(this.client).getConnectionManager();
        Mockito.when(this.response.getEntity()).thenReturn(this.entity);
        InputStream inputStream = IOUtils.toInputStream("INPUT");
        Mockito.when(this.entity.getContent()).thenReturn(inputStream);
        Mockito.when(this.response.getStatusLine()).thenReturn(this.statusLine);
        Mockito.when(this.statusLine.getStatusCode()).thenReturn(500);
        this.request.postRequest(this.emptyBaseRequest, "Foo", null);
    } catch (RequestFailedException rfe) {
        assertEquals("Failed : HTTP error code : 500 INPUT", rfe.getMessage());
    } catch (ClientProtocolException e) {
        fail("Mockito is a good mocking framework, this shouldn't happen");
    } catch (IOException e) {
        fail("Mockito is a good mocking framework, this shouldn't happen");
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) InputStream(java.io.InputStream) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) Test(org.junit.Test)

Example 9 with RequestFailedException

use of com.cribbstechnologies.clients.mandrill.exception.RequestFailedException in project Java-Mandrill-Wrapper by cribbstechnologies.

the class MandrillRESTRequest method performPostRequest.

private BaseMandrillResponse performPostRequest(BaseMandrillRequest request, String serviceMethod, Object responseClass, TypeReference reference) throws RequestFailedException {
    try {
        request.setKey(config.getApiKey());
        HttpPost postRequest = new HttpPost(config.getServiceUrl() + serviceMethod);
        String postData = getPostData(request);
        StringEntity input = new StringEntity(postData, "UTF-8");
        input.setContentType("application/json");
        postRequest.setEntity(input);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
        StringBuffer sb = new StringBuffer();
        String output;
        // System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            sb.append(output);
        // System.out.println(output);
        }
        String responseString = sb.toString();
        EntityUtils.consume(response.getEntity());
        if (response.getStatusLine().getStatusCode() != 200) {
            //throw new RequestFailedException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + " " + responseString);
            throw new RequestFailedException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + " " + responseString, objectMapper.readValue(responseString, MandrillError.class));
        }
        // for whatever reason the ping response isn't well-formed
        if (ServiceMethods.Users.PING.equals(serviceMethod) && responseString.indexOf("PONG!") > -1) {
            return new BaseMandrillStringResponse(responseString);
        }
        if (reference == null) {
            return convertResponseData(responseString, responseClass);
        } else {
            return convertAnonymousListResponseData(responseString, responseClass, reference);
        }
    } catch (MalformedURLException mURLE) {
        throw new RequestFailedException("Malformed url", mURLE);
    } catch (JsonGenerationException jge) {
        throw new RequestFailedException("Json Generation Exception", jge);
    } catch (JsonMappingException jme) {
        throw new RequestFailedException("Json Mapping Exception", jme);
    } catch (IOException ioe) {
        throw new RequestFailedException("IOException", ioe);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) BaseMandrillStringResponse(com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse) RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) MandrillError(com.cribbstechnologies.clients.mandrill.model.MandrillError) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) BufferedReader(java.io.BufferedReader) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException)

Example 10 with RequestFailedException

use of com.cribbstechnologies.clients.mandrill.exception.RequestFailedException in project Java-Mandrill-Wrapper by cribbstechnologies.

the class TemplatesTest method testGetTemplateInfo.

@Test
public void testGetTemplateInfo() {
    MandrillRequestWithName request = new MandrillRequestWithName();
    request.setName("template1");
    try {
        templatesRequest.getTemplateInfo(request);
    } catch (RequestFailedException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : MandrillRequestWithName(com.cribbstechnologies.clients.mandrill.model.MandrillRequestWithName) RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) Test(org.junit.Test)

Aggregations

RequestFailedException (com.cribbstechnologies.clients.mandrill.exception.RequestFailedException)17 Test (org.junit.Test)16 BaseMandrillRequest (com.cribbstechnologies.clients.mandrill.model.BaseMandrillRequest)3 MandrillRequestWithName (com.cribbstechnologies.clients.mandrill.model.MandrillRequestWithName)3 IOException (java.io.IOException)3 HttpPost (org.apache.http.client.methods.HttpPost)3 MandrillRecipient (com.cribbstechnologies.clients.mandrill.model.MandrillRecipient)2 MandrillRequestWithCode (com.cribbstechnologies.clients.mandrill.model.MandrillRequestWithCode)2 BaseMandrillStringResponse (com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse)2 UrlListResponse (com.cribbstechnologies.clients.mandrill.model.response.urls.UrlListResponse)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2 MandrillError (com.cribbstechnologies.clients.mandrill.model.MandrillError)1 MandrillHtmlMessage (com.cribbstechnologies.clients.mandrill.model.MandrillHtmlMessage)1 MandrillMessage (com.cribbstechnologies.clients.mandrill.model.MandrillMessage)1 MandrillMessageRequest (com.cribbstechnologies.clients.mandrill.model.MandrillMessageRequest)1 MandrillRequestWithDomain (com.cribbstechnologies.clients.mandrill.model.MandrillRequestWithDomain)1 MandrillRequestWithEmail (com.cribbstechnologies.clients.mandrill.model.MandrillRequestWithEmail)1