Skip to content

Commit

Permalink
Interface aligned.
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniotarricone committed May 28, 2024
1 parent c4dafe9 commit c4a7782
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import io.quarkus.logging.Log;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import it.pagopa.swclient.mil.azureservices.keyvault.keys.bean.JsonWebKeyOperation;
Expand Down Expand Up @@ -87,21 +89,30 @@ public Multi<KeyBundle> getKeys(String prefix, List<String> expectedOps, List<St
* @param expectedKtys {@link JsonWebKeyType}
* @return
*/
public Uni<KeyBundle> getKeyWithLongestExp(String prefix, List<String> expectedOps, List<String> expectedKtys) {
Comparator<KeyBundle> comparator = Comparator.comparing(
new Function<KeyBundle, Long>() { // NOSONAR
@Override
public Long apply(KeyBundle t) {
return t.getAttributes().getExp();
}

})
.reversed();

public Uni<Optional<KeyBundle>> getKeyWithLongestExp(String prefix, List<String> expectedOps, List<String> expectedKtys) {
return getKeys(prefix, expectedOps, expectedKtys)
.collect()
.asList()
.invoke(keyList -> keyList.sort(comparator))
.map(List::getFirst);
.map(keyList -> {
if (keyList.isEmpty()) {
Log.debug("No key found");
return Optional.empty();
} else {
Log.trace("Keys found");

Comparator<KeyBundle> comparator = Comparator.comparing(
new Function<KeyBundle, Long>() { // NOSONAR
@Override
public Long apply(KeyBundle t) {
return t.getAttributes().getExp();
}

})
.reversed();

keyList.sort(comparator);
return Optional.of(keyList.getFirst());
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -572,6 +573,31 @@ void given_setOfKeys_when_getKeyWithLongestExpInvoked_then_getRelevantKey() {
.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.awaitItem()
.assertItem(bundle__attr_ok_longest_exp__key_rsa_sign_verify);
.assertItem(Optional.of(bundle__attr_ok_longest_exp__key_rsa_sign_verify));
}

/**
*
*/
@Test
void given_noKey_when_getKeyWithLongestExpInvoked_then_getEmpty() {
/*
* Setup
*/
when(keysService.getKeys())
.thenReturn(Uni.createFrom().item(new KeyListResult()
.setValue(List.of())));

/*
* Test
*/
extService.getKeyWithLongestExp(
"attr",
List.of(JsonWebKeyOperation.SIGN, JsonWebKeyOperation.VERIFY),
List.of(JsonWebKeyType.RSA))
.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.awaitItem()
.assertItem(Optional.empty());
}
}

0 comments on commit c4a7782

Please sign in to comment.