package it.w0rd.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import it.w0rd.DataTest;
import it.w0rd.persistence.redis.RedisManager;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
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;

public class ShortenerServiceTest extends DataTest {

    @Autowired
    private ShortenerService shortenerService;

    @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));
    }

    @Test
    public void shouldRemoveExistingShortenedUrlAndMakeAvailable() throws IOException, URISyntaxException {
        ShortenerHandle handle = shortenerService.shorten(URI.create("http://www.example.com"));
        String hashToDelete = handle.getHash();

        MatcherAssert.assertThat(dictionaryManager.getWordsAvailableSize(), IsEqual.equalTo(20L));
        assertThat(shortenerService.getShortenedCount(), IsEqual.equalTo(1L));

        shortenerService.remove(hashToDelete);

        MatcherAssert.assertThat(dictionaryManager.getWordsAvailableSize(), IsEqual.equalTo(21L));
        assertThat(shortenerService.getShortenedCount(), IsEqual.equalTo(0L));
    }
}

@@ -633,40 +633,15 @@
 ould
-ReturnARealWordAsHashIn
 Shorten
-ed
 Url(
@@ -695,242 +695,52 @@
     
-URI inputUri = new URI(%22http://www.example.org%22);%0A        ShortenerHandle shortenerHandle = shortenerService.shorten(inputUri);%0A        assertThat(shortenerHandle.getHash(), equalTo(%22able%22));%0A%0A        URI anotherShortenedUri = new URI
+ShortenerHandle shortenerHandle = shortenUrl
 (%22ht
@@ -759,17 +759,16 @@
 mple
-2
 .org%22);%0A
     
@@ -767,81 +767,8 @@
 %22);%0A
-        shortenerHandle = shortenerService.shorten(anotherShortenedUri);%0A
     
@@ -818,18 +818,16 @@
 qualTo(%22
-un
 able%22));
@@ -876,24 +876,8 @@
 okup
-UrlInRedisGivenA
 Hash
@@ -924,256 +924,8 @@
 n %7B%0A
-        redisManager.setUrlAndHash(%22$count%22, %220%22);%0A%0A        assertThat(shortenerService.getShortenedCount(), equalTo(0L));%0A%0A        redisManager.setUrlAndHash(%22$count%22, %221%22);%0A        ShortenerHandle shortenerHandleForWater = new ShortenerHandle();%0A
     
@@ -981,17 +981,21 @@
 sString(
-s
+new S
 hortener
@@ -1004,20 +1004,13 @@
 ndle
-ForWater
+()
 );%0A
-%0A
     
@@ -1205,17 +1205,21 @@
 equalTo(
-s
+new S
 hortener
@@ -1228,16 +1228,10 @@
 ndle
-ForWater
+()
 ));%0A
@@ -1587,21 +1587,16 @@
 lAnd
-MakeAvailable
+FreeItUp
 () t
@@ -1663,126 +1663,58 @@
 dle 
-handle = shortenerService.shorten(URI.create(%22http://www.example.com%22));%0A        String hashToDelete = handle.getHash(
+shortenedUrl = shortenUrl(%22http://www.example.com%22
 );%0A%0A
@@ -1928,20 +1928,30 @@
 ove(
-hashToDelete
+shortenedUrl.getHash()
 );%0A%0A
@@ -2134,10 +2134,165 @@
 ;%0A    %7D%0A
+%0A    private ShortenerHandle shortenUrl(String url) throws URISyntaxException, IOException %7B%0A        return shortenerService.shorten(new URI(url));%0A    %7D%0A%0A
 %7D%0A

Tidy up ShortenerServiceTest