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());
}
}
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());
}
}
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");
}
}
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);
}
}
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());
}
}
Aggregations