package org.netmelody.cieye.server.observation.test;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.netmelody.cieye.server.observation.GovernmentWatchdog;

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

public class GovernmentWatchdogTest {
    private final StubCommunicationNetwork network = new StubCommunicationNetwork();
    private final GovernmentWatchdog watchdog = new GovernmentWatchdog(network);
    
    @Test public void
    processesJsonResponse() {
        network.respondingWith("http://github.com/api/v2/json/repos/show/netmelody/ci-eye/tags", contentFrom("gh_2.0_tags.json"));
        assertThat(watchdog.getLatestVersion(), equalTo("0.3.0"));
    }

    @Test public void
    ranksTagsNumerically() {
        final String json = "{\"tags\":{\"0.0.2\":\"\",\"0.0.11\":\"\"}}";
        
        network.respondingWith("http://github.com/api/v2/json/repos/show/netmelody/ci-eye/tags", json);
        assertThat(watchdog.getLatestVersion(), equalTo("0.0.11"));
    }
    
    @Test public void
    ranksBetaTagsLower() {
        final String json = "{\"tags\":{\"0.0.1beta1\":\"\",\"0.0.1\":\"\"}}";
        
        network.respondingWith("http://github.com/api/v2/json/repos/show/netmelody/ci-eye/tags", json);
        assertThat(watchdog.getLatestVersion(), equalTo("0.0.1"));
    }
    
    private String contentFrom(String resourceName) {
        try {
            return IOUtils.toString(GovernmentWatchdogTest.class.getResourceAsStream(resourceName));
        }
        catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

@@ -691,24 +691,143 @@
 gs.json%22));%0A
+        network.respondingWith(%22https://api.github.com/repos/netmelody/ci-eye/tags%22, contentFrom(%22gh_3.0_tags.json%22));%0A
         asse
@@ -956,32 +956,35 @@
 inal String json
+_v2
  = %22%7B%5C%22tags%5C%22:%7B%5C
@@ -1009,32 +1009,114 @@
 0.11%5C%22:%5C%22%5C%22%7D%7D%22;%0A
+        final String json_v3 = %22%5B%7B%5C%22name%5C%22: %5C%220.0.2%5C%22%7D, %7B%5C%22name%5C%22: %5C%220.0.11%5C%22%7D%5D%22;%0A
         %0A       
@@ -1201,35 +1201,142 @@
 -eye/tags%22, json
+_v2
 );%0A
+        network.respondingWith(%22https://api.github.com/repos/netmelody/ci-eye/tags%22, json_v3);%0A        %0A
         assertTh
@@ -1472,16 +1472,19 @@
 ing json
+_v2
  = %22%7B%5C%22t
@@ -1521,32 +1521,118 @@
 .0.1%5C%22:%5C%22%5C%22%7D%7D%22;%0A
+        final String json_v3 = %22%5B%7B%5C%22name%5C%22: %5C%220.0.1beta1%5C%22%7D, %7B%5C%22name%5C%22: %5C%220.0.1%5C%22%7D%5D%22;%0A
         %0A       
@@ -1725,18 +1725,125 @@
 s%22, json
-);
+_v2);%0A        network.respondingWith(%22https://api.github.com/repos/netmelody/ci-eye/tags%22, json_v3);%0A        
 %0A       

prepare for transition to v3 of github api