package de.otto.edison.oauth;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static de.otto.edison.oauth.OAuthPublicKey.oAuthPublicKeyBuilder;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class OAuthPublicKeyInMemoryRepositoryTest {


    private OAuthPublicKeyInMemoryRepository inMemoryRepository;

    @BeforeEach
    void setUp() {
        inMemoryRepository = new OAuthPublicKeyInMemoryRepository();
    }

    @Test
    void shouldUpdateCachedListWithValidKeys() {
        // given
        final ZonedDateTime now = ZonedDateTime.now();
        final OAuthPublicKey publicKeyOne = oAuthPublicKeyBuilder()
                .withPublicKey("publicKeyOne")
                .withPublicKeyFingerprint("fingerPrintOne")
                .withValidFrom(now.minusDays(1))
                .withValidUntil(now.plusDays(1))
                .build();
        final OAuthPublicKey publicKeyTwo = oAuthPublicKeyBuilder()
                .withPublicKey("publicKeyTwo")
                .withPublicKeyFingerprint("fingerPrintTwo")
                .withValidFrom(now.minusDays(2))
                .withValidUntil(now.plusDays(2))
                .build();
        final List<OAuthPublicKey> validPublicKeys = Arrays.asList(
                publicKeyOne,
                publicKeyTwo
        );

        // when
        inMemoryRepository.refreshPublicKeys(validPublicKeys);

        // then
        assertThat(inMemoryRepository.retrieveActivePublicKeys(), is(validPublicKeys));
    }


    @Test
    void shouldNotUpdateCachedListWithExistingKey() {
        // given
        final ZonedDateTime now = ZonedDateTime.now();
        final OAuthPublicKey publicKeyOne = oAuthPublicKeyBuilder()
                .withPublicKey("publicKeyOne")
                .withPublicKeyFingerprint("fingerPrintOne")
                .withValidFrom(now.minusDays(1))
                .withValidUntil(now.plusDays(1))
                .build();
        final List<OAuthPublicKey> validPublicKeys = Collections.singletonList(publicKeyOne);

        inMemoryRepository.refreshPublicKeys(validPublicKeys);
        final List<OAuthPublicKey> activeKeys = inMemoryRepository.retrieveActivePublicKeys();

        // when
        inMemoryRepository.refreshPublicKeys(validPublicKeys);

        // then
        assertThat(inMemoryRepository.retrieveActivePublicKeys(), is(activeKeys));
    }

    @Test
    void shouldThrowExceptionWhenInactiveKeysWhereGiven() {
        // given
        final ZonedDateTime now = ZonedDateTime.now();
        final OAuthPublicKey publicKeyOne = oAuthPublicKeyBuilder()
                .withPublicKey("publicKeyOne")
                .withPublicKeyFingerprint("fingerPrintOne")
                .withValidFrom(now.minusDays(2))
                .withValidUntil(now.minusDays(1))
                .build();
        final List<OAuthPublicKey> invalidPublicKeys = Collections.singletonList(publicKeyOne);

        // when/then
        assertThrows(IllegalArgumentException.class, () -> inMemoryRepository.refreshPublicKeys(invalidPublicKeys), "Invalid public keys retrieved");
    }
}
@@ -2605,723 +2605,6 @@
    %7D
-%0A%0A    @Test%0A    void shouldThrowExceptionWhenInactiveKeysWhereGiven() %7B%0A        // given%0A        final ZonedDateTime now = ZonedDateTime.now();%0A        final OAuthPublicKey publicKeyOne = oAuthPublicKeyBuilder()%0A                .withPublicKey(%22publicKeyOne%22)%0A                .withPublicKeyFingerprint(%22fingerPrintOne%22)%0A                .withValidFrom(now.minusDays(2))%0A                .withValidUntil(now.minusDays(1))%0A                .build();%0A        final List%3COAuthPublicKey%3E invalidPublicKeys = Collections.singletonList(publicKeyOne);%0A%0A        // when/then%0A        assertThrows(IllegalArgumentException.class, () -%3E inMemoryRepository.refreshPublicKeys(invalidPublicKeys), %22Invalid public keys retrieved%22);%0A    %7D
 %0A%7D

remove unnecessary test as inactive keys are removed by the job