Skip to content

Commit

Permalink
Apply code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
SailReal committed Nov 27, 2023
1 parent d609ca2 commit 174c7aa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -101,8 +102,7 @@ void refreshLicenseScheduler() throws InterruptedException {

//visible for testing
void refreshLicense(String refreshUrl, String license, HttpClient client) throws InterruptedException {
var parameters = new HashMap<String, String>();
parameters.put("token", license);
var parameters = Map.of("token", license);
var body = parameters.entrySet() //
.stream() //
.map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8)) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Flow;

@QuarkusTest
public class LicenseHolderTest {
Expand Down Expand Up @@ -219,19 +223,26 @@ public void teardown() {
public void testRefreshingExistingValidTokenInculdingRefreshURL() throws IOException, InterruptedException {
var existingJWT = Mockito.mock(DecodedJWT.class);
var receivedJWT = Mockito.mock(DecodedJWT.class);
Mockito.when(existingJWT.getToken()).thenReturn("token");
Mockito.when(existingJWT.getToken()).thenReturn("token&foo=bar");
Mockito.when(validator.validate("token", "42")).thenReturn(receivedJWT);
Mockito.when(validator.validate("oldToken", "42")).thenReturn(existingJWT);
Mockito.when(validator.validate("token&foo=bar", "42")).thenReturn(existingJWT);
Settings settingsMock = new Settings();
settingsMock.hubId = "42";
settingsMock.licenseKey = "oldToken";
settingsMock.licenseKey = "token&foo=bar";
settingsClass.when(Settings::get).thenReturn(settingsMock);

var refreshTokenContainingSpecialChars = HttpRequest.newBuilder() //
.uri(URI.create(refreshURL)) //
.headers("Content-Type", "application/x-www-form-urlencoded") //
.POST(HttpRequest.BodyPublishers.ofString("token&foo=bar")) //
.build();

var httpClient = Mockito.mock(HttpClient.class);
var response = Mockito.mock(HttpResponse.class);
Mockito.doAnswer(invocation -> {
HttpRequest httpRequest = invocation.getArgument(0);
Assertions.assertEquals(refreshRequst, httpRequest);
Assertions.assertEquals(refreshTokenContainingSpecialChars, httpRequest);
Assertions.assertEquals("token=token%26foo%3Dbar", getResponseFromRequest(httpRequest));
return response;
}).when(httpClient).send(Mockito.any(), Mockito.eq(HttpResponse.BodyHandlers.ofString()));
Mockito.when(response.body()).thenReturn("token");
Expand Down Expand Up @@ -268,6 +279,7 @@ public void testInvalidTokenReceivedLeadsToNoOp(String receivedToken, int receiv
Mockito.doAnswer(invocation -> {
HttpRequest httpRequest = invocation.getArgument(0);
Assertions.assertEquals(refreshRequst, httpRequest);
Assertions.assertEquals("token=token", getResponseFromRequest(httpRequest));
return response;
}).when(httpClient).send(Mockito.any(), Mockito.eq(HttpResponse.BodyHandlers.ofString()));
Mockito.when(response.body()).thenReturn(receivedToken);
Expand Down Expand Up @@ -328,6 +340,38 @@ public void testNoOpExistingValidTokenExculdingRefreshURL() throws InterruptedEx
Mockito.verify(session, Mockito.never()).persist(Mockito.any());
Assertions.assertEquals(existingJWT, holder.get());
}

private String getResponseFromRequest(HttpRequest httpRequest) {
return httpRequest.bodyPublisher().map(p -> {
var bodySubscriber = HttpResponse.BodySubscribers.ofString(StandardCharsets.UTF_8);
var flowSubscriber = new StringSubscriber(bodySubscriber);
p.subscribe(flowSubscriber);
return bodySubscriber.getBody().toCompletableFuture().join();
}).get();
}

private record StringSubscriber(HttpResponse.BodySubscriber<String> wrapped) implements Flow.Subscriber<ByteBuffer> {

@Override
public void onSubscribe(Flow.Subscription subscription) {
wrapped.onSubscribe(subscription);
}

@Override
public void onNext(ByteBuffer item) {
wrapped.onNext(List.of(item));
}

@Override
public void onError(Throwable throwable) {
wrapped.onError(throwable);
}

@Override
public void onComplete() {
wrapped.onComplete();
}
}
}

@Nested
Expand Down

0 comments on commit 174c7aa

Please sign in to comment.