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:com.ns.retailmgr.connector.gmaps.GMapConnectorTest.java

@Test(expected = RuntimeException.class)
public void test_getNearestShopDetails_Failure() {
    when(restTemplate.getForObject(anyString(), eq(GeoCodeLocInfo.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    gmapConnector.getNearestShopDetails("12.8124,77.69");
}

From source file:gateway.test.ServiceTests.java

/**
 * Test POST /service endpoint//  www .j  a v a2 s .  c  om
 */
@Test
public void testRegister() {
    // Mock
    Service service = new Service();
    service.setServiceId("123456");
    ServiceIdResponse mockResponse = new ServiceIdResponse(service.getServiceId());
    when(restTemplate.postForEntity(anyString(), any(), eq(ServiceIdResponse.class)))
            .thenReturn(new ResponseEntity<ServiceIdResponse>(mockResponse, HttpStatus.CREATED));

    // Test
    ResponseEntity<PiazzaResponse> entity = serviceController.registerService(service, user);
    ServiceIdResponse response = (ServiceIdResponse) entity.getBody();

    // Verify
    assertTrue(entity.getStatusCode().equals(HttpStatus.CREATED));
    assertTrue(response.data.getServiceId().equals("123456"));

    // Test Exception
    when(restTemplate.postForEntity(anyString(), any(), eq(ServiceIdResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    entity = serviceController.registerService(service, user);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
    assertTrue(entity.getBody() instanceof ErrorResponse);
}

From source file:gateway.test.JobTests.java

/**
 * Test GET /job/{jobId}//www .j a  va2 s. c om
 */
@Test
public void testGetStatus() {
    // Mock
    ResponseEntity<JobStatusResponse> mockResponse = new ResponseEntity<JobStatusResponse>(
            new JobStatusResponse(mockJob), HttpStatus.OK);
    when(restTemplate.getForEntity(anyString(), eq(JobStatusResponse.class))).thenReturn(mockResponse);

    // Test
    ResponseEntity<PiazzaResponse> entity = jobController.getJobStatus("123456", user);
    JobStatusResponse response = (JobStatusResponse) entity.getBody();

    // Verify
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));
    assertTrue(response.data.jobId.equals("123456"));
    assertTrue(response.data.status.equalsIgnoreCase(StatusUpdate.STATUS_RUNNING));
    assertTrue(response.data.progress.getPercentComplete().equals(50));
    assertTrue(response.data.createdBy.equals("Test User 2"));

    // Test Exception
    when(restTemplate.getForEntity(anyString(), eq(JobStatusResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));

    entity = jobController.getJobStatus("123456", user);
    assertTrue(entity.getBody() instanceof ErrorResponse);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
}

From source file:gateway.test.DataTests.java

/**
 * Test GET /data endpoint//w  ww.ja v  a 2 s  .  c  o m
 */
@Test
public void testGetData() {
    // When the Gateway asks Access for a List of Data, Mock that
    // response here.
    DataResourceListResponse mockResponse = new DataResourceListResponse();
    mockResponse.data = new ArrayList<DataResource>();
    mockResponse.getData().add(mockData);
    mockResponse.pagination = new Pagination(1, 0, 10, "test", "asc");
    when(restTemplate.getForEntity(anyString(), eq(DataResourceListResponse.class)))
            .thenReturn(new ResponseEntity<DataResourceListResponse>(mockResponse, HttpStatus.OK));

    // Get the data
    ResponseEntity<PiazzaResponse> entity = dataController.getData(null, null, 0, 10, null, "asc", null, user);
    PiazzaResponse response = entity.getBody();

    // Verify the results
    assertTrue(response instanceof DataResourceListResponse);
    DataResourceListResponse dataList = (DataResourceListResponse) response;
    assertTrue(dataList.getData().size() == 1);
    assertTrue(dataList.getPagination().getCount() == 1);
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));

    // Mock an Exception being thrown and handled.
    when(restTemplate.getForEntity(anyString(), eq(DataResourceListResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));

    // Get the data
    entity = dataController.getData(null, null, 0, 10, null, "asc", null, user);
    response = entity.getBody();

    // Verify that a proper exception was thrown.
    assertTrue(response instanceof ErrorResponse);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
}

From source file:gateway.test.ServiceTests.java

/**
 * Test GET /service/{serviceId}/*from   w w w  .  java 2s.  c  om*/
 */
@Test
public void testGetMetadata() {
    // Mock
    ServiceResponse mockResponse = new ServiceResponse(mockService);
    when(restTemplate.getForEntity(anyString(), eq(ServiceResponse.class)))
            .thenReturn(new ResponseEntity<ServiceResponse>(mockResponse, HttpStatus.OK));

    // Test
    ResponseEntity<PiazzaResponse> entity = serviceController.getService("123456", user);
    ServiceResponse response = (ServiceResponse) entity.getBody();

    // Verify
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));
    assertTrue(response.data.getServiceId().equalsIgnoreCase("123456"));
    assertTrue(response.data.getResourceMetadata().getName().equalsIgnoreCase("Test"));

    // Test Exception
    when(restTemplate.getForEntity(anyString(), eq(ServiceResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    entity = serviceController.getService("123456", user);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
    assertTrue(entity.getBody() instanceof ErrorResponse);
}

From source file:gateway.test.DeploymentTests.java

/**
 * Test GET /deployment//from w  w w.  j av  a2  s.c  o  m
 */
@Test
public void testGetList() {
    // Mock
    DeploymentListResponse mockResponse = new DeploymentListResponse();
    mockResponse.data = new ArrayList<Deployment>();
    mockResponse.getData().add(mockDeployment);
    mockResponse.pagination = new Pagination(1, 0, 10, "test", "asc");
    when(restTemplate.getForEntity(anyString(), eq(DeploymentListResponse.class)))
            .thenReturn(new ResponseEntity<DeploymentListResponse>(mockResponse, HttpStatus.OK));

    // Test
    ResponseEntity<PiazzaResponse> entity = deploymentController.getDeployment(null, 0, 10, "asc", "test",
            user);
    PiazzaResponse response = entity.getBody();

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

    // Test Exception
    when(restTemplate.getForEntity(anyString(), eq(DeploymentListResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    entity = deploymentController.getDeployment(null, 0, 10, "asc", "test", user);
    response = entity.getBody();
    assertTrue(response instanceof ErrorResponse);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
}

From source file:gateway.test.JobTests.java

/**
 * Test DELETE /job/{jobId}//from   w  w  w .  j a va  2 s.c  om
 */
@Test
public void testAbort() {
    // Mock
    ResponseEntity<SuccessResponse> mockEntity = new ResponseEntity<SuccessResponse>(
            new SuccessResponse("Deleted", "Job Manager"), HttpStatus.OK);
    when(restTemplate.postForEntity(anyString(), any(), eq(SuccessResponse.class))).thenReturn(mockEntity);

    // Test
    ResponseEntity<PiazzaResponse> entity = jobController.abortJob("123456", "Not Needed", user);

    // Verify
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));

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

    entity = jobController.abortJob("123456", "Not Needed", user);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
    assertTrue(entity.getBody() instanceof ErrorResponse);
}

From source file:gateway.test.JobTests.java

/**
 * Test PUT /job/{jobId}//  w  w w .j  a v a2s.  c  o m
 */
@Test
public void testRepeat() {
    // Mock
    ResponseEntity<JobResponse> mockEntity = new ResponseEntity<JobResponse>(new JobResponse("Updated"),
            HttpStatus.OK);
    when(restTemplate.postForEntity(anyString(), any(), eq(JobResponse.class))).thenReturn(mockEntity);

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

    // Verify
    assertTrue(entity.getStatusCode().equals(HttpStatus.CREATED));

    // Test Exception
    when(restTemplate.postForEntity(anyString(), any(), eq(JobResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));

    entity = jobController.repeatJob("123456", user);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
    assertTrue(entity.getBody() instanceof ErrorResponse);
}

From source file:gateway.test.ServiceTests.java

/**
 * Test DELETE /service/{serviceId}//from w ww .  j av a2 s  .  c o m
 */
@Test
public void testDelete() {
    // Mock
    when(restTemplate.exchange(anyString(), eq(HttpMethod.DELETE), eq(null), eq(SuccessResponse.class)))
            .thenReturn(new ResponseEntity<SuccessResponse>(
                    new SuccessResponse("Deleted", "Service Controller"), HttpStatus.OK));

    // Test
    ResponseEntity<PiazzaResponse> entity = serviceController.deleteService("123456", false, user);
    SuccessResponse response = (SuccessResponse) entity.getBody();

    // Verify
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));
    assertTrue(response.data.getMessage().contains("Deleted"));

    // Test Exception
    when(restTemplate.exchange(anyString(), eq(HttpMethod.DELETE), eq(null), eq(SuccessResponse.class)))
            .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));
    entity = serviceController.deleteService("123456", false, user);
    assertTrue(entity.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR));
    assertTrue(entity.getBody() instanceof ErrorResponse);
}

From source file:gateway.test.DeploymentTests.java

/**
 * Test GET /deployment/{deploymentId}/*from  w  w  w  . j av a2  s.  co m*/
 */
@Test
public void testGetMetadata() {
    // Mock the Response
    DeploymentResponse mockResponse = new DeploymentResponse(mockDeployment, "Now");
    when(restTemplate.getForEntity(anyString(), eq(DeploymentResponse.class)))
            .thenReturn(new ResponseEntity<DeploymentResponse>(mockResponse, HttpStatus.OK));

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

    // Verify
    assertTrue(response instanceof ErrorResponse == false);
    assertTrue(((DeploymentResponse) response).data.getDeployment().getDeploymentId()
            .equalsIgnoreCase(mockDeployment.getDeploymentId()));
    assertTrue(entity.getStatusCode().equals(HttpStatus.OK));

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