public void ensureFolder(String parentPath, String name)
      throws IOException, InvalidTokenException {
    Map<String, Object> rawFolder = new LinkedHashMap<>();
    rawFolder.put("name", name);

    String url;
    try {
      url =
          getUriBuilder()
              .setPath(API_PATH_PREFIX + "/mounts/primary/files/folder")
              .setParameter("path", parentPath)
              .build()
              .toString();
    } catch (URISyntaxException e) {
      throw new IllegalStateException("Could not produce url.", e);
    }

    Request.Builder requestBuilder = getRequestBuilder(url);
    requestBuilder.post(
        RequestBody.create(
            MediaType.parse("application/json"), objectMapper.writeValueAsString(rawFolder)));

    try (Response response = getResponse(requestBuilder)) {
      int code = response.code();
      // 409 response code means that the folder already exists
      if ((code < 200 || code > 299) && code != 409) {
       throw new KoofrClientIOException(response);
      }
    }
  }
@Test
  public void testEnsureFolderTokenExpired() throws Exception {
    when(credentialFactory.refreshCredential(credential))
        .then(
            (InvocationOnMock invocation) -> {
              final Credential cred = invocation.getArgument(0);
              cred.setAccessToken("acc1");
              return cred;
            });

    server.enqueue(new MockResponse().setResponseCode(401));
    server.enqueue(new MockResponse().setResponseCode(200));

    client.ensureFolder("/path/to/folder", "name");

    assertEquals(2, server.getRequestCount());

    RecordedRequest recordedRequest = server.takeRequest();

    assertEquals("POST", recordedRequest.getMethod());
    assertEquals(
        "/api/v2/mounts/primary/files/folder?path=%2Fpath%2Fto%2Ffolder",
        recordedRequest.getPath());
    assertEquals("Bearer acc", recordedRequest.getHeader("Authorization"));
    assertEquals("2.1", recordedRequest.getHeader("X-Koofr-Version"));
    assertEquals(
        "application/json; charset=utf-8", recordedRequest.getHeader("Content-Type"));
    assertEquals("{\"name\":\"name\"}", recordedRequest.getBody().readUtf8());

    recordedRequest = server.takeRequest();

    assertEquals("POST", recordedRequest.getMethod());
    assertEquals(
        "/api/v2/mounts/primary/files/folder?path=%2Fpath%2Fto%2Ffolder",
        recordedRequest.getPath());
    assertEquals("Bearer acc1", recordedRequest.getHeader("Authorization"));
    assertEquals("2.1", recordedRequest.getHeader("X-Koofr-Version"));
    assertEquals(
        "application/json; charset=utf-8", recordedRequest.getHeader("Content-Type"));
    assertEquals("{\"name\":\"name\"}", recordedRequest.getBody().readUtf8());
  }