package com.justinbeltran.jelp;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.scribe.model.Response;

import com.justinbeltran.jelp.model.Business;
import com.justinbeltran.jelp.model.Results;

public class JelpTest extends BaseTest {

    @Mock
    private Api api;

    @InjectMocks
    private Jelp jelp = new Jelp("consumerKey", "consumerSecret", "tokenKey", "tokenSecret");

    @Test
    public void search() throws Exception {

        Response resp = mock(Response.class);
        when(api.search(Mockito.anyString(), Mockito.anyString())).thenReturn(resp);

        when(resp.isSuccessful()).thenReturn(true);

        String exampleResponse = IOUtils.toString(this.getClass().getResourceAsStream("/example_search_result.json"));
        when(resp.getBody()).thenReturn(exampleResponse);

        Results results = jelp.search("sushi", "Irvine, CA");
        assertThat(results.getTotal(), is(176));
    }

    @Test(expected = IllegalArgumentException.class)
    public void search_unsuccessful() {
        Response resp = mock(Response.class);
        when(api.search(Mockito.anyString(), Mockito.anyString())).thenReturn(resp);

        when(resp.isSuccessful()).thenReturn(false);

        jelp.search("sushi", "Irvine, CA");
    }

    @Test
    public void business() throws Exception {

        Response resp = mock(Response.class);
        when(api.business(Mockito.anyString())).thenReturn(resp);

        when(resp.isSuccessful()).thenReturn(true);

        String exampleResponse = IOUtils.toString(this.getClass().getResourceAsStream("/example_business.json"));
        when(resp.getBody()).thenReturn(exampleResponse);

        Business business = jelp.business("yelp-san-francisco");
        assertThat(business.getDisplay_phone(), is("+1-415-677-9744"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void business_unsuccessful() {
        Response resp = mock(Response.class);
        when(api.search(Mockito.anyString(), Mockito.anyString())).thenReturn(resp);

        when(resp.isSuccessful()).thenReturn(false);

        jelp.search("sushi", "Irvine, CA");
    }

    private class Name implements Comparable<Name> {
        private String firstName;
        private String lastName;

        public Name(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + getOuterType().hashCode();
            result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
            result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Name other = (Name) obj;
            if (!getOuterType().equals(other.getOuterType()))
                return false;
            if (firstName == null) {
                if (other.firstName != null)
                    return false;
            } else if (!firstName.equals(other.firstName))
                return false;
            if (lastName == null) {
                if (other.lastName != null)
                    return false;
            } else if (!lastName.equals(other.lastName))
                return false;
            return true;
        }

        private JelpTest getOuterType() {
            return JelpTest.this;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public int compareTo(Name other) {
            return this.lastName.compareTo(other.getLastName());
        }

        public String toString() {
            return firstName + " " + lastName;
        }

    }

}

@@ -2394,2157 +2394,6 @@
   %7D%0A
-%0A    private class Name implements Comparable%3CName%3E %7B%0A        private String firstName;%0A        private String lastName;%0A%0A        public Name(String firstName, String lastName) %7B%0A            this.firstName = firstName;%0A            this.lastName = lastName;%0A        %7D%0A%0A        @Override%0A        public int hashCode() %7B%0A            final int prime = 31;%0A            int result = 1;%0A            result = prime * result + getOuterType().hashCode();%0A            result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());%0A            result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());%0A            return result;%0A        %7D%0A%0A        @Override%0A        public boolean equals(Object obj) %7B%0A            if (this == obj)%0A                return true;%0A            if (obj == null)%0A                return false;%0A            if (getClass() != obj.getClass())%0A                return false;%0A            Name other = (Name) obj;%0A            if (!getOuterType().equals(other.getOuterType()))%0A                return false;%0A            if (firstName == null) %7B%0A                if (other.firstName != null)%0A                    return false;%0A            %7D else if (!firstName.equals(other.firstName))%0A                return false;%0A            if (lastName == null) %7B%0A                if (other.lastName != null)%0A                    return false;%0A            %7D else if (!lastName.equals(other.lastName))%0A                return false;%0A            return true;%0A        %7D%0A%0A        private JelpTest getOuterType() %7B%0A            return JelpTest.this;%0A        %7D%0A%0A        public String getFirstName() %7B%0A            return firstName;%0A        %7D%0A%0A        public void setFirstName(String firstName) %7B%0A            this.firstName = firstName;%0A        %7D%0A%0A        public String getLastName() %7B%0A            return lastName;%0A        %7D%0A%0A        public void setLastName(String lastName) %7B%0A            this.lastName = lastName;%0A        %7D%0A%0A        public int compareTo(Name other) %7B%0A            return this.lastName.compareTo(other.getLastName());%0A        %7D%0A%0A        public String toString() %7B%0A            return firstName + %22 %22 + lastName;%0A        %7D%0A%0A    %7D%0A%0A
 %7D%0A

Update JelpTest.java