package org.onap.holmes.common.utils;
-import org.eclipse.jetty.http.HttpStatus;
import org.onap.holmes.common.exception.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private Map<String, Object> parameters = new HashMap();
private List<String> paths = new ArrayList();
+ public static JerseyClient newInstance() {
+ return new JerseyClient();
+ }
+
+ public static JerseyClient newInstance(long timeout) {
+ return new JerseyClient(timeout);
+ }
- public JerseyClient() {
+ private JerseyClient() {
this(DEFAULT_TIMEOUT);
}
- public JerseyClient(long timeout) {
+ private JerseyClient(long timeout) {
this.client = ClientBuilder.newBuilder()
.connectTimeout(timeout, TimeUnit.MILLISECONDS)
.readTimeout(timeout, TimeUnit.MILLISECONDS)
}
private boolean isSuccessful(Response response) {
- int status = response.getStatus();
- if (!HttpStatus.isSuccess(status)) {
- throw new HttpException(status, String.format("Failed to get response from the server. Info: %s",
+ if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
+ throw new HttpException(response.getStatus(), String.format("Failed to get response from the server. Info: %s",
response.readEntity(String.class)));
}
return true;
public class JerseyClientTest {
private final String ERROR_MSG = "Status code: <404>. Message: Failed to get response from the server. Info: Error";
- private JerseyClient jerseyClient = new JerseyClient();
+ private JerseyClient jerseyClient = JerseyClient.newInstance();
private String url = "http://www.onap.org/holmes/test";
+
@Rule
private ExpectedException exception = ExpectedException.none();
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse);
EasyMock.expect(mockedResponse.getStatus()).andReturn(404);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.NOT_FOUND);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse);
EasyMock.expect(mockedResponse.getStatus()).andReturn(404);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.NOT_FOUND);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse);
EasyMock.expect(mockedResponse.getStatus()).andReturn(404);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.NOT_FOUND);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean));
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse);
- EasyMock.expect(mockedResponse.getStatus()).andReturn(200);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.OK);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);
EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder);
EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse);
EasyMock.expect(mockedResponse.getStatus()).andReturn(404);
+ EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.NOT_FOUND);
EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error");
WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient);