package com.bluetangstudio.searchcloud.client;
import com.bluetangstudio.searchcloud.client.exception.ServiceException;
import com.bluetangstudio.searchcloud.client.http.DefaultHttpClientFactory;
import com.bluetangstudio.searchcloud.client.model.Document;
import com.bluetangstudio.searchcloud.client.model.DocumentId;
import com.bluetangstudio.searchcloud.client.model.UserId;
import com.bluetangstudio.searchcloud.client.model.UserModelId;
import com.bluetangstudio.searchcloud.client.request.GetDocumentRequest;
import com.bluetangstudio.searchcloud.client.test.GetDocumentService;
import com.sun.jersey.test.framework.LowLevelAppDescriptor;
import com.sun.jersey.test.framework.spi.container.TestContainer;
import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
import com.sun.jersey.test.framework.spi.container.grizzly2.GrizzlyTestContainerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
/**
*
*/
public class RestSearchCloudClientTest {
private static int port = 8889;
private TestContainer testContainer = null;
@BeforeClass
public void startupFakeServer() {
/**
* Setup fake service
*/
LowLevelAppDescriptor.Builder builder = new LowLevelAppDescriptor.Builder(
GetDocumentService.class,
org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider.class
);
// :~)
/**
* Start container
*/
TestContainerFactory containerFactory = new GrizzlyTestContainerFactory();
UriBuilder uriBuilder = UriBuilder.fromUri(URI.create("http://localhost:" + port));
testContainer = containerFactory.create(
uriBuilder.build(), builder.build()
);
testContainer.start();
// :~)
}
@AfterClass
public void stopFakeServer() {
if (testContainer != null) {
testContainer.stop();
}
}
@Test
public void testGetDocument() throws IOException, ServiceException {
RestSearchCloudClient client = new RestSearchCloudClient(
new DefaultHttpClientFactory(),
URI.create("http://localhost:" + port), "apikey"
);
UserModelId modelId = new UserModelId(new UserId("test"), "testGetDocument");
DocumentId docId = new DocumentId(10);
Document document = client.getDocument(new GetDocumentRequest(modelId, docId));
Assert.assertNotNull(document);
Assert.assertEquals(document.getId(), docId);
}
}