UriForLocationHeaderSupplier implements Supplier<Uri> { @Override public Uri get() { final Uri requestUri = httpRequest.getUri().query(Query.EMPTY); if (isRequestIdempotent()) { return requestUri.query(Query.EMPTY); } return Uri.create(removeTrailingSlash(getLocationUriString(removeEntityId(requestUri.toString())))) .query(Query.EMPTY); } UriForLocationHeaderSupplier(final HttpRequest httpRequest, final CommandResponse<?> commandResponse); @Override Uri get();  }
@Test public void getUriForNonIdempotentRequestWithEndingSlash() { final Uri uri = Uri.create("https: final HttpRequest httpRequest = HttpRequest.create() .withUri(uri) .withMethod(HttpMethods.POST); final CommandResponse<?> commandResponse = mock(CommandResponse.class); when(commandResponse.getEntityId()).thenReturn(KNOWN_ENTITY_ID); when(commandResponse.getResourcePath()).thenReturn(JsonPointer.of(KNOWN_RESOURCE_PATH + "/")); final Uri expectedUri = Uri.create(uri.toString() + "/" + commandResponse.getEntityId() + "/" + KNOWN_RESOURCE_PATH); final UriForLocationHeaderSupplier underTest = new UriForLocationHeaderSupplier(httpRequest, commandResponse); assertThat(underTest.get()).isEqualTo(expectedUri); }
@Test public void getUriForNonIdempotentRequestWithEntityIdInUri() { final String host = "https: final Uri uri = Uri.create(host + KNOWN_ENTITY_ID + "will-be-discarded/"); final HttpRequest httpRequest = HttpRequest.create() .withUri(uri) .withMethod(HttpMethods.POST); final CommandResponse<?> commandResponse = mock(CommandResponse.class); when(commandResponse.getEntityId()).thenReturn(KNOWN_ENTITY_ID); when(commandResponse.getResourcePath()).thenReturn(JsonPointer.of(KNOWN_RESOURCE_PATH)); final Uri expectedUri = Uri.create(host + commandResponse.getEntityId() + commandResponse.getResourcePath()); final UriForLocationHeaderSupplier underTest = new UriForLocationHeaderSupplier(httpRequest, commandResponse); assertThat(underTest.get()).isEqualTo(expectedUri); }