// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.uber.jenkins.phabricator.conduit;

import hudson.EnvVars;
import junit.framework.TestCase;
import net.sf.json.JSONObject;
import net.sf.json.groovy.JsonSlurper;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.*;

import java.io.IOException;
import java.io.InputStream;

public class DifferentialTest extends TestCase {
    private static final String FAKE_DIFF_ID = "not-a-real-id";

    Differential differential;

    @Before
    public void setupDiff () throws InterruptedException, ArcanistUsageException, IOException {
        differential = diffWithResponse();
    }

    @Test
    public void testFetchRevisionID() throws Exception {
        assertEquals(FAKE_DIFF_ID, differential.getRevisionID(false));
    }

    @Test
    public void testGetPhabricatorLink() throws Exception {
        assertTrue(differential.getPhabricatorLink("http://example.com").contains(FAKE_DIFF_ID));
    }

    @Test
    public void testGetPhabricatorLinkInvalidURL() throws Exception {
        // Try our best to join URLs, even when they are wrong
        assertTrue(differential.getPhabricatorLink("aoeu").contains("aoeu"));
    }

    @Test
    public void testGetBuildStartedMessage() throws Exception {
        assertTrue(differential.getBuildStartedMessage(new EnvVars()).contains("Build started"));
    }

    private Differential diffWithResponse() throws InterruptedException, ArcanistUsageException, IOException {
        DifferentialClient client = mock(DifferentialClient.class);
        when(client.fetchDiff()).thenReturn(getValidQueryResponse());

        return new Differential(client.fetchDiff());
    }

    private JSONObject getValidQueryResponse() throws IOException {
        InputStream input = getClass().getResourceAsStream(
                "validDifferentialQueryResponse.json"
        );
        return (JSONObject) new JsonSlurper().parse(input);
    }

}

@@ -1309,75 +1309,12 @@
 nit.
-Before;%0Aimport org.junit.Test;%0A%0Aimport static org.mockito.Mockito.*
+Test
 ;%0A%0Ai
@@ -1523,26 +1523,17 @@
     
-@Before%0A    public
+protected
  voi
@@ -1541,15 +1541,10 @@
  set
-upDiff 
+Up
 () t
@@ -1546,34 +1546,25 @@
 p() throws I
-nterrupted
+O
 Exception, A
@@ -1579,33 +1579,42 @@
 sageException, I
-O
+nterrupted
 Exception %7B%0A    
@@ -1632,24 +1632,46 @@
 ntial = 
-diffWith
+new Differential(getValidQuery
 Response
@@ -1672,16 +1672,17 @@
 sponse()
+)
 ;%0A    %7D%0A
@@ -2296,32 +2296,32 @@
 ows Exception %7B%0A
+
         assertTr
@@ -2413,318 +2413,8 @@
  %7D%0A%0A
-    private Differential diffWithResponse() throws InterruptedException, ArcanistUsageException, IOException %7B%0A        DifferentialClient client = mock(DifferentialClient.class);%0A        when(client.fetchDiff()).thenReturn(getValidQueryResponse());%0A%0A        return new Differential(client.fetchDiff());%0A    %7D%0A%0A
     

Address review feedback