package org.sagebionetworks.search;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.sagebionetworks.simpleHttpClient.SimpleHttpClient;
import org.sagebionetworks.simpleHttpClient.SimpleHttpRequest;
import org.sagebionetworks.simpleHttpClient.SimpleHttpResponse;
import org.springframework.test.util.ReflectionTestUtils;

public class CloudSearchClientTest {

	SimpleHttpClient mockHttpClient;
	CloudSearchClient cloudSearchClient;

	@Before
	public void before() {
		mockHttpClient = Mockito.mock(SimpleHttpClient.class);
		cloudSearchClient = new CloudSearchClient("https://svc.endpoint.com", "https://doc.endpoint.com");
		ReflectionTestUtils.setField(cloudSearchClient, "httpClient", mockHttpClient);
	}
	
	
	@Test
	public void testPLFM2968NoError() throws Exception {
		SimpleHttpResponse resp = new SimpleHttpResponse();
		resp.setStatusCode(200);
		resp.setContent("s");
		when(mockHttpClient.get(any(SimpleHttpRequest.class))).thenReturn(resp);
		assertEquals(resp.getContent(), cloudSearchClient.performSearch("aQuery"));
		verify(mockHttpClient).get(any(SimpleHttpRequest.class));
	}
	
	@Test
	public void testPLFM2968NoRecover() throws Exception {
		SimpleHttpResponse resp = new SimpleHttpResponse();
		resp.setStatusCode(507);
		when(mockHttpClient.get(any(SimpleHttpRequest.class))).thenReturn(resp);
		try {
			cloudSearchClient.performSearch("aQuery");
		} catch (CloudSearchClientException e) {
			assertEquals(507, e.getStatusCode());
		} finally {
			verify(mockHttpClient, times(6)).get(any(SimpleHttpRequest.class));
		}
	}

	@Test
	public void testPLFM3777() throws Exception {
		SimpleHttpResponse resp = new SimpleHttpResponse();
		resp.setStatusCode(504);
		when(mockHttpClient.get(any(SimpleHttpRequest.class))).thenReturn(resp);
		try {
			cloudSearchClient.performSearch("aQuery");
		} catch (CloudSearchClientException e) {
			assertEquals(504, e.getStatusCode());
		} finally {
			verify(mockHttpClient).get(any(SimpleHttpRequest.class));
		}
	}

}

@@ -310,19 +310,55 @@
 ito.Mock
-ito
+;%0Aimport org.mockito.MockitoAnnotations
 ;%0Aimport
@@ -637,16 +637,23 @@
 Test %7B%0A%0A
+%09@Mock%0A
 %09SimpleH
@@ -678,16 +678,57 @@
 Client;%0A
+%09@Mock%0A%09SimpleHttpResponse mockResponse;%0A
 %09CloudSe
@@ -797,59 +797,40 @@
 %7B%0A%09%09
-mockHttpClient = Mockito.mock(SimpleHttpClient.clas
+MockitoAnnotations.initMocks(thi
 s);%0A
@@ -1085,104 +1085,104 @@
 %7B%0A%09%09
-SimpleHttpResponse resp = new SimpleHttpResponse();%0A%09%09resp.setStatusCode(200);%0A%09%09resp.setContent
+when(mockResponse.getStatusCode()).thenReturn(200);%0A%09%09when(mockResponse.getContent()).thenReturn
 (%22s%22
@@ -1248,28 +1248,36 @@
 .thenReturn(
-resp
+mockResponse
 );%0A%09%09assertE
@@ -1282,20 +1282,28 @@
 tEquals(
-resp
+mockResponse
 .getCont
@@ -1487,80 +1487,53 @@
 %7B%0A%09%09
-SimpleHttpResponse resp = new SimpleHttpResponse();%0A%09%09resp.setStatusCode
+when(mockResponse.getStatusCode()).thenReturn
 (507
@@ -1595,36 +1595,44 @@
 s))).thenReturn(
-resp
+mockResponse
 );%0A%09%09try %7B%0A%09%09%09cl
@@ -1909,80 +1909,53 @@
 %7B%0A%09%09
-SimpleHttpResponse resp = new SimpleHttpResponse();%0A%09%09resp.setStatusCode
+when(mockResponse.getStatusCode()).thenReturn
 (504
@@ -2029,12 +2029,20 @@
 urn(
-resp
+mockResponse
 );%0A%09

update test