/*
 * Copyright 2015 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.hod.client.token;

import com.hp.autonomy.hod.client.api.authentication.AuthenticationToken;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class InMemoryTokenRepositoryTest {

    private InMemoryTokenRepository tokenRepository;

    @Before
    public void setUp() {
        tokenRepository = new InMemoryTokenRepository();
    }

    @Test
    public void testGetReturnsNullIfNoKey() {
        assertThat(tokenRepository.get(new TokenProxy()), is(nullValue()));
    }

    @Test
    public void testInsertedTokensCanBeRetrieved() {
        final AuthenticationToken token = mockToken();

        final TokenProxy key = tokenRepository.insert(token);

        assertThat(tokenRepository.get(key), is(token));
    }

    @Test
    public void testUpdate() {
        final AuthenticationToken token = mockToken();

        final TokenProxy key = tokenRepository.insert(token);

        final AuthenticationToken newToken = mockToken();
        tokenRepository.update(key, newToken);

        assertThat(tokenRepository.get(key), is(newToken));
    }

    @Test
    public void testUpdateReturnsNullAndDoesNothingIfKeyNotPresent() {
        final TokenProxy key = new TokenProxy();
        final AuthenticationToken oldToken = tokenRepository.update(key, mockToken());

        assertThat(oldToken, is(nullValue()));
        assertThat(tokenRepository.get(key), is(nullValue()));
    }

    @Test
    public void testRemove() {
        final AuthenticationToken token = mockToken();
        final TokenProxy key = tokenRepository.insert(token);

        assertThat(tokenRepository.remove(key), is(token));
        assertThat(tokenRepository.get(key), is(nullValue()));
    }

    private AuthenticationToken mockToken() {
        final AuthenticationToken token = mock(AuthenticationToken.class);
        when(token.hasExpired()).thenReturn(false);

        return token;
    }
}
@@ -343,16 +343,45 @@
 .Test;%0A%0A
+import java.io.IOException;%0A%0A
 import s
@@ -511,16 +511,53 @@
 .Is.is;%0A
+import static org.junit.Assert.fail;%0A
 import s
@@ -2167,24 +2167,955 @@
 )));%0A    %7D%0A%0A
+    @Test(expected = IllegalArgumentException.class)%0A    public void testInsertThrowsIllegalArgumentExceptionForExpiredTokens() throws IOException %7B%0A        final AuthenticationToken token = mock(AuthenticationToken.class);%0A        when(token.hasExpired()).thenReturn(true);%0A%0A        tokenRepository.insert(token);%0A    %7D%0A%0A    @Test(expected = IllegalArgumentException.class)%0A    public void testUpdateThrowsIllegalArgumentExceptionForExpiredTokens() throws IOException %7B%0A        final AuthenticationToken token1 = mockToken();%0A        final AuthenticationToken token2 = mock(AuthenticationToken.class);%0A        when(token2.hasExpired()).thenReturn(true);%0A%0A        TokenProxy key = null;%0A%0A        try %7B%0A            key = tokenRepository.insert(token1);%0A        %7D catch (final IllegalArgumentException e) %7B%0A            fail(%22The initial token should not have expired%22);%0A        %7D%0A%0A        tokenRepository.update(key, token2);%0A    %7D%0A%0A
     private 

Check for IllegalArgumentExceptions. [rev: matthew.gordon]