@Test
  public void deleteProxy() {
    when(aclServiceDao.getProxy(IpInterval.parse("10.0.0.0/32")))
        .thenReturn(new Proxy("10.0.0.0/32", ""));

    final Response response = subject.deleteProxy("10.0.0.0/32");
    assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));

    verify(aclServiceDao).deleteProxy(IpInterval.parse("10.0.0.0/32"));
  }
  @Test
  public void getProxy_not_found() {
    when(aclServiceDao.getProxy(IpInterval.parse("10.0.0.0/32")))
        .thenThrow(EmptyResultDataAccessException.class);

    final Response response = subject.getProxy("10.0.0.0/32");
    assertThat(response.getStatus(), is(404));
  }
  @Test
  public void getProxy() {
    final Proxy proxy = new Proxy("10.0.0.0/32", "test");
    when(aclServiceDao.getProxy(IpInterval.parse("10.0.0.0/32"))).thenReturn(proxy);

    final Response response = subject.getProxy("10.0.0.0/32");
    assertThat((Proxy) response.getEntity(), is(proxy));
  }
  @Test
  public void deleteProxy_unknown() {
    when(aclServiceDao.getProxy(IpInterval.parse("10.0.0.0/32")))
        .thenThrow(EmptyResultDataAccessException.class);

    final Response response = subject.deleteProxy("10.0.0.0/32");
    assertThat(response.getStatus(), is(Response.Status.NOT_FOUND.getStatusCode()));

    verify(aclServiceDao, never()).deleteProxy(Matchers.any(IpInterval.class));
  }
  @Test
  public void saveProxy_modify() {
    when(aclServiceDao.getProxy(IpInterval.parse("10.0.0.0/32")))
        .thenReturn(new Proxy("10.0.0.0/32", ""));

    final Proxy proxy = new Proxy("10.0.0.0/32", "some");
    final Response response = subject.saveProxy(proxy);
    assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
    assertThat(response.getEntity(), instanceOf(Proxy.class));
    assertThat(((Proxy) response.getEntity()).getPrefix(), is("10.0.0.0/32"));

    verify(aclServiceDao).updateProxy(proxy);
  }
  @Test
  public void saveProxy_create() {
    when(aclServiceDao.getProxy(IpInterval.parse("10.0.0.0/32")))
        .thenThrow(EmptyResultDataAccessException.class);

    final Proxy proxy = new Proxy("10.0.0.0/32", "some");
    final Response response = subject.saveProxy(proxy);
    assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
    assertThat(response.getEntity(), instanceOf(Proxy.class));
    assertThat(((Proxy) response.getEntity()).getPrefix(), is("10.0.0.0/32"));

    verify(aclServiceDao).createProxy(proxy);
  }
  private Response doSyncUpdate(
      final HttpServletRequest httpServletRequest, final Request request, final Charset charset) {
    loggerContext.init(getRequestId(request.getRemoteAddress()));

    try {
      loggerContext.log(new HttpRequestMessage(httpServletRequest));

      if (!sourceMatchesContext(request.getSource())) {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("Invalid source specified: " + request.getSource())
            .build();
      }

      if (request.isParam(Command.DIFF)) {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("the DIFF method is not actually supported by the Syncupdates interface")
            .build();
      }

      boolean notificationsEnabled = true;
      if (request.isParam(Command.REDIRECT)) {
        if (!ipRanges.isTrusted(IpInterval.parse(request.getRemoteAddress()))) {
          return Response.status(Response.Status.FORBIDDEN)
              .entity("Not allowed to disable notifications: " + request.getRemoteAddress())
              .build();
        }

        notificationsEnabled = false;
      }

      if (!request.hasParam(Command.DATA) && request.isParam(Command.NEW)) {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("DATA parameter is missing")
            .build();
      }

      if (!request.hasParam(Command.DATA) && !request.isParam(Command.HELP)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Invalid request").build();
      }

      loggerContext.log("msg-in.txt", new SyncUpdateLogCallback(request.toString()));

      final UpdateContext updateContext = new UpdateContext(loggerContext);

      setSsoSessionToContext(updateContext, request.getSsoToken());

      final String content = request.hasParam("DATA") ? request.getParam("DATA") : "";

      final UpdateRequest updateRequest =
          new UpdateRequest(
              new SyncUpdate(dateTimeProvider, request.getRemoteAddress()),
              getKeyword(request),
              content,
              updatesParser.parse(
                  updateContext, Lists.newArrayList(new ContentWithCredentials(content, charset))),
              notificationsEnabled);

      final UpdateResponse updateResponse =
          updateRequestHandler.handle(updateRequest, updateContext);
      loggerContext.log("msg-out.txt", new SyncUpdateLogCallback(updateResponse.getResponse()));
      return getResponse(updateResponse);

    } finally {
      loggerContext.remove();
    }
  }