package benjamin.groehbiel.ch.shortener;

import benjamin.groehbiel.ch.DataTest;
import benjamin.groehbiel.ch.shortener.redis.RedisManager;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@Ignore
public class ShortenerServiceTest extends DataTest {

    @Autowired
    private ShortenerService shortenerService;

    @Autowired
    private RedisManager redisManager;

    @Test
    public void shouldReturnARealWordAsHashInShortenedUrl() throws URISyntaxException, IOException {
        URI inputUri = new URI("http://www.example.org");
        ShortenerHandle shortenerHandle = shortenerService.shorten(inputUri);
        assertThat(shortenerHandle.getHash(), equalTo("able"));

        URI anotherShortenedUri = new URI("http://www.example2.org");
        shortenerHandle = shortenerService.shorten(anotherShortenedUri);
        assertThat(shortenerHandle.getHash(), equalTo("unable"));
    }

    @Test
    public void shouldLookupUrlInRedisGivenAHash() throws URISyntaxException, IOException {
        redisManager.setUrlAndHash("$count", "0");

        assertThat(shortenerService.getShortenedCount(), equalTo(0L));

        redisManager.setUrlAndHash("$count", "1");
        ShortenerHandle shortenerHandleForWater = new ShortenerHandle();
        String value = new ObjectMapper().writeValueAsString(shortenerHandleForWater);

        redisManager.setUrlAndHash(RedisManager.HASH_PREFIX + "water", value);

        ShortenerHandle shortenerHandle = shortenerService.expand("water");
        assertThat(shortenerHandle, equalTo(shortenerHandleForWater));
    }

    @Test
    public void shouldReturnCorrectRemainingCount() {
        assertThat(shortenerService.getRemainingCount(), equalTo(21L));
    }

    @Test
    public void shouldReturnCorrectShortenedCount() {
        assertThat(shortenerService.getShortenedCount(), equalTo(0L));
    }

}

@@ -181,24 +181,150 @@
 jectMapper;%0A
+%3C%3C%3C%3C%3C%3C%3C HEAD%0A=======%0Aimport org.junit.Before;%0A%3E%3E%3E%3E%3E%3E%3E Add admin api for listing all available hashes including paging/sorting%0A
 import org.j
@@ -1308,32 +1308,33 @@
 able%22));%0A    %7D%0A%0A
+%0A
     @Test%0A    pu

Add admin api for listing all available hashes including paging/sorting