use of com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse 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.model.response.BaseMandrillStringResponse in project Java-Mandrill-Wrapper by cribbstechnologies.
the class MandrillUsersRequestTest method testPerformPing.
@Test
public void testPerformPing() throws RequestFailedException {
usersRequest.setRequest(request);
BaseMandrillStringResponse response = new BaseMandrillStringResponse();
Mockito.when(request.postRequest(bmr, ServiceMethods.Users.PING, null)).thenReturn(response);
usersRequest.performPing(bmr);
Mockito.verify(request).postRequest(bmr, ServiceMethods.Users.PING, null);
}
use of com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse 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);
}
}
Aggregations