Example usage for org.springframework.web.client HttpServerErrorException HttpServerErrorException

List of usage examples for org.springframework.web.client HttpServerErrorException HttpServerErrorException

Introduction

In this page you can find the example usage for org.springframework.web.client HttpServerErrorException HttpServerErrorException.

Prototype

public HttpServerErrorException(HttpStatus statusCode) 

Source Link

Document

Constructor with a status code only.

Usage

From source file:gateway.test.ServiceTests.java

/**
 * Test PUT /service/{serviceId}/*from   www .j a  va2s .co m*/
 */
@Test
public void testUpdateMetadata() {
    // Mock
    HttpEntity<Service> request = new HttpEntity<Service>(null, null);
    doReturn(new ResponseEntity<PiazzaResponse>(new SuccessResponse("Yes", "Gateway"), HttpStatus.OK))
            .when(restTemplate).exchange(any(String.class), eq(HttpMethod.PUT), any(request.getClass()),
                    eq(SuccessResponse.class));

    // Test
    ResponseEntity<PiazzaResponse> entity = serviceController.updateService("123456", mockService, user);

    // Verify
    assertTrue(entity.getBody() instanceof SuccessResponse);

    // Test Exception
    doThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR)).when(restTemplate).exchange(
            any(String.class), eq(HttpMethod.PUT), any(request.getClass()), eq(SuccessResponse.class));
    ResponseEntity<PiazzaResponse> entity2 = serviceController.updateService("123456", mockService, user);
    assertTrue(entity2.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
    assertTrue(entity2.getBody() instanceof ErrorResponse);
}

From source file:gateway.test.DeploymentTests.java

/**
 * Test DELETE /deployment/{deploymentId}
 *///  w w  w .  ja v  a2 s . c  o  m
@Test
public void testDeleteDeployment() {
    // Mock the Response
    when(restTemplate.exchange(anyString(), any(), any(), eq(SuccessResponse.class))).thenReturn(
            new ResponseEntity<SuccessResponse>(new SuccessResponse("Deleted", "Access"), HttpStatus.OK));

    // Test
    ResponseEntity<PiazzaResponse> entity = deploymentController.deleteDeployment("123456", user);

    // Verify
    assertTrue(entity.getBody() instanceof SuccessResponse);

    // Test an Exception
    when(restTemplate.exchange(anyString(), any(), any(), eq(SuccessResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    entity = deploymentController.deleteDeployment("123456", user);
    PiazzaResponse response = entity.getBody();
    assertTrue(response instanceof ErrorResponse);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
}

From source file:gateway.test.ServiceTests.java

/**
 * Test GET /service//from   w  w  w. j a  v a  2 s. c o  m
 */
@Test
public void testGetServices() {
    // Mock
    ServiceListResponse mockResponse = new ServiceListResponse();
    mockResponse.data = new ArrayList<Service>();
    mockResponse.getData().add(mockService);
    mockResponse.pagination = new Pagination(1, 0, 10, "test", "asc");
    when(restTemplate.getForEntity(anyString(), eq(ServiceListResponse.class)))
            .thenReturn(new ResponseEntity<ServiceListResponse>(mockResponse, HttpStatus.OK));

    // Test
    ResponseEntity<PiazzaResponse> entity = serviceController.getServices(null, 0, 10, null, null, null, user);
    PiazzaResponse response = entity.getBody();

    // Verify
    assertTrue(response instanceof ServiceListResponse);
    ServiceListResponse serviceList = (ServiceListResponse) response;
    assertTrue(serviceList.getData().size() == 1);
    assertTrue(serviceList.getPagination().getCount() == 1);
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));

    // Test Exception
    when(restTemplate.getForEntity(anyString(), eq(ServiceListResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    entity = serviceController.getServices(null, 0, 10, null, null, null, user);
    response = entity.getBody();
    assertTrue(response instanceof ErrorResponse);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
}

From source file:gateway.test.DataTests.java

/**
 * Test GET /data/{dataId}//from w ww.j  a  va2  s .  com
 */
@Test
public void testGetDataItem() {
    // Mock the Response
    DataResourceResponse mockResponse = new DataResourceResponse(mockData);
    when(restTemplate.getForEntity(anyString(), eq(DataResourceResponse.class)))
            .thenReturn(new ResponseEntity<DataResourceResponse>(mockResponse, HttpStatus.OK));

    // Test
    ResponseEntity<PiazzaResponse> entity = dataController.getMetadata("123456", user);
    PiazzaResponse response = entity.getBody();

    // Verify
    assertTrue(response instanceof ErrorResponse == false);
    assertTrue(((DataResourceResponse) response).data.getDataId().equalsIgnoreCase(mockData.getDataId()));
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));

    // Test an Exception
    when(restTemplate.getForEntity(anyString(), eq(DataResourceResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    entity = dataController.getMetadata("123456", user);
    response = entity.getBody();
    assertTrue(response instanceof ErrorResponse);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
}

From source file:gateway.test.DataTests.java

/**
 * Test DELETE /data/{dataId}/*from w w w  .j av  a 2  s  . c om*/
 */
@Test
public void testDeleteData() {
    // Mock the Response
    when(restTemplate.exchange(anyString(), any(), any(), eq(SuccessResponse.class))).thenReturn(
            new ResponseEntity<SuccessResponse>(new SuccessResponse("Deleted", "Ingest"), HttpStatus.OK));

    // Test
    ResponseEntity<PiazzaResponse> entity = dataController.deleteData("123456", user);
    PiazzaResponse response = entity.getBody();

    // Verify
    assertTrue(response instanceof ErrorResponse == false);
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));

    // Test an Exception
    when(restTemplate.exchange(anyString(), any(), any(), eq(SuccessResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));

    entity = dataController.deleteData("123456", user);
    response = entity.getBody();
    assertTrue(response instanceof ErrorResponse);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
}

From source file:gateway.test.DataTests.java

/**
 * Test PUT /data/{dataId}//from  ww  w.ja va 2s . co  m
 */
@Test
public void testUpdateData() {
    // Mock
    when(restTemplate.postForEntity(anyString(), any(), eq(SuccessResponse.class))).thenReturn(
            new ResponseEntity<SuccessResponse>(new SuccessResponse("Updated", "Ingest"), HttpStatus.OK));

    // Test
    ResponseEntity<PiazzaResponse> entity = dataController.updateMetadata("123456", mockData.getMetadata(),
            user);

    // Verify
    assertTrue(entity.getBody() instanceof SuccessResponse);

    // Test an Exception
    when(restTemplate.postForEntity(anyString(), any(), eq(SuccessResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    entity = dataController.updateMetadata("123456", mockData.getMetadata(), user);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
    assertTrue(entity.getBody() instanceof ErrorResponse);
}

From source file:org.springframework.web.socket.sockjs.client.AbstractXhrTransport.java

@Override
public String executeInfoRequest(URI infoUrl, @Nullable HttpHeaders headers) {
    if (logger.isDebugEnabled()) {
        logger.debug("Executing SockJS Info request, url=" + infoUrl);
    }/*from   w w  w . j a  va2  s .  c  o  m*/
    HttpHeaders infoRequestHeaders = new HttpHeaders();
    if (headers != null) {
        infoRequestHeaders.putAll(headers);
    }
    ResponseEntity<String> response = executeInfoRequestInternal(infoUrl, infoRequestHeaders);
    if (response.getStatusCode() != HttpStatus.OK) {
        if (logger.isErrorEnabled()) {
            logger.error("SockJS Info request (url=" + infoUrl + ") failed: " + response);
        }
        throw new HttpServerErrorException(response.getStatusCode());
    }
    if (logger.isTraceEnabled()) {
        logger.trace("SockJS Info request (url=" + infoUrl + ") response: " + response);
    }
    String result = response.getBody();
    return (result != null ? result : "");
}

From source file:org.springframework.web.socket.sockjs.client.AbstractXhrTransport.java

@Override
public void executeSendRequest(URI url, HttpHeaders headers, TextMessage message) {
    if (logger.isTraceEnabled()) {
        logger.trace("Starting XHR send, url=" + url);
    }//from   w w  w.  j a va 2s .com
    ResponseEntity<String> response = executeSendRequestInternal(url, headers, message);
    if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
        if (logger.isErrorEnabled()) {
            logger.error("XHR send request (url=" + url + ") failed: " + response);
        }
        throw new HttpServerErrorException(response.getStatusCode());
    }
    if (logger.isTraceEnabled()) {
        logger.trace("XHR send request (url=" + url + ") response: " + response);
    }
}

From source file:org.venice.beachfront.bfapi.services.OAuthServiceTests.java

@Test
public void testRequestAccessTokenServerError() {
    String mockAuthCode = "mock-auth-code-123";

    Mockito.when(this.restTemplate.exchange(Mockito.eq(this.oauthTokenUrl), Mockito.eq(HttpMethod.POST),
            Mockito.any(), Mockito.eq(AccessTokenResponseBody.class)))
            .then(new Answer<ResponseEntity<AccessTokenResponseBody>>() {
                @Override//  w  w w.  j  a  v a 2  s. com
                public ResponseEntity<AccessTokenResponseBody> answer(InvocationOnMock invocation) {
                    throw new HttpServerErrorException(HttpStatus.GATEWAY_TIMEOUT);
                }
            });

    try {
        oauthService.requestAccessToken(mockAuthCode);
        fail("request should not have succeeded");
    } catch (UserException ex) {
        assertEquals(HttpStatus.BAD_GATEWAY, ex.getRecommendedStatusCode());
        assertTrue(ex.getMessage().contains("" + HttpStatus.GATEWAY_TIMEOUT.value()));
    }
}

From source file:org.venice.beachfront.bfapi.services.OAuthServiceTests.java

@Test
public void testRequestOAuthProfileServerError() {
    String mockAccessToken = "mock-access-token-321";

    Mockito.when(this.restTemplate.exchange(Mockito.eq(this.oauthProfileUrl), Mockito.eq(HttpMethod.GET),
            Mockito.any(), Mockito.eq(String.class))).then(new Answer<ResponseEntity<String>>() {
                @Override//  w w w .ja v  a 2 s.com
                public ResponseEntity<String> answer(InvocationOnMock invocation) {
                    throw new HttpServerErrorException(HttpStatus.GATEWAY_TIMEOUT);
                }
            });

    try {
        oauthService.requestOAuthProfile(mockAccessToken);
        fail("request should not have succeeded");
    } catch (UserException ex) {
        assertEquals(HttpStatus.BAD_GATEWAY, ex.getRecommendedStatusCode());
        assertTrue(ex.getMessage().contains("" + HttpStatus.GATEWAY_TIMEOUT.value()));
    }
}