@Test
  public void testUnauthorized() throws Exception {
    MockWebServer server = mockWebServer();
    server.enqueue(
        new MockResponse().setResponseCode(401).setBody(payloadFromResource("/fault-401.html")));

    ProfitBricksApi pbApi = api(server.getUrl("/"));
    DataCenterApi api = pbApi.dataCenterApi();

    try {
      api.clearDataCenter("some-datacenter-id");
      fail("Request should have failed");
    } catch (Exception ex) {
      assertTrue(
          ex instanceof AuthorizationException, "Exception should be an AuthorizationException");
    } finally {
      pbApi.close();
      server.shutdown();
    }
  }
  @Test
  public void testBadRequest() throws Exception {
    MockWebServer server = mockWebServer();
    server.enqueue(
        new MockResponse().setResponseCode(500).setBody(payloadFromResource("/fault-400.xml")));

    ProfitBricksApi pbApi = api(server.getUrl("/"));
    DataCenterApi api = pbApi.dataCenterApi();

    try {
      api.createDataCenter(DataCenter.Request.creatingPayload("D@tacenter", Location.DE_FKB));
      fail("Request should have failed");
    } catch (Exception ex) {
      assertTrue(
          ex instanceof IllegalArgumentException,
          "Exception should be an IllegalArgumentException");
    } finally {
      pbApi.close();
      server.shutdown();
    }
  }
  @Test
  public void testNotFound() throws Exception {
    MockWebServer server = mockWebServer();
    server.enqueue(
        new MockResponse().setResponseCode(500).setBody(payloadFromResource("/fault-404.xml")));

    ProfitBricksApi pbApi = api(server.getUrl("/"));
    DataCenterApi api = pbApi.dataCenterApi();

    String id = "random-non-existing-id";
    try {
      api.clearDataCenter(id);
      fail("Request should have failed");
    } catch (Exception ex) {
      assertTrue(
          ex instanceof ResourceNotFoundException,
          "Exception should be an ResourceNotFoundException");
    } finally {
      pbApi.close();
      server.shutdown();
    }
  }
  @Test
  public void testServiceUnderMaintenance() throws Exception {
    MockWebServer server = mockWebServer();
    for (int i = 0; i <= MAX_RETRIES; i++) // jclouds retries 5 times
    server.enqueue(
          new MockResponse()
              .setResponseCode(503)
              .setBody(payloadFromResource("/maintenance-503.html")));

    ProfitBricksApi pbApi = api(server.getUrl("/"));
    DataCenterApi api = pbApi.dataCenterApi();

    try {
      api.clearDataCenter("some-datacenter-id");
      fail("Request should have failed.");
    } catch (Exception ex) {
      assertTrue(ex instanceof HttpResponseException, "Exception should be HttpResponseException");
    } finally {
      pbApi.close();
      server.shutdown();
    }
  }